| 1 |
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 |
/* SPDX-License-Identifier: Unlicense */ |
| 3 |
#include "tomcrypt_private.h" |
| 4 |
|
| 5 |
/** |
| 6 |
@file der_decode_sequence_flexi.c |
| 7 |
ASN.1 DER, decode an array of ASN.1 types with a flexi parser, Tom St Denis |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifdef LTC_DER |
| 11 |
|
| 12 |
static int s_new_element(ltc_asn1_list **l) |
| 13 |
{ |
| 14 |
/* alloc new link */ |
| 15 |
if (*l == NULL) { |
| 16 |
*l = XCALLOC(1, sizeof(ltc_asn1_list)); |
| 17 |
if (*l == NULL) { |
| 18 |
return CRYPT_MEM; |
| 19 |
} |
| 20 |
} else { |
| 21 |
(*l)->next = XCALLOC(1, sizeof(ltc_asn1_list)); |
| 22 |
if ((*l)->next == NULL) { |
| 23 |
return CRYPT_MEM; |
| 24 |
} |
| 25 |
(*l)->next->prev = *l; |
| 26 |
*l = (*l)->next; |
| 27 |
} |
| 28 |
return CRYPT_OK; |
| 29 |
} |
| 30 |
#if defined(LTC_TEST_DBG) |
| 31 |
void s_print_err(const char *errstr, ltc_asn1_list *l, int err, unsigned long identifier, unsigned long data_offset, unsigned long len) |
| 32 |
{ |
| 33 |
#if LTC_TEST_DBG <= 1 |
| 34 |
if (err == CRYPT_OK) |
| 35 |
return; |
| 36 |
#endif |
| 37 |
if (l->type == LTC_ASN1_CUSTOM_TYPE) { |
| 38 |
fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s[%s %llu] (%s)\n", errstr, identifier, data_offset, len, der_asn1_class_to_string_map[l->klass], der_asn1_pc_to_string_map[l->pc], l->tag, error_to_string(err)); |
| 39 |
} else { |
| 40 |
fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s (%s)\n", errstr, identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err)); |
| 41 |
} |
| 42 |
} |
| 43 |
#else |
| 44 |
#define s_print_err(errstr, l, err, identifier, data_offset, len) LTC_UNUSED_PARAM(data_offset) |
| 45 |
#endif |
| 46 |
|
| 47 |
/** |
| 48 |
ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements. |
| 49 |
@param in The input buffer |
| 50 |
@param inlen [in/out] The length of the input buffer and on output the amount of decoded data |
| 51 |
@param out [out] A pointer to the linked list |
| 52 |
@param depth The depth/level of decoding recursion we've already reached |
| 53 |
@return CRYPT_OK on success. |
| 54 |
*/ |
| 55 |
static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth) |
| 56 |
{ |
| 57 |
ltc_asn1_list *l; |
| 58 |
int err; |
| 59 |
unsigned long identifier, len, totlen, data_offset, id_len, len_len; |
| 60 |
void *realloc_tmp; |
| 61 |
|
| 62 |
LTC_ARGCHK(in != NULL); |
| 63 |
LTC_ARGCHK(inlen != NULL); |
| 64 |
LTC_ARGCHK(out != NULL); |
| 65 |
|
| 66 |
l = NULL; |
| 67 |
totlen = 0; |
| 68 |
|
| 69 |
if (*inlen == 0) { |
| 70 |
/* alloc new link */ |
| 71 |
if ((err = s_new_element(&l)) != CRYPT_OK) { |
| 72 |
goto error; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/* scan the input and and get lengths and what not */ |
| 77 |
while (*inlen) { |
| 78 |
/* alloc new link */ |
| 79 |
if ((err = s_new_element(&l)) != CRYPT_OK) { |
| 80 |
goto error; |
| 81 |
} |
| 82 |
|
| 83 |
id_len = *inlen; |
| 84 |
if ((err = der_decode_asn1_identifier(in, &id_len, l)) != CRYPT_OK) { |
| 85 |
goto error; |
| 86 |
} |
| 87 |
/* read the type byte */ |
| 88 |
identifier = *in; |
| 89 |
|
| 90 |
if (l->type != LTC_ASN1_EOL) { |
| 91 |
/* fetch length */ |
| 92 |
len_len = *inlen - id_len; |
| 93 |
/* init with dummy values for error cases */ |
| 94 |
data_offset = 666; |
| 95 |
len = 0; |
| 96 |
if ((err = der_decode_asn1_length(&in[id_len], &len_len, &len)) != CRYPT_OK) { |
| 97 |
s_print_err("E1", l, err, identifier, data_offset, len); |
| 98 |
goto error; |
| 99 |
} else if (len > (*inlen - id_len - len_len)) { |
| 100 |
err = CRYPT_INVALID_PACKET; |
| 101 |
s_print_err("E2", l, err, identifier, data_offset, len); |
| 102 |
goto error; |
| 103 |
} |
| 104 |
data_offset = id_len + len_len; |
| 105 |
s_print_err("OK", l, err, identifier, data_offset, len); |
| 106 |
len += data_offset; |
| 107 |
|
| 108 |
if (l->type == LTC_ASN1_CUSTOM_TYPE) { |
| 109 |
/* Custom type, use the 'used' field to store the original identifier */ |
| 110 |
l->used = identifier; |
| 111 |
if (l->pc == LTC_ASN1_PC_CONSTRUCTED) { |
| 112 |
/* treat constructed elements like SEQUENCEs */ |
| 113 |
identifier = 0x20; |
| 114 |
} else { |
| 115 |
/* primitive elements are treated as opaque data */ |
| 116 |
identifier = 0x80; |
| 117 |
} |
| 118 |
} |
| 119 |
} else { |
| 120 |
/* Init this so gcc won't complain, |
| 121 |
* as this case will only be hit when we |
| 122 |
* can't decode the identifier so the |
| 123 |
* switch-case should go to default anyway... |
| 124 |
*/ |
| 125 |
data_offset = 0; |
| 126 |
len = 0; |
| 127 |
} |
| 128 |
|
| 129 |
/* now switch on type */ |
| 130 |
switch (identifier) { |
| 131 |
case 0x01: /* BOOLEAN */ |
| 132 |
if (l->type != LTC_ASN1_BOOLEAN) { |
| 133 |
err = CRYPT_PK_ASN1_ERROR; |
| 134 |
goto error; |
| 135 |
} |
| 136 |
|
| 137 |
/* init field */ |
| 138 |
l->size = 1; |
| 139 |
l->data = XCALLOC(1, sizeof(int)); |
| 140 |
|
| 141 |
if ((err = der_decode_boolean(in, *inlen, l->data)) != CRYPT_OK) { |
| 142 |
goto error; |
| 143 |
} |
| 144 |
|
| 145 |
if ((err = der_length_boolean(&len)) != CRYPT_OK) { |
| 146 |
goto error; |
| 147 |
} |
| 148 |
break; |
| 149 |
|
| 150 |
case 0x02: /* INTEGER */ |
| 151 |
if (l->type != LTC_ASN1_INTEGER) { |
| 152 |
err = CRYPT_PK_ASN1_ERROR; |
| 153 |
goto error; |
| 154 |
} |
| 155 |
|
| 156 |
/* init field */ |
| 157 |
l->size = 1; |
| 158 |
if ((err = ltc_mp_init(&l->data)) != CRYPT_OK) { |
| 159 |
goto error; |
| 160 |
} |
| 161 |
|
| 162 |
/* decode field */ |
| 163 |
if ((err = der_decode_integer(in, *inlen, l->data)) != CRYPT_OK) { |
| 164 |
goto error; |
| 165 |
} |
| 166 |
|
| 167 |
/* calc length of object */ |
| 168 |
if ((err = der_length_integer(l->data, &len)) != CRYPT_OK) { |
| 169 |
goto error; |
| 170 |
} |
| 171 |
break; |
| 172 |
|
| 173 |
case 0x03: /* BIT */ |
| 174 |
if (l->type != LTC_ASN1_BIT_STRING) { |
| 175 |
err = CRYPT_PK_ASN1_ERROR; |
| 176 |
goto error; |
| 177 |
} |
| 178 |
|
| 179 |
/* init field */ |
| 180 |
l->size = len * 8; /* *8 because we store decoded bits one per char and they are encoded 8 per char. */ |
| 181 |
|
| 182 |
if ((l->data = XCALLOC(1, l->size)) == NULL) { |
| 183 |
err = CRYPT_MEM; |
| 184 |
goto error; |
| 185 |
} |
| 186 |
|
| 187 |
if ((err = der_decode_bit_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 188 |
goto error; |
| 189 |
} |
| 190 |
|
| 191 |
if ((err = der_length_bit_string(l->size, &len)) != CRYPT_OK) { |
| 192 |
goto error; |
| 193 |
} |
| 194 |
break; |
| 195 |
|
| 196 |
case 0x04: /* OCTET */ |
| 197 |
if (l->type != LTC_ASN1_OCTET_STRING) { |
| 198 |
err = CRYPT_PK_ASN1_ERROR; |
| 199 |
goto error; |
| 200 |
} |
| 201 |
|
| 202 |
/* init field */ |
| 203 |
l->size = len; |
| 204 |
|
| 205 |
if ((l->data = XCALLOC(1, l->size)) == NULL) { |
| 206 |
err = CRYPT_MEM; |
| 207 |
goto error; |
| 208 |
} |
| 209 |
|
| 210 |
if ((err = der_decode_octet_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 211 |
goto error; |
| 212 |
} |
| 213 |
|
| 214 |
if ((err = der_length_octet_string(l->size, &len)) != CRYPT_OK) { |
| 215 |
goto error; |
| 216 |
} |
| 217 |
break; |
| 218 |
|
| 219 |
case 0x05: /* NULL */ |
| 220 |
if (l->type != LTC_ASN1_NULL) { |
| 221 |
err = CRYPT_PK_ASN1_ERROR; |
| 222 |
goto error; |
| 223 |
} |
| 224 |
|
| 225 |
/* valid NULL is 0x05 0x00 */ |
| 226 |
if (in[0] != 0x05 || in[1] != 0x00) { |
| 227 |
err = CRYPT_INVALID_PACKET; |
| 228 |
goto error; |
| 229 |
} |
| 230 |
|
| 231 |
/* simple to store ;-) */ |
| 232 |
l->data = NULL; |
| 233 |
l->size = 0; |
| 234 |
len = 2; |
| 235 |
|
| 236 |
break; |
| 237 |
|
| 238 |
case 0x06: /* OID */ |
| 239 |
if (l->type != LTC_ASN1_OBJECT_IDENTIFIER) { |
| 240 |
err = CRYPT_PK_ASN1_ERROR; |
| 241 |
goto error; |
| 242 |
} |
| 243 |
|
| 244 |
/* init field */ |
| 245 |
l->size = len; |
| 246 |
|
| 247 |
if ((l->data = XCALLOC(len, sizeof(unsigned long))) == NULL) { |
| 248 |
err = CRYPT_MEM; |
| 249 |
goto error; |
| 250 |
} |
| 251 |
|
| 252 |
if ((err = der_decode_object_identifier(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 253 |
goto error; |
| 254 |
} |
| 255 |
|
| 256 |
if ((err = der_length_object_identifier(l->data, l->size, &len)) != CRYPT_OK) { |
| 257 |
goto error; |
| 258 |
} |
| 259 |
|
| 260 |
/* resize it to save a bunch of mem */ |
| 261 |
if ((realloc_tmp = XREALLOC(l->data, l->size * sizeof(unsigned long))) == NULL) { |
| 262 |
/* out of heap but this is not an error */ |
| 263 |
break; |
| 264 |
} |
| 265 |
l->data = realloc_tmp; |
| 266 |
break; |
| 267 |
|
| 268 |
case 0x0C: /* UTF8 */ |
| 269 |
|
| 270 |
/* init field */ |
| 271 |
if (l->type != LTC_ASN1_UTF8_STRING) { |
| 272 |
err = CRYPT_PK_ASN1_ERROR; |
| 273 |
goto error; |
| 274 |
} |
| 275 |
l->size = len; |
| 276 |
|
| 277 |
if ((l->data = XCALLOC(l->size, sizeof(wchar_t))) == NULL) { |
| 278 |
err = CRYPT_MEM; |
| 279 |
goto error; |
| 280 |
} |
| 281 |
|
| 282 |
if ((err = der_decode_utf8_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 283 |
goto error; |
| 284 |
} |
| 285 |
|
| 286 |
if ((err = der_length_utf8_string(l->data, l->size, &len)) != CRYPT_OK) { |
| 287 |
goto error; |
| 288 |
} |
| 289 |
break; |
| 290 |
|
| 291 |
case 0x13: /* PRINTABLE */ |
| 292 |
if (l->type != LTC_ASN1_PRINTABLE_STRING) { |
| 293 |
err = CRYPT_PK_ASN1_ERROR; |
| 294 |
goto error; |
| 295 |
} |
| 296 |
|
| 297 |
/* init field */ |
| 298 |
l->size = len; |
| 299 |
|
| 300 |
if ((l->data = XCALLOC(1, l->size)) == NULL) { |
| 301 |
err = CRYPT_MEM; |
| 302 |
goto error; |
| 303 |
} |
| 304 |
|
| 305 |
if ((err = der_decode_printable_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 306 |
goto error; |
| 307 |
} |
| 308 |
|
| 309 |
if ((err = der_length_printable_string(l->data, l->size, &len)) != CRYPT_OK) { |
| 310 |
goto error; |
| 311 |
} |
| 312 |
break; |
| 313 |
|
| 314 |
case 0x14: /* TELETEXT */ |
| 315 |
if (l->type != LTC_ASN1_TELETEX_STRING) { |
| 316 |
err = CRYPT_PK_ASN1_ERROR; |
| 317 |
goto error; |
| 318 |
} |
| 319 |
|
| 320 |
/* init field */ |
| 321 |
l->size = len; |
| 322 |
|
| 323 |
if ((l->data = XCALLOC(1, l->size)) == NULL) { |
| 324 |
err = CRYPT_MEM; |
| 325 |
goto error; |
| 326 |
} |
| 327 |
|
| 328 |
if ((err = der_decode_teletex_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 329 |
goto error; |
| 330 |
} |
| 331 |
|
| 332 |
if ((err = der_length_teletex_string(l->data, l->size, &len)) != CRYPT_OK) { |
| 333 |
goto error; |
| 334 |
} |
| 335 |
break; |
| 336 |
|
| 337 |
case 0x16: /* IA5 */ |
| 338 |
if (l->type != LTC_ASN1_IA5_STRING) { |
| 339 |
err = CRYPT_PK_ASN1_ERROR; |
| 340 |
goto error; |
| 341 |
} |
| 342 |
|
| 343 |
/* init field */ |
| 344 |
l->size = len; |
| 345 |
|
| 346 |
if ((l->data = XCALLOC(1, l->size)) == NULL) { |
| 347 |
err = CRYPT_MEM; |
| 348 |
goto error; |
| 349 |
} |
| 350 |
|
| 351 |
if ((err = der_decode_ia5_string(in, *inlen, l->data, &l->size)) != CRYPT_OK) { |
| 352 |
goto error; |
| 353 |
} |
| 354 |
|
| 355 |
if ((err = der_length_ia5_string(l->data, l->size, &len)) != CRYPT_OK) { |
| 356 |
goto error; |
| 357 |
} |
| 358 |
break; |
| 359 |
|
| 360 |
case 0x17: /* UTC TIME */ |
| 361 |
if (l->type != LTC_ASN1_UTCTIME) { |
| 362 |
err = CRYPT_PK_ASN1_ERROR; |
| 363 |
goto error; |
| 364 |
} |
| 365 |
|
| 366 |
/* init field */ |
| 367 |
l->size = 1; |
| 368 |
|
| 369 |
if ((l->data = XCALLOC(1, sizeof(ltc_utctime))) == NULL) { |
| 370 |
err = CRYPT_MEM; |
| 371 |
goto error; |
| 372 |
} |
| 373 |
|
| 374 |
len = *inlen; |
| 375 |
if ((err = der_decode_utctime(in, &len, l->data)) != CRYPT_OK) { |
| 376 |
goto error; |
| 377 |
} |
| 378 |
|
| 379 |
if ((err = der_length_utctime(l->data, &len)) != CRYPT_OK) { |
| 380 |
goto error; |
| 381 |
} |
| 382 |
break; |
| 383 |
|
| 384 |
case 0x18: |
| 385 |
if (l->type != LTC_ASN1_GENERALIZEDTIME) { |
| 386 |
err = CRYPT_PK_ASN1_ERROR; |
| 387 |
goto error; |
| 388 |
} |
| 389 |
|
| 390 |
/* init field */ |
| 391 |
l->size = len; |
| 392 |
|
| 393 |
if ((l->data = XCALLOC(1, sizeof(ltc_generalizedtime))) == NULL) { |
| 394 |
err = CRYPT_MEM; |
| 395 |
goto error; |
| 396 |
} |
| 397 |
|
| 398 |
if ((err = der_decode_generalizedtime(in, &len, l->data)) != CRYPT_OK) { |
| 399 |
goto error; |
| 400 |
} |
| 401 |
|
| 402 |
if ((err = der_length_generalizedtime(l->data, &len)) != CRYPT_OK) { |
| 403 |
goto error; |
| 404 |
} |
| 405 |
|
| 406 |
break; |
| 407 |
|
| 408 |
case 0x20: /* Any CONSTRUCTED element that is neither SEQUENCE nor SET */ |
| 409 |
case 0x30: /* SEQUENCE */ |
| 410 |
case 0x31: /* SET */ |
| 411 |
|
| 412 |
/* init field */ |
| 413 |
if (identifier == 0x20) { |
| 414 |
if (l->type != LTC_ASN1_CUSTOM_TYPE) { |
| 415 |
err = CRYPT_PK_ASN1_ERROR; |
| 416 |
goto error; |
| 417 |
} |
| 418 |
} |
| 419 |
else if (identifier == 0x30) { |
| 420 |
if (l->type != LTC_ASN1_SEQUENCE) { |
| 421 |
err = CRYPT_PK_ASN1_ERROR; |
| 422 |
goto error; |
| 423 |
} |
| 424 |
} |
| 425 |
else { |
| 426 |
if (l->type != LTC_ASN1_SET) { |
| 427 |
err = CRYPT_PK_ASN1_ERROR; |
| 428 |
goto error; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/* check that we don't go over the recursion limit */ |
| 433 |
if (depth > LTC_DER_MAX_RECURSION) { |
| 434 |
err = CRYPT_PK_ASN1_ERROR; |
| 435 |
goto error; |
| 436 |
} |
| 437 |
|
| 438 |
if ((l->data = XMALLOC(len)) == NULL) { |
| 439 |
err = CRYPT_MEM; |
| 440 |
goto error; |
| 441 |
} |
| 442 |
|
| 443 |
XMEMCPY(l->data, in, len); |
| 444 |
l->size = len; |
| 445 |
|
| 446 |
|
| 447 |
/* jump to the start of the data */ |
| 448 |
in += data_offset; |
| 449 |
*inlen -= data_offset; |
| 450 |
len -= data_offset; |
| 451 |
|
| 452 |
/* save the decoded ASN.1 len */ |
| 453 |
len_len = len; |
| 454 |
|
| 455 |
/* Sequence elements go as child */ |
| 456 |
if ((err = s_der_decode_sequence_flexi(in, &len, &(l->child), depth+1)) != CRYPT_OK) { |
| 457 |
goto error; |
| 458 |
} |
| 459 |
if (len_len != len) { |
| 460 |
err = CRYPT_PK_ASN1_ERROR; |
| 461 |
goto error; |
| 462 |
} |
| 463 |
|
| 464 |
/* len update */ |
| 465 |
totlen += data_offset; |
| 466 |
|
| 467 |
/* the flexi decoder can also do nothing, so make sure a child has been allocated */ |
| 468 |
if (l->child) { |
| 469 |
/* link them up y0 */ |
| 470 |
l->child->parent = l; |
| 471 |
} |
| 472 |
|
| 473 |
break; |
| 474 |
|
| 475 |
case 0x80: /* Context-specific */ |
| 476 |
if (l->type != LTC_ASN1_CUSTOM_TYPE) { |
| 477 |
err = CRYPT_PK_ASN1_ERROR; |
| 478 |
goto error; |
| 479 |
} |
| 480 |
|
| 481 |
if ((l->data = XCALLOC(1, len - data_offset)) == NULL) { |
| 482 |
err = CRYPT_MEM; |
| 483 |
goto error; |
| 484 |
} |
| 485 |
|
| 486 |
XMEMCPY(l->data, in + data_offset, len - data_offset); |
| 487 |
l->size = len - data_offset; |
| 488 |
|
| 489 |
break; |
| 490 |
|
| 491 |
default: |
| 492 |
/* invalid byte ... this is a soft error */ |
| 493 |
/* remove link */ |
| 494 |
if (l->prev) { |
| 495 |
l = l->prev; |
| 496 |
XFREE(l->next); |
| 497 |
l->next = NULL; |
| 498 |
} |
| 499 |
goto outside; |
| 500 |
} |
| 501 |
|
| 502 |
/* advance pointers */ |
| 503 |
totlen += len; |
| 504 |
in += len; |
| 505 |
*inlen -= len; |
| 506 |
} |
| 507 |
|
| 508 |
outside: |
| 509 |
|
| 510 |
/* in case we processed anything */ |
| 511 |
if (totlen) { |
| 512 |
/* rewind l please */ |
| 513 |
while (l->prev != NULL || l->parent != NULL) { |
| 514 |
if (l->parent != NULL) { |
| 515 |
l = l->parent; |
| 516 |
} else { |
| 517 |
l = l->prev; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
/* return */ |
| 523 |
*out = l; |
| 524 |
*inlen = totlen; |
| 525 |
return CRYPT_OK; |
| 526 |
|
| 527 |
error: |
| 528 |
/* free list */ |
| 529 |
der_sequence_free(l); |
| 530 |
|
| 531 |
return err; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements. |
| 536 |
@param in The input buffer |
| 537 |
@param inlen [in/out] The length of the input buffer and on output the amount of decoded data |
| 538 |
@param out [out] A pointer to the linked list |
| 539 |
@return CRYPT_OK on success. |
| 540 |
*/ |
| 541 |
int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out) |
| 542 |
{ |
| 543 |
return s_der_decode_sequence_flexi(in, inlen, out, 0); |
| 544 |
} |
| 545 |
|
| 546 |
#endif |
| 547 |
|