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.
 
 
 
 
 
 

283 rivejä
9.1 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. #define HEADER_SIZE 8
  127. #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
  128. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  129. {
  130. BUF_MEM *b;
  131. unsigned char *p;
  132. int i;
  133. ASN1_const_CTX c;
  134. size_t want = HEADER_SIZE;
  135. int eos = 0;
  136. size_t off = 0;
  137. size_t len = 0;
  138. b = BUF_MEM_new();
  139. if (b == NULL) {
  140. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  141. return -1;
  142. }
  143. ERR_clear_error();
  144. for (;;) {
  145. if (want >= (len - off)) {
  146. want -= (len - off);
  147. if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
  148. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  149. goto err;
  150. }
  151. i = BIO_read(in, &(b->data[len]), want);
  152. if ((i < 0) && ((len - off) == 0)) {
  153. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
  154. goto err;
  155. }
  156. if (i > 0) {
  157. if (len + i < len) {
  158. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  159. goto err;
  160. }
  161. len += i;
  162. }
  163. }
  164. /* else data already loaded */
  165. p = (unsigned char *)&(b->data[off]);
  166. c.p = p;
  167. c.inf = ASN1_get_object(&(c.p), &(c.slen), &(c.tag), &(c.xclass),
  168. len - off);
  169. if (c.inf & 0x80) {
  170. uint32_t e;
  171. e = ERR_GET_REASON(ERR_peek_error());
  172. if (e != ASN1_R_TOO_LONG)
  173. goto err;
  174. else
  175. ERR_clear_error(); /* clear error */
  176. }
  177. i = c.p - p; /* header length */
  178. off += i; /* end of data */
  179. if (c.inf & 1) {
  180. /* no data body so go round again */
  181. eos++;
  182. if (eos < 0) {
  183. OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
  184. goto err;
  185. }
  186. want = HEADER_SIZE;
  187. } else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC)) {
  188. /* eos value, so go back and read another header */
  189. eos--;
  190. if (eos <= 0)
  191. break;
  192. else
  193. want = HEADER_SIZE;
  194. } else {
  195. /* suck in c.slen bytes of data */
  196. want = c.slen;
  197. if (want > (len - off)) {
  198. size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
  199. want -= (len - off);
  200. if (want > INT_MAX /* BIO_read takes an int length */ ||
  201. len + want < len) {
  202. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  203. goto err;
  204. }
  205. while (want > 0) {
  206. /*
  207. * Read content in chunks of increasing size
  208. * so we can return an error for EOF without
  209. * having to allocate the entire content length
  210. * in one go.
  211. */
  212. size_t chunk = want > chunk_max ? chunk_max : want;
  213. if (!BUF_MEM_grow_clean(b, len + chunk)) {
  214. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  215. goto err;
  216. }
  217. want -= chunk;
  218. while (chunk > 0) {
  219. i = BIO_read(in, &(b->data[len]), chunk);
  220. if (i <= 0) {
  221. OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
  222. goto err;
  223. }
  224. /*
  225. * This can't overflow because |len+want| didn't
  226. * overflow.
  227. */
  228. len += i;
  229. chunk -= i;
  230. }
  231. if (chunk_max < INT_MAX/2)
  232. chunk_max *= 2;
  233. }
  234. }
  235. if (off + c.slen < off) {
  236. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  237. goto err;
  238. }
  239. off += c.slen;
  240. if (eos <= 0) {
  241. break;
  242. } else
  243. want = HEADER_SIZE;
  244. }
  245. }
  246. if (off > INT_MAX) {
  247. OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
  248. goto err;
  249. }
  250. *pb = b;
  251. return off;
  252. err:
  253. if (b != NULL)
  254. BUF_MEM_free(b);
  255. return -1;
  256. }