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