cbb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <string.h>
  17. #include <openssl/mem.h>
  18. #include "../internal.h"
  19. void CBB_zero(CBB *cbb) {
  20. OPENSSL_memset(cbb, 0, sizeof(CBB));
  21. }
  22. static int cbb_init(CBB *cbb, uint8_t *buf, size_t cap) {
  23. /* This assumes that |cbb| has already been zeroed. */
  24. struct cbb_buffer_st *base;
  25. base = OPENSSL_malloc(sizeof(struct cbb_buffer_st));
  26. if (base == NULL) {
  27. return 0;
  28. }
  29. base->buf = buf;
  30. base->len = 0;
  31. base->cap = cap;
  32. base->can_resize = 1;
  33. base->error = 0;
  34. cbb->base = base;
  35. cbb->is_top_level = 1;
  36. return 1;
  37. }
  38. int CBB_init(CBB *cbb, size_t initial_capacity) {
  39. CBB_zero(cbb);
  40. uint8_t *buf = OPENSSL_malloc(initial_capacity);
  41. if (initial_capacity > 0 && buf == NULL) {
  42. return 0;
  43. }
  44. if (!cbb_init(cbb, buf, initial_capacity)) {
  45. OPENSSL_free(buf);
  46. return 0;
  47. }
  48. return 1;
  49. }
  50. int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
  51. CBB_zero(cbb);
  52. if (!cbb_init(cbb, buf, len)) {
  53. return 0;
  54. }
  55. cbb->base->can_resize = 0;
  56. return 1;
  57. }
  58. void CBB_cleanup(CBB *cbb) {
  59. if (cbb->base) {
  60. /* Only top-level |CBB|s are cleaned up. Child |CBB|s are non-owning. They
  61. * are implicitly discarded when the parent is flushed or cleaned up. */
  62. assert(cbb->is_top_level);
  63. if (cbb->base->can_resize) {
  64. OPENSSL_free(cbb->base->buf);
  65. }
  66. OPENSSL_free(cbb->base);
  67. }
  68. cbb->base = NULL;
  69. }
  70. static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
  71. size_t len) {
  72. size_t newlen;
  73. if (base == NULL) {
  74. return 0;
  75. }
  76. newlen = base->len + len;
  77. if (newlen < base->len) {
  78. /* Overflow */
  79. goto err;
  80. }
  81. if (newlen > base->cap) {
  82. size_t newcap = base->cap * 2;
  83. uint8_t *newbuf;
  84. if (!base->can_resize) {
  85. goto err;
  86. }
  87. if (newcap < base->cap || newcap < newlen) {
  88. newcap = newlen;
  89. }
  90. newbuf = OPENSSL_realloc(base->buf, newcap);
  91. if (newbuf == NULL) {
  92. goto err;
  93. }
  94. base->buf = newbuf;
  95. base->cap = newcap;
  96. }
  97. if (out) {
  98. *out = base->buf + base->len;
  99. }
  100. return 1;
  101. err:
  102. base->error = 1;
  103. return 0;
  104. }
  105. static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
  106. size_t len) {
  107. if (!cbb_buffer_reserve(base, out, len)) {
  108. return 0;
  109. }
  110. /* This will not overflow or |cbb_buffer_reserve| would have failed. */
  111. base->len += len;
  112. return 1;
  113. }
  114. static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
  115. size_t len_len) {
  116. if (len_len == 0) {
  117. return 1;
  118. }
  119. uint8_t *buf;
  120. if (!cbb_buffer_add(base, &buf, len_len)) {
  121. return 0;
  122. }
  123. for (size_t i = len_len - 1; i < len_len; i--) {
  124. buf[i] = v;
  125. v >>= 8;
  126. }
  127. if (v != 0) {
  128. base->error = 1;
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
  134. if (!cbb->is_top_level) {
  135. return 0;
  136. }
  137. if (!CBB_flush(cbb)) {
  138. return 0;
  139. }
  140. if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) {
  141. /* |out_data| and |out_len| can only be NULL if the CBB is fixed. */
  142. return 0;
  143. }
  144. if (out_data != NULL) {
  145. *out_data = cbb->base->buf;
  146. }
  147. if (out_len != NULL) {
  148. *out_len = cbb->base->len;
  149. }
  150. cbb->base->buf = NULL;
  151. CBB_cleanup(cbb);
  152. return 1;
  153. }
  154. /* CBB_flush recurses and then writes out any pending length prefix. The
  155. * current length of the underlying base is taken to be the length of the
  156. * length-prefixed data. */
  157. int CBB_flush(CBB *cbb) {
  158. size_t child_start, i, len;
  159. /* If |cbb->base| has hit an error, the buffer is in an undefined state, so
  160. * fail all following calls. In particular, |cbb->child| may point to invalid
  161. * memory. */
  162. if (cbb->base == NULL || cbb->base->error) {
  163. return 0;
  164. }
  165. if (cbb->child == NULL || cbb->child->pending_len_len == 0) {
  166. return 1;
  167. }
  168. child_start = cbb->child->offset + cbb->child->pending_len_len;
  169. if (!CBB_flush(cbb->child) ||
  170. child_start < cbb->child->offset ||
  171. cbb->base->len < child_start) {
  172. goto err;
  173. }
  174. len = cbb->base->len - child_start;
  175. if (cbb->child->pending_is_asn1) {
  176. /* For ASN.1 we assume that we'll only need a single byte for the length.
  177. * If that turned out to be incorrect, we have to move the contents along
  178. * in order to make space. */
  179. uint8_t len_len;
  180. uint8_t initial_length_byte;
  181. assert (cbb->child->pending_len_len == 1);
  182. if (len > 0xfffffffe) {
  183. /* Too large. */
  184. goto err;
  185. } else if (len > 0xffffff) {
  186. len_len = 5;
  187. initial_length_byte = 0x80 | 4;
  188. } else if (len > 0xffff) {
  189. len_len = 4;
  190. initial_length_byte = 0x80 | 3;
  191. } else if (len > 0xff) {
  192. len_len = 3;
  193. initial_length_byte = 0x80 | 2;
  194. } else if (len > 0x7f) {
  195. len_len = 2;
  196. initial_length_byte = 0x80 | 1;
  197. } else {
  198. len_len = 1;
  199. initial_length_byte = (uint8_t)len;
  200. len = 0;
  201. }
  202. if (len_len != 1) {
  203. /* We need to move the contents along in order to make space. */
  204. size_t extra_bytes = len_len - 1;
  205. if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) {
  206. goto err;
  207. }
  208. OPENSSL_memmove(cbb->base->buf + child_start + extra_bytes,
  209. cbb->base->buf + child_start, len);
  210. }
  211. cbb->base->buf[cbb->child->offset++] = initial_length_byte;
  212. cbb->child->pending_len_len = len_len - 1;
  213. }
  214. for (i = cbb->child->pending_len_len - 1; i < cbb->child->pending_len_len;
  215. i--) {
  216. cbb->base->buf[cbb->child->offset + i] = (uint8_t)len;
  217. len >>= 8;
  218. }
  219. if (len != 0) {
  220. goto err;
  221. }
  222. cbb->child->base = NULL;
  223. cbb->child = NULL;
  224. return 1;
  225. err:
  226. cbb->base->error = 1;
  227. return 0;
  228. }
  229. const uint8_t *CBB_data(const CBB *cbb) {
  230. assert(cbb->child == NULL);
  231. return cbb->base->buf + cbb->offset + cbb->pending_len_len;
  232. }
  233. size_t CBB_len(const CBB *cbb) {
  234. assert(cbb->child == NULL);
  235. assert(cbb->offset + cbb->pending_len_len <= cbb->base->len);
  236. return cbb->base->len - cbb->offset - cbb->pending_len_len;
  237. }
  238. static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents,
  239. uint8_t len_len) {
  240. uint8_t *prefix_bytes;
  241. if (!CBB_flush(cbb)) {
  242. return 0;
  243. }
  244. size_t offset = cbb->base->len;
  245. if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) {
  246. return 0;
  247. }
  248. OPENSSL_memset(prefix_bytes, 0, len_len);
  249. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  250. out_contents->base = cbb->base;
  251. cbb->child = out_contents;
  252. cbb->child->offset = offset;
  253. cbb->child->pending_len_len = len_len;
  254. cbb->child->pending_is_asn1 = 0;
  255. return 1;
  256. }
  257. int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
  258. return cbb_add_length_prefixed(cbb, out_contents, 1);
  259. }
  260. int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
  261. return cbb_add_length_prefixed(cbb, out_contents, 2);
  262. }
  263. int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
  264. return cbb_add_length_prefixed(cbb, out_contents, 3);
  265. }
  266. int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag) {
  267. if (tag > 0xff ||
  268. (tag & 0x1f) == 0x1f) {
  269. /* Long form identifier octets are not supported. Further, all current valid
  270. * tag serializations are 8 bits. */
  271. cbb->base->error = 1;
  272. return 0;
  273. }
  274. if (!CBB_flush(cbb) ||
  275. /* |tag|'s representation matches the DER encoding. */
  276. !CBB_add_u8(cbb, (uint8_t)tag)) {
  277. return 0;
  278. }
  279. size_t offset = cbb->base->len;
  280. if (!CBB_add_u8(cbb, 0)) {
  281. return 0;
  282. }
  283. OPENSSL_memset(out_contents, 0, sizeof(CBB));
  284. out_contents->base = cbb->base;
  285. cbb->child = out_contents;
  286. cbb->child->offset = offset;
  287. cbb->child->pending_len_len = 1;
  288. cbb->child->pending_is_asn1 = 1;
  289. return 1;
  290. }
  291. int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
  292. uint8_t *dest;
  293. if (!CBB_flush(cbb) ||
  294. !cbb_buffer_add(cbb->base, &dest, len)) {
  295. return 0;
  296. }
  297. OPENSSL_memcpy(dest, data, len);
  298. return 1;
  299. }
  300. int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
  301. if (!CBB_flush(cbb) ||
  302. !cbb_buffer_add(cbb->base, out_data, len)) {
  303. return 0;
  304. }
  305. return 1;
  306. }
  307. int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) {
  308. if (!CBB_flush(cbb) ||
  309. !cbb_buffer_reserve(cbb->base, out_data, len)) {
  310. return 0;
  311. }
  312. return 1;
  313. }
  314. int CBB_did_write(CBB *cbb, size_t len) {
  315. size_t newlen = cbb->base->len + len;
  316. if (cbb->child != NULL ||
  317. newlen < cbb->base->len ||
  318. newlen > cbb->base->cap) {
  319. return 0;
  320. }
  321. cbb->base->len = newlen;
  322. return 1;
  323. }
  324. int CBB_add_u8(CBB *cbb, uint8_t value) {
  325. if (!CBB_flush(cbb)) {
  326. return 0;
  327. }
  328. return cbb_buffer_add_u(cbb->base, value, 1);
  329. }
  330. int CBB_add_u16(CBB *cbb, uint16_t value) {
  331. if (!CBB_flush(cbb)) {
  332. return 0;
  333. }
  334. return cbb_buffer_add_u(cbb->base, value, 2);
  335. }
  336. int CBB_add_u24(CBB *cbb, uint32_t value) {
  337. if (!CBB_flush(cbb)) {
  338. return 0;
  339. }
  340. return cbb_buffer_add_u(cbb->base, value, 3);
  341. }
  342. int CBB_add_u32(CBB *cbb, uint32_t value) {
  343. if (!CBB_flush(cbb)) {
  344. return 0;
  345. }
  346. return cbb_buffer_add_u(cbb->base, value, 4);
  347. }
  348. void CBB_discard_child(CBB *cbb) {
  349. if (cbb->child == NULL) {
  350. return;
  351. }
  352. cbb->base->len = cbb->child->offset;
  353. cbb->child->base = NULL;
  354. cbb->child = NULL;
  355. }
  356. int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
  357. CBB child;
  358. int started = 0;
  359. if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) {
  360. return 0;
  361. }
  362. for (size_t i = 0; i < 8; i++) {
  363. uint8_t byte = (value >> 8*(7-i)) & 0xff;
  364. if (!started) {
  365. if (byte == 0) {
  366. /* Don't encode leading zeros. */
  367. continue;
  368. }
  369. /* If the high bit is set, add a padding byte to make it
  370. * unsigned. */
  371. if ((byte & 0x80) && !CBB_add_u8(&child, 0)) {
  372. return 0;
  373. }
  374. started = 1;
  375. }
  376. if (!CBB_add_u8(&child, byte)) {
  377. return 0;
  378. }
  379. }
  380. /* 0 is encoded as a single 0, not the empty string. */
  381. if (!started && !CBB_add_u8(&child, 0)) {
  382. return 0;
  383. }
  384. return CBB_flush(cbb);
  385. }