openssl/x509/
store.rs
1use cfg_if::cfg_if;
45use foreign_types::{ForeignType, ForeignTypeRef};
46use std::mem;
47
48use crate::error::ErrorStack;
49#[cfg(not(any(boringssl, awslc)))]
50use crate::ssl::SslFiletype;
51#[cfg(ossl300)]
52use crate::stack::Stack;
53use crate::stack::StackRef;
54use crate::util::ForeignTypeRefExt;
55#[cfg(any(ossl102, boringssl, libressl261, awslc))]
56use crate::x509::verify::{X509VerifyFlags, X509VerifyParamRef};
57use crate::x509::{X509Object, X509PurposeId, X509};
58use crate::{cvt, cvt_p};
59use openssl_macros::corresponds;
60#[cfg(not(any(boringssl, awslc)))]
61use std::ffi::CString;
62#[cfg(not(any(boringssl, awslc)))]
63use std::path::Path;
64
65foreign_type_and_impl_send_sync! {
66 type CType = ffi::X509_STORE;
67 fn drop = ffi::X509_STORE_free;
68
69 pub struct X509StoreBuilder;
71 pub struct X509StoreBuilderRef;
73}
74
75impl X509StoreBuilder {
76 #[corresponds(X509_STORE_new)]
80 pub fn new() -> Result<X509StoreBuilder, ErrorStack> {
81 unsafe {
82 ffi::init();
83
84 cvt_p(ffi::X509_STORE_new()).map(X509StoreBuilder)
85 }
86 }
87
88 pub fn build(self) -> X509Store {
90 let store = X509Store(self.0);
91 mem::forget(self);
92 store
93 }
94}
95
96impl X509StoreBuilderRef {
97 #[corresponds(X509_STORE_add_cert)]
100 pub fn add_cert(&mut self, cert: X509) -> Result<(), ErrorStack> {
101 unsafe { cvt(ffi::X509_STORE_add_cert(self.as_ptr(), cert.as_ptr())).map(|_| ()) }
102 }
103
104 #[corresponds(X509_STORE_set_default_paths)]
110 pub fn set_default_paths(&mut self) -> Result<(), ErrorStack> {
111 unsafe { cvt(ffi::X509_STORE_set_default_paths(self.as_ptr())).map(|_| ()) }
112 }
113
114 #[corresponds(X509_STORE_add_lookup)]
116 pub fn add_lookup<T>(
117 &mut self,
118 method: &'static X509LookupMethodRef<T>,
119 ) -> Result<&mut X509LookupRef<T>, ErrorStack> {
120 let lookup = unsafe { ffi::X509_STORE_add_lookup(self.as_ptr(), method.as_ptr()) };
121 cvt_p(lookup).map(|ptr| unsafe { X509LookupRef::from_ptr_mut(ptr) })
122 }
123
124 #[corresponds(X509_STORE_set_flags)]
126 #[cfg(any(ossl102, boringssl, libressl261, awslc))]
127 pub fn set_flags(&mut self, flags: X509VerifyFlags) -> Result<(), ErrorStack> {
128 unsafe { cvt(ffi::X509_STORE_set_flags(self.as_ptr(), flags.bits())).map(|_| ()) }
129 }
130
131 #[corresponds(X509_STORE_set_purpose)]
134 pub fn set_purpose(&mut self, purpose: X509PurposeId) -> Result<(), ErrorStack> {
135 unsafe { cvt(ffi::X509_STORE_set_purpose(self.as_ptr(), purpose.as_raw())).map(|_| ()) }
136 }
137
138 #[corresponds[X509_STORE_set1_param]]
140 #[cfg(any(ossl102, boringssl, libressl261, awslc))]
141 pub fn set_param(&mut self, param: &X509VerifyParamRef) -> Result<(), ErrorStack> {
142 unsafe { cvt(ffi::X509_STORE_set1_param(self.as_ptr(), param.as_ptr())).map(|_| ()) }
143 }
144}
145
146generic_foreign_type_and_impl_send_sync! {
147 type CType = ffi::X509_LOOKUP;
148 fn drop = ffi::X509_LOOKUP_free;
149
150 pub struct X509Lookup<T>;
152 pub struct X509LookupRef<T>;
154}
155
156pub struct HashDir;
161
162impl X509Lookup<HashDir> {
163 #[corresponds(X509_LOOKUP_hash_dir)]
168 pub fn hash_dir() -> &'static X509LookupMethodRef<HashDir> {
169 unsafe { X509LookupMethodRef::from_const_ptr(ffi::X509_LOOKUP_hash_dir()) }
170 }
171}
172
173#[cfg(not(any(boringssl, awslc)))]
174impl X509LookupRef<HashDir> {
175 #[corresponds(X509_LOOKUP_add_dir)]
178 pub fn add_dir(&mut self, name: &str, file_type: SslFiletype) -> Result<(), ErrorStack> {
179 let name = CString::new(name).unwrap();
180 unsafe {
181 cvt(ffi::X509_LOOKUP_add_dir(
182 self.as_ptr(),
183 name.as_ptr(),
184 file_type.as_raw(),
185 ))
186 .map(|_| ())
187 }
188 }
189}
190
191pub struct File;
195
196impl X509Lookup<File> {
197 #[corresponds(X509_LOOKUP_file)]
200 pub fn file() -> &'static X509LookupMethodRef<File> {
201 unsafe { X509LookupMethodRef::from_const_ptr(ffi::X509_LOOKUP_file()) }
202 }
203}
204
205#[cfg(not(any(boringssl, awslc)))]
206impl X509LookupRef<File> {
207 #[corresponds(X509_load_cert_file)]
209 pub fn load_cert_file<P: AsRef<Path>>(
211 &mut self,
212 file: P,
213 file_type: SslFiletype,
214 ) -> Result<(), ErrorStack> {
215 let file = CString::new(file.as_ref().as_os_str().to_str().unwrap()).unwrap();
216 unsafe {
217 cvt(ffi::X509_load_cert_file(
218 self.as_ptr(),
219 file.as_ptr(),
220 file_type.as_raw(),
221 ))
222 .map(|_| ())
223 }
224 }
225
226 #[corresponds(X509_load_crl_file)]
228 pub fn load_crl_file<P: AsRef<Path>>(
229 &mut self,
230 file: P,
231 file_type: SslFiletype,
232 ) -> Result<i32, ErrorStack> {
233 let file = CString::new(file.as_ref().as_os_str().to_str().unwrap()).unwrap();
234 unsafe {
235 cvt(ffi::X509_load_crl_file(
236 self.as_ptr(),
237 file.as_ptr(),
238 file_type.as_raw(),
239 ))
240 }
241 }
242}
243
244generic_foreign_type_and_impl_send_sync! {
245 type CType = ffi::X509_LOOKUP_METHOD;
246 fn drop = X509_LOOKUP_meth_free;
247
248 pub struct X509LookupMethod<T>;
250 pub struct X509LookupMethodRef<T>;
252}
253
254foreign_type_and_impl_send_sync! {
255 type CType = ffi::X509_STORE;
256 fn drop = ffi::X509_STORE_free;
257
258 pub struct X509Store;
260 pub struct X509StoreRef;
262}
263
264impl X509StoreRef {
265 #[deprecated(
271 note = "This method is unsound, and will be removed in a future version of rust-openssl. X509StoreRef::all_certificates should be used instead."
272 )]
273 #[corresponds(X509_STORE_get0_objects)]
274 pub fn objects(&self) -> &StackRef<X509Object> {
275 unsafe { StackRef::from_ptr(X509_STORE_get0_objects(self.as_ptr())) }
276 }
277
278 #[corresponds(X509_STORE_get1_all_certs)]
280 #[cfg(ossl300)]
281 pub fn all_certificates(&self) -> Stack<X509> {
282 unsafe { Stack::from_ptr(ffi::X509_STORE_get1_all_certs(self.as_ptr())) }
283 }
284}
285
286cfg_if! {
287 if #[cfg(any(boringssl, ossl110, libressl270, awslc))] {
288 use ffi::X509_STORE_get0_objects;
289 } else {
290 #[allow(bad_style)]
291 unsafe fn X509_STORE_get0_objects(x: *mut ffi::X509_STORE) -> *mut ffi::stack_st_X509_OBJECT {
292 (*x).objs
293 }
294 }
295}
296
297cfg_if! {
298 if #[cfg(ossl110)] {
299 use ffi::X509_LOOKUP_meth_free;
300 } else {
301 #[allow(bad_style)]
302 unsafe fn X509_LOOKUP_meth_free(_x: *mut ffi::X509_LOOKUP_METHOD) {}
303 }
304}