tongsuo_sys/
lib.rs

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