Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/x509/x509_set.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 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 == 0) {
27
0
        ASN1_INTEGER_free(x->cert_info.version);
28
0
        x->cert_info.version = NULL;
29
0
        return 1;
30
0
    }
31
0
    if (x->cert_info.version == NULL) {
32
0
        if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
33
0
            return 0;
34
0
    }
35
0
    return ASN1_INTEGER_set(x->cert_info.version, version);
36
0
}
37
38
int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
39
28.3k
{
40
28.3k
    ASN1_INTEGER *in;
41
42
28.3k
    if (x == NULL)
43
0
        return 0;
44
28.3k
    in = &x->cert_info.serialNumber;
45
28.3k
    if (in != serial)
46
28.3k
        return ASN1_STRING_copy(in, serial);
47
0
    return 1;
48
28.3k
}
49
50
int X509_set_issuer_name(X509 *x, const X509_NAME *name)
51
13.9k
{
52
13.9k
    if (x == NULL)
53
0
        return 0;
54
13.9k
    return X509_NAME_set(&x->cert_info.issuer, name);
55
13.9k
}
56
57
int X509_set_subject_name(X509 *x, const X509_NAME *name)
58
0
{
59
0
    if (x == NULL)
60
0
        return 0;
61
0
    return X509_NAME_set(&x->cert_info.subject, name);
62
0
}
63
64
int ossl_x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm)
65
0
{
66
0
    ASN1_TIME *in;
67
0
    in = *ptm;
68
0
    if (in != tm) {
69
0
        in = ASN1_STRING_dup(tm);
70
0
        if (in != NULL) {
71
0
            ASN1_TIME_free(*ptm);
72
0
            *ptm = in;
73
0
        }
74
0
    }
75
0
    return (in != NULL);
76
0
}
77
78
int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
79
0
{
80
0
    if (x == NULL)
81
0
        return 0;
82
0
    return ossl_x509_set1_time(&x->cert_info.validity.notBefore, tm);
83
0
}
84
85
int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
86
0
{
87
0
    if (x == NULL)
88
0
        return 0;
89
0
    return ossl_x509_set1_time(&x->cert_info.validity.notAfter, tm);
90
0
}
91
92
int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
93
0
{
94
0
    if (x == NULL)
95
0
        return 0;
96
0
    return X509_PUBKEY_set(&(x->cert_info.key), pkey);
97
0
}
98
99
int X509_up_ref(X509 *x)
100
90.4k
{
101
90.4k
    int i;
102
103
90.4k
    if (CRYPTO_UP_REF(&x->references, &i, x->lock) <= 0)
104
0
        return 0;
105
106
90.4k
    REF_PRINT_COUNT("X509", x);
107
90.4k
    REF_ASSERT_ISNT(i < 2);
108
90.4k
    return ((i > 1) ? 1 : 0);
109
90.4k
}
110
111
long X509_get_version(const X509 *x)
112
142k
{
113
142k
    return ASN1_INTEGER_get(x->cert_info.version);
114
142k
}
115
116
const ASN1_TIME *X509_get0_notBefore(const X509 *x)
117
42.1k
{
118
42.1k
    return x->cert_info.validity.notBefore;
119
42.1k
}
120
121
const ASN1_TIME *X509_get0_notAfter(const X509 *x)
122
42.0k
{
123
42.0k
    return x->cert_info.validity.notAfter;
124
42.0k
}
125
126
ASN1_TIME *X509_getm_notBefore(const X509 *x)
127
0
{
128
0
    return x->cert_info.validity.notBefore;
129
0
}
130
131
ASN1_TIME *X509_getm_notAfter(const X509 *x)
132
0
{
133
0
    return x->cert_info.validity.notAfter;
134
0
}
135
136
int X509_get_signature_type(const X509 *x)
137
0
{
138
0
    return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
139
0
}
140
141
X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
142
29.2k
{
143
29.2k
    return x->cert_info.key;
144
29.2k
}
145
146
const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
147
33.5k
{
148
33.5k
    return x->cert_info.extensions;
149
33.5k
}
150
151
void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
152
                    const ASN1_BIT_STRING **psuid)
153
29.2k
{
154
29.2k
    if (piuid != NULL)
155
29.2k
        *piuid = x->cert_info.issuerUID;
156
29.2k
    if (psuid != NULL)
157
29.2k
        *psuid = x->cert_info.subjectUID;
158
29.2k
}
159
160
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
161
29.3k
{
162
29.3k
    return &x->cert_info.signature;
163
29.3k
}
164
165
int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
166
                      int *secbits, uint32_t *flags)
167
27.1k
{
168
27.1k
    if (mdnid != NULL)
169
27.1k
        *mdnid = siginf->mdnid;
170
27.1k
    if (pknid != NULL)
171
27.1k
        *pknid = siginf->pknid;
172
27.1k
    if (secbits != NULL)
173
0
        *secbits = siginf->secbits;
174
27.1k
    if (flags != NULL)
175
0
        *flags = siginf->flags;
176
27.1k
    return (siginf->flags & X509_SIG_INFO_VALID) != 0;
177
27.1k
}
178
179
void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
180
                       int secbits, uint32_t flags)
181
470
{
182
470
    siginf->mdnid = mdnid;
183
470
    siginf->pknid = pknid;
184
470
    siginf->secbits = secbits;
185
470
    siginf->flags = flags;
186
470
}
187
188
int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
189
                            uint32_t *flags)
190
27.1k
{
191
27.1k
    X509_check_purpose(x, -1, -1);
192
27.1k
    return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
193
27.1k
}
194
195
/* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
196
static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
197
                              const ASN1_STRING *sig)
198
43.1k
{
199
43.1k
    int pknid, mdnid;
200
43.1k
    const EVP_MD *md;
201
43.1k
    const EVP_PKEY_ASN1_METHOD *ameth;
202
203
43.1k
    siginf->mdnid = NID_undef;
204
43.1k
    siginf->pknid = NID_undef;
205
43.1k
    siginf->secbits = -1;
206
43.1k
    siginf->flags = 0;
207
43.1k
    if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
208
43.1k
            || pknid == NID_undef) {
209
8.22k
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
210
8.22k
        return 0;
211
8.22k
    }
212
34.9k
    siginf->mdnid = mdnid;
213
34.9k
    siginf->pknid = pknid;
214
215
34.9k
    switch (mdnid) {
216
460
    case NID_undef:
217
        /* If we have one, use a custom handler for this algorithm */
218
460
        ameth = EVP_PKEY_asn1_find(NULL, pknid);
219
460
        if (ameth == NULL || ameth->siginf_set == NULL
220
460
                || !ameth->siginf_set(siginf, alg, sig)) {
221
326
            ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
222
326
            return 0;
223
326
        }
224
134
        break;
225
        /*
226
         * SHA1 and MD5 are known to be broken. Reduce security bits so that
227
         * they're no longer accepted at security level 1.
228
         * The real values don't really matter as long as they're lower than 80,
229
         * which is our security level 1.
230
         */
231
1.28k
    case NID_sha1:
232
        /*
233
         * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
234
         * for SHA1 at2^63.4
235
         */
236
1.28k
        siginf->secbits = 63;
237
1.28k
        break;
238
172
    case NID_md5:
239
        /*
240
         * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
241
         * puts a chosen-prefix attack for MD5 at 2^39.
242
         */
243
172
        siginf->secbits = 39;
244
172
        break;
245
6
    case NID_id_GostR3411_94:
246
        /*
247
         * There is a collision attack on GOST R 34.11-94 at 2^105, see
248
         * https://link.springer.com/chapter/10.1007%2F978-3-540-85174-5_10
249
         */
250
6
        siginf->secbits = 105;
251
6
        break;
252
33.0k
    default:
253
        /* Security bits: half number of bits in digest */
254
33.0k
        if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
255
153
            ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
256
153
            return 0;
257
153
        }
258
32.8k
        siginf->secbits = EVP_MD_get_size(md) * 4;
259
32.8k
        break;
260
34.9k
    }
261
34.4k
    switch (mdnid) {
262
1.28k
    case NID_sha1:
263
33.7k
    case NID_sha256:
264
33.8k
    case NID_sha384:
265
34.0k
    case NID_sha512:
266
34.0k
        siginf->flags |= X509_SIG_INFO_TLS;
267
34.4k
    }
268
34.4k
    siginf->flags |= X509_SIG_INFO_VALID;
269
34.4k
    return 1;
270
34.4k
}
271
272
/* Returns 1 on success, 0 on failure */
273
int ossl_x509_init_sig_info(X509 *x)
274
108k
{
275
108k
    return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature);
276
108k
}