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.
 
 
 
 
 
 

446 lines
9.8 KiB

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