Skip to main content

openssl/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(ossl110)]
52        const SUITEB_128_LOS_ONLY = ffi::X509_V_FLAG_SUITEB_128_LOS_ONLY;
53        #[cfg(ossl110)]
54        const SUITEB_192_LOS = ffi::X509_V_FLAG_SUITEB_128_LOS;
55        #[cfg(ossl110)]
56        const SUITEB_128_LOS = ffi::X509_V_FLAG_SUITEB_192_LOS;
57        const PARTIAL_CHAIN = ffi::X509_V_FLAG_PARTIAL_CHAIN as _;
58        const NO_ALT_CHAINS = ffi::X509_V_FLAG_NO_ALT_CHAINS as _;
59        const NO_CHECK_TIME = ffi::X509_V_FLAG_NO_CHECK_TIME as _;
60    }
61}
62
63foreign_type_and_impl_send_sync! {
64    type CType = ffi::X509_VERIFY_PARAM;
65    fn drop = ffi::X509_VERIFY_PARAM_free;
66
67    /// Adjust parameters associated with certificate verification.
68    pub struct X509VerifyParam;
69    /// Reference to `X509VerifyParam`.
70    pub struct X509VerifyParamRef;
71}
72
73impl X509VerifyParam {
74    /// Create an X509VerifyParam
75    #[corresponds(X509_VERIFY_PARAM_new)]
76    pub fn new() -> Result<X509VerifyParam, ErrorStack> {
77        unsafe {
78            ffi::init();
79            cvt_p(ffi::X509_VERIFY_PARAM_new()).map(X509VerifyParam)
80        }
81    }
82}
83
84impl X509VerifyParamRef {
85    /// Set the host flags.
86    #[corresponds(X509_VERIFY_PARAM_set_hostflags)]
87    pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) {
88        unsafe {
89            ffi::X509_VERIFY_PARAM_set_hostflags(self.as_ptr(), hostflags.bits());
90        }
91    }
92
93    /// Set verification flags.
94    #[corresponds(X509_VERIFY_PARAM_set_flags)]
95    pub fn set_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
96        unsafe {
97            cvt(ffi::X509_VERIFY_PARAM_set_flags(
98                self.as_ptr(),
99                flags.bits(),
100            ))
101            .map(|_| ())
102        }
103    }
104
105    /// Clear verification flags.
106    #[corresponds(X509_VERIFY_PARAM_clear_flags)]
107    pub fn clear_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
108        unsafe {
109            cvt(ffi::X509_VERIFY_PARAM_clear_flags(
110                self.as_ptr(),
111                flags.bits(),
112            ))
113            .map(|_| ())
114        }
115    }
116
117    /// Gets verification flags.
118    #[corresponds(X509_VERIFY_PARAM_get_flags)]
119    pub fn flags(&mut self) -> X509VerifyFlags {
120        let bits = unsafe { ffi::X509_VERIFY_PARAM_get_flags(self.as_ptr()) };
121        X509VerifyFlags::from_bits_retain(bits)
122    }
123
124    /// Set the expected DNS hostname.
125    #[corresponds(X509_VERIFY_PARAM_set1_host)]
126    pub fn set_host(&mut self, host: &str) -> Result<(), ErrorStack> {
127        unsafe {
128            // len == 0 means "run strlen" :(
129            let raw_host = if host.is_empty() { "\0" } else { host };
130            cvt(ffi::X509_VERIFY_PARAM_set1_host(
131                self.as_ptr(),
132                raw_host.as_ptr() as *const _,
133                host.len(),
134            ))
135            .map(|_| ())
136        }
137    }
138
139    /// Set the expected email address.
140    #[corresponds(X509_VERIFY_PARAM_set1_email)]
141    pub fn set_email(&mut self, email: &str) -> Result<(), ErrorStack> {
142        unsafe {
143            // len == 0 means "run strlen" :(
144            let raw_email = if email.is_empty() { "\0" } else { email };
145            cvt(ffi::X509_VERIFY_PARAM_set1_email(
146                self.as_ptr(),
147                raw_email.as_ptr() as *const _,
148                email.len(),
149            ))
150            .map(|_| ())
151        }
152    }
153
154    /// Set the expected IPv4 or IPv6 address.
155    #[corresponds(X509_VERIFY_PARAM_set1_ip)]
156    pub fn set_ip(&mut self, ip: IpAddr) -> Result<(), ErrorStack> {
157        unsafe {
158            let mut buf = [0; 16];
159            let len = match ip {
160                IpAddr::V4(addr) => {
161                    buf[..4].copy_from_slice(&addr.octets());
162                    4
163                }
164                IpAddr::V6(addr) => {
165                    buf.copy_from_slice(&addr.octets());
166                    16
167                }
168            };
169            cvt(ffi::X509_VERIFY_PARAM_set1_ip(
170                self.as_ptr(),
171                buf.as_ptr() as *const _,
172                len,
173            ))
174            .map(|_| ())
175        }
176    }
177
178    /// Set the verification time, where time is of type time_t, traditionally defined as seconds since the epoch
179    #[corresponds(X509_VERIFY_PARAM_set_time)]
180    pub fn set_time(&mut self, time: time_t) {
181        unsafe { ffi::X509_VERIFY_PARAM_set_time(self.as_ptr(), time) }
182    }
183
184    /// Set the verification depth
185    #[corresponds(X509_VERIFY_PARAM_set_depth)]
186    pub fn set_depth(&mut self, depth: c_int) {
187        unsafe { ffi::X509_VERIFY_PARAM_set_depth(self.as_ptr(), depth) }
188    }
189
190    /// Sets the authentication security level to auth_level
191    #[corresponds(X509_VERIFY_PARAM_set_auth_level)]
192    #[cfg(ossl110)]
193    pub fn set_auth_level(&mut self, lvl: c_int) {
194        unsafe { ffi::X509_VERIFY_PARAM_set_auth_level(self.as_ptr(), lvl) }
195    }
196
197    /// Gets the current authentication security level
198    #[corresponds(X509_VERIFY_PARAM_get_auth_level)]
199    #[cfg(ossl110)]
200    pub fn auth_level(&self) -> i32 {
201        unsafe { ffi::X509_VERIFY_PARAM_get_auth_level(self.as_ptr()) }
202    }
203
204    /// Sets the verification purpose
205    #[corresponds(X509_VERIFY_PARAM_set_purpose)]
206    pub fn set_purpose(&mut self, purpose: X509PurposeId) -> Result<(), ErrorStack> {
207        unsafe { cvt(ffi::X509_VERIFY_PARAM_set_purpose(self.as_ptr(), purpose.0)).map(|_| ()) }
208    }
209}