Coverage Report

Created: 2026-07-12 07:21

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