1 //! Bindings to OpenSSL
2 //!
3 //! This crate provides a safe interface to the popular OpenSSL cryptography library. OpenSSL versions 1.0.1 through
4 //! 1.1.1 and LibreSSL versions 2.5 through 3.3.x are supported.
5 //!
6 //! # Building
7 //!
8 //! Both OpenSSL libraries and headers are required to build this crate. There are multiple options available to locate
9 //! OpenSSL.
10 //!
11 //! ## Vendored
12 //!
13 //! If the `vendored` Cargo feature is enabled, the `openssl-src` crate will be used to compile and statically link to
14 //! a copy of OpenSSL. The build process requires a C compiler, perl, and make. The OpenSSL version will generally track
15 //! the newest OpenSSL release, and changes to the version are *not* considered breaking changes.
16 //!
17 //! ```toml
18 //! [dependencies]
19 //! openssl = { version = "0.10", features = ["vendored"] }
20 //! ```
21 //!
22 //! The vendored copy will not be configured to automatically find the system's root certificates, but the
23 //! `openssl-probe` crate can be used to do that instead.
24 //!
25 //! ## Automatic
26 //!
27 //! The `openssl-sys` crate will automatically detect OpenSSL installations via Homebrew on macOS and vcpkg on Windows.
28 //! Additionally, it will use `pkg-config` on Unix-like systems to find the system installation.
29 //!
30 //! ```not_rust
31 //! # macOS (Homebrew)
32 //! $ brew install openssl@1.1
33 //!
34 //! # macOS (MacPorts)
35 //! $ sudo port install openssl
36 //!
37 //! # macOS (pkgsrc)
38 //! $ sudo pkgin install openssl
39 //!
40 //! # Arch Linux
41 //! $ sudo pacman -S pkg-config openssl
42 //!
43 //! # Debian and Ubuntu
44 //! $ sudo apt-get install pkg-config libssl-dev
45 //!
46 //! # Fedora
47 //! $ sudo dnf install pkg-config openssl-devel
48 //! ```
49 //!
50 //! ## Manual
51 //!
52 //! A set of environment variables can be used to point `openssl-sys` towards an OpenSSL installation. They will
53 //! override the automatic detection logic.
54 //!
55 //! * `OPENSSL_DIR` - If specified, the directory of an OpenSSL installation. The directory should contain `lib` and
56 //!     `include` subdirectories containing the libraries and headers respectively.
57 //! * `OPENSSL_LIB_DIR` and `OPENSSL_INCLUDE_DIR` - If specified, the directories containing the OpenSSL libraries and
58 //!     headers respectively. This can be used if the OpenSSL installation is split in a nonstandard directory layout.
59 //! * `OPENSSL_STATIC` - If set, the crate will statically link to OpenSSL rather than dynamically link.
60 //! * `OPENSSL_LIBS` - If set, a `:`-separated list of library names to link to (e.g. `ssl:crypto`). This can be used
61 //!     if nonstandard library names were used for whatever reason.
62 //! * `OPENSSL_NO_VENDOR` - If set, always find OpenSSL in the system, even if the `vendored` feature is enabled.
63 //!
64 //! Additionally, these variables can be prefixed with the upper-cased target architecture (e.g.
65 //!     `X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR`), which can be useful when cross compiling.
66 //!
67 //! # Feature Detection
68 //!
69 //! APIs have been added to and removed from the various supported OpenSSL versions, and this library exposes the
70 //! functionality available in the version being linked against. This means that methods, constants, and even modules
71 //! will be present when building against one version of OpenSSL but not when building against another! APIs will
72 //! document any version-specific availability restrictions.
73 //!
74 //! A build script can be used to detect the OpenSSL or LibreSSL version at compile time if needed. The `openssl-sys`
75 //! crate propagates the version via the `DEP_OPENSSL_VERSION_NUMBER` and `DEP_OPENSSL_LIBRESSL_VERSION_NUMBER`
76 //! environment variables to build scripts. The version format is a hex-encoding of the OpenSSL release version:
77 //! `0xMNNFFPPS`. For example, version 1.0.2g's encoding is `0x1_00_02_07_0`.
78 //!
79 //! For example, let's say we want to adjust the TLSv1.3 cipher suites used by a client, but also want to compile
80 //! against OpenSSL versions that don't support TLSv1.3:
81 //!
82 //! Cargo.toml:
83 //!
84 //! ```toml
85 //! [dependencies]
86 //! openssl-sys = "0.9"
87 //! openssl = "0.10"
88 //! ```
89 //!
90 //! build.rs:
91 //!
92 //! ```
93 //! use std::env;
94 //!
95 //! fn main() {
96 //!     if let Ok(v) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
97 //!         let version = u64::from_str_radix(&v, 16).unwrap();
98 //!
99 //!         if version >= 0x1_01_01_00_0 {
100 //!             println!("cargo:rustc-cfg=openssl111");
101 //!         }
102 //!     }
103 //! }
104 //! ```
105 //!
106 //! lib.rs:
107 //!
108 //! ```
109 //! use openssl::ssl::{SslConnector, SslMethod};
110 //!
111 //! let mut ctx = SslConnector::builder(SslMethod::tls()).unwrap();
112 //!
113 //! // set_ciphersuites was added in OpenSSL 1.1.1, so we can only call it when linking against that version
114 //! #[cfg(openssl111)]
115 //! ctx.set_ciphersuites("TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256").unwrap();
116 //! ```
117 #![doc(html_root_url = "https://docs.rs/openssl/0.10")]
118 #![warn(rust_2018_idioms)]
119 
120 #[doc(inline)]
121 pub use ffi::init;
122 
123 use libc::c_int;
124 
125 use crate::error::ErrorStack;
126 
127 #[macro_use]
128 mod macros;
129 
130 mod bio;
131 #[macro_use]
132 mod util;
133 pub mod aes;
134 pub mod asn1;
135 pub mod base64;
136 pub mod bn;
137 #[cfg(all(not(libressl), not(osslconf = "OPENSSL_NO_CMS")))]
138 pub mod cms;
139 pub mod conf;
140 pub mod derive;
141 pub mod dh;
142 pub mod dsa;
143 pub mod ec;
144 pub mod ecdsa;
145 pub mod encrypt;
146 pub mod envelope;
147 pub mod error;
148 pub mod ex_data;
149 #[cfg(not(any(libressl, ossl300)))]
150 pub mod fips;
151 pub mod hash;
152 pub mod memcmp;
153 pub mod nid;
154 #[cfg(not(osslconf = "OPENSSL_NO_OCSP"))]
155 pub mod ocsp;
156 pub mod pkcs12;
157 pub mod pkcs5;
158 pub mod pkcs7;
159 pub mod pkey;
160 pub mod rand;
161 pub mod rsa;
162 pub mod sha;
163 pub mod sign;
164 pub mod srtp;
165 pub mod ssl;
166 pub mod stack;
167 pub mod string;
168 pub mod symm;
169 pub mod version;
170 pub mod x509;
171 
cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack>172 fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
173     if r.is_null() {
174         Err(ErrorStack::get())
175     } else {
176         Ok(r)
177     }
178 }
179 
cvt(r: c_int) -> Result<c_int, ErrorStack>180 fn cvt(r: c_int) -> Result<c_int, ErrorStack> {
181     if r <= 0 {
182         Err(ErrorStack::get())
183     } else {
184         Ok(r)
185     }
186 }
187 
cvt_n(r: c_int) -> Result<c_int, ErrorStack>188 fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
189     if r < 0 {
190         Err(ErrorStack::get())
191     } else {
192         Ok(r)
193     }
194 }
195