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>
23 void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
28 static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
39 int CBS_skip(CBS *cbs, size_t len) {
41 return cbs_get(cbs, &dummy, len);
44 const uint8_t *CBS_data(const CBS *cbs) {
48 size_t CBS_len(const CBS *cbs) {
52 int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
53 if (*out_ptr != NULL) {
54 OPENSSL_free(*out_ptr);
62 *out_ptr = BUF_memdup(cbs->data, cbs->len);
63 if (*out_ptr == NULL) {
70 int CBS_strdup(const CBS *cbs, char **out_ptr) {
71 if (*out_ptr != NULL) {
72 OPENSSL_free(*out_ptr);
74 *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
75 return (*out_ptr != NULL);
78 int CBS_contains_zero_byte(const CBS *cbs) {
79 return memchr(cbs->data, 0, cbs->len) != NULL;
82 int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
85 return CRYPTO_memcmp(cbs->data, data, len) == 0;
88 static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
93 if (!cbs_get(cbs, &data, len)) {
96 for (i = 0; i < len; i++) {
104 int CBS_get_u8(CBS *cbs, uint8_t *out) {
106 if (!cbs_get(cbs, &v, 1)) {
113 int CBS_get_u16(CBS *cbs, uint16_t *out) {
115 if (!cbs_get_u(cbs, &v, 2)) {
122 int CBS_get_u24(CBS *cbs, uint32_t *out) {
123 return cbs_get_u(cbs, out, 3);
126 int CBS_get_u32(CBS *cbs, uint32_t *out) {
127 return cbs_get_u(cbs, out, 4);
130 int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
132 if (!cbs_get(cbs, &v, len)) {
135 CBS_init(out, v, len);
139 static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
141 if (!cbs_get_u(cbs, &len, len_len)) {
144 return CBS_get_bytes(cbs, out, len);
147 int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
148 return cbs_get_length_prefixed(cbs, out, 1);
151 int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
152 return cbs_get_length_prefixed(cbs, out, 2);
155 int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
156 return cbs_get_length_prefixed(cbs, out, 3);
159 static int cbs_get_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
160 size_t *out_header_len, unsigned depth,
161 int *was_indefinite_len);
163 /* cbs_get_asn1_indefinite_len sets |*out| to be a CBS that covers an
164 * indefinite length element in |cbs| and advances |*in|. On entry, |cbs| will
165 * not have had the tag and length byte removed. On exit, |*out| does not cover
166 * the EOC element, but |*in| is skipped over it.
168 * The |depth| argument counts the number of times the code has recursed trying
169 * to find an indefinite length. */
170 static int cbs_get_asn1_indefinite_len(CBS *in, CBS *out, unsigned depth) {
171 static const size_t kEOCLength = 2;
174 int was_indefinite_len;
175 CBS orig = *in, child;
177 if (!CBS_skip(in, 2 /* tag plus 0x80 byte for indefinite len */)) {
182 if (!cbs_get_asn1_element(in, &child, &tag, &header_len, depth + 1,
183 &was_indefinite_len)) {
187 if (!was_indefinite_len && CBS_len(&child) == kEOCLength &&
188 header_len == kEOCLength && tag == 0) {
193 return CBS_get_bytes(&orig, out, CBS_len(&orig) - CBS_len(in) - kEOCLength);
196 /* MAX_DEPTH the maximum number of levels of indefinite lengths that we'll
200 static int cbs_get_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
201 size_t *out_header_len, unsigned depth,
202 int *was_indefinite_len) {
203 uint8_t tag, length_byte;
205 if (!CBS_get_u8(&header, &tag) ||
206 !CBS_get_u8(&header, &length_byte)) {
210 if ((tag & 0x1f) == 0x1f) {
211 /* Long form tags are not supported. */
216 if (was_indefinite_len) {
217 *was_indefinite_len = 0;
221 if ((length_byte & 0x80) == 0) {
222 /* Short form length. */
223 len = ((size_t) length_byte) + 2;
226 /* Long form length. */
227 const size_t num_bytes = length_byte & 0x7f;
230 if (depth < MAX_DEPTH && num_bytes == 0) {
231 /* indefinite length */
233 if (was_indefinite_len) {
234 *was_indefinite_len = 1;
236 return cbs_get_asn1_indefinite_len(cbs, out, depth);
239 if (num_bytes == 0 || num_bytes > 4) {
242 if (!cbs_get_u(&header, &len32, num_bytes)) {
246 /* Length should have used short-form encoding. */
249 if ((len32 >> ((num_bytes-1)*8)) == 0) {
250 /* Length should have been at least one byte shorter. */
254 if (len + 2 + num_bytes < len) {
258 len += 2 + num_bytes;
259 *out_header_len = 2 + num_bytes;
262 return CBS_get_bytes(cbs, out, len);
265 static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value, int ber,
275 if (!cbs_get_asn1_element(cbs, out, &tag, &header_len, ber ? 0 : MAX_DEPTH,
281 if (skip_header && !CBS_skip(out, header_len)) {
289 int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
290 return cbs_get_asn1(cbs, out, tag_value, 0 /* DER */,
291 1 /* skip header */);
294 int CBS_get_asn1_ber(CBS *cbs, CBS *out, unsigned tag_value) {
295 return cbs_get_asn1(cbs, out, tag_value, 1 /* BER */,
296 1 /* skip header */);
299 int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
300 return cbs_get_asn1(cbs, out, tag_value, 0 /* DER */,
301 0 /* include header */);