bio_ndef.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_SUPPORT));
  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. ndef_aux->derbuf = NULL;
  123. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  124. return sarg.ndef_bio;
  125. err:
  126. if (asn_bio)
  127. BIO_free(asn_bio);
  128. if (ndef_aux)
  129. OPENSSL_free(ndef_aux);
  130. return NULL;
  131. }
  132. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  133. {
  134. NDEF_SUPPORT *ndef_aux;
  135. unsigned char *p;
  136. int derlen;
  137. if (!parg)
  138. return 0;
  139. ndef_aux = *(NDEF_SUPPORT **)parg;
  140. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  141. p = OPENSSL_malloc(derlen);
  142. if (!p)
  143. return 0;
  144. ndef_aux->derbuf = p;
  145. *pbuf = p;
  146. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  147. if (!*ndef_aux->boundary)
  148. return 0;
  149. *plen = *ndef_aux->boundary - *pbuf;
  150. return 1;
  151. }
  152. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  153. void *parg)
  154. {
  155. NDEF_SUPPORT *ndef_aux;
  156. if (!parg)
  157. return 0;
  158. ndef_aux = *(NDEF_SUPPORT **)parg;
  159. if (ndef_aux->derbuf)
  160. OPENSSL_free(ndef_aux->derbuf);
  161. ndef_aux->derbuf = NULL;
  162. *pbuf = NULL;
  163. *plen = 0;
  164. return 1;
  165. }
  166. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  167. void *parg)
  168. {
  169. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  170. if (!ndef_prefix_free(b, pbuf, plen, parg))
  171. return 0;
  172. OPENSSL_free(*pndef_aux);
  173. *pndef_aux = NULL;
  174. return 1;
  175. }
  176. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  177. {
  178. NDEF_SUPPORT *ndef_aux;
  179. unsigned char *p;
  180. int derlen;
  181. const ASN1_AUX *aux;
  182. ASN1_STREAM_ARG sarg;
  183. if (!parg)
  184. return 0;
  185. ndef_aux = *(NDEF_SUPPORT **)parg;
  186. aux = ndef_aux->it->funcs;
  187. /* Finalize structures */
  188. sarg.ndef_bio = ndef_aux->ndef_bio;
  189. sarg.out = ndef_aux->out;
  190. sarg.boundary = ndef_aux->boundary;
  191. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  192. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  193. return 0;
  194. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  195. p = OPENSSL_malloc(derlen);
  196. if (!p)
  197. return 0;
  198. ndef_aux->derbuf = p;
  199. *pbuf = p;
  200. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  201. if (!*ndef_aux->boundary)
  202. return 0;
  203. *pbuf = *ndef_aux->boundary;
  204. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  205. return 1;
  206. }