2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2014-06-25 04:27:17 +01:00
|
|
|
#include <openssl/buf.h>
|
|
|
|
#include <openssl/mem.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/bytestring.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-11-29 22:17:41 +00:00
|
|
|
#include <inttypes.h>
|
2014-07-15 00:13:02 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-08-21 18:54:06 +01:00
|
|
|
#include "internal.h"
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../internal.h"
|
2014-08-21 18:54:06 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
|
|
|
|
cbs->data = data;
|
|
|
|
cbs->len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
|
|
|
|
if (cbs->len < n) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = cbs->data;
|
|
|
|
cbs->data += n;
|
|
|
|
cbs->len -= n;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_skip(CBS *cbs, size_t len) {
|
|
|
|
const uint8_t *dummy;
|
|
|
|
return cbs_get(cbs, &dummy, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *CBS_data(const CBS *cbs) {
|
|
|
|
return cbs->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CBS_len(const CBS *cbs) {
|
|
|
|
return cbs->len;
|
|
|
|
}
|
|
|
|
|
2014-06-25 04:27:17 +01:00
|
|
|
int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
|
2015-04-22 18:50:28 +01:00
|
|
|
OPENSSL_free(*out_ptr);
|
|
|
|
*out_ptr = NULL;
|
2014-06-25 04:27:17 +01:00
|
|
|
*out_len = 0;
|
|
|
|
|
|
|
|
if (cbs->len == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*out_ptr = BUF_memdup(cbs->data, cbs->len);
|
|
|
|
if (*out_ptr == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out_len = cbs->len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:13:02 +01:00
|
|
|
int CBS_strdup(const CBS *cbs, char **out_ptr) {
|
|
|
|
if (*out_ptr != NULL) {
|
|
|
|
OPENSSL_free(*out_ptr);
|
|
|
|
}
|
|
|
|
*out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
|
|
|
|
return (*out_ptr != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_contains_zero_byte(const CBS *cbs) {
|
2016-12-13 06:07:13 +00:00
|
|
|
return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL;
|
2014-06-25 04:27:17 +01:00
|
|
|
}
|
|
|
|
|
2014-07-13 17:29:21 +01:00
|
|
|
int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
|
2015-02-11 06:16:26 +00:00
|
|
|
if (len != cbs->len) {
|
2014-07-13 17:29:21 +01:00
|
|
|
return 0;
|
2015-02-11 06:16:26 +00:00
|
|
|
}
|
2014-07-13 17:29:21 +01:00
|
|
|
return CRYPTO_memcmp(cbs->data, data, len) == 0;
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:40:06 +00:00
|
|
|
static int cbs_get_u(CBS *cbs, uint64_t *out, size_t len) {
|
|
|
|
uint64_t result = 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
const uint8_t *data;
|
|
|
|
|
|
|
|
if (!cbs_get(cbs, &data, len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-05 17:47:25 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2014-06-20 20:00:00 +01:00
|
|
|
result <<= 8;
|
|
|
|
result |= data[i];
|
|
|
|
}
|
|
|
|
*out = result;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u8(CBS *cbs, uint8_t *out) {
|
|
|
|
const uint8_t *v;
|
|
|
|
if (!cbs_get(cbs, &v, 1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out = *v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u16(CBS *cbs, uint16_t *out) {
|
2019-02-21 17:40:06 +00:00
|
|
|
uint64_t v;
|
2014-06-20 20:00:00 +01:00
|
|
|
if (!cbs_get_u(cbs, &v, 2)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u24(CBS *cbs, uint32_t *out) {
|
2019-02-21 17:40:06 +00:00
|
|
|
uint64_t v;
|
|
|
|
if (!cbs_get_u(cbs, &v, 3)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out = v;
|
|
|
|
return 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u32(CBS *cbs, uint32_t *out) {
|
2019-02-21 17:40:06 +00:00
|
|
|
uint64_t v;
|
|
|
|
if (!cbs_get_u(cbs, &v, 4)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u64(CBS *cbs, uint64_t *out) {
|
|
|
|
return cbs_get_u(cbs, out, 8);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 23:54:51 +01:00
|
|
|
int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
|
|
|
|
if (cbs->len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out = cbs->data[cbs->len - 1];
|
|
|
|
cbs->len--;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
|
|
|
|
const uint8_t *v;
|
|
|
|
if (!cbs_get(cbs, &v, len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
CBS_init(out, v, len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-29 02:34:45 +01:00
|
|
|
int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
|
|
|
|
const uint8_t *v;
|
|
|
|
if (!cbs_get(cbs, &v, len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(out, v, len);
|
2015-07-29 02:34:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
|
2019-02-21 17:40:06 +00:00
|
|
|
uint64_t len;
|
2014-06-20 20:00:00 +01:00
|
|
|
if (!cbs_get_u(cbs, &len, len_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-21 17:40:06 +00:00
|
|
|
// If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on
|
|
|
|
// 32-bit systems.
|
|
|
|
assert(len_len <= 3);
|
2014-06-20 20:00:00 +01:00
|
|
|
return CBS_get_bytes(cbs, out, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
|
|
|
|
return cbs_get_length_prefixed(cbs, out, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
|
|
|
|
return cbs_get_length_prefixed(cbs, out, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
|
|
|
|
return cbs_get_length_prefixed(cbs, out, 3);
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:17:41 +00:00
|
|
|
// parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
|
|
|
|
// |*out| to the result. This is the encoding used in DER for both high tag
|
|
|
|
// number form and OID components.
|
|
|
|
static int parse_base128_integer(CBS *cbs, uint64_t *out) {
|
|
|
|
uint64_t v = 0;
|
|
|
|
uint8_t b;
|
|
|
|
do {
|
|
|
|
if (!CBS_get_u8(cbs, &b)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((v >> (64 - 7)) != 0) {
|
|
|
|
// The value is too large.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (v == 0 && b == 0x80) {
|
|
|
|
// The value must be minimally encoded.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
v = (v << 7) | (b & 0x7f);
|
|
|
|
|
|
|
|
// Values end at an octet with the high bit cleared.
|
|
|
|
} while (b & 0x80);
|
|
|
|
|
|
|
|
*out = v;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:11:23 +00:00
|
|
|
static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
|
|
|
uint8_t tag_byte;
|
|
|
|
if (!CBS_get_u8(cbs, &tag_byte)) {
|
2017-11-21 21:25:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-11-30 21:41:24 +00:00
|
|
|
// ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
|
|
|
|
// number no greater than 30.
|
|
|
|
//
|
|
|
|
// If the number portion is 31 (0x1f, the largest value that fits in the
|
|
|
|
// allotted bits), then the tag is more than one byte long and the
|
|
|
|
// continuation bytes contain the tag number. This parser only supports tag
|
|
|
|
// numbers less than 31 (and thus single-byte tags).
|
2017-12-22 04:11:23 +00:00
|
|
|
unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
|
|
|
|
unsigned tag_number = tag_byte & 0x1f;
|
|
|
|
if (tag_number == 0x1f) {
|
|
|
|
uint64_t v;
|
|
|
|
if (!parse_base128_integer(cbs, &v) ||
|
|
|
|
// Check the tag number is within our supported bounds.
|
|
|
|
v > CBS_ASN1_TAG_NUMBER_MASK ||
|
|
|
|
// Small tag numbers should have used low tag number form.
|
|
|
|
v < 0x1f) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tag_number = (unsigned)v;
|
2017-11-21 21:25:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 04:11:23 +00:00
|
|
|
tag |= tag_number;
|
|
|
|
|
|
|
|
*out = tag;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
|
|
|
size_t *out_header_len, int ber_ok) {
|
|
|
|
CBS header = *cbs;
|
|
|
|
CBS throwaway;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
out = &throwaway;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned tag;
|
|
|
|
if (!parse_asn1_tag(&header, &tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-30 21:41:24 +00:00
|
|
|
if (out_tag != NULL) {
|
|
|
|
*out_tag = tag;
|
|
|
|
}
|
2017-11-21 21:25:12 +00:00
|
|
|
|
2017-12-22 04:11:23 +00:00
|
|
|
uint8_t length_byte;
|
|
|
|
if (!CBS_get_u8(&header, &length_byte)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t header_len = CBS_len(cbs) - CBS_len(&header);
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
size_t len;
|
2017-08-18 19:06:02 +01:00
|
|
|
// The format for the length encoding is specified in ITU-T X.690 section
|
|
|
|
// 8.1.3.
|
2014-06-20 20:00:00 +01:00
|
|
|
if ((length_byte & 0x80) == 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Short form length.
|
2017-12-22 04:11:23 +00:00
|
|
|
len = ((size_t) length_byte) + header_len;
|
2014-08-28 01:19:50 +01:00
|
|
|
if (out_header_len != NULL) {
|
2017-12-22 04:11:23 +00:00
|
|
|
*out_header_len = header_len;
|
2014-08-28 01:19:50 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
} else {
|
2017-08-18 19:06:02 +01:00
|
|
|
// The high bit indicate that this is the long form, while the next 7 bits
|
|
|
|
// encode the number of subsequent octets used to encode the length (ITU-T
|
|
|
|
// X.690 clause 8.1.3.5.b).
|
2014-06-20 20:00:00 +01:00
|
|
|
const size_t num_bytes = length_byte & 0x7f;
|
2019-02-21 17:40:06 +00:00
|
|
|
uint64_t len64;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-06-02 23:49:48 +01:00
|
|
|
if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// indefinite length
|
2015-06-02 23:48:37 +01:00
|
|
|
if (out_header_len != NULL) {
|
2017-12-22 04:11:23 +00:00
|
|
|
*out_header_len = header_len;
|
2015-06-02 23:48:37 +01:00
|
|
|
}
|
2017-12-22 04:11:23 +00:00
|
|
|
return CBS_get_bytes(cbs, out, header_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
|
|
|
|
// used as the first byte of the length. If this parser encounters that
|
|
|
|
// value, num_bytes will be parsed as 127, which will fail the check below.
|
2014-06-20 20:00:00 +01:00
|
|
|
if (num_bytes == 0 || num_bytes > 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-21 17:40:06 +00:00
|
|
|
if (!cbs_get_u(&header, &len64, num_bytes)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-18 19:06:02 +01:00
|
|
|
// ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
|
|
|
|
// with the minimum number of octets.
|
2019-02-21 17:40:06 +00:00
|
|
|
if (len64 < 128) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Length should have used short-form encoding.
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2019-02-21 17:40:06 +00:00
|
|
|
if ((len64 >> ((num_bytes-1)*8)) == 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Length should have been at least one byte shorter.
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2019-02-21 17:40:06 +00:00
|
|
|
len = len64;
|
2017-12-22 04:11:23 +00:00
|
|
|
if (len + header_len + num_bytes < len) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Overflow.
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-22 04:11:23 +00:00
|
|
|
len += header_len + num_bytes;
|
2014-08-28 01:19:50 +01:00
|
|
|
if (out_header_len != NULL) {
|
2017-12-22 04:11:23 +00:00
|
|
|
*out_header_len = header_len + num_bytes;
|
2014-08-28 01:19:50 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return CBS_get_bytes(cbs, out, len);
|
|
|
|
}
|
|
|
|
|
2016-09-30 20:05:32 +01:00
|
|
|
int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
|
|
|
|
size_t header_len;
|
|
|
|
if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CBS_skip(out, header_len)) {
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 23:49:48 +01:00
|
|
|
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
|
|
|
size_t *out_header_len) {
|
|
|
|
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
|
|
|
|
0 /* DER only */);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
|
|
|
size_t *out_header_len) {
|
|
|
|
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
|
|
|
|
1 /* BER allowed */);
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:54:06 +01:00
|
|
|
static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
|
2014-06-20 20:00:00 +01:00
|
|
|
int skip_header) {
|
|
|
|
size_t header_len;
|
|
|
|
unsigned tag;
|
|
|
|
CBS throwaway;
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
out = &throwaway;
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:54:06 +01:00
|
|
|
if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
|
2015-06-02 23:49:48 +01:00
|
|
|
tag != tag_value) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip_header && !CBS_skip(out, header_len)) {
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
|
2014-08-21 18:54:06 +01:00
|
|
|
return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-08-21 18:54:06 +01:00
|
|
|
int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
|
|
|
|
return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-10-10 00:39:15 +01:00
|
|
|
int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
|
|
|
|
if (CBS_len(cbs) < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-22 04:11:23 +00:00
|
|
|
|
|
|
|
CBS copy = *cbs;
|
|
|
|
unsigned actual_tag;
|
|
|
|
return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag;
|
2014-10-10 00:39:15 +01:00
|
|
|
}
|
|
|
|
|
2014-08-21 18:54:06 +01:00
|
|
|
int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
|
|
|
|
CBS bytes;
|
|
|
|
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = 0;
|
2017-04-17 15:04:39 +01:00
|
|
|
const uint8_t *data = CBS_data(&bytes);
|
|
|
|
size_t len = CBS_len(&bytes);
|
2014-08-21 18:54:06 +01:00
|
|
|
|
2014-10-21 05:45:25 +01:00
|
|
|
if (len == 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// An INTEGER is encoded with at least one octet.
|
2014-10-21 05:45:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((data[0] & 0x80) != 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Negative number.
|
2015-04-03 23:06:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Extra leading zeros.
|
2014-08-21 18:54:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:04:39 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2014-08-21 18:54:06 +01:00
|
|
|
if ((*out >> 56) != 0) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Too large to represent as a uint64_t.
|
2014-08-21 18:54:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*out <<= 8;
|
|
|
|
*out |= data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2014-10-19 09:33:38 +01:00
|
|
|
|
2018-01-25 23:37:46 +00:00
|
|
|
int CBS_get_asn1_bool(CBS *cbs, int *out) {
|
|
|
|
CBS bytes;
|
|
|
|
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) ||
|
|
|
|
CBS_len(&bytes) != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t value = *CBS_data(&bytes);
|
|
|
|
if (value != 0 && value != 0xff) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = !!value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-19 09:33:38 +01:00
|
|
|
int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
|
2015-09-03 14:40:21 +01:00
|
|
|
int present = 0;
|
|
|
|
|
2014-10-19 09:33:38 +01:00
|
|
|
if (CBS_peek_asn1_tag(cbs, tag)) {
|
|
|
|
if (!CBS_get_asn1(cbs, out, tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-03 14:40:21 +01:00
|
|
|
present = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_present != NULL) {
|
|
|
|
*out_present = present;
|
2014-10-19 09:33:38 +01:00
|
|
|
}
|
2015-09-03 14:40:21 +01:00
|
|
|
|
2014-10-19 09:33:38 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
|
|
|
|
unsigned tag) {
|
|
|
|
CBS child;
|
|
|
|
int present;
|
|
|
|
if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (present) {
|
2018-03-15 20:54:33 +00:00
|
|
|
assert(out);
|
2014-10-19 09:33:38 +01:00
|
|
|
if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
|
|
|
|
CBS_len(&child) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CBS_init(out, NULL, 0);
|
|
|
|
}
|
|
|
|
if (out_present) {
|
|
|
|
*out_present = present;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
|
|
|
|
uint64_t default_value) {
|
|
|
|
CBS child;
|
|
|
|
int present;
|
|
|
|
if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (present) {
|
|
|
|
if (!CBS_get_asn1_uint64(&child, out) ||
|
|
|
|
CBS_len(&child) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*out = default_value;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-11 00:23:43 +01:00
|
|
|
|
|
|
|
int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
|
|
|
|
int default_value) {
|
|
|
|
CBS child, child2;
|
|
|
|
int present;
|
|
|
|
if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (present) {
|
|
|
|
uint8_t boolean;
|
|
|
|
|
|
|
|
if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
|
|
|
|
CBS_len(&child2) != 1 ||
|
|
|
|
CBS_len(&child) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean = CBS_data(&child2)[0];
|
|
|
|
if (boolean == 0) {
|
|
|
|
*out = 0;
|
|
|
|
} else if (boolean == 0xff) {
|
|
|
|
*out = 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*out = default_value;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2016-12-14 01:48:19 +00:00
|
|
|
|
|
|
|
int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
|
|
|
|
CBS in = *cbs;
|
|
|
|
uint8_t num_unused_bits;
|
|
|
|
if (!CBS_get_u8(&in, &num_unused_bits) ||
|
|
|
|
num_unused_bits > 7) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_unused_bits == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// All num_unused_bits bits must exist and be zeros.
|
2016-12-14 01:48:19 +00:00
|
|
|
uint8_t last;
|
|
|
|
if (!CBS_get_last_u8(&in, &last) ||
|
|
|
|
(last & ((1 << num_unused_bits) - 1)) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
|
|
|
|
if (!CBS_is_valid_asn1_bitstring(cbs)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned byte_num = (bit >> 3) + 1;
|
|
|
|
const unsigned bit_num = 7 - (bit & 7);
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Unused bits are zero, and this function does not distinguish between
|
|
|
|
// missing and unset bits. Thus it is sufficient to do a byte-level length
|
|
|
|
// check.
|
2016-12-14 01:48:19 +00:00
|
|
|
return byte_num < CBS_len(cbs) &&
|
|
|
|
(CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
|
|
|
|
}
|
2017-11-29 22:17:41 +00:00
|
|
|
|
|
|
|
static int add_decimal(CBB *out, uint64_t v) {
|
|
|
|
char buf[DECIMAL_SIZE(uint64_t) + 1];
|
|
|
|
BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
|
|
|
|
return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *CBS_asn1_oid_to_text(const CBS *cbs) {
|
|
|
|
CBB cbb;
|
|
|
|
if (!CBB_init(&cbb, 32)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBS copy = *cbs;
|
|
|
|
// The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
|
|
|
|
uint64_t v;
|
|
|
|
if (!parse_base128_integer(©, &v)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v >= 80) {
|
|
|
|
if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
|
|
|
|
!add_decimal(&cbb, v - 80)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else if (!add_decimal(&cbb, v / 40) ||
|
|
|
|
!CBB_add_u8(&cbb, '.') ||
|
|
|
|
!add_decimal(&cbb, v % 40)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (CBS_len(©) != 0) {
|
|
|
|
if (!parse_base128_integer(©, &v) ||
|
|
|
|
!CBB_add_u8(&cbb, '.') ||
|
|
|
|
!add_decimal(&cbb, v)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *txt;
|
|
|
|
size_t txt_len;
|
|
|
|
if (!CBB_add_u8(&cbb, '\0') ||
|
|
|
|
!CBB_finish(&cbb, &txt, &txt_len)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *)txt;
|
|
|
|
|
|
|
|
err:
|
|
|
|
CBB_cleanup(&cbb);
|
|
|
|
return NULL;
|
|
|
|
}
|