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