Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/crypto/x509/t_acert.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2021-2024 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/buffer.h>
13
#include <openssl/bn.h>
14
#include <openssl/objects.h>
15
#include <openssl/x509_acert.h>
16
17
static int print_attribute(BIO *bp, X509_ATTRIBUTE *a)
18
82.3k
{
19
82.3k
    ASN1_OBJECT *aobj;
20
82.3k
    int i, j, count;
21
82.3k
    int ret = 0;
22
23
82.3k
    aobj = X509_ATTRIBUTE_get0_object(a);
24
82.3k
    if (BIO_printf(bp, "%12s", "") <= 0)
25
0
        goto err;
26
27
82.3k
    if ((j = i2a_ASN1_OBJECT(bp, aobj)) <= 0)
28
0
        goto err;
29
30
82.3k
    count = X509_ATTRIBUTE_count(a);
31
82.3k
    if (count == 0) {
32
37
        ERR_raise(ERR_LIB_X509, X509_R_INVALID_ATTRIBUTES);
33
37
        goto err;
34
37
    }
35
36
82.3k
    if (j < 25 && (BIO_printf(bp, "%*s", 25 - j, " ") <= 0))
37
0
        goto err;
38
39
82.3k
    if (BIO_puts(bp, ":") <= 0)
40
0
        goto err;
41
42
2.38M
    for (i = 0; i < count; i++) {
43
2.30M
        ASN1_TYPE *at;
44
2.30M
        int type;
45
2.30M
        ASN1_BIT_STRING *bs;
46
47
2.30M
        at = X509_ATTRIBUTE_get0_type(a, i);
48
2.30M
        type = at->type;
49
50
2.30M
        switch (type) {
51
352
        case V_ASN1_PRINTABLESTRING:
52
733
        case V_ASN1_T61STRING:
53
1.57k
        case V_ASN1_NUMERICSTRING:
54
2.26k
        case V_ASN1_UTF8STRING:
55
2.58k
        case V_ASN1_IA5STRING:
56
2.58k
            bs = at->value.asn1_string;
57
2.58k
            if (BIO_write(bp, (char *)bs->data, bs->length) != bs->length)
58
0
                goto err;
59
2.58k
            if (BIO_puts(bp, "\n") <= 0)
60
0
                goto err;
61
2.58k
            break;
62
41.9k
        case V_ASN1_SEQUENCE:
63
41.9k
            if (BIO_puts(bp, "\n") <= 0)
64
0
                goto err;
65
41.9k
            ASN1_parse_dump(bp, at->value.sequence->data,
66
41.9k
                            at->value.sequence->length, i, 1);
67
41.9k
            break;
68
2.25M
        default:
69
2.25M
            if (BIO_printf(bp, "unable to print attribute of type 0x%X\n",
70
2.25M
                           type) < 0)
71
0
                goto err;
72
2.25M
            break;
73
2.30M
        }
74
2.30M
    }
75
82.3k
    ret = 1;
76
82.3k
err:
77
82.3k
    return ret;
78
82.3k
}
79
80
int X509_ACERT_print_ex(BIO *bp, X509_ACERT *x, unsigned long nmflags,
81
                        unsigned long cflag)
82
7.43k
{
83
7.43k
    int i;
84
7.43k
    char mlch = ' ';
85
86
7.43k
    if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
87
0
        mlch = '\n';
88
0
    }
89
90
7.43k
    if ((cflag & X509_FLAG_NO_HEADER) == 0) {
91
7.43k
        if (BIO_printf(bp, "Attribute Certificate:\n") <= 0)
92
0
            goto err;
93
7.43k
        if (BIO_printf(bp, "%4sData:\n", "") <= 0)
94
0
            goto err;
95
7.43k
    }
96
97
7.43k
    if ((cflag & X509_FLAG_NO_VERSION) == 0) {
98
7.43k
        long l;
99
100
7.43k
        l = X509_ACERT_get_version(x);
101
7.43k
        if (l == X509_ACERT_VERSION_2) {
102
1.40k
            if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
103
1.40k
                           (unsigned long)l) <= 0)
104
0
                goto err;
105
6.02k
        } else {
106
6.02k
            if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
107
0
                goto err;
108
6.02k
        }
109
7.43k
    }
110
111
7.43k
    if ((cflag & X509_FLAG_NO_SERIAL) == 0) {
112
7.43k
        const ASN1_INTEGER *serial;
113
114
7.43k
        serial = X509_ACERT_get0_serialNumber(x);
115
116
7.43k
        if (BIO_printf(bp, "%8sSerial Number: ", "") <= 0)
117
0
            goto err;
118
119
7.43k
        if (i2a_ASN1_INTEGER(bp, serial) <= 0)
120
0
            goto err;
121
122
7.43k
        if (BIO_write(bp, "\n", 1) <= 0)
123
0
            goto err;
124
7.43k
    }
125
126
7.43k
    if ((cflag & X509_FLAG_NO_SUBJECT) == 0) {
127
7.43k
        const GENERAL_NAMES *holderEntities;
128
7.43k
        const OSSL_ISSUER_SERIAL *holder_bcid;
129
7.43k
        const X509_NAME *holderIssuer = NULL;
130
131
7.43k
        if (BIO_printf(bp, "%8sHolder:\n", "") <= 0)
132
0
            goto err;
133
134
7.43k
        holderEntities = X509_ACERT_get0_holder_entityName(x);
135
7.43k
        if (holderEntities != NULL) {
136
7.52k
            for (i = 0; i < sk_GENERAL_NAME_num(holderEntities); i++) {
137
4.16k
                GENERAL_NAME *entity;
138
139
4.16k
                entity = sk_GENERAL_NAME_value(holderEntities, i);
140
141
4.16k
                if (BIO_printf(bp, "%12sName:%c", "", mlch) <= 0)
142
0
                    goto err;
143
4.16k
                if (GENERAL_NAME_print(bp, entity) <= 0)
144
0
                    goto err;
145
4.16k
                if (BIO_write(bp, "\n", 1) <= 0)
146
0
                    goto err;
147
4.16k
            }
148
3.36k
        }
149
150
7.43k
        if ((holder_bcid = X509_ACERT_get0_holder_baseCertId(x)) != NULL)
151
4.13k
            holderIssuer = OSSL_ISSUER_SERIAL_get0_issuer(holder_bcid);
152
153
7.43k
        if (holderIssuer != NULL) {
154
1.67k
            const ASN1_INTEGER *holder_serial;
155
1.67k
            const ASN1_BIT_STRING *iuid;
156
157
1.67k
            if (BIO_printf(bp, "%12sIssuer:%c", "", mlch) <= 0)
158
0
                goto err;
159
160
1.67k
            if (X509_NAME_print_ex(bp, holderIssuer, 0, nmflags) <= 0)
161
0
                goto err;
162
163
1.67k
            if (BIO_write(bp, "\n", 1) <= 0)
164
0
                goto err;
165
166
1.67k
            if (BIO_printf(bp, "%12sSerial: ", "") <= 0)
167
0
                goto err;
168
169
1.67k
            holder_serial = OSSL_ISSUER_SERIAL_get0_serial(holder_bcid);
170
171
1.67k
            if (i2a_ASN1_INTEGER(bp, holder_serial) <= 0)
172
0
                goto err;
173
174
1.67k
            iuid = OSSL_ISSUER_SERIAL_get0_issuerUID(holder_bcid);
175
1.67k
            if (iuid != NULL) {
176
0
                if (BIO_printf(bp, "%12sIssuer UID: ", "") <= 0)
177
0
                    goto err;
178
0
                if (X509_signature_dump(bp, iuid, 24) <= 0)
179
0
                    goto err;
180
0
            }
181
1.67k
            if (BIO_write(bp, "\n", 1) <= 0)
182
0
                goto err;
183
1.67k
        }
184
7.43k
    }
185
186
7.43k
    if ((cflag & X509_FLAG_NO_ISSUER) == 0) {
187
7.43k
        const X509_NAME *issuer;
188
189
7.43k
        if (BIO_printf(bp, "%8sIssuer:%c", "", mlch) <= 0)
190
0
            goto err;
191
7.43k
        issuer = X509_ACERT_get0_issuerName(x);
192
7.43k
        if (issuer) {
193
1.54k
            if (X509_NAME_print_ex(bp, issuer, 0, nmflags) < 0)
194
0
                goto err;
195
5.88k
        } else {
196
5.88k
            if (BIO_printf(bp, "Unsupported Issuer Type") <= 0)
197
0
                goto err;
198
5.88k
        }
199
7.43k
        if (BIO_write(bp, "\n", 1) <= 0)
200
0
            goto err;
201
7.43k
    }
202
203
7.43k
    if ((cflag & X509_FLAG_NO_VALIDITY) == 0) {
204
7.43k
        if (BIO_printf(bp, "%8sValidity\n", "") <= 0)
205
0
            goto err;
206
7.43k
        if (BIO_printf(bp, "%12sNot Before: ", "") <= 0)
207
0
            goto err;
208
7.43k
        if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notBefore(x)) == 0)
209
1.63k
            goto err;
210
5.80k
        if (BIO_printf(bp, "\n%12sNot After : ", "") <= 0)
211
0
            goto err;
212
5.80k
        if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notAfter(x)) == 0)
213
249
            goto err;
214
5.55k
        if (BIO_write(bp, "\n", 1) <= 0)
215
0
            goto err;
216
5.55k
    }
217
218
5.55k
    if ((cflag & X509_FLAG_NO_ATTRIBUTES) == 0) {
219
5.55k
        if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
220
0
            goto err;
221
222
5.55k
        if (X509_ACERT_get_attr_count(x) == 0) {
223
908
            if (BIO_printf(bp, "%12s(none)\n", "") <= 0)
224
0
                goto err;
225
4.64k
        } else {
226
86.9k
            for (i = 0; i < X509_ACERT_get_attr_count(x); i++) {
227
82.3k
                if (print_attribute(bp, X509_ACERT_get_attr(x, i)) == 0)
228
37
                    goto err;
229
82.3k
            }
230
4.64k
        }
231
5.55k
    }
232
233
5.51k
    if ((cflag & X509_FLAG_NO_EXTENSIONS) == 0) {
234
5.51k
        const STACK_OF(X509_EXTENSION) *exts;
235
236
5.51k
        exts = X509_ACERT_get0_extensions(x);
237
5.51k
        if (exts != NULL) {
238
2.89k
            if (BIO_printf(bp, "%8sExtensions:\n", "") <= 0)
239
0
                goto err;
240
9.57k
            for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
241
6.67k
                ASN1_OBJECT *obj;
242
6.67k
                X509_EXTENSION *ex;
243
6.67k
                int critical;
244
245
6.67k
                ex = sk_X509_EXTENSION_value(exts, i);
246
6.67k
                if (BIO_printf(bp, "%12s", "") <= 0)
247
0
                    goto err;
248
6.67k
                obj = X509_EXTENSION_get_object(ex);
249
6.67k
                if (i2a_ASN1_OBJECT(bp, obj) <= 0)
250
0
                    goto err;
251
6.67k
                critical = X509_EXTENSION_get_critical(ex);
252
6.67k
                if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
253
0
                    goto err;
254
6.67k
                if (X509V3_EXT_print(bp, ex, cflag, 20) <= 0) {
255
4.70k
                    if (BIO_printf(bp, "%16s", "") <= 0)
256
0
                        goto err;
257
4.70k
                    if (ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex)) <= 0)
258
0
                        goto err;
259
4.70k
                }
260
6.67k
                if (BIO_write(bp, "\n", 1) <= 0)
261
0
                    goto err;
262
6.67k
            }
263
2.89k
        }
264
5.51k
    }
265
266
5.51k
    if ((cflag & X509_FLAG_NO_SIGDUMP) == 0) {
267
5.51k
        const X509_ALGOR *sig_alg;
268
5.51k
        const ASN1_BIT_STRING *sig;
269
270
5.51k
        X509_ACERT_get0_signature(x, &sig, &sig_alg);
271
5.51k
        if (X509_signature_print(bp, sig_alg, sig) <= 0)
272
0
            return 0;
273
5.51k
    }
274
275
5.51k
    return 1;
276
277
1.91k
err:
278
1.91k
    ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
279
1.91k
    return 0;
280
5.51k
}
281
282
int X509_ACERT_print(BIO *bp, X509_ACERT *x)
283
7.43k
{
284
7.43k
    return X509_ACERT_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
285
7.43k
}