Update ASN1 printing code and add a -print option to 'pkcs7' utility for
[openssl.git] / crypto / asn1 / tasn_prn.c
1 /* tasn_prn.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000,2005 The OpenSSL Project.  All rights reserved.
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>
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/objects.h>
64 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include <openssl/x509v3.h>
67 #include "asn1_locl.h"
68
69 #include <string.h>
70
71 /* Print routines.
72  */
73
74 /* ASN1_PCTX routines */
75
76 ASN1_PCTX default_pctx = 
77         {
78         ASN1_PCTX_FLAGS_SHOW_ABSENT,    /* flags */
79         0,      /* nm_flags */
80         0,      /* cert_flags */
81         0,      /* oid_flags */
82         0       /* str_flags */
83         };
84         
85
86 ASN1_PCTX *ASN1_PCTX_new(void)
87         {
88         ASN1_PCTX *ret;
89         ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
90         if (ret == NULL)
91                 {
92                 ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
93                 return NULL;
94                 }
95         ret->flags = 0;
96         ret->nm_flags = 0;
97         ret->cert_flags = 0;
98         ret->oid_flags = 0;
99         ret->str_flags = 0;
100         return ret;
101         }
102
103 void ASN1_PCTX_free(ASN1_PCTX *p)
104         {
105         OPENSSL_free(p);
106         }
107
108 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
109         {
110         return p->flags;
111         }
112
113 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
114         {
115         p->flags = flags;
116         }
117
118 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
119         {
120         return p->nm_flags;
121         }
122
123 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
124         {
125         p->nm_flags = flags;
126         }
127
128 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
129         {
130         return p->cert_flags;
131         }
132
133 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
134         {
135         p->cert_flags = flags;
136         }
137
138 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
139         {
140         return p->oid_flags;
141         }
142
143 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
144         {
145         p->oid_flags = flags;
146         }
147
148 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
149         {
150         return p->str_flags;
151         }
152
153 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
154         {
155         p->str_flags = flags;
156         }
157
158 /* Main print routines */
159
160 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
161                                 const ASN1_ITEM *it,
162                                 const char *fname, const char *sname,
163                                 int nohdr, const ASN1_PCTX *pctx);
164
165 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
166                                 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
167
168 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
169                                 const ASN1_ITEM *it, int indent,
170                                 const char *fname, const char *sname,
171                                 const ASN1_PCTX *pctx);
172
173 static int asn1_print_fsname(BIO *out, int indent,
174                         const char *fname, const char *sname,
175                         const ASN1_PCTX *pctx);
176
177 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
178                                 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
179         {
180         const char *sname;
181         if (pctx == NULL)
182                 pctx = &default_pctx;
183         if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
184                 sname = NULL;
185         else
186                 sname = it->sname;
187         return asn1_item_print_ctx(out, &ifld, indent, it,
188                                                         NULL, sname, 0, pctx);
189         }
190
191 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
192                                 const ASN1_ITEM *it,
193                                 const char *fname, const char *sname,
194                                 int nohdr, const ASN1_PCTX *pctx)
195         {
196         const ASN1_TEMPLATE *tt;
197         const ASN1_EXTERN_FUNCS *ef;
198         ASN1_VALUE **tmpfld;
199         int i;
200         if(*fld == NULL)
201                 {
202                 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
203                         {
204                         if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
205                                 return 0;
206                         if (BIO_puts(out, "<ABSENT>\n") <= 0)
207                                 return 0;
208                         }
209                 return 1;
210                 }
211
212         switch(it->itype)
213                 {
214                 case ASN1_ITYPE_PRIMITIVE:
215                 if(it->templates)
216                         {
217                         if (!asn1_template_print_ctx(out, fld, indent,
218                                                         it->templates, pctx))
219                                 return 0;
220                         }
221                 /* fall thru */
222                 case ASN1_ITYPE_MSTRING:
223                 if (!asn1_primitive_print(out, fld, it,
224                                 indent, fname, sname,pctx))
225                         return 0;
226                 break;
227
228                 case ASN1_ITYPE_EXTERN:
229                 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
230                         return 0;
231                 /* Use new style print routine if possible */
232                 ef = it->funcs;
233                 if (ef && ef->asn1_ex_print)
234                         {
235                         i = ef->asn1_ex_print(out, fld, indent, "", pctx);
236                         if (!i)
237                                 return 0;
238                         if ((i == 2) && (BIO_puts(out, "\n") <= 0))
239                                 return 0;
240                         return 1;
241                         }
242                 else if (sname && 
243                         BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
244                         return 0;
245                 break;
246
247                 case ASN1_ITYPE_CHOICE:
248 #if 0
249                 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
250                         return 0;
251 #endif
252                 /* CHOICE type, get selector */
253                 i = asn1_get_choice_selector(fld, it);
254                 /* This should never happen... */
255                 if((i < 0) || (i >= it->tcount))
256                         {
257                         if (BIO_printf(out,
258                                 "ERROR: selector [%d] invalid\n", i) <= 0)
259                                 return 0;
260                         return 1;
261                         }
262                 tt = it->templates + i;
263                 tmpfld = asn1_get_field_ptr(fld, tt);
264                 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
265                         return 0;
266                 break;
267
268                 case ASN1_ITYPE_SEQUENCE:
269                 case ASN1_ITYPE_NDEF_SEQUENCE:
270                 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
271                         return 0;
272                 if (fname || sname)
273                         {
274                         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
275                                 {
276                                 if (BIO_puts(out, " {\n") <= 0)
277                                         return 0;
278                                 }
279                         else
280                                 {
281                                 if (BIO_puts(out, "\n") <= 0)
282                                         return 0;
283                                 }
284                         }
285
286                 /* Print each field entry */
287                 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
288                         {
289                         const ASN1_TEMPLATE *seqtt;
290                         seqtt = asn1_do_adb(fld, tt, 1);
291                         tmpfld = asn1_get_field_ptr(fld, seqtt);
292                         if (!asn1_template_print_ctx(out, tmpfld,
293                                                 indent + 2, seqtt, pctx))
294                                 return 0;
295                         }
296                 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
297                         {
298                         if (BIO_printf(out, "%*s}\n", indent, "") < 0)
299                                 return 0;
300                         }
301                 break;
302
303                 default:
304                 BIO_printf(out, "Unprocessed type %d\n", it->itype);
305                 return 0;
306                 }
307
308         return 1;
309         }
310
311 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
312                                 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
313         {
314         int i, flags;
315         const char *sname, *fname;
316         flags = tt->flags;
317         if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
318                 sname = tt->item->sname;
319         else
320                 sname = NULL;
321         if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
322                 fname = NULL;
323         else
324                 fname = tt->field_name;
325         if(flags & ASN1_TFLG_SK_MASK)
326                 {
327                 char *tname;
328                 ASN1_VALUE *skitem;
329                 /* SET OF, SEQUENCE OF */
330                 if (fname)
331                         {
332                         if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
333                                 {
334                                 if(flags & ASN1_TFLG_SET_OF)
335                                         tname = "SET";
336                                 else
337                                         tname = "SEQUENCE";
338                                 if (BIO_printf(out, "%*s%s OF %s {\n",
339                                         indent, "", tname, tt->field_name) <= 0)
340                                         return 0;
341                                 }
342                         else if (BIO_printf(out, "%*s%s:\n", indent, "",
343                                         fname) <= 0)
344                                 return 0;
345                         }
346                 for(i = 0; i < sk_num((STACK *)*fld); i++)
347                         {
348                         if ((i > 0) && (BIO_puts(out, "\n") <= 0))
349                                 return 0;
350
351                         skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
352                         if (!asn1_item_print_ctx(out, &skitem, indent + 2,
353                                         tt->item, NULL, NULL, 1, pctx))
354                                 return 0;
355                         }
356                 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
357                                 return 0;
358                 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
359                         {
360                         if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
361                                 return 0;
362                         }
363                 return 1;
364                 }
365         return asn1_item_print_ctx(out, fld, indent, tt->item,
366                                                         fname, sname, 0, pctx);
367         }
368
369 static int asn1_print_fsname(BIO *out, int indent,
370                         const char *fname, const char *sname,
371                         const ASN1_PCTX *pctx)
372         {
373         static char spaces[] = "                    ";
374         const int nspaces = sizeof(spaces) - 1;
375
376 #if 0
377         if (!sname && !fname)
378                 return 1;
379 #endif
380
381         while (indent > nspaces)
382                 {
383                 if (BIO_write(out, spaces, nspaces) != nspaces)
384                         return 0;
385                 indent -= nspaces;
386                 }
387         if (BIO_write(out, spaces, indent) != indent)
388                 return 0;
389         if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
390                 sname = NULL;
391         if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
392                 fname = NULL;
393         if (!sname && !fname)
394                 return 1;
395         if (fname)
396                 {
397                 if (BIO_puts(out, fname) <= 0)
398                         return 0;
399                 }
400         if (sname)
401                 {
402                 if (fname)
403                         {
404                         if (BIO_printf(out, " (%s)", sname) <= 0)
405                                 return 0;
406                         }
407                 else
408                         {
409                         if (BIO_puts(out, sname) <= 0)
410                                 return 0;
411                         }
412                 }
413         if (BIO_write(out, ": ", 2) != 2)
414                 return 0;
415         return 1;
416         }
417
418 static int asn1_print_boolean_ctx(BIO *out, const int bool,
419                                                         const ASN1_PCTX *pctx)
420         {
421         const char *str;
422         switch (bool)
423                 {
424                 case -1:
425                 str = "BOOL ABSENT";
426                 break;
427
428                 case 0:
429                 str = "FALSE";
430                 break;
431
432                 default:
433                 str = "TRUE";
434                 break;
435
436                 }
437
438         if (BIO_puts(out, str) <= 0)
439                 return 0;
440         return 1;
441
442         }
443
444 static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
445                                                 const ASN1_PCTX *pctx)
446         {
447         char *s;
448         int ret = 1;
449         s = i2s_ASN1_INTEGER(NULL, str);
450         if (BIO_puts(out, s) <= 0)
451                 ret = 0;
452         OPENSSL_free(s);
453         return ret;
454         }
455
456 static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
457                                                 const ASN1_PCTX *pctx)
458         {
459         char objbuf[80];
460         const char *ln;
461         ln = OBJ_nid2ln(OBJ_obj2nid(oid));
462         if(!ln)
463                 ln = "";
464         OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
465         if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
466                 return 0;
467         return 1;
468         }
469
470 static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
471                                                 const ASN1_PCTX *pctx)
472         {
473         if (str->type == V_ASN1_BIT_STRING)
474                 {
475                 if (BIO_printf(out, " (%ld unused bits)\n",
476                                         str->flags & 0x7) <= 0)
477                                 return 0;
478                 }
479         else if (BIO_puts(out, "\n") <= 0)
480                 return 0;
481         if (BIO_dump_indent(out, (char *)str->data, str->length,
482                                 indent + 2) <= 0)
483                 return 0;
484         return 1;
485         }
486
487 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
488                                 const ASN1_ITEM *it, int indent,
489                                 const char *fname, const char *sname,
490                                 const ASN1_PCTX *pctx)
491         {
492         long utype;
493         ASN1_STRING *str;
494         int ret = 1, needlf = 1;
495         const char *pname;
496         if (!asn1_print_fsname(out, indent, fname, sname, pctx))
497                         return 0;
498         str = (ASN1_STRING *)*fld;
499         if (it->itype == ASN1_ITYPE_MSTRING)
500                 utype = str->type & ~V_ASN1_NEG;
501         else
502                 utype = it->utype;
503         if (utype == V_ASN1_ANY)
504                 {
505                 ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
506                 utype = atype->type;
507                 fld = (ASN1_VALUE **)&atype->value.ptr;
508                 str = (ASN1_STRING *)*fld;
509                 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
510                         pname = NULL;
511                 else 
512                         pname = ASN1_tag2str(utype);
513                 }
514         else
515                 {
516                 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
517                         pname = ASN1_tag2str(utype);
518                 else 
519                         pname = NULL;
520                 }
521
522         if (utype == V_ASN1_NULL)
523                 {
524                 if (BIO_puts(out, "NULL\n") <= 0)
525                         return 0;
526                 return 1;
527                 }
528
529         if (pname)
530                 {
531                 if (BIO_puts(out, pname) <= 0)
532                         return 0;
533                 if (BIO_puts(out, ":") <= 0)
534                         return 0;
535                 }
536
537         switch (utype)
538                 {
539                 case V_ASN1_BOOLEAN:
540                         {
541                         int bool = *(int *)fld;
542                         if (bool == -1)
543                                 bool = it->size;
544                         ret = asn1_print_boolean_ctx(out, bool, pctx);
545                         }
546                 break;
547
548                 case V_ASN1_INTEGER:
549                 case V_ASN1_ENUMERATED:
550                 ret = asn1_print_integer_ctx(out, str, pctx);
551                 break;
552
553                 case V_ASN1_UTCTIME:
554                 ret = ASN1_UTCTIME_print(out, str);
555                 break;
556
557                 case V_ASN1_GENERALIZEDTIME:
558                 ret = ASN1_GENERALIZEDTIME_print(out, str);
559                 break;
560
561                 case V_ASN1_OBJECT:
562                 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
563                 break;
564
565                 case V_ASN1_OCTET_STRING:
566                 case V_ASN1_BIT_STRING:
567                 ret = asn1_print_obstring_ctx(out, str, indent, pctx);
568                 needlf = 0;
569                 break;
570
571                 case V_ASN1_SEQUENCE:
572                 case V_ASN1_SET:
573                 case V_ASN1_OTHER:
574                 if (BIO_puts(out, "\n") <= 0)
575                         return 0;
576                 if (ASN1_parse_dump(out, str->data, str->length,
577                                                 indent, 0) <= 0)
578                         ret = 0;
579                 needlf = 0;
580                 break;
581
582                 default:
583                 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
584
585                 }
586         if (!ret)
587                 return 0;
588         if (needlf && BIO_puts(out, "\n") <= 0)
589                 return 0;
590         return 1;
591         }