Coverage Report

Created: 2018-08-29 13:53

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