1 /*
2 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "crypto/asn1.h"
19 #include "asn1_local.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
ASN1_PCTX_new(void)35 ASN1_PCTX *ASN1_PCTX_new(void)
36 {
37 ASN1_PCTX *ret;
38
39 ret = OPENSSL_zalloc(sizeof(*ret));
40 if (ret == NULL)
41 return NULL;
42 return ret;
43 }
44
ASN1_PCTX_free(ASN1_PCTX * p)45 void ASN1_PCTX_free(ASN1_PCTX *p)
46 {
47 OPENSSL_free(p);
48 }
49
ASN1_PCTX_get_flags(const ASN1_PCTX * p)50 unsigned long ASN1_PCTX_get_flags(const ASN1_PCTX *p)
51 {
52 return p->flags;
53 }
54
ASN1_PCTX_set_flags(ASN1_PCTX * p,unsigned long flags)55 void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
56 {
57 p->flags = flags;
58 }
59
ASN1_PCTX_get_nm_flags(const ASN1_PCTX * p)60 unsigned long ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p)
61 {
62 return p->nm_flags;
63 }
64
ASN1_PCTX_set_nm_flags(ASN1_PCTX * p,unsigned long flags)65 void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
66 {
67 p->nm_flags = flags;
68 }
69
ASN1_PCTX_get_cert_flags(const ASN1_PCTX * p)70 unsigned long ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p)
71 {
72 return p->cert_flags;
73 }
74
ASN1_PCTX_set_cert_flags(ASN1_PCTX * p,unsigned long flags)75 void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
76 {
77 p->cert_flags = flags;
78 }
79
ASN1_PCTX_get_oid_flags(const ASN1_PCTX * p)80 unsigned long ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p)
81 {
82 return p->oid_flags;
83 }
84
ASN1_PCTX_set_oid_flags(ASN1_PCTX * p,unsigned long flags)85 void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
86 {
87 p->oid_flags = flags;
88 }
89
ASN1_PCTX_get_str_flags(const ASN1_PCTX * p)90 unsigned long ASN1_PCTX_get_str_flags(const ASN1_PCTX *p)
91 {
92 return p->str_flags;
93 }
94
ASN1_PCTX_set_str_flags(ASN1_PCTX * p,unsigned long flags)95 void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
96 {
97 p->str_flags = flags;
98 }
99
100 /* Main print routines */
101
102 static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
103 const ASN1_ITEM *it,
104 const char *fname, const char *sname,
105 int nohdr, const ASN1_PCTX *pctx);
106
107 static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
108 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
109
110 static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
111 const ASN1_ITEM *it, int indent,
112 const char *fname, const char *sname,
113 const ASN1_PCTX *pctx);
114
115 static int asn1_print_fsname(BIO *out, int indent,
116 const char *fname, const char *sname,
117 const ASN1_PCTX *pctx);
118
ASN1_item_print(BIO * out,const ASN1_VALUE * ifld,int indent,const ASN1_ITEM * it,const ASN1_PCTX * pctx)119 int ASN1_item_print(BIO *out, const ASN1_VALUE *ifld, int indent,
120 const ASN1_ITEM *it, const ASN1_PCTX *pctx)
121 {
122 const char *sname;
123 if (pctx == NULL)
124 pctx = &default_pctx;
125 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
126 sname = NULL;
127 else
128 sname = it->sname;
129 return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
130 }
131
asn1_item_print_ctx(BIO * out,const ASN1_VALUE ** fld,int indent,const ASN1_ITEM * it,const char * fname,const char * sname,int nohdr,const ASN1_PCTX * pctx)132 static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
133 const ASN1_ITEM *it,
134 const char *fname, const char *sname,
135 int nohdr, const ASN1_PCTX *pctx)
136 {
137 const ASN1_TEMPLATE *tt;
138 const ASN1_EXTERN_FUNCS *ef;
139 const ASN1_VALUE **tmpfld;
140 const ASN1_AUX *aux = it->funcs;
141 ASN1_aux_const_cb *asn1_cb = NULL;
142 ASN1_PRINT_ARG parg;
143 int i;
144 if (aux != NULL) {
145 parg.out = out;
146 parg.indent = indent;
147 parg.pctx = pctx;
148 asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
149 : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
150 }
151
152 if (((it->itype != ASN1_ITYPE_PRIMITIVE)
153 || (it->utype != V_ASN1_BOOLEAN))
154 && *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 through */
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 && BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
191 return 0;
192 break;
193
194 case ASN1_ITYPE_CHOICE:
195 /* CHOICE type, get selector */
196 i = ossl_asn1_get_choice_selector_const(fld, it);
197 /* This should never happen... */
198 if ((i < 0) || (i >= it->tcount)) {
199 if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
200 return 0;
201 return 1;
202 }
203 tt = it->templates + i;
204 tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
205 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
206 return 0;
207 break;
208
209 case ASN1_ITYPE_SEQUENCE:
210 case ASN1_ITYPE_NDEF_SEQUENCE:
211 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
212 return 0;
213 if (fname || sname) {
214 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
215 if (BIO_puts(out, " {\n") <= 0)
216 return 0;
217 } else {
218 if (BIO_puts(out, "\n") <= 0)
219 return 0;
220 }
221 }
222
223 if (asn1_cb) {
224 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
225 if (i == 0)
226 return 0;
227 if (i == 2)
228 return 1;
229 }
230
231 /* Print each field entry */
232 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
233 const ASN1_TEMPLATE *seqtt;
234 seqtt = ossl_asn1_do_adb(*fld, tt, 1);
235 if (!seqtt)
236 return 0;
237 tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
238 if (!asn1_template_print_ctx(out, tmpfld,
239 indent + 2, seqtt, pctx))
240 return 0;
241 }
242 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
243 if (BIO_printf(out, "%*s}\n", indent, "") < 0)
244 return 0;
245 }
246
247 if (asn1_cb) {
248 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
249 if (i == 0)
250 return 0;
251 }
252 break;
253
254 default:
255 BIO_printf(out, "Unprocessed type %d\n", it->itype);
256 return 0;
257 }
258
259 return 1;
260 }
261
asn1_template_print_ctx(BIO * out,const ASN1_VALUE ** fld,int indent,const ASN1_TEMPLATE * tt,const ASN1_PCTX * pctx)262 static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
263 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
264 {
265 int i, flags;
266 const char *sname, *fname;
267 const ASN1_VALUE *tfld;
268 flags = tt->flags;
269 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
270 sname = ASN1_ITEM_ptr(tt->item)->sname;
271 else
272 sname = NULL;
273 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
274 fname = NULL;
275 else
276 fname = tt->field_name;
277
278 /*
279 * If field is embedded then fld needs fixing so it is a pointer to
280 * a pointer to a field.
281 */
282 if (flags & ASN1_TFLG_EMBED) {
283 tfld = (const ASN1_VALUE *)fld;
284 fld = &tfld;
285 }
286
287 if (flags & ASN1_TFLG_SK_MASK) {
288 char *tname;
289 const ASN1_VALUE *skitem;
290 STACK_OF(const_ASN1_VALUE) *stack;
291
292 /* SET OF, SEQUENCE OF */
293 if (fname) {
294 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
295 if (flags & ASN1_TFLG_SET_OF)
296 tname = "SET";
297 else
298 tname = "SEQUENCE";
299 if (BIO_printf(out, "%*s%s OF %s {\n",
300 indent, "", tname, tt->field_name)
301 <= 0)
302 return 0;
303 } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
304 return 0;
305 }
306 stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
307 for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
308 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
309 return 0;
310
311 skitem = sk_const_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 == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "", stack == NULL ? "ABSENT" : "EMPTY") <= 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
asn1_print_fsname(BIO * out,int indent,const char * fname,const char * sname,const ASN1_PCTX * pctx)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
asn1_print_boolean(BIO * out,int boolval)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 if (BIO_puts(out, str) <= 0)
385 return 0;
386 return 1;
387 }
388
asn1_print_integer(BIO * out,const ASN1_INTEGER * str)389 static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
390 {
391 char *s;
392 int ret = 1;
393 s = i2s_ASN1_INTEGER(NULL, str);
394 if (s == NULL)
395 return 0;
396 if (BIO_puts(out, s) <= 0)
397 ret = 0;
398 OPENSSL_free(s);
399 return ret;
400 }
401
asn1_print_oid(BIO * out,const ASN1_OBJECT * oid)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
asn1_print_obstring(BIO * out,const ASN1_STRING * str,int indent)415 static int asn1_print_obstring(BIO *out, const 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, (const char *)str->data, str->length,
424 indent + 2)
425 <= 0)
426 return 0;
427 return 1;
428 }
429
asn1_primitive_print(BIO * out,const ASN1_VALUE ** fld,const ASN1_ITEM * it,int indent,const char * fname,const char * sname,const ASN1_PCTX * pctx)430 static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
431 const ASN1_ITEM *it, int indent,
432 const char *fname, const char *sname,
433 const ASN1_PCTX *pctx)
434 {
435 long utype;
436 ASN1_STRING *str;
437 int ret = 1, needlf = 1;
438 const char *pname;
439 const ASN1_PRIMITIVE_FUNCS *pf;
440 pf = it->funcs;
441 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
442 return 0;
443 if (pf && pf->prim_print)
444 return pf->prim_print(out, fld, it, indent, pctx);
445 if (it->itype == ASN1_ITYPE_MSTRING) {
446 str = (ASN1_STRING *)*fld;
447 utype = str->type & ~V_ASN1_NEG;
448 } else {
449 utype = it->utype;
450 if (utype == V_ASN1_BOOLEAN)
451 str = NULL;
452 else
453 str = (ASN1_STRING *)*fld;
454 }
455 if (utype == V_ASN1_ANY) {
456 const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
457 utype = atype->type;
458 fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
459 str = (ASN1_STRING *)*fld;
460 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
461 pname = NULL;
462 else
463 pname = ASN1_tag2str(utype);
464 } else {
465 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
466 pname = ASN1_tag2str(utype);
467 else
468 pname = NULL;
469 }
470
471 if (utype == V_ASN1_NULL) {
472 if (BIO_puts(out, "NULL\n") <= 0)
473 return 0;
474 return 1;
475 }
476
477 if (pname) {
478 if (BIO_puts(out, pname) <= 0)
479 return 0;
480 if (BIO_puts(out, ":") <= 0)
481 return 0;
482 }
483
484 switch (utype) {
485 case V_ASN1_BOOLEAN: {
486 int boolval = *(int *)fld;
487 if (boolval == -1)
488 boolval = it->size;
489 ret = asn1_print_boolean(out, boolval);
490 } break;
491
492 case V_ASN1_INTEGER:
493 case V_ASN1_ENUMERATED:
494 ret = asn1_print_integer(out, str);
495 break;
496
497 case V_ASN1_UTCTIME:
498 ret = ASN1_UTCTIME_print(out, str);
499 break;
500
501 case V_ASN1_GENERALIZEDTIME:
502 ret = ASN1_GENERALIZEDTIME_print(out, str);
503 break;
504
505 case V_ASN1_OBJECT:
506 ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
507 break;
508
509 case V_ASN1_OCTET_STRING:
510 case V_ASN1_BIT_STRING:
511 ret = asn1_print_obstring(out, str, indent);
512 needlf = 0;
513 break;
514
515 case V_ASN1_SEQUENCE:
516 case V_ASN1_SET:
517 case V_ASN1_OTHER:
518 if (BIO_puts(out, "\n") <= 0)
519 return 0;
520 if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
521 ret = 0;
522 needlf = 0;
523 break;
524
525 default:
526 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
527 }
528 if (!ret)
529 return 0;
530 if (needlf && BIO_puts(out, "\n") <= 0)
531 return 0;
532 return 1;
533 }
534