You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cbb.c 8.8 KiB

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