bio_ndef.c 6.9 KB

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