1use crate::ffi;
2use foreign_types::{ForeignType, ForeignTypeRef};
3use libc::{c_int, c_uint, c_ulong, time_t};
4use openssl_macros::corresponds;
5use std::net::IpAddr;
6
7use crate::error::ErrorStack;
8use crate::{cvt, cvt_p};
9
10bitflags! {
11 #[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
13 #[repr(transparent)]
14 pub struct X509CheckFlags: c_uint {
15 const ALWAYS_CHECK_SUBJECT = ffi::X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT as _;
16 const NO_WILDCARDS = ffi::X509_CHECK_FLAG_NO_WILDCARDS as _;
17 const NO_PARTIAL_WILDCARDS = ffi::X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS as _;
18 const MULTI_LABEL_WILDCARDS = ffi::X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS as _;
19 const SINGLE_LABEL_SUBDOMAINS = ffi::X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS as _;
20 const NEVER_CHECK_SUBJECT = ffi::X509_CHECK_FLAG_NEVER_CHECK_SUBJECT as _;
21 #[cfg(feature = "underscore-wildcards")]
22 const UNDERSCORE_WILDCARDS = ffi::X509_CHECK_FLAG_UNDERSCORE_WILDCARDS as _;
23 }
24}
25
26bitflags! {
27 #[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
29 #[repr(transparent)]
30 #[doc(alias = "X509Flags")]
31 pub struct X509VerifyFlags: c_ulong {
32 const CB_ISSUER_CHECK = ffi::X509_V_FLAG_CB_ISSUER_CHECK as _;
33 const USE_CHECK_TIME = ffi::X509_V_FLAG_USE_CHECK_TIME as _;
34 const CRL_CHECK = ffi::X509_V_FLAG_CRL_CHECK as _;
35 const CRL_CHECK_ALL = ffi::X509_V_FLAG_CRL_CHECK_ALL as _;
36 const IGNORE_CRITICAL = ffi::X509_V_FLAG_IGNORE_CRITICAL as _;
37 const X509_STRICT = ffi::X509_V_FLAG_X509_STRICT as _;
38 const ALLOW_PROXY_CERTS = ffi::X509_V_FLAG_ALLOW_PROXY_CERTS as _;
39 const POLICY_CHECK = ffi::X509_V_FLAG_POLICY_CHECK as _;
40 const EXPLICIT_POLICY = ffi::X509_V_FLAG_EXPLICIT_POLICY as _;
41 const INHIBIT_ANY = ffi::X509_V_FLAG_INHIBIT_ANY as _;
42 const INHIBIT_MAP = ffi::X509_V_FLAG_INHIBIT_MAP as _;
43 const NOTIFY_POLICY = ffi::X509_V_FLAG_NOTIFY_POLICY as _;
44 const EXTENDED_CRL_SUPPORT = ffi::X509_V_FLAG_EXTENDED_CRL_SUPPORT as _;
45 const USE_DELTAS = ffi::X509_V_FLAG_USE_DELTAS as _;
46 const CHECK_SS_SIGNATURE = ffi::X509_V_FLAG_CHECK_SS_SIGNATURE as _;
47 const TRUSTED_FIRST = ffi::X509_V_FLAG_TRUSTED_FIRST as _;
48 const PARTIAL_CHAIN = ffi::X509_V_FLAG_PARTIAL_CHAIN as _;
49 const NO_ALT_CHAINS = ffi::X509_V_FLAG_NO_ALT_CHAINS as _;
50 }
51}
52
53foreign_type_and_impl_send_sync! {
54 type CType = ffi::X509_VERIFY_PARAM;
55 fn drop = ffi::X509_VERIFY_PARAM_free;
56
57 pub struct X509VerifyParam;
59}
60
61impl X509VerifyParam {
62 #[corresponds(X509_VERIFY_PARAM_new)]
64 pub fn new() -> Result<Self, ErrorStack> {
65 unsafe {
66 ffi::init();
67 let handle = cvt_p(ffi::X509_VERIFY_PARAM_new())?;
68 Ok(Self::from_ptr(handle))
69 }
70 }
71}
72
73impl X509VerifyParamRef {
74 #[corresponds(X509_VERIFY_PARAM_set_flags)]
76 pub fn set_flags(&mut self, flags: X509VerifyFlags) {
77 unsafe {
78 cvt(ffi::X509_VERIFY_PARAM_set_flags(
79 self.as_ptr(),
80 flags.bits(),
81 ))
82 .unwrap();
83 }
84 }
85
86 #[corresponds(X509_VERIFY_PARAM_clear_flags)]
88 pub fn clear_flags(&mut self, flags: X509VerifyFlags) {
89 unsafe {
90 cvt(ffi::X509_VERIFY_PARAM_clear_flags(
91 self.as_ptr(),
92 flags.bits(),
93 ))
94 .unwrap();
95 }
96 }
97
98 #[corresponds(X509_VERIFY_PARAM_set_hostflags)]
101 pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) {
102 unsafe {
103 ffi::X509_VERIFY_PARAM_set_hostflags(self.as_ptr(), hostflags.bits());
104 }
105 }
106
107 #[corresponds(X509_VERIFY_PARAM_get_flags)]
109 #[must_use]
110 pub fn flags(&self) -> X509VerifyFlags {
111 let bits = unsafe { ffi::X509_VERIFY_PARAM_get_flags(self.as_ptr()) };
112 X509VerifyFlags::from_bits_retain(bits)
113 }
114
115 #[corresponds(X509_VERIFY_PARAM_set1_host)]
117 pub fn set_host(&mut self, host: &str) -> Result<(), ErrorStack> {
118 unsafe {
119 let raw_host = if host.is_empty() { "\0" } else { host };
121 cvt(ffi::X509_VERIFY_PARAM_set1_host(
122 self.as_ptr(),
123 raw_host.as_ptr().cast(),
124 host.len(),
125 ))
126 }
127 }
128
129 #[corresponds(X509_VERIFY_PARAM_set1_email)]
131 pub fn set_email(&mut self, email: &str) -> Result<(), ErrorStack> {
132 unsafe {
133 let raw_email = if email.is_empty() { "\0" } else { email };
135 cvt(ffi::X509_VERIFY_PARAM_set1_email(
136 self.as_ptr(),
137 raw_email.as_ptr().cast(),
138 email.len(),
139 ))
140 }
141 }
142
143 #[corresponds(X509_VERIFY_PARAM_set1_ip)]
145 pub fn set_ip(&mut self, ip: IpAddr) -> Result<(), ErrorStack> {
146 unsafe {
147 let mut buf = [0; 16];
148 let len = match ip {
149 IpAddr::V4(addr) => {
150 buf[..4].copy_from_slice(&addr.octets());
151 4
152 }
153 IpAddr::V6(addr) => {
154 buf.copy_from_slice(&addr.octets());
155 16
156 }
157 };
158 cvt(ffi::X509_VERIFY_PARAM_set1_ip(
159 self.as_ptr(),
160 buf.as_ptr().cast(),
161 len,
162 ))
163 }
164 }
165
166 #[corresponds(X509_VERIFY_PARAM_set_time)]
168 pub fn set_time(&mut self, time: time_t) {
169 unsafe { ffi::X509_VERIFY_PARAM_set_time(self.as_ptr(), time) }
170 }
171
172 #[corresponds(X509_VERIFY_PARAM_set_depth)]
174 pub fn set_depth(&mut self, depth: c_int) {
175 unsafe { ffi::X509_VERIFY_PARAM_set_depth(self.as_ptr(), depth) }
176 }
177
178 #[corresponds(X509_VERIFY_PARAM_set1)]
182 pub fn copy_from(&mut self, src: &Self) -> Result<(), ErrorStack> {
183 unsafe { cvt(ffi::X509_VERIFY_PARAM_set1(self.as_ptr(), src.as_ptr())) }
184 }
185}