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