Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

252 рядки
7.9 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. /*
  64. * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
  65. * then any data written through the BIO will end up translated to
  66. * approptiate format on the fly. The data is streamed out and does *not*
  67. * need to be all held in memory at once. When the BIO is flushed the output
  68. * is finalized and any signatures etc written out. The BIO is a 'proper'
  69. * BIO and can handle non blocking I/O correctly. The usage is simple. The
  70. * implementation is *not*...
  71. */
  72. /* BIO support data stored in the ASN1 BIO ex_arg */
  73. typedef struct ndef_aux_st {
  74. /* ASN1 structure this BIO refers to */
  75. ASN1_VALUE *val;
  76. const ASN1_ITEM *it;
  77. /* Top of the BIO chain */
  78. BIO *ndef_bio;
  79. /* Output BIO */
  80. BIO *out;
  81. /* Boundary where content is inserted */
  82. unsigned char **boundary;
  83. /* DER buffer start */
  84. unsigned char *derbuf;
  85. } NDEF_SUPPORT;
  86. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  87. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  88. void *parg);
  89. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  90. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  91. void *parg);
  92. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  93. {
  94. NDEF_SUPPORT *ndef_aux = NULL;
  95. BIO *asn_bio = NULL;
  96. const ASN1_AUX *aux = it->funcs;
  97. ASN1_STREAM_ARG sarg;
  98. if (!aux || !aux->asn1_cb) {
  99. OPENSSL_PUT_ERROR(ASN1, ASN1_R_STREAMING_NOT_SUPPORTED);
  100. return NULL;
  101. }
  102. ndef_aux = OPENSSL_malloc(sizeof(NDEF_SUPPORT));
  103. asn_bio = BIO_new(BIO_f_asn1());
  104. /* ASN1 bio needs to be next to output BIO */
  105. out = BIO_push(asn_bio, out);
  106. if (!ndef_aux || !asn_bio || !out)
  107. goto err;
  108. BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
  109. BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
  110. /*
  111. * Now let callback prepend any digest, cipher etc BIOs ASN1 structure
  112. * needs.
  113. */
  114. sarg.out = out;
  115. sarg.ndef_bio = NULL;
  116. sarg.boundary = NULL;
  117. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
  118. goto err;
  119. ndef_aux->val = val;
  120. ndef_aux->it = it;
  121. ndef_aux->ndef_bio = sarg.ndef_bio;
  122. ndef_aux->boundary = sarg.boundary;
  123. ndef_aux->out = out;
  124. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  125. return sarg.ndef_bio;
  126. err:
  127. if (asn_bio)
  128. BIO_free(asn_bio);
  129. if (ndef_aux)
  130. OPENSSL_free(ndef_aux);
  131. return NULL;
  132. }
  133. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  134. {
  135. NDEF_SUPPORT *ndef_aux;
  136. unsigned char *p;
  137. int derlen;
  138. if (!parg)
  139. return 0;
  140. ndef_aux = *(NDEF_SUPPORT **)parg;
  141. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  142. p = OPENSSL_malloc(derlen);
  143. if (p == NULL)
  144. return 0;
  145. ndef_aux->derbuf = p;
  146. *pbuf = p;
  147. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  148. if (!*ndef_aux->boundary)
  149. return 0;
  150. *plen = *ndef_aux->boundary - *pbuf;
  151. return 1;
  152. }
  153. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  154. void *parg)
  155. {
  156. NDEF_SUPPORT *ndef_aux;
  157. if (!parg)
  158. return 0;
  159. ndef_aux = *(NDEF_SUPPORT **)parg;
  160. if (ndef_aux->derbuf)
  161. OPENSSL_free(ndef_aux->derbuf);
  162. ndef_aux->derbuf = NULL;
  163. *pbuf = NULL;
  164. *plen = 0;
  165. return 1;
  166. }
  167. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  168. 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. if (p == NULL)
  198. return 0;
  199. ndef_aux->derbuf = p;
  200. *pbuf = p;
  201. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  202. if (!*ndef_aux->boundary)
  203. return 0;
  204. *pbuf = *ndef_aux->boundary;
  205. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  206. return 1;
  207. }