| 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/asn1t.h> |
| 17 | #include <openssl/bytestring.h> |
| 18 | #include <openssl/err.h> |
| 19 | #include <openssl/mem.h> |
| 20 | #include <openssl/pool.h> |
| 21 |
| 22 | #include <assert.h> |
| 23 | #include <limits.h> |
| 24 | #include <string.h> |
| 25 |
| 26 | #include "../bytestring/internal.h" |
| 27 | #include "../internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 | // Constructed types with a recursive definition (such as can be found in PKCS7) |
| 31 | // could eventually exceed the stack given malicious input with excessive |
| 32 | // recursion. Therefore we limit the stack depth. This is the maximum number of |
| 33 | // recursive invocations of asn1_item_embed_d2i(). |
| 34 | #define ASN1_MAX_CONSTRUCTED_NEST 30 |
| 35 |
| 36 | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
| 37 | char *cst, const unsigned char **in, long len, |
| 38 | int exptag, int expclass, char opt); |
| 39 |
| 40 | static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
| 41 | long len, const ASN1_TEMPLATE *tt, char opt, |
| 42 | CRYPTO_BUFFER *buf, int depth); |
| 43 | static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, |
| 44 | long len, const ASN1_TEMPLATE *tt, char opt, |
| 45 | CRYPTO_BUFFER *buf, int depth); |
| 46 | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len, |
| 47 | int utype, const ASN1_ITEM *it); |
| 48 | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, |
| 49 | long len, const ASN1_ITEM *it, int tag, |
| 50 | int aclass, char opt); |
| 51 | static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
| 52 | long len, const ASN1_ITEM *it, int tag, int aclass, |
| 53 | char opt, CRYPTO_BUFFER *buf, int depth); |
| 54 |
| 55 | // Table to convert tags to bit values, used for MSTRING type |
| 56 | static const unsigned long tag2bit[31] = { |
| 57 | 0, // (reserved) |
| 58 | 0, // BOOLEAN |
| 59 | 0, // INTEGER |
| 60 | B_ASN1_BIT_STRING, |
| 61 | B_ASN1_OCTET_STRING, |
| 62 | 0, // NULL |
| 63 | 0, // OBJECT IDENTIFIER |
| 64 | B_ASN1_UNKNOWN, // ObjectDescriptor |
| 65 | B_ASN1_UNKNOWN, // EXTERNAL |
| 66 | B_ASN1_UNKNOWN, // REAL |
| 67 | B_ASN1_UNKNOWN, // ENUMERATED |
| 68 | B_ASN1_UNKNOWN, // EMBEDDED PDV |
| 69 | B_ASN1_UTF8STRING, |
| 70 | B_ASN1_UNKNOWN, // RELATIVE-OID |
| 71 | B_ASN1_UNKNOWN, // TIME |
| 72 | B_ASN1_UNKNOWN, // (reserved) |
| 73 | B_ASN1_SEQUENCE, |
| 74 | 0, // SET |
| 75 | B_ASN1_NUMERICSTRING, |
| 76 | B_ASN1_PRINTABLESTRING, |
| 77 | B_ASN1_T61STRING, |
| 78 | B_ASN1_VIDEOTEXSTRING, |
| 79 | B_ASN1_IA5STRING, |
| 80 | B_ASN1_UTCTIME, |
| 81 | B_ASN1_GENERALIZEDTIME, |
| 82 | B_ASN1_GRAPHICSTRING, |
| 83 | B_ASN1_ISO64STRING, |
| 84 | B_ASN1_GENERALSTRING, |
| 85 | B_ASN1_UNIVERSALSTRING, |
| 86 | B_ASN1_UNKNOWN, // CHARACTER STRING |
| 87 | B_ASN1_BMPSTRING, |
| 88 | }; |
| 89 |
| 90 | unsigned long ASN1_tag2bit(int tag) { |
| 91 | if (tag < 0 || tag > 30) { |
| 92 | return 0; |
| 93 | } |
| 94 | return tag2bit[tag]; |
| 95 | } |
| 96 |
| 97 | static int is_supported_universal_type(int tag, int aclass) { |
| 98 | if (aclass != V_ASN1_UNIVERSAL) { |
| 99 | return 0; |
| 100 | } |
| 101 | return tag == V_ASN1_OBJECT || tag == V_ASN1_NULL || tag == V_ASN1_BOOLEAN || |
| 102 | tag == V_ASN1_BIT_STRING || tag == V_ASN1_INTEGER || |
| 103 | tag == V_ASN1_ENUMERATED || tag == V_ASN1_OCTET_STRING || |
| 104 | tag == V_ASN1_NUMERICSTRING || tag == V_ASN1_PRINTABLESTRING || |
| 105 | tag == V_ASN1_T61STRING || tag == V_ASN1_VIDEOTEXSTRING || |
| 106 | tag == V_ASN1_IA5STRING || tag == V_ASN1_UTCTIME || |
| 107 | tag == V_ASN1_GENERALIZEDTIME || tag == V_ASN1_GRAPHICSTRING || |
| 108 | tag == V_ASN1_VISIBLESTRING || tag == V_ASN1_GENERALSTRING || |
| 109 | tag == V_ASN1_UNIVERSALSTRING || tag == V_ASN1_BMPSTRING || |
| 110 | tag == V_ASN1_UTF8STRING || tag == V_ASN1_SET || |
| 111 | tag == V_ASN1_SEQUENCE; |
| 112 | } |
| 113 |
| 114 | // Macro to initialize and invalidate the cache |
| 115 |
| 116 | // Decode an ASN1 item, this currently behaves just like a standard 'd2i' |
| 117 | // function. 'in' points to a buffer to read the data from, in future we |
| 118 | // will have more advanced versions that can input data a piece at a time and |
| 119 | // this will simply be a special case. |
| 120 |
| 121 | ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
| 122 | const ASN1_ITEM *it) { |
| 123 | ASN1_VALUE *ret = NULL; |
| 124 | if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0, |
| 125 | /*buf=*/NULL, /*depth=*/0) <= 0) { |
| 126 | // Clean up, in case the caller left a partial object. |
| 127 | // |
| 128 | // TODO(davidben): I don't think it can leave one, but the codepaths below |
| 129 | // are a bit inconsistent. Revisit this when rewriting this function. |
| 130 | ASN1_item_ex_free(&ret, it); |
| 131 | } |
| 132 |
| 133 | // If the caller supplied an output pointer, free the old one and replace it |
| 134 | // with |ret|. This differs from OpenSSL slightly in that we don't support |
| 135 | // object reuse. We run this on both success and failure. On failure, even |
| 136 | // with object reuse, OpenSSL destroys the previous object. |
| 137 | if (pval != NULL) { |
| 138 | ASN1_item_ex_free(pval, it); |
| 139 | *pval = ret; |
| 140 | } |
| 141 | return ret; |
| 142 | } |
| 143 |
| 144 | // Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and |
| 145 | // tag mismatch return -1 to handle OPTIONAL |
| 146 | // |
| 147 | // TODO(davidben): Historically, all functions in this file had to account for |
| 148 | // |*pval| containing an arbitrary existing value. This is no longer the case |
| 149 | // because |ASN1_item_d2i| now always starts from NULL. As part of rewriting |
| 150 | // this function, take the simplified assumptions into account. Though we must |
| 151 | // still account for the internal calls to |ASN1_item_ex_new|. |
| 152 |
| 153 | static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
| 154 | long len, const ASN1_ITEM *it, int tag, int aclass, |
| 155 | char opt, CRYPTO_BUFFER *buf, int depth) { |
| 156 | const ASN1_TEMPLATE *tt, *errtt = NULL; |
| 157 | const unsigned char *p = NULL, *q; |
| 158 | unsigned char oclass; |
| 159 | char cst, isopt; |
| 160 | int i; |
| 161 | int otag; |
| 162 | int ret = 0; |
| 163 | ASN1_VALUE **pchptr; |
| 164 | if (!pval) { |
| 165 | return 0; |
| 166 | } |
| 167 |
| 168 | if (buf != NULL) { |
| 169 | assert(CRYPTO_BUFFER_data(buf) <= *in && |
| 170 | *in + len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf)); |
| 171 | } |
| 172 |
| 173 | // Bound |len| to comfortably fit in an int. Lengths in this module often |
| 174 | // switch between int and long without overflow checks. |
| 175 | if (len > INT_MAX / 2) { |
| 176 | len = INT_MAX / 2; |
| 177 | } |
| 178 |
| 179 | if (++depth > ASN1_MAX_CONSTRUCTED_NEST) { |
| 180 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_TOO_DEEP); |
| 181 | goto err; |
| 182 | } |
| 183 |
| 184 | switch (it->itype) { |
| 185 | case ASN1_ITYPE_PRIMITIVE: |
| 186 | if (it->templates) { |
| 187 | // tagging or OPTIONAL is currently illegal on an item template |
| 188 | // because the flags can't get passed down. In practice this |
| 189 | // isn't a problem: we include the relevant flags from the item |
| 190 | // template in the template itself. |
| 191 | if ((tag != -1) || opt) { |
| 192 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); |
| 193 | goto err; |
| 194 | } |
| 195 | return asn1_template_ex_d2i(pval, in, len, it->templates, opt, buf, |
| 196 | depth); |
| 197 | } |
| 198 | return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt); |
| 199 | break; |
| 200 |
| 201 | case ASN1_ITYPE_MSTRING: |
| 202 | // It never makes sense for multi-strings to have implicit tagging, so |
| 203 | // if tag != -1, then this looks like an error in the template. |
| 204 | if (tag != -1) { |
| 205 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 206 | goto err; |
| 207 | } |
| 208 |
| 209 | p = *in; |
| 210 | // Just read in tag and class |
| 211 | ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, &p, len, -1, 0, 1); |
| 212 | if (!ret) { |
| 213 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 214 | goto err; |
| 215 | } |
| 216 |
| 217 | // Must be UNIVERSAL class |
| 218 | if (oclass != V_ASN1_UNIVERSAL) { |
| 219 | // If OPTIONAL, assume this is OK |
| 220 | if (opt) { |
| 221 | return -1; |
| 222 | } |
| 223 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL); |
| 224 | goto err; |
| 225 | } |
| 226 | // Check tag matches bit map |
| 227 | if (!(ASN1_tag2bit(otag) & it->utype)) { |
| 228 | // If OPTIONAL, assume this is OK |
| 229 | if (opt) { |
| 230 | return -1; |
| 231 | } |
| 232 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG); |
| 233 | goto err; |
| 234 | } |
| 235 | return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0); |
| 236 |
| 237 | case ASN1_ITYPE_EXTERN: { |
| 238 | // We don't support implicit tagging with external types. |
| 239 | if (tag != -1) { |
| 240 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 241 | goto err; |
| 242 | } |
| 243 | const ASN1_EXTERN_FUNCS *ef = |
| 244 | reinterpret_cast<const ASN1_EXTERN_FUNCS *>(it->funcs); |
| 245 | return ef->asn1_ex_d2i(pval, in, len, it, opt, NULL); |
| 246 | } |
| 247 |
| 248 | case ASN1_ITYPE_CHOICE: { |
| 249 | // It never makes sense for CHOICE types to have implicit tagging, so if |
| 250 | // tag != -1, then this looks like an error in the template. |
| 251 | if (tag != -1) { |
| 252 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 253 | goto err; |
| 254 | } |
| 255 |
| 256 | const ASN1_AUX *aux = reinterpret_cast<const ASN1_AUX *>(it->funcs); |
| 257 | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
| 258 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) { |
| 259 | goto auxerr; |
| 260 | } |
| 261 |
| 262 | if (*pval) { |
| 263 | // Free up and zero CHOICE value if initialised |
| 264 | i = asn1_get_choice_selector(pval, it); |
| 265 | if ((i >= 0) && (i < it->tcount)) { |
| 266 | tt = it->templates + i; |
| 267 | pchptr = asn1_get_field_ptr(pval, tt); |
| 268 | ASN1_template_free(pchptr, tt); |
| 269 | asn1_set_choice_selector(pval, -1, it); |
| 270 | } |
| 271 | } else if (!ASN1_item_ex_new(pval, it)) { |
| 272 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 273 | goto err; |
| 274 | } |
| 275 | // CHOICE type, try each possibility in turn |
| 276 | p = *in; |
| 277 | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 278 | pchptr = asn1_get_field_ptr(pval, tt); |
| 279 | // We mark field as OPTIONAL so its absence can be recognised. |
| 280 | ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, buf, depth); |
| 281 | // If field not present, try the next one |
| 282 | if (ret == -1) { |
| 283 | continue; |
| 284 | } |
| 285 | // If positive return, read OK, break loop |
| 286 | if (ret > 0) { |
| 287 | break; |
| 288 | } |
| 289 | // Otherwise must be an ASN1 parsing error |
| 290 | errtt = tt; |
| 291 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 292 | goto err; |
| 293 | } |
| 294 |
| 295 | // Did we fall off the end without reading anything? |
| 296 | if (i == it->tcount) { |
| 297 | // If OPTIONAL, this is OK |
| 298 | if (opt) { |
| 299 | // Free and zero it |
| 300 | ASN1_item_ex_free(pval, it); |
| 301 | return -1; |
| 302 | } |
| 303 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); |
| 304 | goto err; |
| 305 | } |
| 306 |
| 307 | asn1_set_choice_selector(pval, i, it); |
| 308 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) { |
| 309 | goto auxerr; |
| 310 | } |
| 311 | *in = p; |
| 312 | return 1; |
| 313 | } |
| 314 |
| 315 | case ASN1_ITYPE_SEQUENCE: { |
| 316 | p = *in; |
| 317 |
| 318 | // If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL |
| 319 | if (tag == -1) { |
| 320 | tag = V_ASN1_SEQUENCE; |
| 321 | aclass = V_ASN1_UNIVERSAL; |
| 322 | } |
| 323 | // Get SEQUENCE length and update len, p |
| 324 | ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, len, tag, aclass, opt); |
| 325 | if (!ret) { |
| 326 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 327 | goto err; |
| 328 | } else if (ret == -1) { |
| 329 | return -1; |
| 330 | } |
| 331 | if (!cst) { |
| 332 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); |
| 333 | goto err; |
| 334 | } |
| 335 |
| 336 | if (!*pval && !ASN1_item_ex_new(pval, it)) { |
| 337 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 338 | goto err; |
| 339 | } |
| 340 |
| 341 | const ASN1_AUX *aux = reinterpret_cast<const ASN1_AUX *>(it->funcs); |
| 342 | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
| 343 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) { |
| 344 | goto auxerr; |
| 345 | } |
| 346 |
| 347 | // Free up and zero any ADB found |
| 348 | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 349 | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
| 350 | const ASN1_TEMPLATE *seqtt; |
| 351 | ASN1_VALUE **pseqval; |
| 352 | seqtt = asn1_do_adb(pval, tt, 0); |
| 353 | if (seqtt == NULL) { |
| 354 | continue; |
| 355 | } |
| 356 | pseqval = asn1_get_field_ptr(pval, seqtt); |
| 357 | ASN1_template_free(pseqval, seqtt); |
| 358 | } |
| 359 | } |
| 360 |
| 361 | // Get each field entry |
| 362 | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
| 363 | const ASN1_TEMPLATE *seqtt; |
| 364 | ASN1_VALUE **pseqval; |
| 365 | seqtt = asn1_do_adb(pval, tt, 1); |
| 366 | if (seqtt == NULL) { |
| 367 | goto err; |
| 368 | } |
| 369 | pseqval = asn1_get_field_ptr(pval, seqtt); |
| 370 | // Have we ran out of data? |
| 371 | if (!len) { |
| 372 | break; |
| 373 | } |
| 374 | q = p; |
| 375 | // This determines the OPTIONAL flag value. The field cannot be |
| 376 | // omitted if it is the last of a SEQUENCE and there is still |
| 377 | // data to be read. This isn't strictly necessary but it |
| 378 | // increases efficiency in some cases. |
| 379 | if (i == (it->tcount - 1)) { |
| 380 | isopt = 0; |
| 381 | } else { |
| 382 | isopt = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0; |
| 383 | } |
| 384 | // attempt to read in field, allowing each to be OPTIONAL |
| 385 |
| 386 | ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, buf, depth); |
| 387 | if (!ret) { |
| 388 | errtt = seqtt; |
| 389 | goto err; |
| 390 | } else if (ret == -1) { |
| 391 | // OPTIONAL component absent. Free and zero the field. |
| 392 | ASN1_template_free(pseqval, seqtt); |
| 393 | continue; |
| 394 | } |
| 395 | // Update length |
| 396 | len -= p - q; |
| 397 | } |
| 398 |
| 399 | // Check all data read |
| 400 | if (len) { |
| 401 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH); |
| 402 | goto err; |
| 403 | } |
| 404 |
| 405 | // If we get here we've got no more data in the SEQUENCE, however we |
| 406 | // may not have read all fields so check all remaining are OPTIONAL |
| 407 | // and clear any that are. |
| 408 | for (; i < it->tcount; tt++, i++) { |
| 409 | const ASN1_TEMPLATE *seqtt; |
| 410 | seqtt = asn1_do_adb(pval, tt, 1); |
| 411 | if (seqtt == NULL) { |
| 412 | goto err; |
| 413 | } |
| 414 | if (seqtt->flags & ASN1_TFLG_OPTIONAL) { |
| 415 | ASN1_VALUE **pseqval; |
| 416 | pseqval = asn1_get_field_ptr(pval, seqtt); |
| 417 | ASN1_template_free(pseqval, seqtt); |
| 418 | } else { |
| 419 | errtt = seqtt; |
| 420 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING); |
| 421 | goto err; |
| 422 | } |
| 423 | } |
| 424 | // Save encoding |
| 425 | if (!asn1_enc_save(pval, *in, p - *in, it, buf)) { |
| 426 | goto auxerr; |
| 427 | } |
| 428 | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) { |
| 429 | goto auxerr; |
| 430 | } |
| 431 | *in = p; |
| 432 | return 1; |
| 433 | } |
| 434 |
| 435 | default: |
| 436 | return 0; |
| 437 | } |
| 438 | auxerr: |
| 439 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); |
| 440 | err: |
| 441 | ASN1_item_ex_free(pval, it); |
| 442 | if (errtt) { |
| 443 | ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname); |
| 444 | } else { |
| 445 | ERR_add_error_data(2, "Type=", it->sname); |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 |
| 450 | int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
| 451 | const ASN1_ITEM *it, int tag, int aclass, char opt, |
| 452 | CRYPTO_BUFFER *buf) { |
| 453 | return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, buf, |
| 454 | /*depth=*/0); |
| 455 | } |
| 456 |
| 457 | // Templates are handled with two separate functions. One handles any |
| 458 | // EXPLICIT tag and the other handles the rest. |
| 459 |
| 460 | static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, |
| 461 | long inlen, const ASN1_TEMPLATE *tt, char opt, |
| 462 | CRYPTO_BUFFER *buf, int depth) { |
| 463 | int aclass; |
| 464 | int ret; |
| 465 | long len; |
| 466 | const unsigned char *p, *q; |
| 467 | if (!val) { |
| 468 | return 0; |
| 469 | } |
| 470 | uint32_t flags = tt->flags; |
| 471 | aclass = flags & ASN1_TFLG_TAG_CLASS; |
| 472 |
| 473 | p = *in; |
| 474 |
| 475 | // Check if EXPLICIT tag expected |
| 476 | if (flags & ASN1_TFLG_EXPTAG) { |
| 477 | char cst; |
| 478 | // Need to work out amount of data available to the inner content and |
| 479 | // where it starts: so read in EXPLICIT header to get the info. |
| 480 | ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, inlen, tt->tag, aclass, |
| 481 | opt); |
| 482 | q = p; |
| 483 | if (!ret) { |
| 484 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 485 | return 0; |
| 486 | } else if (ret == -1) { |
| 487 | return -1; |
| 488 | } |
| 489 | if (!cst) { |
| 490 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); |
| 491 | return 0; |
| 492 | } |
| 493 | // We've found the field so it can't be OPTIONAL now |
| 494 | ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, buf, depth); |
| 495 | if (!ret) { |
| 496 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 497 | return 0; |
| 498 | } |
| 499 | // We read the field in OK so update length |
| 500 | len -= p - q; |
| 501 | // Check for trailing data. |
| 502 | if (len) { |
| 503 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); |
| 504 | goto err; |
| 505 | } |
| 506 | } else { |
| 507 | return asn1_template_noexp_d2i(val, in, inlen, tt, opt, buf, depth); |
| 508 | } |
| 509 |
| 510 | *in = p; |
| 511 | return 1; |
| 512 |
| 513 | err: |
| 514 | ASN1_template_free(val, tt); |
| 515 | return 0; |
| 516 | } |
| 517 |
| 518 | static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, |
| 519 | long len, const ASN1_TEMPLATE *tt, char opt, |
| 520 | CRYPTO_BUFFER *buf, int depth) { |
| 521 | int aclass; |
| 522 | int ret; |
| 523 | const unsigned char *p; |
| 524 | if (!val) { |
| 525 | return 0; |
| 526 | } |
| 527 | uint32_t flags = tt->flags; |
| 528 | aclass = flags & ASN1_TFLG_TAG_CLASS; |
| 529 |
| 530 | p = *in; |
| 531 |
| 532 | if (flags & ASN1_TFLG_SK_MASK) { |
| 533 | // SET OF, SEQUENCE OF |
| 534 | int sktag, skaclass; |
| 535 | // First work out expected inner tag value |
| 536 | if (flags & ASN1_TFLG_IMPTAG) { |
| 537 | sktag = tt->tag; |
| 538 | skaclass = aclass; |
| 539 | } else { |
| 540 | skaclass = V_ASN1_UNIVERSAL; |
| 541 | if (flags & ASN1_TFLG_SET_OF) { |
| 542 | sktag = V_ASN1_SET; |
| 543 | } else { |
| 544 | sktag = V_ASN1_SEQUENCE; |
| 545 | } |
| 546 | } |
| 547 | // Get the tag |
| 548 | ret = |
| 549 | asn1_check_tlen(&len, NULL, NULL, NULL, &p, len, sktag, skaclass, opt); |
| 550 | if (!ret) { |
| 551 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 552 | return 0; |
| 553 | } else if (ret == -1) { |
| 554 | return -1; |
| 555 | } |
| 556 | if (!*val) { |
| 557 | *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null(); |
| 558 | } else { |
| 559 | // We've got a valid STACK: free up any items present |
| 560 | STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val; |
| 561 | ASN1_VALUE *vtmp; |
| 562 | while (sk_ASN1_VALUE_num(sktmp) > 0) { |
| 563 | vtmp = sk_ASN1_VALUE_pop(sktmp); |
| 564 | ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); |
| 565 | } |
| 566 | } |
| 567 |
| 568 | if (!*val) { |
| 569 | goto err; |
| 570 | } |
| 571 |
| 572 | // Read as many items as we can |
| 573 | while (len > 0) { |
| 574 | ASN1_VALUE *skfield; |
| 575 | const unsigned char *q = p; |
| 576 | skfield = NULL; |
| 577 | if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), |
| 578 | /*tag=*/-1, /*aclass=*/0, /*opt=*/0, buf, depth)) { |
| 579 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 580 | goto err; |
| 581 | } |
| 582 | len -= p - q; |
| 583 | if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) { |
| 584 | ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item)); |
| 585 | goto err; |
| 586 | } |
| 587 | } |
| 588 | } else if (flags & ASN1_TFLG_IMPTAG) { |
| 589 | // IMPLICIT tagging |
| 590 | ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag, |
| 591 | aclass, opt, buf, depth); |
| 592 | if (!ret) { |
| 593 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 594 | goto err; |
| 595 | } else if (ret == -1) { |
| 596 | return -1; |
| 597 | } |
| 598 | } else { |
| 599 | // Nothing special |
| 600 | ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1, |
| 601 | /*aclass=*/0, opt, buf, depth); |
| 602 | if (!ret) { |
| 603 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 604 | goto err; |
| 605 | } else if (ret == -1) { |
| 606 | return -1; |
| 607 | } |
| 608 | } |
| 609 |
| 610 | *in = p; |
| 611 | return 1; |
| 612 |
| 613 | err: |
| 614 | ASN1_template_free(val, tt); |
| 615 | return 0; |
| 616 | } |
| 617 |
| 618 | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, |
| 619 | long inlen, const ASN1_ITEM *it, int tag, |
| 620 | int aclass, char opt) { |
| 621 | int ret = 0, utype; |
| 622 | long plen; |
| 623 | char cst; |
| 624 | const unsigned char *p; |
| 625 | const unsigned char *cont = NULL; |
| 626 | long len; |
| 627 | if (!pval) { |
| 628 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL); |
| 629 | return 0; // Should never happen |
| 630 | } |
| 631 |
| 632 | if (it->itype == ASN1_ITYPE_MSTRING) { |
| 633 | utype = tag; |
| 634 | tag = -1; |
| 635 | } else { |
| 636 | utype = it->utype; |
| 637 | } |
| 638 |
| 639 | if (utype == V_ASN1_ANY) { |
| 640 | // If type is ANY need to figure out type from tag |
| 641 | unsigned char oclass; |
| 642 | if (tag >= 0) { |
| 643 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY); |
| 644 | return 0; |
| 645 | } |
| 646 | if (opt) { |
| 647 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY); |
| 648 | return 0; |
| 649 | } |
| 650 | p = *in; |
| 651 | ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, &p, inlen, -1, 0, 0); |
| 652 | if (!ret) { |
| 653 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 654 | return 0; |
| 655 | } |
| 656 | if (!is_supported_universal_type(utype, oclass)) { |
| 657 | utype = V_ASN1_OTHER; |
| 658 | } |
| 659 | } |
| 660 | if (tag == -1) { |
| 661 | tag = utype; |
| 662 | aclass = V_ASN1_UNIVERSAL; |
| 663 | } |
| 664 | p = *in; |
| 665 | // Check header |
| 666 | ret = asn1_check_tlen(&plen, NULL, NULL, &cst, &p, inlen, tag, aclass, opt); |
| 667 | if (!ret) { |
| 668 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
| 669 | return 0; |
| 670 | } else if (ret == -1) { |
| 671 | return -1; |
| 672 | } |
| 673 | ret = 0; |
| 674 | // SEQUENCE, SET and "OTHER" are left in encoded form |
| 675 | if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || |
| 676 | (utype == V_ASN1_OTHER)) { |
| 677 | // SEQUENCE and SET must be constructed |
| 678 | if (utype != V_ASN1_OTHER && !cst) { |
| 679 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED); |
| 680 | return 0; |
| 681 | } |
| 682 |
| 683 | cont = *in; |
| 684 | len = p - cont + plen; |
| 685 | p += plen; |
| 686 | } else if (cst) { |
| 687 | // This parser historically supported BER constructed strings. We no |
| 688 | // longer do and will gradually tighten this parser into a DER |
| 689 | // parser. BER types should use |CBS_asn1_ber_to_der|. |
| 690 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE); |
| 691 | return 0; |
| 692 | } else { |
| 693 | cont = p; |
| 694 | len = plen; |
| 695 | p += plen; |
| 696 | } |
| 697 |
| 698 | // We now have content length and type: translate into a structure |
| 699 | if (!asn1_ex_c2i(pval, cont, len, utype, it)) { |
| 700 | goto err; |
| 701 | } |
| 702 |
| 703 | *in = p; |
| 704 | ret = 1; |
| 705 | err: |
| 706 | return ret; |
| 707 | } |
| 708 |
| 709 | // Translate ASN1 content octets into a structure |
| 710 |
| 711 | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len, |
| 712 | int utype, const ASN1_ITEM *it) { |
| 713 | ASN1_VALUE **opval = NULL; |
| 714 | ASN1_STRING *stmp; |
| 715 | ASN1_TYPE *typ = NULL; |
| 716 | int ret = 0; |
| 717 | ASN1_INTEGER **tint; |
| 718 |
| 719 | // Historically, |it->funcs| for primitive types contained an |
| 720 | // |ASN1_PRIMITIVE_FUNCS| table of callbacks. |
| 721 | assert(it->funcs == NULL); |
| 722 |
| 723 | // If ANY type clear type and set pointer to internal value |
| 724 | if (it->utype == V_ASN1_ANY) { |
| 725 | if (!*pval) { |
| 726 | typ = ASN1_TYPE_new(); |
| 727 | if (typ == NULL) { |
| 728 | goto err; |
| 729 | } |
| 730 | *pval = (ASN1_VALUE *)typ; |
| 731 | } else { |
| 732 | typ = (ASN1_TYPE *)*pval; |
| 733 | } |
| 734 |
| 735 | if (utype != typ->type) { |
| 736 | ASN1_TYPE_set(typ, utype, NULL); |
| 737 | } |
| 738 | opval = pval; |
| 739 | pval = &typ->value.asn1_value; |
| 740 | } |
| 741 | switch (utype) { |
| 742 | case V_ASN1_OBJECT: |
| 743 | if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) { |
| 744 | goto err; |
| 745 | } |
| 746 | break; |
| 747 |
| 748 | case V_ASN1_NULL: |
| 749 | if (len) { |
| 750 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); |
| 751 | goto err; |
| 752 | } |
| 753 | *pval = (ASN1_VALUE *)1; |
| 754 | break; |
| 755 |
| 756 | case V_ASN1_BOOLEAN: |
| 757 | if (len != 1) { |
| 758 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
| 759 | goto err; |
| 760 | } else { |
| 761 | ASN1_BOOLEAN *tbool; |
| 762 | tbool = (ASN1_BOOLEAN *)pval; |
| 763 | *tbool = *cont; |
| 764 | } |
| 765 | break; |
| 766 |
| 767 | case V_ASN1_BIT_STRING: |
| 768 | if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) { |
| 769 | goto err; |
| 770 | } |
| 771 | break; |
| 772 |
| 773 | case V_ASN1_INTEGER: |
| 774 | case V_ASN1_ENUMERATED: |
| 775 | tint = (ASN1_INTEGER **)pval; |
| 776 | if (!c2i_ASN1_INTEGER(tint, &cont, len)) { |
| 777 | goto err; |
| 778 | } |
| 779 | // Fixup type to match the expected form |
| 780 | (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); |
| 781 | break; |
| 782 |
| 783 | case V_ASN1_OCTET_STRING: |
| 784 | case V_ASN1_NUMERICSTRING: |
| 785 | case V_ASN1_PRINTABLESTRING: |
| 786 | case V_ASN1_T61STRING: |
| 787 | case V_ASN1_VIDEOTEXSTRING: |
| 788 | case V_ASN1_IA5STRING: |
| 789 | case V_ASN1_UTCTIME: |
| 790 | case V_ASN1_GENERALIZEDTIME: |
| 791 | case V_ASN1_GRAPHICSTRING: |
| 792 | case V_ASN1_VISIBLESTRING: |
| 793 | case V_ASN1_GENERALSTRING: |
| 794 | case V_ASN1_UNIVERSALSTRING: |
| 795 | case V_ASN1_BMPSTRING: |
| 796 | case V_ASN1_UTF8STRING: |
| 797 | case V_ASN1_OTHER: |
| 798 | case V_ASN1_SET: |
| 799 | case V_ASN1_SEQUENCE: |
| 800 | // TODO(crbug.com/boringssl/412): This default case should be removed, now |
| 801 | // that we've resolved https://crbug.com/boringssl/561. However, it is still |
| 802 | // needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE| |
| 803 | // broadly doesn't tolerate unrecognized universal tags, but except for |
| 804 | // eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the |
| 805 | // X509Test.NameAttributeValues test. |
| 806 | default: { |
| 807 | CBS cbs; |
| 808 | CBS_init(&cbs, cont, (size_t)len); |
| 809 | if (utype == V_ASN1_BMPSTRING) { |
| 810 | while (CBS_len(&cbs) != 0) { |
| 811 | uint32_t c; |
| 812 | if (!CBS_get_ucs2_be(&cbs, &c)) { |
| 813 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING); |
| 814 | goto err; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | if (utype == V_ASN1_UNIVERSALSTRING) { |
| 819 | while (CBS_len(&cbs) != 0) { |
| 820 | uint32_t c; |
| 821 | if (!CBS_get_utf32_be(&cbs, &c)) { |
| 822 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING); |
| 823 | goto err; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | if (utype == V_ASN1_UTF8STRING) { |
| 828 | while (CBS_len(&cbs) != 0) { |
| 829 | uint32_t c; |
| 830 | if (!CBS_get_utf8(&cbs, &c)) { |
| 831 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING); |
| 832 | goto err; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | if (utype == V_ASN1_UTCTIME) { |
| 837 | if (!CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)) { |
| 838 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT); |
| 839 | goto err; |
| 840 | } |
| 841 | } |
| 842 | if (utype == V_ASN1_GENERALIZEDTIME) { |
| 843 | if (!CBS_parse_generalized_time(&cbs, NULL, |
| 844 | /*allow_timezone_offset=*/0)) { |
| 845 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT); |
| 846 | goto err; |
| 847 | } |
| 848 | } |
| 849 | // TODO(https://crbug.com/boringssl/427): Check other string types. |
| 850 |
| 851 | // All based on ASN1_STRING and handled the same |
| 852 | if (!*pval) { |
| 853 | stmp = ASN1_STRING_type_new(utype); |
| 854 | if (!stmp) { |
| 855 | goto err; |
| 856 | } |
| 857 | *pval = (ASN1_VALUE *)stmp; |
| 858 | } else { |
| 859 | stmp = (ASN1_STRING *)*pval; |
| 860 | stmp->type = utype; |
| 861 | } |
| 862 | if (!ASN1_STRING_set(stmp, cont, len)) { |
| 863 | ASN1_STRING_free(stmp); |
| 864 | *pval = NULL; |
| 865 | goto err; |
| 866 | } |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | // If ASN1_ANY and NULL type fix up value |
| 871 | if (typ && (utype == V_ASN1_NULL)) { |
| 872 | typ->value.ptr = NULL; |
| 873 | } |
| 874 |
| 875 | ret = 1; |
| 876 | err: |
| 877 | if (!ret) { |
| 878 | ASN1_TYPE_free(typ); |
| 879 | if (opval) { |
| 880 | *opval = NULL; |
| 881 | } |
| 882 | } |
| 883 | return ret; |
| 884 | } |
| 885 |
| 886 | // Check an ASN1 tag and length: a bit like ASN1_get_object but it |
| 887 | // checks the expected tag. |
| 888 |
| 889 | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
| 890 | char *cst, const unsigned char **in, long len, |
| 891 | int exptag, int expclass, char opt) { |
| 892 | int i; |
| 893 | int ptag, pclass; |
| 894 | long plen; |
| 895 | const unsigned char *p; |
| 896 | p = *in; |
| 897 |
| 898 | i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); |
| 899 | if (i & 0x80) { |
| 900 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER); |
| 901 | return 0; |
| 902 | } |
| 903 | if (exptag >= 0) { |
| 904 | if ((exptag != ptag) || (expclass != pclass)) { |
| 905 | // If type is OPTIONAL, not an error: indicate missing type. |
| 906 | if (opt) { |
| 907 | return -1; |
| 908 | } |
| 909 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG); |
| 910 | return 0; |
| 911 | } |
| 912 | } |
| 913 |
| 914 | if (cst) { |
| 915 | *cst = i & V_ASN1_CONSTRUCTED; |
| 916 | } |
| 917 |
| 918 | if (olen) { |
| 919 | *olen = plen; |
| 920 | } |
| 921 |
| 922 | if (oclass) { |
| 923 | *oclass = pclass; |
| 924 | } |
| 925 |
| 926 | if (otag) { |
| 927 | *otag = ptag; |
| 928 | } |
| 929 |
| 930 | *in = p; |
| 931 | return 1; |
| 932 | } |
| 933 |