25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

298 satır
9.7 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. OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
  70. return (NULL);
  71. }
  72. BIO_set_fp(b, in, BIO_NOCLOSE);
  73. ret = ASN1_d2i_bio(xnew, d2i, b, x);
  74. BIO_free(b);
  75. return (ret);
  76. }
  77. # endif
  78. void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
  79. {
  80. BUF_MEM *b = NULL;
  81. const unsigned char *p;
  82. void *ret = NULL;
  83. int len;
  84. len = asn1_d2i_read_bio(in, &b);
  85. if (len < 0)
  86. goto err;
  87. p = (unsigned char *)b->data;
  88. ret = d2i(x, &p, len);
  89. err:
  90. if (b != NULL)
  91. BUF_MEM_free(b);
  92. return (ret);
  93. }
  94. #endif
  95. void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
  96. {
  97. BUF_MEM *b = NULL;
  98. const unsigned char *p;
  99. void *ret = NULL;
  100. int len;
  101. len = asn1_d2i_read_bio(in, &b);
  102. if (len < 0)
  103. goto err;
  104. p = (const unsigned char *)b->data;
  105. ret = ASN1_item_d2i(x, &p, len, it);
  106. err:
  107. if (b != NULL)
  108. BUF_MEM_free(b);
  109. return (ret);
  110. }
  111. #ifndef OPENSSL_NO_FP_API
  112. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
  113. {
  114. BIO *b;
  115. char *ret;
  116. if ((b = BIO_new(BIO_s_file())) == NULL) {
  117. OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
  118. return (NULL);
  119. }
  120. BIO_set_fp(b, in, BIO_NOCLOSE);
  121. ret = ASN1_item_d2i_bio(it, b, x);
  122. BIO_free(b);
  123. return (ret);
  124. }
  125. #endif
  126. typedef struct asn1_const_ctx_st
  127. {
  128. const unsigned char *p;/* work char pointer */
  129. int eos; /* end of sequence read for indefinite encoding */
  130. int error; /* error code to use when returning an error */
  131. int inf; /* constructed if 0x20, indefinite is 0x21 */
  132. int tag; /* tag from last 'get object' */
  133. int xclass; /* class from last 'get object' */
  134. long slen; /* length of last 'get object' */
  135. const unsigned char *max; /* largest value of p allowed */
  136. const unsigned char *q;/* temporary variable */
  137. const unsigned char **pp;/* variable */
  138. int line; /* used in error processing */
  139. } ASN1_const_CTX;
  140. #define HEADER_SIZE 8
  141. #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
  142. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  143. {
  144. BUF_MEM *b;
  145. unsigned char *p;
  146. int i;
  147. ASN1_const_CTX c;
  148. size_t want = HEADER_SIZE;
  149. int eos = 0;
  150. size_t off = 0;
  151. size_t len = 0;
  152. b = BUF_MEM_new();
  153. if (b == NULL) {
  154. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  155. return -1;
  156. }
  157. ERR_clear_error();
  158. for (;;) {
  159. if (want >= (len - off)) {
  160. want -= (len - off);
  161. if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
  162. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  163. goto err;
  164. }
  165. i = BIO_read(in, &(b->data[len]), want);
  166. if ((i < 0) && ((len - off) == 0)) {
  167. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
  168. goto err;
  169. }
  170. if (i > 0) {
  171. if (len + i < len) {
  172. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  173. goto err;
  174. }
  175. len += i;
  176. }
  177. }
  178. /* else data already loaded */
  179. p = (unsigned char *)&(b->data[off]);
  180. c.p = p;
  181. c.inf = ASN1_get_object(&(c.p), &(c.slen), &(c.tag), &(c.xclass),
  182. len - off);
  183. if (c.inf & 0x80) {
  184. uint32_t e;
  185. e = ERR_GET_REASON(ERR_peek_error());
  186. if (e != ASN1_R_TOO_LONG)
  187. goto err;
  188. else
  189. ERR_clear_error(); /* clear error */
  190. }
  191. i = c.p - p; /* header length */
  192. off += i; /* end of data */
  193. if (c.inf & 1) {
  194. /* no data body so go round again */
  195. eos++;
  196. if (eos < 0) {
  197. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
  198. goto err;
  199. }
  200. want = HEADER_SIZE;
  201. } else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC)) {
  202. /* eos value, so go back and read another header */
  203. eos--;
  204. if (eos <= 0)
  205. break;
  206. else
  207. want = HEADER_SIZE;
  208. } else {
  209. /* suck in c.slen bytes of data */
  210. want = c.slen;
  211. if (want > (len - off)) {
  212. size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
  213. want -= (len - off);
  214. if (want > INT_MAX /* BIO_read takes an int length */ ||
  215. len + want < len) {
  216. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  217. goto err;
  218. }
  219. while (want > 0) {
  220. /*
  221. * Read content in chunks of increasing size
  222. * so we can return an error for EOF without
  223. * having to allocate the entire content length
  224. * in one go.
  225. */
  226. size_t chunk = want > chunk_max ? chunk_max : want;
  227. if (!BUF_MEM_grow_clean(b, len + chunk)) {
  228. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  229. goto err;
  230. }
  231. want -= chunk;
  232. while (chunk > 0) {
  233. i = BIO_read(in, &(b->data[len]), chunk);
  234. if (i <= 0) {
  235. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
  236. goto err;
  237. }
  238. /*
  239. * This can't overflow because |len+want| didn't
  240. * overflow.
  241. */
  242. len += i;
  243. chunk -= i;
  244. }
  245. if (chunk_max < INT_MAX/2)
  246. chunk_max *= 2;
  247. }
  248. }
  249. if (off + c.slen < off) {
  250. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  251. goto err;
  252. }
  253. off += c.slen;
  254. if (eos <= 0) {
  255. break;
  256. } else
  257. want = HEADER_SIZE;
  258. }
  259. }
  260. if (off > INT_MAX) {
  261. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  262. goto err;
  263. }
  264. *pb = b;
  265. return off;
  266. err:
  267. if (b != NULL)
  268. BUF_MEM_free(b);
  269. return -1;
  270. }