Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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/buf.h> |
| 16 | #include <openssl/mem.h> |
| 17 | #include <openssl/bytestring.h> |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "internal.h" |
| 23 | |
| 24 | |
| 25 | void CBS_init(CBS *cbs, const uint8_t *data, size_t len) { |
| 26 | cbs->data = data; |
| 27 | cbs->len = len; |
| 28 | } |
| 29 | |
| 30 | static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) { |
| 31 | if (cbs->len < n) { |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | *p = cbs->data; |
| 36 | cbs->data += n; |
| 37 | cbs->len -= n; |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | int CBS_skip(CBS *cbs, size_t len) { |
| 42 | const uint8_t *dummy; |
| 43 | return cbs_get(cbs, &dummy, len); |
| 44 | } |
| 45 | |
| 46 | const uint8_t *CBS_data(const CBS *cbs) { |
| 47 | return cbs->data; |
| 48 | } |
| 49 | |
| 50 | size_t CBS_len(const CBS *cbs) { |
| 51 | return cbs->len; |
| 52 | } |
| 53 | |
| 54 | int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 55 | OPENSSL_free(*out_ptr); |
| 56 | *out_ptr = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 57 | *out_len = 0; |
| 58 | |
| 59 | if (cbs->len == 0) { |
| 60 | return 1; |
| 61 | } |
| 62 | *out_ptr = BUF_memdup(cbs->data, cbs->len); |
| 63 | if (*out_ptr == NULL) { |
| 64 | return 0; |
| 65 | } |
| 66 | *out_len = cbs->len; |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | int CBS_strdup(const CBS *cbs, char **out_ptr) { |
| 71 | if (*out_ptr != NULL) { |
| 72 | OPENSSL_free(*out_ptr); |
| 73 | } |
| 74 | *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len); |
| 75 | return (*out_ptr != NULL); |
| 76 | } |
| 77 | |
| 78 | int CBS_contains_zero_byte(const CBS *cbs) { |
| 79 | return memchr(cbs->data, 0, cbs->len) != NULL; |
| 80 | } |
| 81 | |
| 82 | int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 83 | if (len != cbs->len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 84 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 85 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 86 | return CRYPTO_memcmp(cbs->data, data, len) == 0; |
| 87 | } |
| 88 | |
| 89 | static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) { |
| 90 | uint32_t result = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 91 | const uint8_t *data; |
| 92 | |
| 93 | if (!cbs_get(cbs, &data, len)) { |
| 94 | return 0; |
| 95 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 96 | for (size_t i = 0; i < len; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 97 | result <<= 8; |
| 98 | result |= data[i]; |
| 99 | } |
| 100 | *out = result; |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | int CBS_get_u8(CBS *cbs, uint8_t *out) { |
| 105 | const uint8_t *v; |
| 106 | if (!cbs_get(cbs, &v, 1)) { |
| 107 | return 0; |
| 108 | } |
| 109 | *out = *v; |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | int CBS_get_u16(CBS *cbs, uint16_t *out) { |
| 114 | uint32_t v; |
| 115 | if (!cbs_get_u(cbs, &v, 2)) { |
| 116 | return 0; |
| 117 | } |
| 118 | *out = v; |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | int CBS_get_u24(CBS *cbs, uint32_t *out) { |
| 123 | return cbs_get_u(cbs, out, 3); |
| 124 | } |
| 125 | |
| 126 | int CBS_get_u32(CBS *cbs, uint32_t *out) { |
| 127 | return cbs_get_u(cbs, out, 4); |
| 128 | } |
| 129 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 130 | int CBS_get_last_u8(CBS *cbs, uint8_t *out) { |
| 131 | if (cbs->len == 0) { |
| 132 | return 0; |
| 133 | } |
| 134 | *out = cbs->data[cbs->len - 1]; |
| 135 | cbs->len--; |
| 136 | return 1; |
| 137 | } |
| 138 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) { |
| 140 | const uint8_t *v; |
| 141 | if (!cbs_get(cbs, &v, len)) { |
| 142 | return 0; |
| 143 | } |
| 144 | CBS_init(out, v, len); |
| 145 | return 1; |
| 146 | } |
| 147 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 148 | int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) { |
| 149 | const uint8_t *v; |
| 150 | if (!cbs_get(cbs, &v, len)) { |
| 151 | return 0; |
| 152 | } |
| 153 | memcpy(out, v, len); |
| 154 | return 1; |
| 155 | } |
| 156 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) { |
| 158 | uint32_t len; |
| 159 | if (!cbs_get_u(cbs, &len, len_len)) { |
| 160 | return 0; |
| 161 | } |
| 162 | return CBS_get_bytes(cbs, out, len); |
| 163 | } |
| 164 | |
| 165 | int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) { |
| 166 | return cbs_get_length_prefixed(cbs, out, 1); |
| 167 | } |
| 168 | |
| 169 | int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) { |
| 170 | return cbs_get_length_prefixed(cbs, out, 2); |
| 171 | } |
| 172 | |
| 173 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) { |
| 174 | return cbs_get_length_prefixed(cbs, out, 3); |
| 175 | } |
| 176 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 177 | static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 178 | size_t *out_header_len, int ber_ok) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | uint8_t tag, length_byte; |
| 180 | CBS header = *cbs; |
| 181 | CBS throwaway; |
| 182 | |
| 183 | if (out == NULL) { |
| 184 | out = &throwaway; |
| 185 | } |
| 186 | |
| 187 | if (!CBS_get_u8(&header, &tag) || |
| 188 | !CBS_get_u8(&header, &length_byte)) { |
| 189 | return 0; |
| 190 | } |
| 191 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 192 | /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag |
| 193 | * number no greater than 30. |
| 194 | * |
| 195 | * If the number portion is 31 (0x1f, the largest value that fits in the |
| 196 | * allotted bits), then the tag is more than one byte long and the |
| 197 | * continuation bytes contain the tag number. This parser only supports tag |
| 198 | * numbers less than 31 (and thus single-byte tags). */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 199 | if ((tag & 0x1f) == 0x1f) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | if (out_tag != NULL) { |
| 204 | *out_tag = tag; |
| 205 | } |
| 206 | |
| 207 | size_t len; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 208 | /* The format for the length encoding is specified in ITU-T X.690 section |
| 209 | * 8.1.3. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | if ((length_byte & 0x80) == 0) { |
| 211 | /* Short form length. */ |
| 212 | len = ((size_t) length_byte) + 2; |
| 213 | if (out_header_len != NULL) { |
| 214 | *out_header_len = 2; |
| 215 | } |
| 216 | } else { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 217 | /* The high bit indicate that this is the long form, while the next 7 bits |
| 218 | * encode the number of subsequent octets used to encode the length (ITU-T |
| 219 | * X.690 clause 8.1.3.5.b). */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | const size_t num_bytes = length_byte & 0x7f; |
| 221 | uint32_t len32; |
| 222 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 223 | if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 224 | /* indefinite length */ |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 225 | if (out_header_len != NULL) { |
| 226 | *out_header_len = 2; |
| 227 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | return CBS_get_bytes(cbs, out, 2); |
| 229 | } |
| 230 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 231 | /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be |
| 232 | * used as the first byte of the length. If this parser encounters that |
| 233 | * value, num_bytes will be parsed as 127, which will fail the check below. |
| 234 | */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | if (num_bytes == 0 || num_bytes > 4) { |
| 236 | return 0; |
| 237 | } |
| 238 | if (!cbs_get_u(&header, &len32, num_bytes)) { |
| 239 | return 0; |
| 240 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 241 | /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length |
| 242 | * with the minimum number of octets. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | if (len32 < 128) { |
| 244 | /* Length should have used short-form encoding. */ |
| 245 | return 0; |
| 246 | } |
| 247 | if ((len32 >> ((num_bytes-1)*8)) == 0) { |
| 248 | /* Length should have been at least one byte shorter. */ |
| 249 | return 0; |
| 250 | } |
| 251 | len = len32; |
| 252 | if (len + 2 + num_bytes < len) { |
| 253 | /* Overflow. */ |
| 254 | return 0; |
| 255 | } |
| 256 | len += 2 + num_bytes; |
| 257 | if (out_header_len != NULL) { |
| 258 | *out_header_len = 2 + num_bytes; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return CBS_get_bytes(cbs, out, len); |
| 263 | } |
| 264 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame^] | 265 | int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) { |
| 266 | size_t header_len; |
| 267 | if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | if (!CBS_skip(out, header_len)) { |
| 272 | assert(0); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | return 1; |
| 277 | } |
| 278 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 279 | int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 280 | size_t *out_header_len) { |
| 281 | return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, |
| 282 | 0 /* DER only */); |
| 283 | } |
| 284 | |
| 285 | int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 286 | size_t *out_header_len) { |
| 287 | return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, |
| 288 | 1 /* BER allowed */); |
| 289 | } |
| 290 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value, |
| 292 | int skip_header) { |
| 293 | size_t header_len; |
| 294 | unsigned tag; |
| 295 | CBS throwaway; |
| 296 | |
| 297 | if (out == NULL) { |
| 298 | out = &throwaway; |
| 299 | } |
| 300 | |
| 301 | if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) || |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 302 | tag != tag_value) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | if (skip_header && !CBS_skip(out, header_len)) { |
| 307 | assert(0); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) { |
| 315 | return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */); |
| 316 | } |
| 317 | |
| 318 | int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) { |
| 319 | return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */); |
| 320 | } |
| 321 | |
| 322 | int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) { |
| 323 | if (CBS_len(cbs) < 1) { |
| 324 | return 0; |
| 325 | } |
| 326 | return CBS_data(cbs)[0] == tag_value; |
| 327 | } |
| 328 | |
| 329 | int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) { |
| 330 | CBS bytes; |
| 331 | const uint8_t *data; |
| 332 | size_t i, len; |
| 333 | |
| 334 | if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) { |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | *out = 0; |
| 339 | data = CBS_data(&bytes); |
| 340 | len = CBS_len(&bytes); |
| 341 | |
| 342 | if (len == 0) { |
| 343 | /* An INTEGER is encoded with at least one octet. */ |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | if ((data[0] & 0x80) != 0) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 348 | /* Negative number. */ |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) { |
| 353 | /* Extra leading zeros. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | for (i = 0; i < len; i++) { |
| 358 | if ((*out >> 56) != 0) { |
| 359 | /* Too large to represent as a uint64_t. */ |
| 360 | return 0; |
| 361 | } |
| 362 | *out <<= 8; |
| 363 | *out |= data[i]; |
| 364 | } |
| 365 | |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 370 | int present = 0; |
| 371 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 372 | if (CBS_peek_asn1_tag(cbs, tag)) { |
| 373 | if (!CBS_get_asn1(cbs, out, tag)) { |
| 374 | return 0; |
| 375 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 376 | present = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 377 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 378 | |
| 379 | if (out_present != NULL) { |
| 380 | *out_present = present; |
| 381 | } |
| 382 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present, |
| 387 | unsigned tag) { |
| 388 | CBS child; |
| 389 | int present; |
| 390 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 391 | return 0; |
| 392 | } |
| 393 | if (present) { |
| 394 | if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) || |
| 395 | CBS_len(&child) != 0) { |
| 396 | return 0; |
| 397 | } |
| 398 | } else { |
| 399 | CBS_init(out, NULL, 0); |
| 400 | } |
| 401 | if (out_present) { |
| 402 | *out_present = present; |
| 403 | } |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag, |
| 408 | uint64_t default_value) { |
| 409 | CBS child; |
| 410 | int present; |
| 411 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 412 | return 0; |
| 413 | } |
| 414 | if (present) { |
| 415 | if (!CBS_get_asn1_uint64(&child, out) || |
| 416 | CBS_len(&child) != 0) { |
| 417 | return 0; |
| 418 | } |
| 419 | } else { |
| 420 | *out = default_value; |
| 421 | } |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag, |
| 426 | int default_value) { |
| 427 | CBS child, child2; |
| 428 | int present; |
| 429 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 430 | return 0; |
| 431 | } |
| 432 | if (present) { |
| 433 | uint8_t boolean; |
| 434 | |
| 435 | if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 436 | CBS_len(&child2) != 1 || |
| 437 | CBS_len(&child) != 0) { |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | boolean = CBS_data(&child2)[0]; |
| 442 | if (boolean == 0) { |
| 443 | *out = 0; |
| 444 | } else if (boolean == 0xff) { |
| 445 | *out = 1; |
| 446 | } else { |
| 447 | return 0; |
| 448 | } |
| 449 | } else { |
| 450 | *out = default_value; |
| 451 | } |
| 452 | return 1; |
| 453 | } |