support embed in ASN.1 print
[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     ASN1_VALUE *tfld;
318     flags = tt->flags;
319     if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
320         sname = ASN1_ITEM_ptr(tt->item)->sname;
321     else
322         sname = NULL;
323     if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
324         fname = NULL;
325     else
326         fname = tt->field_name;
327
328     /*
329      * If field is embedded then fld needs fixing so it is a pointer to
330      * a pointer to a field.
331      */
332     if (flags & ASN1_TFLG_EMBED) {
333         tfld = (ASN1_VALUE *)fld;
334         fld = &tfld;
335     }
336
337     if (flags & ASN1_TFLG_SK_MASK) {
338         char *tname;
339         ASN1_VALUE *skitem;
340         STACK_OF(ASN1_VALUE) *stack;
341
342         /* SET OF, SEQUENCE OF */
343         if (fname) {
344             if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
345                 if (flags & ASN1_TFLG_SET_OF)
346                     tname = "SET";
347                 else
348                     tname = "SEQUENCE";
349                 if (BIO_printf(out, "%*s%s OF %s {\n",
350                                indent, "", tname, tt->field_name) <= 0)
351                     return 0;
352             } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
353                 return 0;
354         }
355         stack = (STACK_OF(ASN1_VALUE) *)*fld;
356         for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
357             if ((i > 0) && (BIO_puts(out, "\n") <= 0))
358                 return 0;
359
360             skitem = sk_ASN1_VALUE_value(stack, i);
361             if (!asn1_item_print_ctx(out, &skitem, indent + 2,
362                                      ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
363                                      pctx))
364                 return 0;
365         }
366         if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
367             return 0;
368         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
369             if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
370                 return 0;
371         }
372         return 1;
373     }
374     return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
375                                fname, sname, 0, pctx);
376 }
377
378 static int asn1_print_fsname(BIO *out, int indent,
379                              const char *fname, const char *sname,
380                              const ASN1_PCTX *pctx)
381 {
382     static const char spaces[] = "                    ";
383     static const int nspaces = sizeof(spaces) - 1;
384
385     while (indent > nspaces) {
386         if (BIO_write(out, spaces, nspaces) != nspaces)
387             return 0;
388         indent -= nspaces;
389     }
390     if (BIO_write(out, spaces, indent) != indent)
391         return 0;
392     if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
393         sname = NULL;
394     if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
395         fname = NULL;
396     if (!sname && !fname)
397         return 1;
398     if (fname) {
399         if (BIO_puts(out, fname) <= 0)
400             return 0;
401     }
402     if (sname) {
403         if (fname) {
404             if (BIO_printf(out, " (%s)", sname) <= 0)
405                 return 0;
406         } else {
407             if (BIO_puts(out, sname) <= 0)
408                 return 0;
409         }
410     }
411     if (BIO_write(out, ": ", 2) != 2)
412         return 0;
413     return 1;
414 }
415
416 static int asn1_print_boolean(BIO *out, int boolval)
417 {
418     const char *str;
419     switch (boolval) {
420     case -1:
421         str = "BOOL ABSENT";
422         break;
423
424     case 0:
425         str = "FALSE";
426         break;
427
428     default:
429         str = "TRUE";
430         break;
431
432     }
433
434     if (BIO_puts(out, str) <= 0)
435         return 0;
436     return 1;
437
438 }
439
440 static int asn1_print_integer(BIO *out, ASN1_INTEGER *str)
441 {
442     char *s;
443     int ret = 1;
444     s = i2s_ASN1_INTEGER(NULL, str);
445     if (BIO_puts(out, s) <= 0)
446         ret = 0;
447     OPENSSL_free(s);
448     return ret;
449 }
450
451 static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
452 {
453     char objbuf[80];
454     const char *ln;
455     ln = OBJ_nid2ln(OBJ_obj2nid(oid));
456     if (!ln)
457         ln = "";
458     OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
459     if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
460         return 0;
461     return 1;
462 }
463
464 static int asn1_print_obstring(BIO *out, ASN1_STRING *str, int indent)
465 {
466     if (str->type == V_ASN1_BIT_STRING) {
467         if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
468             return 0;
469     } else if (BIO_puts(out, "\n") <= 0)
470         return 0;
471     if ((str->length > 0)
472         && BIO_dump_indent(out, (char *)str->data, str->length,
473                            indent + 2) <= 0)
474         return 0;
475     return 1;
476 }
477
478 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
479                                 const ASN1_ITEM *it, int indent,
480                                 const char *fname, const char *sname,
481                                 const ASN1_PCTX *pctx)
482 {
483     long utype;
484     ASN1_STRING *str;
485     int ret = 1, needlf = 1;
486     const char *pname;
487     const ASN1_PRIMITIVE_FUNCS *pf;
488     pf = it->funcs;
489     if (!asn1_print_fsname(out, indent, fname, sname, pctx))
490         return 0;
491     if (pf && pf->prim_print)
492         return pf->prim_print(out, fld, it, indent, pctx);
493     str = (ASN1_STRING *)*fld;
494     if (it->itype == ASN1_ITYPE_MSTRING)
495         utype = str->type & ~V_ASN1_NEG;
496     else
497         utype = it->utype;
498     if (utype == V_ASN1_ANY) {
499         ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
500         utype = atype->type;
501         fld = &atype->value.asn1_value;
502         str = (ASN1_STRING *)*fld;
503         if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
504             pname = NULL;
505         else
506             pname = ASN1_tag2str(utype);
507     } else {
508         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
509             pname = ASN1_tag2str(utype);
510         else
511             pname = NULL;
512     }
513
514     if (utype == V_ASN1_NULL) {
515         if (BIO_puts(out, "NULL\n") <= 0)
516             return 0;
517         return 1;
518     }
519
520     if (pname) {
521         if (BIO_puts(out, pname) <= 0)
522             return 0;
523         if (BIO_puts(out, ":") <= 0)
524             return 0;
525     }
526
527     switch (utype) {
528     case V_ASN1_BOOLEAN:
529         {
530             int boolval = *(int *)fld;
531             if (boolval == -1)
532                 boolval = it->size;
533             ret = asn1_print_boolean(out, boolval);
534         }
535         break;
536
537     case V_ASN1_INTEGER:
538     case V_ASN1_ENUMERATED:
539         ret = asn1_print_integer(out, str);
540         break;
541
542     case V_ASN1_UTCTIME:
543         ret = ASN1_UTCTIME_print(out, str);
544         break;
545
546     case V_ASN1_GENERALIZEDTIME:
547         ret = ASN1_GENERALIZEDTIME_print(out, str);
548         break;
549
550     case V_ASN1_OBJECT:
551         ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
552         break;
553
554     case V_ASN1_OCTET_STRING:
555     case V_ASN1_BIT_STRING:
556         ret = asn1_print_obstring(out, str, indent);
557         needlf = 0;
558         break;
559
560     case V_ASN1_SEQUENCE:
561     case V_ASN1_SET:
562     case V_ASN1_OTHER:
563         if (BIO_puts(out, "\n") <= 0)
564             return 0;
565         if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
566             ret = 0;
567         needlf = 0;
568         break;
569
570     default:
571         ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
572
573     }
574     if (!ret)
575         return 0;
576     if (needlf && BIO_puts(out, "\n") <= 0)
577         return 0;
578     return 1;
579 }