vendor/libressl: upgrade from 3.1.4 to 3.2.2
[dragonfly.git] / crypto / libressl / crypto / x509 / x509_internal.h
1 /* $OpenBSD: x509_internal.h,v 1.3 2020/09/15 11:55:14 beck Exp $ */
2 /*
3  * Copyright (c) 2020 Bob Beck <beck@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef HEADER_X509_INTERNAL_H
18 #define HEADER_X509_INTERNAL_H
19
20 /* Internal use only, not public API */
21 #include <netinet/in.h>
22
23 #include <openssl/x509_verify.h>
24
25 /* Hard limits on structure size and number of signature checks. */
26 #define X509_VERIFY_MAX_CHAINS          8       /* Max validated chains */
27 #define X509_VERIFY_MAX_CHAIN_CERTS     32      /* Max depth of a chain */
28 #define X509_VERIFY_MAX_SIGCHECKS       256     /* Max signature checks */
29
30 /*
31  * Limit the number of names and constraints we will check in a chain
32  * to avoid a hostile input DOS
33  */
34 #define X509_VERIFY_MAX_CHAIN_NAMES             512
35 #define X509_VERIFY_MAX_CHAIN_CONSTRAINTS       512
36
37 /*
38  * Hold the parsed and validated result of names from a certificate.
39  * these typically come from a GENERALNAME, but we store the parsed
40  * and validated results, not the ASN1 bytes.
41  */
42 struct x509_constraints_name {
43         int type;                       /* GEN_* types from GENERAL_NAME */
44         char *name;                     /* Name to check */
45         char *local;                    /* holds the local part of GEN_EMAIL */
46         uint8_t *der;                   /* DER encoded value or NULL*/
47         size_t der_len;
48         int af;                         /* INET and INET6 are supported */
49         uint8_t address[32];            /* Must hold ipv6 + mask */
50 };
51
52 struct x509_constraints_names {
53         struct x509_constraints_name **names;
54         size_t names_len;
55         size_t names_count;
56 };
57
58 struct x509_verify_chain {
59         STACK_OF(X509) *certs;          /* Kept in chain order, includes leaf */
60         struct x509_constraints_names *names;   /* All names from all certs */
61 };
62
63 struct x509_verify_ctx {
64         X509_STORE_CTX *xsc;
65         struct x509_verify_chain **chains;      /* Validated chains */
66         size_t chains_count;
67         STACK_OF(X509) *roots;          /* Trusted roots for this validation */
68         STACK_OF(X509) *intermediates;  /* Intermediates provided by peer */
69         time_t *check_time;             /* Time for validity checks */
70         int purpose;                    /* Cert purpose we are validating */
71         size_t max_chains;              /* Max chains to return */
72         size_t max_depth;               /* Max chain depth for validation */
73         size_t max_sigs;                /* Max number of signature checks */
74         size_t sig_checks;              /* Number of signature checks done */
75         size_t error_depth;             /* Depth of last error seen */
76         int error;                      /* Last error seen */
77 };
78
79 int ASN1_time_tm_clamp_notafter(struct tm *tm);
80
81 __BEGIN_HIDDEN_DECLS
82
83 int x509_vfy_check_id(X509_STORE_CTX *ctx);
84 int x509_vfy_check_revocation(X509_STORE_CTX *ctx);
85 int x509_vfy_check_policy(X509_STORE_CTX *ctx);
86 int x509_vfy_check_trust(X509_STORE_CTX *ctx);
87 int x509_vfy_check_chain_extensions(X509_STORE_CTX *ctx);
88 void x509v3_cache_extensions(X509 *x);
89
90 int x509_verify_asn1_time_to_tm(const ASN1_TIME *atime, struct tm *tm,
91     int notafter);
92
93 struct x509_verify_ctx *x509_verify_ctx_new_from_xsc(X509_STORE_CTX *xsc,
94     STACK_OF(X509) *roots);
95
96 void x509_constraints_name_clear(struct x509_constraints_name *name);
97 int x509_constraints_names_add(struct x509_constraints_names *names,
98     struct x509_constraints_name *name);
99 struct x509_constraints_names *x509_constraints_names_dup(
100     struct x509_constraints_names *names);
101 void x509_constraints_names_clear(struct x509_constraints_names *names);
102 struct x509_constraints_names *x509_constraints_names_new(void);
103 void x509_constraints_names_free(struct x509_constraints_names *names);
104 int x509_constraints_valid_host(uint8_t *name, size_t len);
105 int x509_constraints_valid_sandns(uint8_t *name, size_t len);
106 int x509_constraints_domain(char *domain, size_t dlen, char *constraint,
107     size_t len);
108 int x509_constraints_parse_mailbox(uint8_t *candidate, size_t len,
109     struct x509_constraints_name *name);
110 int x509_constraints_valid_domain_constraint(uint8_t *constraint,
111     size_t len);
112 int x509_constraints_uri_host(uint8_t *uri, size_t len, char **hostp);
113 int x509_constraints_uri(uint8_t *uri, size_t ulen, uint8_t *constraint,
114     size_t len, int *error);
115 int x509_constraints_extract_names(struct x509_constraints_names *names,
116     X509 *cert, int include_cn, int *error);
117 int x509_constraints_extract_constraints(X509 *cert,
118     struct x509_constraints_names *permitted,
119     struct x509_constraints_names *excluded, int *error);
120 int x509_constraints_check(struct x509_constraints_names *names,
121     struct x509_constraints_names *permitted,
122     struct x509_constraints_names *excluded, int *error);
123 int x509_constraints_chain(STACK_OF(X509) *chain, int *error,
124     int *depth);
125
126 __END_HIDDEN_DECLS
127
128 #endif