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
X509_set_version(X509 * x,long version)27 int X509_set_version(X509 *x, long version)
28 {
29 if (x == NULL)
30 return 0;
31 if (version == X509_get_version(x))
32 return 1; /* avoid needless modification even re-allocation */
33 if (version == X509_VERSION_1) {
34 ASN1_INTEGER_free(x->cert_info.version);
35 x->cert_info.version = NULL;
36 x->cert_info.enc.modified = 1;
37 return 1;
38 }
39 if (x->cert_info.version == NULL) {
40 if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
41 return 0;
42 }
43 if (!ASN1_INTEGER_set(x->cert_info.version, version))
44 return 0;
45 x->cert_info.enc.modified = 1;
46 return 1;
47 }
48
X509_set_serialNumber(X509 * x,ASN1_INTEGER * serial)49 int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
50 {
51 ASN1_INTEGER *in;
52
53 if (x == NULL)
54 return 0;
55 in = &x->cert_info.serialNumber;
56 if (in != serial)
57 return ASN1_STRING_copy(in, serial);
58 x->cert_info.enc.modified = 1;
59 return 1;
60 }
61
X509_set_issuer_name(X509 * x,const X509_NAME * name)62 int X509_set_issuer_name(X509 *x, const X509_NAME *name)
63 {
64 if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
65 return 0;
66 x->cert_info.enc.modified = 1;
67 return 1;
68 }
69
X509_set_subject_name(X509 * x,const X509_NAME * name)70 int X509_set_subject_name(X509 *x, const X509_NAME *name)
71 {
72 if (x == NULL || !X509_NAME_set(&x->cert_info.subject, name))
73 return 0;
74 x->cert_info.enc.modified = 1;
75 return 1;
76 }
77
ossl_x509_set1_time(int * modified,ASN1_TIME ** ptm,const ASN1_TIME * tm)78 int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm)
79 {
80 ASN1_TIME *new;
81
82 if (*ptm == tm)
83 return 1;
84 new = ASN1_STRING_dup(tm);
85 if (tm != NULL && new == NULL)
86 return 0;
87 ASN1_TIME_free(*ptm);
88 *ptm = new;
89 if (modified != NULL)
90 *modified = 1;
91 return 1;
92 }
93
X509_set1_notBefore(X509 * x,const ASN1_TIME * tm)94 int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
95 {
96 if (x == NULL || tm == NULL)
97 return 0;
98 return ossl_x509_set1_time(&x->cert_info.enc.modified,
99 &x->cert_info.validity.notBefore, tm);
100 }
101
X509_set1_notAfter(X509 * x,const ASN1_TIME * tm)102 int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
103 {
104 if (x == NULL || tm == NULL)
105 return 0;
106 return ossl_x509_set1_time(&x->cert_info.enc.modified,
107 &x->cert_info.validity.notAfter, tm);
108 }
109
X509_set_pubkey(X509 * x,EVP_PKEY * pkey)110 int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
111 {
112 if (x == NULL)
113 return 0;
114 if (!X509_PUBKEY_set(&(x->cert_info.key), pkey))
115 return 0;
116 x->cert_info.enc.modified = 1;
117 return 1;
118 }
119
X509_up_ref(X509 * x)120 int X509_up_ref(X509 *x)
121 {
122 int i;
123
124 if (CRYPTO_UP_REF(&x->references, &i) <= 0)
125 return 0;
126
127 REF_PRINT_COUNT("X509", i, x);
128 REF_ASSERT_ISNT(i < 2);
129 return i > 1;
130 }
131
X509_get_version(const X509 * x)132 long X509_get_version(const X509 *x)
133 {
134 return ASN1_INTEGER_get(x->cert_info.version);
135 }
136
X509_get0_notBefore(const X509 * x)137 const ASN1_TIME *X509_get0_notBefore(const X509 *x)
138 {
139 return x->cert_info.validity.notBefore;
140 }
141
X509_get0_notAfter(const X509 * x)142 const ASN1_TIME *X509_get0_notAfter(const X509 *x)
143 {
144 return x->cert_info.validity.notAfter;
145 }
146
X509_getm_notBefore(const X509 * x)147 ASN1_TIME *X509_getm_notBefore(const X509 *x)
148 {
149 return x->cert_info.validity.notBefore;
150 }
151
X509_getm_notAfter(const X509 * x)152 ASN1_TIME *X509_getm_notAfter(const X509 *x)
153 {
154 return x->cert_info.validity.notAfter;
155 }
156
X509_get_signature_type(const X509 * x)157 int X509_get_signature_type(const X509 *x)
158 {
159 return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
160 }
161
X509_get_X509_PUBKEY(const X509 * x)162 X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
163 {
164 return x->cert_info.key;
165 }
166
STACK_OF(X509_EXTENSION)167 const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
168 {
169 return x->cert_info.extensions;
170 }
171
X509_get0_uids(const X509 * x,const ASN1_BIT_STRING ** piuid,const ASN1_BIT_STRING ** psuid)172 void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
173 const ASN1_BIT_STRING **psuid)
174 {
175 if (piuid != NULL)
176 *piuid = x->cert_info.issuerUID;
177 if (psuid != NULL)
178 *psuid = x->cert_info.subjectUID;
179 }
180
X509_get0_tbs_sigalg(const X509 * x)181 const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
182 {
183 return &x->cert_info.signature;
184 }
185
X509_SIG_INFO_get(const X509_SIG_INFO * siginf,int * mdnid,int * pknid,int * secbits,uint32_t * flags)186 int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
187 int *secbits, uint32_t *flags)
188 {
189 if (mdnid != NULL)
190 *mdnid = siginf->mdnid;
191 if (pknid != NULL)
192 *pknid = siginf->pknid;
193 if (secbits != NULL)
194 *secbits = siginf->secbits;
195 if (flags != NULL)
196 *flags = siginf->flags;
197 return (siginf->flags & X509_SIG_INFO_VALID) != 0;
198 }
199
X509_SIG_INFO_set(X509_SIG_INFO * siginf,int mdnid,int pknid,int secbits,uint32_t flags)200 void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
201 int secbits, uint32_t flags)
202 {
203 siginf->mdnid = mdnid;
204 siginf->pknid = pknid;
205 siginf->secbits = secbits;
206 siginf->flags = flags;
207 }
208
X509_get_signature_info(X509 * x,int * mdnid,int * pknid,int * secbits,uint32_t * flags)209 int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
210 uint32_t *flags)
211 {
212 X509_check_purpose(x, -1, -1);
213 return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
214 }
215
216 /* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
x509_sig_info_init(X509_SIG_INFO * siginf,const X509_ALGOR * alg,const ASN1_STRING * sig,const EVP_PKEY * pubkey)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 {
220 int pknid, mdnid, md_size;
221 const EVP_MD *md;
222 const EVP_PKEY_ASN1_METHOD *ameth;
223
224 siginf->mdnid = NID_undef;
225 siginf->pknid = NID_undef;
226 siginf->secbits = -1;
227 siginf->flags = 0;
228 if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
229 || pknid == NID_undef) {
230 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
231 return 0;
232 }
233 siginf->mdnid = mdnid;
234 siginf->pknid = pknid;
235
236 switch (mdnid) {
237 case NID_undef:
238 /* If we have one, use a custom handler for this algorithm */
239 ameth = EVP_PKEY_asn1_find(NULL, pknid);
240 if (ameth != NULL && ameth->siginf_set != NULL
241 && ameth->siginf_set(siginf, alg, sig))
242 break;
243 if (pubkey != NULL) {
244 int secbits;
245
246 secbits = EVP_PKEY_get_security_bits(pubkey);
247 if (secbits != 0) {
248 siginf->secbits = secbits;
249 break;
250 }
251 }
252 ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
253 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 case NID_sha1:
261 /*
262 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
263 * for SHA1 at2^63.4
264 */
265 siginf->secbits = 63;
266 break;
267 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 siginf->secbits = 39;
273 break;
274 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 siginf->secbits = 105;
280 break;
281 default:
282 /* Security bits: half number of bits in digest */
283 if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
284 ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
285 return 0;
286 }
287 md_size = EVP_MD_get_size(md);
288 if (md_size <= 0)
289 return 0;
290 siginf->secbits = md_size * 4;
291 break;
292 }
293 switch (mdnid) {
294 case NID_sha1:
295 case NID_sha256:
296 case NID_sha384:
297 case NID_sha512:
298 siginf->flags |= X509_SIG_INFO_TLS;
299 }
300 siginf->flags |= X509_SIG_INFO_VALID;
301 return 1;
302 }
303
304 /* Returns 1 on success, 0 on failure */
ossl_x509_init_sig_info(X509 * x)305 int ossl_x509_init_sig_info(X509 *x)
306 {
307 return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature,
308 X509_PUBKEY_get0(x->cert_info.key));
309 }
310