• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CBS_get_until_first(CBS * cbs,CBS * out,uint8_t c)219 int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
220   const uint8_t *split = OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs));
221   if (split == NULL) {
222     return 0;
223   }
224   return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
225 }
226 
227 // parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
228 // |*out| to the result. This is the encoding used in DER for both high tag
229 // number form and OID components.
parse_base128_integer(CBS * cbs,uint64_t * out)230 static int parse_base128_integer(CBS *cbs, uint64_t *out) {
231   uint64_t v = 0;
232   uint8_t b;
233   do {
234     if (!CBS_get_u8(cbs, &b)) {
235       return 0;
236     }
237     if ((v >> (64 - 7)) != 0) {
238       // The value is too large.
239       return 0;
240     }
241     if (v == 0 && b == 0x80) {
242       // The value must be minimally encoded.
243       return 0;
244     }
245     v = (v << 7) | (b & 0x7f);
246 
247     // Values end at an octet with the high bit cleared.
248   } while (b & 0x80);
249 
250   *out = v;
251   return 1;
252 }
253 
parse_asn1_tag(CBS * cbs,unsigned * out)254 static int parse_asn1_tag(CBS *cbs, unsigned *out) {
255   uint8_t tag_byte;
256   if (!CBS_get_u8(cbs, &tag_byte)) {
257     return 0;
258   }
259 
260   // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
261   // number no greater than 30.
262   //
263   // If the number portion is 31 (0x1f, the largest value that fits in the
264   // allotted bits), then the tag is more than one byte long and the
265   // continuation bytes contain the tag number.
266   unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
267   unsigned tag_number = tag_byte & 0x1f;
268   if (tag_number == 0x1f) {
269     uint64_t v;
270     if (!parse_base128_integer(cbs, &v) ||
271         // Check the tag number is within our supported bounds.
272         v > CBS_ASN1_TAG_NUMBER_MASK ||
273         // Small tag numbers should have used low tag number form, even in BER.
274         v < 0x1f) {
275       return 0;
276     }
277     tag_number = (unsigned)v;
278   }
279 
280   tag |= tag_number;
281 
282   *out = tag;
283   return 1;
284 }
285 
cbs_get_any_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len,int * out_ber_found,int ber_ok)286 static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
287                                     size_t *out_header_len, int *out_ber_found,
288                                     int ber_ok) {
289   CBS header = *cbs;
290   CBS throwaway;
291 
292   if (out == NULL) {
293     out = &throwaway;
294   }
295   if (ber_ok) {
296     *out_ber_found = 0;
297   }
298 
299   unsigned tag;
300   if (!parse_asn1_tag(&header, &tag)) {
301     return 0;
302   }
303   if (out_tag != NULL) {
304     *out_tag = tag;
305   }
306 
307   uint8_t length_byte;
308   if (!CBS_get_u8(&header, &length_byte)) {
309     return 0;
310   }
311 
312   size_t header_len = CBS_len(cbs) - CBS_len(&header);
313 
314   size_t len;
315   // The format for the length encoding is specified in ITU-T X.690 section
316   // 8.1.3.
317   if ((length_byte & 0x80) == 0) {
318     // Short form length.
319     len = ((size_t) length_byte) + header_len;
320     if (out_header_len != NULL) {
321       *out_header_len = header_len;
322     }
323   } else {
324     // The high bit indicate that this is the long form, while the next 7 bits
325     // encode the number of subsequent octets used to encode the length (ITU-T
326     // X.690 clause 8.1.3.5.b).
327     const size_t num_bytes = length_byte & 0x7f;
328     uint64_t len64;
329 
330     if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
331       // indefinite length
332       if (out_header_len != NULL) {
333         *out_header_len = header_len;
334       }
335       *out_ber_found = 1;
336       return CBS_get_bytes(cbs, out, header_len);
337     }
338 
339     // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
340     // used as the first byte of the length. If this parser encounters that
341     // value, num_bytes will be parsed as 127, which will fail this check.
342     if (num_bytes == 0 || num_bytes > 4) {
343       return 0;
344     }
345     if (!cbs_get_u(&header, &len64, num_bytes)) {
346       return 0;
347     }
348     // ITU-T X.690 section 10.1 (DER length forms) requires encoding the
349     // length with the minimum number of octets. BER could, technically, have
350     // 125 superfluous zero bytes. We do not attempt to handle that and still
351     // require that the length fit in a |uint32_t| for BER.
352     if (len64 < 128) {
353       // Length should have used short-form encoding.
354       if (ber_ok) {
355         *out_ber_found = 1;
356       } else {
357         return 0;
358       }
359     }
360     if ((len64 >> ((num_bytes - 1) * 8)) == 0) {
361       // Length should have been at least one byte shorter.
362       if (ber_ok) {
363         *out_ber_found = 1;
364       } else {
365         return 0;
366       }
367     }
368     len = len64;
369     if (len + header_len + num_bytes < len) {
370       // Overflow.
371       return 0;
372     }
373     len += header_len + num_bytes;
374     if (out_header_len != NULL) {
375       *out_header_len = header_len + num_bytes;
376     }
377   }
378 
379   return CBS_get_bytes(cbs, out, len);
380 }
381 
CBS_get_any_asn1(CBS * cbs,CBS * out,unsigned * out_tag)382 int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
383   size_t header_len;
384   if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
385     return 0;
386   }
387 
388   if (!CBS_skip(out, header_len)) {
389     assert(0);
390     return 0;
391   }
392 
393   return 1;
394 }
395 
CBS_get_any_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len)396 int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
397                                     size_t *out_header_len) {
398   return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
399                                   NULL, 0 /* DER only */);
400 }
401 
CBS_get_any_ber_asn1_element(CBS * cbs,CBS * out,unsigned * out_tag,size_t * out_header_len,int * out_ber_found)402 int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
403                                  size_t *out_header_len, int *out_ber_found) {
404   int ber_found_temp;
405   return cbs_get_any_asn1_element(
406       cbs, out, out_tag, out_header_len,
407       out_ber_found ? out_ber_found : &ber_found_temp, 1 /* BER allowed */);
408 }
409 
cbs_get_asn1(CBS * cbs,CBS * out,unsigned tag_value,int skip_header)410 static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
411                         int skip_header) {
412   size_t header_len;
413   unsigned tag;
414   CBS throwaway;
415 
416   if (out == NULL) {
417     out = &throwaway;
418   }
419 
420   if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
421       tag != tag_value) {
422     return 0;
423   }
424 
425   if (skip_header && !CBS_skip(out, header_len)) {
426     assert(0);
427     return 0;
428   }
429 
430   return 1;
431 }
432 
CBS_get_asn1(CBS * cbs,CBS * out,unsigned tag_value)433 int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
434   return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
435 }
436 
CBS_get_asn1_element(CBS * cbs,CBS * out,unsigned tag_value)437 int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
438   return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
439 }
440 
CBS_peek_asn1_tag(const CBS * cbs,unsigned tag_value)441 int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
442   if (CBS_len(cbs) < 1) {
443     return 0;
444   }
445 
446   CBS copy = *cbs;
447   unsigned actual_tag;
448   return parse_asn1_tag(&copy, &actual_tag) && tag_value == actual_tag;
449 }
450 
CBS_get_asn1_uint64(CBS * cbs,uint64_t * out)451 int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
452   CBS bytes;
453   if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
454       !CBS_is_unsigned_asn1_integer(&bytes)) {
455     return 0;
456   }
457 
458   *out = 0;
459   const uint8_t *data = CBS_data(&bytes);
460   size_t len = CBS_len(&bytes);
461   for (size_t i = 0; i < len; i++) {
462     if ((*out >> 56) != 0) {
463       // Too large to represent as a uint64_t.
464       return 0;
465     }
466     *out <<= 8;
467     *out |= data[i];
468   }
469 
470   return 1;
471 }
472 
CBS_get_asn1_int64(CBS * cbs,int64_t * out)473 int CBS_get_asn1_int64(CBS *cbs, int64_t *out) {
474   int is_negative;
475   CBS bytes;
476   if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
477       !CBS_is_valid_asn1_integer(&bytes, &is_negative)) {
478     return 0;
479   }
480   const uint8_t *data = CBS_data(&bytes);
481   const size_t len = CBS_len(&bytes);
482   if (len > sizeof(int64_t)) {
483     return 0;
484   }
485   union {
486     int64_t i;
487     uint8_t bytes[sizeof(int64_t)];
488   } u;
489   memset(u.bytes, is_negative ? 0xff : 0, sizeof(u.bytes));  // Sign-extend.
490   for (size_t i = 0; i < len; i++) {
491     u.bytes[i] = data[len - i - 1];
492   }
493   *out = u.i;
494   return 1;
495 }
496 
CBS_get_asn1_bool(CBS * cbs,int * out)497 int CBS_get_asn1_bool(CBS *cbs, int *out) {
498   CBS bytes;
499   if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) ||
500       CBS_len(&bytes) != 1) {
501     return 0;
502   }
503 
504   const uint8_t value = *CBS_data(&bytes);
505   if (value != 0 && value != 0xff) {
506     return 0;
507   }
508 
509   *out = !!value;
510   return 1;
511 }
512 
CBS_get_optional_asn1(CBS * cbs,CBS * out,int * out_present,unsigned tag)513 int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
514   int present = 0;
515 
516   if (CBS_peek_asn1_tag(cbs, tag)) {
517     if (!CBS_get_asn1(cbs, out, tag)) {
518       return 0;
519     }
520     present = 1;
521   }
522 
523   if (out_present != NULL) {
524     *out_present = present;
525   }
526 
527   return 1;
528 }
529 
CBS_get_optional_asn1_octet_string(CBS * cbs,CBS * out,int * out_present,unsigned tag)530 int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
531                                        unsigned tag) {
532   CBS child;
533   int present;
534   if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
535     return 0;
536   }
537   if (present) {
538     assert(out);
539     if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
540         CBS_len(&child) != 0) {
541       return 0;
542     }
543   } else {
544     CBS_init(out, NULL, 0);
545   }
546   if (out_present) {
547     *out_present = present;
548   }
549   return 1;
550 }
551 
CBS_get_optional_asn1_uint64(CBS * cbs,uint64_t * out,unsigned tag,uint64_t default_value)552 int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
553                                  uint64_t default_value) {
554   CBS child;
555   int present;
556   if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
557     return 0;
558   }
559   if (present) {
560     if (!CBS_get_asn1_uint64(&child, out) ||
561         CBS_len(&child) != 0) {
562       return 0;
563     }
564   } else {
565     *out = default_value;
566   }
567   return 1;
568 }
569 
CBS_get_optional_asn1_bool(CBS * cbs,int * out,unsigned tag,int default_value)570 int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
571                                int default_value) {
572   CBS child, child2;
573   int present;
574   if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
575     return 0;
576   }
577   if (present) {
578     uint8_t boolean;
579 
580     if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
581         CBS_len(&child2) != 1 ||
582         CBS_len(&child) != 0) {
583       return 0;
584     }
585 
586     boolean = CBS_data(&child2)[0];
587     if (boolean == 0) {
588       *out = 0;
589     } else if (boolean == 0xff) {
590       *out = 1;
591     } else {
592       return 0;
593     }
594   } else {
595     *out = default_value;
596   }
597   return 1;
598 }
599 
CBS_is_valid_asn1_bitstring(const CBS * cbs)600 int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
601   CBS in = *cbs;
602   uint8_t num_unused_bits;
603   if (!CBS_get_u8(&in, &num_unused_bits) ||
604       num_unused_bits > 7) {
605     return 0;
606   }
607 
608   if (num_unused_bits == 0) {
609     return 1;
610   }
611 
612   // All num_unused_bits bits must exist and be zeros.
613   uint8_t last;
614   if (!CBS_get_last_u8(&in, &last) ||
615       (last & ((1 << num_unused_bits) - 1)) != 0) {
616     return 0;
617   }
618 
619   return 1;
620 }
621 
CBS_asn1_bitstring_has_bit(const CBS * cbs,unsigned bit)622 int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
623   if (!CBS_is_valid_asn1_bitstring(cbs)) {
624     return 0;
625   }
626 
627   const unsigned byte_num = (bit >> 3) + 1;
628   const unsigned bit_num = 7 - (bit & 7);
629 
630   // Unused bits are zero, and this function does not distinguish between
631   // missing and unset bits. Thus it is sufficient to do a byte-level length
632   // check.
633   return byte_num < CBS_len(cbs) &&
634          (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
635 }
636 
CBS_is_valid_asn1_integer(const CBS * cbs,int * out_is_negative)637 int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) {
638   CBS copy = *cbs;
639   uint8_t first_byte, second_byte;
640   if (!CBS_get_u8(&copy, &first_byte)) {
641     return 0;  // INTEGERs may not be empty.
642   }
643   if (out_is_negative != NULL) {
644     *out_is_negative = (first_byte & 0x80) != 0;
645   }
646   if (!CBS_get_u8(&copy, &second_byte)) {
647     return 1;  // One byte INTEGERs are always minimal.
648   }
649   if ((first_byte == 0x00 && (second_byte & 0x80) == 0) ||
650       (first_byte == 0xff && (second_byte & 0x80) != 0)) {
651     return 0;  // The value is minimal iff the first 9 bits are not all equal.
652   }
653   return 1;
654 }
655 
CBS_is_unsigned_asn1_integer(const CBS * cbs)656 int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
657   int is_negative;
658   return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative;
659 }
660 
add_decimal(CBB * out,uint64_t v)661 static int add_decimal(CBB *out, uint64_t v) {
662   char buf[DECIMAL_SIZE(uint64_t) + 1];
663   BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
664   return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
665 }
666 
CBS_asn1_oid_to_text(const CBS * cbs)667 char *CBS_asn1_oid_to_text(const CBS *cbs) {
668   CBB cbb;
669   if (!CBB_init(&cbb, 32)) {
670     goto err;
671   }
672 
673   CBS copy = *cbs;
674   // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
675   uint64_t v;
676   if (!parse_base128_integer(&copy, &v)) {
677     goto err;
678   }
679 
680   if (v >= 80) {
681     if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
682         !add_decimal(&cbb, v - 80)) {
683       goto err;
684     }
685   } else if (!add_decimal(&cbb, v / 40) ||
686              !CBB_add_u8(&cbb, '.') ||
687              !add_decimal(&cbb, v % 40)) {
688     goto err;
689   }
690 
691   while (CBS_len(&copy) != 0) {
692     if (!parse_base128_integer(&copy, &v) ||
693         !CBB_add_u8(&cbb, '.') ||
694         !add_decimal(&cbb, v)) {
695       goto err;
696     }
697   }
698 
699   uint8_t *txt;
700   size_t txt_len;
701   if (!CBB_add_u8(&cbb, '\0') ||
702       !CBB_finish(&cbb, &txt, &txt_len)) {
703     goto err;
704   }
705 
706   return (char *)txt;
707 
708 err:
709   CBB_cleanup(&cbb);
710   return NULL;
711 }
712