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