Coverage Report

Created: 2025-11-17 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
26
27
19.1M
void CBB_zero(CBB *cbb) { OPENSSL_memset(cbb, 0, sizeof(CBB)); }
28
29
2.46M
static void cbb_init(CBB *cbb, uint8_t *buf, size_t cap, int can_resize) {
30
2.46M
  cbb->is_child = 0;
31
2.46M
  cbb->child = nullptr;
32
2.46M
  cbb->u.base.buf = buf;
33
2.46M
  cbb->u.base.len = 0;
34
2.46M
  cbb->u.base.cap = cap;
35
2.46M
  cbb->u.base.can_resize = can_resize;
36
2.46M
  cbb->u.base.error = 0;
37
2.46M
}
38
39
2.30M
int CBB_init(CBB *cbb, size_t initial_capacity) {
40
2.30M
  CBB_zero(cbb);
41
42
2.30M
  uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(initial_capacity));
43
2.30M
  if (initial_capacity > 0 && buf == nullptr) {
44
0
    return 0;
45
0
  }
46
47
2.30M
  cbb_init(cbb, buf, initial_capacity, /*can_resize=*/1);
48
2.30M
  return 1;
49
2.30M
}
50
51
155k
int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
52
155k
  CBB_zero(cbb);
53
155k
  cbb_init(cbb, buf, len, /*can_resize=*/0);
54
155k
  return 1;
55
155k
}
56
57
3.28M
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
3.28M
  assert(!cbb->is_child);
61
3.28M
  if (cbb->is_child) {
62
0
    return;
63
0
  }
64
65
3.28M
  if (cbb->u.base.can_resize) {
66
3.18M
    OPENSSL_free(cbb->u.base.buf);
67
3.18M
  }
68
3.28M
}
69
70
static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
71
543M
                              size_t len) {
72
543M
  if (base == nullptr) {
73
0
    return 0;
74
0
  }
75
76
543M
  size_t newlen = base->len + len;
77
543M
  if (newlen < base->len) {
78
    // Overflow
79
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
80
0
    goto err;
81
0
  }
82
83
543M
  if (newlen > base->cap) {
84
3.14M
    if (!base->can_resize) {
85
0
      OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
86
0
      goto err;
87
0
    }
88
89
3.14M
    size_t newcap = base->cap * 2;
90
3.14M
    if (newcap < base->cap || newcap < newlen) {
91
518k
      newcap = newlen;
92
518k
    }
93
3.14M
    uint8_t *newbuf =
94
3.14M
        reinterpret_cast<uint8_t *>(OPENSSL_realloc(base->buf, newcap));
95
3.14M
    if (newbuf == nullptr) {
96
0
      goto err;
97
0
    }
98
99
3.14M
    base->buf = newbuf;
100
3.14M
    base->cap = newcap;
101
3.14M
  }
102
103
543M
  if (out) {
104
542M
    *out = base->buf + base->len;
105
542M
  }
106
107
543M
  return 1;
108
109
0
err:
110
0
  base->error = 1;
111
0
  return 0;
112
543M
}
113
114
static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
115
543M
                          size_t len) {
116
543M
  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
543M
  base->len += len;
121
543M
  return 1;
122
543M
}
123
124
1.91M
int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
125
1.91M
  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.91M
  if (!CBB_flush(cbb)) {
131
0
    return 0;
132
0
  }
133
134
1.91M
  if (cbb->u.base.can_resize && (out_data == nullptr || out_len == nullptr)) {
135
    // |out_data| and |out_len| can only be NULL if the CBB is fixed.
136
0
    return 0;
137
0
  }
138
139
1.91M
  if (out_data != nullptr) {
140
1.86M
    *out_data = cbb->u.base.buf;
141
1.86M
  }
142
1.91M
  if (out_len != nullptr) {
143
1.86M
    *out_len = cbb->u.base.len;
144
1.86M
  }
145
1.91M
  cbb->u.base.buf = nullptr;
146
1.91M
  CBB_cleanup(cbb);
147
1.91M
  return 1;
148
1.91M
}
149
150
1.11G
static struct cbb_buffer_st *cbb_get_base(CBB *cbb) {
151
1.11G
  if (cbb->is_child) {
152
128M
    return cbb->u.child.base;
153
128M
  }
154
985M
  return &cbb->u.base;
155
1.11G
}
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 = nullptr;
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
571M
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
571M
  struct cbb_buffer_st *base = cbb_get_base(cbb);
188
571M
  if (base == nullptr || base->error) {
189
0
    return 0;
190
0
  }
191
192
571M
  if (cbb->child == nullptr) {
193
    // Nothing to flush.
194
557M
    return 1;
195
557M
  }
196
197
571M
  assert(cbb->child->is_child);
198
14.7M
  struct cbb_child_st *child = &cbb->child->u.child;
199
14.7M
  assert(child->base == base);
200
14.7M
  size_t child_start = child->offset + child->pending_len_len;
201
202
14.7M
  size_t len;
203
14.7M
  if (!CBB_flush(cbb->child) || child_start < child->offset ||
204
14.7M
      base->len < child_start) {
205
0
    goto err;
206
0
  }
207
208
14.7M
  len = base->len - child_start;
209
210
14.7M
  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
12.0M
    uint8_t len_len;
215
12.0M
    uint8_t initial_length_byte;
216
217
12.0M
    assert(child->pending_len_len == 1);
218
219
12.0M
    if (len > 0xfffffffe) {
220
0
      OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
221
      // Too large.
222
0
      goto err;
223
12.0M
    } else if (len > 0xffffff) {
224
0
      len_len = 5;
225
0
      initial_length_byte = 0x80 | 4;
226
12.0M
    } else if (len > 0xffff) {
227
10.3k
      len_len = 4;
228
10.3k
      initial_length_byte = 0x80 | 3;
229
11.9M
    } else if (len > 0xff) {
230
825k
      len_len = 3;
231
825k
      initial_length_byte = 0x80 | 2;
232
11.1M
    } else if (len > 0x7f) {
233
640k
      len_len = 2;
234
640k
      initial_length_byte = 0x80 | 1;
235
10.5M
    } else {
236
10.5M
      len_len = 1;
237
10.5M
      initial_length_byte = (uint8_t)len;
238
10.5M
      len = 0;
239
10.5M
    }
240
241
12.0M
    if (len_len != 1) {
242
      // We need to move the contents along in order to make space.
243
1.47M
      size_t extra_bytes = len_len - 1;
244
1.47M
      if (!cbb_buffer_add(base, nullptr, extra_bytes)) {
245
0
        goto err;
246
0
      }
247
1.47M
      OPENSSL_memmove(base->buf + child_start + extra_bytes,
248
1.47M
                      base->buf + child_start, len);
249
1.47M
    }
250
12.0M
    base->buf[child->offset++] = initial_length_byte;
251
12.0M
    child->pending_len_len = len_len - 1;
252
12.0M
  }
253
254
22.0M
  for (size_t i = child->pending_len_len - 1; i < child->pending_len_len; i--) {
255
7.31M
    base->buf[child->offset + i] = (uint8_t)len;
256
7.31M
    len >>= 8;
257
7.31M
  }
258
14.7M
  if (len != 0) {
259
0
    OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
260
0
    goto err;
261
0
  }
262
263
14.7M
  child->base = nullptr;
264
14.7M
  cbb->child = nullptr;
265
266
14.7M
  return 1;
267
268
0
err:
269
0
  cbb_on_error(cbb);
270
0
  return 0;
271
14.7M
}
272
273
3.24M
const uint8_t *CBB_data(const CBB *cbb) {
274
3.24M
  assert(cbb->child == nullptr);
275
3.24M
  if (cbb->is_child) {
276
2.62M
    return cbb->u.child.base->buf + cbb->u.child.offset +
277
2.62M
           cbb->u.child.pending_len_len;
278
2.62M
  }
279
614k
  return cbb->u.base.buf;
280
3.24M
}
281
282
7.82M
size_t CBB_len(const CBB *cbb) {
283
7.82M
  assert(cbb->child == nullptr);
284
7.82M
  if (cbb->is_child) {
285
7.15M
    assert(cbb->u.child.offset + cbb->u.child.pending_len_len <=
286
7.15M
           cbb->u.child.base->len);
287
7.15M
    return cbb->u.child.base->len - cbb->u.child.offset -
288
7.15M
           cbb->u.child.pending_len_len;
289
7.15M
  }
290
675k
  return cbb->u.base.len;
291
7.82M
}
292
293
static int cbb_add_child(CBB *cbb, CBB *out_child, uint8_t len_len,
294
14.7M
                         int is_asn1) {
295
14.7M
  assert(cbb->child == nullptr);
296
14.7M
  assert(!is_asn1 || len_len == 1);
297
14.7M
  struct cbb_buffer_st *base = cbb_get_base(cbb);
298
14.7M
  size_t offset = base->len;
299
300
  // Reserve space for the length prefix.
301
14.7M
  uint8_t *prefix_bytes;
302
14.7M
  if (!cbb_buffer_add(base, &prefix_bytes, len_len)) {
303
0
    return 0;
304
0
  }
305
14.7M
  OPENSSL_memset(prefix_bytes, 0, len_len);
306
307
14.7M
  CBB_zero(out_child);
308
14.7M
  out_child->is_child = 1;
309
14.7M
  out_child->u.child.base = base;
310
14.7M
  out_child->u.child.offset = offset;
311
14.7M
  out_child->u.child.pending_len_len = len_len;
312
14.7M
  out_child->u.child.pending_is_asn1 = is_asn1;
313
14.7M
  cbb->child = out_child;
314
14.7M
  return 1;
315
14.7M
}
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
894k
int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
327
894k
  return cbb_add_length_prefixed(cbb, out_contents, 1);
328
894k
}
329
330
1.42M
int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
331
1.42M
  return cbb_add_length_prefixed(cbb, out_contents, 2);
332
1.42M
}
333
334
417k
int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
335
417k
  return cbb_add_length_prefixed(cbb, out_contents, 3);
336
417k
}
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
40.3k
static int add_base128_integer(CBB *cbb, uint64_t v) {
342
40.3k
  unsigned len_len = 0;
343
40.3k
  uint64_t copy = v;
344
110k
  while (copy > 0) {
345
69.9k
    len_len++;
346
69.9k
    copy >>= 7;
347
69.9k
  }
348
40.3k
  if (len_len == 0) {
349
6.91k
    len_len = 1;  // Zero is encoded with one byte.
350
6.91k
  }
351
117k
  for (unsigned i = len_len - 1; i < len_len; i--) {
352
76.8k
    uint8_t byte = (v >> (7 * i)) & 0x7f;
353
76.8k
    if (i != 0) {
354
      // The high bit denotes whether there is more data.
355
36.5k
      byte |= 0x80;
356
36.5k
    }
357
76.8k
    if (!CBB_add_u8(cbb, byte)) {
358
0
      return 0;
359
0
    }
360
76.8k
  }
361
40.3k
  return 1;
362
40.3k
}
363
364
12.0M
int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) {
365
12.0M
  if (!CBB_flush(cbb)) {
366
0
    return 0;
367
0
  }
368
369
  // Split the tag into leading bits and tag number.
370
12.0M
  uint8_t tag_bits = (tag >> CBS_ASN1_TAG_SHIFT) & 0xe0;
371
12.0M
  CBS_ASN1_TAG tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
372
12.0M
  if (tag_number >= 0x1f) {
373
    // Set all the bits in the tag number to signal high tag number form.
374
8.35k
    if (!CBB_add_u8(cbb, tag_bits | 0x1f) ||
375
8.35k
        !add_base128_integer(cbb, tag_number)) {
376
0
      return 0;
377
0
    }
378
12.0M
  } 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
12.0M
  return cbb_add_child(cbb, out_contents, /*len_len=*/1, /*is_asn1=*/1);
384
12.0M
}
385
386
7.59M
int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
387
7.59M
  uint8_t *out;
388
7.59M
  if (!CBB_add_space(cbb, &out, len)) {
389
0
    return 0;
390
0
  }
391
7.59M
  OPENSSL_memcpy(out, data, len);
392
7.59M
  return 1;
393
7.59M
}
394
395
475
int CBB_add_zeros(CBB *cbb, size_t len) {
396
475
  uint8_t *out;
397
475
  if (!CBB_add_space(cbb, &out, len)) {
398
0
    return 0;
399
0
  }
400
475
  OPENSSL_memset(out, 0, len);
401
475
  return 1;
402
475
}
403
404
527M
int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
405
527M
  if (!CBB_flush(cbb) || !cbb_buffer_add(cbb_get_base(cbb), out_data, len)) {
406
0
    return 0;
407
0
  }
408
527M
  return 1;
409
527M
}
410
411
44.6k
int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) {
412
44.6k
  if (!CBB_flush(cbb) ||
413
44.6k
      !cbb_buffer_reserve(cbb_get_base(cbb), out_data, len)) {
414
0
    return 0;
415
0
  }
416
44.6k
  return 1;
417
44.6k
}
418
419
44.6k
int CBB_did_write(CBB *cbb, size_t len) {
420
44.6k
  struct cbb_buffer_st *base = cbb_get_base(cbb);
421
44.6k
  size_t newlen = base->len + len;
422
44.6k
  if (cbb->child != nullptr || newlen < base->len || newlen > base->cap) {
423
0
    return 0;
424
0
  }
425
44.6k
  base->len = newlen;
426
44.6k
  return 1;
427
44.6k
}
428
429
519M
static int cbb_add_u(CBB *cbb, uint64_t v, size_t len_len) {
430
519M
  uint8_t *buf;
431
519M
  if (!CBB_add_space(cbb, &buf, len_len)) {
432
0
    return 0;
433
0
  }
434
435
2.43G
  for (size_t i = len_len - 1; i < len_len; i--) {
436
1.91G
    buf[i] = v;
437
1.91G
    v >>= 8;
438
1.91G
  }
439
440
  // |v| must fit in |len_len| bytes.
441
519M
  if (v != 0) {
442
0
    cbb_on_error(cbb);
443
0
    return 0;
444
0
  }
445
446
519M
  return 1;
447
519M
}
448
449
50.6M
int CBB_add_u8(CBB *cbb, uint8_t value) { return cbb_add_u(cbb, value, 1); }
450
451
4.86M
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
289k
int CBB_add_u24(CBB *cbb, uint32_t value) { return cbb_add_u(cbb, value, 3); }
458
459
463M
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
55.3k
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
8
void CBB_discard(CBB *cbb, size_t len) {
472
8
  BSSL_CHECK(cbb->child == nullptr);
473
8
  BSSL_CHECK(len <= CBB_len(cbb));
474
8
  struct cbb_buffer_st *base = cbb_get_base(cbb);
475
8
  base->len -= len;
476
8
}
477
478
1.92k
void CBB_discard_child(CBB *cbb) {
479
1.92k
  if (cbb->child == nullptr) {
480
0
    return;
481
0
  }
482
483
1.92k
  struct cbb_buffer_st *base = cbb_get_base(cbb);
484
1.92k
  assert(cbb->child->is_child);
485
1.92k
  base->len = cbb->child->u.child.offset;
486
487
1.92k
  cbb->child->u.child.base = nullptr;
488
1.92k
  cbb->child = nullptr;
489
1.92k
}
490
491
int CBB_add_asn1_element(CBB *cbb, CBS_ASN1_TAG tag, const uint8_t *data,
492
2.94M
                         size_t data_len) {
493
2.94M
  CBB child;
494
2.94M
  if (!CBB_add_asn1(cbb, &child, tag) ||
495
2.94M
      !CBB_add_bytes(&child, data, data_len) ||  //
496
2.94M
      !CBB_flush(cbb)) {
497
0
    cbb_on_error(cbb);
498
0
    return 0;
499
0
  }
500
501
2.94M
  return 1;
502
2.94M
}
503
504
14.9k
int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
505
14.9k
  return CBB_add_asn1_uint64_with_tag(cbb, value, CBS_ASN1_INTEGER);
506
14.9k
}
507
508
14.9k
int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, CBS_ASN1_TAG tag) {
509
14.9k
  CBB child;
510
14.9k
  int started = 0;
511
14.9k
  if (!CBB_add_asn1(cbb, &child, tag)) {
512
0
    goto err;
513
0
  }
514
515
134k
  for (size_t i = 0; i < 8; i++) {
516
119k
    uint8_t byte = (value >> 8 * (7 - i)) & 0xff;
517
119k
    if (!started) {
518
110k
      if (byte == 0) {
519
        // Don't encode leading zeros.
520
97.7k
        continue;
521
97.7k
      }
522
      // If the high bit is set, add a padding byte to make it
523
      // unsigned.
524
13.1k
      if ((byte & 0x80) && !CBB_add_u8(&child, 0)) {
525
0
        goto err;
526
0
      }
527
13.1k
      started = 1;
528
13.1k
    }
529
21.8k
    if (!CBB_add_u8(&child, byte)) {
530
0
      goto err;
531
0
    }
532
21.8k
  }
533
534
  // 0 is encoded as a single 0, not the empty string.
535
14.9k
  if (!started && !CBB_add_u8(&child, 0)) {
536
0
    goto err;
537
0
  }
538
539
14.9k
  return CBB_flush(cbb);
540
541
0
err:
542
0
  cbb_on_error(cbb);
543
0
  return 0;
544
14.9k
}
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.43k
int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) {
580
5.43k
  return CBB_add_asn1_element(cbb, CBS_ASN1_OCTETSTRING, data, data_len);
581
5.43k
}
582
583
308
int CBB_add_asn1_bool(CBB *cbb, int value) {
584
308
  CBB child;
585
308
  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_BOOLEAN) ||
586
308
      !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
308
  return 1;
592
308
}
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
43.4k
static int parse_dotted_decimal(CBS *cbs, uint64_t *out) {
599
43.4k
  if (!CBS_get_u64_decimal(cbs, out)) {
600
974
    return 0;
601
974
  }
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
42.4k
  uint8_t dot;
607
42.4k
  return !CBS_get_u8(cbs, &dot) || (dot == '.' && CBS_len(cbs) > 0);
608
43.4k
}
609
610
10.6k
int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
611
10.6k
  if (!CBB_flush(cbb)) {
612
0
    return 0;
613
0
  }
614
615
10.6k
  CBS cbs;
616
10.6k
  CBS_init(&cbs, (const uint8_t *)text, len);
617
618
  // OIDs must have at least two components.
619
10.6k
  uint64_t a, b;
620
10.6k
  if (!parse_dotted_decimal(&cbs, &a) || !parse_dotted_decimal(&cbs, &b)) {
621
1.00k
    return 0;
622
1.00k
  }
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
9.61k
  if (a > 2 || (a < 2 && b > 39) || b > UINT64_MAX - 80 ||
627
9.17k
      !add_base128_integer(cbb, 40u * a + b)) {
628
441
    return 0;
629
441
  }
630
631
  // The remaining components are encoded unmodified.
632
31.9k
  while (CBS_len(&cbs) > 0) {
633
22.8k
    if (!parse_dotted_decimal(&cbs, &a) || !add_base128_integer(cbb, a)) {
634
72
      return 0;
635
72
    }
636
22.8k
  }
637
638
9.09k
  return 1;
639
9.17k
}
640
641
472k
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
472k
  const CBS *a = reinterpret_cast<const CBS *>(a_ptr),
645
472k
            *b = reinterpret_cast<const CBS *>(b_ptr);
646
472k
  size_t a_len = CBS_len(a), b_len = CBS_len(b);
647
472k
  size_t min_len = a_len < b_len ? a_len : b_len;
648
472k
  int ret = OPENSSL_memcmp(CBS_data(a), CBS_data(b), min_len);
649
472k
  if (ret != 0) {
650
331k
    return ret;
651
331k
  }
652
140k
  if (a_len == b_len) {
653
140k
    return 0;
654
140k
  }
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
140k
}
659
660
2.20M
int CBB_flush_asn1_set_of(CBB *cbb) {
661
2.20M
  if (!CBB_flush(cbb)) {
662
0
    return 0;
663
0
  }
664
665
2.20M
  CBS cbs;
666
2.20M
  size_t num_children = 0;
667
2.20M
  CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb));
668
4.76M
  while (CBS_len(&cbs) != 0) {
669
2.55M
    if (!CBS_get_any_asn1_element(&cbs, nullptr, nullptr, nullptr)) {
670
0
      OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
671
0
      return 0;
672
0
    }
673
2.55M
    num_children++;
674
2.55M
  }
675
676
2.20M
  if (num_children < 2) {
677
1.99M
    return 1;  // Nothing to do. This is the common case for X.509.
678
1.99M
  }
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
211k
  int ret = 0;
683
211k
  size_t buf_len = CBB_len(cbb);
684
211k
  uint8_t *buf =
685
211k
      reinterpret_cast<uint8_t *>(OPENSSL_memdup(CBB_data(cbb), buf_len));
686
211k
  CBS *children =
687
211k
      reinterpret_cast<CBS *>(OPENSSL_calloc(num_children, sizeof(CBS)));
688
211k
  uint8_t *out;
689
211k
  size_t offset = 0;
690
211k
  if (buf == nullptr || children == nullptr) {
691
0
    goto err;
692
0
  }
693
211k
  CBS_init(&cbs, buf, buf_len);
694
779k
  for (size_t i = 0; i < num_children; i++) {
695
568k
    if (!CBS_get_any_asn1_element(&cbs, &children[i], nullptr, nullptr)) {
696
0
      goto err;
697
0
    }
698
568k
  }
699
211k
  qsort(children, num_children, sizeof(CBS), compare_set_of_element);
700
701
  // Write the contents back in the new order.
702
211k
  out = (uint8_t *)CBB_data(cbb);
703
779k
  for (size_t i = 0; i < num_children; i++) {
704
568k
    OPENSSL_memcpy(out + offset, CBS_data(&children[i]), CBS_len(&children[i]));
705
568k
    offset += CBS_len(&children[i]);
706
568k
  }
707
211k
  assert(offset == buf_len);
708
709
211k
  ret = 1;
710
711
211k
err:
712
211k
  OPENSSL_free(buf);
713
211k
  OPENSSL_free(children);
714
211k
  return ret;
715
211k
}