Coverage Report

Created: 2025-12-04 06:33

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
42.7k
{
51
42.7k
    ASN1_INTEGER *in;
52
53
42.7k
    if (x == NULL)
54
0
        return 0;
55
42.7k
    in = &x->cert_info.serialNumber;
56
42.7k
    if (in != serial)
57
42.7k
        return ASN1_STRING_copy(in, serial);
58
0
    x->cert_info.enc.modified = 1;
59
0
    return 1;
60
42.7k
}
61
62
int X509_set_issuer_name(X509 *x, const X509_NAME *name)
63
36.1k
{
64
36.1k
    if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
65
0
        return 0;
66
36.1k
    x->cert_info.enc.modified = 1;
67
36.1k
    return 1;
68
36.1k
}
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
437k
{
122
437k
    int i;
123
124
437k
    if (CRYPTO_UP_REF(&x->references, &i) <= 0)
125
0
        return 0;
126
127
437k
    REF_PRINT_COUNT("X509", i, x);
128
437k
    REF_ASSERT_ISNT(i < 2);
129
437k
    return i > 1;
130
437k
}
131
132
long X509_get_version(const X509 *x)
133
261k
{
134
261k
    return ASN1_INTEGER_get(x->cert_info.version);
135
261k
}
136
137
const ASN1_TIME *X509_get0_notBefore(const X509 *x)
138
72.2k
{
139
72.2k
    return x->cert_info.validity.notBefore;
140
72.2k
}
141
142
const ASN1_TIME *X509_get0_notAfter(const X509 *x)
143
71.2k
{
144
71.2k
    return x->cert_info.validity.notAfter;
145
71.2k
}
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.8k
{
164
40.8k
    return x->cert_info.key;
165
40.8k
}
166
167
const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
168
48.5k
{
169
48.5k
    return x->cert_info.extensions;
170
48.5k
}
171
172
void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
173
                    const ASN1_BIT_STRING **psuid)
174
40.8k
{
175
40.8k
    if (piuid != NULL)
176
40.8k
        *piuid = x->cert_info.issuerUID;
177
40.8k
    if (psuid != NULL)
178
40.8k
        *psuid = x->cert_info.subjectUID;
179
40.8k
}
180
181
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
182
40.9k
{
183
40.9k
    return &x->cert_info.signature;
184
40.9k
}
185
186
int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
187
                      int *secbits, uint32_t *flags)
188
24.7k
{
189
24.7k
    if (mdnid != NULL)
190
24.7k
        *mdnid = siginf->mdnid;
191
24.7k
    if (pknid != NULL)
192
24.7k
        *pknid = siginf->pknid;
193
24.7k
    if (secbits != NULL)
194
0
        *secbits = siginf->secbits;
195
24.7k
    if (flags != NULL)
196
0
        *flags = siginf->flags;
197
24.7k
    return (siginf->flags & X509_SIG_INFO_VALID) != 0;
198
24.7k
}
199
200
void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
201
                       int secbits, uint32_t flags)
202
611
{
203
611
    siginf->mdnid = mdnid;
204
611
    siginf->pknid = pknid;
205
611
    siginf->secbits = secbits;
206
611
    siginf->flags = flags;
207
611
}
208
209
int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
210
                            uint32_t *flags)
211
24.7k
{
212
24.7k
    X509_check_purpose(x, -1, -1);
213
24.7k
    return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
214
24.7k
}
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
133k
{
220
133k
    int pknid, mdnid, md_size;
221
133k
    const EVP_MD *md;
222
133k
    const EVP_PKEY_ASN1_METHOD *ameth;
223
224
133k
    siginf->mdnid = NID_undef;
225
133k
    siginf->pknid = NID_undef;
226
133k
    siginf->secbits = -1;
227
133k
    siginf->flags = 0;
228
133k
    if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
229
119k
            || pknid == NID_undef) {
230
14.2k
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
231
14.2k
        return 0;
232
14.2k
    }
233
119k
    siginf->mdnid = mdnid;
234
119k
    siginf->pknid = pknid;
235
236
119k
    switch (mdnid) {
237
1.33k
    case NID_undef:
238
        /* If we have one, use a custom handler for this algorithm */
239
1.33k
        ameth = EVP_PKEY_asn1_find(NULL, pknid);
240
1.33k
        if (ameth != NULL && ameth->siginf_set != NULL
241
1.18k
                && ameth->siginf_set(siginf, alg, sig))
242
428
           break;
243
908
        if (pubkey != NULL) {
244
639
            int secbits;
245
246
639
            secbits = EVP_PKEY_get_security_bits(pubkey);
247
639
            if (secbits != 0) {
248
626
                siginf->secbits = secbits;
249
626
                break;
250
626
            }
251
639
        }
252
908
        ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
253
282
        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.87k
    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.87k
        siginf->secbits = 63;
266
3.87k
        break;
267
369
    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
369
        siginf->secbits = 39;
273
369
        break;
274
11
    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
11
        siginf->secbits = 105;
280
11
        break;
281
114k
    default:
282
        /* Security bits: half number of bits in digest */
283
114k
        if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
284
428
            ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
285
428
            return 0;
286
428
        }
287
113k
        md_size = EVP_MD_get_size(md);
288
113k
        if (md_size <= 0)
289
0
            return 0;
290
113k
        siginf->secbits = md_size * 4;
291
113k
        break;
292
119k
    }
293
118k
    switch (mdnid) {
294
3.87k
    case NID_sha1:
295
116k
    case NID_sha256:
296
116k
    case NID_sha384:
297
117k
    case NID_sha512:
298
117k
        siginf->flags |= X509_SIG_INFO_TLS;
299
118k
    }
300
118k
    siginf->flags |= X509_SIG_INFO_VALID;
301
118k
    return 1;
302
118k
}
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
}