2
0

asn1_lib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 1995-2021 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 <openssl/asn1.h>
  13. #include "asn1_local.h"
  14. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  15. long max);
  16. static void asn1_put_length(unsigned char **pp, int length);
  17. static int _asn1_check_infinite_end(const unsigned char **p, long len)
  18. {
  19. /*
  20. * If there is 0 or 1 byte left, the length check should pick things up
  21. */
  22. if (len <= 0)
  23. return 1;
  24. else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
  25. (*p) += 2;
  26. return 1;
  27. }
  28. return 0;
  29. }
  30. int ASN1_check_infinite_end(unsigned char **p, long len)
  31. {
  32. return _asn1_check_infinite_end((const unsigned char **)p, len);
  33. }
  34. int ASN1_const_check_infinite_end(const unsigned char **p, long len)
  35. {
  36. return _asn1_check_infinite_end(p, len);
  37. }
  38. int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
  39. int *pclass, long omax)
  40. {
  41. int i, ret;
  42. long l;
  43. const unsigned char *p = *pp;
  44. int tag, xclass, inf;
  45. long max = omax;
  46. if (!max)
  47. goto err;
  48. ret = (*p & V_ASN1_CONSTRUCTED);
  49. xclass = (*p & V_ASN1_PRIVATE);
  50. i = *p & V_ASN1_PRIMITIVE_TAG;
  51. if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
  52. p++;
  53. if (--max == 0)
  54. goto err;
  55. l = 0;
  56. while (*p & 0x80) {
  57. l <<= 7L;
  58. l |= *(p++) & 0x7f;
  59. if (--max == 0)
  60. goto err;
  61. if (l > (INT_MAX >> 7L))
  62. goto err;
  63. }
  64. l <<= 7L;
  65. l |= *(p++) & 0x7f;
  66. tag = (int)l;
  67. if (--max == 0)
  68. goto err;
  69. } else {
  70. tag = i;
  71. p++;
  72. if (--max == 0)
  73. goto err;
  74. }
  75. *ptag = tag;
  76. *pclass = xclass;
  77. if (!asn1_get_length(&p, &inf, plength, max))
  78. goto err;
  79. if (inf && !(ret & V_ASN1_CONSTRUCTED))
  80. goto err;
  81. if (*plength > (omax - (p - *pp))) {
  82. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
  83. /*
  84. * Set this so that even if things are not long enough the values are
  85. * set correctly
  86. */
  87. ret |= 0x80;
  88. }
  89. *pp = p;
  90. return ret | inf;
  91. err:
  92. ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
  93. return 0x80;
  94. }
  95. /*
  96. * Decode a length field.
  97. * The short form is a single byte defining a length 0 - 127.
  98. * The long form is a byte 0 - 127 with the top bit set and this indicates
  99. * the number of following octets that contain the length. These octets
  100. * are stored most significant digit first.
  101. */
  102. static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
  103. long max)
  104. {
  105. const unsigned char *p = *pp;
  106. unsigned long ret = 0;
  107. int i;
  108. if (max-- < 1)
  109. return 0;
  110. if (*p == 0x80) {
  111. *inf = 1;
  112. p++;
  113. } else {
  114. *inf = 0;
  115. i = *p & 0x7f;
  116. if (*p++ & 0x80) {
  117. if (max < i + 1)
  118. return 0;
  119. /* Skip leading zeroes */
  120. while (i > 0 && *p == 0) {
  121. p++;
  122. i--;
  123. }
  124. if (i > (int)sizeof(long))
  125. return 0;
  126. while (i > 0) {
  127. ret <<= 8;
  128. ret |= *p++;
  129. i--;
  130. }
  131. if (ret > LONG_MAX)
  132. return 0;
  133. } else
  134. ret = i;
  135. }
  136. *pp = p;
  137. *rl = (long)ret;
  138. return 1;
  139. }
  140. /*
  141. * class 0 is constructed constructed == 2 for indefinite length constructed
  142. */
  143. void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
  144. int xclass)
  145. {
  146. unsigned char *p = *pp;
  147. int i, ttag;
  148. i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
  149. i |= (xclass & V_ASN1_PRIVATE);
  150. if (tag < 31)
  151. *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
  152. else {
  153. *(p++) = i | V_ASN1_PRIMITIVE_TAG;
  154. for (i = 0, ttag = tag; ttag > 0; i++)
  155. ttag >>= 7;
  156. ttag = i;
  157. while (i-- > 0) {
  158. p[i] = tag & 0x7f;
  159. if (i != (ttag - 1))
  160. p[i] |= 0x80;
  161. tag >>= 7;
  162. }
  163. p += ttag;
  164. }
  165. if (constructed == 2)
  166. *(p++) = 0x80;
  167. else
  168. asn1_put_length(&p, length);
  169. *pp = p;
  170. }
  171. int ASN1_put_eoc(unsigned char **pp)
  172. {
  173. unsigned char *p = *pp;
  174. *p++ = 0;
  175. *p++ = 0;
  176. *pp = p;
  177. return 2;
  178. }
  179. static void asn1_put_length(unsigned char **pp, int length)
  180. {
  181. unsigned char *p = *pp;
  182. int i, l;
  183. if (length <= 127)
  184. *(p++) = (unsigned char)length;
  185. else {
  186. l = length;
  187. for (i = 0; l > 0; i++)
  188. l >>= 8;
  189. *(p++) = i | 0x80;
  190. l = i;
  191. while (i-- > 0) {
  192. p[i] = length & 0xff;
  193. length >>= 8;
  194. }
  195. p += l;
  196. }
  197. *pp = p;
  198. }
  199. int ASN1_object_size(int constructed, int length, int tag)
  200. {
  201. int ret = 1;
  202. if (length < 0)
  203. return -1;
  204. if (tag >= 31) {
  205. while (tag > 0) {
  206. tag >>= 7;
  207. ret++;
  208. }
  209. }
  210. if (constructed == 2) {
  211. ret += 3;
  212. } else {
  213. ret++;
  214. if (length > 127) {
  215. int tmplen = length;
  216. while (tmplen > 0) {
  217. tmplen >>= 8;
  218. ret++;
  219. }
  220. }
  221. }
  222. if (ret >= INT_MAX - length)
  223. return -1;
  224. return ret + length;
  225. }
  226. int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
  227. {
  228. if (str == NULL)
  229. return 0;
  230. dst->type = str->type;
  231. if (!ASN1_STRING_set(dst, str->data, str->length))
  232. return 0;
  233. /* Copy flags but preserve embed value */
  234. dst->flags &= ASN1_STRING_FLAG_EMBED;
  235. dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
  236. return 1;
  237. }
  238. ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
  239. {
  240. ASN1_STRING *ret;
  241. if (!str)
  242. return NULL;
  243. ret = ASN1_STRING_new();
  244. if (ret == NULL)
  245. return NULL;
  246. if (!ASN1_STRING_copy(ret, str)) {
  247. ASN1_STRING_free(ret);
  248. return NULL;
  249. }
  250. return ret;
  251. }
  252. int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
  253. {
  254. unsigned char *c;
  255. const char *data = _data;
  256. size_t len;
  257. if (len_in < 0) {
  258. if (data == NULL)
  259. return 0;
  260. len = strlen(data);
  261. } else {
  262. len = (size_t)len_in;
  263. }
  264. /*
  265. * Verify that the length fits within an integer for assignment to
  266. * str->length below. The additional 1 is subtracted to allow for the
  267. * '\0' terminator even though this isn't strictly necessary.
  268. */
  269. if (len > INT_MAX - 1) {
  270. ASN1err(0, ASN1_R_TOO_LARGE);
  271. return 0;
  272. }
  273. if ((size_t)str->length <= len || str->data == NULL) {
  274. c = str->data;
  275. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  276. /* No NUL terminator in fuzzing builds */
  277. str->data = OPENSSL_realloc(c, len);
  278. #else
  279. str->data = OPENSSL_realloc(c, len + 1);
  280. #endif
  281. if (str->data == NULL) {
  282. ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
  283. str->data = c;
  284. return 0;
  285. }
  286. }
  287. str->length = len;
  288. if (data != NULL) {
  289. memcpy(str->data, data, len);
  290. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  291. /*
  292. * Add a NUL terminator. This should not be necessary - but we add it as
  293. * a safety precaution
  294. */
  295. str->data[len] = '\0';
  296. #endif
  297. }
  298. return 1;
  299. }
  300. void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
  301. {
  302. OPENSSL_free(str->data);
  303. str->data = data;
  304. str->length = len;
  305. }
  306. ASN1_STRING *ASN1_STRING_new(void)
  307. {
  308. return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  309. }
  310. ASN1_STRING *ASN1_STRING_type_new(int type)
  311. {
  312. ASN1_STRING *ret;
  313. ret = OPENSSL_zalloc(sizeof(*ret));
  314. if (ret == NULL) {
  315. ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
  316. return NULL;
  317. }
  318. ret->type = type;
  319. return ret;
  320. }
  321. void asn1_string_embed_free(ASN1_STRING *a, int embed)
  322. {
  323. if (a == NULL)
  324. return;
  325. if (!(a->flags & ASN1_STRING_FLAG_NDEF))
  326. OPENSSL_free(a->data);
  327. if (embed == 0)
  328. OPENSSL_free(a);
  329. }
  330. void ASN1_STRING_free(ASN1_STRING *a)
  331. {
  332. if (a == NULL)
  333. return;
  334. asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
  335. }
  336. void ASN1_STRING_clear_free(ASN1_STRING *a)
  337. {
  338. if (a == NULL)
  339. return;
  340. if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
  341. OPENSSL_cleanse(a->data, a->length);
  342. ASN1_STRING_free(a);
  343. }
  344. int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
  345. {
  346. int i;
  347. i = (a->length - b->length);
  348. if (i == 0) {
  349. i = memcmp(a->data, b->data, a->length);
  350. if (i == 0)
  351. return a->type - b->type;
  352. else
  353. return i;
  354. } else
  355. return i;
  356. }
  357. int ASN1_STRING_length(const ASN1_STRING *x)
  358. {
  359. return x->length;
  360. }
  361. void ASN1_STRING_length_set(ASN1_STRING *x, int len)
  362. {
  363. x->length = len;
  364. }
  365. int ASN1_STRING_type(const ASN1_STRING *x)
  366. {
  367. return x->type;
  368. }
  369. const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
  370. {
  371. return x->data;
  372. }
  373. # if OPENSSL_API_COMPAT < 0x10100000L
  374. unsigned char *ASN1_STRING_data(ASN1_STRING *x)
  375. {
  376. return x->data;
  377. }
  378. #endif