Coverage Report

Created: 2025-11-16 06:40

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