gmssl_sys/
lib.rs

1#![allow(
2    clippy::missing_safety_doc,
3    clippy::unreadable_literal,
4    clippy::uninlined_format_args,
5    clippy::upper_case_acronyms,
6    dead_code,
7    non_camel_case_types,
8    non_snake_case,
9    non_upper_case_globals,
10    overflowing_literals,
11    unused_imports
12)]
13#![doc(html_root_url = "https://docs.rs/gmssl-sys/0.1")]
14#![recursion_limit = "128"] // configure fixed limit across all rust versions
15
16extern crate libc;
17pub use libc::*;
18
19#[cfg(feature = "unstable_boringssl")]
20extern crate bssl_sys;
21#[cfg(feature = "unstable_boringssl")]
22pub use bssl_sys::*;
23
24#[cfg(all(boringssl, not(feature = "unstable_boringssl")))]
25#[path = "."]
26mod boringssl {
27    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
28
29    pub fn init() {
30        unsafe {
31            CRYPTO_library_init();
32        }
33    }
34}
35#[cfg(all(boringssl, not(feature = "unstable_boringssl")))]
36pub use boringssl::*;
37
38#[cfg(openssl)]
39#[path = "."]
40mod openssl {
41    use libc::*;
42
43    #[cfg(feature = "bindgen")]
44    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
45
46    pub use self::aes::*;
47    pub use self::asn1::*;
48    pub use self::bio::*;
49    pub use self::bn::*;
50    pub use self::cms::*;
51    pub use self::crypto::*;
52    pub use self::dtls1::*;
53    pub use self::ec::*;
54    pub use self::err::*;
55    pub use self::evp::*;
56    #[cfg(not(feature = "bindgen"))]
57    pub use self::handwritten::*;
58    pub use self::obj_mac::*;
59    pub use self::ocsp::*;
60    pub use self::pem::*;
61    pub use self::pkcs7::*;
62    pub use self::rsa::*;
63    pub use self::sha::*;
64    pub use self::srtp::*;
65    pub use self::ssl::*;
66    pub use self::ssl3::*;
67    pub use self::tls1::*;
68    pub use self::types::*;
69    pub use self::x509::*;
70    pub use self::x509_vfy::*;
71    pub use self::x509v3::*;
72
73    #[macro_use]
74    mod macros;
75
76    mod aes;
77    mod asn1;
78    mod bio;
79    mod bn;
80    mod cms;
81    mod crypto;
82    mod dtls1;
83    mod ec;
84    mod err;
85    mod evp;
86    #[cfg(not(feature = "bindgen"))]
87    mod handwritten;
88    mod obj_mac;
89    mod ocsp;
90    mod pem;
91    mod pkcs7;
92    mod rsa;
93    mod sha;
94    mod srtp;
95    mod ssl;
96    mod ssl3;
97    mod tls1;
98    mod types;
99    mod x509;
100    mod x509_vfy;
101    mod x509v3;
102
103    use std::sync::Once;
104    // explicitly initialize to work around https://github.com/openssl/openssl/issues/3505
105    static INIT: Once = Once::new();
106
107    // FIXME remove
108    pub type PasswordCallback = unsafe extern "C" fn(
109        buf: *mut c_char,
110        size: c_int,
111        rwflag: c_int,
112        user_data: *mut c_void,
113    ) -> c_int;
114
115    #[cfg(ossl110)]
116    pub fn init() {
117        use std::ptr;
118
119        #[cfg(not(ossl111b))]
120        let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS;
121        #[cfg(ossl111b)]
122        let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_NO_ATEXIT;
123
124        INIT.call_once(|| unsafe {
125            OPENSSL_init_ssl(init_options, ptr::null_mut());
126        })
127    }
128
129    #[cfg(not(ossl110))]
130    pub fn init() {
131        use std::io::{self, Write};
132        use std::mem;
133        use std::process;
134        use std::sync::{Mutex, MutexGuard};
135
136        static mut MUTEXES: *mut Vec<Mutex<()>> = 0 as *mut Vec<Mutex<()>>;
137        static mut GUARDS: *mut Vec<Option<MutexGuard<'static, ()>>> =
138            0 as *mut Vec<Option<MutexGuard<'static, ()>>>;
139
140        unsafe extern "C" fn locking_function(
141            mode: c_int,
142            n: c_int,
143            _file: *const c_char,
144            _line: c_int,
145        ) {
146            let mutex = &(*MUTEXES)[n as usize];
147
148            if mode & CRYPTO_LOCK != 0 {
149                (*GUARDS)[n as usize] = Some(mutex.lock().unwrap());
150            } else {
151                if let None = (*GUARDS)[n as usize].take() {
152                    let _ = writeln!(
153                        io::stderr(),
154                        "BUG: rust-openssl lock {} already unlocked, aborting",
155                        n
156                    );
157                    process::abort();
158                }
159            }
160        }
161
162        cfg_if! {
163            if #[cfg(unix)] {
164                fn set_id_callback() {
165                    unsafe extern "C" fn thread_id() -> c_ulong {
166                        ::libc::pthread_self() as c_ulong
167                    }
168
169                    unsafe {
170                        CRYPTO_set_id_callback__fixed_rust(Some(thread_id));
171                    }
172                }
173            } else {
174                fn set_id_callback() {}
175            }
176        }
177
178        INIT.call_once(|| unsafe {
179            SSL_library_init();
180            SSL_load_error_strings();
181            OPENSSL_add_all_algorithms_noconf();
182
183            let num_locks = CRYPTO_num_locks();
184            let mut mutexes = Box::new(Vec::new());
185            for _ in 0..num_locks {
186                mutexes.push(Mutex::new(()));
187            }
188            MUTEXES = mem::transmute(mutexes);
189            let guards: Box<Vec<Option<MutexGuard<()>>>> =
190                Box::new((0..num_locks).map(|_| None).collect());
191            GUARDS = mem::transmute(guards);
192
193            CRYPTO_set_locking_callback__fixed_rust(Some(locking_function));
194            set_id_callback();
195        })
196    }
197
198    /// Disable explicit initialization of the openssl libs.
199    ///
200    /// This is only appropriate to use if the openssl crate is being consumed by an application
201    /// that will be performing the initialization explicitly.
202    ///
203    /// # Safety
204    ///
205    /// In some versions of openssl, skipping initialization will fall back to the default procedure
206    /// while other will cause difficult to debug errors so care must be taken when calling this.
207    pub unsafe fn assume_init() {
208        INIT.call_once(|| {});
209    }
210}
211
212#[cfg(openssl)]
213pub use openssl::*;