You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

255 regels
7.5 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <assert.h>
  58. #include <openssl/asn1t.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.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. OPENSSL_PUT_ERROR(ASN1, 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. if (p == NULL)
  148. return 0;
  149. ndef_aux->derbuf = p;
  150. *pbuf = p;
  151. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  152. if (!*ndef_aux->boundary)
  153. return 0;
  154. *plen = *ndef_aux->boundary - *pbuf;
  155. return 1;
  156. }
  157. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  158. {
  159. NDEF_SUPPORT *ndef_aux;
  160. if (!parg)
  161. return 0;
  162. ndef_aux = *(NDEF_SUPPORT **)parg;
  163. if (ndef_aux->derbuf)
  164. OPENSSL_free(ndef_aux->derbuf);
  165. ndef_aux->derbuf = NULL;
  166. *pbuf = NULL;
  167. *plen = 0;
  168. return 1;
  169. }
  170. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  171. {
  172. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  173. if (!ndef_prefix_free(b, pbuf, plen, parg))
  174. return 0;
  175. OPENSSL_free(*pndef_aux);
  176. *pndef_aux = NULL;
  177. return 1;
  178. }
  179. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  180. {
  181. NDEF_SUPPORT *ndef_aux;
  182. unsigned char *p;
  183. int derlen;
  184. const ASN1_AUX *aux;
  185. ASN1_STREAM_ARG sarg;
  186. if (!parg)
  187. return 0;
  188. ndef_aux = *(NDEF_SUPPORT **)parg;
  189. aux = ndef_aux->it->funcs;
  190. /* Finalize structures */
  191. sarg.ndef_bio = ndef_aux->ndef_bio;
  192. sarg.out = ndef_aux->out;
  193. sarg.boundary = ndef_aux->boundary;
  194. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  195. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  196. return 0;
  197. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  198. p = OPENSSL_malloc(derlen);
  199. if (p == NULL)
  200. return 0;
  201. ndef_aux->derbuf = p;
  202. *pbuf = p;
  203. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  204. if (!*ndef_aux->boundary)
  205. return 0;
  206. *pbuf = *ndef_aux->boundary;
  207. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  208. return 1;
  209. }