a_d2i_fp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/numbers.h"
  13. #include <openssl/buffer.h>
  14. #include <openssl/asn1.h>
  15. #include "internal/asn1_int.h"
  16. #ifndef NO_OLD_ASN1
  17. # ifndef OPENSSL_NO_STDIO
  18. void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
  19. {
  20. BIO *b;
  21. void *ret;
  22. if ((b = BIO_new(BIO_s_file())) == NULL) {
  23. ASN1err(ASN1_F_ASN1_D2I_FP, ERR_R_BUF_LIB);
  24. return NULL;
  25. }
  26. BIO_set_fp(b, in, BIO_NOCLOSE);
  27. ret = ASN1_d2i_bio(xnew, d2i, b, x);
  28. BIO_free(b);
  29. return ret;
  30. }
  31. # endif
  32. void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
  33. {
  34. BUF_MEM *b = NULL;
  35. const unsigned char *p;
  36. void *ret = NULL;
  37. int len;
  38. len = asn1_d2i_read_bio(in, &b);
  39. if (len < 0)
  40. goto err;
  41. p = (unsigned char *)b->data;
  42. ret = d2i(x, &p, len);
  43. err:
  44. BUF_MEM_free(b);
  45. return ret;
  46. }
  47. #endif
  48. void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
  49. {
  50. BUF_MEM *b = NULL;
  51. const unsigned char *p;
  52. void *ret = NULL;
  53. int len;
  54. len = asn1_d2i_read_bio(in, &b);
  55. if (len < 0)
  56. goto err;
  57. p = (const unsigned char *)b->data;
  58. ret = ASN1_item_d2i(x, &p, len, it);
  59. err:
  60. BUF_MEM_free(b);
  61. return ret;
  62. }
  63. #ifndef OPENSSL_NO_STDIO
  64. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
  65. {
  66. BIO *b;
  67. char *ret;
  68. if ((b = BIO_new(BIO_s_file())) == NULL) {
  69. ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
  70. return NULL;
  71. }
  72. BIO_set_fp(b, in, BIO_NOCLOSE);
  73. ret = ASN1_item_d2i_bio(it, b, x);
  74. BIO_free(b);
  75. return ret;
  76. }
  77. #endif
  78. #define HEADER_SIZE 8
  79. #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
  80. int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  81. {
  82. BUF_MEM *b;
  83. unsigned char *p;
  84. int i;
  85. size_t want = HEADER_SIZE;
  86. uint32_t eos = 0;
  87. size_t off = 0;
  88. size_t len = 0;
  89. const unsigned char *q;
  90. long slen;
  91. int inf, tag, xclass;
  92. b = BUF_MEM_new();
  93. if (b == NULL) {
  94. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
  95. return -1;
  96. }
  97. ERR_clear_error();
  98. for (;;) {
  99. if (want >= (len - off)) {
  100. want -= (len - off);
  101. if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
  102. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
  103. goto err;
  104. }
  105. i = BIO_read(in, &(b->data[len]), want);
  106. if ((i < 0) && ((len - off) == 0)) {
  107. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
  108. goto err;
  109. }
  110. if (i > 0) {
  111. if (len + i < len) {
  112. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  113. goto err;
  114. }
  115. len += i;
  116. }
  117. }
  118. /* else data already loaded */
  119. p = (unsigned char *)&(b->data[off]);
  120. q = p;
  121. inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
  122. if (inf & 0x80) {
  123. unsigned long e;
  124. e = ERR_GET_REASON(ERR_peek_error());
  125. if (e != ASN1_R_TOO_LONG)
  126. goto err;
  127. else
  128. ERR_clear_error(); /* clear error */
  129. }
  130. i = q - p; /* header length */
  131. off += i; /* end of data */
  132. if (inf & 1) {
  133. /* no data body so go round again */
  134. if (eos == UINT32_MAX) {
  135. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
  136. goto err;
  137. }
  138. eos++;
  139. want = HEADER_SIZE;
  140. } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
  141. /* eos value, so go back and read another header */
  142. eos--;
  143. if (eos == 0)
  144. break;
  145. else
  146. want = HEADER_SIZE;
  147. } else {
  148. /* suck in slen bytes of data */
  149. want = slen;
  150. if (want > (len - off)) {
  151. size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
  152. want -= (len - off);
  153. if (want > INT_MAX /* BIO_read takes an int length */ ||
  154. len + want < len) {
  155. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  156. goto err;
  157. }
  158. while (want > 0) {
  159. /*
  160. * Read content in chunks of increasing size
  161. * so we can return an error for EOF without
  162. * having to allocate the entire content length
  163. * in one go.
  164. */
  165. size_t chunk = want > chunk_max ? chunk_max : want;
  166. if (!BUF_MEM_grow_clean(b, len + chunk)) {
  167. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
  168. goto err;
  169. }
  170. want -= chunk;
  171. while (chunk > 0) {
  172. i = BIO_read(in, &(b->data[len]), chunk);
  173. if (i <= 0) {
  174. ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
  175. ASN1_R_NOT_ENOUGH_DATA);
  176. goto err;
  177. }
  178. /*
  179. * This can't overflow because |len+want| didn't
  180. * overflow.
  181. */
  182. len += i;
  183. chunk -= i;
  184. }
  185. if (chunk_max < INT_MAX/2)
  186. chunk_max *= 2;
  187. }
  188. }
  189. if (off + slen < off) {
  190. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  191. goto err;
  192. }
  193. off += slen;
  194. if (eos == 0) {
  195. break;
  196. } else
  197. want = HEADER_SIZE;
  198. }
  199. }
  200. if (off > INT_MAX) {
  201. ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
  202. goto err;
  203. }
  204. *pb = b;
  205. return off;
  206. err:
  207. BUF_MEM_free(b);
  208. return -1;
  209. }