Skip to main content

openssl_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#![doc(html_root_url = "https://docs.rs/openssl-sys/0.9")]
10#![recursion_limit = "128"] // configure fixed limit across all rust versions
11
12extern crate libc;
13pub use libc::c_int;
14
15#[cfg(feature = "unstable_boringssl")]
16extern crate bssl_sys;
17
18#[cfg(boringssl)]
19#[path = "."]
20mod boringssl {
21    #[cfg(feature = "unstable_boringssl")]
22    pub use bssl_sys::*;
23    #[cfg(not(feature = "unstable_boringssl"))]
24    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
25
26    // BoringSSL does not require initialization.
27    pub fn init() {}
28}
29#[cfg(boringssl)]
30pub use boringssl::*;
31
32#[cfg(feature = "aws-lc")]
33extern crate aws_lc_sys;
34
35#[cfg(awslc)]
36#[path = "."]
37#[allow(unpredictable_function_pointer_comparisons)]
38mod aws_lc {
39    #[cfg(all(feature = "aws-lc", not(feature = "aws-lc-fips")))]
40    pub use aws_lc_sys::*;
41
42    #[cfg(feature = "aws-lc-fips")]
43    pub use aws_lc_fips_sys::*;
44
45    #[cfg(not(any(feature = "aws-lc", feature = "aws-lc-fips")))]
46    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
47
48    use libc::{c_char, c_long, c_void};
49
50    pub fn init() {
51        unsafe { CRYPTO_library_init() }
52    }
53
54    // BIO_get_mem_data is a C preprocessor macro by definition
55    #[allow(non_snake_case, clippy::not_unsafe_ptr_arg_deref)]
56    pub fn BIO_get_mem_data(b: *mut BIO, pp: *mut *mut c_char) -> c_long {
57        unsafe { BIO_ctrl(b, BIO_CTRL_INFO, 0, pp.cast::<c_void>()) }
58    }
59
60    // ERR_GET_{LIB,REASON,FUNC} are macros/static inlines in AWS-LC and
61    // therefore not emitted by pregenerated bindings. We provide pure-Rust
62    // implementations matching the logic in aws-lc-sys.
63    //
64    // When aws-lc-sys is used (feature = "aws-lc" or "aws-lc-fips"), these
65    // come from the glob import instead. When normal bindgen runs
66    // (wrap_static_fns), they're in the generated output.
67    #[cfg(awslc_pregenerated)]
68    #[allow(non_snake_case, clippy::cast_possible_wrap)]
69    pub fn ERR_GET_LIB(packed_error: ::libc::c_uint) -> ::libc::c_int {
70        ((packed_error >> 24) & 0xFF) as ::libc::c_int
71    }
72
73    #[cfg(awslc_pregenerated)]
74    #[allow(non_snake_case, clippy::cast_possible_wrap)]
75    pub fn ERR_GET_REASON(packed_error: ::libc::c_uint) -> ::libc::c_int {
76        (packed_error & 0xFFF) as ::libc::c_int
77    }
78
79    #[cfg(awslc_pregenerated)]
80    #[allow(non_snake_case)]
81    pub fn ERR_GET_FUNC(_packed_error: ::libc::c_uint) -> ::libc::c_int {
82        0
83    }
84}
85#[cfg(awslc)]
86pub use aws_lc::*;
87
88#[cfg(openssl)]
89#[path = "."]
90mod openssl {
91    use libc::*;
92
93    #[cfg(feature = "bindgen")]
94    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
95
96    pub use self::aes::*;
97    pub use self::asn1::*;
98    pub use self::bio::*;
99    pub use self::bn::*;
100    pub use self::cms::*;
101    #[cfg(ossl300)]
102    pub use self::core_dispatch::*;
103    pub use self::crypto::*;
104    pub use self::dh::*;
105    pub use self::dsa::*;
106    pub use self::dtls1::*;
107    pub use self::ec::*;
108    pub use self::err::*;
109    pub use self::evp::*;
110    #[cfg(not(feature = "bindgen"))]
111    pub use self::handwritten::*;
112    pub use self::obj_mac::*;
113    pub use self::ocsp::*;
114    pub use self::pem::*;
115    pub use self::pkcs7::*;
116    pub use self::rsa::*;
117    pub use self::sha::*;
118    pub use self::srtp::*;
119    pub use self::ssl::*;
120    pub use self::ssl3::*;
121    pub use self::tls1::*;
122    pub use self::types::*;
123    pub use self::x509::*;
124    pub use self::x509_vfy::*;
125    pub use self::x509v3::*;
126
127    #[macro_use]
128    mod macros;
129
130    mod aes;
131    mod asn1;
132    mod bio;
133    mod bn;
134    mod cms;
135    #[cfg(ossl300)]
136    mod core_dispatch;
137    mod crypto;
138    mod dh;
139    mod dsa;
140    mod dtls1;
141    mod ec;
142    mod err;
143    mod evp;
144    #[cfg(not(feature = "bindgen"))]
145    mod handwritten;
146    mod obj_mac;
147    mod ocsp;
148    mod pem;
149    mod pkcs7;
150    mod rsa;
151    mod sha;
152    mod srtp;
153    mod ssl;
154    mod ssl3;
155    mod tls1;
156    mod types;
157    mod x509;
158    mod x509_vfy;
159    mod x509v3;
160
161    use std::sync::Once;
162    // explicitly initialize to work around https://github.com/openssl/openssl/issues/3505
163    static INIT: Once = Once::new();
164
165    // FIXME remove
166    pub type PasswordCallback = unsafe extern "C" fn(
167        buf: *mut c_char,
168        size: c_int,
169        rwflag: c_int,
170        user_data: *mut c_void,
171    ) -> c_int;
172
173    #[cfg(ossl110)]
174    pub fn init() {
175        use std::ptr;
176
177        #[cfg(not(ossl111b))]
178        let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS;
179        #[cfg(ossl111b)]
180        let init_options = OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_NO_ATEXIT;
181
182        INIT.call_once(|| unsafe {
183            OPENSSL_init_ssl(init_options, ptr::null_mut());
184        })
185    }
186
187    #[cfg(libressl)]
188    pub fn init() {}
189
190    /// Disable explicit initialization of the openssl libs.
191    ///
192    /// This is only appropriate to use if the openssl crate is being consumed by an application
193    /// that will be performing the initialization explicitly.
194    ///
195    /// # Safety
196    ///
197    /// In some versions of openssl, skipping initialization will fall back to the default procedure
198    /// while other will cause difficult to debug errors so care must be taken when calling this.
199    pub unsafe fn assume_init() {
200        INIT.call_once(|| {});
201    }
202}
203#[cfg(openssl)]
204pub use openssl::*;