Avoid creating an illegal pointer.
[openssl.git] / crypto / asn1 / tasn_prn.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stddef.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/objects.h>
15 #include <openssl/buffer.h>
16 #include <openssl/err.h>
17 #include <openssl/x509v3.h>
18 #include "internal/asn1_int.h"
19 #include "asn1_locl.h"
20
21 /*
22  * Print routines.
23  */
24
25 /* ASN1_PCTX routines */
26
27 static ASN1_PCTX default_pctx = {
28     ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
29     0,                          /* nm_flags */
30     0,                          /* cert_flags */
31     0,                          /* oid_flags */
32     0                           /* str_flags */
33 };
34
35 ASN1_PCTX *ASN1_PCTX_new(void)
36 {
37     ASN1_PCTX *ret;
38
39     ret = OPENSSL_zalloc(sizeof(*ret));
40     if (ret == NULL) {
41         ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
42         return NULL;
43     }
44     return ret;
45 }
46
47 void ASN1_PCTX_free(ASN1_PCTX *p)
48 {
49     OPENSSL_free(p);
50 }
51
52 unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
53 {
54     return p->flags;
55 }
56
57 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
58 {
59     p->flags = flags;
60 }
61
62 unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
63 {
64     return p->nm_flags;
65 }
66
67 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
68 {
69     p->nm_flags = flags;
70 }
71
72 unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
73 {
74     return p->cert_flags;
75 }
76
77 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
78 {
79     p->cert_flags = flags;
80 }
81
82 unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
83 {
84     return p->oid_flags;
85 }
86
87 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
88 {
89     p->oid_flags = flags;
90 }
91
92 unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
93 {
94     return p->str_flags;
95 }
96
97 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
98 {
99     p->str_flags = flags;
100 }
101
102 /* Main print routines */
103
104 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
105                                const ASN1_ITEM *it,
106                                const char *fname, const char *sname,
107                                int nohdr, const ASN1_PCTX *pctx);
108
109 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
110                             const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
111
112 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
113                                 const ASN1_ITEM *it, int indent,
114                                 const char *fname, const char *sname,
115                                 const ASN1_PCTX *pctx);
116
117 static int asn1_print_fsname(BIO *out, int indent,
118                              const char *fname, const char *sname,
119                              const ASN1_PCTX *pctx);
120
121 int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
122                     const ASN1_ITEM *it, const ASN1_PCTX *pctx)
123 {
124     const char *sname;
125     if (pctx == NULL)
126         pctx = &default_pctx;
127     if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
128         sname = NULL;
129     else
130         sname = it->sname;
131     return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
132 }
133
134 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
135                                const ASN1_ITEM *it,
136                                const char *fname, const char *sname,
137                                int nohdr, const ASN1_PCTX *pctx)
138 {
139     const ASN1_TEMPLATE *tt;
140     const ASN1_EXTERN_FUNCS *ef;
141     ASN1_VALUE **tmpfld;
142     const ASN1_AUX *aux = it->funcs;
143     ASN1_aux_cb *asn1_cb;
144     ASN1_PRINT_ARG parg;
145     int i;
146     if (aux && aux->asn1_cb) {
147         parg.out = out;
148         parg.indent = indent;
149         parg.pctx = pctx;
150         asn1_cb = aux->asn1_cb;
151     } else
152         asn1_cb = 0;
153
154     if (*fld == NULL) {
155         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
156             if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
157                 return 0;
158             if (BIO_puts(out, "<ABSENT>\n") <= 0)
159                 return 0;
160         }
161         return 1;
162     }
163
164     switch (it->itype) {
165     case ASN1_ITYPE_PRIMITIVE:
166         if (it->templates) {
167             if (!asn1_template_print_ctx(out, fld, indent,
168                                          it->templates, pctx))
169                 return 0;
170             break;
171         }
172         /* fall thru */
173     case ASN1_ITYPE_MSTRING:
174         if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
175             return 0;
176         break;
177
178     case ASN1_ITYPE_EXTERN:
179         if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
180             return 0;
181         /* Use new style print routine if possible */
182         ef = it->funcs;
183         if (ef && ef->asn1_ex_print) {
184             i = ef->asn1_ex_print(out, fld, indent, "", pctx);
185             if (!i)
186                 return 0;
187             if ((i == 2) && (BIO_puts(out, "\n") <= 0))
188                 return 0;
189             return 1;
190         } else if (sname &&
191                    BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
192             return 0;
193         break;
194
195     case ASN1_ITYPE_CHOICE:
196         /* CHOICE type, get selector */
197         i = asn1_get_choice_selector(fld, it);
198         /* This should never happen... */
199         if ((i < 0) || (i >= it->tcount)) {
200             if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
201                 return 0;
202             return 1;
203         }
204         tt = it->templates + i;
205         tmpfld = asn1_get_field_ptr(fld, tt);
206         if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
207             return 0;
208         break;
209
210     case ASN1_ITYPE_SEQUENCE:
211     case ASN1_ITYPE_NDEF_SEQUENCE:
212         if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
213             return 0;
214         if (fname || sname) {
215             if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
216                 if (BIO_puts(out, " {\n") <= 0)
217                     return 0;
218             } else {
219                 if (BIO_puts(out, "\n") <= 0)
220                     return 0;
221             }
222         }
223
224         if (asn1_cb) {
225             i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
226             if (i == 0)
227                 return 0;
228             if (i == 2)
229                 return 1;
230         }
231
232         /* Print each field entry */
233         for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
234             const ASN1_TEMPLATE *seqtt;
235             seqtt = asn1_do_adb(fld, tt, 1);
236             if (!seqtt)
237                 return 0;
238             tmpfld = asn1_get_field_ptr(fld, seqtt);
239             if (!asn1_template_print_ctx(out, tmpfld,
240                                          indent + 2, seqtt, pctx))
241                 return 0;
242         }
243         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
244             if (BIO_printf(out, "%*s}\n", indent, "") < 0)
245                 return 0;
246         }
247
248         if (asn1_cb) {
249             i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
250             if (i == 0)
251                 return 0;
252         }
253         break;
254
255     default:
256         BIO_printf(out, "Unprocessed type %d\n", it->itype);
257         return 0;
258     }
259
260     return 1;
261 }
262
263 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
264                             const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
265 {
266     int i, flags;
267     const char *sname, *fname;
268     ASN1_VALUE *tfld;
269     flags = tt->flags;
270     if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
271         sname = ASN1_ITEM_ptr(tt->item)->sname;
272     else
273         sname = NULL;
274     if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
275         fname = NULL;
276     else
277         fname = tt->field_name;
278
279     /*
280      * If field is embedded then fld needs fixing so it is a pointer to
281      * a pointer to a field.
282      */
283     if (flags & ASN1_TFLG_EMBED) {
284         tfld = (ASN1_VALUE *)fld;
285         fld = &tfld;
286     }
287
288     if (flags & ASN1_TFLG_SK_MASK) {
289         char *tname;
290         ASN1_VALUE *skitem;
291         STACK_OF(ASN1_VALUE) *stack;
292
293         /* SET OF, SEQUENCE OF */
294         if (fname) {
295             if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
296                 if (flags & ASN1_TFLG_SET_OF)
297                     tname = "SET";
298                 else
299                     tname = "SEQUENCE";
300                 if (BIO_printf(out, "%*s%s OF %s {\n",
301                                indent, "", tname, tt->field_name) <= 0)
302                     return 0;
303             } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
304                 return 0;
305         }
306         stack = (STACK_OF(ASN1_VALUE) *)*fld;
307         for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
308             if ((i > 0) && (BIO_puts(out, "\n") <= 0))
309                 return 0;
310
311             skitem = sk_ASN1_VALUE_value(stack, i);
312             if (!asn1_item_print_ctx(out, &skitem, indent + 2,
313                                      ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
314                                      pctx))
315                 return 0;
316         }
317         if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
318             return 0;
319         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
320             if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
321                 return 0;
322         }
323         return 1;
324     }
325     return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
326                                fname, sname, 0, pctx);
327 }
328
329 static int asn1_print_fsname(BIO *out, int indent,
330                              const char *fname, const char *sname,
331                              const ASN1_PCTX *pctx)
332 {
333     static const char spaces[] = "                    ";
334     static const int nspaces = sizeof(spaces) - 1;
335
336     while (indent > nspaces) {
337         if (BIO_write(out, spaces, nspaces) != nspaces)
338             return 0;
339         indent -= nspaces;
340     }
341     if (BIO_write(out, spaces, indent) != indent)
342         return 0;
343     if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
344         sname = NULL;
345     if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
346         fname = NULL;
347     if (!sname && !fname)
348         return 1;
349     if (fname) {
350         if (BIO_puts(out, fname) <= 0)
351             return 0;
352     }
353     if (sname) {
354         if (fname) {
355             if (BIO_printf(out, " (%s)", sname) <= 0)
356                 return 0;
357         } else {
358             if (BIO_puts(out, sname) <= 0)
359                 return 0;
360         }
361     }
362     if (BIO_write(out, ": ", 2) != 2)
363         return 0;
364     return 1;
365 }
366
367 static int asn1_print_boolean(BIO *out, int boolval)
368 {
369     const char *str;
370     switch (boolval) {
371     case -1:
372         str = "BOOL ABSENT";
373         break;
374
375     case 0:
376         str = "FALSE";
377         break;
378
379     default:
380         str = "TRUE";
381         break;
382
383     }
384
385     if (BIO_puts(out, str) <= 0)
386         return 0;
387     return 1;
388
389 }
390
391 static int asn1_print_integer(BIO *out, ASN1_INTEGER *str)
392 {
393     char *s;
394     int ret = 1;
395     s = i2s_ASN1_INTEGER(NULL, str);
396     if (BIO_puts(out, s) <= 0)
397         ret = 0;
398     OPENSSL_free(s);
399     return ret;
400 }
401
402 static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
403 {
404     char objbuf[80];
405     const char *ln;
406     ln = OBJ_nid2ln(OBJ_obj2nid(oid));
407     if (!ln)
408         ln = "";
409     OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
410     if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
411         return 0;
412     return 1;
413 }
414
415 static int asn1_print_obstring(BIO *out, ASN1_STRING *str, int indent)
416 {
417     if (str->type == V_ASN1_BIT_STRING) {
418         if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
419             return 0;
420     } else if (BIO_puts(out, "\n") <= 0)
421         return 0;
422     if ((str->length > 0)
423         && BIO_dump_indent(out, (char *)str->data, str->length,
424                            indent + 2) <= 0)
425         return 0;
426     return 1;
427 }
428
429 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
430                                 const ASN1_ITEM *it, int indent,
431                                 const char *fname, const char *sname,
432                                 const ASN1_PCTX *pctx)
433 {
434     long utype;
435     ASN1_STRING *str;
436     int ret = 1, needlf = 1;
437     const char *pname;
438     const ASN1_PRIMITIVE_FUNCS *pf;
439     pf = it->funcs;
440     if (!asn1_print_fsname(out, indent, fname, sname, pctx))
441         return 0;
442     if (pf && pf->prim_print)
443         return pf->prim_print(out, fld, it, indent, pctx);
444     str = (ASN1_STRING *)*fld;
445     if (it->itype == ASN1_ITYPE_MSTRING)
446         utype = str->type & ~V_ASN1_NEG;
447     else
448         utype = it->utype;
449     if (utype == V_ASN1_ANY) {
450         ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
451         utype = atype->type;
452         fld = &atype->value.asn1_value;
453         str = (ASN1_STRING *)*fld;
454         if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
455             pname = NULL;
456         else
457             pname = ASN1_tag2str(utype);
458     } else {
459         if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
460             pname = ASN1_tag2str(utype);
461         else
462             pname = NULL;
463     }
464
465     if (utype == V_ASN1_NULL) {
466         if (BIO_puts(out, "NULL\n") <= 0)
467             return 0;
468         return 1;
469     }
470
471     if (pname) {
472         if (BIO_puts(out, pname) <= 0)
473             return 0;
474         if (BIO_puts(out, ":") <= 0)
475             return 0;
476     }
477
478     switch (utype) {
479     case V_ASN1_BOOLEAN:
480         {
481             int boolval = *(int *)fld;
482             if (boolval == -1)
483                 boolval = it->size;
484             ret = asn1_print_boolean(out, boolval);
485         }
486         break;
487
488     case V_ASN1_INTEGER:
489     case V_ASN1_ENUMERATED:
490         ret = asn1_print_integer(out, str);
491         break;
492
493     case V_ASN1_UTCTIME:
494         ret = ASN1_UTCTIME_print(out, str);
495         break;
496
497     case V_ASN1_GENERALIZEDTIME:
498         ret = ASN1_GENERALIZEDTIME_print(out, str);
499         break;
500
501     case V_ASN1_OBJECT:
502         ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
503         break;
504
505     case V_ASN1_OCTET_STRING:
506     case V_ASN1_BIT_STRING:
507         ret = asn1_print_obstring(out, str, indent);
508         needlf = 0;
509         break;
510
511     case V_ASN1_SEQUENCE:
512     case V_ASN1_SET:
513     case V_ASN1_OTHER:
514         if (BIO_puts(out, "\n") <= 0)
515             return 0;
516         if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
517             ret = 0;
518         needlf = 0;
519         break;
520
521     default:
522         ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
523
524     }
525     if (!ret)
526         return 0;
527     if (needlf && BIO_puts(out, "\n") <= 0)
528         return 0;
529     return 1;
530 }