a_d2i_fp.c 8.4 KB

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