1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: x509_internal.h,v 1.19 2022/06/27 14:10:22 tb Exp $ */
28edacedfSDaniel Fojt /*
38edacedfSDaniel Fojt  * Copyright (c) 2020 Bob Beck <beck@openbsd.org>
48edacedfSDaniel Fojt  *
58edacedfSDaniel Fojt  * Permission to use, copy, modify, and distribute this software for any
68edacedfSDaniel Fojt  * purpose with or without fee is hereby granted, provided that the above
78edacedfSDaniel Fojt  * copyright notice and this permission notice appear in all copies.
88edacedfSDaniel Fojt  *
98edacedfSDaniel Fojt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108edacedfSDaniel Fojt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118edacedfSDaniel Fojt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128edacedfSDaniel Fojt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138edacedfSDaniel Fojt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148edacedfSDaniel Fojt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158edacedfSDaniel Fojt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168edacedfSDaniel Fojt  */
178edacedfSDaniel Fojt #ifndef HEADER_X509_INTERNAL_H
188edacedfSDaniel Fojt #define HEADER_X509_INTERNAL_H
198edacedfSDaniel Fojt 
208edacedfSDaniel Fojt /* Internal use only, not public API */
218edacedfSDaniel Fojt #include <netinet/in.h>
228edacedfSDaniel Fojt 
238edacedfSDaniel Fojt #include <openssl/x509_verify.h>
248edacedfSDaniel Fojt 
25*de0e0e4dSAntonio Huete Jimenez #include "x509_lcl.h"
26*de0e0e4dSAntonio Huete Jimenez 
278edacedfSDaniel Fojt /* Hard limits on structure size and number of signature checks. */
288edacedfSDaniel Fojt #define X509_VERIFY_MAX_CHAINS		8	/* Max validated chains */
298edacedfSDaniel Fojt #define X509_VERIFY_MAX_CHAIN_CERTS	32	/* Max depth of a chain */
308edacedfSDaniel Fojt #define X509_VERIFY_MAX_SIGCHECKS	256	/* Max signature checks */
318edacedfSDaniel Fojt 
328edacedfSDaniel Fojt /*
338edacedfSDaniel Fojt  * Limit the number of names and constraints we will check in a chain
348edacedfSDaniel Fojt  * to avoid a hostile input DOS
358edacedfSDaniel Fojt  */
368edacedfSDaniel Fojt #define X509_VERIFY_MAX_CHAIN_NAMES		512
378edacedfSDaniel Fojt #define X509_VERIFY_MAX_CHAIN_CONSTRAINTS	512
388edacedfSDaniel Fojt 
398edacedfSDaniel Fojt /*
408edacedfSDaniel Fojt  * Hold the parsed and validated result of names from a certificate.
418edacedfSDaniel Fojt  * these typically come from a GENERALNAME, but we store the parsed
428edacedfSDaniel Fojt  * and validated results, not the ASN1 bytes.
438edacedfSDaniel Fojt  */
448edacedfSDaniel Fojt struct x509_constraints_name {
458edacedfSDaniel Fojt 	int type;			/* GEN_* types from GENERAL_NAME */
468edacedfSDaniel Fojt 	char *name;			/* Name to check */
478edacedfSDaniel Fojt 	char *local;			/* holds the local part of GEN_EMAIL */
488edacedfSDaniel Fojt 	uint8_t *der;			/* DER encoded value or NULL*/
498edacedfSDaniel Fojt 	size_t der_len;
508edacedfSDaniel Fojt 	int af;				/* INET and INET6 are supported */
518edacedfSDaniel Fojt 	uint8_t address[32];		/* Must hold ipv6 + mask */
528edacedfSDaniel Fojt };
538edacedfSDaniel Fojt 
548edacedfSDaniel Fojt struct x509_constraints_names {
558edacedfSDaniel Fojt 	struct x509_constraints_name **names;
568edacedfSDaniel Fojt 	size_t names_count;
57*de0e0e4dSAntonio Huete Jimenez 	size_t names_len;
58*de0e0e4dSAntonio Huete Jimenez 	size_t names_max;
598edacedfSDaniel Fojt };
608edacedfSDaniel Fojt 
618edacedfSDaniel Fojt struct x509_verify_chain {
628edacedfSDaniel Fojt 	STACK_OF(X509) *certs;		/* Kept in chain order, includes leaf */
63*de0e0e4dSAntonio Huete Jimenez 	int *cert_errors;		/* Verify error for each cert in chain. */
648edacedfSDaniel Fojt 	struct x509_constraints_names *names;	/* All names from all certs */
658edacedfSDaniel Fojt };
668edacedfSDaniel Fojt 
678edacedfSDaniel Fojt struct x509_verify_ctx {
688edacedfSDaniel Fojt 	X509_STORE_CTX *xsc;
698edacedfSDaniel Fojt 	struct x509_verify_chain **chains;	/* Validated chains */
70*de0e0e4dSAntonio Huete Jimenez 	STACK_OF(X509) *saved_error_chain;
71*de0e0e4dSAntonio Huete Jimenez 	int saved_error;
72*de0e0e4dSAntonio Huete Jimenez 	int saved_error_depth;
738edacedfSDaniel Fojt 	size_t chains_count;
748edacedfSDaniel Fojt 	STACK_OF(X509) *roots;		/* Trusted roots for this validation */
758edacedfSDaniel Fojt 	STACK_OF(X509) *intermediates;	/* Intermediates provided by peer */
768edacedfSDaniel Fojt 	time_t *check_time;		/* Time for validity checks */
778edacedfSDaniel Fojt 	int purpose;			/* Cert purpose we are validating */
788edacedfSDaniel Fojt 	size_t max_chains;		/* Max chains to return */
798edacedfSDaniel Fojt 	size_t max_depth;		/* Max chain depth for validation */
808edacedfSDaniel Fojt 	size_t max_sigs;		/* Max number of signature checks */
818edacedfSDaniel Fojt 	size_t sig_checks;		/* Number of signature checks done */
828edacedfSDaniel Fojt 	size_t error_depth;		/* Depth of last error seen */
838edacedfSDaniel Fojt 	int error;			/* Last error seen */
848edacedfSDaniel Fojt };
858edacedfSDaniel Fojt 
868edacedfSDaniel Fojt int ASN1_time_tm_clamp_notafter(struct tm *tm);
878edacedfSDaniel Fojt 
888edacedfSDaniel Fojt __BEGIN_HIDDEN_DECLS
898edacedfSDaniel Fojt 
908edacedfSDaniel Fojt int x509_vfy_check_id(X509_STORE_CTX *ctx);
918edacedfSDaniel Fojt int x509_vfy_check_revocation(X509_STORE_CTX *ctx);
928edacedfSDaniel Fojt int x509_vfy_check_policy(X509_STORE_CTX *ctx);
938edacedfSDaniel Fojt int x509_vfy_check_trust(X509_STORE_CTX *ctx);
948edacedfSDaniel Fojt int x509_vfy_check_chain_extensions(X509_STORE_CTX *ctx);
95*de0e0e4dSAntonio Huete Jimenez int x509_vfy_callback_indicate_completion(X509_STORE_CTX *ctx);
968edacedfSDaniel Fojt void x509v3_cache_extensions(X509 *x);
97*de0e0e4dSAntonio Huete Jimenez X509 *x509_vfy_lookup_cert_match(X509_STORE_CTX *ctx, X509 *x);
988edacedfSDaniel Fojt 
99*de0e0e4dSAntonio Huete Jimenez time_t x509_verify_asn1_time_to_time_t(const ASN1_TIME *atime, int notafter);
1008edacedfSDaniel Fojt 
101*de0e0e4dSAntonio Huete Jimenez struct x509_verify_ctx *x509_verify_ctx_new_from_xsc(X509_STORE_CTX *xsc);
1028edacedfSDaniel Fojt 
1038edacedfSDaniel Fojt void x509_constraints_name_clear(struct x509_constraints_name *name);
104*de0e0e4dSAntonio Huete Jimenez void x509_constraints_name_free(struct x509_constraints_name *name);
1058edacedfSDaniel Fojt int x509_constraints_names_add(struct x509_constraints_names *names,
1068edacedfSDaniel Fojt     struct x509_constraints_name *name);
1078edacedfSDaniel Fojt struct x509_constraints_names *x509_constraints_names_dup(
1088edacedfSDaniel Fojt     struct x509_constraints_names *names);
1098edacedfSDaniel Fojt void x509_constraints_names_clear(struct x509_constraints_names *names);
110*de0e0e4dSAntonio Huete Jimenez struct x509_constraints_names *x509_constraints_names_new(size_t names_max);
111*de0e0e4dSAntonio Huete Jimenez int x509_constraints_general_to_bytes(GENERAL_NAME *name, uint8_t **bytes,
112*de0e0e4dSAntonio Huete Jimenez     size_t *len);
1138edacedfSDaniel Fojt void x509_constraints_names_free(struct x509_constraints_names *names);
1148edacedfSDaniel Fojt int x509_constraints_valid_host(uint8_t *name, size_t len);
1158edacedfSDaniel Fojt int x509_constraints_valid_sandns(uint8_t *name, size_t len);
1168edacedfSDaniel Fojt int x509_constraints_domain(char *domain, size_t dlen, char *constraint,
1178edacedfSDaniel Fojt     size_t len);
1188edacedfSDaniel Fojt int x509_constraints_parse_mailbox(uint8_t *candidate, size_t len,
1198edacedfSDaniel Fojt     struct x509_constraints_name *name);
1208edacedfSDaniel Fojt int x509_constraints_valid_domain_constraint(uint8_t *constraint,
1218edacedfSDaniel Fojt     size_t len);
1228edacedfSDaniel Fojt int x509_constraints_uri_host(uint8_t *uri, size_t len, char **hostp);
1238edacedfSDaniel Fojt int x509_constraints_uri(uint8_t *uri, size_t ulen, uint8_t *constraint,
1248edacedfSDaniel Fojt     size_t len, int *error);
1258edacedfSDaniel Fojt int x509_constraints_extract_names(struct x509_constraints_names *names,
1268edacedfSDaniel Fojt     X509 *cert, int include_cn, int *error);
1278edacedfSDaniel Fojt int x509_constraints_extract_constraints(X509 *cert,
1288edacedfSDaniel Fojt     struct x509_constraints_names *permitted,
1298edacedfSDaniel Fojt     struct x509_constraints_names *excluded, int *error);
130*de0e0e4dSAntonio Huete Jimenez int x509_constraints_validate(GENERAL_NAME *constraint,
131*de0e0e4dSAntonio Huete Jimenez     struct x509_constraints_name **out_name, int *error);
1328edacedfSDaniel Fojt int x509_constraints_check(struct x509_constraints_names *names,
1338edacedfSDaniel Fojt     struct x509_constraints_names *permitted,
1348edacedfSDaniel Fojt     struct x509_constraints_names *excluded, int *error);
1358edacedfSDaniel Fojt int x509_constraints_chain(STACK_OF(X509) *chain, int *error,
1368edacedfSDaniel Fojt     int *depth);
137*de0e0e4dSAntonio Huete Jimenez void x509_verify_cert_info_populate(X509 *cert);
138*de0e0e4dSAntonio Huete Jimenez int x509_vfy_check_security_level(X509_STORE_CTX *ctx);
1398edacedfSDaniel Fojt 
1408edacedfSDaniel Fojt __END_HIDDEN_DECLS
1418edacedfSDaniel Fojt 
1428edacedfSDaniel Fojt #endif
143