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