Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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