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