bio_ndef.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2008-2023 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. /*
  45. * On success, the returned BIO owns the input BIO as part of its BIO chain.
  46. * On failure, NULL is returned and the input BIO is owned by the caller.
  47. *
  48. * Unfortunately cannot constify this due to CMS_stream() and PKCS7_stream()
  49. */
  50. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  51. {
  52. NDEF_SUPPORT *ndef_aux = NULL;
  53. BIO *asn_bio = NULL;
  54. const ASN1_AUX *aux = it->funcs;
  55. ASN1_STREAM_ARG sarg;
  56. BIO *pop_bio = NULL;
  57. if (!aux || !aux->asn1_cb) {
  58. ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
  59. return NULL;
  60. }
  61. ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
  62. asn_bio = BIO_new(BIO_f_asn1());
  63. if (ndef_aux == NULL || asn_bio == NULL)
  64. goto err;
  65. /* ASN1 bio needs to be next to output BIO */
  66. out = BIO_push(asn_bio, out);
  67. if (out == NULL)
  68. goto err;
  69. pop_bio = asn_bio;
  70. if (BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free) <= 0
  71. || BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free) <= 0
  72. || BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux) <= 0)
  73. goto err;
  74. /*
  75. * Now let the callback prepend any digest, cipher, etc., that the BIO's
  76. * ASN1 structure needs.
  77. */
  78. sarg.out = out;
  79. sarg.ndef_bio = NULL;
  80. sarg.boundary = NULL;
  81. /*
  82. * The asn1_cb(), must not have mutated asn_bio on error, leaving it in the
  83. * middle of some partially built, but not returned BIO chain.
  84. */
  85. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0) {
  86. /*
  87. * ndef_aux is now owned by asn_bio so we must not free it in the err
  88. * clean up block
  89. */
  90. ndef_aux = NULL;
  91. goto err;
  92. }
  93. /*
  94. * We must not fail now because the callback has prepended additional
  95. * BIOs to the chain
  96. */
  97. ndef_aux->val = val;
  98. ndef_aux->it = it;
  99. ndef_aux->ndef_bio = sarg.ndef_bio;
  100. ndef_aux->boundary = sarg.boundary;
  101. ndef_aux->out = out;
  102. return sarg.ndef_bio;
  103. err:
  104. /* BIO_pop() is NULL safe */
  105. (void)BIO_pop(pop_bio);
  106. BIO_free(asn_bio);
  107. OPENSSL_free(ndef_aux);
  108. return NULL;
  109. }
  110. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  111. {
  112. NDEF_SUPPORT *ndef_aux;
  113. unsigned char *p;
  114. int derlen;
  115. if (!parg)
  116. return 0;
  117. ndef_aux = *(NDEF_SUPPORT **)parg;
  118. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  119. if (derlen < 0)
  120. return 0;
  121. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  122. ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
  123. return 0;
  124. }
  125. ndef_aux->derbuf = p;
  126. *pbuf = p;
  127. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  128. if (!*ndef_aux->boundary)
  129. return 0;
  130. *plen = *ndef_aux->boundary - *pbuf;
  131. return 1;
  132. }
  133. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  134. void *parg)
  135. {
  136. NDEF_SUPPORT *ndef_aux;
  137. if (!parg)
  138. return 0;
  139. ndef_aux = *(NDEF_SUPPORT **)parg;
  140. if (ndef_aux == NULL)
  141. return 0;
  142. OPENSSL_free(ndef_aux->derbuf);
  143. ndef_aux->derbuf = NULL;
  144. *pbuf = NULL;
  145. *plen = 0;
  146. return 1;
  147. }
  148. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  149. void *parg)
  150. {
  151. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  152. if (!ndef_prefix_free(b, pbuf, plen, parg))
  153. return 0;
  154. OPENSSL_free(*pndef_aux);
  155. *pndef_aux = NULL;
  156. return 1;
  157. }
  158. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  159. {
  160. NDEF_SUPPORT *ndef_aux;
  161. unsigned char *p;
  162. int derlen;
  163. const ASN1_AUX *aux;
  164. ASN1_STREAM_ARG sarg;
  165. if (!parg)
  166. return 0;
  167. ndef_aux = *(NDEF_SUPPORT **)parg;
  168. aux = ndef_aux->it->funcs;
  169. /* Finalize structures */
  170. sarg.ndef_bio = ndef_aux->ndef_bio;
  171. sarg.out = ndef_aux->out;
  172. sarg.boundary = ndef_aux->boundary;
  173. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  174. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  175. return 0;
  176. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  177. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  178. ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
  179. return 0;
  180. }
  181. ndef_aux->derbuf = p;
  182. *pbuf = p;
  183. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  184. if (!*ndef_aux->boundary)
  185. return 0;
  186. *pbuf = *ndef_aux->boundary;
  187. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  188. return 1;
  189. }