Add support for reference counting using C11 atomics
[openssl.git] / crypto / include / internal / x509_int.h
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include "internal/refcount.h"
11
12 /* Internal X509 structures and functions: not for application use */
13
14 /* Note: unless otherwise stated a field pointer is mandatory and should
15  * never be set to NULL: the ASN.1 code and accessors rely on mandatory
16  * fields never being NULL.
17  */
18
19 /*
20  * name entry structure, equivalent to AttributeTypeAndValue defined
21  * in RFC5280 et al.
22  */
23 struct X509_name_entry_st {
24     ASN1_OBJECT *object;        /* AttributeType */
25     ASN1_STRING *value;         /* AttributeValue */
26     int set;                    /* index of RDNSequence for this entry */
27     int size;                   /* temp variable */
28 };
29
30 /* Name from RFC 5280. */
31 struct X509_name_st {
32     STACK_OF(X509_NAME_ENTRY) *entries; /* DN components */
33     int modified;               /* true if 'bytes' needs to be built */
34     BUF_MEM *bytes;             /* cached encoding: cannot be NULL */
35     /* canonical encoding used for rapid Name comparison */
36     unsigned char *canon_enc;
37     int canon_enclen;
38 } /* X509_NAME */ ;
39
40 /* PKCS#10 certificate request */
41
42 struct X509_req_info_st {
43     ASN1_ENCODING enc;          /* cached encoding of signed part */
44     ASN1_INTEGER *version;      /* version, defaults to v1(0) so can be NULL */
45     X509_NAME *subject;         /* certificate request DN */
46     X509_PUBKEY *pubkey;        /* public key of request */
47     /*
48      * Zero or more attributes.
49      * NB: although attributes is a mandatory field some broken
50      * encodings omit it so this may be NULL in that case.
51      */
52     STACK_OF(X509_ATTRIBUTE) *attributes;
53 };
54
55 struct X509_req_st {
56     X509_REQ_INFO req_info;     /* signed certificate request data */
57     X509_ALGOR sig_alg;         /* signature algorithm */
58     ASN1_BIT_STRING *signature; /* signature */
59     CRYPTO_REF_COUNT references;
60     CRYPTO_RWLOCK *lock;
61 };
62
63 struct X509_crl_info_st {
64     ASN1_INTEGER *version;      /* version: defaults to v1(0) so may be NULL */
65     X509_ALGOR sig_alg;         /* signature algorithm */
66     X509_NAME *issuer;          /* CRL issuer name */
67     ASN1_TIME *lastUpdate;      /* lastUpdate field */
68     ASN1_TIME *nextUpdate;      /* nextUpdate field: optional */
69     STACK_OF(X509_REVOKED) *revoked;        /* revoked entries: optional */
70     STACK_OF(X509_EXTENSION) *extensions;   /* extensions: optional */
71     ASN1_ENCODING enc;                      /* encoding of signed portion of CRL */
72 };
73
74 struct X509_crl_st {
75     X509_CRL_INFO crl;          /* signed CRL data */
76     X509_ALGOR sig_alg;         /* CRL signature algorithm */
77     ASN1_BIT_STRING signature;  /* CRL signature */
78     CRYPTO_REF_COUNT references;
79     int flags;
80     /*
81      * Cached copies of decoded extension values, since extensions
82      * are optional any of these can be NULL.
83      */
84     AUTHORITY_KEYID *akid;
85     ISSUING_DIST_POINT *idp;
86     /* Convenient breakdown of IDP */
87     int idp_flags;
88     int idp_reasons;
89     /* CRL and base CRL numbers for delta processing */
90     ASN1_INTEGER *crl_number;
91     ASN1_INTEGER *base_crl_number;
92     STACK_OF(GENERAL_NAMES) *issuers;
93     /* hash of CRL */
94     unsigned char sha1_hash[SHA_DIGEST_LENGTH];
95     /* alternative method to handle this CRL */
96     const X509_CRL_METHOD *meth;
97     void *meth_data;
98     CRYPTO_RWLOCK *lock;
99 };
100
101 struct x509_revoked_st {
102     ASN1_INTEGER serialNumber; /* revoked entry serial number */
103     ASN1_TIME *revocationDate;  /* revocation date */
104     STACK_OF(X509_EXTENSION) *extensions;   /* CRL entry extensions: optional */
105     /* decoded value of CRLissuer extension: set if indirect CRL */
106     STACK_OF(GENERAL_NAME) *issuer;
107     /* revocation reason: set to CRL_REASON_NONE if reason extension absent */
108     int reason;
109     /*
110      * CRL entries are reordered for faster lookup of serial numbers. This
111      * field contains the original load sequence for this entry.
112      */
113     int sequence;
114 };
115
116 /*
117  * This stuff is certificate "auxiliary info": it contains details which are
118  * useful in certificate stores and databases. When used this is tagged onto
119  * the end of the certificate itself. OpenSSL specific structure not defined
120  * in any RFC.
121  */
122
123 struct x509_cert_aux_st {
124     STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */
125     STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */
126     ASN1_UTF8STRING *alias;     /* "friendly name" */
127     ASN1_OCTET_STRING *keyid;   /* key id of private key */
128     STACK_OF(X509_ALGOR) *other; /* other unspecified info */
129 };
130
131 struct x509_cinf_st {
132     ASN1_INTEGER *version;      /* [ 0 ] default of v1 */
133     ASN1_INTEGER serialNumber;
134     X509_ALGOR signature;
135     X509_NAME *issuer;
136     X509_VAL validity;
137     X509_NAME *subject;
138     X509_PUBKEY *key;
139     ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
140     ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
141     STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */
142     ASN1_ENCODING enc;
143 };
144
145 struct x509_st {
146     X509_CINF cert_info;
147     X509_ALGOR sig_alg;
148     ASN1_BIT_STRING signature;
149     CRYPTO_REF_COUNT references;
150     CRYPTO_EX_DATA ex_data;
151     /* These contain copies of various extension values */
152     long ex_pathlen;
153     long ex_pcpathlen;
154     uint32_t ex_flags;
155     uint32_t ex_kusage;
156     uint32_t ex_xkusage;
157     uint32_t ex_nscert;
158     ASN1_OCTET_STRING *skid;
159     AUTHORITY_KEYID *akid;
160     X509_POLICY_CACHE *policy_cache;
161     STACK_OF(DIST_POINT) *crldp;
162     STACK_OF(GENERAL_NAME) *altname;
163     NAME_CONSTRAINTS *nc;
164 #ifndef OPENSSL_NO_RFC3779
165     STACK_OF(IPAddressFamily) *rfc3779_addr;
166     struct ASIdentifiers_st *rfc3779_asid;
167 # endif
168     unsigned char sha1_hash[SHA_DIGEST_LENGTH];
169     X509_CERT_AUX *aux;
170     CRYPTO_RWLOCK *lock;
171 } /* X509 */ ;
172
173 /*
174  * This is a used when verifying cert chains.  Since the gathering of the
175  * cert chain can take some time (and have to be 'retried', this needs to be
176  * kept and passed around.
177  */
178 struct x509_store_ctx_st {      /* X509_STORE_CTX */
179     X509_STORE *ctx;
180     /* The following are set by the caller */
181     /* The cert to check */
182     X509 *cert;
183     /* chain of X509s - untrusted - passed in */
184     STACK_OF(X509) *untrusted;
185     /* set of CRLs passed in */
186     STACK_OF(X509_CRL) *crls;
187     X509_VERIFY_PARAM *param;
188     /* Other info for use with get_issuer() */
189     void *other_ctx;
190     /* Callbacks for various operations */
191     /* called to verify a certificate */
192     int (*verify) (X509_STORE_CTX *ctx);
193     /* error callback */
194     int (*verify_cb) (int ok, X509_STORE_CTX *ctx);
195     /* get issuers cert from ctx */
196     int (*get_issuer) (X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
197     /* check issued */
198     int (*check_issued) (X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
199     /* Check revocation status of chain */
200     int (*check_revocation) (X509_STORE_CTX *ctx);
201     /* retrieve CRL */
202     int (*get_crl) (X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x);
203     /* Check CRL validity */
204     int (*check_crl) (X509_STORE_CTX *ctx, X509_CRL *crl);
205     /* Check certificate against CRL */
206     int (*cert_crl) (X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
207     /* Check policy status of the chain */
208     int (*check_policy) (X509_STORE_CTX *ctx);
209     STACK_OF(X509) *(*lookup_certs) (X509_STORE_CTX *ctx, X509_NAME *nm);
210     STACK_OF(X509_CRL) *(*lookup_crls) (X509_STORE_CTX *ctx, X509_NAME *nm);
211     int (*cleanup) (X509_STORE_CTX *ctx);
212     /* The following is built up */
213     /* if 0, rebuild chain */
214     int valid;
215     /* number of untrusted certs */
216     int num_untrusted;
217     /* chain of X509s - built up and trusted */
218     STACK_OF(X509) *chain;
219     /* Valid policy tree */
220     X509_POLICY_TREE *tree;
221     /* Require explicit policy value */
222     int explicit_policy;
223     /* When something goes wrong, this is why */
224     int error_depth;
225     int error;
226     X509 *current_cert;
227     /* cert currently being tested as valid issuer */
228     X509 *current_issuer;
229     /* current CRL */
230     X509_CRL *current_crl;
231     /* score of current CRL */
232     int current_crl_score;
233     /* Reason mask */
234     unsigned int current_reasons;
235     /* For CRL path validation: parent context */
236     X509_STORE_CTX *parent;
237     CRYPTO_EX_DATA ex_data;
238     SSL_DANE *dane;
239     /* signed via bare TA public key, rather than CA certificate */
240     int bare_ta_signed;
241 };
242
243 /* PKCS#8 private key info structure */
244
245 struct pkcs8_priv_key_info_st {
246     ASN1_INTEGER *version;
247     X509_ALGOR *pkeyalg;
248     ASN1_OCTET_STRING *pkey;
249     STACK_OF(X509_ATTRIBUTE) *attributes;
250 };
251
252 struct X509_sig_st {
253     X509_ALGOR *algor;
254     ASN1_OCTET_STRING *digest;
255 };
256
257 struct x509_object_st {
258     /* one of the above types */
259     X509_LOOKUP_TYPE type;
260     union {
261         char *ptr;
262         X509 *x509;
263         X509_CRL *crl;
264         EVP_PKEY *pkey;
265     } data;
266 };
267
268 int a2i_ipadd(unsigned char *ipout, const char *ipasc);
269 int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm);