| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/asn1.h> |
| 16 | #include <openssl/bytestring.h> |
| 17 | #include <openssl/mem.h> |
| 18 |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <string.h> |
| 23 |
| 24 | #include "../asn1/internal.h" |
| 25 | #include "../internal.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) { |
| 30 | if (cbs->len < n) { |
| 31 | return 0; |
| 32 | } |
| 33 |
| 34 | *p = cbs->data; |
| 35 | cbs->data += n; |
| 36 | cbs->len -= n; |
| 37 | return 1; |
| 38 | } |
| 39 |
| 40 | int CBS_skip(CBS *cbs, size_t len) { |
| 41 | const uint8_t *dummy; |
| 42 | return cbs_get(cbs, &dummy, len); |
| 43 | } |
| 44 |
| 45 | int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) { |
| 46 | OPENSSL_free(*out_ptr); |
| 47 | *out_ptr = NULL; |
| 48 | *out_len = 0; |
| 49 |
| 50 | if (cbs->len == 0) { |
| 51 | return 1; |
| 52 | } |
| 53 | *out_ptr = reinterpret_cast<uint8_t *>(OPENSSL_memdup(cbs->data, cbs->len)); |
| 54 | if (*out_ptr == NULL) { |
| 55 | return 0; |
| 56 | } |
| 57 | *out_len = cbs->len; |
| 58 | return 1; |
| 59 | } |
| 60 |
| 61 | int CBS_strdup(const CBS *cbs, char **out_ptr) { |
| 62 | if (*out_ptr != NULL) { |
| 63 | OPENSSL_free(*out_ptr); |
| 64 | } |
| 65 | *out_ptr = OPENSSL_strndup((const char *)cbs->data, cbs->len); |
| 66 | return (*out_ptr != NULL); |
| 67 | } |
| 68 |
| 69 | int CBS_contains_zero_byte(const CBS *cbs) { |
| 70 | return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL; |
| 71 | } |
| 72 |
| 73 | int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) { |
| 74 | if (len != cbs->len) { |
| 75 | return 0; |
| 76 | } |
| 77 | return CRYPTO_memcmp(cbs->data, data, len) == 0; |
| 78 | } |
| 79 |
| 80 | static int cbs_get_u(CBS *cbs, uint64_t *out, size_t len) { |
| 81 | uint64_t result = 0; |
| 82 | const uint8_t *data; |
| 83 |
| 84 | if (!cbs_get(cbs, &data, len)) { |
| 85 | return 0; |
| 86 | } |
| 87 | for (size_t i = 0; i < len; i++) { |
| 88 | result <<= 8; |
| 89 | result |= data[i]; |
| 90 | } |
| 91 | *out = result; |
| 92 | return 1; |
| 93 | } |
| 94 |
| 95 | int CBS_get_u8(CBS *cbs, uint8_t *out) { |
| 96 | const uint8_t *v; |
| 97 | if (!cbs_get(cbs, &v, 1)) { |
| 98 | return 0; |
| 99 | } |
| 100 | *out = *v; |
| 101 | return 1; |
| 102 | } |
| 103 |
| 104 | int CBS_get_u16(CBS *cbs, uint16_t *out) { |
| 105 | uint64_t v; |
| 106 | if (!cbs_get_u(cbs, &v, 2)) { |
| 107 | return 0; |
| 108 | } |
| 109 | *out = v; |
| 110 | return 1; |
| 111 | } |
| 112 |
| 113 | int CBS_get_u16le(CBS *cbs, uint16_t *out) { |
| 114 | if (!CBS_get_u16(cbs, out)) { |
| 115 | return 0; |
| 116 | } |
| 117 | *out = CRYPTO_bswap2(*out); |
| 118 | return 1; |
| 119 | } |
| 120 |
| 121 | int CBS_get_u24(CBS *cbs, uint32_t *out) { |
| 122 | uint64_t v; |
| 123 | if (!cbs_get_u(cbs, &v, 3)) { |
| 124 | return 0; |
| 125 | } |
| 126 | *out = (uint32_t)v; |
| 127 | return 1; |
| 128 | } |
| 129 |
| 130 | int CBS_get_u32(CBS *cbs, uint32_t *out) { |
| 131 | uint64_t v; |
| 132 | if (!cbs_get_u(cbs, &v, 4)) { |
| 133 | return 0; |
| 134 | } |
| 135 | *out = (uint32_t)v; |
| 136 | return 1; |
| 137 | } |
| 138 |
| 139 | int CBS_get_u32le(CBS *cbs, uint32_t *out) { |
| 140 | if (!CBS_get_u32(cbs, out)) { |
| 141 | return 0; |
| 142 | } |
| 143 | *out = CRYPTO_bswap4(*out); |
| 144 | return 1; |
| 145 | } |
| 146 |
| 147 | int CBS_get_u64(CBS *cbs, uint64_t *out) { return cbs_get_u(cbs, out, 8); } |
| 148 |
| 149 | int CBS_get_u64le(CBS *cbs, uint64_t *out) { |
| 150 | if (!cbs_get_u(cbs, out, 8)) { |
| 151 | return 0; |
| 152 | } |
| 153 | *out = CRYPTO_bswap8(*out); |
| 154 | return 1; |
| 155 | } |
| 156 |
| 157 | int CBS_get_last_u8(CBS *cbs, uint8_t *out) { |
| 158 | if (cbs->len == 0) { |
| 159 | return 0; |
| 160 | } |
| 161 | *out = cbs->data[cbs->len - 1]; |
| 162 | cbs->len--; |
| 163 | return 1; |
| 164 | } |
| 165 |
| 166 | int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) { |
| 167 | const uint8_t *v; |
| 168 | if (!cbs_get(cbs, &v, len)) { |
| 169 | return 0; |
| 170 | } |
| 171 | CBS_init(out, v, len); |
| 172 | return 1; |
| 173 | } |
| 174 |
| 175 | int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) { |
| 176 | const uint8_t *v; |
| 177 | if (!cbs_get(cbs, &v, len)) { |
| 178 | return 0; |
| 179 | } |
| 180 | OPENSSL_memcpy(out, v, len); |
| 181 | return 1; |
| 182 | } |
| 183 |
| 184 | static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) { |
| 185 | uint64_t len; |
| 186 | if (!cbs_get_u(cbs, &len, len_len)) { |
| 187 | return 0; |
| 188 | } |
| 189 | // If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on |
| 190 | // 32-bit systems. |
| 191 | assert(len_len <= 3); |
| 192 | return CBS_get_bytes(cbs, out, len); |
| 193 | } |
| 194 |
| 195 | int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) { |
| 196 | return cbs_get_length_prefixed(cbs, out, 1); |
| 197 | } |
| 198 |
| 199 | int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) { |
| 200 | return cbs_get_length_prefixed(cbs, out, 2); |
| 201 | } |
| 202 |
| 203 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) { |
| 204 | return cbs_get_length_prefixed(cbs, out, 3); |
| 205 | } |
| 206 |
| 207 | int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) { |
| 208 | const uint8_t *split = reinterpret_cast<const uint8_t *>( |
| 209 | OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs))); |
| 210 | if (split == NULL) { |
| 211 | return 0; |
| 212 | } |
| 213 | return CBS_get_bytes(cbs, out, split - CBS_data(cbs)); |
| 214 | } |
| 215 |
| 216 | int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) { |
| 217 | uint64_t v = 0; |
| 218 | int seen_digit = 0; |
| 219 | while (CBS_len(cbs) != 0) { |
| 220 | uint8_t c = CBS_data(cbs)[0]; |
| 221 | if (!OPENSSL_isdigit(c)) { |
| 222 | break; |
| 223 | } |
| 224 | CBS_skip(cbs, 1); |
| 225 | if (/* Forbid stray leading zeros */ |
| 226 | (v == 0 && seen_digit) || |
| 227 | // Check for overflow. |
| 228 | v > UINT64_MAX / 10 || // |
| 229 | v * 10 > UINT64_MAX - (c - '0')) { |
| 230 | return 0; |
| 231 | } |
| 232 | v = v * 10 + (c - '0'); |
| 233 | seen_digit = 1; |
| 234 | } |
| 235 |
| 236 | *out = v; |
| 237 | return seen_digit; |
| 238 | } |
| 239 |
| 240 | // parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets |
| 241 | // |*out| to the result. This is the encoding used in DER for both high tag |
| 242 | // number form and OID components. |
| 243 | static int parse_base128_integer(CBS *cbs, uint64_t *out) { |
| 244 | uint64_t v = 0; |
| 245 | uint8_t b; |
| 246 | do { |
| 247 | if (!CBS_get_u8(cbs, &b)) { |
| 248 | return 0; |
| 249 | } |
| 250 | if ((v >> (64 - 7)) != 0) { |
| 251 | // The value is too large. |
| 252 | return 0; |
| 253 | } |
| 254 | if (v == 0 && b == 0x80) { |
| 255 | // The value must be minimally encoded. |
| 256 | return 0; |
| 257 | } |
| 258 | v = (v << 7) | (b & 0x7f); |
| 259 |
| 260 | // Values end at an octet with the high bit cleared. |
| 261 | } while (b & 0x80); |
| 262 |
| 263 | *out = v; |
| 264 | return 1; |
| 265 | } |
| 266 |
| 267 | static int parse_asn1_tag(CBS *cbs, CBS_ASN1_TAG *out) { |
| 268 | uint8_t tag_byte; |
| 269 | if (!CBS_get_u8(cbs, &tag_byte)) { |
| 270 | return 0; |
| 271 | } |
| 272 |
| 273 | // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag |
| 274 | // number no greater than 30. |
| 275 | // |
| 276 | // If the number portion is 31 (0x1f, the largest value that fits in the |
| 277 | // allotted bits), then the tag is more than one byte long and the |
| 278 | // continuation bytes contain the tag number. |
| 279 | CBS_ASN1_TAG tag = ((CBS_ASN1_TAG)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT; |
| 280 | CBS_ASN1_TAG tag_number = tag_byte & 0x1f; |
| 281 | if (tag_number == 0x1f) { |
| 282 | uint64_t v; |
| 283 | if (!parse_base128_integer(cbs, &v) || |
| 284 | // Check the tag number is within our supported bounds. |
| 285 | v > CBS_ASN1_TAG_NUMBER_MASK || |
| 286 | // Small tag numbers should have used low tag number form, even in BER. |
| 287 | v < 0x1f) { |
| 288 | return 0; |
| 289 | } |
| 290 | tag_number = (CBS_ASN1_TAG)v; |
| 291 | } |
| 292 |
| 293 | tag |= tag_number; |
| 294 |
| 295 | // Tag [UNIVERSAL 0] is reserved for use by the encoding. Reject it here to |
| 296 | // avoid some ambiguity around ANY values and BER indefinite-length EOCs. See |
| 297 | // https://crbug.com/boringssl/455. |
| 298 | if ((tag & ~CBS_ASN1_CONSTRUCTED) == 0) { |
| 299 | return 0; |
| 300 | } |
| 301 |
| 302 | *out = tag; |
| 303 | return 1; |
| 304 | } |
| 305 |
| 306 | static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, |
| 307 | size_t *out_header_len, int *out_ber_found, |
| 308 | int *out_indefinite, int ber_ok) { |
| 309 | CBS header = *cbs; |
| 310 | CBS throwaway; |
| 311 |
| 312 | if (out == NULL) { |
| 313 | out = &throwaway; |
| 314 | } |
| 315 | if (ber_ok) { |
| 316 | *out_ber_found = 0; |
| 317 | *out_indefinite = 0; |
| 318 | } else { |
| 319 | assert(out_ber_found == NULL); |
| 320 | assert(out_indefinite == NULL); |
| 321 | } |
| 322 |
| 323 | CBS_ASN1_TAG tag; |
| 324 | if (!parse_asn1_tag(&header, &tag)) { |
| 325 | return 0; |
| 326 | } |
| 327 | if (out_tag != NULL) { |
| 328 | *out_tag = tag; |
| 329 | } |
| 330 |
| 331 | uint8_t length_byte; |
| 332 | if (!CBS_get_u8(&header, &length_byte)) { |
| 333 | return 0; |
| 334 | } |
| 335 |
| 336 | size_t header_len = CBS_len(cbs) - CBS_len(&header); |
| 337 |
| 338 | size_t len; |
| 339 | // The format for the length encoding is specified in ITU-T X.690 section |
| 340 | // 8.1.3. |
| 341 | if ((length_byte & 0x80) == 0) { |
| 342 | // Short form length. |
| 343 | len = ((size_t)length_byte) + header_len; |
| 344 | if (out_header_len != NULL) { |
| 345 | *out_header_len = header_len; |
| 346 | } |
| 347 | } else { |
| 348 | // The high bit indicate that this is the long form, while the next 7 bits |
| 349 | // encode the number of subsequent octets used to encode the length (ITU-T |
| 350 | // X.690 clause 8.1.3.5.b). |
| 351 | const size_t num_bytes = length_byte & 0x7f; |
| 352 | uint64_t len64; |
| 353 |
| 354 | if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) { |
| 355 | // indefinite length |
| 356 | if (out_header_len != NULL) { |
| 357 | *out_header_len = header_len; |
| 358 | } |
| 359 | *out_ber_found = 1; |
| 360 | *out_indefinite = 1; |
| 361 | return CBS_get_bytes(cbs, out, header_len); |
| 362 | } |
| 363 |
| 364 | // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be |
| 365 | // used as the first byte of the length. If this parser encounters that |
| 366 | // value, num_bytes will be parsed as 127, which will fail this check. |
| 367 | if (num_bytes == 0 || num_bytes > 4) { |
| 368 | return 0; |
| 369 | } |
| 370 | if (!cbs_get_u(&header, &len64, num_bytes)) { |
| 371 | return 0; |
| 372 | } |
| 373 | // ITU-T X.690 section 10.1 (DER length forms) requires encoding the |
| 374 | // length with the minimum number of octets. BER could, technically, have |
| 375 | // 125 superfluous zero bytes. We do not attempt to handle that and still |
| 376 | // require that the length fit in a |uint32_t| for BER. |
| 377 | if (len64 < 128) { |
| 378 | // Length should have used short-form encoding. |
| 379 | if (ber_ok) { |
| 380 | *out_ber_found = 1; |
| 381 | } else { |
| 382 | return 0; |
| 383 | } |
| 384 | } |
| 385 | if ((len64 >> ((num_bytes - 1) * 8)) == 0) { |
| 386 | // Length should have been at least one byte shorter. |
| 387 | if (ber_ok) { |
| 388 | *out_ber_found = 1; |
| 389 | } else { |
| 390 | return 0; |
| 391 | } |
| 392 | } |
| 393 | len = len64; |
| 394 | if (len + header_len + num_bytes < len) { |
| 395 | // Overflow. |
| 396 | return 0; |
| 397 | } |
| 398 | len += header_len + num_bytes; |
| 399 | if (out_header_len != NULL) { |
| 400 | *out_header_len = header_len + num_bytes; |
| 401 | } |
| 402 | } |
| 403 |
| 404 | return CBS_get_bytes(cbs, out, len); |
| 405 | } |
| 406 |
| 407 | int CBS_get_any_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag) { |
| 408 | size_t header_len; |
| 409 | if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) { |
| 410 | return 0; |
| 411 | } |
| 412 |
| 413 | if (out && !CBS_skip(out, header_len)) { |
| 414 | assert(0); |
| 415 | return 0; |
| 416 | } |
| 417 |
| 418 | return 1; |
| 419 | } |
| 420 |
| 421 | int CBS_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, |
| 422 | size_t *out_header_len) { |
| 423 | return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, NULL, NULL, |
| 424 | /*ber_ok=*/0); |
| 425 | } |
| 426 |
| 427 | int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag, |
| 428 | size_t *out_header_len, int *out_ber_found, |
| 429 | int *out_indefinite) { |
| 430 | int ber_found_temp; |
| 431 | return cbs_get_any_asn1_element( |
| 432 | cbs, out, out_tag, out_header_len, |
| 433 | out_ber_found ? out_ber_found : &ber_found_temp, out_indefinite, |
| 434 | /*ber_ok=*/1); |
| 435 | } |
| 436 |
| 437 | static int cbs_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value, |
| 438 | int skip_header) { |
| 439 | size_t header_len; |
| 440 | CBS_ASN1_TAG tag; |
| 441 | CBS throwaway; |
| 442 |
| 443 | if (out == NULL) { |
| 444 | out = &throwaway; |
| 445 | } |
| 446 |
| 447 | if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) || |
| 448 | tag != tag_value) { |
| 449 | return 0; |
| 450 | } |
| 451 |
| 452 | if (skip_header && !CBS_skip(out, header_len)) { |
| 453 | assert(0); |
| 454 | return 0; |
| 455 | } |
| 456 |
| 457 | return 1; |
| 458 | } |
| 459 |
| 460 | int CBS_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) { |
| 461 | return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */); |
| 462 | } |
| 463 |
| 464 | int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) { |
| 465 | return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */); |
| 466 | } |
| 467 |
| 468 | int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) { |
| 469 | CBS copy = *cbs; |
| 470 | CBS_ASN1_TAG actual_tag; |
| 471 | return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag; |
| 472 | } |
| 473 |
| 474 | int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) { |
| 475 | return CBS_get_asn1_uint64_with_tag(cbs, out, CBS_ASN1_INTEGER); |
| 476 | } |
| 477 |
| 478 | int CBS_get_asn1_uint64_with_tag(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag) { |
| 479 | CBS bytes; |
| 480 | if (!CBS_get_asn1(cbs, &bytes, tag) || |
| 481 | !CBS_is_unsigned_asn1_integer(&bytes)) { |
| 482 | return 0; |
| 483 | } |
| 484 |
| 485 | *out = 0; |
| 486 | const uint8_t *data = CBS_data(&bytes); |
| 487 | size_t len = CBS_len(&bytes); |
| 488 | for (size_t i = 0; i < len; i++) { |
| 489 | if ((*out >> 56) != 0) { |
| 490 | // Too large to represent as a uint64_t. |
| 491 | return 0; |
| 492 | } |
| 493 | *out <<= 8; |
| 494 | *out |= data[i]; |
| 495 | } |
| 496 |
| 497 | return 1; |
| 498 | } |
| 499 |
| 500 | int CBS_get_asn1_int64(CBS *cbs, int64_t *out) { |
| 501 | return CBS_get_asn1_int64_with_tag(cbs, out, CBS_ASN1_INTEGER); |
| 502 | } |
| 503 |
| 504 | int CBS_get_asn1_int64_with_tag(CBS *cbs, int64_t *out, CBS_ASN1_TAG tag) { |
| 505 | int is_negative; |
| 506 | CBS bytes; |
| 507 | if (!CBS_get_asn1(cbs, &bytes, tag) || |
| 508 | !CBS_is_valid_asn1_integer(&bytes, &is_negative)) { |
| 509 | return 0; |
| 510 | } |
| 511 | const uint8_t *data = CBS_data(&bytes); |
| 512 | const size_t len = CBS_len(&bytes); |
| 513 | if (len > sizeof(int64_t)) { |
| 514 | return 0; |
| 515 | } |
| 516 | uint8_t sign_extend[sizeof(int64_t)]; |
| 517 | OPENSSL_memset(sign_extend, is_negative ? 0xff : 0, sizeof(sign_extend)); |
| 518 | OPENSSL_memcpy(sign_extend + sizeof(int64_t) - len, data, len); |
| 519 | *out = CRYPTO_load_u64_be(sign_extend); |
| 520 | return 1; |
| 521 | } |
| 522 |
| 523 | int CBS_get_asn1_bool(CBS *cbs, int *out) { |
| 524 | CBS bytes; |
| 525 | if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) || CBS_len(&bytes) != 1) { |
| 526 | return 0; |
| 527 | } |
| 528 |
| 529 | const uint8_t value = *CBS_data(&bytes); |
| 530 | if (value != 0 && value != 0xff) { |
| 531 | return 0; |
| 532 | } |
| 533 |
| 534 | *out = !!value; |
| 535 | return 1; |
| 536 | } |
| 537 |
| 538 | int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, |
| 539 | CBS_ASN1_TAG tag) { |
| 540 | int present = 0; |
| 541 |
| 542 | if (CBS_peek_asn1_tag(cbs, tag)) { |
| 543 | if (!CBS_get_asn1(cbs, out, tag)) { |
| 544 | return 0; |
| 545 | } |
| 546 | present = 1; |
| 547 | } |
| 548 |
| 549 | if (out_present != NULL) { |
| 550 | *out_present = present; |
| 551 | } |
| 552 |
| 553 | return 1; |
| 554 | } |
| 555 |
| 556 | int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present, |
| 557 | CBS_ASN1_TAG tag) { |
| 558 | CBS child; |
| 559 | int present; |
| 560 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 561 | return 0; |
| 562 | } |
| 563 | if (present) { |
| 564 | assert(out); |
| 565 | if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) || |
| 566 | CBS_len(&child) != 0) { |
| 567 | return 0; |
| 568 | } |
| 569 | } else { |
| 570 | CBS_init(out, NULL, 0); |
| 571 | } |
| 572 | if (out_present) { |
| 573 | *out_present = present; |
| 574 | } |
| 575 | return 1; |
| 576 | } |
| 577 |
| 578 | int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag, |
| 579 | uint64_t default_value) { |
| 580 | CBS child; |
| 581 | int present; |
| 582 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 583 | return 0; |
| 584 | } |
| 585 | if (present) { |
| 586 | if (!CBS_get_asn1_uint64(&child, out) || CBS_len(&child) != 0) { |
| 587 | return 0; |
| 588 | } |
| 589 | } else { |
| 590 | *out = default_value; |
| 591 | } |
| 592 | return 1; |
| 593 | } |
| 594 |
| 595 | int CBS_get_optional_asn1_bool(CBS *cbs, int *out, CBS_ASN1_TAG tag, |
| 596 | int default_value) { |
| 597 | CBS child, child2; |
| 598 | int present; |
| 599 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 600 | return 0; |
| 601 | } |
| 602 | if (present) { |
| 603 | uint8_t boolean; |
| 604 |
| 605 | if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 606 | CBS_len(&child2) != 1 || CBS_len(&child) != 0) { |
| 607 | return 0; |
| 608 | } |
| 609 |
| 610 | boolean = CBS_data(&child2)[0]; |
| 611 | if (boolean == 0) { |
| 612 | *out = 0; |
| 613 | } else if (boolean == 0xff) { |
| 614 | *out = 1; |
| 615 | } else { |
| 616 | return 0; |
| 617 | } |
| 618 | } else { |
| 619 | *out = default_value; |
| 620 | } |
| 621 | return 1; |
| 622 | } |
| 623 |
| 624 | int CBS_is_valid_asn1_bitstring(const CBS *cbs) { |
| 625 | CBS in = *cbs; |
| 626 | uint8_t num_unused_bits; |
| 627 | if (!CBS_get_u8(&in, &num_unused_bits) || num_unused_bits > 7) { |
| 628 | return 0; |
| 629 | } |
| 630 |
| 631 | if (num_unused_bits == 0) { |
| 632 | return 1; |
| 633 | } |
| 634 |
| 635 | // All num_unused_bits bits must exist and be zeros. |
| 636 | uint8_t last; |
| 637 | if (!CBS_get_last_u8(&in, &last) || |
| 638 | (last & ((1 << num_unused_bits) - 1)) != 0) { |
| 639 | return 0; |
| 640 | } |
| 641 |
| 642 | return 1; |
| 643 | } |
| 644 |
| 645 | int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) { |
| 646 | if (!CBS_is_valid_asn1_bitstring(cbs)) { |
| 647 | return 0; |
| 648 | } |
| 649 |
| 650 | const unsigned byte_num = (bit >> 3) + 1; |
| 651 | const unsigned bit_num = 7 - (bit & 7); |
| 652 |
| 653 | // Unused bits are zero, and this function does not distinguish between |
| 654 | // missing and unset bits. Thus it is sufficient to do a byte-level length |
| 655 | // check. |
| 656 | return byte_num < CBS_len(cbs) && |
| 657 | (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0; |
| 658 | } |
| 659 |
| 660 | int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) { |
| 661 | CBS copy = *cbs; |
| 662 | uint8_t first_byte, second_byte; |
| 663 | if (!CBS_get_u8(©, &first_byte)) { |
| 664 | return 0; // INTEGERs may not be empty. |
| 665 | } |
| 666 | if (out_is_negative != NULL) { |
| 667 | *out_is_negative = (first_byte & 0x80) != 0; |
| 668 | } |
| 669 | if (!CBS_get_u8(©, &second_byte)) { |
| 670 | return 1; // One byte INTEGERs are always minimal. |
| 671 | } |
| 672 | if ((first_byte == 0x00 && (second_byte & 0x80) == 0) || |
| 673 | (first_byte == 0xff && (second_byte & 0x80) != 0)) { |
| 674 | return 0; // The value is minimal iff the first 9 bits are not all equal. |
| 675 | } |
| 676 | return 1; |
| 677 | } |
| 678 |
| 679 | int CBS_is_unsigned_asn1_integer(const CBS *cbs) { |
| 680 | int is_negative; |
| 681 | return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative; |
| 682 | } |
| 683 |
| 684 | static int add_decimal(CBB *out, uint64_t v) { |
| 685 | char buf[DECIMAL_SIZE(uint64_t) + 1]; |
| 686 | snprintf(buf, sizeof(buf), "%" PRIu64, v); |
| 687 | return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf)); |
| 688 | } |
| 689 |
| 690 | int CBS_is_valid_asn1_oid(const CBS *cbs) { |
| 691 | if (CBS_len(cbs) == 0) { |
| 692 | return 0; // OID encodings cannot be empty. |
| 693 | } |
| 694 |
| 695 | CBS copy = *cbs; |
| 696 | uint8_t v, prev = 0; |
| 697 | while (CBS_get_u8(©, &v)) { |
| 698 | // OID encodings are a sequence of minimally-encoded base-128 integers (see |
| 699 | // |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte |
| 700 | // of an integer (or |v| is the first byte). |v| is then the first byte of |
| 701 | // the next integer. If first byte of an integer is 0x80, it is not |
| 702 | // minimally-encoded. |
| 703 | if ((prev & 0x80) == 0 && v == 0x80) { |
| 704 | return 0; |
| 705 | } |
| 706 | prev = v; |
| 707 | } |
| 708 |
| 709 | // The last byte should must end an integer encoding. |
| 710 | return (prev & 0x80) == 0; |
| 711 | } |
| 712 |
| 713 | char *CBS_asn1_oid_to_text(const CBS *cbs) { |
| 714 | CBS copy = *cbs; |
| 715 | CBB cbb; |
| 716 | if (!CBB_init(&cbb, 32)) { |
| 717 | goto err; |
| 718 | } |
| 719 |
| 720 | // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2. |
| 721 | uint64_t v; |
| 722 | if (!parse_base128_integer(©, &v)) { |
| 723 | goto err; |
| 724 | } |
| 725 |
| 726 | if (v >= 80) { |
| 727 | if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) || |
| 728 | !add_decimal(&cbb, v - 80)) { |
| 729 | goto err; |
| 730 | } |
| 731 | } else if (!add_decimal(&cbb, v / 40) || !CBB_add_u8(&cbb, '.') || |
| 732 | !add_decimal(&cbb, v % 40)) { |
| 733 | goto err; |
| 734 | } |
| 735 |
| 736 | while (CBS_len(©) != 0) { |
| 737 | if (!parse_base128_integer(©, &v) || !CBB_add_u8(&cbb, '.') || |
| 738 | !add_decimal(&cbb, v)) { |
| 739 | goto err; |
| 740 | } |
| 741 | } |
| 742 |
| 743 | uint8_t *txt; |
| 744 | size_t txt_len; |
| 745 | if (!CBB_add_u8(&cbb, '\0') || !CBB_finish(&cbb, &txt, &txt_len)) { |
| 746 | goto err; |
| 747 | } |
| 748 |
| 749 | return (char *)txt; |
| 750 |
| 751 | err: |
| 752 | CBB_cleanup(&cbb); |
| 753 | return NULL; |
| 754 | } |
| 755 |
| 756 | static int cbs_get_two_digits(CBS *cbs, int *out) { |
| 757 | uint8_t first_digit, second_digit; |
| 758 | if (!CBS_get_u8(cbs, &first_digit)) { |
| 759 | return 0; |
| 760 | } |
| 761 | if (!OPENSSL_isdigit(first_digit)) { |
| 762 | return 0; |
| 763 | } |
| 764 | if (!CBS_get_u8(cbs, &second_digit)) { |
| 765 | return 0; |
| 766 | } |
| 767 | if (!OPENSSL_isdigit(second_digit)) { |
| 768 | return 0; |
| 769 | } |
| 770 | *out = (first_digit - '0') * 10 + (second_digit - '0'); |
| 771 | return 1; |
| 772 | } |
| 773 |
| 774 | static int is_valid_day(int year, int month, int day) { |
| 775 | if (day < 1) { |
| 776 | return 0; |
| 777 | } |
| 778 | switch (month) { |
| 779 | case 1: |
| 780 | case 3: |
| 781 | case 5: |
| 782 | case 7: |
| 783 | case 8: |
| 784 | case 10: |
| 785 | case 12: |
| 786 | return day <= 31; |
| 787 | case 4: |
| 788 | case 6: |
| 789 | case 9: |
| 790 | case 11: |
| 791 | return day <= 30; |
| 792 | case 2: |
| 793 | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { |
| 794 | return day <= 29; |
| 795 | } else { |
| 796 | return day <= 28; |
| 797 | } |
| 798 | default: |
| 799 | return 0; |
| 800 | } |
| 801 | } |
| 802 |
| 803 | static int CBS_parse_rfc5280_time_internal(const CBS *cbs, int is_gentime, |
| 804 | int allow_timezone_offset, |
| 805 | struct tm *out_tm) { |
| 806 | int year, month, day, hour, min, sec, tmp; |
| 807 | CBS copy = *cbs; |
| 808 | uint8_t tz; |
| 809 |
| 810 | if (is_gentime) { |
| 811 | if (!cbs_get_two_digits(©, &tmp)) { |
| 812 | return 0; |
| 813 | } |
| 814 | year = tmp * 100; |
| 815 | if (!cbs_get_two_digits(©, &tmp)) { |
| 816 | return 0; |
| 817 | } |
| 818 | year += tmp; |
| 819 | } else { |
| 820 | year = 1900; |
| 821 | if (!cbs_get_two_digits(©, &tmp)) { |
| 822 | return 0; |
| 823 | } |
| 824 | year += tmp; |
| 825 | if (year < 1950) { |
| 826 | year += 100; |
| 827 | } |
| 828 | if (year >= 2050) { |
| 829 | return 0; // A Generalized time must be used. |
| 830 | } |
| 831 | } |
| 832 | if (!cbs_get_two_digits(©, &month) || month < 1 || |
| 833 | month > 12 || // Reject invalid months. |
| 834 | !cbs_get_two_digits(©, &day) || |
| 835 | !is_valid_day(year, month, day) || // Reject invalid days. |
| 836 | !cbs_get_two_digits(©, &hour) || |
| 837 | hour > 23 || // Reject invalid hours. |
| 838 | !cbs_get_two_digits(©, &min) || |
| 839 | min > 59 || // Reject invalid minutes. |
| 840 | !cbs_get_two_digits(©, &sec) || sec > 59 || !CBS_get_u8(©, &tz)) { |
| 841 | return 0; |
| 842 | } |
| 843 |
| 844 | int offset_sign = 0; |
| 845 | switch (tz) { |
| 846 | case 'Z': |
| 847 | break; // We correctly have 'Z' on the end as per spec. |
| 848 | case '+': |
| 849 | offset_sign = 1; |
| 850 | break; // Should not be allowed per RFC 5280. |
| 851 | case '-': |
| 852 | offset_sign = -1; |
| 853 | break; // Should not be allowed per RFC 5280. |
| 854 | default: |
| 855 | return 0; // Reject anything else after the time. |
| 856 | } |
| 857 |
| 858 | // If allow_timezone_offset is non-zero, allow for a four digit timezone |
| 859 | // offset to be specified even though this is not allowed by RFC 5280. We are |
| 860 | // permissive of this for UTCTimes due to the unfortunate existence of |
| 861 | // artisinally rolled long lived certificates that were baked into places that |
| 862 | // are now difficult to change. These certificates were generated with the |
| 863 | // 'openssl' command that permissively allowed the creation of certificates |
| 864 | // with notBefore and notAfter times specified as strings for direct |
| 865 | // certificate inclusion on the command line. For context see cl/237068815. |
| 866 | // |
| 867 | // TODO(bbe): This has been expunged from public web-pki as the ecosystem has |
| 868 | // managed to encourage CA compliance with standards. We should find a way to |
| 869 | // get rid of this or make it off by default. |
| 870 | int offset_seconds = 0; |
| 871 | if (offset_sign != 0) { |
| 872 | if (!allow_timezone_offset) { |
| 873 | return 0; |
| 874 | } |
| 875 | int offset_hours, offset_minutes; |
| 876 | if (!cbs_get_two_digits(©, &offset_hours) || |
| 877 | offset_hours > 23 || // Reject invalid hours. |
| 878 | !cbs_get_two_digits(©, &offset_minutes) || |
| 879 | offset_minutes > 59) { // Reject invalid minutes. |
| 880 | return 0; |
| 881 | } |
| 882 | offset_seconds = offset_sign * (offset_hours * 3600 + offset_minutes * 60); |
| 883 | } |
| 884 |
| 885 | if (CBS_len(©) != 0) { |
| 886 | return 0; // Reject invalid lengths. |
| 887 | } |
| 888 |
| 889 | if (out_tm != NULL) { |
| 890 | // Fill in the tm fields corresponding to what we validated. |
| 891 | out_tm->tm_year = year - 1900; |
| 892 | out_tm->tm_mon = month - 1; |
| 893 | out_tm->tm_mday = day; |
| 894 | out_tm->tm_hour = hour; |
| 895 | out_tm->tm_min = min; |
| 896 | out_tm->tm_sec = sec; |
| 897 | if (offset_seconds && !OPENSSL_gmtime_adj(out_tm, 0, offset_seconds)) { |
| 898 | return 0; |
| 899 | } |
| 900 | } |
| 901 | return 1; |
| 902 | } |
| 903 |
| 904 | int CBS_parse_generalized_time(const CBS *cbs, struct tm *out_tm, |
| 905 | int allow_timezone_offset) { |
| 906 | return CBS_parse_rfc5280_time_internal(cbs, 1, allow_timezone_offset, out_tm); |
| 907 | } |
| 908 |
| 909 | int CBS_parse_utc_time(const CBS *cbs, struct tm *out_tm, |
| 910 | int allow_timezone_offset) { |
| 911 | return CBS_parse_rfc5280_time_internal(cbs, 0, allow_timezone_offset, out_tm); |
| 912 | } |
| 913 |