/src/boringssl/crypto/bytestring/cbb.cc
Line | Count | Source |
1 | | // Copyright 2014 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/bytestring.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <limits.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/mem.h> |
23 | | |
24 | | #include "../internal.h" |
25 | | #include "internal.h" |
26 | | |
27 | | |
28 | | using namespace bssl; |
29 | | |
30 | 15.2M | void CBB_zero(CBB *cbb) { OPENSSL_memset(cbb, 0, sizeof(CBB)); } |
31 | | |
32 | 1.87M | static void cbb_init(CBB *cbb, uint8_t *buf, size_t cap, int can_resize) { |
33 | 1.87M | cbb->is_child = 0; |
34 | 1.87M | cbb->child = nullptr; |
35 | 1.87M | cbb->u.base.buf = buf; |
36 | 1.87M | cbb->u.base.len = 0; |
37 | 1.87M | cbb->u.base.cap = cap; |
38 | 1.87M | cbb->u.base.can_resize = can_resize; |
39 | 1.87M | cbb->u.base.error = 0; |
40 | 1.87M | } |
41 | | |
42 | 1.74M | int CBB_init(CBB *cbb, size_t initial_capacity) { |
43 | 1.74M | CBB_zero(cbb); |
44 | | |
45 | 1.74M | uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(initial_capacity)); |
46 | 1.74M | if (initial_capacity > 0 && buf == nullptr) { |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | 1.74M | cbb_init(cbb, buf, initial_capacity, /*can_resize=*/1); |
51 | 1.74M | return 1; |
52 | 1.74M | } |
53 | | |
54 | 130k | int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) { |
55 | 130k | CBB_zero(cbb); |
56 | 130k | cbb_init(cbb, buf, len, /*can_resize=*/0); |
57 | 130k | return 1; |
58 | 130k | } |
59 | | |
60 | 2.49M | void CBB_cleanup(CBB *cbb) { |
61 | | // Child |CBB|s are non-owning. They are implicitly discarded and should not |
62 | | // be used with |CBB_cleanup| or |ScopedCBB|. |
63 | 2.49M | assert(!cbb->is_child); |
64 | 2.49M | if (cbb->is_child) { |
65 | 0 | return; |
66 | 0 | } |
67 | | |
68 | 2.49M | if (cbb->u.base.can_resize) { |
69 | 2.39M | OPENSSL_free(cbb->u.base.buf); |
70 | 2.39M | } |
71 | 2.49M | } |
72 | | |
73 | | static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out, |
74 | 200M | size_t len) { |
75 | 200M | if (base == nullptr) { |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | 200M | size_t newlen = base->len + len; |
80 | 200M | if (newlen < base->len) { |
81 | | // Overflow |
82 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
83 | 0 | goto err; |
84 | 0 | } |
85 | | |
86 | 200M | if (newlen > base->cap) { |
87 | 2.34M | if (!base->can_resize) { |
88 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
89 | 0 | goto err; |
90 | 0 | } |
91 | | |
92 | 2.34M | size_t newcap = base->cap * 2; |
93 | 2.34M | if (newcap < base->cap || newcap < newlen) { |
94 | 340k | newcap = newlen; |
95 | 340k | } |
96 | 2.34M | uint8_t *newbuf = |
97 | 2.34M | reinterpret_cast<uint8_t *>(OPENSSL_realloc(base->buf, newcap)); |
98 | 2.34M | if (newbuf == nullptr) { |
99 | 0 | goto err; |
100 | 0 | } |
101 | | |
102 | 2.34M | base->buf = newbuf; |
103 | 2.34M | base->cap = newcap; |
104 | 2.34M | } |
105 | | |
106 | 200M | if (out) { |
107 | 199M | *out = base->buf + base->len; |
108 | 199M | } |
109 | | |
110 | 200M | return 1; |
111 | | |
112 | 0 | err: |
113 | 0 | base->error = 1; |
114 | 0 | return 0; |
115 | 200M | } |
116 | | |
117 | | static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out, |
118 | 200M | size_t len) { |
119 | 200M | if (!cbb_buffer_reserve(base, out, len)) { |
120 | 0 | return 0; |
121 | 0 | } |
122 | | // This will not overflow or |cbb_buffer_reserve| would have failed. |
123 | 200M | base->len += len; |
124 | 200M | return 1; |
125 | 200M | } |
126 | | |
127 | 1.41M | int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) { |
128 | 1.41M | if (cbb->is_child) { |
129 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
130 | 0 | return 0; |
131 | 0 | } |
132 | | |
133 | 1.41M | if (!CBB_flush(cbb)) { |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | 1.41M | if (cbb->u.base.can_resize && (out_data == nullptr || out_len == nullptr)) { |
138 | | // |out_data| and |out_len| can only be NULL if the CBB is fixed. |
139 | 0 | return 0; |
140 | 0 | } |
141 | | |
142 | 1.41M | if (out_data != nullptr) { |
143 | 1.36M | *out_data = cbb->u.base.buf; |
144 | 1.36M | } |
145 | 1.41M | if (out_len != nullptr) { |
146 | 1.36M | *out_len = cbb->u.base.len; |
147 | 1.36M | } |
148 | 1.41M | cbb->u.base.buf = nullptr; |
149 | 1.41M | CBB_cleanup(cbb); |
150 | 1.41M | return 1; |
151 | 1.41M | } |
152 | | |
153 | 422M | static struct cbb_buffer_st *cbb_get_base(CBB *cbb) { |
154 | 422M | if (cbb->is_child) { |
155 | 106M | return cbb->u.child.base; |
156 | 106M | } |
157 | 316M | return &cbb->u.base; |
158 | 422M | } |
159 | | |
160 | 0 | static void cbb_on_error(CBB *cbb) { |
161 | | // Due to C's lack of destructors and |CBB|'s auto-flushing API, a failing |
162 | | // |CBB|-taking function may leave a dangling pointer to a child |CBB|. As a |
163 | | // result, the convention is callers may not write to |CBB|s that have failed. |
164 | | // But, as a safety measure, we lock the |CBB| into an error state. Once the |
165 | | // error bit is set, |cbb->child| will not be read. |
166 | | // |
167 | | // TODO(davidben): This still isn't quite ideal. A |CBB| function *outside* |
168 | | // this file may originate an error while the |CBB| points to a local child. |
169 | | // In that case we don't set the error bit and are reliant on the error |
170 | | // convention. Perhaps we allow |CBB_cleanup| on child |CBB|s and make every |
171 | | // child's |CBB_cleanup| set the error bit if unflushed. That will be |
172 | | // convenient for C++ callers, but very tedious for C callers. So C callers |
173 | | // perhaps should get a |CBB_on_error| function that can be, less tediously, |
174 | | // stuck in a |goto err| block. |
175 | 0 | cbb_get_base(cbb)->error = 1; |
176 | | |
177 | | // Clearing the pointer is not strictly necessary, but GCC's dangling pointer |
178 | | // warning does not know |cbb->child| will not be read once |error| is set |
179 | | // above. |
180 | 0 | cbb->child = nullptr; |
181 | 0 | } |
182 | | |
183 | | // CBB_flush recurses and then writes out any pending length prefix. The |
184 | | // current length of the underlying base is taken to be the length of the |
185 | | // length-prefixed data. |
186 | 223M | int CBB_flush(CBB *cbb) { |
187 | | // If |base| has hit an error, the buffer is in an undefined state, so |
188 | | // fail all following calls. In particular, |cbb->child| may point to invalid |
189 | | // memory. |
190 | 223M | struct cbb_buffer_st *base = cbb_get_base(cbb); |
191 | 223M | if (base == nullptr || base->error) { |
192 | 0 | return 0; |
193 | 0 | } |
194 | | |
195 | 223M | if (cbb->child == nullptr) { |
196 | | // Nothing to flush. |
197 | 211M | return 1; |
198 | 211M | } |
199 | | |
200 | 223M | assert(cbb->child->is_child); |
201 | 11.8M | struct cbb_child_st *child = &cbb->child->u.child; |
202 | 11.8M | assert(child->base == base); |
203 | 11.8M | size_t child_start = child->offset + child->pending_len_len; |
204 | | |
205 | 11.8M | size_t len; |
206 | 11.8M | if (!CBB_flush(cbb->child) || child_start < child->offset || |
207 | 11.8M | base->len < child_start) { |
208 | 0 | goto err; |
209 | 0 | } |
210 | | |
211 | 11.8M | len = base->len - child_start; |
212 | | |
213 | 11.8M | if (child->pending_is_asn1) { |
214 | | // For ASN.1 we assume that we'll only need a single byte for the length. |
215 | | // If that turned out to be incorrect, we have to move the contents along |
216 | | // in order to make space. |
217 | 9.87M | uint8_t len_len; |
218 | 9.87M | uint8_t initial_length_byte; |
219 | | |
220 | 9.87M | assert(child->pending_len_len == 1); |
221 | | |
222 | 9.87M | if (len > 0xfffffffe) { |
223 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
224 | | // Too large. |
225 | 0 | goto err; |
226 | 9.87M | } else if (len > 0xffffff) { |
227 | 0 | len_len = 5; |
228 | 0 | initial_length_byte = 0x80 | 4; |
229 | 9.87M | } else if (len > 0xffff) { |
230 | 10.9k | len_len = 4; |
231 | 10.9k | initial_length_byte = 0x80 | 3; |
232 | 9.86M | } else if (len > 0xff) { |
233 | 435k | len_len = 3; |
234 | 435k | initial_length_byte = 0x80 | 2; |
235 | 9.42M | } else if (len > 0x7f) { |
236 | 629k | len_len = 2; |
237 | 629k | initial_length_byte = 0x80 | 1; |
238 | 8.79M | } else { |
239 | 8.79M | len_len = 1; |
240 | 8.79M | initial_length_byte = (uint8_t)len; |
241 | 8.79M | len = 0; |
242 | 8.79M | } |
243 | | |
244 | 9.87M | if (len_len != 1) { |
245 | | // We need to move the contents along in order to make space. |
246 | 1.07M | size_t extra_bytes = len_len - 1; |
247 | 1.07M | if (!cbb_buffer_add(base, nullptr, extra_bytes)) { |
248 | 0 | goto err; |
249 | 0 | } |
250 | 1.07M | OPENSSL_memmove(base->buf + child_start + extra_bytes, |
251 | 1.07M | base->buf + child_start, len); |
252 | 1.07M | } |
253 | 9.87M | base->buf[child->offset++] = initial_length_byte; |
254 | 9.87M | child->pending_len_len = len_len - 1; |
255 | 9.87M | } |
256 | | |
257 | 16.9M | for (size_t i = child->pending_len_len - 1; i < child->pending_len_len; i--) { |
258 | 5.04M | base->buf[child->offset + i] = (uint8_t)len; |
259 | 5.04M | len >>= 8; |
260 | 5.04M | } |
261 | 11.8M | if (len != 0) { |
262 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
263 | 0 | goto err; |
264 | 0 | } |
265 | | |
266 | 11.8M | child->base = nullptr; |
267 | 11.8M | cbb->child = nullptr; |
268 | | |
269 | 11.8M | return 1; |
270 | | |
271 | 0 | err: |
272 | 0 | cbb_on_error(cbb); |
273 | 0 | return 0; |
274 | 11.8M | } |
275 | | |
276 | 2.48M | uint8_t *CBB_data(const CBB *cbb) { |
277 | 2.48M | assert(cbb->child == nullptr); |
278 | 2.48M | if (cbb->is_child) { |
279 | 1.92M | return cbb->u.child.base->buf + cbb->u.child.offset + |
280 | 1.92M | cbb->u.child.pending_len_len; |
281 | 1.92M | } |
282 | 562k | return cbb->u.base.buf; |
283 | 2.48M | } |
284 | | |
285 | 5.36M | size_t CBB_len(const CBB *cbb) { |
286 | 5.36M | assert(cbb->child == nullptr); |
287 | 5.36M | if (cbb->is_child) { |
288 | 4.77M | assert(cbb->u.child.offset + cbb->u.child.pending_len_len <= |
289 | 4.77M | cbb->u.child.base->len); |
290 | 4.77M | return cbb->u.child.base->len - cbb->u.child.offset - |
291 | 4.77M | cbb->u.child.pending_len_len; |
292 | 4.77M | } |
293 | 588k | return cbb->u.base.len; |
294 | 5.36M | } |
295 | | |
296 | | static int cbb_add_child(CBB *cbb, CBB *out_child, uint8_t len_len, |
297 | 11.9M | int is_asn1) { |
298 | 11.9M | assert(cbb->child == nullptr); |
299 | 11.9M | assert(!is_asn1 || len_len == 1); |
300 | 11.9M | struct cbb_buffer_st *base = cbb_get_base(cbb); |
301 | 11.9M | size_t offset = base->len; |
302 | | |
303 | | // Reserve space for the length prefix. |
304 | 11.9M | uint8_t *prefix_bytes; |
305 | 11.9M | if (!cbb_buffer_add(base, &prefix_bytes, len_len)) { |
306 | 0 | return 0; |
307 | 0 | } |
308 | 11.9M | OPENSSL_memset(prefix_bytes, 0, len_len); |
309 | | |
310 | 11.9M | CBB_zero(out_child); |
311 | 11.9M | out_child->is_child = 1; |
312 | 11.9M | out_child->u.child.base = base; |
313 | 11.9M | out_child->u.child.offset = offset; |
314 | 11.9M | out_child->u.child.pending_len_len = len_len; |
315 | 11.9M | out_child->u.child.pending_is_asn1 = is_asn1; |
316 | 11.9M | cbb->child = out_child; |
317 | 11.9M | return 1; |
318 | 11.9M | } |
319 | | |
320 | | static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents, |
321 | 1.99M | uint8_t len_len) { |
322 | 1.99M | if (!CBB_flush(cbb)) { |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | 1.99M | return cbb_add_child(cbb, out_contents, len_len, /*is_asn1=*/0); |
327 | 1.99M | } |
328 | | |
329 | 746k | int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) { |
330 | 746k | return cbb_add_length_prefixed(cbb, out_contents, 1); |
331 | 746k | } |
332 | | |
333 | 973k | int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) { |
334 | 973k | return cbb_add_length_prefixed(cbb, out_contents, 2); |
335 | 973k | } |
336 | | |
337 | 275k | int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) { |
338 | 275k | return cbb_add_length_prefixed(cbb, out_contents, 3); |
339 | 275k | } |
340 | | |
341 | | // add_base128_integer encodes |v| as a big-endian base-128 integer where the |
342 | | // high bit of each byte indicates where there is more data. This is the |
343 | | // encoding used in DER for both high tag number form and OID components. |
344 | 33.6k | static int add_base128_integer(CBB *cbb, uint64_t v) { |
345 | 33.6k | unsigned len_len = 0; |
346 | 33.6k | uint64_t copy = v; |
347 | 90.8k | while (copy > 0) { |
348 | 57.1k | len_len++; |
349 | 57.1k | copy >>= 7; |
350 | 57.1k | } |
351 | 33.6k | if (len_len == 0) { |
352 | 5.63k | len_len = 1; // Zero is encoded with one byte. |
353 | 5.63k | } |
354 | 96.4k | for (unsigned i = len_len - 1; i < len_len; i--) { |
355 | 62.8k | uint8_t byte = (v >> (7 * i)) & 0x7f; |
356 | 62.8k | if (i != 0) { |
357 | | // The high bit denotes whether there is more data. |
358 | 29.1k | byte |= 0x80; |
359 | 29.1k | } |
360 | 62.8k | if (!CBB_add_u8(cbb, byte)) { |
361 | 0 | return 0; |
362 | 0 | } |
363 | 62.8k | } |
364 | 33.6k | return 1; |
365 | 33.6k | } |
366 | | |
367 | 9.90M | int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) { |
368 | 9.90M | if (!CBB_flush(cbb)) { |
369 | 0 | return 0; |
370 | 0 | } |
371 | | |
372 | | // Split the tag into leading bits and tag number. |
373 | 9.90M | uint8_t tag_bits = (tag >> CBS_ASN1_TAG_SHIFT) & 0xe0; |
374 | 9.90M | CBS_ASN1_TAG tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK; |
375 | 9.90M | if (tag_number >= 0x1f) { |
376 | | // Set all the bits in the tag number to signal high tag number form. |
377 | 9.07k | if (!CBB_add_u8(cbb, tag_bits | 0x1f) || |
378 | 9.07k | !add_base128_integer(cbb, tag_number)) { |
379 | 0 | return 0; |
380 | 0 | } |
381 | 9.89M | } else if (!CBB_add_u8(cbb, tag_bits | tag_number)) { |
382 | 0 | return 0; |
383 | 0 | } |
384 | | |
385 | | // Reserve one byte of length prefix. |CBB_flush| will finish it later. |
386 | 9.90M | return cbb_add_child(cbb, out_contents, /*len_len=*/1, /*is_asn1=*/1); |
387 | 9.90M | } |
388 | | |
389 | 6.50M | int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) { |
390 | 6.50M | uint8_t *out; |
391 | 6.50M | if (!CBB_add_space(cbb, &out, len)) { |
392 | 0 | return 0; |
393 | 0 | } |
394 | 6.50M | OPENSSL_memcpy(out, data, len); |
395 | 6.50M | return 1; |
396 | 6.50M | } |
397 | | |
398 | 503 | int CBB_add_zeros(CBB *cbb, size_t len) { |
399 | 503 | uint8_t *out; |
400 | 503 | if (!CBB_add_space(cbb, &out, len)) { |
401 | 0 | return 0; |
402 | 0 | } |
403 | 503 | OPENSSL_memset(out, 0, len); |
404 | 503 | return 1; |
405 | 503 | } |
406 | | |
407 | 187M | int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) { |
408 | 187M | if (!CBB_flush(cbb) || !cbb_buffer_add(cbb_get_base(cbb), out_data, len)) { |
409 | 0 | return 0; |
410 | 0 | } |
411 | 187M | return 1; |
412 | 187M | } |
413 | | |
414 | 26.2k | int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) { |
415 | 26.2k | if (!CBB_flush(cbb) || |
416 | 26.2k | !cbb_buffer_reserve(cbb_get_base(cbb), out_data, len)) { |
417 | 0 | return 0; |
418 | 0 | } |
419 | 26.2k | return 1; |
420 | 26.2k | } |
421 | | |
422 | 26.2k | int CBB_did_write(CBB *cbb, size_t len) { |
423 | 26.2k | struct cbb_buffer_st *base = cbb_get_base(cbb); |
424 | 26.2k | size_t newlen = base->len + len; |
425 | 26.2k | if (cbb->child != nullptr || newlen < base->len || newlen > base->cap) { |
426 | 0 | return 0; |
427 | 0 | } |
428 | 26.2k | base->len = newlen; |
429 | 26.2k | return 1; |
430 | 26.2k | } |
431 | | |
432 | 180M | static int cbb_add_u(CBB *cbb, uint64_t v, size_t len_len) { |
433 | 180M | uint8_t *buf; |
434 | 180M | if (!CBB_add_space(cbb, &buf, len_len)) { |
435 | 0 | return 0; |
436 | 0 | } |
437 | | |
438 | 762M | for (size_t i = len_len - 1; i < len_len; i--) { |
439 | 581M | buf[i] = v; |
440 | 581M | v >>= 8; |
441 | 581M | } |
442 | | |
443 | | // |v| must fit in |len_len| bytes. |
444 | 180M | if (v != 0) { |
445 | 0 | cbb_on_error(cbb); |
446 | 0 | return 0; |
447 | 0 | } |
448 | | |
449 | 180M | return 1; |
450 | 180M | } |
451 | | |
452 | 44.6M | int CBB_add_u8(CBB *cbb, uint8_t value) { return cbb_add_u(cbb, value, 1); } |
453 | | |
454 | 3.37M | int CBB_add_u16(CBB *cbb, uint16_t value) { return cbb_add_u(cbb, value, 2); } |
455 | | |
456 | 0 | int CBB_add_u16le(CBB *cbb, uint16_t value) { |
457 | 0 | return CBB_add_u16(cbb, CRYPTO_bswap2(value)); |
458 | 0 | } |
459 | | |
460 | 278k | int CBB_add_u24(CBB *cbb, uint32_t value) { return cbb_add_u(cbb, value, 3); } |
461 | | |
462 | 132M | int CBB_add_u32(CBB *cbb, uint32_t value) { return cbb_add_u(cbb, value, 4); } |
463 | | |
464 | 0 | int CBB_add_u32le(CBB *cbb, uint32_t value) { |
465 | 0 | return CBB_add_u32(cbb, CRYPTO_bswap4(value)); |
466 | 0 | } |
467 | | |
468 | 52.6k | int CBB_add_u64(CBB *cbb, uint64_t value) { return cbb_add_u(cbb, value, 8); } |
469 | | |
470 | 0 | int CBB_add_u64le(CBB *cbb, uint64_t value) { |
471 | 0 | return CBB_add_u64(cbb, CRYPTO_bswap8(value)); |
472 | 0 | } |
473 | | |
474 | 11 | void CBB_discard(CBB *cbb, size_t len) { |
475 | 11 | BSSL_CHECK(cbb->child == nullptr); |
476 | 11 | BSSL_CHECK(len <= CBB_len(cbb)); |
477 | 11 | struct cbb_buffer_st *base = cbb_get_base(cbb); |
478 | 11 | base->len -= len; |
479 | 11 | } |
480 | | |
481 | 1.76k | void CBB_discard_child(CBB *cbb) { |
482 | 1.76k | if (cbb->child == nullptr) { |
483 | 0 | return; |
484 | 0 | } |
485 | | |
486 | 1.76k | struct cbb_buffer_st *base = cbb_get_base(cbb); |
487 | 1.76k | assert(cbb->child->is_child); |
488 | 1.76k | base->len = cbb->child->u.child.offset; |
489 | | |
490 | 1.76k | cbb->child->u.child.base = nullptr; |
491 | 1.76k | cbb->child = nullptr; |
492 | 1.76k | } |
493 | | |
494 | | int CBB_add_asn1_element(CBB *cbb, CBS_ASN1_TAG tag, const uint8_t *data, |
495 | 2.34M | size_t data_len) { |
496 | 2.34M | CBB child; |
497 | 2.34M | if (!CBB_add_asn1(cbb, &child, tag) || |
498 | 2.34M | !CBB_add_bytes(&child, data, data_len) || // |
499 | 2.34M | !CBB_flush(cbb)) { |
500 | 0 | cbb_on_error(cbb); |
501 | 0 | return 0; |
502 | 0 | } |
503 | | |
504 | 2.34M | return 1; |
505 | 2.34M | } |
506 | | |
507 | 14.9k | int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) { |
508 | 14.9k | return CBB_add_asn1_uint64_with_tag(cbb, value, CBS_ASN1_INTEGER); |
509 | 14.9k | } |
510 | | |
511 | 14.9k | int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, CBS_ASN1_TAG tag) { |
512 | 14.9k | CBB child; |
513 | 14.9k | int started = 0; |
514 | 14.9k | if (!CBB_add_asn1(cbb, &child, tag)) { |
515 | 0 | goto err; |
516 | 0 | } |
517 | | |
518 | 134k | for (size_t i = 0; i < 8; i++) { |
519 | 119k | uint8_t byte = (value >> 8 * (7 - i)) & 0xff; |
520 | 119k | if (!started) { |
521 | 110k | if (byte == 0) { |
522 | | // Don't encode leading zeros. |
523 | 97.6k | continue; |
524 | 97.6k | } |
525 | | // If the high bit is set, add a padding byte to make it |
526 | | // unsigned. |
527 | 13.3k | if ((byte & 0x80) && !CBB_add_u8(&child, 0)) { |
528 | 0 | goto err; |
529 | 0 | } |
530 | 13.3k | started = 1; |
531 | 13.3k | } |
532 | 22.1k | if (!CBB_add_u8(&child, byte)) { |
533 | 0 | goto err; |
534 | 0 | } |
535 | 22.1k | } |
536 | | |
537 | | // 0 is encoded as a single 0, not the empty string. |
538 | 14.9k | if (!started && !CBB_add_u8(&child, 0)) { |
539 | 0 | goto err; |
540 | 0 | } |
541 | | |
542 | 14.9k | return CBB_flush(cbb); |
543 | | |
544 | 0 | err: |
545 | 0 | cbb_on_error(cbb); |
546 | 0 | return 0; |
547 | 14.9k | } |
548 | | |
549 | 0 | int CBB_add_asn1_int64(CBB *cbb, int64_t value) { |
550 | 0 | return CBB_add_asn1_int64_with_tag(cbb, value, CBS_ASN1_INTEGER); |
551 | 0 | } |
552 | | |
553 | 0 | int CBB_add_asn1_int64_with_tag(CBB *cbb, int64_t value, CBS_ASN1_TAG tag) { |
554 | 0 | if (value >= 0) { |
555 | 0 | return CBB_add_asn1_uint64_with_tag(cbb, (uint64_t)value, tag); |
556 | 0 | } |
557 | | |
558 | 0 | uint8_t bytes[sizeof(int64_t)]; |
559 | 0 | memcpy(bytes, &value, sizeof(value)); |
560 | 0 | int start = 7; |
561 | | // Skip leading sign-extension bytes unless they are necessary. |
562 | 0 | while (start > 0 && (bytes[start] == 0xff && (bytes[start - 1] & 0x80))) { |
563 | 0 | start--; |
564 | 0 | } |
565 | |
|
566 | 0 | CBB child; |
567 | 0 | if (!CBB_add_asn1(cbb, &child, tag)) { |
568 | 0 | goto err; |
569 | 0 | } |
570 | 0 | for (int i = start; i >= 0; i--) { |
571 | 0 | if (!CBB_add_u8(&child, bytes[i])) { |
572 | 0 | goto err; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | return CBB_flush(cbb); |
576 | | |
577 | 0 | err: |
578 | 0 | cbb_on_error(cbb); |
579 | 0 | return 0; |
580 | 0 | } |
581 | | |
582 | 5.44k | int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) { |
583 | 5.44k | return CBB_add_asn1_element(cbb, CBS_ASN1_OCTETSTRING, data, data_len); |
584 | 5.44k | } |
585 | | |
586 | 371 | int CBB_add_asn1_bool(CBB *cbb, int value) { |
587 | 371 | CBB child; |
588 | 371 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_BOOLEAN) || |
589 | 371 | !CBB_add_u8(&child, value != 0 ? 0xff : 0) || !CBB_flush(cbb)) { |
590 | 0 | cbb_on_error(cbb); |
591 | 0 | return 0; |
592 | 0 | } |
593 | | |
594 | 371 | return 1; |
595 | 371 | } |
596 | | |
597 | | // parse_dotted_decimal parses one decimal component from |cbs|, where |cbs| is |
598 | | // an OID literal, e.g., "1.2.840.113554.4.1.72585". It consumes both the |
599 | | // component and the dot, so |cbs| may be passed into the function again for the |
600 | | // next value. |
601 | 34.4k | static int parse_dotted_decimal(CBS *cbs, uint64_t *out) { |
602 | 34.4k | if (!CBS_get_u64_decimal(cbs, out)) { |
603 | 877 | return 0; |
604 | 877 | } |
605 | | |
606 | | // The integer must have either ended at the end of the string, or a |
607 | | // non-terminal dot, which should be consumed. If the string ends with a dot, |
608 | | // this is not a valid OID string. |
609 | 33.5k | uint8_t dot; |
610 | 33.5k | return !CBS_get_u8(cbs, &dot) || (dot == '.' && CBS_len(cbs) > 0); |
611 | 34.4k | } |
612 | | |
613 | 9.23k | int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) { |
614 | 9.23k | if (!CBB_flush(cbb)) { |
615 | 0 | return 0; |
616 | 0 | } |
617 | | |
618 | 9.23k | CBS cbs; |
619 | 9.23k | CBS_init(&cbs, (const uint8_t *)text, len); |
620 | | |
621 | | // OIDs must have at least two components. |
622 | 9.23k | uint64_t a, b; |
623 | 9.23k | if (!parse_dotted_decimal(&cbs, &a) || !parse_dotted_decimal(&cbs, &b)) { |
624 | 908 | return 0; |
625 | 908 | } |
626 | | |
627 | | // The first component is encoded as 40 * |a| + |b|. This assumes that |a| is |
628 | | // 0, 1, or 2 and that, when it is 0 or 1, |b| is at most 39. |
629 | 8.32k | if (a > 2 || (a < 2 && b > 39) || b > UINT64_MAX - 80 || |
630 | 8.06k | !add_base128_integer(cbb, 40u * a + b)) { |
631 | 265 | return 0; |
632 | 265 | } |
633 | | |
634 | | // The remaining components are encoded unmodified. |
635 | 24.5k | while (CBS_len(&cbs) > 0) { |
636 | 16.5k | if (!parse_dotted_decimal(&cbs, &a) || !add_base128_integer(cbb, a)) { |
637 | 61 | return 0; |
638 | 61 | } |
639 | 16.5k | } |
640 | | |
641 | 8.00k | return 1; |
642 | 8.06k | } |
643 | | |
644 | | int CBB_add_asn1_relative_oid_from_text(CBB *cbb, const char *text, |
645 | 0 | size_t len) { |
646 | 0 | if (!CBB_flush(cbb)) { |
647 | 0 | return 0; |
648 | 0 | } |
649 | | |
650 | | // Relative OIDs must have at least one component. |
651 | 0 | if (!len) { |
652 | 0 | return 0; |
653 | 0 | } |
654 | | |
655 | 0 | CBS cbs; |
656 | 0 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(text), len); |
657 | |
|
658 | 0 | while (CBS_len(&cbs) > 0) { |
659 | 0 | uint64_t a; |
660 | 0 | if (!parse_dotted_decimal(&cbs, &a) || !add_base128_integer(cbb, a)) { |
661 | 0 | return 0; |
662 | 0 | } |
663 | 0 | } |
664 | | |
665 | 0 | return 1; |
666 | 0 | } |
667 | | |
668 | 0 | int CBB_add_asn1_oid_component(CBB *cbb, uint64_t value) { |
669 | 0 | if (!CBB_flush(cbb)) { |
670 | 0 | return 0; |
671 | 0 | } |
672 | | |
673 | 0 | return add_base128_integer(cbb, value); |
674 | 0 | } |
675 | | |
676 | 241k | static int compare_set_of_element(const void *a_ptr, const void *b_ptr) { |
677 | | // See X.690, section 11.6 for the ordering. They are sorted in ascending |
678 | | // order by their DER encoding. |
679 | 241k | const CBS *a = reinterpret_cast<const CBS *>(a_ptr), |
680 | 241k | *b = reinterpret_cast<const CBS *>(b_ptr); |
681 | 241k | size_t a_len = CBS_len(a), b_len = CBS_len(b); |
682 | 241k | size_t min_len = a_len < b_len ? a_len : b_len; |
683 | 241k | int ret = OPENSSL_memcmp(CBS_data(a), CBS_data(b), min_len); |
684 | 241k | if (ret != 0) { |
685 | 168k | return ret; |
686 | 168k | } |
687 | 73.2k | if (a_len == b_len) { |
688 | 73.2k | return 0; |
689 | 73.2k | } |
690 | | // If one is a prefix of the other, the shorter one sorts first. (This is not |
691 | | // actually reachable. No DER encoding is a prefix of another DER encoding.) |
692 | 0 | return a_len < b_len ? -1 : 1; |
693 | 73.2k | } |
694 | | |
695 | 1.66M | int CBB_flush_asn1_set_of(CBB *cbb) { |
696 | 1.66M | if (!CBB_flush(cbb)) { |
697 | 0 | return 0; |
698 | 0 | } |
699 | | |
700 | 1.66M | CBS cbs; |
701 | 1.66M | size_t num_children = 0; |
702 | 1.66M | CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb)); |
703 | 3.51M | while (CBS_len(&cbs) != 0) { |
704 | 1.85M | if (!CBS_get_any_asn1_element(&cbs, nullptr, nullptr, nullptr)) { |
705 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
706 | 0 | return 0; |
707 | 0 | } |
708 | 1.85M | num_children++; |
709 | 1.85M | } |
710 | | |
711 | 1.66M | if (num_children < 2) { |
712 | 1.56M | return 1; // Nothing to do. This is the common case for X.509. |
713 | 1.56M | } |
714 | | |
715 | | // Parse out the children and sort. We alias them into a copy of so they |
716 | | // remain valid as we rewrite |cbb|. |
717 | 101k | int ret = 0; |
718 | 101k | size_t buf_len = CBB_len(cbb); |
719 | 101k | uint8_t *buf = |
720 | 101k | reinterpret_cast<uint8_t *>(OPENSSL_memdup(CBB_data(cbb), buf_len)); |
721 | 101k | CBS *children = |
722 | 101k | reinterpret_cast<CBS *>(OPENSSL_calloc(num_children, sizeof(CBS))); |
723 | 101k | uint8_t *out; |
724 | 101k | size_t offset = 0; |
725 | 101k | if (buf == nullptr || children == nullptr) { |
726 | 0 | goto err; |
727 | 0 | } |
728 | 101k | CBS_init(&cbs, buf, buf_len); |
729 | 391k | for (size_t i = 0; i < num_children; i++) { |
730 | 289k | if (!CBS_get_any_asn1_element(&cbs, &children[i], nullptr, nullptr)) { |
731 | 0 | goto err; |
732 | 0 | } |
733 | 289k | } |
734 | 101k | qsort(children, num_children, sizeof(CBS), compare_set_of_element); |
735 | | |
736 | | // Write the contents back in the new order. |
737 | 101k | out = (uint8_t *)CBB_data(cbb); |
738 | 391k | for (size_t i = 0; i < num_children; i++) { |
739 | 289k | OPENSSL_memcpy(out + offset, CBS_data(&children[i]), CBS_len(&children[i])); |
740 | 289k | offset += CBS_len(&children[i]); |
741 | 289k | } |
742 | 101k | assert(offset == buf_len); |
743 | | |
744 | 101k | ret = 1; |
745 | | |
746 | 101k | err: |
747 | 101k | OPENSSL_free(buf); |
748 | 101k | OPENSSL_free(children); |
749 | 101k | return ret; |
750 | 101k | } |
751 | | |
752 | 909k | bool bssl::CBBFinishArray(CBB *cbb, Array<uint8_t> *out) { |
753 | 909k | uint8_t *ptr; |
754 | 909k | size_t len; |
755 | 909k | if (!CBB_finish(cbb, &ptr, &len)) { |
756 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
757 | 0 | return false; |
758 | 0 | } |
759 | 909k | out->Reset(ptr, len); |
760 | 909k | return true; |
761 | 909k | } |