Coverage Report

Created: 2026-04-01 06:39

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