a_d2i_fp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. */
  57. #include <stdio.h>
  58. #include <limits.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/buffer.h>
  61. #include <openssl/asn1.h>
  62. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
  63. #ifndef NO_OLD_ASN1
  64. # ifndef OPENSSL_NO_STDIO
  65. void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
  66. {
  67. BIO *b;
  68. void *ret;
  69. if ((b = BIO_new(BIO_s_file())) == NULL) {
  70. ASN1err(ASN1_F_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)
  87. goto err;
  88. p = (unsigned char *)b->data;
  89. ret = d2i(x, &p, len);
  90. err:
  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. BUF_MEM_free(b);
  108. return (ret);
  109. }
  110. #ifndef OPENSSL_NO_STDIO
  111. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
  112. {
  113. BIO *b;
  114. char *ret;
  115. if ((b = BIO_new(BIO_s_file())) == NULL) {
  116. ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
  117. return (NULL);
  118. }
  119. BIO_set_fp(b, in, BIO_NOCLOSE);
  120. ret = ASN1_item_d2i_bio(it, b, x);
  121. BIO_free(b);
  122. return (ret);
  123. }
  124. #endif
  125. #define HEADER_SIZE 8
  126. static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  127. {
  128. BUF_MEM *b;
  129. unsigned char *p;
  130. int i;
  131. size_t want = HEADER_SIZE;
  132. int eos = 0;
  133. size_t off = 0;
  134. size_t len = 0;
  135. const unsigned char *q;
  136. long slen;
  137. int inf, tag, xclass;
  138. b = BUF_MEM_new();
  139. if (b == NULL) {
  140. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 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. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 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. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
  154. goto err;
  155. }
  156. if (i > 0) {
  157. if (len + i < len) {
  158. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 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. q = p;
  167. inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
  168. if (inf & 0x80) {
  169. unsigned long 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 = q - p; /* header length */
  177. off += i; /* end of data */
  178. if (inf & 1) {
  179. /* no data body so go round again */
  180. eos++;
  181. if (eos < 0) {
  182. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
  183. goto err;
  184. }
  185. want = HEADER_SIZE;
  186. } else if (eos && (slen == 0) && (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 slen bytes of data */
  195. want = 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. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  201. goto err;
  202. }
  203. if (!BUF_MEM_grow_clean(b, len + want)) {
  204. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 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. ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
  211. ASN1_R_NOT_ENOUGH_DATA);
  212. goto err;
  213. }
  214. /*
  215. * This can't overflow because |len+want| didn't
  216. * overflow.
  217. */
  218. len += i;
  219. want -= i;
  220. }
  221. }
  222. if (off + slen < off) {
  223. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  224. goto err;
  225. }
  226. off += slen;
  227. if (eos <= 0) {
  228. break;
  229. } else
  230. want = HEADER_SIZE;
  231. }
  232. }
  233. if (off > INT_MAX) {
  234. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  235. goto err;
  236. }
  237. *pb = b;
  238. return off;
  239. err:
  240. BUF_MEM_free(b);
  241. return -1;
  242. }