]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/tasn_prn.c
Update S/MIME code to use default digest.
[thirdparty/openssl.git] / crypto / asn1 / tasn_prn.c
CommitLineData
9d6b1ce6
DSH
1/* tasn_prn.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
56defd9a 6 * Copyright (c) 2000,2005 The OpenSSL Project. All rights reserved.
9d6b1ce6
DSH
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59
60#include <stddef.h>
c11c64fb 61#include "cryptlib.h"
9d6b1ce6 62#include <openssl/asn1.h>
56defd9a 63#include <openssl/asn1t.h>
9d6b1ce6
DSH
64#include <openssl/objects.h>
65#include <openssl/buffer.h>
66#include <openssl/err.h>
56defd9a
DSH
67#include <openssl/x509v3.h>
68#include "asn1_locl.h"
9d6b1ce6 69
56defd9a 70/* Print routines.
9d6b1ce6
DSH
71 */
72
56defd9a 73/* ASN1_PCTX routines */
9d6b1ce6 74
56defd9a
DSH
75ASN1_PCTX default_pctx =
76 {
77 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
78 0, /* nm_flags */
79 0, /* cert_flags */
80 0, /* oid_flags */
81 0 /* str_flags */
82 };
83
9d6b1ce6 84
56defd9a
DSH
85ASN1_PCTX *ASN1_PCTX_new(void)
86 {
87 ASN1_PCTX *ret;
88 ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
89 if (ret == NULL)
90 {
91 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
92 return NULL;
93 }
94 ret->flags = 0;
95 ret->nm_flags = 0;
96 ret->cert_flags = 0;
97 ret->oid_flags = 0;
98 ret->str_flags = 0;
99 return ret;
100 }
101
102void ASN1_PCTX_free(ASN1_PCTX *p)
103 {
104 OPENSSL_free(p);
105 }
106
107unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
108 {
109 return p->flags;
110 }
111
112void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
113 {
114 p->flags = flags;
115 }
116
117unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
118 {
119 return p->nm_flags;
120 }
121
122void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
123 {
124 p->nm_flags = flags;
125 }
126
127unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
128 {
129 return p->cert_flags;
130 }
131
132void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
133 {
134 p->cert_flags = flags;
135 }
136
137unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
138 {
139 return p->oid_flags;
140 }
141
142void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
143 {
144 p->oid_flags = flags;
145 }
146
147unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
148 {
149 return p->str_flags;
150 }
151
152void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
153 {
154 p->str_flags = flags;
155 }
156
157/* Main print routines */
158
159static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
b173acfc
DSH
160 const ASN1_ITEM *it,
161 const char *fname, const char *sname,
162 int nohdr, const ASN1_PCTX *pctx);
9194296d 163
56defd9a
DSH
164int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
165 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
9194296d 166
56defd9a
DSH
167static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
168 const ASN1_ITEM *it, int indent,
b173acfc 169 const char *fname, const char *sname,
56defd9a 170 const ASN1_PCTX *pctx);
9194296d 171
b173acfc
DSH
172static int asn1_print_fsname(BIO *out, int indent,
173 const char *fname, const char *sname,
174 const ASN1_PCTX *pctx);
56defd9a
DSH
175
176int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
177 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
178 {
b173acfc 179 const char *sname;
56defd9a
DSH
180 if (pctx == NULL)
181 pctx = &default_pctx;
b173acfc
DSH
182 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
183 sname = NULL;
184 else
185 sname = it->sname;
186 return asn1_item_print_ctx(out, &ifld, indent, it,
187 NULL, sname, 0, pctx);
56defd9a
DSH
188 }
189
190static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
b173acfc
DSH
191 const ASN1_ITEM *it,
192 const char *fname, const char *sname,
193 int nohdr, const ASN1_PCTX *pctx)
56defd9a 194 {
9d6b1ce6 195 const ASN1_TEMPLATE *tt;
56defd9a
DSH
196 const ASN1_EXTERN_FUNCS *ef;
197 ASN1_VALUE **tmpfld;
c11c64fb
DSH
198 const ASN1_AUX *aux = it->funcs;
199 ASN1_aux_cb *asn1_cb;
200 ASN1_PRINT_ARG parg;
9d6b1ce6 201 int i;
c11c64fb
DSH
202 if (aux && aux->asn1_cb)
203 {
204 parg.out = out;
205 parg.indent = indent;
206 parg.pctx = pctx;
207 asn1_cb = aux->asn1_cb;
208 }
209 else asn1_cb = 0;
210
56defd9a
DSH
211 if(*fld == NULL)
212 {
b173acfc 213 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
56defd9a 214 {
c11c64fb
DSH
215 if (!nohdr && !asn1_print_fsname(out, indent,
216 fname, sname, pctx))
56defd9a 217 return 0;
56defd9a
DSH
218 if (BIO_puts(out, "<ABSENT>\n") <= 0)
219 return 0;
220 }
9d6b1ce6 221 return 1;
56defd9a 222 }
9d6b1ce6 223
56defd9a
DSH
224 switch(it->itype)
225 {
9d6b1ce6
DSH
226 case ASN1_ITYPE_PRIMITIVE:
227 if(it->templates)
56defd9a
DSH
228 {
229 if (!asn1_template_print_ctx(out, fld, indent,
230 it->templates, pctx))
231 return 0;
232 }
233 /* fall thru */
9d6b1ce6 234 case ASN1_ITYPE_MSTRING:
b173acfc
DSH
235 if (!asn1_primitive_print(out, fld, it,
236 indent, fname, sname,pctx))
56defd9a
DSH
237 return 0;
238 break;
9d6b1ce6
DSH
239
240 case ASN1_ITYPE_EXTERN:
b173acfc
DSH
241 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
242 return 0;
56defd9a
DSH
243 /* Use new style print routine if possible */
244 ef = it->funcs;
245 if (ef && ef->asn1_ex_print)
246 {
b173acfc
DSH
247 i = ef->asn1_ex_print(out, fld, indent, "", pctx);
248 if (!i)
56defd9a 249 return 0;
b173acfc
DSH
250 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
251 return 0;
252 return 1;
56defd9a 253 }
b173acfc
DSH
254 else if (sname &&
255 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
56defd9a
DSH
256 return 0;
257 break;
9d6b1ce6
DSH
258
259 case ASN1_ITYPE_CHOICE:
b173acfc
DSH
260#if 0
261 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
262 return 0;
263#endif
9d6b1ce6
DSH
264 /* CHOICE type, get selector */
265 i = asn1_get_choice_selector(fld, it);
266 /* This should never happen... */
56defd9a
DSH
267 if((i < 0) || (i >= it->tcount))
268 {
269 if (BIO_printf(out,
270 "ERROR: selector [%d] invalid\n", i) <= 0)
271 return 0;
9d6b1ce6 272 return 1;
56defd9a 273 }
9d6b1ce6 274 tt = it->templates + i;
56defd9a
DSH
275 tmpfld = asn1_get_field_ptr(fld, tt);
276 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
277 return 0;
278 break;
9d6b1ce6
DSH
279
280 case ASN1_ITYPE_SEQUENCE:
56defd9a 281 case ASN1_ITYPE_NDEF_SEQUENCE:
b173acfc
DSH
282 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
283 return 0;
284 if (fname || sname)
56defd9a 285 {
b173acfc
DSH
286 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
287 {
288 if (BIO_puts(out, " {\n") <= 0)
289 return 0;
290 }
291 else
292 {
293 if (BIO_puts(out, "\n") <= 0)
294 return 0;
295 }
56defd9a
DSH
296 }
297
c11c64fb
DSH
298 if (asn1_cb)
299 {
300 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
301 if (i == 0)
302 return 0;
303 if (i == 2)
304 return 1;
305 }
306
56defd9a
DSH
307 /* Print each field entry */
308 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
309 {
310 const ASN1_TEMPLATE *seqtt;
311 seqtt = asn1_do_adb(fld, tt, 1);
312 tmpfld = asn1_get_field_ptr(fld, seqtt);
313 if (!asn1_template_print_ctx(out, tmpfld,
314 indent + 2, seqtt, pctx))
315 return 0;
316 }
317 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
318 {
319 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
320 return 0;
321 }
c11c64fb
DSH
322
323 if (asn1_cb)
324 {
325 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
326 if (i == 0)
327 return 0;
328 }
56defd9a 329 break;
9d6b1ce6
DSH
330
331 default:
56defd9a 332 BIO_printf(out, "Unprocessed type %d\n", it->itype);
9d6b1ce6 333 return 0;
56defd9a 334 }
b173acfc 335
56defd9a 336 return 1;
9d6b1ce6 337 }
9d6b1ce6 338
56defd9a
DSH
339int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
340 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
341 {
9d6b1ce6 342 int i, flags;
b173acfc 343 const char *sname, *fname;
9d6b1ce6 344 flags = tt->flags;
b173acfc 345 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
270da80b 346 sname = ASN1_ITEM_ptr(tt->item)->sname;
b173acfc
DSH
347 else
348 sname = NULL;
349 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
350 fname = NULL;
351 else
352 fname = tt->field_name;
56defd9a
DSH
353 if(flags & ASN1_TFLG_SK_MASK)
354 {
9d6b1ce6 355 char *tname;
56defd9a 356 ASN1_VALUE *skitem;
9d6b1ce6 357 /* SET OF, SEQUENCE OF */
b173acfc 358 if (fname)
56defd9a 359 {
b173acfc
DSH
360 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
361 {
362 if(flags & ASN1_TFLG_SET_OF)
363 tname = "SET";
364 else
365 tname = "SEQUENCE";
366 if (BIO_printf(out, "%*s%s OF %s {\n",
367 indent, "", tname, tt->field_name) <= 0)
368 return 0;
369 }
370 else if (BIO_printf(out, "%*s%s:\n", indent, "",
371 fname) <= 0)
56defd9a
DSH
372 return 0;
373 }
56defd9a
DSH
374 for(i = 0; i < sk_num((STACK *)*fld); i++)
375 {
b173acfc 376 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
56defd9a 377 return 0;
b173acfc 378
56defd9a
DSH
379 skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
380 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
270da80b 381 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
56defd9a
DSH
382 return 0;
383 }
b173acfc
DSH
384 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
385 return 0;
56defd9a
DSH
386 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
387 {
388 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
389 return 0;
9d6b1ce6 390 }
9d6b1ce6 391 return 1;
56defd9a 392 }
270da80b 393 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
b173acfc 394 fname, sname, 0, pctx);
9d6b1ce6 395 }
56defd9a 396
b173acfc 397static int asn1_print_fsname(BIO *out, int indent,
56defd9a
DSH
398 const char *fname, const char *sname,
399 const ASN1_PCTX *pctx)
400 {
401 static char spaces[] = " ";
402 const int nspaces = sizeof(spaces) - 1;
b173acfc
DSH
403
404#if 0
405 if (!sname && !fname)
406 return 1;
407#endif
408
56defd9a
DSH
409 while (indent > nspaces)
410 {
411 if (BIO_write(out, spaces, nspaces) != nspaces)
412 return 0;
413 indent -= nspaces;
9d6b1ce6 414 }
56defd9a
DSH
415 if (BIO_write(out, spaces, indent) != indent)
416 return 0;
b173acfc
DSH
417 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
418 sname = NULL;
419 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
420 fname = NULL;
421 if (!sname && !fname)
56defd9a 422 return 1;
b173acfc 423 if (fname)
56defd9a
DSH
424 {
425 if (BIO_puts(out, fname) <= 0)
426 return 0;
427 }
b173acfc 428 if (sname)
56defd9a 429 {
b173acfc
DSH
430 if (fname)
431 {
432 if (BIO_printf(out, " (%s)", sname) <= 0)
433 return 0;
434 }
435 else
56defd9a
DSH
436 {
437 if (BIO_puts(out, sname) <= 0)
438 return 0;
439 }
56defd9a 440 }
b173acfc 441 if (BIO_write(out, ": ", 2) != 2)
56defd9a
DSH
442 return 0;
443 return 1;
444 }
445
b173acfc
DSH
446static int asn1_print_boolean_ctx(BIO *out, const int bool,
447 const ASN1_PCTX *pctx)
56defd9a
DSH
448 {
449 const char *str;
450 switch (bool)
451 {
452 case -1:
453 str = "BOOL ABSENT";
454 break;
455
456 case 0:
457 str = "FALSE";
458 break;
459
460 default:
461 str = "TRUE";
462 break;
463
464 }
465
466 if (BIO_puts(out, str) <= 0)
467 return 0;
468 return 1;
469
470 }
471
472static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
473 const ASN1_PCTX *pctx)
474 {
475 char *s;
476 int ret = 1;
477 s = i2s_ASN1_INTEGER(NULL, str);
b173acfc 478 if (BIO_puts(out, s) <= 0)
56defd9a
DSH
479 ret = 0;
480 OPENSSL_free(s);
481 return ret;
482 }
483
484static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
485 const ASN1_PCTX *pctx)
486 {
487 char objbuf[80];
488 const char *ln;
489 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
490 if(!ln)
491 ln = "";
492 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
493 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
494 return 0;
495 return 1;
496 }
497
498static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
499 const ASN1_PCTX *pctx)
500 {
501 if (str->type == V_ASN1_BIT_STRING)
502 {
503 if (BIO_printf(out, " (%ld unused bits)\n",
504 str->flags & 0x7) <= 0)
505 return 0;
506 }
507 else if (BIO_puts(out, "\n") <= 0)
508 return 0;
f3481ca2
DSH
509 if ((str->length > 0)
510 && BIO_dump_indent(out, (char *)str->data, str->length,
56defd9a
DSH
511 indent + 2) <= 0)
512 return 0;
9d6b1ce6 513 return 1;
56defd9a
DSH
514 }
515
516static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
517 const ASN1_ITEM *it, int indent,
b173acfc 518 const char *fname, const char *sname,
56defd9a
DSH
519 const ASN1_PCTX *pctx)
520 {
521 long utype;
522 ASN1_STRING *str;
b173acfc
DSH
523 int ret = 1, needlf = 1;
524 const char *pname;
525 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
526 return 0;
56defd9a
DSH
527 str = (ASN1_STRING *)*fld;
528 if (it->itype == ASN1_ITYPE_MSTRING)
529 utype = str->type & ~V_ASN1_NEG;
530 else
531 utype = it->utype;
532 if (utype == V_ASN1_ANY)
533 {
534 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
535 utype = atype->type;
536 fld = (ASN1_VALUE **)&atype->value.ptr;
537 str = (ASN1_STRING *)*fld;
b173acfc
DSH
538 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
539 pname = NULL;
540 else
541 pname = ASN1_tag2str(utype);
542 }
543 else
544 {
545 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
546 pname = ASN1_tag2str(utype);
547 else
548 pname = NULL;
549 }
550
551 if (utype == V_ASN1_NULL)
552 {
553 if (BIO_puts(out, "NULL\n") <= 0)
554 return 0;
555 return 1;
556 }
557
558 if (pname)
559 {
560 if (BIO_puts(out, pname) <= 0)
561 return 0;
562 if (BIO_puts(out, ":") <= 0)
563 return 0;
56defd9a 564 }
56defd9a
DSH
565
566 switch (utype)
567 {
568 case V_ASN1_BOOLEAN:
569 {
570 int bool = *(int *)fld;
571 if (bool == -1)
572 bool = it->size;
573 ret = asn1_print_boolean_ctx(out, bool, pctx);
574 }
575 break;
576
577 case V_ASN1_INTEGER:
578 case V_ASN1_ENUMERATED:
579 ret = asn1_print_integer_ctx(out, str, pctx);
580 break;
581
56defd9a
DSH
582 case V_ASN1_UTCTIME:
583 ret = ASN1_UTCTIME_print(out, str);
584 break;
585
586 case V_ASN1_GENERALIZEDTIME:
587 ret = ASN1_GENERALIZEDTIME_print(out, str);
588 break;
589
590 case V_ASN1_OBJECT:
591 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
592 break;
593
594 case V_ASN1_OCTET_STRING:
595 case V_ASN1_BIT_STRING:
596 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
b173acfc 597 needlf = 0;
56defd9a
DSH
598 break;
599
600 case V_ASN1_SEQUENCE:
601 case V_ASN1_SET:
b173acfc
DSH
602 case V_ASN1_OTHER:
603 if (BIO_puts(out, "\n") <= 0)
604 return 0;
56defd9a
DSH
605 if (ASN1_parse_dump(out, str->data, str->length,
606 indent, 0) <= 0)
607 ret = 0;
b173acfc 608 needlf = 0;
56defd9a
DSH
609 break;
610
611 default:
612 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
613
614 }
615 if (!ret)
616 return 0;
b173acfc
DSH
617 if (needlf && BIO_puts(out, "\n") <= 0)
618 return 0;
56defd9a
DSH
619 return 1;
620 }