summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_dec.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c409
1 files changed, 143 insertions, 266 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 0df42a4a93..1a06ddc185 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -175,19 +175,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
175 asn1_cb = aux->asn1_cb; 175 asn1_cb = aux->asn1_cb;
176 else asn1_cb = 0; 176 else asn1_cb = 0;
177 177
178 switch(it->itype) 178 switch(it->itype) {
179 { 179 case ASN1_ITYPE_PRIMITIVE:
180 case ASN1_ITYPE_PRIMITIVE: 180 if (it->templates) {
181 if (it->templates)
182 {
183 /* tagging or OPTIONAL is currently illegal on an item 181 /* tagging or OPTIONAL is currently illegal on an item
184 * template because the flags can't get passed down. 182 * template because the flags can't get passed down.
185 * In practice this isn't a problem: we include the 183 * In practice this isn't a problem: we include the
186 * relevant flags from the item template in the 184 * relevant flags from the item template in the
187 * template itself. 185 * template itself.
188 */ 186 */
189 if ((tag != -1) || opt) 187 if ((tag != -1) || opt) {
190 {
191 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 188 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
192 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 189 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
193 goto err; 190 goto err;
@@ -199,21 +196,19 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
199 tag, aclass, opt, ctx); 196 tag, aclass, opt, ctx);
200 break; 197 break;
201 198
202 case ASN1_ITYPE_MSTRING: 199 case ASN1_ITYPE_MSTRING:
203 p = *in; 200 p = *in;
204 /* Just read in tag and class */ 201 /* Just read in tag and class */
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, 202 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx); 203 &p, len, -1, 0, 1, ctx);
207 if (!ret) 204 if (!ret) {
208 {
209 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 205 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
210 ERR_R_NESTED_ASN1_ERROR); 206 ERR_R_NESTED_ASN1_ERROR);
211 goto err; 207 goto err;
212 } 208 }
213 209
214 /* Must be UNIVERSAL class */ 210 /* Must be UNIVERSAL class */
215 if (oclass != V_ASN1_UNIVERSAL) 211 if (oclass != V_ASN1_UNIVERSAL) {
216 {
217 /* If OPTIONAL, assume this is OK */ 212 /* If OPTIONAL, assume this is OK */
218 if (opt) return -1; 213 if (opt) return -1;
219 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
@@ -221,8 +216,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
221 goto err; 216 goto err;
222 } 217 }
223 /* Check tag matches bit map */ 218 /* Check tag matches bit map */
224 if (!(ASN1_tag2bit(otag) & it->utype)) 219 if (!(ASN1_tag2bit(otag) & it->utype)) {
225 {
226 /* If OPTIONAL, assume this is OK */ 220 /* If OPTIONAL, assume this is OK */
227 if (opt) 221 if (opt)
228 return -1; 222 return -1;
@@ -233,19 +227,18 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
233 return asn1_d2i_ex_primitive(pval, in, len, 227 return asn1_d2i_ex_primitive(pval, in, len,
234 it, otag, 0, 0, ctx); 228 it, otag, 0, 0, ctx);
235 229
236 case ASN1_ITYPE_EXTERN: 230 case ASN1_ITYPE_EXTERN:
237 /* Use new style d2i */ 231 /* Use new style d2i */
238 ef = it->funcs; 232 ef = it->funcs;
239 return ef->asn1_ex_d2i(pval, in, len, 233 return ef->asn1_ex_d2i(pval, in, len,
240 it, tag, aclass, opt, ctx); 234 it, tag, aclass, opt, ctx);
241 235
242 case ASN1_ITYPE_COMPAT: 236 case ASN1_ITYPE_COMPAT:
243 /* we must resort to old style evil hackery */ 237 /* we must resort to old style evil hackery */
244 cf = it->funcs; 238 cf = it->funcs;
245 239
246 /* If OPTIONAL see if it is there */ 240 /* If OPTIONAL see if it is there */
247 if (opt) 241 if (opt) {
248 {
249 int exptag; 242 int exptag;
250 p = *in; 243 p = *in;
251 if (tag == -1) 244 if (tag == -1)
@@ -256,8 +249,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
256 249
257 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, 250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
258 &p, len, exptag, aclass, 1, ctx); 251 &p, len, exptag, aclass, 1, ctx);
259 if (!ret) 252 if (!ret) {
260 {
261 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
262 ERR_R_NESTED_ASN1_ERROR); 254 ERR_R_NESTED_ASN1_ERROR);
263 goto err; 255 goto err;
@@ -281,12 +273,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
281 * buffer. 273 * buffer.
282 */ 274 */
283 275
284 if (tag != -1) 276 if (tag != -1) {
285 {
286 wp = *(unsigned char **)in; 277 wp = *(unsigned char **)in;
287 imphack = *wp; 278 imphack = *wp;
288 if (p == NULL) 279 if (p == NULL) {
289 {
290 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
291 ERR_R_NESTED_ASN1_ERROR); 281 ERR_R_NESTED_ASN1_ERROR);
292 goto err; 282 goto err;
@@ -307,21 +297,19 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
307 goto err; 297 goto err;
308 298
309 299
310 case ASN1_ITYPE_CHOICE: 300 case ASN1_ITYPE_CHOICE:
311 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 301 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
312 goto auxerr; 302 goto auxerr;
313 303
314 /* Allocate structure */ 304 /* Allocate structure */
315 if (!*pval && !ASN1_item_ex_new(pval, it)) 305 if (!*pval && !ASN1_item_ex_new(pval, it)) {
316 {
317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
318 ERR_R_NESTED_ASN1_ERROR); 307 ERR_R_NESTED_ASN1_ERROR);
319 goto err; 308 goto err;
320 } 309 }
321 /* CHOICE type, try each possibility in turn */ 310 /* CHOICE type, try each possibility in turn */
322 p = *in; 311 p = *in;
323 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) 312 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) {
324 {
325 pchptr = asn1_get_field_ptr(pval, tt); 313 pchptr = asn1_get_field_ptr(pval, tt);
326 /* We mark field as OPTIONAL so its absence 314 /* We mark field as OPTIONAL so its absence
327 * can be recognised. 315 * can be recognised.
@@ -341,11 +329,9 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
341 } 329 }
342 330
343 /* Did we fall off the end without reading anything? */ 331 /* Did we fall off the end without reading anything? */
344 if (i == it->tcount) 332 if (i == it->tcount) {
345 {
346 /* If OPTIONAL, this is OK */ 333 /* If OPTIONAL, this is OK */
347 if (opt) 334 if (opt) {
348 {
349 /* Free and zero it */ 335 /* Free and zero it */
350 ASN1_item_ex_free(pval, it); 336 ASN1_item_ex_free(pval, it);
351 return -1; 337 return -1;
@@ -361,44 +347,39 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
361 goto auxerr; 347 goto auxerr;
362 return 1; 348 return 1;
363 349
364 case ASN1_ITYPE_NDEF_SEQUENCE: 350 case ASN1_ITYPE_NDEF_SEQUENCE:
365 case ASN1_ITYPE_SEQUENCE: 351 case ASN1_ITYPE_SEQUENCE:
366 p = *in; 352 p = *in;
367 tmplen = len; 353 tmplen = len;
368 354
369 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ 355 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
370 if (tag == -1) 356 if (tag == -1) {
371 {
372 tag = V_ASN1_SEQUENCE; 357 tag = V_ASN1_SEQUENCE;
373 aclass = V_ASN1_UNIVERSAL; 358 aclass = V_ASN1_UNIVERSAL;
374 } 359 }
375 /* Get SEQUENCE length and update len, p */ 360 /* Get SEQUENCE length and update len, p */
376 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, 361 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
377 &p, len, tag, aclass, opt, ctx); 362 &p, len, tag, aclass, opt, ctx);
378 if (!ret) 363 if (!ret) {
379 {
380 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 364 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
381 ERR_R_NESTED_ASN1_ERROR); 365 ERR_R_NESTED_ASN1_ERROR);
382 goto err; 366 goto err;
383 } 367 }
384 else if (ret == -1) 368 else if (ret == -1)
385 return -1; 369 return -1;
386 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) 370 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
387 {
388 len = tmplen - (p - *in); 371 len = tmplen - (p - *in);
389 seq_nolen = 1; 372 seq_nolen = 1;
390 } 373 }
391 /* If indefinite we don't do a length check */ 374 /* If indefinite we don't do a length check */
392 else seq_nolen = seq_eoc; 375 else seq_nolen = seq_eoc;
393 if (!cst) 376 if (!cst) {
394 {
395 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 377 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
396 ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 378 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
397 goto err; 379 goto err;
398 } 380 }
399 381
400 if (!*pval && !ASN1_item_ex_new(pval, it)) 382 if (!*pval && !ASN1_item_ex_new(pval, it)) {
401 {
402 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 383 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
403 ERR_R_NESTED_ASN1_ERROR); 384 ERR_R_NESTED_ASN1_ERROR);
404 goto err; 385 goto err;
@@ -408,8 +389,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
408 goto auxerr; 389 goto auxerr;
409 390
410 /* Get each field entry */ 391 /* Get each field entry */
411 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) 392 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
412 {
413 const ASN1_TEMPLATE *seqtt; 393 const ASN1_TEMPLATE *seqtt;
414 ASN1_VALUE **pseqval; 394 ASN1_VALUE **pseqval;
415 seqtt = asn1_do_adb(pval, tt, 1); 395 seqtt = asn1_do_adb(pval, tt, 1);
@@ -420,10 +400,8 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
420 if (!len) 400 if (!len)
421 break; 401 break;
422 q = p; 402 q = p;
423 if (asn1_check_eoc(&p, len)) 403 if (asn1_check_eoc(&p, len)) {
424 { 404 if (!seq_eoc) {
425 if (!seq_eoc)
426 {
427 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 405 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
428 ASN1_R_UNEXPECTED_EOC); 406 ASN1_R_UNEXPECTED_EOC);
429 goto err; 407 goto err;
@@ -447,13 +425,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
447 425
448 ret = asn1_template_ex_d2i(pseqval, &p, len, 426 ret = asn1_template_ex_d2i(pseqval, &p, len,
449 seqtt, isopt, ctx); 427 seqtt, isopt, ctx);
450 if (!ret) 428 if (!ret) {
451 {
452 errtt = seqtt; 429 errtt = seqtt;
453 goto err; 430 goto err;
454 } 431 } else if (ret == -1) {
455 else if (ret == -1)
456 {
457 /* OPTIONAL component absent. 432 /* OPTIONAL component absent.
458 * Free and zero the field. 433 * Free and zero the field.
459 */ 434 */
@@ -465,14 +440,12 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
465 } 440 }
466 441
467 /* Check for EOC if expecting one */ 442 /* Check for EOC if expecting one */
468 if (seq_eoc && !asn1_check_eoc(&p, len)) 443 if (seq_eoc && !asn1_check_eoc(&p, len)) {
469 {
470 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC); 444 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
471 goto err; 445 goto err;
472 } 446 }
473 /* Check all data read */ 447 /* Check all data read */
474 if (!seq_nolen && len) 448 if (!seq_nolen && len) {
475 {
476 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
477 ASN1_R_SEQUENCE_LENGTH_MISMATCH); 450 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
478 goto err; 451 goto err;
@@ -482,20 +455,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
482 * however we may not have read all fields so check all 455 * however we may not have read all fields so check all
483 * remaining are OPTIONAL and clear any that are. 456 * remaining are OPTIONAL and clear any that are.
484 */ 457 */
485 for (; i < it->tcount; tt++, i++) 458 for (; i < it->tcount; tt++, i++) {
486 {
487 const ASN1_TEMPLATE *seqtt; 459 const ASN1_TEMPLATE *seqtt;
488 seqtt = asn1_do_adb(pval, tt, 1); 460 seqtt = asn1_do_adb(pval, tt, 1);
489 if (!seqtt) 461 if (!seqtt)
490 goto err; 462 goto err;
491 if (seqtt->flags & ASN1_TFLG_OPTIONAL) 463 if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
492 {
493 ASN1_VALUE **pseqval; 464 ASN1_VALUE **pseqval;
494 pseqval = asn1_get_field_ptr(pval, seqtt); 465 pseqval = asn1_get_field_ptr(pval, seqtt);
495 ASN1_template_free(pseqval, seqtt); 466 ASN1_template_free(pseqval, seqtt);
496 } 467 } else {
497 else
498 {
499 errtt = seqtt; 468 errtt = seqtt;
500 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
501 ASN1_R_FIELD_MISSING); 470 ASN1_R_FIELD_MISSING);
@@ -547,8 +516,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
547 p = *in; 516 p = *in;
548 517
549 /* Check if EXPLICIT tag expected */ 518 /* Check if EXPLICIT tag expected */
550 if (flags & ASN1_TFLG_EXPTAG) 519 if (flags & ASN1_TFLG_EXPTAG) {
551 {
552 char cst; 520 char cst;
553 /* Need to work out amount of data available to the inner 521 /* Need to work out amount of data available to the inner
554 * content and where it starts: so read in EXPLICIT header to 522 * content and where it starts: so read in EXPLICIT header to
@@ -557,46 +525,37 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
557 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, 525 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
558 &p, inlen, tt->tag, aclass, opt, ctx); 526 &p, inlen, tt->tag, aclass, opt, ctx);
559 q = p; 527 q = p;
560 if (!ret) 528 if (!ret) {
561 {
562 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 529 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
563 ERR_R_NESTED_ASN1_ERROR); 530 ERR_R_NESTED_ASN1_ERROR);
564 return 0; 531 return 0;
565 } 532 } else if (ret == -1)
566 else if (ret == -1)
567 return -1; 533 return -1;
568 if (!cst) 534 if (!cst) {
569 {
570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 535 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
571 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 536 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
572 return 0; 537 return 0;
573 } 538 }
574 /* We've found the field so it can't be OPTIONAL now */ 539 /* We've found the field so it can't be OPTIONAL now */
575 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 540 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
576 if (!ret) 541 if (!ret) {
577 {
578 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 542 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
579 ERR_R_NESTED_ASN1_ERROR); 543 ERR_R_NESTED_ASN1_ERROR);
580 return 0; 544 return 0;
581 } 545 }
582 /* We read the field in OK so update length */ 546 /* We read the field in OK so update length */
583 len -= p - q; 547 len -= p - q;
584 if (exp_eoc) 548 if (exp_eoc) {
585 {
586 /* If NDEF we must have an EOC here */ 549 /* If NDEF we must have an EOC here */
587 if (!asn1_check_eoc(&p, len)) 550 if (!asn1_check_eoc(&p, len)) {
588 {
589 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 551 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
590 ASN1_R_MISSING_EOC); 552 ASN1_R_MISSING_EOC);
591 goto err; 553 goto err;
592 } 554 }
593 } 555 } else {
594 else
595 {
596 /* Otherwise we must hit the EXPLICIT tag end or its 556 /* Otherwise we must hit the EXPLICIT tag end or its
597 * an error */ 557 * an error */
598 if (len) 558 if (len) {
599 {
600 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 559 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
601 ASN1_R_EXPLICIT_LENGTH_MISMATCH); 560 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
602 goto err; 561 goto err;
@@ -631,19 +590,15 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
631 p = *in; 590 p = *in;
632 q = p; 591 q = p;
633 592
634 if (flags & ASN1_TFLG_SK_MASK) 593 if (flags & ASN1_TFLG_SK_MASK) {
635 {
636 /* SET OF, SEQUENCE OF */ 594 /* SET OF, SEQUENCE OF */
637 int sktag, skaclass; 595 int sktag, skaclass;
638 char sk_eoc; 596 char sk_eoc;
639 /* First work out expected inner tag value */ 597 /* First work out expected inner tag value */
640 if (flags & ASN1_TFLG_IMPTAG) 598 if (flags & ASN1_TFLG_IMPTAG) {
641 {
642 sktag = tt->tag; 599 sktag = tt->tag;
643 skaclass = aclass; 600 skaclass = aclass;
644 } 601 } else {
645 else
646 {
647 skaclass = V_ASN1_UNIVERSAL; 602 skaclass = V_ASN1_UNIVERSAL;
648 if (flags & ASN1_TFLG_SET_OF) 603 if (flags & ASN1_TFLG_SET_OF)
649 sktag = V_ASN1_SET; 604 sktag = V_ASN1_SET;
@@ -653,8 +608,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
653 /* Get the tag */ 608 /* Get the tag */
654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, 609 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
655 &p, len, sktag, skaclass, opt, ctx); 610 &p, len, sktag, skaclass, opt, ctx);
656 if (!ret) 611 if (!ret) {
657 {
658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
659 ERR_R_NESTED_ASN1_ERROR); 613 ERR_R_NESTED_ASN1_ERROR);
660 return 0; 614 return 0;
@@ -663,37 +617,31 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
663 return -1; 617 return -1;
664 if (!*val) 618 if (!*val)
665 *val = (ASN1_VALUE *)sk_new_null(); 619 *val = (ASN1_VALUE *)sk_new_null();
666 else 620 else {
667 {
668 /* We've got a valid STACK: free up any items present */ 621 /* We've got a valid STACK: free up any items present */
669 STACK_OF(ASN1_VALUE) *sktmp 622 STACK_OF(ASN1_VALUE) *sktmp
670 = (STACK_OF(ASN1_VALUE) *)*val; 623 = (STACK_OF(ASN1_VALUE) *)*val;
671 ASN1_VALUE *vtmp; 624 ASN1_VALUE *vtmp;
672 while(sk_ASN1_VALUE_num(sktmp) > 0) 625 while(sk_ASN1_VALUE_num(sktmp) > 0) {
673 {
674 vtmp = sk_ASN1_VALUE_pop(sktmp); 626 vtmp = sk_ASN1_VALUE_pop(sktmp);
675 ASN1_item_ex_free(&vtmp, 627 ASN1_item_ex_free(&vtmp,
676 ASN1_ITEM_ptr(tt->item)); 628 ASN1_ITEM_ptr(tt->item));
677 } 629 }
678 } 630 }
679 631
680 if (!*val) 632 if (!*val) {
681 {
682 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 633 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
683 ERR_R_MALLOC_FAILURE); 634 ERR_R_MALLOC_FAILURE);
684 goto err; 635 goto err;
685 } 636 }
686 637
687 /* Read as many items as we can */ 638 /* Read as many items as we can */
688 while(len > 0) 639 while(len > 0) {
689 {
690 ASN1_VALUE *skfield; 640 ASN1_VALUE *skfield;
691 q = p; 641 q = p;
692 /* See if EOC found */ 642 /* See if EOC found */
693 if (asn1_check_eoc(&p, len)) 643 if (asn1_check_eoc(&p, len)) {
694 { 644 if (!sk_eoc) {
695 if (!sk_eoc)
696 {
697 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 645 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
698 ASN1_R_UNEXPECTED_EOC); 646 ASN1_R_UNEXPECTED_EOC);
699 goto err; 647 goto err;
@@ -705,48 +653,39 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
705 skfield = NULL; 653 skfield = NULL;
706 if (!ASN1_item_ex_d2i(&skfield, &p, len, 654 if (!ASN1_item_ex_d2i(&skfield, &p, len,
707 ASN1_ITEM_ptr(tt->item), 655 ASN1_ITEM_ptr(tt->item),
708 -1, 0, 0, ctx)) 656 -1, 0, 0, ctx)) {
709 {
710 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 657 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
711 ERR_R_NESTED_ASN1_ERROR); 658 ERR_R_NESTED_ASN1_ERROR);
712 goto err; 659 goto err;
713 } 660 }
714 len -= p - q; 661 len -= p - q;
715 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 662 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
716 skfield)) 663 skfield)) {
717 {
718 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 664 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
719 ERR_R_MALLOC_FAILURE); 665 ERR_R_MALLOC_FAILURE);
720 goto err; 666 goto err;
721 } 667 }
722 } 668 }
723 if (sk_eoc) 669 if (sk_eoc) {
724 {
725 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); 670 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
726 goto err; 671 goto err;
727 } 672 }
728 } 673 } else if (flags & ASN1_TFLG_IMPTAG) {
729 else if (flags & ASN1_TFLG_IMPTAG)
730 {
731 /* IMPLICIT tagging */ 674 /* IMPLICIT tagging */
732 ret = ASN1_item_ex_d2i(val, &p, len, 675 ret = ASN1_item_ex_d2i(val, &p, len,
733 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 676 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
734 if (!ret) 677 if (!ret) {
735 {
736 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 678 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
737 ERR_R_NESTED_ASN1_ERROR); 679 ERR_R_NESTED_ASN1_ERROR);
738 goto err; 680 goto err;
739 } 681 }
740 else if (ret == -1) 682 else if (ret == -1)
741 return -1; 683 return -1;
742 } 684 } else {
743 else
744 {
745 /* Nothing special */ 685 /* Nothing special */
746 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), 686 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
747 -1, 0, opt, ctx); 687 -1, 0, opt, ctx);
748 if (!ret) 688 if (!ret) {
749 {
750 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 689 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
751 ERR_R_NESTED_ASN1_ERROR); 690 ERR_R_NESTED_ASN1_ERROR);
752 goto err; 691 goto err;
@@ -775,32 +714,27 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
775 BUF_MEM buf; 714 BUF_MEM buf;
776 const unsigned char *cont = NULL; 715 const unsigned char *cont = NULL;
777 long len; 716 long len;
778 if (!pval) 717 if (!pval) {
779 {
780 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 718 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
781 return 0; /* Should never happen */ 719 return 0; /* Should never happen */
782 } 720 }
783 721
784 if (it->itype == ASN1_ITYPE_MSTRING) 722 if (it->itype == ASN1_ITYPE_MSTRING) {
785 {
786 utype = tag; 723 utype = tag;
787 tag = -1; 724 tag = -1;
788 } 725 }
789 else 726 else
790 utype = it->utype; 727 utype = it->utype;
791 728
792 if (utype == V_ASN1_ANY) 729 if (utype == V_ASN1_ANY) {
793 {
794 /* If type is ANY need to figure out type from tag */ 730 /* If type is ANY need to figure out type from tag */
795 unsigned char oclass; 731 unsigned char oclass;
796 if (tag >= 0) 732 if (tag >= 0) {
797 {
798 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
799 ASN1_R_ILLEGAL_TAGGED_ANY); 734 ASN1_R_ILLEGAL_TAGGED_ANY);
800 return 0; 735 return 0;
801 } 736 }
802 if (opt) 737 if (opt) {
803 {
804 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 738 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
805 ASN1_R_ILLEGAL_OPTIONAL_ANY); 739 ASN1_R_ILLEGAL_OPTIONAL_ANY);
806 return 0; 740 return 0;
@@ -808,8 +742,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
808 p = *in; 742 p = *in;
809 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, 743 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
810 &p, inlen, -1, 0, 0, ctx); 744 &p, inlen, -1, 0, 0, ctx);
811 if (!ret) 745 if (!ret) {
812 {
813 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 746 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
814 ERR_R_NESTED_ASN1_ERROR); 747 ERR_R_NESTED_ASN1_ERROR);
815 return 0; 748 return 0;
@@ -817,8 +750,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
817 if (oclass != V_ASN1_UNIVERSAL) 750 if (oclass != V_ASN1_UNIVERSAL)
818 utype = V_ASN1_OTHER; 751 utype = V_ASN1_OTHER;
819 } 752 }
820 if (tag == -1) 753 if (tag == -1) {
821 {
822 tag = utype; 754 tag = utype;
823 aclass = V_ASN1_UNIVERSAL; 755 aclass = V_ASN1_UNIVERSAL;
824 } 756 }
@@ -826,8 +758,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
826 /* Check header */ 758 /* Check header */
827 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, 759 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
828 &p, inlen, tag, aclass, opt, ctx); 760 &p, inlen, tag, aclass, opt, ctx);
829 if (!ret) 761 if (!ret) {
830 {
831 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 762 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
832 return 0; 763 return 0;
833 } 764 }
@@ -836,18 +767,15 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
836 ret = 0; 767 ret = 0;
837 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 768 /* SEQUENCE, SET and "OTHER" are left in encoded form */
838 if ((utype == V_ASN1_SEQUENCE) 769 if ((utype == V_ASN1_SEQUENCE)
839 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) 770 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
840 {
841 /* Clear context cache for type OTHER because the auto clear 771 /* Clear context cache for type OTHER because the auto clear
842 * when we have a exact match wont work 772 * when we have a exact match wont work
843 */ 773 */
844 if (utype == V_ASN1_OTHER) 774 if (utype == V_ASN1_OTHER) {
845 {
846 asn1_tlc_clear(ctx); 775 asn1_tlc_clear(ctx);
847 } 776 }
848 /* SEQUENCE and SET must be constructed */ 777 /* SEQUENCE and SET must be constructed */
849 else if (!cst) 778 else if (!cst) {
850 {
851 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
852 ASN1_R_TYPE_NOT_CONSTRUCTED); 780 ASN1_R_TYPE_NOT_CONSTRUCTED);
853 return 0; 781 return 0;
@@ -855,21 +783,16 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
855 783
856 cont = *in; 784 cont = *in;
857 /* If indefinite length constructed find the real end */ 785 /* If indefinite length constructed find the real end */
858 if (inf) 786 if (inf) {
859 {
860 if (!asn1_find_end(&p, plen, inf)) 787 if (!asn1_find_end(&p, plen, inf))
861 goto err; 788 goto err;
862 len = p - cont; 789 len = p - cont;
863 } 790 } else {
864 else
865 {
866 len = p - cont + plen; 791 len = p - cont + plen;
867 p += plen; 792 p += plen;
868 buf.data = NULL; 793 buf.data = NULL;
869 } 794 }
870 } 795 } else if (cst) {
871 else if (cst)
872 {
873 buf.length = 0; 796 buf.length = 0;
874 buf.max = 0; 797 buf.max = 0;
875 buf.data = NULL; 798 buf.data = NULL;
@@ -879,15 +802,13 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
879 * internally irrespective of the type. So instead just check 802 * internally irrespective of the type. So instead just check
880 * for UNIVERSAL class and ignore the tag. 803 * for UNIVERSAL class and ignore the tag.
881 */ 804 */
882 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) 805 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
883 {
884 free_cont = 1; 806 free_cont = 1;
885 goto err; 807 goto err;
886 } 808 }
887 len = buf.length; 809 len = buf.length;
888 /* Append a final null to string */ 810 /* Append a final null to string */
889 if (!BUF_MEM_grow_clean(&buf, len + 1)) 811 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
890 {
891 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
892 ERR_R_MALLOC_FAILURE); 813 ERR_R_MALLOC_FAILURE);
893 return 0; 814 return 0;
@@ -895,9 +816,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
895 buf.data[len] = 0; 816 buf.data[len] = 0;
896 cont = (const unsigned char *)buf.data; 817 cont = (const unsigned char *)buf.data;
897 free_cont = 1; 818 free_cont = 1;
898 } 819 } else {
899 else
900 {
901 cont = p; 820 cont = p;
902 len = plen; 821 len = plen;
903 p += plen; 822 p += plen;
@@ -930,10 +849,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
930 if (pf && pf->prim_c2i) 849 if (pf && pf->prim_c2i)
931 return pf->prim_c2i(pval, cont, len, utype, free_cont, it); 850 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
932 /* If ANY type clear type and set pointer to internal value */ 851 /* If ANY type clear type and set pointer to internal value */
933 if (it->utype == V_ASN1_ANY) 852 if (it->utype == V_ASN1_ANY) {
934 { 853 if (!*pval) {
935 if (!*pval)
936 {
937 typ = ASN1_TYPE_new(); 854 typ = ASN1_TYPE_new();
938 if (typ == NULL) 855 if (typ == NULL)
939 goto err; 856 goto err;
@@ -947,16 +864,14 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
947 opval = pval; 864 opval = pval;
948 pval = &typ->value.asn1_value; 865 pval = &typ->value.asn1_value;
949 } 866 }
950 switch(utype) 867 switch(utype) {
951 { 868 case V_ASN1_OBJECT:
952 case V_ASN1_OBJECT:
953 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) 869 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
954 goto err; 870 goto err;
955 break; 871 break;
956 872
957 case V_ASN1_NULL: 873 case V_ASN1_NULL:
958 if (len) 874 if (len) {
959 {
960 ASN1err(ASN1_F_ASN1_EX_C2I, 875 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ASN1_R_NULL_IS_WRONG_LENGTH); 876 ASN1_R_NULL_IS_WRONG_LENGTH);
962 goto err; 877 goto err;
@@ -964,30 +879,27 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
964 *pval = (ASN1_VALUE *)1; 879 *pval = (ASN1_VALUE *)1;
965 break; 880 break;
966 881
967 case V_ASN1_BOOLEAN: 882 case V_ASN1_BOOLEAN:
968 if (len != 1) 883 if (len != 1) {
969 {
970 ASN1err(ASN1_F_ASN1_EX_C2I, 884 ASN1err(ASN1_F_ASN1_EX_C2I,
971 ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 885 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
972 goto err; 886 goto err;
973 } 887 } else {
974 else
975 {
976 ASN1_BOOLEAN *tbool; 888 ASN1_BOOLEAN *tbool;
977 tbool = (ASN1_BOOLEAN *)pval; 889 tbool = (ASN1_BOOLEAN *)pval;
978 *tbool = *cont; 890 *tbool = *cont;
979 } 891 }
980 break; 892 break;
981 893
982 case V_ASN1_BIT_STRING: 894 case V_ASN1_BIT_STRING:
983 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) 895 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
984 goto err; 896 goto err;
985 break; 897 break;
986 898
987 case V_ASN1_INTEGER: 899 case V_ASN1_INTEGER:
988 case V_ASN1_NEG_INTEGER: 900 case V_ASN1_NEG_INTEGER:
989 case V_ASN1_ENUMERATED: 901 case V_ASN1_ENUMERATED:
990 case V_ASN1_NEG_ENUMERATED: 902 case V_ASN1_NEG_ENUMERATED:
991 tint = (ASN1_INTEGER **)pval; 903 tint = (ASN1_INTEGER **)pval;
992 if (!c2i_ASN1_INTEGER(tint, &cont, len)) 904 if (!c2i_ASN1_INTEGER(tint, &cont, len))
993 goto err; 905 goto err;
@@ -995,66 +907,56 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
995 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); 907 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
996 break; 908 break;
997 909
998 case V_ASN1_OCTET_STRING: 910 case V_ASN1_OCTET_STRING:
999 case V_ASN1_NUMERICSTRING: 911 case V_ASN1_NUMERICSTRING:
1000 case V_ASN1_PRINTABLESTRING: 912 case V_ASN1_PRINTABLESTRING:
1001 case V_ASN1_T61STRING: 913 case V_ASN1_T61STRING:
1002 case V_ASN1_VIDEOTEXSTRING: 914 case V_ASN1_VIDEOTEXSTRING:
1003 case V_ASN1_IA5STRING: 915 case V_ASN1_IA5STRING:
1004 case V_ASN1_UTCTIME: 916 case V_ASN1_UTCTIME:
1005 case V_ASN1_GENERALIZEDTIME: 917 case V_ASN1_GENERALIZEDTIME:
1006 case V_ASN1_GRAPHICSTRING: 918 case V_ASN1_GRAPHICSTRING:
1007 case V_ASN1_VISIBLESTRING: 919 case V_ASN1_VISIBLESTRING:
1008 case V_ASN1_GENERALSTRING: 920 case V_ASN1_GENERALSTRING:
1009 case V_ASN1_UNIVERSALSTRING: 921 case V_ASN1_UNIVERSALSTRING:
1010 case V_ASN1_BMPSTRING: 922 case V_ASN1_BMPSTRING:
1011 case V_ASN1_UTF8STRING: 923 case V_ASN1_UTF8STRING:
1012 case V_ASN1_OTHER: 924 case V_ASN1_OTHER:
1013 case V_ASN1_SET: 925 case V_ASN1_SET:
1014 case V_ASN1_SEQUENCE: 926 case V_ASN1_SEQUENCE:
1015 default: 927 default:
1016 if (utype == V_ASN1_BMPSTRING && (len & 1)) 928 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
1017 {
1018 ASN1err(ASN1_F_ASN1_EX_C2I, 929 ASN1err(ASN1_F_ASN1_EX_C2I,
1019 ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 930 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1020 goto err; 931 goto err;
1021 } 932 }
1022 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) 933 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
1023 {
1024 ASN1err(ASN1_F_ASN1_EX_C2I, 934 ASN1err(ASN1_F_ASN1_EX_C2I,
1025 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 935 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1026 goto err; 936 goto err;
1027 } 937 }
1028 /* All based on ASN1_STRING and handled the same */ 938 /* All based on ASN1_STRING and handled the same */
1029 if (!*pval) 939 if (!*pval) {
1030 {
1031 stmp = ASN1_STRING_type_new(utype); 940 stmp = ASN1_STRING_type_new(utype);
1032 if (!stmp) 941 if (!stmp) {
1033 {
1034 ASN1err(ASN1_F_ASN1_EX_C2I, 942 ASN1err(ASN1_F_ASN1_EX_C2I,
1035 ERR_R_MALLOC_FAILURE); 943 ERR_R_MALLOC_FAILURE);
1036 goto err; 944 goto err;
1037 } 945 }
1038 *pval = (ASN1_VALUE *)stmp; 946 *pval = (ASN1_VALUE *)stmp;
1039 } 947 } else {
1040 else
1041 {
1042 stmp = (ASN1_STRING *)*pval; 948 stmp = (ASN1_STRING *)*pval;
1043 stmp->type = utype; 949 stmp->type = utype;
1044 } 950 }
1045 /* If we've already allocated a buffer use it */ 951 /* If we've already allocated a buffer use it */
1046 if (*free_cont) 952 if (*free_cont) {
1047 {
1048 if (stmp->data) 953 if (stmp->data)
1049 free(stmp->data); 954 free(stmp->data);
1050 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */ 955 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1051 stmp->length = len; 956 stmp->length = len;
1052 *free_cont = 0; 957 *free_cont = 0;
1053 } 958 } else {
1054 else 959 if (!ASN1_STRING_set(stmp, cont, len)) {
1055 {
1056 if (!ASN1_STRING_set(stmp, cont, len))
1057 {
1058 ASN1err(ASN1_F_ASN1_EX_C2I, 960 ASN1err(ASN1_F_ASN1_EX_C2I,
1059 ERR_R_MALLOC_FAILURE); 961 ERR_R_MALLOC_FAILURE);
1060 ASN1_STRING_free(stmp); 962 ASN1_STRING_free(stmp);
@@ -1070,8 +972,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
1070 972
1071 ret = 1; 973 ret = 1;
1072 err: 974 err:
1073 if (!ret) 975 if (!ret) {
1074 {
1075 ASN1_TYPE_free(typ); 976 ASN1_TYPE_free(typ);
1076 if (opval) 977 if (opval)
1077 *opval = NULL; 978 *opval = NULL;
@@ -1092,8 +993,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1092 long plen; 993 long plen;
1093 const unsigned char *p = *in, *q; 994 const unsigned char *p = *in, *q;
1094 /* If not indefinite length constructed just add length */ 995 /* If not indefinite length constructed just add length */
1095 if (inf == 0) 996 if (inf == 0) {
1096 {
1097 *in += len; 997 *in += len;
1098 return 1; 998 return 1;
1099 } 999 }
@@ -1103,10 +1003,8 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1103 * are encountered increment the expected eoc count otherwise just 1003 * are encountered increment the expected eoc count otherwise just
1104 * skip to the end of the data. 1004 * skip to the end of the data.
1105 */ 1005 */
1106 while (len > 0) 1006 while (len > 0) {
1107 { 1007 if(asn1_check_eoc(&p, len)) {
1108 if(asn1_check_eoc(&p, len))
1109 {
1110 expected_eoc--; 1008 expected_eoc--;
1111 if (expected_eoc == 0) 1009 if (expected_eoc == 0)
1112 break; 1010 break;
@@ -1116,8 +1014,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1116 q = p; 1014 q = p;
1117 /* Just read in a header: only care about the length */ 1015 /* Just read in a header: only care about the length */
1118 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, 1016 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1119 -1, 0, 0, NULL)) 1017 -1, 0, 0, NULL)) {
1120 {
1121 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); 1018 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1122 return 0; 1019 return 0;
1123 } 1020 }
@@ -1127,8 +1024,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1127 p += plen; 1024 p += plen;
1128 len -= p - q; 1025 len -= p - q;
1129 } 1026 }
1130 if (expected_eoc) 1027 if (expected_eoc) {
1131 {
1132 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC); 1028 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1133 return 0; 1029 return 0;
1134 } 1030 }
@@ -1161,21 +1057,17 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1161 inf &= 1; 1057 inf &= 1;
1162 /* If no buffer and not indefinite length constructed just pass over 1058 /* If no buffer and not indefinite length constructed just pass over
1163 * the encoded data */ 1059 * the encoded data */
1164 if (!buf && !inf) 1060 if (!buf && !inf) {
1165 {
1166 *in += len; 1061 *in += len;
1167 return 1; 1062 return 1;
1168 } 1063 }
1169 while(len > 0) 1064 while(len > 0) {
1170 {
1171 q = p; 1065 q = p;
1172 /* Check for EOC */ 1066 /* Check for EOC */
1173 if (asn1_check_eoc(&p, len)) 1067 if (asn1_check_eoc(&p, len)) {
1174 {
1175 /* EOC is illegal outside indefinite length 1068 /* EOC is illegal outside indefinite length
1176 * constructed form */ 1069 * constructed form */
1177 if (!inf) 1070 if (!inf) {
1178 {
1179 ASN1err(ASN1_F_ASN1_COLLECT, 1071 ASN1err(ASN1_F_ASN1_COLLECT,
1180 ASN1_R_UNEXPECTED_EOC); 1072 ASN1_R_UNEXPECTED_EOC);
1181 return 0; 1073 return 0;
@@ -1185,17 +1077,14 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1185 } 1077 }
1186 1078
1187 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, 1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1188 len, tag, aclass, 0, NULL)) 1080 len, tag, aclass, 0, NULL)) {
1189 {
1190 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1191 return 0; 1082 return 0;
1192 } 1083 }
1193 1084
1194 /* If indefinite length constructed update max length */ 1085 /* If indefinite length constructed update max length */
1195 if (cst) 1086 if (cst) {
1196 { 1087 if (depth >= ASN1_MAX_STRING_NEST) {
1197 if (depth >= ASN1_MAX_STRING_NEST)
1198 {
1199 ASN1err(ASN1_F_ASN1_COLLECT, 1088 ASN1err(ASN1_F_ASN1_COLLECT,
1200 ASN1_R_NESTED_ASN1_STRING); 1089 ASN1_R_NESTED_ASN1_STRING);
1201 return 0; 1090 return 0;
@@ -1208,8 +1097,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1208 return 0; 1097 return 0;
1209 len -= p - q; 1098 len -= p - q;
1210 } 1099 }
1211 if (inf) 1100 if (inf) {
1212 {
1213 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC); 1101 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1214 return 0; 1102 return 0;
1215 } 1103 }
@@ -1220,11 +1108,9 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1220static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) 1108static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1221{ 1109{
1222 int len; 1110 int len;
1223 if (buf) 1111 if (buf) {
1224 {
1225 len = buf->length; 1112 len = buf->length;
1226 if (!BUF_MEM_grow_clean(buf, len + plen)) 1113 if (!BUF_MEM_grow_clean(buf, len + plen)) {
1227 {
1228 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE); 1114 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1229 return 0; 1115 return 0;
1230 } 1116 }
@@ -1241,8 +1127,7 @@ static int asn1_check_eoc(const unsigned char **in, long len)
1241 const unsigned char *p; 1127 const unsigned char *p;
1242 if (len < 2) return 0; 1128 if (len < 2) return 0;
1243 p = *in; 1129 p = *in;
1244 if (!p[0] && !p[1]) 1130 if (!p[0] && !p[1]) {
1245 {
1246 *in += 2; 1131 *in += 2;
1247 return 1; 1132 return 1;
1248 } 1133 }
@@ -1269,19 +1154,15 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1269 p = *in; 1154 p = *in;
1270 q = p; 1155 q = p;
1271 1156
1272 if (ctx && ctx->valid) 1157 if (ctx && ctx->valid) {
1273 {
1274 i = ctx->ret; 1158 i = ctx->ret;
1275 plen = ctx->plen; 1159 plen = ctx->plen;
1276 pclass = ctx->pclass; 1160 pclass = ctx->pclass;
1277 ptag = ctx->ptag; 1161 ptag = ctx->ptag;
1278 p += ctx->hdrlen; 1162 p += ctx->hdrlen;
1279 } 1163 } else {
1280 else
1281 {
1282 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); 1164 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1283 if (ctx) 1165 if (ctx) {
1284 {
1285 ctx->ret = i; 1166 ctx->ret = i;
1286 ctx->plen = plen; 1167 ctx->plen = plen;
1287 ctx->pclass = pclass; 1168 ctx->pclass = pclass;
@@ -1291,8 +1172,7 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1291 /* If definite length, and no error, length + 1172 /* If definite length, and no error, length +
1292 * header can't exceed total amount of data available. 1173 * header can't exceed total amount of data available.
1293 */ 1174 */
1294 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) 1175 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1295 {
1296 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 1176 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1297 ASN1_R_TOO_LONG); 1177 ASN1_R_TOO_LONG);
1298 asn1_tlc_clear(ctx); 1178 asn1_tlc_clear(ctx);
@@ -1301,16 +1181,13 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1301 } 1181 }
1302 } 1182 }
1303 1183
1304 if (i & 0x80) 1184 if (i & 0x80) {
1305 {
1306 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER); 1185 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1307 asn1_tlc_clear(ctx); 1186 asn1_tlc_clear(ctx);
1308 return 0; 1187 return 0;
1309 } 1188 }
1310 if (exptag >= 0) 1189 if (exptag >= 0) {
1311 { 1190 if ((exptag != ptag) || (expclass != pclass)) {
1312 if ((exptag != ptag) || (expclass != pclass))
1313 {
1314 /* If type is OPTIONAL, not an error: 1191 /* If type is OPTIONAL, not an error:
1315 * indicate missing type. 1192 * indicate missing type.
1316 */ 1193 */