1 #![allow(
2     clippy::missing_safety_doc,
3     clippy::unreadable_literal,
4     dead_code,
5     non_camel_case_types,
6     non_snake_case,
7     non_upper_case_globals,
8     overflowing_literals,
9     unused_imports
10 )]
11 #![doc(html_root_url = "https://docs.rs/openssl-sys/0.9")]
12 
13 extern crate libc;
14 
15 use libc::*;
16 
17 pub use aes::*;
18 pub use asn1::*;
19 pub use bio::*;
20 pub use bn::*;
21 pub use cms::*;
22 pub use conf::*;
23 pub use crypto::*;
24 pub use dh::*;
25 pub use dsa::*;
26 pub use dtls1::*;
27 pub use ec::*;
28 pub use err::*;
29 pub use evp::*;
30 pub use hmac::*;
31 pub use obj_mac::*;
32 pub use object::*;
33 pub use ocsp::*;
34 pub use ossl_typ::*;
35 pub use pem::*;
36 pub use pkcs12::*;
37 pub use pkcs7::*;
38 pub use rand::*;
39 pub use rsa::*;
40 pub use safestack::*;
41 pub use sha::*;
42 pub use srtp::*;
43 pub use ssl::*;
44 pub use ssl3::*;
45 pub use stack::*;
46 pub use tls1::*;
47 pub use x509::*;
48 pub use x509_vfy::*;
49 pub use x509v3::*;
50 
51 #[macro_use]
52 mod macros;
53 
54 mod aes;
55 mod asn1;
56 mod bio;
57 mod bn;
58 mod cms;
59 mod conf;
60 mod crypto;
61 mod dh;
62 mod dsa;
63 mod dtls1;
64 mod ec;
65 mod err;
66 mod evp;
67 mod hmac;
68 mod obj_mac;
69 mod object;
70 mod ocsp;
71 mod ossl_typ;
72 mod pem;
73 mod pkcs12;
74 mod pkcs7;
75 mod rand;
76 mod rsa;
77 mod safestack;
78 mod sha;
79 mod srtp;
80 mod ssl;
81 mod ssl3;
82 mod stack;
83 mod tls1;
84 mod x509;
85 mod x509_vfy;
86 mod x509v3;
87 
88 // FIXME remove
89 pub type PasswordCallback = unsafe extern "C" fn(
90     buf: *mut c_char,
91     size: c_int,
92     rwflag: c_int,
93     user_data: *mut c_void,
94 ) -> c_int;
95 
96 #[cfg(ossl110)]
init()97 pub fn init() {
98     use std::ptr;
99     use std::sync::Once;
100 
101     // explicitly initialize to work around https://github.com/openssl/openssl/issues/3505
102     static INIT: Once = Once::new();
103 
104     INIT.call_once(|| unsafe {
105         OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, ptr::null_mut());
106     })
107 }
108 
109 #[cfg(not(ossl110))]
init()110 pub fn init() {
111     use std::io::{self, Write};
112     use std::mem;
113     use std::process;
114     use std::sync::{Mutex, MutexGuard, Once};
115 
116     static mut MUTEXES: *mut Vec<Mutex<()>> = 0 as *mut Vec<Mutex<()>>;
117     static mut GUARDS: *mut Vec<Option<MutexGuard<'static, ()>>> =
118         0 as *mut Vec<Option<MutexGuard<'static, ()>>>;
119 
120     unsafe extern "C" fn locking_function(
121         mode: c_int,
122         n: c_int,
123         _file: *const c_char,
124         _line: c_int,
125     ) {
126         let mutex = &(*MUTEXES)[n as usize];
127 
128         if mode & ::CRYPTO_LOCK != 0 {
129             (*GUARDS)[n as usize] = Some(mutex.lock().unwrap());
130         } else {
131             if let None = (*GUARDS)[n as usize].take() {
132                 let _ = writeln!(
133                     io::stderr(),
134                     "BUG: rust-openssl lock {} already unlocked, aborting",
135                     n
136                 );
137                 process::abort();
138             }
139         }
140     }
141 
142     cfg_if! {
143         if #[cfg(unix)] {
144             fn set_id_callback() {
145                 unsafe extern "C" fn thread_id() -> c_ulong {
146                     ::libc::pthread_self() as c_ulong
147                 }
148 
149                 unsafe {
150                     CRYPTO_set_id_callback(thread_id);
151                 }
152             }
153         } else {
154             fn set_id_callback() {}
155         }
156     }
157 
158     static INIT: Once = Once::new();
159 
160     INIT.call_once(|| unsafe {
161         SSL_library_init();
162         SSL_load_error_strings();
163         OPENSSL_add_all_algorithms_noconf();
164 
165         let num_locks = ::CRYPTO_num_locks();
166         let mut mutexes = Box::new(Vec::new());
167         for _ in 0..num_locks {
168             mutexes.push(Mutex::new(()));
169         }
170         MUTEXES = mem::transmute(mutexes);
171         let guards: Box<Vec<Option<MutexGuard<()>>>> =
172             Box::new((0..num_locks).map(|_| None).collect());
173         GUARDS = mem::transmute(guards);
174 
175         CRYPTO_set_locking_callback(locking_function);
176         set_id_callback();
177     })
178 }
179