Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/x509/x509_set.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 "internal/refcount.h"
13
#include <openssl/asn1.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include <openssl/x509.h>
17
#include <openssl/x509v3.h>
18
#include "crypto/asn1.h"
19
#include "crypto/x509.h"
20
#include "x509_local.h"
21
22
int X509_set_version(X509 *x, long version)
23
0
{
24
0
    if (x == NULL)
25
0
        return 0;
26
0
    if (version == X509_get_version(x))
27
0
        return 1; /* avoid needless modification even re-allocation */
28
0
    if (version == X509_VERSION_1) {
29
0
        ASN1_INTEGER_free(x->cert_info.version);
30
0
        x->cert_info.version = NULL;
31
0
        x->cert_info.enc.modified = 1;
32
0
        return 1;
33
0
    }
34
0
    if (x->cert_info.version == NULL) {
35
0
        if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
36
0
            return 0;
37
0
    }
38
0
    if (!ASN1_INTEGER_set(x->cert_info.version, version))
39
0
        return 0;
40
0
    x->cert_info.enc.modified = 1;
41
0
    return 1;
42
0
}
43
44
int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
45
31.2k
{
46
31.2k
    ASN1_INTEGER *in;
47
48
31.2k
    if (x == NULL)
49
0
        return 0;
50
31.2k
    in = &x->cert_info.serialNumber;
51
31.2k
    if (in != serial)
52
31.2k
        return ASN1_STRING_copy(in, serial);
53
0
    x->cert_info.enc.modified = 1;
54
0
    return 1;
55
31.2k
}
56
57
int X509_set_issuer_name(X509 *x, const X509_NAME *name)
58
37.7k
{
59
37.7k
    if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
60
0
        return 0;
61
37.7k
    x->cert_info.enc.modified = 1;
62
37.7k
    return 1;
63
37.7k
}
64
65
int X509_set_subject_name(X509 *x, const X509_NAME *name)
66
0
{
67
0
    if (x == NULL || !X509_NAME_set(&x->cert_info.subject, name))
68
0
        return 0;
69
0
    x->cert_info.enc.modified = 1;
70
0
    return 1;
71
0
}
72
73
int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm)
74
0
{
75
0
    ASN1_TIME *new;
76
77
0
    if (*ptm == tm)
78
0
        return 1;
79
0
    new = ASN1_STRING_dup(tm);
80
0
    if (tm != NULL && new == NULL)
81
0
        return 0;
82
0
    ASN1_TIME_free(*ptm);
83
0
    *ptm = new;
84
0
    if (modified != NULL)
85
0
        *modified = 1;
86
0
    return 1;
87
0
}
88
89
int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
90
0
{
91
0
    if (x == NULL || tm == NULL)
92
0
        return 0;
93
0
    return ossl_x509_set1_time(&x->cert_info.enc.modified,
94
0
        &x->cert_info.validity.notBefore, tm);
95
0
}
96
97
int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
98
0
{
99
0
    if (x == NULL || tm == NULL)
100
0
        return 0;
101
0
    return ossl_x509_set1_time(&x->cert_info.enc.modified,
102
0
        &x->cert_info.validity.notAfter, tm);
103
0
}
104
105
int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
106
0
{
107
0
    if (x == NULL)
108
0
        return 0;
109
0
    if (!X509_PUBKEY_set(&(x->cert_info.key), pkey))
110
0
        return 0;
111
0
    x->cert_info.enc.modified = 1;
112
0
    return 1;
113
0
}
114
115
int X509_up_ref(X509 *x)
116
319k
{
117
319k
    int i;
118
119
319k
    if (CRYPTO_UP_REF(&x->references, &i) <= 0)
120
0
        return 0;
121
122
319k
    REF_PRINT_COUNT("X509", i, x);
123
319k
    REF_ASSERT_ISNT(i < 2);
124
319k
    return i > 1;
125
319k
}
126
127
long X509_get_version(const X509 *x)
128
170k
{
129
170k
    return ASN1_INTEGER_get(x->cert_info.version);
130
170k
}
131
132
const ASN1_TIME *X509_get0_notBefore(const X509 *x)
133
64.2k
{
134
64.2k
    return x->cert_info.validity.notBefore;
135
64.2k
}
136
137
const ASN1_TIME *X509_get0_notAfter(const X509 *x)
138
61.7k
{
139
61.7k
    return x->cert_info.validity.notAfter;
140
61.7k
}
141
142
ASN1_TIME *X509_getm_notBefore(const X509 *x)
143
0
{
144
0
    return x->cert_info.validity.notBefore;
145
0
}
146
147
ASN1_TIME *X509_getm_notAfter(const X509 *x)
148
0
{
149
0
    return x->cert_info.validity.notAfter;
150
0
}
151
152
int X509_get_signature_type(const X509 *x)
153
0
{
154
0
    return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
155
0
}
156
157
X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
158
36.7k
{
159
36.7k
    return x->cert_info.key;
160
36.7k
}
161
162
const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
163
44.5k
{
164
44.5k
    return x->cert_info.extensions;
165
44.5k
}
166
167
void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
168
    const ASN1_BIT_STRING **psuid)
169
36.7k
{
170
36.7k
    if (piuid != NULL)
171
36.7k
        *piuid = x->cert_info.issuerUID;
172
36.7k
    if (psuid != NULL)
173
36.7k
        *psuid = x->cert_info.subjectUID;
174
36.7k
}
175
176
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
177
36.8k
{
178
36.8k
    return &x->cert_info.signature;
179
36.8k
}
180
181
int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
182
    int *secbits, uint32_t *flags)
183
12.3k
{
184
12.3k
    if (mdnid != NULL)
185
12.3k
        *mdnid = siginf->mdnid;
186
12.3k
    if (pknid != NULL)
187
12.3k
        *pknid = siginf->pknid;
188
12.3k
    if (secbits != NULL)
189
0
        *secbits = siginf->secbits;
190
12.3k
    if (flags != NULL)
191
0
        *flags = siginf->flags;
192
12.3k
    return (siginf->flags & X509_SIG_INFO_VALID) != 0;
193
12.3k
}
194
195
void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
196
    int secbits, uint32_t flags)
197
1.74k
{
198
1.74k
    siginf->mdnid = mdnid;
199
1.74k
    siginf->pknid = pknid;
200
1.74k
    siginf->secbits = secbits;
201
1.74k
    siginf->flags = flags;
202
1.74k
}
203
204
int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
205
    uint32_t *flags)
206
8.20k
{
207
8.20k
    X509_check_purpose(x, -1, -1);
208
8.20k
    return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
209
8.20k
}
210
211
/* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
212
static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
213
    const ASN1_STRING *sig, const EVP_PKEY *pubkey)
214
89.6k
{
215
89.6k
    int pknid, mdnid, md_size;
216
89.6k
    const EVP_MD *md;
217
89.6k
    const EVP_PKEY_ASN1_METHOD *ameth;
218
219
89.6k
    siginf->mdnid = NID_undef;
220
89.6k
    siginf->pknid = NID_undef;
221
89.6k
    siginf->secbits = -1;
222
89.6k
    siginf->flags = 0;
223
89.6k
    if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
224
74.5k
        || pknid == NID_undef) {
225
15.0k
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
226
15.0k
        return 0;
227
15.0k
    }
228
74.5k
    siginf->mdnid = mdnid;
229
74.5k
    siginf->pknid = pknid;
230
231
74.5k
    switch (mdnid) {
232
2.53k
    case NID_undef:
233
        /* If we have one, use a custom handler for this algorithm */
234
2.53k
        ameth = EVP_PKEY_asn1_find(NULL, pknid);
235
2.53k
        if (ameth != NULL && ameth->siginf_set != NULL
236
2.42k
            && ameth->siginf_set(siginf, alg, sig))
237
1.71k
            break;
238
819
        if (pubkey != NULL) {
239
590
            int secbits;
240
241
590
            secbits = EVP_PKEY_get_security_bits(pubkey);
242
590
            if (secbits != 0) {
243
570
                siginf->secbits = secbits;
244
570
                break;
245
570
            }
246
590
        }
247
819
        ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
248
249
        return 0;
249
        /*
250
         * SHA1 and MD5 are known to be broken. Reduce security bits so that
251
         * they're no longer accepted at security level 1.
252
         * The real values don't really matter as long as they're lower than 80,
253
         * which is our security level 1.
254
         */
255
3.74k
    case NID_sha1:
256
        /*
257
         * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
258
         * for SHA1 at2^63.4
259
         */
260
3.74k
        siginf->secbits = 63;
261
3.74k
        break;
262
495
    case NID_md5:
263
        /*
264
         * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
265
         * puts a chosen-prefix attack for MD5 at 2^39.
266
         */
267
495
        siginf->secbits = 39;
268
495
        break;
269
31
    case NID_id_GostR3411_94:
270
        /*
271
         * There is a collision attack on GOST R 34.11-94 at 2^105, see
272
         * https://link.springer.com/chapter/10.1007%2F978-3-540-85174-5_10
273
         */
274
31
        siginf->secbits = 105;
275
31
        break;
276
67.7k
    default:
277
        /* Security bits: half number of bits in digest */
278
67.7k
        if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
279
281
            ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
280
281
            return 0;
281
281
        }
282
67.4k
        md_size = EVP_MD_get_size(md);
283
67.4k
        if (md_size <= 0)
284
0
            return 0;
285
67.4k
        siginf->secbits = md_size * 4;
286
67.4k
        break;
287
74.5k
    }
288
74.0k
    switch (mdnid) {
289
3.74k
    case NID_sha1:
290
69.8k
    case NID_sha256:
291
70.2k
    case NID_sha384:
292
70.8k
    case NID_sha512:
293
70.8k
        siginf->flags |= X509_SIG_INFO_TLS;
294
74.0k
    }
295
74.0k
    siginf->flags |= X509_SIG_INFO_VALID;
296
74.0k
    return 1;
297
74.0k
}
298
299
/* Returns 1 on success, 0 on failure */
300
int ossl_x509_init_sig_info(X509 *x)
301
107k
{
302
107k
    return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature,
303
107k
        X509_PUBKEY_get0(x->cert_info.key));
304
107k
}