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