summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_dec.c
diff options
context:
space:
mode:
authortedu <>2014-04-18 00:10:08 +0000
committertedu <>2014-04-18 00:10:08 +0000
commit07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d (patch)
tree6327d50d69a1982f840dc68fe928ea459e2c41e0 /src/lib/libcrypto/asn1/tasn_dec.c
parent288a9e368d9d4a72792b12a00ad69e3592d94073 (diff)
downloadopenbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.tar.gz
openbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.tar.bz2
openbsd-07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d.zip
putting most of the braces in the right column is the very least we can do.
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_dec.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c468
1 files changed, 234 insertions, 234 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index c594db9140..0df42a4a93 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -103,13 +103,13 @@ B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ 103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ 104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */ 105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
106 }; 106};
107 107
108unsigned long ASN1_tag2bit(int tag) 108unsigned long ASN1_tag2bit(int tag)
109 { 109{
110 if ((tag < 0) || (tag > 30)) return 0; 110 if ((tag < 0) || (tag > 30)) return 0;
111 return tag2bit[tag]; 111 return tag2bit[tag];
112 } 112}
113 113
114/* Macro to initialize and invalidate the cache */ 114/* Macro to initialize and invalidate the cache */
115 115
@@ -127,7 +127,7 @@ unsigned long ASN1_tag2bit(int tag)
127 127
128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, 128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
129 const unsigned char **in, long len, const ASN1_ITEM *it) 129 const unsigned char **in, long len, const ASN1_ITEM *it)
130 { 130{
131 ASN1_TLC c; 131 ASN1_TLC c;
132 ASN1_VALUE *ptmpval = NULL; 132 ASN1_VALUE *ptmpval = NULL;
133 if (!pval) 133 if (!pval)
@@ -136,15 +136,15 @@ ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
137 return *pval; 137 return *pval;
138 return NULL; 138 return NULL;
139 } 139}
140 140
141int ASN1_template_d2i(ASN1_VALUE **pval, 141int ASN1_template_d2i(ASN1_VALUE **pval,
142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt) 142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
143 { 143{
144 ASN1_TLC c; 144 ASN1_TLC c;
145 asn1_tlc_clear_nc(&c); 145 asn1_tlc_clear_nc(&c);
146 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c); 146 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
147 } 147}
148 148
149 149
150/* Decode an item, taking care of IMPLICIT tagging, if any. 150/* Decode an item, taking care of IMPLICIT tagging, if any.
@@ -154,7 +154,7 @@ int ASN1_template_d2i(ASN1_VALUE **pval,
154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
155 const ASN1_ITEM *it, 155 const ASN1_ITEM *it,
156 int tag, int aclass, char opt, ASN1_TLC *ctx) 156 int tag, int aclass, char opt, ASN1_TLC *ctx)
157 { 157{
158 const ASN1_TEMPLATE *tt, *errtt = NULL; 158 const ASN1_TEMPLATE *tt, *errtt = NULL;
159 const ASN1_COMPAT_FUNCS *cf; 159 const ASN1_COMPAT_FUNCS *cf;
160 const ASN1_EXTERN_FUNCS *ef; 160 const ASN1_EXTERN_FUNCS *ef;
@@ -176,10 +176,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
176 else asn1_cb = 0; 176 else asn1_cb = 0;
177 177
178 switch(it->itype) 178 switch(it->itype)
179 { 179 {
180 case ASN1_ITYPE_PRIMITIVE: 180 case ASN1_ITYPE_PRIMITIVE:
181 if (it->templates) 181 if (it->templates)
182 { 182 {
183 /* tagging or OPTIONAL is currently illegal on an item 183 /* tagging or OPTIONAL is currently illegal on an item
184 * template because the flags can't get passed down. 184 * template because the flags can't get passed down.
185 * In practice this isn't a problem: we include the 185 * In practice this isn't a problem: we include the
@@ -187,14 +187,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
187 * template itself. 187 * template itself.
188 */ 188 */
189 if ((tag != -1) || opt) 189 if ((tag != -1) || opt)
190 { 190 {
191 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 191 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
192 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 192 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
193 goto err; 193 goto err;
194 } 194 }
195 return asn1_template_ex_d2i(pval, in, len, 195 return asn1_template_ex_d2i(pval, in, len,
196 it->templates, opt, ctx); 196 it->templates, opt, ctx);
197 } 197 }
198 return asn1_d2i_ex_primitive(pval, in, len, it, 198 return asn1_d2i_ex_primitive(pval, in, len, it,
199 tag, aclass, opt, ctx); 199 tag, aclass, opt, ctx);
200 break; 200 break;
@@ -205,31 +205,31 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, 205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx); 206 &p, len, -1, 0, 1, ctx);
207 if (!ret) 207 if (!ret)
208 { 208 {
209 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 209 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
210 ERR_R_NESTED_ASN1_ERROR); 210 ERR_R_NESTED_ASN1_ERROR);
211 goto err; 211 goto err;
212 } 212 }
213 213
214 /* Must be UNIVERSAL class */ 214 /* Must be UNIVERSAL class */
215 if (oclass != V_ASN1_UNIVERSAL) 215 if (oclass != V_ASN1_UNIVERSAL)
216 { 216 {
217 /* If OPTIONAL, assume this is OK */ 217 /* If OPTIONAL, assume this is OK */
218 if (opt) return -1; 218 if (opt) return -1;
219 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 219 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
220 ASN1_R_MSTRING_NOT_UNIVERSAL); 220 ASN1_R_MSTRING_NOT_UNIVERSAL);
221 goto err; 221 goto err;
222 } 222 }
223 /* Check tag matches bit map */ 223 /* Check tag matches bit map */
224 if (!(ASN1_tag2bit(otag) & it->utype)) 224 if (!(ASN1_tag2bit(otag) & it->utype))
225 { 225 {
226 /* If OPTIONAL, assume this is OK */ 226 /* If OPTIONAL, assume this is OK */
227 if (opt) 227 if (opt)
228 return -1; 228 return -1;
229 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 229 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
230 ASN1_R_MSTRING_WRONG_TAG); 230 ASN1_R_MSTRING_WRONG_TAG);
231 goto err; 231 goto err;
232 } 232 }
233 return asn1_d2i_ex_primitive(pval, in, len, 233 return asn1_d2i_ex_primitive(pval, in, len,
234 it, otag, 0, 0, ctx); 234 it, otag, 0, 0, ctx);
235 235
@@ -245,7 +245,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
245 245
246 /* If OPTIONAL see if it is there */ 246 /* If OPTIONAL see if it is there */
247 if (opt) 247 if (opt)
248 { 248 {
249 int exptag; 249 int exptag;
250 p = *in; 250 p = *in;
251 if (tag == -1) 251 if (tag == -1)
@@ -257,14 +257,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
257 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, 257 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
258 &p, len, exptag, aclass, 1, ctx); 258 &p, len, exptag, aclass, 1, ctx);
259 if (!ret) 259 if (!ret)
260 { 260 {
261 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 261 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
262 ERR_R_NESTED_ASN1_ERROR); 262 ERR_R_NESTED_ASN1_ERROR);
263 goto err; 263 goto err;
264 } 264 }
265 if (ret == -1) 265 if (ret == -1)
266 return -1; 266 return -1;
267 } 267 }
268 268
269 /* This is the old style evil hack IMPLICIT handling: 269 /* This is the old style evil hack IMPLICIT handling:
270 * since the underlying code is expecting a tag and 270 * since the underlying code is expecting a tag and
@@ -282,18 +282,18 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
282 */ 282 */
283 283
284 if (tag != -1) 284 if (tag != -1)
285 { 285 {
286 wp = *(unsigned char **)in; 286 wp = *(unsigned char **)in;
287 imphack = *wp; 287 imphack = *wp;
288 if (p == NULL) 288 if (p == NULL)
289 { 289 {
290 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 290 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
291 ERR_R_NESTED_ASN1_ERROR); 291 ERR_R_NESTED_ASN1_ERROR);
292 goto err; 292 goto err;
293 } 293 }
294 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) 294 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
295 | it->utype); 295 | it->utype);
296 } 296 }
297 297
298 ptmpval = cf->asn1_d2i(pval, in, len); 298 ptmpval = cf->asn1_d2i(pval, in, len);
299 299
@@ -313,15 +313,15 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
313 313
314 /* Allocate structure */ 314 /* Allocate structure */
315 if (!*pval && !ASN1_item_ex_new(pval, it)) 315 if (!*pval && !ASN1_item_ex_new(pval, it))
316 { 316 {
317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 317 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
318 ERR_R_NESTED_ASN1_ERROR); 318 ERR_R_NESTED_ASN1_ERROR);
319 goto err; 319 goto err;
320 } 320 }
321 /* CHOICE type, try each possibility in turn */ 321 /* CHOICE type, try each possibility in turn */
322 p = *in; 322 p = *in;
323 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) 323 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
324 { 324 {
325 pchptr = asn1_get_field_ptr(pval, tt); 325 pchptr = asn1_get_field_ptr(pval, tt);
326 /* We mark field as OPTIONAL so its absence 326 /* We mark field as OPTIONAL so its absence
327 * can be recognised. 327 * can be recognised.
@@ -338,22 +338,22 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
339 ERR_R_NESTED_ASN1_ERROR); 339 ERR_R_NESTED_ASN1_ERROR);
340 goto err; 340 goto err;
341 } 341 }
342 342
343 /* Did we fall off the end without reading anything? */ 343 /* Did we fall off the end without reading anything? */
344 if (i == it->tcount) 344 if (i == it->tcount)
345 { 345 {
346 /* If OPTIONAL, this is OK */ 346 /* If OPTIONAL, this is OK */
347 if (opt) 347 if (opt)
348 { 348 {
349 /* Free and zero it */ 349 /* Free and zero it */
350 ASN1_item_ex_free(pval, it); 350 ASN1_item_ex_free(pval, it);
351 return -1; 351 return -1;
352 } 352 }
353 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 353 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
354 ASN1_R_NO_MATCHING_CHOICE_TYPE); 354 ASN1_R_NO_MATCHING_CHOICE_TYPE);
355 goto err; 355 goto err;
356 } 356 }
357 357
358 asn1_set_choice_selector(pval, i, it); 358 asn1_set_choice_selector(pval, i, it);
359 *in = p; 359 *in = p;
@@ -368,48 +368,48 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
368 368
369 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ 369 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
370 if (tag == -1) 370 if (tag == -1)
371 { 371 {
372 tag = V_ASN1_SEQUENCE; 372 tag = V_ASN1_SEQUENCE;
373 aclass = V_ASN1_UNIVERSAL; 373 aclass = V_ASN1_UNIVERSAL;
374 } 374 }
375 /* Get SEQUENCE length and update len, p */ 375 /* Get SEQUENCE length and update len, p */
376 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, 376 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
377 &p, len, tag, aclass, opt, ctx); 377 &p, len, tag, aclass, opt, ctx);
378 if (!ret) 378 if (!ret)
379 { 379 {
380 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 380 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
381 ERR_R_NESTED_ASN1_ERROR); 381 ERR_R_NESTED_ASN1_ERROR);
382 goto err; 382 goto err;
383 } 383 }
384 else if (ret == -1) 384 else if (ret == -1)
385 return -1; 385 return -1;
386 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) 386 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
387 { 387 {
388 len = tmplen - (p - *in); 388 len = tmplen - (p - *in);
389 seq_nolen = 1; 389 seq_nolen = 1;
390 } 390 }
391 /* If indefinite we don't do a length check */ 391 /* If indefinite we don't do a length check */
392 else seq_nolen = seq_eoc; 392 else seq_nolen = seq_eoc;
393 if (!cst) 393 if (!cst)
394 { 394 {
395 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 395 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
396 ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 396 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
397 goto err; 397 goto err;
398 } 398 }
399 399
400 if (!*pval && !ASN1_item_ex_new(pval, it)) 400 if (!*pval && !ASN1_item_ex_new(pval, it))
401 { 401 {
402 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 402 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
403 ERR_R_NESTED_ASN1_ERROR); 403 ERR_R_NESTED_ASN1_ERROR);
404 goto err; 404 goto err;
405 } 405 }
406 406
407 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 407 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
408 goto auxerr; 408 goto auxerr;
409 409
410 /* Get each field entry */ 410 /* Get each field entry */
411 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) 411 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
412 { 412 {
413 const ASN1_TEMPLATE *seqtt; 413 const ASN1_TEMPLATE *seqtt;
414 ASN1_VALUE **pseqval; 414 ASN1_VALUE **pseqval;
415 seqtt = asn1_do_adb(pval, tt, 1); 415 seqtt = asn1_do_adb(pval, tt, 1);
@@ -421,18 +421,18 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
421 break; 421 break;
422 q = p; 422 q = p;
423 if (asn1_check_eoc(&p, len)) 423 if (asn1_check_eoc(&p, len))
424 { 424 {
425 if (!seq_eoc) 425 if (!seq_eoc)
426 { 426 {
427 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 427 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
428 ASN1_R_UNEXPECTED_EOC); 428 ASN1_R_UNEXPECTED_EOC);
429 goto err; 429 goto err;
430 } 430 }
431 len -= p - q; 431 len -= p - q;
432 seq_eoc = 0; 432 seq_eoc = 0;
433 q = p; 433 q = p;
434 break; 434 break;
435 } 435 }
436 /* This determines the OPTIONAL flag value. The field 436 /* This determines the OPTIONAL flag value. The field
437 * cannot be omitted if it is the last of a SEQUENCE 437 * cannot be omitted if it is the last of a SEQUENCE
438 * and there is still data to be read. This isn't 438 * and there is still data to be read. This isn't
@@ -448,60 +448,60 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
448 ret = asn1_template_ex_d2i(pseqval, &p, len, 448 ret = asn1_template_ex_d2i(pseqval, &p, len,
449 seqtt, isopt, ctx); 449 seqtt, isopt, ctx);
450 if (!ret) 450 if (!ret)
451 { 451 {
452 errtt = seqtt; 452 errtt = seqtt;
453 goto err; 453 goto err;
454 } 454 }
455 else if (ret == -1) 455 else if (ret == -1)
456 { 456 {
457 /* OPTIONAL component absent. 457 /* OPTIONAL component absent.
458 * Free and zero the field. 458 * Free and zero the field.
459 */ 459 */
460 ASN1_template_free(pseqval, seqtt); 460 ASN1_template_free(pseqval, seqtt);
461 continue; 461 continue;
462 } 462 }
463 /* Update length */ 463 /* Update length */
464 len -= p - q; 464 len -= p - q;
465 } 465 }
466 466
467 /* Check for EOC if expecting one */ 467 /* Check for EOC if expecting one */
468 if (seq_eoc && !asn1_check_eoc(&p, len)) 468 if (seq_eoc && !asn1_check_eoc(&p, len))
469 { 469 {
470 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC); 470 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
471 goto err; 471 goto err;
472 } 472 }
473 /* Check all data read */ 473 /* Check all data read */
474 if (!seq_nolen && len) 474 if (!seq_nolen && len)
475 { 475 {
476 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 476 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
477 ASN1_R_SEQUENCE_LENGTH_MISMATCH); 477 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
478 goto err; 478 goto err;
479 } 479 }
480 480
481 /* If we get here we've got no more data in the SEQUENCE, 481 /* If we get here we've got no more data in the SEQUENCE,
482 * however we may not have read all fields so check all 482 * however we may not have read all fields so check all
483 * remaining are OPTIONAL and clear any that are. 483 * remaining are OPTIONAL and clear any that are.
484 */ 484 */
485 for (; i < it->tcount; tt++, i++) 485 for (; i < it->tcount; tt++, i++)
486 { 486 {
487 const ASN1_TEMPLATE *seqtt; 487 const ASN1_TEMPLATE *seqtt;
488 seqtt = asn1_do_adb(pval, tt, 1); 488 seqtt = asn1_do_adb(pval, tt, 1);
489 if (!seqtt) 489 if (!seqtt)
490 goto err; 490 goto err;
491 if (seqtt->flags & ASN1_TFLG_OPTIONAL) 491 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
492 { 492 {
493 ASN1_VALUE **pseqval; 493 ASN1_VALUE **pseqval;
494 pseqval = asn1_get_field_ptr(pval, seqtt); 494 pseqval = asn1_get_field_ptr(pval, seqtt);
495 ASN1_template_free(pseqval, seqtt); 495 ASN1_template_free(pseqval, seqtt);
496 } 496 }
497 else 497 else
498 { 498 {
499 errtt = seqtt; 499 errtt = seqtt;
500 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 500 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
501 ASN1_R_FIELD_MISSING); 501 ASN1_R_FIELD_MISSING);
502 goto err; 502 goto err;
503 }
504 } 503 }
504 }
505 /* Save encoding */ 505 /* Save encoding */
506 if (!asn1_enc_save(pval, *in, p - *in, it)) 506 if (!asn1_enc_save(pval, *in, p - *in, it))
507 goto auxerr; 507 goto auxerr;
@@ -512,7 +512,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
512 512
513 default: 513 default:
514 return 0; 514 return 0;
515 } 515 }
516 auxerr: 516 auxerr:
517 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); 517 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
518 err: 518 err:
@@ -523,7 +523,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
523 else 523 else
524 ERR_add_error_data(2, "Type=", it->sname); 524 ERR_add_error_data(2, "Type=", it->sname);
525 return 0; 525 return 0;
526 } 526}
527 527
528/* Templates are handled with two separate functions. 528/* Templates are handled with two separate functions.
529 * One handles any EXPLICIT tag and the other handles the rest. 529 * One handles any EXPLICIT tag and the other handles the rest.
@@ -533,7 +533,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
533 const unsigned char **in, long inlen, 533 const unsigned char **in, long inlen,
534 const ASN1_TEMPLATE *tt, char opt, 534 const ASN1_TEMPLATE *tt, char opt,
535 ASN1_TLC *ctx) 535 ASN1_TLC *ctx)
536 { 536{
537 int flags, aclass; 537 int flags, aclass;
538 int ret; 538 int ret;
539 long len; 539 long len;
@@ -548,7 +548,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
548 548
549 /* Check if EXPLICIT tag expected */ 549 /* Check if EXPLICIT tag expected */
550 if (flags & ASN1_TFLG_EXPTAG) 550 if (flags & ASN1_TFLG_EXPTAG)
551 { 551 {
552 char cst; 552 char cst;
553 /* Need to work out amount of data available to the inner 553 /* Need to work out amount of data available to the inner
554 * content and where it starts: so read in EXPLICIT header to 554 * content and where it starts: so read in EXPLICIT header to
@@ -558,51 +558,51 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
558 &p, inlen, tt->tag, aclass, opt, ctx); 558 &p, inlen, tt->tag, aclass, opt, ctx);
559 q = p; 559 q = p;
560 if (!ret) 560 if (!ret)
561 { 561 {
562 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 562 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
563 ERR_R_NESTED_ASN1_ERROR); 563 ERR_R_NESTED_ASN1_ERROR);
564 return 0; 564 return 0;
565 } 565 }
566 else if (ret == -1) 566 else if (ret == -1)
567 return -1; 567 return -1;
568 if (!cst) 568 if (!cst)
569 { 569 {
570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 570 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
571 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 571 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
572 return 0; 572 return 0;
573 } 573 }
574 /* We've found the field so it can't be OPTIONAL now */ 574 /* We've found the field so it can't be OPTIONAL now */
575 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 575 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
576 if (!ret) 576 if (!ret)
577 { 577 {
578 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 578 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
579 ERR_R_NESTED_ASN1_ERROR); 579 ERR_R_NESTED_ASN1_ERROR);
580 return 0; 580 return 0;
581 } 581 }
582 /* We read the field in OK so update length */ 582 /* We read the field in OK so update length */
583 len -= p - q; 583 len -= p - q;
584 if (exp_eoc) 584 if (exp_eoc)
585 { 585 {
586 /* If NDEF we must have an EOC here */ 586 /* If NDEF we must have an EOC here */
587 if (!asn1_check_eoc(&p, len)) 587 if (!asn1_check_eoc(&p, len))
588 { 588 {
589 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 589 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
590 ASN1_R_MISSING_EOC); 590 ASN1_R_MISSING_EOC);
591 goto err; 591 goto err;
592 }
593 } 592 }
593 }
594 else 594 else
595 { 595 {
596 /* Otherwise we must hit the EXPLICIT tag end or its 596 /* Otherwise we must hit the EXPLICIT tag end or its
597 * an error */ 597 * an error */
598 if (len) 598 if (len)
599 { 599 {
600 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 600 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
601 ASN1_R_EXPLICIT_LENGTH_MISMATCH); 601 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
602 goto err; 602 goto err;
603 }
604 } 603 }
605 } 604 }
605 }
606 else 606 else
607 return asn1_template_noexp_d2i(val, in, inlen, 607 return asn1_template_noexp_d2i(val, in, inlen,
608 tt, opt, ctx); 608 tt, opt, ctx);
@@ -613,13 +613,13 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
613 err: 613 err:
614 ASN1_template_free(val, tt); 614 ASN1_template_free(val, tt);
615 return 0; 615 return 0;
616 } 616}
617 617
618static int asn1_template_noexp_d2i(ASN1_VALUE **val, 618static int asn1_template_noexp_d2i(ASN1_VALUE **val,
619 const unsigned char **in, long len, 619 const unsigned char **in, long len,
620 const ASN1_TEMPLATE *tt, char opt, 620 const ASN1_TEMPLATE *tt, char opt,
621 ASN1_TLC *ctx) 621 ASN1_TLC *ctx)
622 { 622{
623 int flags, aclass; 623 int flags, aclass;
624 int ret; 624 int ret;
625 const unsigned char *p, *q; 625 const unsigned char *p, *q;
@@ -632,128 +632,128 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
632 q = p; 632 q = p;
633 633
634 if (flags & ASN1_TFLG_SK_MASK) 634 if (flags & ASN1_TFLG_SK_MASK)
635 { 635 {
636 /* SET OF, SEQUENCE OF */ 636 /* SET OF, SEQUENCE OF */
637 int sktag, skaclass; 637 int sktag, skaclass;
638 char sk_eoc; 638 char sk_eoc;
639 /* First work out expected inner tag value */ 639 /* First work out expected inner tag value */
640 if (flags & ASN1_TFLG_IMPTAG) 640 if (flags & ASN1_TFLG_IMPTAG)
641 { 641 {
642 sktag = tt->tag; 642 sktag = tt->tag;
643 skaclass = aclass; 643 skaclass = aclass;
644 } 644 }
645 else 645 else
646 { 646 {
647 skaclass = V_ASN1_UNIVERSAL; 647 skaclass = V_ASN1_UNIVERSAL;
648 if (flags & ASN1_TFLG_SET_OF) 648 if (flags & ASN1_TFLG_SET_OF)
649 sktag = V_ASN1_SET; 649 sktag = V_ASN1_SET;
650 else 650 else
651 sktag = V_ASN1_SEQUENCE; 651 sktag = V_ASN1_SEQUENCE;
652 } 652 }
653 /* Get the tag */ 653 /* Get the tag */
654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, 654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
655 &p, len, sktag, skaclass, opt, ctx); 655 &p, len, sktag, skaclass, opt, ctx);
656 if (!ret) 656 if (!ret)
657 { 657 {
658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 658 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
659 ERR_R_NESTED_ASN1_ERROR); 659 ERR_R_NESTED_ASN1_ERROR);
660 return 0; 660 return 0;
661 } 661 }
662 else if (ret == -1) 662 else if (ret == -1)
663 return -1; 663 return -1;
664 if (!*val) 664 if (!*val)
665 *val = (ASN1_VALUE *)sk_new_null(); 665 *val = (ASN1_VALUE *)sk_new_null();
666 else 666 else
667 { 667 {
668 /* We've got a valid STACK: free up any items present */ 668 /* We've got a valid STACK: free up any items present */
669 STACK_OF(ASN1_VALUE) *sktmp 669 STACK_OF(ASN1_VALUE) *sktmp
670 = (STACK_OF(ASN1_VALUE) *)*val; 670 = (STACK_OF(ASN1_VALUE) *)*val;
671 ASN1_VALUE *vtmp; 671 ASN1_VALUE *vtmp;
672 while(sk_ASN1_VALUE_num(sktmp) > 0) 672 while(sk_ASN1_VALUE_num(sktmp) > 0)
673 { 673 {
674 vtmp = sk_ASN1_VALUE_pop(sktmp); 674 vtmp = sk_ASN1_VALUE_pop(sktmp);
675 ASN1_item_ex_free(&vtmp, 675 ASN1_item_ex_free(&vtmp,
676 ASN1_ITEM_ptr(tt->item)); 676 ASN1_ITEM_ptr(tt->item));
677 }
678 } 677 }
678 }
679 679
680 if (!*val) 680 if (!*val)
681 { 681 {
682 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 682 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
683 ERR_R_MALLOC_FAILURE); 683 ERR_R_MALLOC_FAILURE);
684 goto err; 684 goto err;
685 } 685 }
686 686
687 /* Read as many items as we can */ 687 /* Read as many items as we can */
688 while(len > 0) 688 while(len > 0)
689 { 689 {
690 ASN1_VALUE *skfield; 690 ASN1_VALUE *skfield;
691 q = p; 691 q = p;
692 /* See if EOC found */ 692 /* See if EOC found */
693 if (asn1_check_eoc(&p, len)) 693 if (asn1_check_eoc(&p, len))
694 { 694 {
695 if (!sk_eoc) 695 if (!sk_eoc)
696 { 696 {
697 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 697 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
698 ASN1_R_UNEXPECTED_EOC); 698 ASN1_R_UNEXPECTED_EOC);
699 goto err; 699 goto err;
700 } 700 }
701 len -= p - q; 701 len -= p - q;
702 sk_eoc = 0; 702 sk_eoc = 0;
703 break; 703 break;
704 } 704 }
705 skfield = NULL; 705 skfield = NULL;
706 if (!ASN1_item_ex_d2i(&skfield, &p, len, 706 if (!ASN1_item_ex_d2i(&skfield, &p, len,
707 ASN1_ITEM_ptr(tt->item), 707 ASN1_ITEM_ptr(tt->item),
708 -1, 0, 0, ctx)) 708 -1, 0, 0, ctx))
709 { 709 {
710 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 710 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
711 ERR_R_NESTED_ASN1_ERROR); 711 ERR_R_NESTED_ASN1_ERROR);
712 goto err; 712 goto err;
713 } 713 }
714 len -= p - q; 714 len -= p - q;
715 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 715 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
716 skfield)) 716 skfield))
717 { 717 {
718 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 718 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
719 ERR_R_MALLOC_FAILURE); 719 ERR_R_MALLOC_FAILURE);
720 goto err; 720 goto err;
721 }
722 } 721 }
722 }
723 if (sk_eoc) 723 if (sk_eoc)
724 { 724 {
725 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); 725 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
726 goto err; 726 goto err;
727 }
728 } 727 }
728 }
729 else if (flags & ASN1_TFLG_IMPTAG) 729 else if (flags & ASN1_TFLG_IMPTAG)
730 { 730 {
731 /* IMPLICIT tagging */ 731 /* IMPLICIT tagging */
732 ret = ASN1_item_ex_d2i(val, &p, len, 732 ret = ASN1_item_ex_d2i(val, &p, len,
733 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 733 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
734 if (!ret) 734 if (!ret)
735 { 735 {
736 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 736 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
737 ERR_R_NESTED_ASN1_ERROR); 737 ERR_R_NESTED_ASN1_ERROR);
738 goto err; 738 goto err;
739 } 739 }
740 else if (ret == -1) 740 else if (ret == -1)
741 return -1; 741 return -1;
742 } 742 }
743 else 743 else
744 { 744 {
745 /* Nothing special */ 745 /* Nothing special */
746 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), 746 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
747 -1, 0, opt, ctx); 747 -1, 0, opt, ctx);
748 if (!ret) 748 if (!ret)
749 { 749 {
750 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 750 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
751 ERR_R_NESTED_ASN1_ERROR); 751 ERR_R_NESTED_ASN1_ERROR);
752 goto err; 752 goto err;
753 } 753 }
754 else if (ret == -1) 754 else if (ret == -1)
755 return -1; 755 return -1;
756 } 756 }
757 757
758 *in = p; 758 *in = p;
759 return 1; 759 return 1;
@@ -761,13 +761,13 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
761 err: 761 err:
762 ASN1_template_free(val, tt); 762 ASN1_template_free(val, tt);
763 return 0; 763 return 0;
764 } 764}
765 765
766static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, 766static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
767 const unsigned char **in, long inlen, 767 const unsigned char **in, long inlen,
768 const ASN1_ITEM *it, 768 const ASN1_ITEM *it,
769 int tag, int aclass, char opt, ASN1_TLC *ctx) 769 int tag, int aclass, char opt, ASN1_TLC *ctx)
770 { 770{
771 int ret = 0, utype; 771 int ret = 0, utype;
772 long plen; 772 long plen;
773 char cst, inf, free_cont = 0; 773 char cst, inf, free_cont = 0;
@@ -776,100 +776,100 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
776 const unsigned char *cont = NULL; 776 const unsigned char *cont = NULL;
777 long len; 777 long len;
778 if (!pval) 778 if (!pval)
779 { 779 {
780 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 780 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
781 return 0; /* Should never happen */ 781 return 0; /* Should never happen */
782 } 782 }
783 783
784 if (it->itype == ASN1_ITYPE_MSTRING) 784 if (it->itype == ASN1_ITYPE_MSTRING)
785 { 785 {
786 utype = tag; 786 utype = tag;
787 tag = -1; 787 tag = -1;
788 } 788 }
789 else 789 else
790 utype = it->utype; 790 utype = it->utype;
791 791
792 if (utype == V_ASN1_ANY) 792 if (utype == V_ASN1_ANY)
793 { 793 {
794 /* If type is ANY need to figure out type from tag */ 794 /* If type is ANY need to figure out type from tag */
795 unsigned char oclass; 795 unsigned char oclass;
796 if (tag >= 0) 796 if (tag >= 0)
797 { 797 {
798 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 798 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
799 ASN1_R_ILLEGAL_TAGGED_ANY); 799 ASN1_R_ILLEGAL_TAGGED_ANY);
800 return 0; 800 return 0;
801 } 801 }
802 if (opt) 802 if (opt)
803 { 803 {
804 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 804 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
805 ASN1_R_ILLEGAL_OPTIONAL_ANY); 805 ASN1_R_ILLEGAL_OPTIONAL_ANY);
806 return 0; 806 return 0;
807 } 807 }
808 p = *in; 808 p = *in;
809 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, 809 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
810 &p, inlen, -1, 0, 0, ctx); 810 &p, inlen, -1, 0, 0, ctx);
811 if (!ret) 811 if (!ret)
812 { 812 {
813 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 813 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
814 ERR_R_NESTED_ASN1_ERROR); 814 ERR_R_NESTED_ASN1_ERROR);
815 return 0; 815 return 0;
816 } 816 }
817 if (oclass != V_ASN1_UNIVERSAL) 817 if (oclass != V_ASN1_UNIVERSAL)
818 utype = V_ASN1_OTHER; 818 utype = V_ASN1_OTHER;
819 } 819 }
820 if (tag == -1) 820 if (tag == -1)
821 { 821 {
822 tag = utype; 822 tag = utype;
823 aclass = V_ASN1_UNIVERSAL; 823 aclass = V_ASN1_UNIVERSAL;
824 } 824 }
825 p = *in; 825 p = *in;
826 /* Check header */ 826 /* Check header */
827 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, 827 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
828 &p, inlen, tag, aclass, opt, ctx); 828 &p, inlen, tag, aclass, opt, ctx);
829 if (!ret) 829 if (!ret)
830 { 830 {
831 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 831 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
832 return 0; 832 return 0;
833 } 833 }
834 else if (ret == -1) 834 else if (ret == -1)
835 return -1; 835 return -1;
836 ret = 0; 836 ret = 0;
837 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 837 /* SEQUENCE, SET and "OTHER" are left in encoded form */
838 if ((utype == V_ASN1_SEQUENCE) 838 if ((utype == V_ASN1_SEQUENCE)
839 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) 839 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
840 { 840 {
841 /* Clear context cache for type OTHER because the auto clear 841 /* Clear context cache for type OTHER because the auto clear
842 * when we have a exact match wont work 842 * when we have a exact match wont work
843 */ 843 */
844 if (utype == V_ASN1_OTHER) 844 if (utype == V_ASN1_OTHER)
845 { 845 {
846 asn1_tlc_clear(ctx); 846 asn1_tlc_clear(ctx);
847 } 847 }
848 /* SEQUENCE and SET must be constructed */ 848 /* SEQUENCE and SET must be constructed */
849 else if (!cst) 849 else if (!cst)
850 { 850 {
851 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 851 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
852 ASN1_R_TYPE_NOT_CONSTRUCTED); 852 ASN1_R_TYPE_NOT_CONSTRUCTED);
853 return 0; 853 return 0;
854 } 854 }
855 855
856 cont = *in; 856 cont = *in;
857 /* If indefinite length constructed find the real end */ 857 /* If indefinite length constructed find the real end */
858 if (inf) 858 if (inf)
859 { 859 {
860 if (!asn1_find_end(&p, plen, inf)) 860 if (!asn1_find_end(&p, plen, inf))
861 goto err; 861 goto err;
862 len = p - cont; 862 len = p - cont;
863 } 863 }
864 else 864 else
865 { 865 {
866 len = p - cont + plen; 866 len = p - cont + plen;
867 p += plen; 867 p += plen;
868 buf.data = NULL; 868 buf.data = NULL;
869 }
870 } 869 }
870 }
871 else if (cst) 871 else if (cst)
872 { 872 {
873 buf.length = 0; 873 buf.length = 0;
874 buf.max = 0; 874 buf.max = 0;
875 buf.data = NULL; 875 buf.data = NULL;
@@ -880,28 +880,28 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
880 * for UNIVERSAL class and ignore the tag. 880 * for UNIVERSAL class and ignore the tag.
881 */ 881 */
882 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) 882 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
883 { 883 {
884 free_cont = 1; 884 free_cont = 1;
885 goto err; 885 goto err;
886 } 886 }
887 len = buf.length; 887 len = buf.length;
888 /* Append a final null to string */ 888 /* Append a final null to string */
889 if (!BUF_MEM_grow_clean(&buf, len + 1)) 889 if (!BUF_MEM_grow_clean(&buf, len + 1))
890 { 890 {
891 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 891 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
892 ERR_R_MALLOC_FAILURE); 892 ERR_R_MALLOC_FAILURE);
893 return 0; 893 return 0;
894 } 894 }
895 buf.data[len] = 0; 895 buf.data[len] = 0;
896 cont = (const unsigned char *)buf.data; 896 cont = (const unsigned char *)buf.data;
897 free_cont = 1; 897 free_cont = 1;
898 } 898 }
899 else 899 else
900 { 900 {
901 cont = p; 901 cont = p;
902 len = plen; 902 len = plen;
903 p += plen; 903 p += plen;
904 } 904 }
905 905
906 /* We now have content length and type: translate into a structure */ 906 /* We now have content length and type: translate into a structure */
907 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) 907 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
@@ -912,13 +912,13 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
912 err: 912 err:
913 if (free_cont && buf.data) free(buf.data); 913 if (free_cont && buf.data) free(buf.data);
914 return ret; 914 return ret;
915 } 915}
916 916
917/* Translate ASN1 content octets into a structure */ 917/* Translate ASN1 content octets into a structure */
918 918
919int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 919int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
920 int utype, char *free_cont, const ASN1_ITEM *it) 920 int utype, char *free_cont, const ASN1_ITEM *it)
921 { 921{
922 ASN1_VALUE **opval = NULL; 922 ASN1_VALUE **opval = NULL;
923 ASN1_STRING *stmp; 923 ASN1_STRING *stmp;
924 ASN1_TYPE *typ = NULL; 924 ASN1_TYPE *typ = NULL;
@@ -931,14 +931,14 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
931 return pf->prim_c2i(pval, cont, len, utype, free_cont, it); 931 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
932 /* If ANY type clear type and set pointer to internal value */ 932 /* If ANY type clear type and set pointer to internal value */
933 if (it->utype == V_ASN1_ANY) 933 if (it->utype == V_ASN1_ANY)
934 { 934 {
935 if (!*pval) 935 if (!*pval)
936 { 936 {
937 typ = ASN1_TYPE_new(); 937 typ = ASN1_TYPE_new();
938 if (typ == NULL) 938 if (typ == NULL)
939 goto err; 939 goto err;
940 *pval = (ASN1_VALUE *)typ; 940 *pval = (ASN1_VALUE *)typ;
941 } 941 }
942 else 942 else
943 typ = (ASN1_TYPE *)*pval; 943 typ = (ASN1_TYPE *)*pval;
944 944
@@ -946,9 +946,9 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
946 ASN1_TYPE_set(typ, utype, NULL); 946 ASN1_TYPE_set(typ, utype, NULL);
947 opval = pval; 947 opval = pval;
948 pval = &typ->value.asn1_value; 948 pval = &typ->value.asn1_value;
949 } 949 }
950 switch(utype) 950 switch(utype)
951 { 951 {
952 case V_ASN1_OBJECT: 952 case V_ASN1_OBJECT:
953 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) 953 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
954 goto err; 954 goto err;
@@ -956,27 +956,27 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
956 956
957 case V_ASN1_NULL: 957 case V_ASN1_NULL:
958 if (len) 958 if (len)
959 { 959 {
960 ASN1err(ASN1_F_ASN1_EX_C2I, 960 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ASN1_R_NULL_IS_WRONG_LENGTH); 961 ASN1_R_NULL_IS_WRONG_LENGTH);
962 goto err; 962 goto err;
963 } 963 }
964 *pval = (ASN1_VALUE *)1; 964 *pval = (ASN1_VALUE *)1;
965 break; 965 break;
966 966
967 case V_ASN1_BOOLEAN: 967 case V_ASN1_BOOLEAN:
968 if (len != 1) 968 if (len != 1)
969 { 969 {
970 ASN1err(ASN1_F_ASN1_EX_C2I, 970 ASN1err(ASN1_F_ASN1_EX_C2I,
971 ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 971 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
972 goto err; 972 goto err;
973 } 973 }
974 else 974 else
975 { 975 {
976 ASN1_BOOLEAN *tbool; 976 ASN1_BOOLEAN *tbool;
977 tbool = (ASN1_BOOLEAN *)pval; 977 tbool = (ASN1_BOOLEAN *)pval;
978 *tbool = *cont; 978 *tbool = *cont;
979 } 979 }
980 break; 980 break;
981 981
982 case V_ASN1_BIT_STRING: 982 case V_ASN1_BIT_STRING:
@@ -1014,56 +1014,56 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
1014 case V_ASN1_SEQUENCE: 1014 case V_ASN1_SEQUENCE:
1015 default: 1015 default:
1016 if (utype == V_ASN1_BMPSTRING && (len & 1)) 1016 if (utype == V_ASN1_BMPSTRING && (len & 1))
1017 { 1017 {
1018 ASN1err(ASN1_F_ASN1_EX_C2I, 1018 ASN1err(ASN1_F_ASN1_EX_C2I,
1019 ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 1019 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1020 goto err; 1020 goto err;
1021 } 1021 }
1022 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) 1022 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1023 { 1023 {
1024 ASN1err(ASN1_F_ASN1_EX_C2I, 1024 ASN1err(ASN1_F_ASN1_EX_C2I,
1025 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 1025 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1026 goto err; 1026 goto err;
1027 } 1027 }
1028 /* All based on ASN1_STRING and handled the same */ 1028 /* All based on ASN1_STRING and handled the same */
1029 if (!*pval) 1029 if (!*pval)
1030 { 1030 {
1031 stmp = ASN1_STRING_type_new(utype); 1031 stmp = ASN1_STRING_type_new(utype);
1032 if (!stmp) 1032 if (!stmp)
1033 { 1033 {
1034 ASN1err(ASN1_F_ASN1_EX_C2I, 1034 ASN1err(ASN1_F_ASN1_EX_C2I,
1035 ERR_R_MALLOC_FAILURE); 1035 ERR_R_MALLOC_FAILURE);
1036 goto err; 1036 goto err;
1037 }
1038 *pval = (ASN1_VALUE *)stmp;
1039 } 1037 }
1038 *pval = (ASN1_VALUE *)stmp;
1039 }
1040 else 1040 else
1041 { 1041 {
1042 stmp = (ASN1_STRING *)*pval; 1042 stmp = (ASN1_STRING *)*pval;
1043 stmp->type = utype; 1043 stmp->type = utype;
1044 } 1044 }
1045 /* If we've already allocated a buffer use it */ 1045 /* If we've already allocated a buffer use it */
1046 if (*free_cont) 1046 if (*free_cont)
1047 { 1047 {
1048 if (stmp->data) 1048 if (stmp->data)
1049 free(stmp->data); 1049 free(stmp->data);
1050 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */ 1050 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
1051 stmp->length = len; 1051 stmp->length = len;
1052 *free_cont = 0; 1052 *free_cont = 0;
1053 } 1053 }
1054 else 1054 else
1055 { 1055 {
1056 if (!ASN1_STRING_set(stmp, cont, len)) 1056 if (!ASN1_STRING_set(stmp, cont, len))
1057 { 1057 {
1058 ASN1err(ASN1_F_ASN1_EX_C2I, 1058 ASN1err(ASN1_F_ASN1_EX_C2I,
1059 ERR_R_MALLOC_FAILURE); 1059 ERR_R_MALLOC_FAILURE);
1060 ASN1_STRING_free(stmp); 1060 ASN1_STRING_free(stmp);
1061 *pval = NULL; 1061 *pval = NULL;
1062 goto err; 1062 goto err;
1063 }
1064 } 1063 }
1065 break;
1066 } 1064 }
1065 break;
1066 }
1067 /* If ASN1_ANY and NULL type fix up value */ 1067 /* If ASN1_ANY and NULL type fix up value */
1068 if (typ && (utype == V_ASN1_NULL)) 1068 if (typ && (utype == V_ASN1_NULL))
1069 typ->value.ptr = NULL; 1069 typ->value.ptr = NULL;
@@ -1071,13 +1071,13 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
1071 ret = 1; 1071 ret = 1;
1072 err: 1072 err:
1073 if (!ret) 1073 if (!ret)
1074 { 1074 {
1075 ASN1_TYPE_free(typ); 1075 ASN1_TYPE_free(typ);
1076 if (opval) 1076 if (opval)
1077 *opval = NULL; 1077 *opval = NULL;
1078 }
1079 return ret;
1080 } 1078 }
1079 return ret;
1080}
1081 1081
1082 1082
1083/* This function finds the end of an ASN1 structure when passed its maximum 1083/* This function finds the end of an ASN1 structure when passed its maximum
@@ -1087,16 +1087,16 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
1087 */ 1087 */
1088 1088
1089static int asn1_find_end(const unsigned char **in, long len, char inf) 1089static int asn1_find_end(const unsigned char **in, long len, char inf)
1090 { 1090{
1091 int expected_eoc; 1091 int expected_eoc;
1092 long plen; 1092 long plen;
1093 const unsigned char *p = *in, *q; 1093 const unsigned char *p = *in, *q;
1094 /* If not indefinite length constructed just add length */ 1094 /* If not indefinite length constructed just add length */
1095 if (inf == 0) 1095 if (inf == 0)
1096 { 1096 {
1097 *in += len; 1097 *in += len;
1098 return 1; 1098 return 1;
1099 } 1099 }
1100 expected_eoc = 1; 1100 expected_eoc = 1;
1101 /* Indefinite length constructed form. Find the end when enough EOCs 1101 /* Indefinite length constructed form. Find the end when enough EOCs
1102 * are found. If more indefinite length constructed headers 1102 * are found. If more indefinite length constructed headers
@@ -1104,37 +1104,37 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1104 * skip to the end of the data. 1104 * skip to the end of the data.
1105 */ 1105 */
1106 while (len > 0) 1106 while (len > 0)
1107 { 1107 {
1108 if(asn1_check_eoc(&p, len)) 1108 if(asn1_check_eoc(&p, len))
1109 { 1109 {
1110 expected_eoc--; 1110 expected_eoc--;
1111 if (expected_eoc == 0) 1111 if (expected_eoc == 0)
1112 break; 1112 break;
1113 len -= 2; 1113 len -= 2;
1114 continue; 1114 continue;
1115 } 1115 }
1116 q = p; 1116 q = p;
1117 /* Just read in a header: only care about the length */ 1117 /* Just read in a header: only care about the length */
1118 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, 1118 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1119 -1, 0, 0, NULL)) 1119 -1, 0, 0, NULL))
1120 { 1120 {
1121 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); 1121 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1122 return 0; 1122 return 0;
1123 } 1123 }
1124 if (inf) 1124 if (inf)
1125 expected_eoc++; 1125 expected_eoc++;
1126 else 1126 else
1127 p += plen; 1127 p += plen;
1128 len -= p - q; 1128 len -= p - q;
1129 } 1129 }
1130 if (expected_eoc) 1130 if (expected_eoc)
1131 { 1131 {
1132 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC); 1132 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1133 return 0; 1133 return 0;
1134 } 1134 }
1135 *in = p; 1135 *in = p;
1136 return 1; 1136 return 1;
1137 } 1137}
1138/* This function collects the asn1 data from a constructred string 1138/* This function collects the asn1 data from a constructred string
1139 * type into a buffer. The values of 'in' and 'len' should refer 1139 * type into a buffer. The values of 'in' and 'len' should refer
1140 * to the contents of the constructed type and 'inf' should be set 1140 * to the contents of the constructed type and 'inf' should be set
@@ -1153,7 +1153,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1153 1153
1154static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 1154static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1155 char inf, int tag, int aclass, int depth) 1155 char inf, int tag, int aclass, int depth)
1156 { 1156{
1157 const unsigned char *p, *q; 1157 const unsigned char *p, *q;
1158 long plen; 1158 long plen;
1159 char cst, ininf; 1159 char cst, ininf;
@@ -1162,92 +1162,92 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1162 /* If no buffer and not indefinite length constructed just pass over 1162 /* If no buffer and not indefinite length constructed just pass over
1163 * the encoded data */ 1163 * the encoded data */
1164 if (!buf && !inf) 1164 if (!buf && !inf)
1165 { 1165 {
1166 *in += len; 1166 *in += len;
1167 return 1; 1167 return 1;
1168 } 1168 }
1169 while(len > 0) 1169 while(len > 0)
1170 { 1170 {
1171 q = p; 1171 q = p;
1172 /* Check for EOC */ 1172 /* Check for EOC */
1173 if (asn1_check_eoc(&p, len)) 1173 if (asn1_check_eoc(&p, len))
1174 { 1174 {
1175 /* EOC is illegal outside indefinite length 1175 /* EOC is illegal outside indefinite length
1176 * constructed form */ 1176 * constructed form */
1177 if (!inf) 1177 if (!inf)
1178 { 1178 {
1179 ASN1err(ASN1_F_ASN1_COLLECT, 1179 ASN1err(ASN1_F_ASN1_COLLECT,
1180 ASN1_R_UNEXPECTED_EOC); 1180 ASN1_R_UNEXPECTED_EOC);
1181 return 0; 1181 return 0;
1182 } 1182 }
1183 inf = 0; 1183 inf = 0;
1184 break; 1184 break;
1185 } 1185 }
1186 1186
1187 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, 1187 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1188 len, tag, aclass, 0, NULL)) 1188 len, tag, aclass, 0, NULL))
1189 { 1189 {
1190 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1190 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1191 return 0; 1191 return 0;
1192 } 1192 }
1193 1193
1194 /* If indefinite length constructed update max length */ 1194 /* If indefinite length constructed update max length */
1195 if (cst) 1195 if (cst)
1196 { 1196 {
1197 if (depth >= ASN1_MAX_STRING_NEST) 1197 if (depth >= ASN1_MAX_STRING_NEST)
1198 { 1198 {
1199 ASN1err(ASN1_F_ASN1_COLLECT, 1199 ASN1err(ASN1_F_ASN1_COLLECT,
1200 ASN1_R_NESTED_ASN1_STRING); 1200 ASN1_R_NESTED_ASN1_STRING);
1201 return 0; 1201 return 0;
1202 } 1202 }
1203 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, 1203 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1204 depth + 1)) 1204 depth + 1))
1205 return 0; 1205 return 0;
1206 } 1206 }
1207 else if (plen && !collect_data(buf, &p, plen)) 1207 else if (plen && !collect_data(buf, &p, plen))
1208 return 0; 1208 return 0;
1209 len -= p - q; 1209 len -= p - q;
1210 } 1210 }
1211 if (inf) 1211 if (inf)
1212 { 1212 {
1213 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC); 1213 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1214 return 0; 1214 return 0;
1215 } 1215 }
1216 *in = p; 1216 *in = p;
1217 return 1; 1217 return 1;
1218 } 1218}
1219 1219
1220static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) 1220static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1221 { 1221{
1222 int len; 1222 int len;
1223 if (buf) 1223 if (buf)
1224 { 1224 {
1225 len = buf->length; 1225 len = buf->length;
1226 if (!BUF_MEM_grow_clean(buf, len + plen)) 1226 if (!BUF_MEM_grow_clean(buf, len + plen))
1227 { 1227 {
1228 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE); 1228 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1229 return 0; 1229 return 0;
1230 }
1231 memcpy(buf->data + len, *p, plen);
1232 } 1230 }
1231 memcpy(buf->data + len, *p, plen);
1232 }
1233 *p += plen; 1233 *p += plen;
1234 return 1; 1234 return 1;
1235 } 1235}
1236 1236
1237/* Check for ASN1 EOC and swallow it if found */ 1237/* Check for ASN1 EOC and swallow it if found */
1238 1238
1239static int asn1_check_eoc(const unsigned char **in, long len) 1239static int asn1_check_eoc(const unsigned char **in, long len)
1240 { 1240{
1241 const unsigned char *p; 1241 const unsigned char *p;
1242 if (len < 2) return 0; 1242 if (len < 2) return 0;
1243 p = *in; 1243 p = *in;
1244 if (!p[0] && !p[1]) 1244 if (!p[0] && !p[1])
1245 { 1245 {
1246 *in += 2; 1246 *in += 2;
1247 return 1; 1247 return 1;
1248 }
1249 return 0;
1250 } 1248 }
1249 return 0;
1250}
1251 1251
1252/* Check an ASN1 tag and length: a bit like ASN1_get_object 1252/* Check an ASN1 tag and length: a bit like ASN1_get_object
1253 * but it sets the length for indefinite length constructed 1253 * but it sets the length for indefinite length constructed
@@ -1261,7 +1261,7 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1261 const unsigned char **in, long len, 1261 const unsigned char **in, long len,
1262 int exptag, int expclass, char opt, 1262 int exptag, int expclass, char opt,
1263 ASN1_TLC *ctx) 1263 ASN1_TLC *ctx)
1264 { 1264{
1265 int i; 1265 int i;
1266 int ptag, pclass; 1266 int ptag, pclass;
1267 long plen; 1267 long plen;
@@ -1270,18 +1270,18 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1270 q = p; 1270 q = p;
1271 1271
1272 if (ctx && ctx->valid) 1272 if (ctx && ctx->valid)
1273 { 1273 {
1274 i = ctx->ret; 1274 i = ctx->ret;
1275 plen = ctx->plen; 1275 plen = ctx->plen;
1276 pclass = ctx->pclass; 1276 pclass = ctx->pclass;
1277 ptag = ctx->ptag; 1277 ptag = ctx->ptag;
1278 p += ctx->hdrlen; 1278 p += ctx->hdrlen;
1279 } 1279 }
1280 else 1280 else
1281 { 1281 {
1282 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); 1282 i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1283 if (ctx) 1283 if (ctx)
1284 { 1284 {
1285 ctx->ret = i; 1285 ctx->ret = i;
1286 ctx->plen = plen; 1286 ctx->plen = plen;
1287 ctx->pclass = pclass; 1287 ctx->pclass = pclass;
@@ -1292,25 +1292,25 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1292 * header can't exceed total amount of data available. 1292 * header can't exceed total amount of data available.
1293 */ 1293 */
1294 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) 1294 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
1295 { 1295 {
1296 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 1296 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1297 ASN1_R_TOO_LONG); 1297 ASN1_R_TOO_LONG);
1298 asn1_tlc_clear(ctx); 1298 asn1_tlc_clear(ctx);
1299 return 0; 1299 return 0;
1300 }
1301 } 1300 }
1302 } 1301 }
1302 }
1303 1303
1304 if (i & 0x80) 1304 if (i & 0x80)
1305 { 1305 {
1306 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER); 1306 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1307 asn1_tlc_clear(ctx); 1307 asn1_tlc_clear(ctx);
1308 return 0; 1308 return 0;
1309 } 1309 }
1310 if (exptag >= 0) 1310 if (exptag >= 0)
1311 { 1311 {
1312 if ((exptag != ptag) || (expclass != pclass)) 1312 if ((exptag != ptag) || (expclass != pclass))
1313 { 1313 {
1314 /* If type is OPTIONAL, not an error: 1314 /* If type is OPTIONAL, not an error:
1315 * indicate missing type. 1315 * indicate missing type.
1316 */ 1316 */
@@ -1318,11 +1318,11 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1318 asn1_tlc_clear(ctx); 1318 asn1_tlc_clear(ctx);
1319 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); 1319 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1320 return 0; 1320 return 0;
1321 } 1321 }
1322 /* We have a tag and class match: 1322 /* We have a tag and class match:
1323 * assume we are going to do something with it */ 1323 * assume we are going to do something with it */
1324 asn1_tlc_clear(ctx); 1324 asn1_tlc_clear(ctx);
1325 } 1325 }
1326 1326
1327 if (i & 1) 1327 if (i & 1)
1328 plen = len - (p - q); 1328 plen = len - (p - q);
@@ -1344,4 +1344,4 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1344 1344
1345 *in = p; 1345 *in = p;
1346 return 1; 1346 return 1;
1347 } 1347}