a_d2i_fp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. len = asn1_d2i_read_bio(in, &b);
  56. if (len < 0)
  57. goto err;
  58. p = (const unsigned char *)b->data;
  59. ret = ASN1_item_d2i(x, &p, len, it);
  60. err:
  61. BUF_MEM_free(b);
  62. return ret;
  63. }
  64. #ifndef OPENSSL_NO_STDIO
  65. void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
  66. {
  67. BIO *b;
  68. char *ret;
  69. if ((b = BIO_new(BIO_s_file())) == NULL) {
  70. ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
  71. return NULL;
  72. }
  73. BIO_set_fp(b, in, BIO_NOCLOSE);
  74. ret = ASN1_item_d2i_bio(it, b, x);
  75. BIO_free(b);
  76. return ret;
  77. }
  78. #endif
  79. #define HEADER_SIZE 8
  80. #define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
  81. int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
  82. {
  83. BUF_MEM *b;
  84. unsigned char *p;
  85. int i;
  86. size_t want = HEADER_SIZE;
  87. uint32_t eos = 0;
  88. size_t off = 0;
  89. size_t len = 0;
  90. size_t diff;
  91. const unsigned char *q;
  92. long slen;
  93. int inf, tag, xclass;
  94. b = BUF_MEM_new();
  95. if (b == NULL) {
  96. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  97. return -1;
  98. }
  99. ERR_clear_error();
  100. for (;;) {
  101. diff = len - off;
  102. if (want >= diff) {
  103. want -= diff;
  104. if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
  105. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  106. goto err;
  107. }
  108. i = BIO_read(in, &(b->data[len]), want);
  109. if (i < 0 && diff == 0) {
  110. ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
  111. goto err;
  112. }
  113. if (i > 0) {
  114. if (len + i < len) {
  115. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
  116. goto err;
  117. }
  118. len += i;
  119. }
  120. }
  121. /* else data already loaded */
  122. p = (unsigned char *)&(b->data[off]);
  123. q = p;
  124. diff = len - off;
  125. if (diff == 0)
  126. goto err;
  127. inf = ASN1_get_object(&q, &slen, &tag, &xclass, diff);
  128. if (inf & 0x80) {
  129. unsigned long e;
  130. e = ERR_GET_REASON(ERR_peek_error());
  131. if (e != ASN1_R_TOO_LONG)
  132. goto err;
  133. ERR_clear_error();
  134. }
  135. i = q - p; /* header length */
  136. off += i; /* end of data */
  137. if (inf & 1) {
  138. /* no data body so go round again */
  139. if (eos == UINT32_MAX) {
  140. ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
  141. goto err;
  142. }
  143. eos++;
  144. want = HEADER_SIZE;
  145. } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
  146. /* eos value, so go back and read another header */
  147. eos--;
  148. if (eos == 0)
  149. break;
  150. else
  151. want = HEADER_SIZE;
  152. } else {
  153. /* suck in slen bytes of data */
  154. want = slen;
  155. if (want > (len - off)) {
  156. size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
  157. want -= (len - off);
  158. if (want > INT_MAX /* BIO_read takes an int length */ ||
  159. len + want < len) {
  160. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
  161. goto err;
  162. }
  163. while (want > 0) {
  164. /*
  165. * Read content in chunks of increasing size
  166. * so we can return an error for EOF without
  167. * having to allocate the entire content length
  168. * in one go.
  169. */
  170. size_t chunk = want > chunk_max ? chunk_max : want;
  171. if (!BUF_MEM_grow_clean(b, len + chunk)) {
  172. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  173. goto err;
  174. }
  175. want -= chunk;
  176. while (chunk > 0) {
  177. i = BIO_read(in, &(b->data[len]), chunk);
  178. if (i <= 0) {
  179. ERR_raise(ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA);
  180. goto err;
  181. }
  182. /*
  183. * This can't overflow because |len+want| didn't
  184. * overflow.
  185. */
  186. len += i;
  187. chunk -= i;
  188. }
  189. if (chunk_max < INT_MAX/2)
  190. chunk_max *= 2;
  191. }
  192. }
  193. if (off + slen < off) {
  194. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
  195. goto err;
  196. }
  197. off += slen;
  198. if (eos == 0) {
  199. break;
  200. } else
  201. want = HEADER_SIZE;
  202. }
  203. }
  204. if (off > INT_MAX) {
  205. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
  206. goto err;
  207. }
  208. *pb = b;
  209. return off;
  210. err:
  211. BUF_MEM_free(b);
  212. return -1;
  213. }