cbs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #if !defined(__STDC_FORMAT_MACROS)
  15. #define __STDC_FORMAT_MACROS
  16. #endif
  17. #include <openssl/buf.h>
  18. #include <openssl/mem.h>
  19. #include <openssl/bytestring.h>
  20. #include <assert.h>
  21. #include <inttypes.h>
  22. #include <string.h>
  23. #include "internal.h"
  24. #include "../internal.h"
  25. void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
  26. cbs->data = data;
  27. cbs->len = len;
  28. }
  29. static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
  30. if (cbs->len < n) {
  31. return 0;
  32. }
  33. *p = cbs->data;
  34. cbs->data += n;
  35. cbs->len -= n;
  36. return 1;
  37. }
  38. int CBS_skip(CBS *cbs, size_t len) {
  39. const uint8_t *dummy;
  40. return cbs_get(cbs, &dummy, len);
  41. }
  42. const uint8_t *CBS_data(const CBS *cbs) {
  43. return cbs->data;
  44. }
  45. size_t CBS_len(const CBS *cbs) {
  46. return cbs->len;
  47. }
  48. int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
  49. OPENSSL_free(*out_ptr);
  50. *out_ptr = NULL;
  51. *out_len = 0;
  52. if (cbs->len == 0) {
  53. return 1;
  54. }
  55. *out_ptr = BUF_memdup(cbs->data, cbs->len);
  56. if (*out_ptr == NULL) {
  57. return 0;
  58. }
  59. *out_len = cbs->len;
  60. return 1;
  61. }
  62. int CBS_strdup(const CBS *cbs, char **out_ptr) {
  63. if (*out_ptr != NULL) {
  64. OPENSSL_free(*out_ptr);
  65. }
  66. *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
  67. return (*out_ptr != NULL);
  68. }
  69. int CBS_contains_zero_byte(const CBS *cbs) {
  70. return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL;
  71. }
  72. int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
  73. if (len != cbs->len) {
  74. return 0;
  75. }
  76. return CRYPTO_memcmp(cbs->data, data, len) == 0;
  77. }
  78. static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
  79. uint32_t result = 0;
  80. const uint8_t *data;
  81. if (!cbs_get(cbs, &data, len)) {
  82. return 0;
  83. }
  84. for (size_t i = 0; i < len; i++) {
  85. result <<= 8;
  86. result |= data[i];
  87. }
  88. *out = result;
  89. return 1;
  90. }
  91. int CBS_get_u8(CBS *cbs, uint8_t *out) {
  92. const uint8_t *v;
  93. if (!cbs_get(cbs, &v, 1)) {
  94. return 0;
  95. }
  96. *out = *v;
  97. return 1;
  98. }
  99. int CBS_get_u16(CBS *cbs, uint16_t *out) {
  100. uint32_t v;
  101. if (!cbs_get_u(cbs, &v, 2)) {
  102. return 0;
  103. }
  104. *out = v;
  105. return 1;
  106. }
  107. int CBS_get_u24(CBS *cbs, uint32_t *out) {
  108. return cbs_get_u(cbs, out, 3);
  109. }
  110. int CBS_get_u32(CBS *cbs, uint32_t *out) {
  111. return cbs_get_u(cbs, out, 4);
  112. }
  113. int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
  114. if (cbs->len == 0) {
  115. return 0;
  116. }
  117. *out = cbs->data[cbs->len - 1];
  118. cbs->len--;
  119. return 1;
  120. }
  121. int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
  122. const uint8_t *v;
  123. if (!cbs_get(cbs, &v, len)) {
  124. return 0;
  125. }
  126. CBS_init(out, v, len);
  127. return 1;
  128. }
  129. int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
  130. const uint8_t *v;
  131. if (!cbs_get(cbs, &v, len)) {
  132. return 0;
  133. }
  134. OPENSSL_memcpy(out, v, len);
  135. return 1;
  136. }
  137. static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
  138. uint32_t len;
  139. if (!cbs_get_u(cbs, &len, len_len)) {
  140. return 0;
  141. }
  142. return CBS_get_bytes(cbs, out, len);
  143. }
  144. int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
  145. return cbs_get_length_prefixed(cbs, out, 1);
  146. }
  147. int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
  148. return cbs_get_length_prefixed(cbs, out, 2);
  149. }
  150. int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
  151. return cbs_get_length_prefixed(cbs, out, 3);
  152. }
  153. // parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
  154. // |*out| to the result. This is the encoding used in DER for both high tag
  155. // number form and OID components.
  156. static int parse_base128_integer(CBS *cbs, uint64_t *out) {
  157. uint64_t v = 0;
  158. uint8_t b;
  159. do {
  160. if (!CBS_get_u8(cbs, &b)) {
  161. return 0;
  162. }
  163. if ((v >> (64 - 7)) != 0) {
  164. // The value is too large.
  165. return 0;
  166. }
  167. if (v == 0 && b == 0x80) {
  168. // The value must be minimally encoded.
  169. return 0;
  170. }
  171. v = (v << 7) | (b & 0x7f);
  172. // Values end at an octet with the high bit cleared.
  173. } while (b & 0x80);
  174. *out = v;
  175. return 1;
  176. }
  177. static int parse_asn1_tag(CBS *cbs, unsigned *out) {
  178. uint8_t tag_byte;
  179. if (!CBS_get_u8(cbs, &tag_byte)) {
  180. return 0;
  181. }
  182. // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
  183. // number no greater than 30.
  184. //
  185. // If the number portion is 31 (0x1f, the largest value that fits in the
  186. // allotted bits), then the tag is more than one byte long and the
  187. // continuation bytes contain the tag number. This parser only supports tag
  188. // numbers less than 31 (and thus single-byte tags).
  189. unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
  190. unsigned tag_number = tag_byte & 0x1f;
  191. if (tag_number == 0x1f) {
  192. uint64_t v;
  193. if (!parse_base128_integer(cbs, &v) ||
  194. // Check the tag number is within our supported bounds.
  195. v > CBS_ASN1_TAG_NUMBER_MASK ||
  196. // Small tag numbers should have used low tag number form.
  197. v < 0x1f) {
  198. return 0;
  199. }
  200. tag_number = (unsigned)v;
  201. }
  202. tag |= tag_number;
  203. *out = tag;
  204. return 1;
  205. }
  206. static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  207. size_t *out_header_len, int ber_ok) {
  208. CBS header = *cbs;
  209. CBS throwaway;
  210. if (out == NULL) {
  211. out = &throwaway;
  212. }
  213. unsigned tag;
  214. if (!parse_asn1_tag(&header, &tag)) {
  215. return 0;
  216. }
  217. if (out_tag != NULL) {
  218. *out_tag = tag;
  219. }
  220. uint8_t length_byte;
  221. if (!CBS_get_u8(&header, &length_byte)) {
  222. return 0;
  223. }
  224. size_t header_len = CBS_len(cbs) - CBS_len(&header);
  225. size_t len;
  226. // The format for the length encoding is specified in ITU-T X.690 section
  227. // 8.1.3.
  228. if ((length_byte & 0x80) == 0) {
  229. // Short form length.
  230. len = ((size_t) length_byte) + header_len;
  231. if (out_header_len != NULL) {
  232. *out_header_len = header_len;
  233. }
  234. } else {
  235. // The high bit indicate that this is the long form, while the next 7 bits
  236. // encode the number of subsequent octets used to encode the length (ITU-T
  237. // X.690 clause 8.1.3.5.b).
  238. const size_t num_bytes = length_byte & 0x7f;
  239. uint32_t len32;
  240. if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
  241. // indefinite length
  242. if (out_header_len != NULL) {
  243. *out_header_len = header_len;
  244. }
  245. return CBS_get_bytes(cbs, out, header_len);
  246. }
  247. // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
  248. // used as the first byte of the length. If this parser encounters that
  249. // value, num_bytes will be parsed as 127, which will fail the check below.
  250. if (num_bytes == 0 || num_bytes > 4) {
  251. return 0;
  252. }
  253. if (!cbs_get_u(&header, &len32, num_bytes)) {
  254. return 0;
  255. }
  256. // ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
  257. // with the minimum number of octets.
  258. if (len32 < 128) {
  259. // Length should have used short-form encoding.
  260. return 0;
  261. }
  262. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  263. // Length should have been at least one byte shorter.
  264. return 0;
  265. }
  266. len = len32;
  267. if (len + header_len + num_bytes < len) {
  268. // Overflow.
  269. return 0;
  270. }
  271. len += header_len + num_bytes;
  272. if (out_header_len != NULL) {
  273. *out_header_len = header_len + num_bytes;
  274. }
  275. }
  276. return CBS_get_bytes(cbs, out, len);
  277. }
  278. int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
  279. size_t header_len;
  280. if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
  281. return 0;
  282. }
  283. if (!CBS_skip(out, header_len)) {
  284. assert(0);
  285. return 0;
  286. }
  287. return 1;
  288. }
  289. int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  290. size_t *out_header_len) {
  291. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  292. 0 /* DER only */);
  293. }
  294. int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
  295. size_t *out_header_len) {
  296. return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
  297. 1 /* BER allowed */);
  298. }
  299. static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
  300. int skip_header) {
  301. size_t header_len;
  302. unsigned tag;
  303. CBS throwaway;
  304. if (out == NULL) {
  305. out = &throwaway;
  306. }
  307. if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
  308. tag != tag_value) {
  309. return 0;
  310. }
  311. if (skip_header && !CBS_skip(out, header_len)) {
  312. assert(0);
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
  318. return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
  319. }
  320. int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
  321. return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
  322. }
  323. int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
  324. if (CBS_len(cbs) < 1) {
  325. return 0;
  326. }
  327. CBS copy = *cbs;
  328. unsigned actual_tag;
  329. return parse_asn1_tag(&copy, &actual_tag) && tag_value == actual_tag;
  330. }
  331. int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
  332. CBS bytes;
  333. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
  334. return 0;
  335. }
  336. *out = 0;
  337. const uint8_t *data = CBS_data(&bytes);
  338. size_t len = CBS_len(&bytes);
  339. if (len == 0) {
  340. // An INTEGER is encoded with at least one octet.
  341. return 0;
  342. }
  343. if ((data[0] & 0x80) != 0) {
  344. // Negative number.
  345. return 0;
  346. }
  347. if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
  348. // Extra leading zeros.
  349. return 0;
  350. }
  351. for (size_t i = 0; i < len; i++) {
  352. if ((*out >> 56) != 0) {
  353. // Too large to represent as a uint64_t.
  354. return 0;
  355. }
  356. *out <<= 8;
  357. *out |= data[i];
  358. }
  359. return 1;
  360. }
  361. int CBS_get_asn1_bool(CBS *cbs, int *out) {
  362. CBS bytes;
  363. if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) ||
  364. CBS_len(&bytes) != 1) {
  365. return 0;
  366. }
  367. const uint8_t value = *CBS_data(&bytes);
  368. if (value != 0 && value != 0xff) {
  369. return 0;
  370. }
  371. *out = !!value;
  372. return 1;
  373. }
  374. int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
  375. int present = 0;
  376. if (CBS_peek_asn1_tag(cbs, tag)) {
  377. if (!CBS_get_asn1(cbs, out, tag)) {
  378. return 0;
  379. }
  380. present = 1;
  381. }
  382. if (out_present != NULL) {
  383. *out_present = present;
  384. }
  385. return 1;
  386. }
  387. int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
  388. unsigned tag) {
  389. CBS child;
  390. int present;
  391. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  392. return 0;
  393. }
  394. if (present) {
  395. assert(out);
  396. if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
  397. CBS_len(&child) != 0) {
  398. return 0;
  399. }
  400. } else {
  401. CBS_init(out, NULL, 0);
  402. }
  403. if (out_present) {
  404. *out_present = present;
  405. }
  406. return 1;
  407. }
  408. int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
  409. uint64_t default_value) {
  410. CBS child;
  411. int present;
  412. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  413. return 0;
  414. }
  415. if (present) {
  416. if (!CBS_get_asn1_uint64(&child, out) ||
  417. CBS_len(&child) != 0) {
  418. return 0;
  419. }
  420. } else {
  421. *out = default_value;
  422. }
  423. return 1;
  424. }
  425. int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
  426. int default_value) {
  427. CBS child, child2;
  428. int present;
  429. if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
  430. return 0;
  431. }
  432. if (present) {
  433. uint8_t boolean;
  434. if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
  435. CBS_len(&child2) != 1 ||
  436. CBS_len(&child) != 0) {
  437. return 0;
  438. }
  439. boolean = CBS_data(&child2)[0];
  440. if (boolean == 0) {
  441. *out = 0;
  442. } else if (boolean == 0xff) {
  443. *out = 1;
  444. } else {
  445. return 0;
  446. }
  447. } else {
  448. *out = default_value;
  449. }
  450. return 1;
  451. }
  452. int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
  453. CBS in = *cbs;
  454. uint8_t num_unused_bits;
  455. if (!CBS_get_u8(&in, &num_unused_bits) ||
  456. num_unused_bits > 7) {
  457. return 0;
  458. }
  459. if (num_unused_bits == 0) {
  460. return 1;
  461. }
  462. // All num_unused_bits bits must exist and be zeros.
  463. uint8_t last;
  464. if (!CBS_get_last_u8(&in, &last) ||
  465. (last & ((1 << num_unused_bits) - 1)) != 0) {
  466. return 0;
  467. }
  468. return 1;
  469. }
  470. int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
  471. if (!CBS_is_valid_asn1_bitstring(cbs)) {
  472. return 0;
  473. }
  474. const unsigned byte_num = (bit >> 3) + 1;
  475. const unsigned bit_num = 7 - (bit & 7);
  476. // Unused bits are zero, and this function does not distinguish between
  477. // missing and unset bits. Thus it is sufficient to do a byte-level length
  478. // check.
  479. return byte_num < CBS_len(cbs) &&
  480. (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
  481. }
  482. static int add_decimal(CBB *out, uint64_t v) {
  483. char buf[DECIMAL_SIZE(uint64_t) + 1];
  484. BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
  485. return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
  486. }
  487. char *CBS_asn1_oid_to_text(const CBS *cbs) {
  488. CBB cbb;
  489. if (!CBB_init(&cbb, 32)) {
  490. goto err;
  491. }
  492. CBS copy = *cbs;
  493. // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
  494. uint64_t v;
  495. if (!parse_base128_integer(&copy, &v)) {
  496. goto err;
  497. }
  498. if (v >= 80) {
  499. if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
  500. !add_decimal(&cbb, v - 80)) {
  501. goto err;
  502. }
  503. } else if (!add_decimal(&cbb, v / 40) ||
  504. !CBB_add_u8(&cbb, '.') ||
  505. !add_decimal(&cbb, v % 40)) {
  506. goto err;
  507. }
  508. while (CBS_len(&copy) != 0) {
  509. if (!parse_base128_integer(&copy, &v) ||
  510. !CBB_add_u8(&cbb, '.') ||
  511. !add_decimal(&cbb, v)) {
  512. goto err;
  513. }
  514. }
  515. uint8_t *txt;
  516. size_t txt_len;
  517. if (!CBB_add_u8(&cbb, '\0') ||
  518. !CBB_finish(&cbb, &txt, &txt_len)) {
  519. goto err;
  520. }
  521. return (char *)txt;
  522. err:
  523. CBB_cleanup(&cbb);
  524. return NULL;
  525. }