Coverage Report

Created: 2025-07-23 06:08

/src/openssl/crypto/x509/x509_set.c
Line
Count
Source (jump to first uncovered line)
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
/*
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
0
{
51
0
    ASN1_INTEGER *in;
52
53
0
    if (x == NULL)
54
0
        return 0;
55
0
    in = &x->cert_info.serialNumber;
56
0
    if (in != serial)
57
0
        return ASN1_STRING_copy(in, serial);
58
0
    x->cert_info.enc.modified = 1;
59
0
    return 1;
60
0
}
61
62
int X509_set_issuer_name(X509 *x, const X509_NAME *name)
63
0
{
64
0
    if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
65
0
        return 0;
66
0
    x->cert_info.enc.modified = 1;
67
0
    return 1;
68
0
}
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
0
{
122
0
    int i;
123
124
0
    if (CRYPTO_UP_REF(&x->references, &i) <= 0)
125
0
        return 0;
126
127
0
    REF_PRINT_COUNT("X509", i, x);
128
0
    REF_ASSERT_ISNT(i < 2);
129
0
    return i > 1;
130
0
}
131
132
long X509_get_version(const X509 *x)
133
0
{
134
0
    return ASN1_INTEGER_get(x->cert_info.version);
135
0
}
136
137
const ASN1_TIME *X509_get0_notBefore(const X509 *x)
138
0
{
139
0
    return x->cert_info.validity.notBefore;
140
0
}
141
142
const ASN1_TIME *X509_get0_notAfter(const X509 *x)
143
0
{
144
0
    return x->cert_info.validity.notAfter;
145
0
}
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
0
{
164
0
    return x->cert_info.key;
165
0
}
166
167
const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
168
0
{
169
0
    return x->cert_info.extensions;
170
0
}
171
172
void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
173
                    const ASN1_BIT_STRING **psuid)
174
0
{
175
0
    if (piuid != NULL)
176
0
        *piuid = x->cert_info.issuerUID;
177
0
    if (psuid != NULL)
178
0
        *psuid = x->cert_info.subjectUID;
179
0
}
180
181
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
182
0
{
183
0
    return &x->cert_info.signature;
184
0
}
185
186
int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
187
                      int *secbits, uint32_t *flags)
188
0
{
189
0
    if (mdnid != NULL)
190
0
        *mdnid = siginf->mdnid;
191
0
    if (pknid != NULL)
192
0
        *pknid = siginf->pknid;
193
0
    if (secbits != NULL)
194
0
        *secbits = siginf->secbits;
195
0
    if (flags != NULL)
196
0
        *flags = siginf->flags;
197
0
    return (siginf->flags & X509_SIG_INFO_VALID) != 0;
198
0
}
199
200
void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
201
                       int secbits, uint32_t flags)
202
0
{
203
0
    siginf->mdnid = mdnid;
204
0
    siginf->pknid = pknid;
205
0
    siginf->secbits = secbits;
206
0
    siginf->flags = flags;
207
0
}
208
209
int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
210
                            uint32_t *flags)
211
0
{
212
0
    X509_check_purpose(x, -1, -1);
213
0
    return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
214
0
}
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
0
{
220
0
    int pknid, mdnid, md_size;
221
0
    const EVP_MD *md;
222
0
    const EVP_PKEY_ASN1_METHOD *ameth;
223
224
0
    siginf->mdnid = NID_undef;
225
0
    siginf->pknid = NID_undef;
226
0
    siginf->secbits = -1;
227
0
    siginf->flags = 0;
228
0
    if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
229
0
            || pknid == NID_undef) {
230
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
231
0
        return 0;
232
0
    }
233
0
    siginf->mdnid = mdnid;
234
0
    siginf->pknid = pknid;
235
236
0
    switch (mdnid) {
237
0
    case NID_undef:
238
        /* If we have one, use a custom handler for this algorithm */
239
0
        ameth = EVP_PKEY_asn1_find(NULL, pknid);
240
0
        if (ameth != NULL && ameth->siginf_set != NULL
241
0
                && ameth->siginf_set(siginf, alg, sig))
242
0
           break;
243
0
        if (pubkey != NULL) {
244
0
            int secbits;
245
246
0
            secbits = EVP_PKEY_get_security_bits(pubkey);
247
0
            if (secbits != 0) {
248
0
                siginf->secbits = secbits;
249
0
                break;
250
0
            }
251
0
        }
252
0
        ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
253
0
        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
0
    case NID_sha1:
261
        /*
262
         * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
263
         * for SHA1 at2^63.4
264
         */
265
0
        siginf->secbits = 63;
266
0
        break;
267
0
    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
0
        siginf->secbits = 39;
273
0
        break;
274
0
    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
0
        siginf->secbits = 105;
280
0
        break;
281
0
    default:
282
        /* Security bits: half number of bits in digest */
283
0
        if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
284
0
            ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
285
0
            return 0;
286
0
        }
287
0
        md_size = EVP_MD_get_size(md);
288
0
        if (md_size <= 0)
289
0
            return 0;
290
0
        siginf->secbits = md_size * 4;
291
0
        break;
292
0
    }
293
0
    switch (mdnid) {
294
0
    case NID_sha1:
295
0
    case NID_sha256:
296
0
    case NID_sha384:
297
0
    case NID_sha512:
298
0
        siginf->flags |= X509_SIG_INFO_TLS;
299
0
    }
300
0
    siginf->flags |= X509_SIG_INFO_VALID;
301
0
    return 1;
302
0
}
303
304
/* Returns 1 on success, 0 on failure */
305
int ossl_x509_init_sig_info(X509 *x)
306
0
{
307
0
    return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature,
308
0
                              X509_PUBKEY_get0(x->cert_info.key));
309
0
}