cbb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/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_top_level = 1;
  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. if (cbb->base) {
  61. // Only top-level |CBB|s are cleaned up. Child |CBB|s are non-owning. They
  62. // are implicitly discarded when the parent is flushed or cleaned up.
  63. assert(cbb->is_top_level);
  64. if (cbb->base->can_resize) {
  65. OPENSSL_free(cbb->base->buf);
  66. }
  67. OPENSSL_free(cbb->base);
  68. }
  69. cbb->base = NULL;
  70. }
  71. static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
  72. size_t len) {
  73. size_t newlen;
  74. if (base == NULL) {
  75. return 0;
  76. }
  77. newlen = base->len + len;
  78. if (newlen < base->len) {
  79. // Overflow
  80. goto err;
  81. }
  82. if (newlen > base->cap) {
  83. size_t newcap = base->cap * 2;
  84. uint8_t *newbuf;
  85. if (!base->can_resize) {
  86. goto err;
  87. }
  88. if (newcap < base->cap || newcap < newlen) {
  89. newcap = newlen;
  90. }
  91. newbuf = OPENSSL_realloc(base->buf, newcap);
  92. if (newbuf == NULL) {
  93. goto err;
  94. }
  95. base->buf = newbuf;
  96. base->cap = newcap;
  97. }
  98. if (out) {
  99. *out = base->buf + base->len;
  100. }
  101. return 1;
  102. err:
  103. base->error = 1;
  104. return 0;
  105. }
  106. static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
  107. size_t len) {
  108. if (!cbb_buffer_reserve(base, out, len)) {
  109. return 0;
  110. }
  111. // This will not overflow or |cbb_buffer_reserve| would have failed.
  112. base->len += len;
  113. return 1;
  114. }
  115. static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
  116. size_t len_len) {
  117. if (len_len == 0) {
  118. return 1;
  119. }
  120. uint8_t *buf;
  121. if (!cbb_buffer_add(base, &buf, len_len)) {
  122. return 0;
  123. }
  124. for (size_t i = len_len - 1; i < len_len; i--) {
  125. buf[i] = v;
  126. v >>= 8;
  127. }
  128. if (v != 0) {
  129. base->error = 1;
  130. return 0;
  131. }
  132. return 1;
  133. }
  134. int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
  135. if (!cbb->is_top_level) {
  136. return 0;
  137. }
  138. if (!CBB_flush(cbb)) {
  139. return 0;
  140. }
  141. if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) {
  142. // |out_data| and |out_len| can only be NULL if the CBB is fixed.
  143. return 0;
  144. }
  145. if (out_data != NULL) {
  146. *out_data = cbb->base->buf;
  147. }
  148. if (out_len != NULL) {
  149. *out_len = cbb->base->len;
  150. }
  151. cbb->base->buf = NULL;
  152. CBB_cleanup(cbb);
  153. return 1;
  154. }
  155. // CBB_flush recurses and then writes out any pending length prefix. The
  156. // current length of the underlying base is taken to be the length of the
  157. // length-prefixed data.
  158. int CBB_flush(CBB *cbb) {
  159. size_t child_start, i, len;
  160. // If |cbb->base| has hit an error, the buffer is in an undefined state, so
  161. // fail all following calls. In particular, |cbb->child| may point to invalid
  162. // memory.
  163. if (cbb->base == NULL || cbb->base->error) {
  164. return 0;
  165. }
  166. if (cbb->child == NULL || cbb->child->pending_len_len == 0) {
  167. return 1;
  168. }
  169. child_start = cbb->child->offset + cbb->child->pending_len_len;
  170. if (!CBB_flush(cbb->child) ||
  171. child_start < cbb->child->offset ||
  172. cbb->base->len < child_start) {
  173. goto err;
  174. }
  175. len = cbb->base->len - child_start;
  176. if (cbb->child->pending_is_asn1) {
  177. // For ASN.1 we assume that we'll only need a single byte for the length.
  178. // If that turned out to be incorrect, we have to move the contents along
  179. // in order to make space.
  180. uint8_t len_len;
  181. uint8_t initial_length_byte;
  182. assert (cbb->child->pending_len_len == 1);
  183. if (len > 0xfffffffe) {
  184. // Too large.
  185. goto err;
  186. } else if (len > 0xffffff) {
  187. len_len = 5;
  188. initial_length_byte = 0x80 | 4;
  189. } else if (len > 0xffff) {
  190. len_len = 4;
  191. initial_length_byte = 0x80 | 3;
  192. } else if (len > 0xff) {
  193. len_len = 3;
  194. initial_length_byte = 0x80 | 2;
  195. } else if (len > 0x7f) {
  196. len_len = 2;
  197. initial_length_byte = 0x80 | 1;
  198. } else {
  199. len_len = 1;
  200. initial_length_byte = (uint8_t)len;
  201. len = 0;
  202. }
  203. if (len_len != 1) {
  204. // We need to move the contents along in order to make space.
  205. size_t extra_bytes = len_len - 1;
  206. if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) {
  207. goto err;
  208. }
  209. OPENSSL_memmove(cbb->base->buf + child_start + extra_bytes,
  210. cbb->base->buf + child_start, len);
  211. }
  212. cbb->base->buf[cbb->child->offset++] = initial_length_byte;
  213. cbb->child->pending_len_len = len_len - 1;
  214. }
  215. for (i = cbb->child->pending_len_len - 1; i < cbb->child->pending_len_len;
  216. i--) {
  217. cbb->base->buf[cbb->child->offset + i] = (uint8_t)len;
  218. len >>= 8;
  219. }
  220. if (len != 0) {
  221. goto err;
  222. }
  223. cbb->child->base = NULL;
  224. cbb->child = NULL;
  225. return 1;
  226. err:
  227. cbb->base->error = 1;
  228. return 0;
  229. }
  230. const uint8_t *CBB_data(const CBB *cbb) {
  231. assert(cbb->child == NULL);
  232. return cbb->base->buf + cbb->offset + cbb->pending_len_len;
  233. }
  234. size_t CBB_len(const CBB *cbb) {
  235. assert(cbb->child == NULL);
  236. assert(cbb->offset + cbb->pending_len_len <= cbb->base->len);
  237. return cbb->base->len - cbb->offset - cbb->pending_len_len;
  238. }
  239. static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents,
  240. uint8_t len_len) {
  241. uint8_t *prefix_bytes;
  242. if (!CBB_flush(cbb)) {
  243. return 0;
  244. }
  245. size_t offset = cbb->base->len;
  246. if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) {
  247. return 0;
  248. }
  249. OPENSSL_memset(prefix_bytes, 0, len_len);
  250. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  251. out_contents->base = cbb->base;
  252. cbb->child = out_contents;
  253. cbb->child->offset = offset;
  254. cbb->child->pending_len_len = len_len;
  255. cbb->child->pending_is_asn1 = 0;
  256. return 1;
  257. }
  258. int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
  259. return cbb_add_length_prefixed(cbb, out_contents, 1);
  260. }
  261. int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
  262. return cbb_add_length_prefixed(cbb, out_contents, 2);
  263. }
  264. int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
  265. return cbb_add_length_prefixed(cbb, out_contents, 3);
  266. }
  267. // add_base128_integer encodes |v| as a big-endian base-128 integer where the
  268. // high bit of each byte indicates where there is more data. This is the
  269. // encoding used in DER for both high tag number form and OID components.
  270. static int add_base128_integer(CBB *cbb, uint32_t v) {
  271. unsigned len_len = 0;
  272. unsigned copy = v;
  273. while (copy > 0) {
  274. len_len++;
  275. copy >>= 7;
  276. }
  277. if (len_len == 0) {
  278. len_len = 1; // Zero is encoded with one byte.
  279. }
  280. for (unsigned i = len_len - 1; i < len_len; i--) {
  281. uint8_t byte = (v >> (7 * i)) & 0x7f;
  282. if (i != 0) {
  283. // The high bit denotes whether there is more data.
  284. byte |= 0x80;
  285. }
  286. if (!CBB_add_u8(cbb, byte)) {
  287. return 0;
  288. }
  289. }
  290. return 1;
  291. }
  292. int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag) {
  293. if (tag > 0xff ||
  294. (tag & 0x1f) == 0x1f) {
  295. // Long form identifier octets are not supported. Further, all current valid
  296. // tag serializations are 8 bits.
  297. cbb->base->error = 1;
  298. return 0;
  299. }
  300. if (!CBB_flush(cbb) ||
  301. // |tag|'s representation matches the DER encoding.
  302. !CBB_add_u8(cbb, (uint8_t)tag)) {
  303. return 0;
  304. }
  305. size_t offset = cbb->base->len;
  306. if (!CBB_add_u8(cbb, 0)) {
  307. return 0;
  308. }
  309. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  310. out_contents->base = cbb->base;
  311. cbb->child = out_contents;
  312. cbb->child->offset = offset;
  313. cbb->child->pending_len_len = 1;
  314. cbb->child->pending_is_asn1 = 1;
  315. return 1;
  316. }
  317. int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
  318. uint8_t *dest;
  319. if (!CBB_flush(cbb) ||
  320. !cbb_buffer_add(cbb->base, &dest, len)) {
  321. return 0;
  322. }
  323. OPENSSL_memcpy(dest, data, len);
  324. return 1;
  325. }
  326. int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
  327. if (!CBB_flush(cbb) ||
  328. !cbb_buffer_add(cbb->base, out_data, len)) {
  329. return 0;
  330. }
  331. return 1;
  332. }
  333. int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) {
  334. if (!CBB_flush(cbb) ||
  335. !cbb_buffer_reserve(cbb->base, out_data, len)) {
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. int CBB_did_write(CBB *cbb, size_t len) {
  341. size_t newlen = cbb->base->len + len;
  342. if (cbb->child != NULL ||
  343. newlen < cbb->base->len ||
  344. newlen > cbb->base->cap) {
  345. return 0;
  346. }
  347. cbb->base->len = newlen;
  348. return 1;
  349. }
  350. int CBB_add_u8(CBB *cbb, uint8_t value) {
  351. if (!CBB_flush(cbb)) {
  352. return 0;
  353. }
  354. return cbb_buffer_add_u(cbb->base, value, 1);
  355. }
  356. int CBB_add_u16(CBB *cbb, uint16_t value) {
  357. if (!CBB_flush(cbb)) {
  358. return 0;
  359. }
  360. return cbb_buffer_add_u(cbb->base, value, 2);
  361. }
  362. int CBB_add_u24(CBB *cbb, uint32_t value) {
  363. if (!CBB_flush(cbb)) {
  364. return 0;
  365. }
  366. return cbb_buffer_add_u(cbb->base, value, 3);
  367. }
  368. int CBB_add_u32(CBB *cbb, uint32_t value) {
  369. if (!CBB_flush(cbb)) {
  370. return 0;
  371. }
  372. return cbb_buffer_add_u(cbb->base, value, 4);
  373. }
  374. void CBB_discard_child(CBB *cbb) {
  375. if (cbb->child == NULL) {
  376. return;
  377. }
  378. cbb->base->len = cbb->child->offset;
  379. cbb->child->base = NULL;
  380. cbb->child = NULL;
  381. }
  382. int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
  383. CBB child;
  384. int started = 0;
  385. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) {
  386. return 0;
  387. }
  388. for (size_t i = 0; i < 8; i++) {
  389. uint8_t byte = (value >> 8*(7-i)) & 0xff;
  390. if (!started) {
  391. if (byte == 0) {
  392. // Don't encode leading zeros.
  393. continue;
  394. }
  395. // If the high bit is set, add a padding byte to make it
  396. // unsigned.
  397. if ((byte & 0x80) && !CBB_add_u8(&child, 0)) {
  398. return 0;
  399. }
  400. started = 1;
  401. }
  402. if (!CBB_add_u8(&child, byte)) {
  403. return 0;
  404. }
  405. }
  406. // 0 is encoded as a single 0, not the empty string.
  407. if (!started && !CBB_add_u8(&child, 0)) {
  408. return 0;
  409. }
  410. return CBB_flush(cbb);
  411. }
  412. // parse_dotted_decimal parses one decimal component from |cbs|, where |cbs| is
  413. // an OID literal, e.g., "1.2.840.113554.4.1.72585". It consumes both the
  414. // component and the dot, so |cbs| may be passed into the function again for the
  415. // next value.
  416. static int parse_dotted_decimal(CBS *cbs, uint32_t *out) {
  417. *out = 0;
  418. int seen_digit = 0;
  419. for (;;) {
  420. // Valid terminators for a component are the end of the string or a
  421. // non-terminal dot. If the string ends with a dot, this is not a valid OID
  422. // string.
  423. uint8_t u;
  424. if (!CBS_get_u8(cbs, &u) ||
  425. (u == '.' && CBS_len(cbs) > 0)) {
  426. break;
  427. }
  428. if (u < '0' || u > '9' ||
  429. // Forbid stray leading zeros.
  430. (seen_digit && *out == 0) ||
  431. // Check for overflow.
  432. *out > UINT32_MAX / 10 ||
  433. *out * 10 > UINT32_MAX - (u - '0')) {
  434. return 0;
  435. }
  436. *out = *out * 10 + (u - '0');
  437. seen_digit = 1;
  438. }
  439. // The empty string is not a legal OID component.
  440. return seen_digit;
  441. }
  442. int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
  443. if (!CBB_flush(cbb)) {
  444. return 0;
  445. }
  446. CBS cbs;
  447. CBS_init(&cbs, (const uint8_t *)text, len);
  448. // OIDs must have at least two components.
  449. uint32_t a, b;
  450. if (!parse_dotted_decimal(&cbs, &a) ||
  451. !parse_dotted_decimal(&cbs, &b)) {
  452. return 0;
  453. }
  454. // The first component is encoded as 40 * |a| + |b|. This assumes that |a| is
  455. // 0, 1, or 2 and that, when it is 0 or 1, |b| is at most 39.
  456. if (a > 2 ||
  457. (a < 2 && b > 39) ||
  458. b > UINT32_MAX - 80 ||
  459. !add_base128_integer(cbb, 40 * a + b)) {
  460. return 0;
  461. }
  462. // The remaining components are encoded unmodified.
  463. while (CBS_len(&cbs) > 0) {
  464. if (!parse_dotted_decimal(&cbs, &a) ||
  465. !add_base128_integer(cbb, a)) {
  466. return 0;
  467. }
  468. }
  469. return 1;
  470. }