bio_ndef.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/asn1.h>
  10. #include <openssl/asn1t.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/err.h>
  13. #include <stdio.h>
  14. /* Experimental NDEF ASN1 BIO support routines */
  15. /*
  16. * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
  17. * then any data written through the BIO will end up translated to
  18. * appropriate format on the fly. The data is streamed out and does *not*
  19. * need to be all held in memory at once. When the BIO is flushed the output
  20. * is finalized and any signatures etc written out. The BIO is a 'proper'
  21. * BIO and can handle non blocking I/O correctly. The usage is simple. The
  22. * implementation is *not*...
  23. */
  24. /* BIO support data stored in the ASN1 BIO ex_arg */
  25. typedef struct ndef_aux_st {
  26. /* ASN1 structure this BIO refers to */
  27. ASN1_VALUE *val;
  28. const ASN1_ITEM *it;
  29. /* Top of the BIO chain */
  30. BIO *ndef_bio;
  31. /* Output BIO */
  32. BIO *out;
  33. /* Boundary where content is inserted */
  34. unsigned char **boundary;
  35. /* DER buffer start */
  36. unsigned char *derbuf;
  37. } NDEF_SUPPORT;
  38. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  39. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  40. void *parg);
  41. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  42. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  43. void *parg);
  44. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  45. {
  46. NDEF_SUPPORT *ndef_aux = NULL;
  47. BIO *asn_bio = NULL;
  48. const ASN1_AUX *aux = it->funcs;
  49. ASN1_STREAM_ARG sarg;
  50. if (!aux || !aux->asn1_cb) {
  51. ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
  52. return NULL;
  53. }
  54. ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
  55. asn_bio = BIO_new(BIO_f_asn1());
  56. if (ndef_aux == NULL || asn_bio == NULL)
  57. goto err;
  58. /* ASN1 bio needs to be next to output BIO */
  59. out = BIO_push(asn_bio, out);
  60. if (out == NULL)
  61. goto err;
  62. BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
  63. BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
  64. /*
  65. * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
  66. * needs.
  67. */
  68. sarg.out = out;
  69. sarg.ndef_bio = NULL;
  70. sarg.boundary = NULL;
  71. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
  72. goto err;
  73. ndef_aux->val = val;
  74. ndef_aux->it = it;
  75. ndef_aux->ndef_bio = sarg.ndef_bio;
  76. ndef_aux->boundary = sarg.boundary;
  77. ndef_aux->out = out;
  78. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  79. return sarg.ndef_bio;
  80. err:
  81. BIO_free(asn_bio);
  82. OPENSSL_free(ndef_aux);
  83. return NULL;
  84. }
  85. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  86. {
  87. NDEF_SUPPORT *ndef_aux;
  88. unsigned char *p;
  89. int derlen;
  90. if (!parg)
  91. return 0;
  92. ndef_aux = *(NDEF_SUPPORT **)parg;
  93. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  94. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  95. ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
  96. return 0;
  97. }
  98. ndef_aux->derbuf = p;
  99. *pbuf = p;
  100. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  101. if (!*ndef_aux->boundary)
  102. return 0;
  103. *plen = *ndef_aux->boundary - *pbuf;
  104. return 1;
  105. }
  106. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  107. void *parg)
  108. {
  109. NDEF_SUPPORT *ndef_aux;
  110. if (!parg)
  111. return 0;
  112. ndef_aux = *(NDEF_SUPPORT **)parg;
  113. OPENSSL_free(ndef_aux->derbuf);
  114. ndef_aux->derbuf = NULL;
  115. *pbuf = NULL;
  116. *plen = 0;
  117. return 1;
  118. }
  119. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  120. void *parg)
  121. {
  122. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  123. if (!ndef_prefix_free(b, pbuf, plen, parg))
  124. return 0;
  125. OPENSSL_free(*pndef_aux);
  126. *pndef_aux = NULL;
  127. return 1;
  128. }
  129. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  130. {
  131. NDEF_SUPPORT *ndef_aux;
  132. unsigned char *p;
  133. int derlen;
  134. const ASN1_AUX *aux;
  135. ASN1_STREAM_ARG sarg;
  136. if (!parg)
  137. return 0;
  138. ndef_aux = *(NDEF_SUPPORT **)parg;
  139. aux = ndef_aux->it->funcs;
  140. /* Finalize structures */
  141. sarg.ndef_bio = ndef_aux->ndef_bio;
  142. sarg.out = ndef_aux->out;
  143. sarg.boundary = ndef_aux->boundary;
  144. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  145. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  146. return 0;
  147. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  148. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  149. ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
  150. return 0;
  151. }
  152. ndef_aux->derbuf = p;
  153. *pbuf = p;
  154. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  155. if (!*ndef_aux->boundary)
  156. return 0;
  157. *pbuf = *ndef_aux->boundary;
  158. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  159. return 1;
  160. }