Coverage Report

Created: 2025-12-31 06:58

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