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" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 23 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | void CBS_init(CBS *cbs, const uint8_t *data, size_t len) { |
| 27 | cbs->data = data; |
| 28 | cbs->len = len; |
| 29 | } |
| 30 | |
| 31 | static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) { |
| 32 | if (cbs->len < n) { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | *p = cbs->data; |
| 37 | cbs->data += n; |
| 38 | cbs->len -= n; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | int CBS_skip(CBS *cbs, size_t len) { |
| 43 | const uint8_t *dummy; |
| 44 | return cbs_get(cbs, &dummy, len); |
| 45 | } |
| 46 | |
| 47 | const uint8_t *CBS_data(const CBS *cbs) { |
| 48 | return cbs->data; |
| 49 | } |
| 50 | |
| 51 | size_t CBS_len(const CBS *cbs) { |
| 52 | return cbs->len; |
| 53 | } |
| 54 | |
| 55 | 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] | 56 | OPENSSL_free(*out_ptr); |
| 57 | *out_ptr = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 58 | *out_len = 0; |
| 59 | |
| 60 | if (cbs->len == 0) { |
| 61 | return 1; |
| 62 | } |
| 63 | *out_ptr = BUF_memdup(cbs->data, cbs->len); |
| 64 | if (*out_ptr == NULL) { |
| 65 | return 0; |
| 66 | } |
| 67 | *out_len = cbs->len; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | int CBS_strdup(const CBS *cbs, char **out_ptr) { |
| 72 | if (*out_ptr != NULL) { |
| 73 | OPENSSL_free(*out_ptr); |
| 74 | } |
| 75 | *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len); |
| 76 | return (*out_ptr != NULL); |
| 77 | } |
| 78 | |
| 79 | int CBS_contains_zero_byte(const CBS *cbs) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 80 | return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | 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] | 84 | if (len != cbs->len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 86 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 87 | return CRYPTO_memcmp(cbs->data, data, len) == 0; |
| 88 | } |
| 89 | |
| 90 | static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) { |
| 91 | uint32_t result = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 92 | const uint8_t *data; |
| 93 | |
| 94 | if (!cbs_get(cbs, &data, len)) { |
| 95 | return 0; |
| 96 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 97 | for (size_t i = 0; i < len; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 131 | int CBS_get_last_u8(CBS *cbs, uint8_t *out) { |
| 132 | if (cbs->len == 0) { |
| 133 | return 0; |
| 134 | } |
| 135 | *out = cbs->data[cbs->len - 1]; |
| 136 | cbs->len--; |
| 137 | return 1; |
| 138 | } |
| 139 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 140 | int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) { |
| 141 | const uint8_t *v; |
| 142 | if (!cbs_get(cbs, &v, len)) { |
| 143 | return 0; |
| 144 | } |
| 145 | CBS_init(out, v, len); |
| 146 | return 1; |
| 147 | } |
| 148 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 149 | int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) { |
| 150 | const uint8_t *v; |
| 151 | if (!cbs_get(cbs, &v, len)) { |
| 152 | return 0; |
| 153 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 154 | OPENSSL_memcpy(out, v, len); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 155 | return 1; |
| 156 | } |
| 157 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) { |
| 159 | uint32_t len; |
| 160 | if (!cbs_get_u(cbs, &len, len_len)) { |
| 161 | return 0; |
| 162 | } |
| 163 | return CBS_get_bytes(cbs, out, len); |
| 164 | } |
| 165 | |
| 166 | int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) { |
| 167 | return cbs_get_length_prefixed(cbs, out, 1); |
| 168 | } |
| 169 | |
| 170 | int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) { |
| 171 | return cbs_get_length_prefixed(cbs, out, 2); |
| 172 | } |
| 173 | |
| 174 | int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) { |
| 175 | return cbs_get_length_prefixed(cbs, out, 3); |
| 176 | } |
| 177 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 178 | static int parse_asn1_tag(CBS *cbs, unsigned *out) { |
| 179 | uint8_t tag_byte; |
| 180 | if (!CBS_get_u8(cbs, &tag_byte)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [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). |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 191 | unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT; |
| 192 | unsigned tag_number = tag_byte & 0x1f; |
| 193 | if (tag_number == 0x1f) { |
| 194 | tag_number = 0; |
| 195 | for (;;) { |
| 196 | if (!CBS_get_u8(cbs, &tag_byte) || |
| 197 | ((tag_number << 7) >> 7) != tag_number) { |
| 198 | return 0; |
| 199 | } |
| 200 | tag_number = (tag_number << 7) | (tag_byte & 0x7f); |
| 201 | // The tag must be represented in the minimal number of bytes. |
| 202 | if (tag_number == 0) { |
| 203 | return 0; |
| 204 | } |
| 205 | if ((tag_byte & 0x80) == 0) { |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | if (// Check the tag number is within our supported bounds. |
| 210 | tag_number > CBS_ASN1_TAG_NUMBER_MASK || |
| 211 | // Small tag numbers should have used low tag number form. |
| 212 | tag_number < 0x1f) { |
| 213 | return 0; |
| 214 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 217 | tag |= tag_number; |
| 218 | |
| 219 | *out = tag; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 224 | size_t *out_header_len, int ber_ok) { |
| 225 | CBS header = *cbs; |
| 226 | CBS throwaway; |
| 227 | |
| 228 | if (out == NULL) { |
| 229 | out = &throwaway; |
| 230 | } |
| 231 | |
| 232 | unsigned tag; |
| 233 | if (!parse_asn1_tag(&header, &tag)) { |
| 234 | return 0; |
| 235 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | if (out_tag != NULL) { |
| 237 | *out_tag = tag; |
| 238 | } |
| 239 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 240 | uint8_t length_byte; |
| 241 | if (!CBS_get_u8(&header, &length_byte)) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | size_t header_len = CBS_len(cbs) - CBS_len(&header); |
| 246 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | size_t len; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 248 | // The format for the length encoding is specified in ITU-T X.690 section |
| 249 | // 8.1.3. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 250 | if ((length_byte & 0x80) == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 251 | // Short form length. |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 252 | len = ((size_t) length_byte) + header_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | if (out_header_len != NULL) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 254 | *out_header_len = header_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | } |
| 256 | } else { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 257 | // The high bit indicate that this is the long form, while the next 7 bits |
| 258 | // encode the number of subsequent octets used to encode the length (ITU-T |
| 259 | // X.690 clause 8.1.3.5.b). |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 260 | const size_t num_bytes = length_byte & 0x7f; |
| 261 | uint32_t len32; |
| 262 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 263 | if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 264 | // indefinite length |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 265 | if (out_header_len != NULL) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 266 | *out_header_len = header_len; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 267 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 268 | return CBS_get_bytes(cbs, out, header_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 271 | // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be |
| 272 | // used as the first byte of the length. If this parser encounters that |
| 273 | // value, num_bytes will be parsed as 127, which will fail the check below. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | if (num_bytes == 0 || num_bytes > 4) { |
| 275 | return 0; |
| 276 | } |
| 277 | if (!cbs_get_u(&header, &len32, num_bytes)) { |
| 278 | return 0; |
| 279 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 280 | // ITU-T X.690 section 10.1 (DER length forms) requires encoding the length |
| 281 | // with the minimum number of octets. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 282 | if (len32 < 128) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 283 | // Length should have used short-form encoding. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | if ((len32 >> ((num_bytes-1)*8)) == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 287 | // Length should have been at least one byte shorter. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | len = len32; |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 291 | if (len + header_len + num_bytes < len) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 292 | // Overflow. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 293 | return 0; |
| 294 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 295 | len += header_len + num_bytes; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | if (out_header_len != NULL) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 297 | *out_header_len = header_len + num_bytes; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | return CBS_get_bytes(cbs, out, len); |
| 302 | } |
| 303 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 304 | int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) { |
| 305 | size_t header_len; |
| 306 | if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) { |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | if (!CBS_skip(out, header_len)) { |
| 311 | assert(0); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | return 1; |
| 316 | } |
| 317 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 318 | int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 319 | size_t *out_header_len) { |
| 320 | return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, |
| 321 | 0 /* DER only */); |
| 322 | } |
| 323 | |
| 324 | int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag, |
| 325 | size_t *out_header_len) { |
| 326 | return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, |
| 327 | 1 /* BER allowed */); |
| 328 | } |
| 329 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 330 | static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value, |
| 331 | int skip_header) { |
| 332 | size_t header_len; |
| 333 | unsigned tag; |
| 334 | CBS throwaway; |
| 335 | |
| 336 | if (out == NULL) { |
| 337 | out = &throwaway; |
| 338 | } |
| 339 | |
| 340 | if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) || |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 341 | tag != tag_value) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | if (skip_header && !CBS_skip(out, header_len)) { |
| 346 | assert(0); |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) { |
| 354 | return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */); |
| 355 | } |
| 356 | |
| 357 | int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) { |
| 358 | return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */); |
| 359 | } |
| 360 | |
| 361 | int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) { |
| 362 | if (CBS_len(cbs) < 1) { |
| 363 | return 0; |
| 364 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame^] | 365 | |
| 366 | CBS copy = *cbs; |
| 367 | unsigned actual_tag; |
| 368 | return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) { |
| 372 | CBS bytes; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 373 | if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) { |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | *out = 0; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 378 | const uint8_t *data = CBS_data(&bytes); |
| 379 | size_t len = CBS_len(&bytes); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 380 | |
| 381 | if (len == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 382 | // An INTEGER is encoded with at least one octet. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | if ((data[0] & 0x80) != 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 387 | // Negative number. |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 392 | // Extra leading zeros. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 396 | for (size_t i = 0; i < len; i++) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 397 | if ((*out >> 56) != 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 398 | // Too large to represent as a uint64_t. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | *out <<= 8; |
| 402 | *out |= data[i]; |
| 403 | } |
| 404 | |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | 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] | 409 | int present = 0; |
| 410 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 411 | if (CBS_peek_asn1_tag(cbs, tag)) { |
| 412 | if (!CBS_get_asn1(cbs, out, tag)) { |
| 413 | return 0; |
| 414 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 415 | present = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 416 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 417 | |
| 418 | if (out_present != NULL) { |
| 419 | *out_present = present; |
| 420 | } |
| 421 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present, |
| 426 | unsigned tag) { |
| 427 | CBS child; |
| 428 | int present; |
| 429 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 430 | return 0; |
| 431 | } |
| 432 | if (present) { |
| 433 | if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) || |
| 434 | CBS_len(&child) != 0) { |
| 435 | return 0; |
| 436 | } |
| 437 | } else { |
| 438 | CBS_init(out, NULL, 0); |
| 439 | } |
| 440 | if (out_present) { |
| 441 | *out_present = present; |
| 442 | } |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag, |
| 447 | uint64_t default_value) { |
| 448 | CBS child; |
| 449 | int present; |
| 450 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 451 | return 0; |
| 452 | } |
| 453 | if (present) { |
| 454 | if (!CBS_get_asn1_uint64(&child, out) || |
| 455 | CBS_len(&child) != 0) { |
| 456 | return 0; |
| 457 | } |
| 458 | } else { |
| 459 | *out = default_value; |
| 460 | } |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag, |
| 465 | int default_value) { |
| 466 | CBS child, child2; |
| 467 | int present; |
| 468 | if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) { |
| 469 | return 0; |
| 470 | } |
| 471 | if (present) { |
| 472 | uint8_t boolean; |
| 473 | |
| 474 | if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 475 | CBS_len(&child2) != 1 || |
| 476 | CBS_len(&child) != 0) { |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | boolean = CBS_data(&child2)[0]; |
| 481 | if (boolean == 0) { |
| 482 | *out = 0; |
| 483 | } else if (boolean == 0xff) { |
| 484 | *out = 1; |
| 485 | } else { |
| 486 | return 0; |
| 487 | } |
| 488 | } else { |
| 489 | *out = default_value; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 493 | |
| 494 | int CBS_is_valid_asn1_bitstring(const CBS *cbs) { |
| 495 | CBS in = *cbs; |
| 496 | uint8_t num_unused_bits; |
| 497 | if (!CBS_get_u8(&in, &num_unused_bits) || |
| 498 | num_unused_bits > 7) { |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | if (num_unused_bits == 0) { |
| 503 | return 1; |
| 504 | } |
| 505 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 506 | // All num_unused_bits bits must exist and be zeros. |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 507 | uint8_t last; |
| 508 | if (!CBS_get_last_u8(&in, &last) || |
| 509 | (last & ((1 << num_unused_bits) - 1)) != 0) { |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) { |
| 517 | if (!CBS_is_valid_asn1_bitstring(cbs)) { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | const unsigned byte_num = (bit >> 3) + 1; |
| 522 | const unsigned bit_num = 7 - (bit & 7); |
| 523 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 524 | // Unused bits are zero, and this function does not distinguish between |
| 525 | // missing and unset bits. Thus it is sufficient to do a byte-level length |
| 526 | // check. |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 527 | return byte_num < CBS_len(cbs) && |
| 528 | (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0; |
| 529 | } |