Coverage Report

Created: 2026-10-28 06:28

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