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