1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include <openssl/bytestring.h>
19 #include <openssl/mem.h>
22 static int cbb_init(CBB *cbb, uint8_t *buf, size_t cap) {
23 struct cbb_buffer_st *base;
25 base = OPENSSL_malloc(sizeof(struct cbb_buffer_st));
36 memset(cbb, 0, sizeof(CBB));
38 cbb->is_top_level = 1;
42 int CBB_init(CBB *cbb, size_t initial_capacity) {
45 buf = OPENSSL_malloc(initial_capacity);
46 if (initial_capacity > 0 && buf == NULL) {
50 return cbb_init(cbb, buf, initial_capacity);
53 int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
54 if (!cbb_init(cbb, buf, len)) {
58 cbb->base->can_resize = 0;
62 void CBB_cleanup(CBB *cbb) {
64 if (cbb->base->buf && cbb->base->can_resize) {
65 OPENSSL_free(cbb->base->buf);
67 OPENSSL_free(cbb->base);
72 static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
80 newlen = base->len + len;
81 if (newlen < base->len) {
86 if (newlen > base->cap) {
87 size_t newcap = base->cap * 2;
90 if (!base->can_resize) {
94 if (newcap < base->cap || newcap < newlen) {
97 newbuf = OPENSSL_realloc(base->buf, newcap);
107 *out = base->buf + base->len;
113 static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
121 if (!cbb_buffer_add(base, &buf, len_len)) {
125 for (i = len_len - 1; i < len_len; i--) {
132 int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
133 if (!cbb->is_top_level) {
137 if (!CBB_flush(cbb)) {
141 if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) {
142 /* |out_data| and |out_len| can only be NULL if the CBB is fixed. */
146 if (out_data != NULL) {
147 *out_data = cbb->base->buf;
149 if (out_len != NULL) {
150 *out_len = cbb->base->len;
152 cbb->base->buf = NULL;
157 /* CBB_flush recurses and then writes out any pending length prefix. The
158 * current length of the underlying base is taken to be the length of the
159 * length-prefixed data. */
160 int CBB_flush(CBB *cbb) {
161 size_t child_start, i, len;
163 if (cbb->base == NULL) {
167 if (cbb->child == NULL || cbb->pending_len_len == 0) {
171 child_start = cbb->offset + cbb->pending_len_len;
173 if (!CBB_flush(cbb->child) ||
174 child_start < cbb->offset ||
175 cbb->base->len < child_start) {
179 len = cbb->base->len - child_start;
181 if (cbb->pending_is_asn1) {
182 /* For ASN.1 we assume that we'll only need a single byte for the length.
183 * If that turned out to be incorrect, we have to move the contents along
184 * in order to make space. */
186 uint8_t initial_length_byte;
188 assert (cbb->pending_len_len == 1);
190 if (len > 0xfffffffe) {
193 } else if (len > 0xffffff) {
195 initial_length_byte = 0x80 | 4;
196 } else if (len > 0xffff) {
198 initial_length_byte = 0x80 | 3;
199 } else if (len > 0xff) {
201 initial_length_byte = 0x80 | 2;
202 } else if (len > 0x7f) {
204 initial_length_byte = 0x80 | 1;
207 initial_length_byte = len;
212 /* We need to move the contents along in order to make space. */
213 size_t extra_bytes = len_len - 1;
214 if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) {
217 memmove(cbb->base->buf + child_start + extra_bytes,
218 cbb->base->buf + child_start, len);
220 cbb->base->buf[cbb->offset++] = initial_length_byte;
221 cbb->pending_len_len = len_len - 1;
224 for (i = cbb->pending_len_len - 1; i < cbb->pending_len_len; i--) {
225 cbb->base->buf[cbb->offset + i] = len;
232 cbb->child->base = NULL;
234 cbb->pending_len_len = 0;
235 cbb->pending_is_asn1 = 0;
242 static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents,
244 uint8_t *prefix_bytes;
246 if (!CBB_flush(cbb)) {
250 cbb->offset = cbb->base->len;
251 if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) {
255 memset(prefix_bytes, 0, len_len);
256 memset(out_contents, 0, sizeof(CBB));
257 out_contents->base = cbb->base;
258 cbb->child = out_contents;
259 cbb->pending_len_len = len_len;
260 cbb->pending_is_asn1 = 0;
265 int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
266 return cbb_add_length_prefixed(cbb, out_contents, 1);
269 int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
270 return cbb_add_length_prefixed(cbb, out_contents, 2);
273 int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
274 return cbb_add_length_prefixed(cbb, out_contents, 3);
277 int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag) {
278 if (!CBB_flush(cbb) ||
279 !CBB_add_u8(cbb, tag)) {
283 cbb->offset = cbb->base->len;
284 if (!CBB_add_u8(cbb, 0)) {
288 memset(out_contents, 0, sizeof(CBB));
289 out_contents->base = cbb->base;
290 cbb->child = out_contents;
291 cbb->pending_len_len = 1;
292 cbb->pending_is_asn1 = 1;
297 int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
300 if (!CBB_flush(cbb) ||
301 !cbb_buffer_add(cbb->base, &dest, len)) {
304 memcpy(dest, data, len);
308 int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
309 if (!CBB_flush(cbb) ||
310 !cbb_buffer_add(cbb->base, out_data, len)) {
316 int CBB_add_u8(CBB *cbb, uint8_t value) {
317 if (!CBB_flush(cbb)) {
321 return cbb_buffer_add_u(cbb->base, value, 1);
324 int CBB_add_u16(CBB *cbb, uint16_t value) {
325 if (!CBB_flush(cbb)) {
329 return cbb_buffer_add_u(cbb->base, value, 2);
332 int CBB_add_u24(CBB *cbb, uint32_t value) {
333 if (!CBB_flush(cbb)) {
337 return cbb_buffer_add_u(cbb->base, value, 3);