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.
 
 
 
 
 
 

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