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.
 
 
 
 
 
 

287 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 <limits.h>
  58. #include <openssl/buf.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
  62. #ifndef NO_OLD_ASN1
  63. #ifndef OPENSSL_NO_FP_API
  64. void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
  65. {
  66. BIO *b;
  67. void *ret;
  68. if ((b=BIO_new(BIO_s_file())) == NULL)
  69. {
  70. OPENSSL_PUT_ERROR(ASN1, ASN1_d2i_fp, ERR_R_BUF_LIB);
  71. return(NULL);
  72. }
  73. BIO_set_fp(b,in,BIO_NOCLOSE);
  74. ret=ASN1_d2i_bio(xnew,d2i,b,x);
  75. BIO_free(b);
  76. return(ret);
  77. }
  78. #endif
  79. void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
  80. {
  81. BUF_MEM *b = NULL;
  82. const unsigned char *p;
  83. void *ret=NULL;
  84. int len;
  85. len = asn1_d2i_read_bio(in, &b);
  86. if(len < 0) goto err;
  87. p=(unsigned char *)b->data;
  88. ret=d2i(x,&p,len);
  89. err:
  90. if (b != NULL) BUF_MEM_free(b);
  91. return(ret);
  92. }
  93. #endif
  94. void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
  95. {
  96. BUF_MEM *b = NULL;
  97. const unsigned char *p;
  98. void *ret=NULL;
  99. int len;
  100. len = asn1_d2i_read_bio(in, &b);
  101. if(len < 0) goto err;
  102. p=(const unsigned char *)b->data;
  103. ret=ASN1_item_d2i(x,&p,len, it);
  104. err:
  105. if (b != NULL) BUF_MEM_free(b);
  106. return(ret);
  107. }
  108. #ifndef OPENSSL_NO_FP_API
  109. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
  110. {
  111. BIO *b;
  112. char *ret;
  113. if ((b=BIO_new(BIO_s_file())) == NULL)
  114. {
  115. OPENSSL_PUT_ERROR(ASN1, ASN1_item_d2i_fp, ERR_R_BUF_LIB);
  116. return(NULL);
  117. }
  118. BIO_set_fp(b,in,BIO_NOCLOSE);
  119. ret=ASN1_item_d2i_bio(it,b,x);
  120. BIO_free(b);
  121. return(ret);
  122. }
  123. #endif
  124. #define HEADER_SIZE 8
  125. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  126. {
  127. BUF_MEM *b;
  128. unsigned char *p;
  129. int i;
  130. ASN1_const_CTX c;
  131. size_t want=HEADER_SIZE;
  132. int eos=0;
  133. size_t off=0;
  134. size_t len=0;
  135. b=BUF_MEM_new();
  136. if (b == NULL)
  137. {
  138. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ERR_R_MALLOC_FAILURE);
  139. return -1;
  140. }
  141. ERR_clear_error();
  142. for (;;)
  143. {
  144. if (want >= (len-off))
  145. {
  146. want-=(len-off);
  147. if (len + want < len || !BUF_MEM_grow_clean(b,len+want))
  148. {
  149. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ERR_R_MALLOC_FAILURE);
  150. goto err;
  151. }
  152. i=BIO_read(in,&(b->data[len]),want);
  153. if ((i < 0) && ((len-off) == 0))
  154. {
  155. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_NOT_ENOUGH_DATA);
  156. goto err;
  157. }
  158. if (i > 0)
  159. {
  160. if (len+i < len)
  161. {
  162. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_TOO_LONG);
  163. goto err;
  164. }
  165. len+=i;
  166. }
  167. }
  168. /* else data already loaded */
  169. p=(unsigned char *)&(b->data[off]);
  170. c.p=p;
  171. c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
  172. len-off);
  173. if (c.inf & 0x80)
  174. {
  175. unsigned long e;
  176. e=ERR_GET_REASON(ERR_peek_error());
  177. if (e != ASN1_R_TOO_LONG)
  178. goto err;
  179. else
  180. ERR_clear_error(); /* clear error */
  181. }
  182. i=c.p-p;/* header length */
  183. off+=i; /* end of data */
  184. if (c.inf & 1)
  185. {
  186. /* no data body so go round again */
  187. eos++;
  188. if (eos < 0)
  189. {
  190. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_HEADER_TOO_LONG);
  191. goto err;
  192. }
  193. want=HEADER_SIZE;
  194. }
  195. else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
  196. {
  197. /* eos value, so go back and read another header */
  198. eos--;
  199. if (eos <= 0)
  200. break;
  201. else
  202. want=HEADER_SIZE;
  203. }
  204. else
  205. {
  206. /* suck in c.slen bytes of data */
  207. want=c.slen;
  208. if (want > (len-off))
  209. {
  210. want-=(len-off);
  211. if (want > INT_MAX /* BIO_read takes an int length */ ||
  212. len+want < len)
  213. {
  214. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_TOO_LONG);
  215. goto err;
  216. }
  217. if (!BUF_MEM_grow_clean(b,len+want))
  218. {
  219. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ERR_R_MALLOC_FAILURE);
  220. goto err;
  221. }
  222. while (want > 0)
  223. {
  224. i=BIO_read(in,&(b->data[len]),want);
  225. if (i <= 0)
  226. {
  227. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_NOT_ENOUGH_DATA);
  228. goto err;
  229. }
  230. /* This can't overflow because
  231. * |len+want| didn't overflow. */
  232. len+=i;
  233. want-=i;
  234. }
  235. }
  236. if (off + c.slen < off)
  237. {
  238. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_TOO_LONG);
  239. goto err;
  240. }
  241. off+=c.slen;
  242. if (eos <= 0)
  243. {
  244. break;
  245. }
  246. else
  247. want=HEADER_SIZE;
  248. }
  249. }
  250. if (off > INT_MAX)
  251. {
  252. OPENSSL_PUT_ERROR(ASN1, asn1_d2i_read_bio, ASN1_R_TOO_LONG);
  253. goto err;
  254. }
  255. *pb = b;
  256. return off;
  257. err:
  258. if (b != NULL) BUF_MEM_free(b);
  259. return -1;
  260. }