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/mem.h>
16 #include <openssl/bytestring.h>
17
18 #include <assert.h>
19 #include <inttypes.h>
20 #include <string.h>
21
22 #include "internal.h"
23 #include "../internal.h"
24
25
CBS_init(CBS * cbs,const uint8_t * data,size_t len)26 void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
27 cbs->data = data;
28 cbs->len = len;
29 }
30
cbs_get(CBS * cbs,const uint8_t ** p,size_t n)31 static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
32 if (cbs->len < n) {
33 return 0;
34 }
35
36 *p = cbs->data;
37 cbs->data += n;
38 cbs->len -= n;
39 return 1;
40 }
41
CBS_skip(CBS * cbs,size_t len)42 int CBS_skip(CBS *cbs, size_t len) {
43 const uint8_t *dummy;
44 return cbs_get(cbs, &dummy, len);
45 }
46
CBS_data(const CBS * cbs)47 const uint8_t *CBS_data(const CBS *cbs) {
48 return cbs->data;
49 }
50
CBS_len(const CBS * cbs)51 size_t CBS_len(const CBS *cbs) {
52 return cbs->len;
53 }
54
CBS_stow(const CBS * cbs,uint8_t ** out_ptr,size_t * out_len)55 int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
56 OPENSSL_free(*out_ptr);
57 *out_ptr = NULL;
58 *out_len = 0;
59
60 if (cbs->len == 0) {
61 return 1;
62 }
63 *out_ptr = OPENSSL_memdup(cbs->data, cbs->len);
64 if (*out_ptr == NULL) {
65 return 0;
66 }
67 *out_len = cbs->len;
68 return 1;
69 }
70
CBS_strdup(const CBS * cbs,char ** out_ptr)71 int CBS_strdup(const CBS *cbs, char **out_ptr) {
72 if (*out_ptr != NULL) {
73 OPENSSL_free(*out_ptr);
74 }
75 *out_ptr = OPENSSL_strndup((const char*)cbs->data, cbs->len);
76 return (*out_ptr != NULL);
77 }
78
CBS_contains_zero_byte(const CBS * cbs)79 int CBS_contains_zero_byte(const CBS *cbs) {
80 return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL;
81 }
82
CBS_mem_equal(const CBS * cbs,const uint8_t * data,size_t len)83 int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
84 if (len != cbs->len) {
85 return 0;
86 }
87 return CRYPTO_memcmp(cbs->data, data, len) == 0;
88 }
89
cbs_get_u(CBS * cbs,uint64_t * out,size_t len)90 static int cbs_get_u(CBS *cbs, uint64_t *out, size_t len) {
91 uint64_t result = 0;
92 const uint8_t *data;
93
94 if (!cbs_get(cbs, &data, len)) {
95 return 0;
96 }
97 for (size_t i = 0; i < len; i++) {
98 result <<= 8;
99 result |= data[i];
100 }
101 *out = result;
102 return 1;
103 }
104
CBS_get_u8(CBS * cbs,uint8_t * out)105 int CBS_get_u8(CBS *cbs, uint8_t *out) {
106 const uint8_t *v;
107 if (!cbs_get(cbs, &v, 1)) {
108 return 0;
109 }
110 *out = *v;
111 return 1;
112 }
113
CBS_get_u16(CBS * cbs,uint16_t * out)114 int CBS_get_u16(CBS *cbs, uint16_t *out) {
115 uint64_t v;
116 if (!cbs_get_u(cbs, &v, 2)) {
117 return 0;
118 }
119 *out = v;
120 return 1;
121 }
122
CBS_get_u16le(CBS * cbs,uint16_t * out)123 int CBS_get_u16le(CBS *cbs, uint16_t *out) {
124 if (!CBS_get_u16(cbs, out)) {
125 return 0;
126 }
127 *out = CRYPTO_bswap2(*out);
128 return 1;
129 }
130
CBS_get_u24(CBS * cbs,uint32_t * out)131 int CBS_get_u24(CBS *cbs, uint32_t *out) {
132 uint64_t v;
133 if (!cbs_get_u(cbs, &v, 3)) {
134 return 0;
135 }
136 *out = v;
137 return 1;
138 }
139
CBS_get_u32(CBS * cbs,uint32_t * out)140 int CBS_get_u32(CBS *cbs, uint32_t *out) {
141 uint64_t v;
142 if (!cbs_get_u(cbs, &v, 4)) {
143 return 0;
144 }
145 *out = v;
146 return 1;
147 }
148
CBS_get_u32le(CBS * cbs,uint32_t * out)149 int CBS_get_u32le(CBS *cbs, uint32_t *out) {
150 if (!CBS_get_u32(cbs, out)) {
151 return 0;
152 }
153 *out = CRYPTO_bswap4(*out);
154 return 1;
155 }
156
CBS_get_u64(CBS * cbs,uint64_t * out)157 int CBS_get_u64(CBS *cbs, uint64_t *out) {
158 return cbs_get_u(cbs, out, 8);
159 }
160
CBS_get_u64le(CBS * cbs,uint64_t * out)161 int CBS_get_u64le(CBS *cbs, uint64_t *out) {
162 if (!cbs_get_u(cbs, out, 8)) {
163 return 0;
164 }
165 *out = CRYPTO_bswap8(*out);
166 return 1;
167 }
168
CBS_get_last_u8(CBS * cbs,uint8_t * out)169 int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
170 if (cbs->len == 0) {
171 return 0;
172 }
173 *out = cbs->data[cbs->len - 1];
174 cbs->len--;
175 return 1;
176 }
177
CBS_get_bytes(CBS * cbs,CBS * out,size_t len)178 int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
179 const uint8_t *v;
180 if (!cbs_get(cbs, &v, len)) {
181 return 0;
182 }
183 CBS_init(out, v, len);
184 return 1;
185 }
186
CBS_copy_bytes(CBS * cbs,uint8_t * out,size_t len)187 int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
188 const uint8_t *v;
189 if (!cbs_get(cbs, &v, len)) {
190 return 0;
191 }
192 OPENSSL_memcpy(out, v, len);
193 return 1;
194 }
195
cbs_get_length_prefixed(CBS * cbs,CBS * out,size_t len_len)196 static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
197 uint64_t len;
198 if (!cbs_get_u(cbs, &len, len_len)) {
199 return 0;
200 }
201 // If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on
202 // 32-bit systems.
203 assert(len_len <= 3);
204 return CBS_get_bytes(cbs, out, len);
205 }
206
CBS_get_u8_length_prefixed(CBS * cbs,CBS * out)207 int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
208 return cbs_get_length_prefixed(cbs, out, 1);
209 }
210
CBS_get_u16_length_prefixed(CBS * cbs,CBS * out)211 int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
212 return cbs_get_length_prefixed(cbs, out, 2);
213 }
214
CBS_get_u24_length_prefixed(CBS * cbs,CBS * out)215 int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
216 return cbs_get_length_prefixed(cbs, out, 3);
217 }
218
219 // parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
220 // |*out| to the result. This is the encoding used in DER for both high tag
221 // number form and OID components.
parse_base128_integer(CBS * cbs,uint64_t * out)222 static int parse_base128_integer(CBS *cbs, uint64_t *out) {
223 uint64_t v = 0;
224 uint8_t b;
225 do {
226 if (!CBS_get_u8(cbs, &b)) {
227 return 0;
228 }
229 if ((v >> (64 - 7)) != 0) {
230 // The value is too large.
231 return 0;
232 }
233 if (v == 0 && b == 0x80) {
234 // The value must be minimally encoded.
235 return 0;
236 }
237 v = (v << 7) | (b & 0x7f);
238
239 // Values end at an octet with the high bit cleared.
240 } while (b & 0x80);
241
242 *out = v;
243 return 1;
244 }
245
parse_asn1_tag(CBS * cbs,unsigned * out)246 static int parse_asn1_tag(CBS *cbs, unsigned *out) {
247 uint8_t tag_byte;
248 if (!CBS_get_u8(cbs, &tag_byte)) {
249 return 0;
250 }
251
252 // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
253 // number no greater than 30.
254 //
255 // If the number portion is 31 (0x1f, the largest value that fits in the
256 // allotted bits), then the tag is more than one byte long and the
257 // continuation bytes contain the tag number.
258 unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
259 unsigned tag_number = tag_byte & 0x1f;
260 if (tag_number == 0x1f) {
261 uint64_t v;
262 if (!parse_base128_integer(cbs, &v) ||
263 // Check the tag number is within our supported bounds.
264 v > CBS_ASN1_TAG_NUMBER_MASK ||
265 // Small tag numbers should have used low tag number form, even in BER.
266 v < 0x1f) {
267 return 0;
268 }
269 tag_number = (unsigned)v;
270 }
271
272 tag |= tag_number;
273
274 *out = tag;
275 return 1;
276 }
277
cbs_get_any_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len,int * out_ber_found,int ber_ok)278 static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
279 size_t *out_header_len, int *out_ber_found,
280 int ber_ok) {
281 CBS header = *cbs;
282 CBS throwaway;
283
284 if (out == NULL) {
285 out = &throwaway;
286 }
287 if (ber_ok) {
288 *out_ber_found = 0;
289 }
290
291 unsigned tag;
292 if (!parse_asn1_tag(&header, &tag)) {
293 return 0;
294 }
295 if (out_tag != NULL) {
296 *out_tag = tag;
297 }
298
299 uint8_t length_byte;
300 if (!CBS_get_u8(&header, &length_byte)) {
301 return 0;
302 }
303
304 size_t header_len = CBS_len(cbs) - CBS_len(&header);
305
306 size_t len;
307 // The format for the length encoding is specified in ITU-T X.690 section
308 // 8.1.3.
309 if ((length_byte & 0x80) == 0) {
310 // Short form length.
311 len = ((size_t) length_byte) + header_len;
312 if (out_header_len != NULL) {
313 *out_header_len = header_len;
314 }
315 } else {
316 // The high bit indicate that this is the long form, while the next 7 bits
317 // encode the number of subsequent octets used to encode the length (ITU-T
318 // X.690 clause 8.1.3.5.b).
319 const size_t num_bytes = length_byte & 0x7f;
320 uint64_t len64;
321
322 if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
323 // indefinite length
324 if (out_header_len != NULL) {
325 *out_header_len = header_len;
326 }
327 *out_ber_found = 1;
328 return CBS_get_bytes(cbs, out, header_len);
329 }
330
331 // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
332 // used as the first byte of the length. If this parser encounters that
333 // value, num_bytes will be parsed as 127, which will fail this check.
334 if (num_bytes == 0 || num_bytes > 4) {
335 return 0;
336 }
337 if (!cbs_get_u(&header, &len64, num_bytes)) {
338 return 0;
339 }
340 // ITU-T X.690 section 10.1 (DER length forms) requires encoding the
341 // length with the minimum number of octets. BER could, technically, have
342 // 125 superfluous zero bytes. We do not attempt to handle that and still
343 // require that the length fit in a |uint32_t| for BER.
344 if (len64 < 128) {
345 // Length should have used short-form encoding.
346 if (ber_ok) {
347 *out_ber_found = 1;
348 } else {
349 return 0;
350 }
351 }
352 if ((len64 >> ((num_bytes - 1) * 8)) == 0) {
353 // Length should have been at least one byte shorter.
354 if (ber_ok) {
355 *out_ber_found = 1;
356 } else {
357 return 0;
358 }
359 }
360 len = len64;
361 if (len + header_len + num_bytes < len) {
362 // Overflow.
363 return 0;
364 }
365 len += header_len + num_bytes;
366 if (out_header_len != NULL) {
367 *out_header_len = header_len + num_bytes;
368 }
369 }
370
371 return CBS_get_bytes(cbs, out, len);
372 }
373
CBS_get_any_asn1(CBS * cbs,CBS * out,unsigned * out_tag)374 int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
375 size_t header_len;
376 if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
377 return 0;
378 }
379
380 if (!CBS_skip(out, header_len)) {
381 assert(0);
382 return 0;
383 }
384
385 return 1;
386 }
387
CBS_get_any_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len)388 int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
389 size_t *out_header_len) {
390 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
391 NULL, 0 /* DER only */);
392 }
393
CBS_get_any_ber_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len,int * out_ber_found)394 int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
395 size_t *out_header_len, int *out_ber_found) {
396 int ber_found_temp;
397 return cbs_get_any_asn1_element(
398 cbs, out, out_tag, out_header_len,
399 out_ber_found ? out_ber_found : &ber_found_temp, 1 /* BER allowed */);
400 }
401
cbs_get_asn1(CBS * cbs,CBS * out,unsigned tag_value,int skip_header)402 static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
403 int skip_header) {
404 size_t header_len;
405 unsigned tag;
406 CBS throwaway;
407
408 if (out == NULL) {
409 out = &throwaway;
410 }
411
412 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
413 tag != tag_value) {
414 return 0;
415 }
416
417 if (skip_header && !CBS_skip(out, header_len)) {
418 assert(0);
419 return 0;
420 }
421
422 return 1;
423 }
424
CBS_get_asn1(CBS * cbs,CBS * out,unsigned tag_value)425 int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
426 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
427 }
428
CBS_get_asn1_element(CBS * cbs,CBS * out,unsigned tag_value)429 int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
430 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
431 }
432
CBS_peek_asn1_tag(const CBS * cbs,unsigned tag_value)433 int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
434 if (CBS_len(cbs) < 1) {
435 return 0;
436 }
437
438 CBS copy = *cbs;
439 unsigned actual_tag;
440 return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag;
441 }
442
CBS_get_asn1_uint64(CBS * cbs,uint64_t * out)443 int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
444 CBS bytes;
445 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
446 !CBS_is_unsigned_asn1_integer(&bytes)) {
447 return 0;
448 }
449
450 *out = 0;
451 const uint8_t *data = CBS_data(&bytes);
452 size_t len = CBS_len(&bytes);
453 for (size_t i = 0; i < len; i++) {
454 if ((*out >> 56) != 0) {
455 // Too large to represent as a uint64_t.
456 return 0;
457 }
458 *out <<= 8;
459 *out |= data[i];
460 }
461
462 return 1;
463 }
464
CBS_get_asn1_int64(CBS * cbs,int64_t * out)465 int CBS_get_asn1_int64(CBS *cbs, int64_t *out) {
466 int is_negative;
467 CBS bytes;
468 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
469 !CBS_is_valid_asn1_integer(&bytes, &is_negative)) {
470 return 0;
471 }
472 const uint8_t *data = CBS_data(&bytes);
473 const size_t len = CBS_len(&bytes);
474 if (len > sizeof(int64_t)) {
475 return 0;
476 }
477 union {
478 int64_t i;
479 uint8_t bytes[sizeof(int64_t)];
480 } u;
481 memset(u.bytes, is_negative ? 0xff : 0, sizeof(u.bytes)); // Sign-extend.
482 for (size_t i = 0; i < len; i++) {
483 u.bytes[i] = data[len - i - 1];
484 }
485 *out = u.i;
486 return 1;
487 }
488
CBS_get_asn1_bool(CBS * cbs,int * out)489 int CBS_get_asn1_bool(CBS *cbs, int *out) {
490 CBS bytes;
491 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) ||
492 CBS_len(&bytes) != 1) {
493 return 0;
494 }
495
496 const uint8_t value = *CBS_data(&bytes);
497 if (value != 0 && value != 0xff) {
498 return 0;
499 }
500
501 *out = !!value;
502 return 1;
503 }
504
CBS_get_optional_asn1(CBS * cbs,CBS * out,int * out_present,unsigned tag)505 int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
506 int present = 0;
507
508 if (CBS_peek_asn1_tag(cbs, tag)) {
509 if (!CBS_get_asn1(cbs, out, tag)) {
510 return 0;
511 }
512 present = 1;
513 }
514
515 if (out_present != NULL) {
516 *out_present = present;
517 }
518
519 return 1;
520 }
521
CBS_get_optional_asn1_octet_string(CBS * cbs,CBS * out,int * out_present,unsigned tag)522 int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
523 unsigned tag) {
524 CBS child;
525 int present;
526 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
527 return 0;
528 }
529 if (present) {
530 assert(out);
531 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
532 CBS_len(&child) != 0) {
533 return 0;
534 }
535 } else {
536 CBS_init(out, NULL, 0);
537 }
538 if (out_present) {
539 *out_present = present;
540 }
541 return 1;
542 }
543
CBS_get_optional_asn1_uint64(CBS * cbs,uint64_t * out,unsigned tag,uint64_t default_value)544 int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
545 uint64_t default_value) {
546 CBS child;
547 int present;
548 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
549 return 0;
550 }
551 if (present) {
552 if (!CBS_get_asn1_uint64(&child, out) ||
553 CBS_len(&child) != 0) {
554 return 0;
555 }
556 } else {
557 *out = default_value;
558 }
559 return 1;
560 }
561
CBS_get_optional_asn1_bool(CBS * cbs,int * out,unsigned tag,int default_value)562 int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
563 int default_value) {
564 CBS child, child2;
565 int present;
566 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
567 return 0;
568 }
569 if (present) {
570 uint8_t boolean;
571
572 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
573 CBS_len(&child2) != 1 ||
574 CBS_len(&child) != 0) {
575 return 0;
576 }
577
578 boolean = CBS_data(&child2)[0];
579 if (boolean == 0) {
580 *out = 0;
581 } else if (boolean == 0xff) {
582 *out = 1;
583 } else {
584 return 0;
585 }
586 } else {
587 *out = default_value;
588 }
589 return 1;
590 }
591
CBS_is_valid_asn1_bitstring(const CBS * cbs)592 int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
593 CBS in = *cbs;
594 uint8_t num_unused_bits;
595 if (!CBS_get_u8(&in, &num_unused_bits) ||
596 num_unused_bits > 7) {
597 return 0;
598 }
599
600 if (num_unused_bits == 0) {
601 return 1;
602 }
603
604 // All num_unused_bits bits must exist and be zeros.
605 uint8_t last;
606 if (!CBS_get_last_u8(&in, &last) ||
607 (last & ((1 << num_unused_bits) - 1)) != 0) {
608 return 0;
609 }
610
611 return 1;
612 }
613
CBS_asn1_bitstring_has_bit(const CBS * cbs,unsigned bit)614 int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
615 if (!CBS_is_valid_asn1_bitstring(cbs)) {
616 return 0;
617 }
618
619 const unsigned byte_num = (bit >> 3) + 1;
620 const unsigned bit_num = 7 - (bit & 7);
621
622 // Unused bits are zero, and this function does not distinguish between
623 // missing and unset bits. Thus it is sufficient to do a byte-level length
624 // check.
625 return byte_num < CBS_len(cbs) &&
626 (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
627 }
628
CBS_is_valid_asn1_integer(const CBS * cbs,int * out_is_negative)629 int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) {
630 CBS copy = *cbs;
631 uint8_t first_byte, second_byte;
632 if (!CBS_get_u8(©, &first_byte)) {
633 return 0; // INTEGERs may not be empty.
634 }
635 if (out_is_negative != NULL) {
636 *out_is_negative = (first_byte & 0x80) != 0;
637 }
638 if (!CBS_get_u8(©, &second_byte)) {
639 return 1; // One byte INTEGERs are always minimal.
640 }
641 if ((first_byte == 0x00 && (second_byte & 0x80) == 0) ||
642 (first_byte == 0xff && (second_byte & 0x80) != 0)) {
643 return 0; // The value is minimal iff the first 9 bits are not all equal.
644 }
645 return 1;
646 }
647
CBS_is_unsigned_asn1_integer(const CBS * cbs)648 int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
649 int is_negative;
650 return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative;
651 }
652
add_decimal(CBB * out,uint64_t v)653 static int add_decimal(CBB *out, uint64_t v) {
654 char buf[DECIMAL_SIZE(uint64_t) + 1];
655 BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
656 return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
657 }
658
CBS_asn1_oid_to_text(const CBS * cbs)659 char *CBS_asn1_oid_to_text(const CBS *cbs) {
660 CBB cbb;
661 if (!CBB_init(&cbb, 32)) {
662 goto err;
663 }
664
665 CBS copy = *cbs;
666 // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
667 uint64_t v;
668 if (!parse_base128_integer(©, &v)) {
669 goto err;
670 }
671
672 if (v >= 80) {
673 if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
674 !add_decimal(&cbb, v - 80)) {
675 goto err;
676 }
677 } else if (!add_decimal(&cbb, v / 40) ||
678 !CBB_add_u8(&cbb, '.') ||
679 !add_decimal(&cbb, v % 40)) {
680 goto err;
681 }
682
683 while (CBS_len(©) != 0) {
684 if (!parse_base128_integer(©, &v) ||
685 !CBB_add_u8(&cbb, '.') ||
686 !add_decimal(&cbb, v)) {
687 goto err;
688 }
689 }
690
691 uint8_t *txt;
692 size_t txt_len;
693 if (!CBB_add_u8(&cbb, '\0') ||
694 !CBB_finish(&cbb, &txt, &txt_len)) {
695 goto err;
696 }
697
698 return (char *)txt;
699
700 err:
701 CBB_cleanup(&cbb);
702 return NULL;
703 }
704