variant_ssl/x509/
verify.rs

1use bitflags::bitflags;
2use foreign_types::ForeignTypeRef;
3use libc::{c_int, c_uint, c_ulong, time_t};
4use std::net::IpAddr;
5
6use crate::error::ErrorStack;
7use crate::x509::X509PurposeId;
8use crate::{cvt, cvt_p};
9use openssl_macros::corresponds;
10
11bitflags! {
12    /// Flags used to check an `X509` certificate.
13    #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
14    #[repr(transparent)]
15    pub struct X509CheckFlags: c_uint {
16        const ALWAYS_CHECK_SUBJECT = ffi::X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT as _;
17        const NO_WILDCARDS = ffi::X509_CHECK_FLAG_NO_WILDCARDS as _;
18        const NO_PARTIAL_WILDCARDS = ffi::X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS as _;
19        const MULTI_LABEL_WILDCARDS = ffi::X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS as _;
20        const SINGLE_LABEL_SUBDOMAINS = ffi::X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS as _;
21        /// Requires OpenSSL 1.1.0 or newer.
22        #[cfg(any(ossl110))]
23        const NEVER_CHECK_SUBJECT = ffi::X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
24
25        #[deprecated(since = "0.10.6", note = "renamed to NO_WILDCARDS")]
26        const FLAG_NO_WILDCARDS = ffi::X509_CHECK_FLAG_NO_WILDCARDS as _;
27    }
28}
29
30bitflags! {
31    /// Flags used to verify an `X509` certificate chain.
32    #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
33    #[repr(transparent)]
34    pub struct X509VerifyFlags: c_ulong {
35        const CB_ISSUER_CHECK = ffi::X509_V_FLAG_CB_ISSUER_CHECK as _;
36        const USE_CHECK_TIME = ffi::X509_V_FLAG_USE_CHECK_TIME as _;
37        const CRL_CHECK = ffi::X509_V_FLAG_CRL_CHECK as _;
38        const CRL_CHECK_ALL = ffi::X509_V_FLAG_CRL_CHECK_ALL as _;
39        const IGNORE_CRITICAL = ffi::X509_V_FLAG_IGNORE_CRITICAL as _;
40        const X509_STRICT = ffi::X509_V_FLAG_X509_STRICT as _;
41        const ALLOW_PROXY_CERTS = ffi::X509_V_FLAG_ALLOW_PROXY_CERTS as _;
42        const POLICY_CHECK = ffi::X509_V_FLAG_POLICY_CHECK as _;
43        const EXPLICIT_POLICY = ffi::X509_V_FLAG_EXPLICIT_POLICY as _;
44        const INHIBIT_ANY = ffi::X509_V_FLAG_INHIBIT_ANY as _;
45        const INHIBIT_MAP = ffi::X509_V_FLAG_INHIBIT_MAP as _;
46        const NOTIFY_POLICY = ffi::X509_V_FLAG_NOTIFY_POLICY as _;
47        const EXTENDED_CRL_SUPPORT = ffi::X509_V_FLAG_EXTENDED_CRL_SUPPORT as _;
48        const USE_DELTAS = ffi::X509_V_FLAG_USE_DELTAS as _;
49        const CHECK_SS_SIGNATURE = ffi::X509_V_FLAG_CHECK_SS_SIGNATURE as _;
50        const TRUSTED_FIRST = ffi::X509_V_FLAG_TRUSTED_FIRST as _;
51        #[cfg(ossl102)]
52        const SUITEB_128_LOS_ONLY = ffi::X509_V_FLAG_SUITEB_128_LOS_ONLY;
53        #[cfg(ossl102)]
54        const SUITEB_192_LOS = ffi::X509_V_FLAG_SUITEB_128_LOS;
55        #[cfg(ossl102)]
56        const SUITEB_128_LOS = ffi::X509_V_FLAG_SUITEB_192_LOS;
57        const PARTIAL_CHAIN = ffi::X509_V_FLAG_PARTIAL_CHAIN as _;
58        #[cfg(any(ossl110, boringssl, awslc, libressl))]
59        const NO_ALT_CHAINS = ffi::X509_V_FLAG_NO_ALT_CHAINS as _;
60        #[cfg(any(ossl110, boringssl, awslc, libressl))]
61        const NO_CHECK_TIME = ffi::X509_V_FLAG_NO_CHECK_TIME as _;
62    }
63}
64
65foreign_type_and_impl_send_sync! {
66    type CType = ffi::X509_VERIFY_PARAM;
67    fn drop = ffi::X509_VERIFY_PARAM_free;
68
69    /// Adjust parameters associated with certificate verification.
70    pub struct X509VerifyParam;
71    /// Reference to `X509VerifyParam`.
72    pub struct X509VerifyParamRef;
73}
74
75impl X509VerifyParam {
76    /// Create an X509VerifyParam
77    #[corresponds(X509_VERIFY_PARAM_new)]
78    pub fn new() -> Result<X509VerifyParam, ErrorStack> {
79        unsafe {
80            ffi::init();
81            cvt_p(ffi::X509_VERIFY_PARAM_new()).map(X509VerifyParam)
82        }
83    }
84}
85
86impl X509VerifyParamRef {
87    /// Set the host flags.
88    #[corresponds(X509_VERIFY_PARAM_set_hostflags)]
89    pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) {
90        unsafe {
91            ffi::X509_VERIFY_PARAM_set_hostflags(self.as_ptr(), hostflags.bits());
92        }
93    }
94
95    /// Set verification flags.
96    #[corresponds(X509_VERIFY_PARAM_set_flags)]
97    pub fn set_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
98        unsafe {
99            cvt(ffi::X509_VERIFY_PARAM_set_flags(
100                self.as_ptr(),
101                flags.bits(),
102            ))
103            .map(|_| ())
104        }
105    }
106
107    /// Clear verification flags.
108    #[corresponds(X509_VERIFY_PARAM_clear_flags)]
109    pub fn clear_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
110        unsafe {
111            cvt(ffi::X509_VERIFY_PARAM_clear_flags(
112                self.as_ptr(),
113                flags.bits(),
114            ))
115            .map(|_| ())
116        }
117    }
118
119    /// Gets verification flags.
120    #[corresponds(X509_VERIFY_PARAM_get_flags)]
121    pub fn flags(&mut self) -> X509VerifyFlags {
122        let bits = unsafe { ffi::X509_VERIFY_PARAM_get_flags(self.as_ptr()) };
123        X509VerifyFlags::from_bits_retain(bits)
124    }
125
126    /// Set the expected DNS hostname.
127    #[corresponds(X509_VERIFY_PARAM_set1_host)]
128    pub fn set_host(&mut self, host: &str) -> Result<(), ErrorStack> {
129        unsafe {
130            // len == 0 means "run strlen" :(
131            let raw_host = if host.is_empty() { "\0" } else { host };
132            cvt(ffi::X509_VERIFY_PARAM_set1_host(
133                self.as_ptr(),
134                raw_host.as_ptr() as *const _,
135                host.len(),
136            ))
137            .map(|_| ())
138        }
139    }
140
141    /// Set the expected email address.
142    #[corresponds(X509_VERIFY_PARAM_set1_email)]
143    pub fn set_email(&mut self, email: &str) -> Result<(), ErrorStack> {
144        unsafe {
145            // len == 0 means "run strlen" :(
146            let raw_email = if email.is_empty() { "\0" } else { email };
147            cvt(ffi::X509_VERIFY_PARAM_set1_email(
148                self.as_ptr(),
149                raw_email.as_ptr() as *const _,
150                email.len(),
151            ))
152            .map(|_| ())
153        }
154    }
155
156    /// Set the expected IPv4 or IPv6 address.
157    #[corresponds(X509_VERIFY_PARAM_set1_ip)]
158    pub fn set_ip(&mut self, ip: IpAddr) -> Result<(), ErrorStack> {
159        unsafe {
160            let mut buf = [0; 16];
161            let len = match ip {
162                IpAddr::V4(addr) => {
163                    buf[..4].copy_from_slice(&addr.octets());
164                    4
165                }
166                IpAddr::V6(addr) => {
167                    buf.copy_from_slice(&addr.octets());
168                    16
169                }
170            };
171            cvt(ffi::X509_VERIFY_PARAM_set1_ip(
172                self.as_ptr(),
173                buf.as_ptr() as *const _,
174                len,
175            ))
176            .map(|_| ())
177        }
178    }
179
180    /// Set the verification time, where time is of type time_t, traditionaly defined as seconds since the epoch
181    #[corresponds(X509_VERIFY_PARAM_set_time)]
182    pub fn set_time(&mut self, time: time_t) {
183        unsafe { ffi::X509_VERIFY_PARAM_set_time(self.as_ptr(), time) }
184    }
185
186    /// Set the verification depth
187    #[corresponds(X509_VERIFY_PARAM_set_depth)]
188    pub fn set_depth(&mut self, depth: c_int) {
189        unsafe { ffi::X509_VERIFY_PARAM_set_depth(self.as_ptr(), depth) }
190    }
191
192    /// Sets the authentication security level to auth_level
193    #[corresponds(X509_VERIFY_PARAM_set_auth_level)]
194    #[cfg(ossl110)]
195    pub fn set_auth_level(&mut self, lvl: c_int) {
196        unsafe { ffi::X509_VERIFY_PARAM_set_auth_level(self.as_ptr(), lvl) }
197    }
198
199    /// Gets the current authentication security level
200    #[corresponds(X509_VERIFY_PARAM_get_auth_level)]
201    #[cfg(ossl110)]
202    pub fn auth_level(&self) -> i32 {
203        unsafe { ffi::X509_VERIFY_PARAM_get_auth_level(self.as_ptr()) }
204    }
205
206    /// Sets the verification purpose
207    #[corresponds(X509_VERIFY_PARAM_set_purpose)]
208    pub fn set_purpose(&mut self, purpose: X509PurposeId) -> Result<(), ErrorStack> {
209        unsafe { cvt(ffi::X509_VERIFY_PARAM_set_purpose(self.as_ptr(), purpose.0)).map(|_| ()) }
210    }
211}