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
)
27 ASN1_INTEGER_free(x
->cert_info
.version
);
28 x
->cert_info
.version
= NULL
;
31 if (x
->cert_info
.version
== NULL
) {
32 if ((x
->cert_info
.version
= ASN1_INTEGER_new()) == NULL
)
35 return ASN1_INTEGER_set(x
->cert_info
.version
, version
);
38 int X509_set_serialNumber(X509
*x
, ASN1_INTEGER
*serial
)
44 in
= &x
->cert_info
.serialNumber
;
46 return ASN1_STRING_copy(in
, serial
);
50 int X509_set_issuer_name(X509
*x
, const X509_NAME
*name
)
54 return X509_NAME_set(&x
->cert_info
.issuer
, name
);
57 int X509_set_subject_name(X509
*x
, const X509_NAME
*name
)
61 return X509_NAME_set(&x
->cert_info
.subject
, name
);
64 int ossl_x509_set1_time(ASN1_TIME
**ptm
, const ASN1_TIME
*tm
)
69 in
= ASN1_STRING_dup(tm
);
78 int X509_set1_notBefore(X509
*x
, const ASN1_TIME
*tm
)
82 return ossl_x509_set1_time(&x
->cert_info
.validity
.notBefore
, tm
);
85 int X509_set1_notAfter(X509
*x
, const ASN1_TIME
*tm
)
89 return ossl_x509_set1_time(&x
->cert_info
.validity
.notAfter
, tm
);
92 int X509_set_pubkey(X509
*x
, EVP_PKEY
*pkey
)
96 return X509_PUBKEY_set(&(x
->cert_info
.key
), pkey
);
99 int X509_up_ref(X509
*x
)
103 if (CRYPTO_UP_REF(&x
->references
, &i
, x
->lock
) <= 0)
106 REF_PRINT_COUNT("X509", x
);
107 REF_ASSERT_ISNT(i
< 2);
108 return ((i
> 1) ? 1 : 0);
111 long X509_get_version(const X509
*x
)
113 return ASN1_INTEGER_get(x
->cert_info
.version
);
116 const ASN1_TIME
*X509_get0_notBefore(const X509
*x
)
118 return x
->cert_info
.validity
.notBefore
;
121 const ASN1_TIME
*X509_get0_notAfter(const X509
*x
)
123 return x
->cert_info
.validity
.notAfter
;
126 ASN1_TIME
*X509_getm_notBefore(const X509
*x
)
128 return x
->cert_info
.validity
.notBefore
;
131 ASN1_TIME
*X509_getm_notAfter(const X509
*x
)
133 return x
->cert_info
.validity
.notAfter
;
136 int X509_get_signature_type(const X509
*x
)
138 return EVP_PKEY_type(OBJ_obj2nid(x
->sig_alg
.algorithm
));
141 X509_PUBKEY
*X509_get_X509_PUBKEY(const X509
*x
)
143 return x
->cert_info
.key
;
146 const STACK_OF(X509_EXTENSION
) *X509_get0_extensions(const X509
*x
)
148 return x
->cert_info
.extensions
;
151 void X509_get0_uids(const X509
*x
, const ASN1_BIT_STRING
**piuid
,
152 const ASN1_BIT_STRING
**psuid
)
155 *piuid
= x
->cert_info
.issuerUID
;
157 *psuid
= x
->cert_info
.subjectUID
;
160 const X509_ALGOR
*X509_get0_tbs_sigalg(const X509
*x
)
162 return &x
->cert_info
.signature
;
165 int X509_SIG_INFO_get(const X509_SIG_INFO
*siginf
, int *mdnid
, int *pknid
,
166 int *secbits
, uint32_t *flags
)
169 *mdnid
= siginf
->mdnid
;
171 *pknid
= siginf
->pknid
;
173 *secbits
= siginf
->secbits
;
175 *flags
= siginf
->flags
;
176 return (siginf
->flags
& X509_SIG_INFO_VALID
) != 0;
179 void X509_SIG_INFO_set(X509_SIG_INFO
*siginf
, int mdnid
, int pknid
,
180 int secbits
, uint32_t flags
)
182 siginf
->mdnid
= mdnid
;
183 siginf
->pknid
= pknid
;
184 siginf
->secbits
= secbits
;
185 siginf
->flags
= flags
;
188 int X509_get_signature_info(X509
*x
, int *mdnid
, int *pknid
, int *secbits
,
191 X509_check_purpose(x
, -1, -1);
192 return X509_SIG_INFO_get(&x
->siginf
, mdnid
, pknid
, secbits
, flags
);
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
)
201 const EVP_PKEY_ASN1_METHOD
*ameth
;
203 siginf
->mdnid
= NID_undef
;
204 siginf
->pknid
= NID_undef
;
205 siginf
->secbits
= -1;
207 if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg
->algorithm
), &mdnid
, &pknid
)
208 || pknid
== NID_undef
) {
209 ERR_raise(ERR_LIB_X509
, X509_R_UNKNOWN_SIGID_ALGS
);
212 siginf
->mdnid
= mdnid
;
213 siginf
->pknid
= pknid
;
217 /* If we have one, use a custom handler for this algorithm */
218 ameth
= EVP_PKEY_asn1_find(NULL
, pknid
);
219 if (ameth
== NULL
|| ameth
->siginf_set
== NULL
220 || !ameth
->siginf_set(siginf
, alg
, sig
)) {
221 ERR_raise(ERR_LIB_X509
, X509_R_ERROR_USING_SIGINF_SET
);
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.
233 * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack
236 siginf
->secbits
= 63;
240 * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
241 * puts a chosen-prefix attack for MD5 at 2^39.
243 siginf
->secbits
= 39;
245 case NID_id_GostR3411_94
:
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
250 siginf
->secbits
= 105;
253 /* Security bits: half number of bits in digest */
254 if ((md
= EVP_get_digestbynid(mdnid
)) == NULL
) {
255 ERR_raise(ERR_LIB_X509
, X509_R_ERROR_GETTING_MD_BY_NID
);
258 siginf
->secbits
= EVP_MD_size(md
) * 4;
266 siginf
->flags
|= X509_SIG_INFO_TLS
;
268 siginf
->flags
|= X509_SIG_INFO_VALID
;
272 /* Returns 1 on success, 0 on failure */
273 int ossl_x509_init_sig_info(X509
*x
)
275 return x509_sig_info_init(&x
->siginf
, &x
->sig_alg
, &x
->signature
);