Coverage Report

Created: 2026-09-12 06:55

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