bio_ndef.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. ERR_raise(ERR_LIB_ASN1, 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 == NULL)
  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. return 0;
  123. ndef_aux->derbuf = p;
  124. *pbuf = p;
  125. ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  126. if (*ndef_aux->boundary == NULL)
  127. return 0;
  128. *plen = *ndef_aux->boundary - *pbuf;
  129. return 1;
  130. }
  131. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  132. void *parg)
  133. {
  134. NDEF_SUPPORT *ndef_aux;
  135. if (parg == NULL)
  136. return 0;
  137. ndef_aux = *(NDEF_SUPPORT **)parg;
  138. if (ndef_aux == NULL)
  139. return 0;
  140. OPENSSL_free(ndef_aux->derbuf);
  141. ndef_aux->derbuf = NULL;
  142. *pbuf = NULL;
  143. *plen = 0;
  144. return 1;
  145. }
  146. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  147. void *parg)
  148. {
  149. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  150. if (!ndef_prefix_free(b, pbuf, plen, parg))
  151. return 0;
  152. OPENSSL_free(*pndef_aux);
  153. *pndef_aux = NULL;
  154. return 1;
  155. }
  156. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  157. {
  158. NDEF_SUPPORT *ndef_aux;
  159. unsigned char *p;
  160. int derlen;
  161. const ASN1_AUX *aux;
  162. ASN1_STREAM_ARG sarg;
  163. if (parg == NULL)
  164. return 0;
  165. ndef_aux = *(NDEF_SUPPORT **)parg;
  166. aux = ndef_aux->it->funcs;
  167. /* Finalize structures */
  168. sarg.ndef_bio = ndef_aux->ndef_bio;
  169. sarg.out = ndef_aux->out;
  170. sarg.boundary = ndef_aux->boundary;
  171. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  172. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  173. return 0;
  174. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  175. if (derlen < 0)
  176. return 0;
  177. if ((p = OPENSSL_malloc(derlen)) == NULL)
  178. return 0;
  179. ndef_aux->derbuf = p;
  180. *pbuf = p;
  181. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  182. if (*ndef_aux->boundary == NULL)
  183. return 0;
  184. *pbuf = *ndef_aux->boundary;
  185. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  186. return 1;
  187. }