cbb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. #include <openssl/bytestring.h>
  15. #include <assert.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <openssl/buf.h>
  19. #include <openssl/mem.h>
  20. #include "../internal.h"
  21. void CBB_zero(CBB *cbb) {
  22. OPENSSL_memset(cbb, 0, sizeof(CBB));
  23. }
  24. static int cbb_init(CBB *cbb, uint8_t *buf, size_t cap) {
  25. // This assumes that |cbb| has already been zeroed.
  26. struct cbb_buffer_st *base;
  27. base = OPENSSL_malloc(sizeof(struct cbb_buffer_st));
  28. if (base == NULL) {
  29. return 0;
  30. }
  31. base->buf = buf;
  32. base->len = 0;
  33. base->cap = cap;
  34. base->can_resize = 1;
  35. base->error = 0;
  36. cbb->base = base;
  37. cbb->is_top_level = 1;
  38. return 1;
  39. }
  40. int CBB_init(CBB *cbb, size_t initial_capacity) {
  41. CBB_zero(cbb);
  42. uint8_t *buf = OPENSSL_malloc(initial_capacity);
  43. if (initial_capacity > 0 && buf == NULL) {
  44. return 0;
  45. }
  46. if (!cbb_init(cbb, buf, initial_capacity)) {
  47. OPENSSL_free(buf);
  48. return 0;
  49. }
  50. return 1;
  51. }
  52. int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
  53. CBB_zero(cbb);
  54. if (!cbb_init(cbb, buf, len)) {
  55. return 0;
  56. }
  57. cbb->base->can_resize = 0;
  58. return 1;
  59. }
  60. void CBB_cleanup(CBB *cbb) {
  61. if (cbb->base) {
  62. // Only top-level |CBB|s are cleaned up. Child |CBB|s are non-owning. They
  63. // are implicitly discarded when the parent is flushed or cleaned up.
  64. assert(cbb->is_top_level);
  65. if (cbb->base->can_resize) {
  66. OPENSSL_free(cbb->base->buf);
  67. }
  68. OPENSSL_free(cbb->base);
  69. }
  70. cbb->base = NULL;
  71. }
  72. static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
  73. size_t len) {
  74. size_t newlen;
  75. if (base == NULL) {
  76. return 0;
  77. }
  78. newlen = base->len + len;
  79. if (newlen < base->len) {
  80. // Overflow
  81. goto err;
  82. }
  83. if (newlen > base->cap) {
  84. size_t newcap = base->cap * 2;
  85. uint8_t *newbuf;
  86. if (!base->can_resize) {
  87. goto err;
  88. }
  89. if (newcap < base->cap || newcap < newlen) {
  90. newcap = newlen;
  91. }
  92. newbuf = OPENSSL_realloc(base->buf, newcap);
  93. if (newbuf == NULL) {
  94. goto err;
  95. }
  96. base->buf = newbuf;
  97. base->cap = newcap;
  98. }
  99. if (out) {
  100. *out = base->buf + base->len;
  101. }
  102. return 1;
  103. err:
  104. base->error = 1;
  105. return 0;
  106. }
  107. static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
  108. size_t len) {
  109. if (!cbb_buffer_reserve(base, out, len)) {
  110. return 0;
  111. }
  112. // This will not overflow or |cbb_buffer_reserve| would have failed.
  113. base->len += len;
  114. return 1;
  115. }
  116. static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
  117. size_t len_len) {
  118. if (len_len == 0) {
  119. return 1;
  120. }
  121. uint8_t *buf;
  122. if (!cbb_buffer_add(base, &buf, len_len)) {
  123. return 0;
  124. }
  125. for (size_t i = len_len - 1; i < len_len; i--) {
  126. buf[i] = v;
  127. v >>= 8;
  128. }
  129. if (v != 0) {
  130. base->error = 1;
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
  136. if (!cbb->is_top_level) {
  137. return 0;
  138. }
  139. if (!CBB_flush(cbb)) {
  140. return 0;
  141. }
  142. if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) {
  143. // |out_data| and |out_len| can only be NULL if the CBB is fixed.
  144. return 0;
  145. }
  146. if (out_data != NULL) {
  147. *out_data = cbb->base->buf;
  148. }
  149. if (out_len != NULL) {
  150. *out_len = cbb->base->len;
  151. }
  152. cbb->base->buf = NULL;
  153. CBB_cleanup(cbb);
  154. return 1;
  155. }
  156. // CBB_flush recurses and then writes out any pending length prefix. The
  157. // current length of the underlying base is taken to be the length of the
  158. // length-prefixed data.
  159. int CBB_flush(CBB *cbb) {
  160. size_t child_start, i, len;
  161. // If |cbb->base| has hit an error, the buffer is in an undefined state, so
  162. // fail all following calls. In particular, |cbb->child| may point to invalid
  163. // memory.
  164. if (cbb->base == NULL || cbb->base->error) {
  165. return 0;
  166. }
  167. if (cbb->child == NULL || cbb->child->pending_len_len == 0) {
  168. return 1;
  169. }
  170. child_start = cbb->child->offset + cbb->child->pending_len_len;
  171. if (!CBB_flush(cbb->child) ||
  172. child_start < cbb->child->offset ||
  173. cbb->base->len < child_start) {
  174. goto err;
  175. }
  176. len = cbb->base->len - child_start;
  177. if (cbb->child->pending_is_asn1) {
  178. // For ASN.1 we assume that we'll only need a single byte for the length.
  179. // If that turned out to be incorrect, we have to move the contents along
  180. // in order to make space.
  181. uint8_t len_len;
  182. uint8_t initial_length_byte;
  183. assert (cbb->child->pending_len_len == 1);
  184. if (len > 0xfffffffe) {
  185. // Too large.
  186. goto err;
  187. } else if (len > 0xffffff) {
  188. len_len = 5;
  189. initial_length_byte = 0x80 | 4;
  190. } else if (len > 0xffff) {
  191. len_len = 4;
  192. initial_length_byte = 0x80 | 3;
  193. } else if (len > 0xff) {
  194. len_len = 3;
  195. initial_length_byte = 0x80 | 2;
  196. } else if (len > 0x7f) {
  197. len_len = 2;
  198. initial_length_byte = 0x80 | 1;
  199. } else {
  200. len_len = 1;
  201. initial_length_byte = (uint8_t)len;
  202. len = 0;
  203. }
  204. if (len_len != 1) {
  205. // We need to move the contents along in order to make space.
  206. size_t extra_bytes = len_len - 1;
  207. if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) {
  208. goto err;
  209. }
  210. OPENSSL_memmove(cbb->base->buf + child_start + extra_bytes,
  211. cbb->base->buf + child_start, len);
  212. }
  213. cbb->base->buf[cbb->child->offset++] = initial_length_byte;
  214. cbb->child->pending_len_len = len_len - 1;
  215. }
  216. for (i = cbb->child->pending_len_len - 1; i < cbb->child->pending_len_len;
  217. i--) {
  218. cbb->base->buf[cbb->child->offset + i] = (uint8_t)len;
  219. len >>= 8;
  220. }
  221. if (len != 0) {
  222. goto err;
  223. }
  224. cbb->child->base = NULL;
  225. cbb->child = NULL;
  226. return 1;
  227. err:
  228. cbb->base->error = 1;
  229. return 0;
  230. }
  231. const uint8_t *CBB_data(const CBB *cbb) {
  232. assert(cbb->child == NULL);
  233. return cbb->base->buf + cbb->offset + cbb->pending_len_len;
  234. }
  235. size_t CBB_len(const CBB *cbb) {
  236. assert(cbb->child == NULL);
  237. assert(cbb->offset + cbb->pending_len_len <= cbb->base->len);
  238. return cbb->base->len - cbb->offset - cbb->pending_len_len;
  239. }
  240. static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents,
  241. uint8_t len_len) {
  242. uint8_t *prefix_bytes;
  243. if (!CBB_flush(cbb)) {
  244. return 0;
  245. }
  246. size_t offset = cbb->base->len;
  247. if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) {
  248. return 0;
  249. }
  250. OPENSSL_memset(prefix_bytes, 0, len_len);
  251. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  252. out_contents->base = cbb->base;
  253. cbb->child = out_contents;
  254. cbb->child->offset = offset;
  255. cbb->child->pending_len_len = len_len;
  256. cbb->child->pending_is_asn1 = 0;
  257. return 1;
  258. }
  259. int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
  260. return cbb_add_length_prefixed(cbb, out_contents, 1);
  261. }
  262. int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
  263. return cbb_add_length_prefixed(cbb, out_contents, 2);
  264. }
  265. int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
  266. return cbb_add_length_prefixed(cbb, out_contents, 3);
  267. }
  268. // add_base128_integer encodes |v| as a big-endian base-128 integer where the
  269. // high bit of each byte indicates where there is more data. This is the
  270. // encoding used in DER for both high tag number form and OID components.
  271. static int add_base128_integer(CBB *cbb, uint64_t v) {
  272. unsigned len_len = 0;
  273. uint64_t copy = v;
  274. while (copy > 0) {
  275. len_len++;
  276. copy >>= 7;
  277. }
  278. if (len_len == 0) {
  279. len_len = 1; // Zero is encoded with one byte.
  280. }
  281. for (unsigned i = len_len - 1; i < len_len; i--) {
  282. uint8_t byte = (v >> (7 * i)) & 0x7f;
  283. if (i != 0) {
  284. // The high bit denotes whether there is more data.
  285. byte |= 0x80;
  286. }
  287. if (!CBB_add_u8(cbb, byte)) {
  288. return 0;
  289. }
  290. }
  291. return 1;
  292. }
  293. int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag) {
  294. if (!CBB_flush(cbb)) {
  295. return 0;
  296. }
  297. // Split the tag into leading bits and tag number.
  298. uint8_t tag_bits = (tag >> CBS_ASN1_TAG_SHIFT) & 0xe0;
  299. unsigned tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
  300. if (tag_number >= 0x1f) {
  301. // Set all the bits in the tag number to signal high tag number form.
  302. if (!CBB_add_u8(cbb, tag_bits | 0x1f) ||
  303. !add_base128_integer(cbb, tag_number)) {
  304. return 0;
  305. }
  306. } else if (!CBB_add_u8(cbb, tag_bits | tag_number)) {
  307. return 0;
  308. }
  309. size_t offset = cbb->base->len;
  310. if (!CBB_add_u8(cbb, 0)) {
  311. return 0;
  312. }
  313. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  314. out_contents->base = cbb->base;
  315. cbb->child = out_contents;
  316. cbb->child->offset = offset;
  317. cbb->child->pending_len_len = 1;
  318. cbb->child->pending_is_asn1 = 1;
  319. return 1;
  320. }
  321. int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
  322. uint8_t *dest;
  323. if (!CBB_flush(cbb) ||
  324. !cbb_buffer_add(cbb->base, &dest, len)) {
  325. return 0;
  326. }
  327. OPENSSL_memcpy(dest, data, len);
  328. return 1;
  329. }
  330. int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
  331. if (!CBB_flush(cbb) ||
  332. !cbb_buffer_add(cbb->base, out_data, len)) {
  333. return 0;
  334. }
  335. return 1;
  336. }
  337. int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) {
  338. if (!CBB_flush(cbb) ||
  339. !cbb_buffer_reserve(cbb->base, out_data, len)) {
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. int CBB_did_write(CBB *cbb, size_t len) {
  345. size_t newlen = cbb->base->len + len;
  346. if (cbb->child != NULL ||
  347. newlen < cbb->base->len ||
  348. newlen > cbb->base->cap) {
  349. return 0;
  350. }
  351. cbb->base->len = newlen;
  352. return 1;
  353. }
  354. int CBB_add_u8(CBB *cbb, uint8_t value) {
  355. if (!CBB_flush(cbb)) {
  356. return 0;
  357. }
  358. return cbb_buffer_add_u(cbb->base, value, 1);
  359. }
  360. int CBB_add_u16(CBB *cbb, uint16_t value) {
  361. if (!CBB_flush(cbb)) {
  362. return 0;
  363. }
  364. return cbb_buffer_add_u(cbb->base, value, 2);
  365. }
  366. int CBB_add_u24(CBB *cbb, uint32_t value) {
  367. if (!CBB_flush(cbb)) {
  368. return 0;
  369. }
  370. return cbb_buffer_add_u(cbb->base, value, 3);
  371. }
  372. int CBB_add_u32(CBB *cbb, uint32_t value) {
  373. if (!CBB_flush(cbb)) {
  374. return 0;
  375. }
  376. return cbb_buffer_add_u(cbb->base, value, 4);
  377. }
  378. void CBB_discard_child(CBB *cbb) {
  379. if (cbb->child == NULL) {
  380. return;
  381. }
  382. cbb->base->len = cbb->child->offset;
  383. cbb->child->base = NULL;
  384. cbb->child = NULL;
  385. }
  386. int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
  387. CBB child;
  388. int started = 0;
  389. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) {
  390. return 0;
  391. }
  392. for (size_t i = 0; i < 8; i++) {
  393. uint8_t byte = (value >> 8*(7-i)) & 0xff;
  394. if (!started) {
  395. if (byte == 0) {
  396. // Don't encode leading zeros.
  397. continue;
  398. }
  399. // If the high bit is set, add a padding byte to make it
  400. // unsigned.
  401. if ((byte & 0x80) && !CBB_add_u8(&child, 0)) {
  402. return 0;
  403. }
  404. started = 1;
  405. }
  406. if (!CBB_add_u8(&child, byte)) {
  407. return 0;
  408. }
  409. }
  410. // 0 is encoded as a single 0, not the empty string.
  411. if (!started && !CBB_add_u8(&child, 0)) {
  412. return 0;
  413. }
  414. return CBB_flush(cbb);
  415. }
  416. int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) {
  417. CBB child;
  418. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_OCTETSTRING) ||
  419. !CBB_add_bytes(&child, data, data_len) ||
  420. !CBB_flush(cbb)) {
  421. return 0;
  422. }
  423. return 1;
  424. }
  425. int CBB_add_asn1_bool(CBB *cbb, int value) {
  426. CBB child;
  427. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_BOOLEAN) ||
  428. !CBB_add_u8(&child, value != 0 ? 0xff : 0) ||
  429. !CBB_flush(cbb)) {
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. // parse_dotted_decimal parses one decimal component from |cbs|, where |cbs| is
  435. // an OID literal, e.g., "1.2.840.113554.4.1.72585". It consumes both the
  436. // component and the dot, so |cbs| may be passed into the function again for the
  437. // next value.
  438. static int parse_dotted_decimal(CBS *cbs, uint64_t *out) {
  439. *out = 0;
  440. int seen_digit = 0;
  441. for (;;) {
  442. // Valid terminators for a component are the end of the string or a
  443. // non-terminal dot. If the string ends with a dot, this is not a valid OID
  444. // string.
  445. uint8_t u;
  446. if (!CBS_get_u8(cbs, &u) ||
  447. (u == '.' && CBS_len(cbs) > 0)) {
  448. break;
  449. }
  450. if (u < '0' || u > '9' ||
  451. // Forbid stray leading zeros.
  452. (seen_digit && *out == 0) ||
  453. // Check for overflow.
  454. *out > UINT64_MAX / 10 ||
  455. *out * 10 > UINT64_MAX - (u - '0')) {
  456. return 0;
  457. }
  458. *out = *out * 10 + (u - '0');
  459. seen_digit = 1;
  460. }
  461. // The empty string is not a legal OID component.
  462. return seen_digit;
  463. }
  464. int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
  465. if (!CBB_flush(cbb)) {
  466. return 0;
  467. }
  468. CBS cbs;
  469. CBS_init(&cbs, (const uint8_t *)text, len);
  470. // OIDs must have at least two components.
  471. uint64_t a, b;
  472. if (!parse_dotted_decimal(&cbs, &a) ||
  473. !parse_dotted_decimal(&cbs, &b)) {
  474. return 0;
  475. }
  476. // The first component is encoded as 40 * |a| + |b|. This assumes that |a| is
  477. // 0, 1, or 2 and that, when it is 0 or 1, |b| is at most 39.
  478. if (a > 2 ||
  479. (a < 2 && b > 39) ||
  480. b > UINT64_MAX - 80 ||
  481. !add_base128_integer(cbb, 40u * a + b)) {
  482. return 0;
  483. }
  484. // The remaining components are encoded unmodified.
  485. while (CBS_len(&cbs) > 0) {
  486. if (!parse_dotted_decimal(&cbs, &a) ||
  487. !add_base128_integer(cbb, a)) {
  488. return 0;
  489. }
  490. }
  491. return 1;
  492. }
  493. static int compare_set_of_element(const void *a_ptr, const void *b_ptr) {
  494. // See X.690, section 11.6 for the ordering. They are sorted in ascending
  495. // order by their DER encoding.
  496. const CBS *a = a_ptr, *b = b_ptr;
  497. size_t a_len = CBS_len(a), b_len = CBS_len(b);
  498. size_t min_len = a_len < b_len ? a_len : b_len;
  499. int ret = OPENSSL_memcmp(CBS_data(a), CBS_data(b), min_len);
  500. if (ret != 0) {
  501. return ret;
  502. }
  503. if (a_len == b_len) {
  504. return 0;
  505. }
  506. // If one is a prefix of the other, the shorter one sorts first. (This is not
  507. // actually reachable. No DER encoding is a prefix of another DER encoding.)
  508. return a_len < b_len ? -1 : 1;
  509. }
  510. int CBB_flush_asn1_set_of(CBB *cbb) {
  511. if (!CBB_flush(cbb)) {
  512. return 0;
  513. }
  514. CBS cbs;
  515. size_t num_children = 0;
  516. CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb));
  517. while (CBS_len(&cbs) != 0) {
  518. if (!CBS_get_any_asn1_element(&cbs, NULL, NULL, NULL)) {
  519. return 0;
  520. }
  521. num_children++;
  522. }
  523. if (num_children < 2) {
  524. return 1; // Nothing to do. This is the common case for X.509.
  525. }
  526. if (num_children > ((size_t)-1) / sizeof(CBS)) {
  527. return 0; // Overflow.
  528. }
  529. // Parse out the children and sort. We alias them into a copy of so they
  530. // remain valid as we rewrite |cbb|.
  531. int ret = 0;
  532. size_t buf_len = CBB_len(cbb);
  533. uint8_t *buf = BUF_memdup(CBB_data(cbb), buf_len);
  534. CBS *children = OPENSSL_malloc(num_children * sizeof(CBS));
  535. if (buf == NULL || children == NULL) {
  536. goto err;
  537. }
  538. CBS_init(&cbs, buf, buf_len);
  539. for (size_t i = 0; i < num_children; i++) {
  540. if (!CBS_get_any_asn1_element(&cbs, &children[i], NULL, NULL)) {
  541. goto err;
  542. }
  543. }
  544. qsort(children, num_children, sizeof(CBS), compare_set_of_element);
  545. // Rewind |cbb| and write the contents back in the new order.
  546. cbb->base->len = cbb->offset + cbb->pending_len_len;
  547. for (size_t i = 0; i < num_children; i++) {
  548. if (!CBB_add_bytes(cbb, CBS_data(&children[i]), CBS_len(&children[i]))) {
  549. goto err;
  550. }
  551. }
  552. assert(CBB_len(cbb) == buf_len);
  553. ret = 1;
  554. err:
  555. OPENSSL_free(buf);
  556. OPENSSL_free(children);
  557. return ret;
  558. }