Coverage Report

Created: 2026-10-28 06:28

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