Skip to main content

boring/x509/
verify.rs

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    /// Flags used to check an `X509` certificate.
12    #[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    /// Flags used to check an `X509` certificate.
28    #[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    /// Adjust parameters associated with certificate verification.
58    pub struct X509VerifyParam;
59}
60
61impl X509VerifyParam {
62    /// Create an X509VerifyParam
63    #[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    /// Set verification flags.
75    #[corresponds(X509_VERIFY_PARAM_set_flags)]
76    pub fn set_flags(&mut self, flags: X509VerifyFlags) {
77        self.try_set_flags(flags).expect("use try_set_flags");
78    }
79
80    /// Set verification flags.
81    #[corresponds(X509_VERIFY_PARAM_set_flags)]
82    pub fn try_set_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
83        unsafe {
84            cvt(ffi::X509_VERIFY_PARAM_set_flags(
85                self.as_ptr(),
86                flags.bits(),
87            ))
88        }
89    }
90
91    /// Clear verification flags.
92    #[corresponds(X509_VERIFY_PARAM_clear_flags)]
93    pub fn clear_flags(&mut self, flags: X509VerifyFlags) {
94        self.try_clear_flags(flags).expect("use try_clear_flags");
95    }
96
97    /// Clear verification flags.
98    #[corresponds(X509_VERIFY_PARAM_clear_flags)]
99    pub fn try_clear_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
100        unsafe {
101            cvt(ffi::X509_VERIFY_PARAM_clear_flags(
102                self.as_ptr(),
103                flags.bits(),
104            ))
105        }
106    }
107
108    /// Set the host flags.
109    #[corresponds(X509_VERIFY_PARAM_set_hostflags)]
110    pub fn set_hostflags(&mut self, hostflags: X509CheckFlags) {
111        unsafe {
112            ffi::X509_VERIFY_PARAM_set_hostflags(self.as_ptr(), hostflags.bits());
113        }
114    }
115
116    /// Gets verification flags.
117    #[corresponds(X509_VERIFY_PARAM_get_flags)]
118    #[must_use]
119    pub fn flags(&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().cast(),
133                host.len(),
134            ))
135        }
136    }
137
138    /// Set the expected email address.
139    #[corresponds(X509_VERIFY_PARAM_set1_email)]
140    pub fn set_email(&mut self, email: &str) -> Result<(), ErrorStack> {
141        unsafe {
142            // len == 0 means "run strlen" :(
143            let raw_email = if email.is_empty() { "\0" } else { email };
144            cvt(ffi::X509_VERIFY_PARAM_set1_email(
145                self.as_ptr(),
146                raw_email.as_ptr().cast(),
147                email.len(),
148            ))
149        }
150    }
151
152    /// Set the expected IPv4 or IPv6 address.
153    #[corresponds(X509_VERIFY_PARAM_set1_ip)]
154    pub fn set_ip(&mut self, ip: IpAddr) -> Result<(), ErrorStack> {
155        unsafe {
156            let mut buf = [0; 16];
157            let len = match ip {
158                IpAddr::V4(addr) => {
159                    buf[..4].copy_from_slice(&addr.octets());
160                    4
161                }
162                IpAddr::V6(addr) => {
163                    buf.copy_from_slice(&addr.octets());
164                    16
165                }
166            };
167            cvt(ffi::X509_VERIFY_PARAM_set1_ip(
168                self.as_ptr(),
169                buf.as_ptr().cast(),
170                len,
171            ))
172        }
173    }
174
175    /// Set the verification time, where time is of type time_t, traditionaly defined as seconds since the epoch
176    #[corresponds(X509_VERIFY_PARAM_set_time)]
177    pub fn set_time(&mut self, time: time_t) {
178        unsafe { ffi::X509_VERIFY_PARAM_set_time(self.as_ptr(), time) }
179    }
180
181    /// Set the verification depth
182    #[corresponds(X509_VERIFY_PARAM_set_depth)]
183    pub fn set_depth(&mut self, depth: c_int) {
184        unsafe { ffi::X509_VERIFY_PARAM_set_depth(self.as_ptr(), depth) }
185    }
186
187    /// Copies parameters from `src`.
188    ///
189    /// If a parameter is unset in `src`, the existing value in `self`` is preserved.
190    #[corresponds(X509_VERIFY_PARAM_set1)]
191    pub fn copy_from(&mut self, src: &Self) -> Result<(), ErrorStack> {
192        unsafe { cvt(ffi::X509_VERIFY_PARAM_set1(self.as_ptr(), src.as_ptr())) }
193    }
194}