bio_ndef.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* bio_ndef.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. */
  55. #include <openssl/asn1.h>
  56. #include <openssl/asn1t.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/err.h>
  59. #include <stdio.h>
  60. /* Experimental NDEF ASN1 BIO support routines */
  61. /*
  62. * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
  63. * then any data written through the BIO will end up translated to
  64. * approptiate format on the fly. The data is streamed out and does *not*
  65. * need to be all held in memory at once. When the BIO is flushed the output
  66. * is finalized and any signatures etc written out. The BIO is a 'proper'
  67. * BIO and can handle non blocking I/O correctly. The usage is simple. The
  68. * implementation is *not*...
  69. */
  70. /* BIO support data stored in the ASN1 BIO ex_arg */
  71. typedef struct ndef_aux_st {
  72. /* ASN1 structure this BIO refers to */
  73. ASN1_VALUE *val;
  74. const ASN1_ITEM *it;
  75. /* Top of the BIO chain */
  76. BIO *ndef_bio;
  77. /* Output BIO */
  78. BIO *out;
  79. /* Boundary where content is inserted */
  80. unsigned char **boundary;
  81. /* DER buffer start */
  82. unsigned char *derbuf;
  83. } NDEF_SUPPORT;
  84. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  85. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  86. void *parg);
  87. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  88. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  89. void *parg);
  90. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  91. {
  92. NDEF_SUPPORT *ndef_aux = NULL;
  93. BIO *asn_bio = NULL;
  94. const ASN1_AUX *aux = it->funcs;
  95. ASN1_STREAM_ARG sarg;
  96. if (!aux || !aux->asn1_cb) {
  97. ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
  98. return NULL;
  99. }
  100. ndef_aux = OPENSSL_malloc(sizeof(*ndef_aux));
  101. asn_bio = BIO_new(BIO_f_asn1());
  102. /* ASN1 bio needs to be next to output BIO */
  103. out = BIO_push(asn_bio, out);
  104. if (!ndef_aux || !asn_bio || !out)
  105. goto err;
  106. BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
  107. BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
  108. /*
  109. * Now let callback prepend any digest, cipher etc BIOs ASN1 structure
  110. * needs.
  111. */
  112. sarg.out = out;
  113. sarg.ndef_bio = NULL;
  114. sarg.boundary = NULL;
  115. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
  116. goto err;
  117. ndef_aux->val = val;
  118. ndef_aux->it = it;
  119. ndef_aux->ndef_bio = sarg.ndef_bio;
  120. ndef_aux->boundary = sarg.boundary;
  121. ndef_aux->out = out;
  122. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  123. return sarg.ndef_bio;
  124. err:
  125. BIO_free(asn_bio);
  126. OPENSSL_free(ndef_aux);
  127. return NULL;
  128. }
  129. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  130. {
  131. NDEF_SUPPORT *ndef_aux;
  132. unsigned char *p;
  133. int derlen;
  134. if (!parg)
  135. return 0;
  136. ndef_aux = *(NDEF_SUPPORT **)parg;
  137. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  138. p = OPENSSL_malloc(derlen);
  139. if (!p)
  140. return 0;
  141. ndef_aux->derbuf = p;
  142. *pbuf = p;
  143. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  144. if (!*ndef_aux->boundary)
  145. return 0;
  146. *plen = *ndef_aux->boundary - *pbuf;
  147. return 1;
  148. }
  149. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  150. void *parg)
  151. {
  152. NDEF_SUPPORT *ndef_aux;
  153. if (!parg)
  154. return 0;
  155. ndef_aux = *(NDEF_SUPPORT **)parg;
  156. OPENSSL_free(ndef_aux->derbuf);
  157. ndef_aux->derbuf = NULL;
  158. *pbuf = NULL;
  159. *plen = 0;
  160. return 1;
  161. }
  162. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  163. void *parg)
  164. {
  165. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  166. if (!ndef_prefix_free(b, pbuf, plen, parg))
  167. return 0;
  168. OPENSSL_free(*pndef_aux);
  169. *pndef_aux = NULL;
  170. return 1;
  171. }
  172. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  173. {
  174. NDEF_SUPPORT *ndef_aux;
  175. unsigned char *p;
  176. int derlen;
  177. const ASN1_AUX *aux;
  178. ASN1_STREAM_ARG sarg;
  179. if (!parg)
  180. return 0;
  181. ndef_aux = *(NDEF_SUPPORT **)parg;
  182. aux = ndef_aux->it->funcs;
  183. /* Finalize structures */
  184. sarg.ndef_bio = ndef_aux->ndef_bio;
  185. sarg.out = ndef_aux->out;
  186. sarg.boundary = ndef_aux->boundary;
  187. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  188. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  189. return 0;
  190. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  191. p = OPENSSL_malloc(derlen);
  192. if (!p)
  193. return 0;
  194. ndef_aux->derbuf = p;
  195. *pbuf = p;
  196. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  197. if (!*ndef_aux->boundary)
  198. return 0;
  199. *pbuf = *ndef_aux->boundary;
  200. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  201. return 1;
  202. }