Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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