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