2 * Copyright 2015-2020 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
10 #include "internal/refcount.h"
11 #include <openssl/asn1.h>
12 #include <openssl/x509.h>
14 /* Internal X509 structures and functions: not for application use */
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.
22 * name entry structure, equivalent to AttributeTypeAndValue defined
25 struct X509_name_entry_st {
26 ASN1_OBJECT *object; /* AttributeType */
27 ASN1_STRING *value; /* AttributeValue */
28 int set; /* index of RDNSequence for this entry */
29 int size; /* temp variable */
32 /* Name from RFC 5280. */
34 STACK_OF(X509_NAME_ENTRY) *entries; /* DN components */
35 int modified; /* true if 'bytes' needs to be built */
36 BUF_MEM *bytes; /* cached encoding: cannot be NULL */
37 /* canonical encoding used for rapid Name comparison */
38 unsigned char *canon_enc;
42 /* Signature info structure */
44 struct x509_sig_info_st {
45 /* NID of message digest */
47 /* NID of public key algorithm */
55 /* PKCS#10 certificate request */
57 struct X509_req_info_st {
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 */
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.
67 STACK_OF(X509_ATTRIBUTE) *attributes;
71 X509_REQ_INFO req_info; /* signed certificate request data */
72 X509_ALGOR sig_alg; /* signature algorithm */
73 ASN1_BIT_STRING *signature; /* signature */
74 CRYPTO_REF_COUNT references;
77 /* Set on live certificates for authentication purposes */
78 ASN1_OCTET_STRING *distinguishing_id;
81 struct X509_crl_info_st {
82 ASN1_INTEGER *version; /* version: defaults to v1(0) so may be NULL */
83 X509_ALGOR sig_alg; /* signature algorithm */
84 X509_NAME *issuer; /* CRL issuer name */
85 ASN1_TIME *lastUpdate; /* lastUpdate field */
86 ASN1_TIME *nextUpdate; /* nextUpdate field: optional */
87 STACK_OF(X509_REVOKED) *revoked; /* revoked entries: optional */
88 STACK_OF(X509_EXTENSION) *extensions; /* extensions: optional */
89 ASN1_ENCODING enc; /* encoding of signed portion of CRL */
93 X509_CRL_INFO crl; /* signed CRL data */
94 X509_ALGOR sig_alg; /* CRL signature algorithm */
95 ASN1_BIT_STRING signature; /* CRL signature */
96 CRYPTO_REF_COUNT references;
99 * Cached copies of decoded extension values, since extensions
100 * are optional any of these can be NULL.
102 AUTHORITY_KEYID *akid;
103 ISSUING_DIST_POINT *idp;
104 /* Convenient breakdown of IDP */
107 /* CRL and base CRL numbers for delta processing */
108 ASN1_INTEGER *crl_number;
109 ASN1_INTEGER *base_crl_number;
110 STACK_OF(GENERAL_NAMES) *issuers;
112 unsigned char sha1_hash[SHA_DIGEST_LENGTH];
113 /* alternative method to handle this CRL */
114 const X509_CRL_METHOD *meth;
119 struct x509_revoked_st {
120 ASN1_INTEGER serialNumber; /* revoked entry serial number */
121 ASN1_TIME *revocationDate; /* revocation date */
122 STACK_OF(X509_EXTENSION) *extensions; /* CRL entry extensions: optional */
123 /* decoded value of CRLissuer extension: set if indirect CRL */
124 STACK_OF(GENERAL_NAME) *issuer;
125 /* revocation reason: set to CRL_REASON_NONE if reason extension absent */
128 * CRL entries are reordered for faster lookup of serial numbers. This
129 * field contains the original load sequence for this entry.
135 * This stuff is certificate "auxiliary info": it contains details which are
136 * useful in certificate stores and databases. When used this is tagged onto
137 * the end of the certificate itself. OpenSSL specific structure not defined
141 struct x509_cert_aux_st {
142 STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */
143 STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */
144 ASN1_UTF8STRING *alias; /* "friendly name" */
145 ASN1_OCTET_STRING *keyid; /* key id of private key */
146 STACK_OF(X509_ALGOR) *other; /* other unspecified info */
149 struct x509_cinf_st {
150 ASN1_INTEGER *version; /* [ 0 ] default of v1 */
151 ASN1_INTEGER serialNumber;
152 X509_ALGOR signature;
157 ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
158 ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
159 STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */
166 ASN1_BIT_STRING signature;
167 X509_SIG_INFO siginf;
168 CRYPTO_REF_COUNT references;
169 CRYPTO_EX_DATA ex_data;
170 /* These contain copies of various extension values */
177 ASN1_OCTET_STRING *skid;
178 AUTHORITY_KEYID *akid;
179 X509_POLICY_CACHE *policy_cache;
180 STACK_OF(DIST_POINT) *crldp;
181 STACK_OF(GENERAL_NAME) *altname;
182 NAME_CONSTRAINTS *nc;
183 # ifndef OPENSSL_NO_RFC3779
184 STACK_OF(IPAddressFamily) *rfc3779_addr;
185 struct ASIdentifiers_st *rfc3779_asid;
187 unsigned char sha1_hash[SHA_DIGEST_LENGTH];
190 volatile int ex_cached;
192 /* Set on live certificates for authentication purposes */
193 ASN1_OCTET_STRING *distinguishing_id;
200 * This is a used when verifying cert chains. Since the gathering of the
201 * cert chain can take some time (and have to be 'retried', this needs to be
202 * kept and passed around.
204 struct x509_store_ctx_st { /* X509_STORE_CTX */
206 /* The following are set by the caller */
207 /* The cert to check */
209 /* chain of X509s - untrusted - passed in */
210 STACK_OF(X509) *untrusted;
211 /* set of CRLs passed in */
212 STACK_OF(X509_CRL) *crls;
213 X509_VERIFY_PARAM *param;
214 /* Other info for use with get_issuer() */
216 /* Callbacks for various operations */
217 /* called to verify a certificate */
218 int (*verify) (X509_STORE_CTX *ctx);
220 int (*verify_cb) (int ok, X509_STORE_CTX *ctx);
221 /* get issuers cert from ctx */
222 int (*get_issuer) (X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
224 int (*check_issued) (X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
225 /* Check revocation status of chain */
226 int (*check_revocation) (X509_STORE_CTX *ctx);
228 int (*get_crl) (X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x);
229 /* Check CRL validity */
230 int (*check_crl) (X509_STORE_CTX *ctx, X509_CRL *crl);
231 /* Check certificate against CRL */
232 int (*cert_crl) (X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
233 /* Check policy status of the chain */
234 int (*check_policy) (X509_STORE_CTX *ctx);
235 STACK_OF(X509) *(*lookup_certs) (X509_STORE_CTX *ctx,
236 const X509_NAME *nm);
237 /* cannot constify 'ctx' param due to lookup_certs_sk() in x509_vfy.c */
238 STACK_OF(X509_CRL) *(*lookup_crls) (const X509_STORE_CTX *ctx,
239 const X509_NAME *nm);
240 int (*cleanup) (X509_STORE_CTX *ctx);
241 /* The following is built up */
242 /* if 0, rebuild chain */
244 /* number of untrusted certs */
246 /* chain of X509s - built up and trusted */
247 STACK_OF(X509) *chain;
248 /* Valid policy tree */
249 X509_POLICY_TREE *tree;
250 /* Require explicit policy value */
252 /* When something goes wrong, this is why */
256 /* cert currently being tested as valid issuer */
257 X509 *current_issuer;
259 X509_CRL *current_crl;
260 /* score of current CRL */
261 int current_crl_score;
263 unsigned int current_reasons;
264 /* For CRL path validation: parent context */
265 X509_STORE_CTX *parent;
266 CRYPTO_EX_DATA ex_data;
268 /* signed via bare TA public key, rather than CA certificate */
275 /* PKCS#8 private key info structure */
277 struct pkcs8_priv_key_info_st {
278 ASN1_INTEGER *version;
280 ASN1_OCTET_STRING *pkey;
281 STACK_OF(X509_ATTRIBUTE) *attributes;
286 ASN1_OCTET_STRING *digest;
289 struct x509_object_st {
290 /* one of the above types */
291 X509_LOOKUP_TYPE type;
300 int a2i_ipadd(unsigned char *ipout, const char *ipasc);
301 int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm);
302 int x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags);
303 int x509v3_cache_extensions(X509 *x);
304 int x509_set0_libctx(X509 *x, OPENSSL_CTX *libctx, const char *propq);
305 void x509_init_sig_info(X509 *x);
306 int asn1_item_digest_with_libctx(const ASN1_ITEM *it, const EVP_MD *type,
307 void *data, unsigned char *md,
308 unsigned int *len, OPENSSL_CTX *libctx,
310 int X509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags);
312 int X509_PUBKEY_get0_libctx(OPENSSL_CTX **plibctx, const char **ppropq,
313 const X509_PUBKEY *key);