Coverage Report

Created: 2025-06-11 06:41

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