]>
Commit | Line | Data |
---|---|---|
2743e38c | 1 | /* |
4333b89f | 2 | * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. |
2743e38c | 3 | * |
48f4ad77 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
2743e38c DSH |
8 | */ |
9 | ||
2f545ae4 | 10 | #include "internal/refcount.h" |
bc03cfad DDO |
11 | #include <openssl/asn1.h> |
12 | #include <openssl/x509.h> | |
2f545ae4 | 13 | |
2743e38c DSH |
14 | /* Internal X509 structures and functions: not for application use */ |
15 | ||
40ffdc9c DSH |
16 | /* Note: unless otherwise stated a field pointer is mandatory and should |
17 | * never be set to NULL: the ASN.1 code and accessors rely on mandatory | |
18 | * fields never being NULL. | |
19 | */ | |
20 | ||
21 | /* | |
22 | * name entry structure, equivalent to AttributeTypeAndValue defined | |
23 | * in RFC5280 et al. | |
24 | */ | |
2743e38c | 25 | struct X509_name_entry_st { |
40ffdc9c DSH |
26 | ASN1_OBJECT *object; /* AttributeType */ |
27 | ASN1_STRING *value; /* AttributeValue */ | |
28 | int set; /* index of RDNSequence for this entry */ | |
2743e38c DSH |
29 | int size; /* temp variable */ |
30 | }; | |
31 | ||
40ffdc9c | 32 | /* Name from RFC 5280. */ |
2743e38c | 33 | struct X509_name_st { |
40ffdc9c | 34 | STACK_OF(X509_NAME_ENTRY) *entries; /* DN components */ |
2743e38c | 35 | int modified; /* true if 'bytes' needs to be built */ |
40ffdc9c DSH |
36 | BUF_MEM *bytes; /* cached encoding: cannot be NULL */ |
37 | /* canonical encoding used for rapid Name comparison */ | |
2743e38c DSH |
38 | unsigned char *canon_enc; |
39 | int canon_enclen; | |
40 | } /* X509_NAME */ ; | |
bc3686df | 41 | |
786dd2c2 DSH |
42 | /* Signature info structure */ |
43 | ||
44 | struct x509_sig_info_st { | |
45 | /* NID of message digest */ | |
46 | int mdnid; | |
47 | /* NID of public key algorithm */ | |
48 | int pknid; | |
49 | /* Security bits */ | |
50 | int secbits; | |
51 | /* Various flags */ | |
52 | uint32_t flags; | |
53 | }; | |
54 | ||
40ffdc9c | 55 | /* PKCS#10 certificate request */ |
124055a9 DSH |
56 | |
57 | struct X509_req_info_st { | |
40ffdc9c DSH |
58 | ASN1_ENCODING enc; /* cached encoding of signed part */ |
59 | ASN1_INTEGER *version; /* version, defaults to v1(0) so can be NULL */ | |
60 | X509_NAME *subject; /* certificate request DN */ | |
61 | X509_PUBKEY *pubkey; /* public key of request */ | |
62 | /* | |
63 | * Zero or more attributes. | |
64 | * NB: although attributes is a mandatory field some broken | |
65 | * encodings omit it so this may be NULL in that case. | |
66 | */ | |
67 | STACK_OF(X509_ATTRIBUTE) *attributes; | |
124055a9 DSH |
68 | }; |
69 | ||
70 | struct X509_req_st { | |
40ffdc9c DSH |
71 | X509_REQ_INFO req_info; /* signed certificate request data */ |
72 | X509_ALGOR sig_alg; /* signature algorithm */ | |
73 | ASN1_BIT_STRING *signature; /* signature */ | |
2f545ae4 | 74 | CRYPTO_REF_COUNT references; |
c001ce33 | 75 | CRYPTO_RWLOCK *lock; |
70a7dd6f RL |
76 | |
77 | /* Set on live certificates for authentication purposes */ | |
78 | ASN1_OCTET_STRING *distinguishing_id; | |
124055a9 | 79 | }; |
e3e57192 DSH |
80 | |
81 | struct X509_crl_info_st { | |
40ffdc9c | 82 | ASN1_INTEGER *version; /* version: defaults to v1(0) so may be NULL */ |
8483a003 | 83 | X509_ALGOR sig_alg; /* signature algorithm */ |
40ffdc9c DSH |
84 | X509_NAME *issuer; /* CRL issuer name */ |
85 | ASN1_TIME *lastUpdate; /* lastUpdate field */ | |
86 | ASN1_TIME *nextUpdate; /* nextUpdate field: optional */ | |
687b4868 | 87 | STACK_OF(X509_REVOKED) *revoked; /* revoked entries: optional */ |
40ffdc9c | 88 | STACK_OF(X509_EXTENSION) *extensions; /* extensions: optional */ |
687b4868 | 89 | ASN1_ENCODING enc; /* encoding of signed portion of CRL */ |
e3e57192 DSH |
90 | }; |
91 | ||
92 | struct X509_crl_st { | |
40ffdc9c DSH |
93 | X509_CRL_INFO crl; /* signed CRL data */ |
94 | X509_ALGOR sig_alg; /* CRL signature algorithm */ | |
687b4868 | 95 | ASN1_BIT_STRING signature; /* CRL signature */ |
2f545ae4 | 96 | CRYPTO_REF_COUNT references; |
e3e57192 | 97 | int flags; |
40ffdc9c DSH |
98 | /* |
99 | * Cached copies of decoded extension values, since extensions | |
100 | * are optional any of these can be NULL. | |
101 | */ | |
e3e57192 DSH |
102 | AUTHORITY_KEYID *akid; |
103 | ISSUING_DIST_POINT *idp; | |
104 | /* Convenient breakdown of IDP */ | |
105 | int idp_flags; | |
106 | int idp_reasons; | |
107 | /* CRL and base CRL numbers for delta processing */ | |
108 | ASN1_INTEGER *crl_number; | |
109 | ASN1_INTEGER *base_crl_number; | |
e3e57192 | 110 | STACK_OF(GENERAL_NAMES) *issuers; |
40ffdc9c DSH |
111 | /* hash of CRL */ |
112 | unsigned char sha1_hash[SHA_DIGEST_LENGTH]; | |
113 | /* alternative method to handle this CRL */ | |
e3e57192 DSH |
114 | const X509_CRL_METHOD *meth; |
115 | void *meth_data; | |
c001ce33 | 116 | CRYPTO_RWLOCK *lock; |
34b80d06 | 117 | |
b4250010 | 118 | OSSL_LIB_CTX *libctx; |
c22139a7 | 119 | char *propq; |
e3e57192 | 120 | }; |
44748efc DSH |
121 | |
122 | struct x509_revoked_st { | |
34a42e14 | 123 | ASN1_INTEGER serialNumber; /* revoked entry serial number */ |
40ffdc9c DSH |
124 | ASN1_TIME *revocationDate; /* revocation date */ |
125 | STACK_OF(X509_EXTENSION) *extensions; /* CRL entry extensions: optional */ | |
126 | /* decoded value of CRLissuer extension: set if indirect CRL */ | |
44748efc | 127 | STACK_OF(GENERAL_NAME) *issuer; |
40ffdc9c | 128 | /* revocation reason: set to CRL_REASON_NONE if reason extension absent */ |
44748efc | 129 | int reason; |
40ffdc9c DSH |
130 | /* |
131 | * CRL entries are reordered for faster lookup of serial numbers. This | |
132 | * field contains the original load sequence for this entry. | |
133 | */ | |
134 | int sequence; | |
135 | }; | |
136 | ||
137 | /* | |
138 | * This stuff is certificate "auxiliary info": it contains details which are | |
139 | * useful in certificate stores and databases. When used this is tagged onto | |
140 | * the end of the certificate itself. OpenSSL specific structure not defined | |
141 | * in any RFC. | |
142 | */ | |
143 | ||
144 | struct x509_cert_aux_st { | |
145 | STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */ | |
146 | STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */ | |
147 | ASN1_UTF8STRING *alias; /* "friendly name" */ | |
148 | ASN1_OCTET_STRING *keyid; /* key id of private key */ | |
149 | STACK_OF(X509_ALGOR) *other; /* other unspecified info */ | |
44748efc | 150 | }; |
2c81e476 DSH |
151 | |
152 | struct x509_cinf_st { | |
153 | ASN1_INTEGER *version; /* [ 0 ] default of v1 */ | |
81e49438 | 154 | ASN1_INTEGER serialNumber; |
2c81e476 DSH |
155 | X509_ALGOR signature; |
156 | X509_NAME *issuer; | |
157 | X509_VAL validity; | |
158 | X509_NAME *subject; | |
159 | X509_PUBKEY *key; | |
160 | ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */ | |
161 | ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */ | |
162 | STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */ | |
163 | ASN1_ENCODING enc; | |
164 | }; | |
165 | ||
166 | struct x509_st { | |
167 | X509_CINF cert_info; | |
168 | X509_ALGOR sig_alg; | |
81e49438 | 169 | ASN1_BIT_STRING signature; |
786dd2c2 | 170 | X509_SIG_INFO siginf; |
2f545ae4 | 171 | CRYPTO_REF_COUNT references; |
2c81e476 DSH |
172 | CRYPTO_EX_DATA ex_data; |
173 | /* These contain copies of various extension values */ | |
174 | long ex_pathlen; | |
175 | long ex_pcpathlen; | |
176 | uint32_t ex_flags; | |
177 | uint32_t ex_kusage; | |
178 | uint32_t ex_xkusage; | |
179 | uint32_t ex_nscert; | |
180 | ASN1_OCTET_STRING *skid; | |
181 | AUTHORITY_KEYID *akid; | |
182 | X509_POLICY_CACHE *policy_cache; | |
183 | STACK_OF(DIST_POINT) *crldp; | |
184 | STACK_OF(GENERAL_NAME) *altname; | |
185 | NAME_CONSTRAINTS *nc; | |
8267becb | 186 | # ifndef OPENSSL_NO_RFC3779 |
2c81e476 DSH |
187 | STACK_OF(IPAddressFamily) *rfc3779_addr; |
188 | struct ASIdentifiers_st *rfc3779_asid; | |
189 | # endif | |
190 | unsigned char sha1_hash[SHA_DIGEST_LENGTH]; | |
191 | X509_CERT_AUX *aux; | |
c001ce33 | 192 | CRYPTO_RWLOCK *lock; |
f21b5b64 | 193 | volatile int ex_cached; |
70a7dd6f RL |
194 | |
195 | /* Set on live certificates for authentication purposes */ | |
196 | ASN1_OCTET_STRING *distinguishing_id; | |
6725682d | 197 | |
b4250010 | 198 | OSSL_LIB_CTX *libctx; |
22b9230f | 199 | char *propq; |
2c81e476 | 200 | } /* X509 */ ; |
54dbf423 | 201 | |
f0e0fd51 RS |
202 | /* |
203 | * This is a used when verifying cert chains. Since the gathering of the | |
204 | * cert chain can take some time (and have to be 'retried', this needs to be | |
205 | * kept and passed around. | |
206 | */ | |
207 | struct x509_store_ctx_st { /* X509_STORE_CTX */ | |
faa9dcd4 | 208 | X509_STORE *store; |
f0e0fd51 RS |
209 | /* The following are set by the caller */ |
210 | /* The cert to check */ | |
211 | X509 *cert; | |
212 | /* chain of X509s - untrusted - passed in */ | |
213 | STACK_OF(X509) *untrusted; | |
214 | /* set of CRLs passed in */ | |
215 | STACK_OF(X509_CRL) *crls; | |
216 | X509_VERIFY_PARAM *param; | |
217 | /* Other info for use with get_issuer() */ | |
218 | void *other_ctx; | |
219 | /* Callbacks for various operations */ | |
220 | /* called to verify a certificate */ | |
221 | int (*verify) (X509_STORE_CTX *ctx); | |
222 | /* error callback */ | |
223 | int (*verify_cb) (int ok, X509_STORE_CTX *ctx); | |
224 | /* get issuers cert from ctx */ | |
225 | int (*get_issuer) (X509 **issuer, X509_STORE_CTX *ctx, X509 *x); | |
226 | /* check issued */ | |
227 | int (*check_issued) (X509_STORE_CTX *ctx, X509 *x, X509 *issuer); | |
228 | /* Check revocation status of chain */ | |
229 | int (*check_revocation) (X509_STORE_CTX *ctx); | |
230 | /* retrieve CRL */ | |
231 | int (*get_crl) (X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x); | |
232 | /* Check CRL validity */ | |
233 | int (*check_crl) (X509_STORE_CTX *ctx, X509_CRL *crl); | |
234 | /* Check certificate against CRL */ | |
235 | int (*cert_crl) (X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); | |
0a5fe2eb | 236 | /* Check policy status of the chain */ |
f0e0fd51 | 237 | int (*check_policy) (X509_STORE_CTX *ctx); |
8cc86b81 DDO |
238 | STACK_OF(X509) *(*lookup_certs) (X509_STORE_CTX *ctx, |
239 | const X509_NAME *nm); | |
240 | /* cannot constify 'ctx' param due to lookup_certs_sk() in x509_vfy.c */ | |
241 | STACK_OF(X509_CRL) *(*lookup_crls) (const X509_STORE_CTX *ctx, | |
242 | const X509_NAME *nm); | |
f0e0fd51 RS |
243 | int (*cleanup) (X509_STORE_CTX *ctx); |
244 | /* The following is built up */ | |
245 | /* if 0, rebuild chain */ | |
246 | int valid; | |
247 | /* number of untrusted certs */ | |
248 | int num_untrusted; | |
249 | /* chain of X509s - built up and trusted */ | |
250 | STACK_OF(X509) *chain; | |
251 | /* Valid policy tree */ | |
252 | X509_POLICY_TREE *tree; | |
253 | /* Require explicit policy value */ | |
254 | int explicit_policy; | |
255 | /* When something goes wrong, this is why */ | |
256 | int error_depth; | |
257 | int error; | |
258 | X509 *current_cert; | |
259 | /* cert currently being tested as valid issuer */ | |
260 | X509 *current_issuer; | |
261 | /* current CRL */ | |
262 | X509_CRL *current_crl; | |
263 | /* score of current CRL */ | |
264 | int current_crl_score; | |
265 | /* Reason mask */ | |
266 | unsigned int current_reasons; | |
267 | /* For CRL path validation: parent context */ | |
268 | X509_STORE_CTX *parent; | |
269 | CRYPTO_EX_DATA ex_data; | |
270 | SSL_DANE *dane; | |
271 | /* signed via bare TA public key, rather than CA certificate */ | |
272 | int bare_ta_signed; | |
1143c27b | 273 | |
b4250010 | 274 | OSSL_LIB_CTX *libctx; |
1143c27b | 275 | char *propq; |
f0e0fd51 RS |
276 | }; |
277 | ||
54dbf423 DSH |
278 | /* PKCS#8 private key info structure */ |
279 | ||
280 | struct pkcs8_priv_key_info_st { | |
281 | ASN1_INTEGER *version; | |
282 | X509_ALGOR *pkeyalg; | |
283 | ASN1_OCTET_STRING *pkey; | |
284 | STACK_OF(X509_ATTRIBUTE) *attributes; | |
285 | }; | |
a6eb1ce6 DSH |
286 | |
287 | struct X509_sig_st { | |
288 | X509_ALGOR *algor; | |
289 | ASN1_OCTET_STRING *digest; | |
290 | }; | |
f0e0fd51 RS |
291 | |
292 | struct x509_object_st { | |
293 | /* one of the above types */ | |
294 | X509_LOOKUP_TYPE type; | |
295 | union { | |
296 | char *ptr; | |
297 | X509 *x509; | |
298 | X509_CRL *crl; | |
299 | EVP_PKEY *pkey; | |
300 | } data; | |
301 | }; | |
6670d55a RS |
302 | |
303 | int a2i_ipadd(unsigned char *ipout, const char *ipasc); | |
3a60d6fa | 304 | int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm); |
31b28ad9 | 305 | int x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags); |
6725682d | 306 | int x509v3_cache_extensions(X509 *x); |
1e41dadf | 307 | int x509_init_sig_info(X509 *x); |
b4250010 | 308 | int x509_check_issued_int(X509 *issuer, X509 *subject, OSSL_LIB_CTX *libctx, |
1e41dadf DDO |
309 | const char *propq); |
310 | ||
b4250010 DMSP |
311 | int x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq); |
312 | int x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx, const char *propq); | |
1e41dadf | 313 | int x509_init_sig_info(X509 *x); |
d8652be0 MC |
314 | int asn1_item_digest_ex(const ASN1_ITEM *it, const EVP_MD *type, void *data, |
315 | unsigned char *md, unsigned int *len, | |
b4250010 | 316 | OSSL_LIB_CTX *libctx, const char *propq); |
eeccc237 | 317 | int X509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags); |
22b81444 | 318 | |
b4250010 | 319 | int X509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq, |
22b81444 | 320 | const X509_PUBKEY *key); |
41e597a0 DDO |
321 | /* Calculate default key identifier according to RFC 5280 section 4.2.1.2 (1) */ |
322 | ASN1_OCTET_STRING *x509_pubkey_hash(X509_PUBKEY *pubkey); |