1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include <openssl/buf.h>
16 #include <openssl/mem.h>
17 #include <openssl/bytestring.h>
25 void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
30 static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
41 int CBS_skip(CBS *cbs, size_t len) {
43 return cbs_get(cbs, &dummy, len);
46 const uint8_t *CBS_data(const CBS *cbs) {
50 size_t CBS_len(const CBS *cbs) {
54 int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
55 if (*out_ptr != NULL) {
56 OPENSSL_free(*out_ptr);
64 *out_ptr = BUF_memdup(cbs->data, cbs->len);
65 if (*out_ptr == NULL) {
72 int CBS_strdup(const CBS *cbs, char **out_ptr) {
73 if (*out_ptr != NULL) {
74 OPENSSL_free(*out_ptr);
76 *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
77 return (*out_ptr != NULL);
80 int CBS_contains_zero_byte(const CBS *cbs) {
81 return memchr(cbs->data, 0, cbs->len) != NULL;
84 int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
87 return CRYPTO_memcmp(cbs->data, data, len) == 0;
90 static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
95 if (!cbs_get(cbs, &data, len)) {
98 for (i = 0; i < len; i++) {
106 int CBS_get_u8(CBS *cbs, uint8_t *out) {
108 if (!cbs_get(cbs, &v, 1)) {
115 int CBS_get_u16(CBS *cbs, uint16_t *out) {
117 if (!cbs_get_u(cbs, &v, 2)) {
124 int CBS_get_u24(CBS *cbs, uint32_t *out) {
125 return cbs_get_u(cbs, out, 3);
128 int CBS_get_u32(CBS *cbs, uint32_t *out) {
129 return cbs_get_u(cbs, out, 4);
132 int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
134 if (!cbs_get(cbs, &v, len)) {
137 CBS_init(out, v, len);
141 static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
143 if (!cbs_get_u(cbs, &len, len_len)) {
146 return CBS_get_bytes(cbs, out, len);
149 int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
150 return cbs_get_length_prefixed(cbs, out, 1);
153 int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
154 return cbs_get_length_prefixed(cbs, out, 2);
157 int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
158 return cbs_get_length_prefixed(cbs, out, 3);
161 int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
162 size_t *out_header_len) {
163 uint8_t tag, length_byte;
171 if (!CBS_get_u8(&header, &tag) ||
172 !CBS_get_u8(&header, &length_byte)) {
176 if ((tag & 0x1f) == 0x1f) {
177 /* Long form tags are not supported. */
181 if (out_tag != NULL) {
186 if ((length_byte & 0x80) == 0) {
187 /* Short form length. */
188 len = ((size_t) length_byte) + 2;
189 if (out_header_len != NULL) {
193 /* Long form length. */
194 const size_t num_bytes = length_byte & 0x7f;
197 if ((tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
198 /* indefinite length */
200 return CBS_get_bytes(cbs, out, 2);
203 if (num_bytes == 0 || num_bytes > 4) {
206 if (!cbs_get_u(&header, &len32, num_bytes)) {
210 /* Length should have used short-form encoding. */
213 if ((len32 >> ((num_bytes-1)*8)) == 0) {
214 /* Length should have been at least one byte shorter. */
218 if (len + 2 + num_bytes < len) {
222 len += 2 + num_bytes;
223 if (out_header_len != NULL) {
224 *out_header_len = 2 + num_bytes;
228 return CBS_get_bytes(cbs, out, len);
231 static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
241 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
244 /* This ensures that the tag is either zero length or
245 * indefinite-length. */
246 CBS_len(out) == header_len &&
247 CBS_data(out)[header_len - 1] == 0x80)) {
251 if (skip_header && !CBS_skip(out, header_len)) {
259 int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
260 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
263 int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
264 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
267 int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
268 if (CBS_len(cbs) < 1) {
271 return CBS_data(cbs)[0] == tag_value;
274 int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
279 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
284 data = CBS_data(&bytes);
285 len = CBS_len(&bytes);
288 /* An INTEGER is encoded with at least one octet. */
292 if ((data[0] & 0x80) != 0) {
293 /* negative number */
297 for (i = 0; i < len; i++) {
298 if ((*out >> 56) != 0) {
299 /* Too large to represent as a uint64_t. */
309 int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
310 if (CBS_peek_asn1_tag(cbs, tag)) {
311 if (!CBS_get_asn1(cbs, out, tag)) {
321 int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
325 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
329 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
330 CBS_len(&child) != 0) {
334 CBS_init(out, NULL, 0);
337 *out_present = present;
342 int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
343 uint64_t default_value) {
346 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
350 if (!CBS_get_asn1_uint64(&child, out) ||
351 CBS_len(&child) != 0) {
355 *out = default_value;
360 int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
364 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
370 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
371 CBS_len(&child2) != 1 ||
372 CBS_len(&child) != 0) {
376 boolean = CBS_data(&child2)[0];
379 } else if (boolean == 0xff) {
385 *out = default_value;