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