asn1_lib.c 11 KB

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