2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
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"
22 int X509_set_version(X509 *x, long version)
26 if (version == X509_get_version(x))
27 return 1; /* avoid needless modification even re-allocation */
28 if (version == X509_VERSION_1) {
29 ASN1_INTEGER_free(x->cert_info.version);
30 x->cert_info.version = NULL;
31 x->cert_info.enc.modified = 1;
34 if (x->cert_info.version == NULL) {
35 if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
38 if (!ASN1_INTEGER_set(x->cert_info.version, version))
40 x->cert_info.enc.modified = 1;
44 int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
50 in = &x->cert_info.serialNumber;
52 return ASN1_STRING_copy(in, serial);
53 x->cert_info.enc.modified = 1;
57 int X509_set_issuer_name(X509 *x, const X509_NAME *name)
59 if (x == NULL || !X509_NAME_set(&x->cert_info.issuer, name))
61 x->cert_info.enc.modified = 1;
65 int X509_set_subject_name(X509 *x, const X509_NAME *name)
67 if (x == NULL || !X509_NAME_set(&x->cert_info.subject, name))
69 x->cert_info.enc.modified = 1;
73 int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm)
79 new = ASN1_STRING_dup(tm);
80 if (tm != NULL && new == NULL)
89 int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm)
91 if (x == NULL || tm == NULL)
93 return ossl_x509_set1_time(&x->cert_info.enc.modified,
94 &x->cert_info.validity.notBefore, tm);
97 int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm)
99 if (x == NULL || tm == NULL)
101 return ossl_x509_set1_time(&x->cert_info.enc.modified,
102 &x->cert_info.validity.notAfter, tm);
105 int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
109 if (!X509_PUBKEY_set(&(x->cert_info.key), pkey))
111 x->cert_info.enc.modified = 1;
115 int X509_up_ref(X509 *x)
119 if (CRYPTO_UP_REF(&x->references, &i) <= 0)
122 REF_PRINT_COUNT("X509", x);
123 REF_ASSERT_ISNT(i < 2);
127 long X509_get_version(const X509 *x)
129 return ASN1_INTEGER_get(x->cert_info.version);
132 const ASN1_TIME *X509_get0_notBefore(const X509 *x)
134 return x->cert_info.validity.notBefore;
137 const ASN1_TIME *X509_get0_notAfter(const X509 *x)
139 return x->cert_info.validity.notAfter;
142 ASN1_TIME *X509_getm_notBefore(const X509 *x)
144 return x->cert_info.validity.notBefore;
147 ASN1_TIME *X509_getm_notAfter(const X509 *x)
149 return x->cert_info.validity.notAfter;
152 int X509_get_signature_type(const X509 *x)
154 return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
157 X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
159 return x->cert_info.key;
162 const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
164 return x->cert_info.extensions;
167 void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid,
168 const ASN1_BIT_STRING **psuid)
171 *piuid = x->cert_info.issuerUID;
173 *psuid = x->cert_info.subjectUID;
176 const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
178 return &x->cert_info.signature;
181 int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid,
182 int *secbits, uint32_t *flags)
185 *mdnid = siginf->mdnid;
187 *pknid = siginf->pknid;
189 *secbits = siginf->secbits;
191 *flags = siginf->flags;
192 return (siginf->flags & X509_SIG_INFO_VALID) != 0;
195 void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid,
196 int secbits, uint32_t flags)
198 siginf->mdnid = mdnid;
199 siginf->pknid = pknid;
200 siginf->secbits = secbits;
201 siginf->flags = flags;
204 int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits,
207 X509_check_purpose(x, -1, -1);
208 return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags);
211 /* Modify *siginf according to alg and sig. Return 1 on success, else 0. */
212 static int x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
213 const ASN1_STRING *sig, const EVP_PKEY *pubkey)
217 const EVP_PKEY_ASN1_METHOD *ameth;
219 siginf->mdnid = NID_undef;
220 siginf->pknid = NID_undef;
221 siginf->secbits = -1;
223 if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)
224 || pknid == NID_undef) {
225 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
228 siginf->mdnid = mdnid;
229 siginf->pknid = pknid;
233 /* If we have one, use a custom handler for this algorithm */
234 ameth = EVP_PKEY_asn1_find(NULL, pknid);
235 if (ameth != NULL && ameth->siginf_set != NULL
236 && ameth->siginf_set(siginf, alg, sig))
238 if (pubkey != NULL) {
241 secbits = EVP_PKEY_get_security_bits(pubkey);
243 siginf->secbits = secbits;
247 ERR_raise(ERR_LIB_X509, X509_R_ERROR_USING_SIGINF_SET);
250 * SHA1 and MD5 are known to be broken. Reduce security bits so that
251 * they're no longer accepted at security level 1.
252 * The real values don't really matter as long as they're lower than 80,
253 * which is our security level 1.
257 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
260 siginf->secbits = 63;
264 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
265 * puts a chosen-prefix attack for MD5 at 2^39.
267 siginf->secbits = 39;
269 case NID_id_GostR3411_94:
271 * There is a collision attack on GOST R 34.11-94 at 2^105, see
272 * https://link.springer.com/chapter/10.1007%2F978-3-540-85174-5_10
274 siginf->secbits = 105;
277 /* Security bits: half number of bits in digest */
278 if ((md = EVP_get_digestbynid(mdnid)) == NULL) {
279 ERR_raise(ERR_LIB_X509, X509_R_ERROR_GETTING_MD_BY_NID);
282 siginf->secbits = EVP_MD_get_size(md) * 4;
290 siginf->flags |= X509_SIG_INFO_TLS;
292 siginf->flags |= X509_SIG_INFO_VALID;
296 /* Returns 1 on success, 0 on failure */
297 int ossl_x509_init_sig_info(X509 *x)
299 return x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature,
300 X509_PUBKEY_get0(x->cert_info.key));