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