cbb.c 17 KB

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