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