a_d2i_fp.c 6.3 KB

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