openssl/
lib.rs

1//! Bindings to OpenSSL
2//!
3//! This crate provides a safe interface to the popular OpenSSL cryptography library. OpenSSL versions 1.0.2 through
4//! 3.x.x and LibreSSL versions 3.5 through 4.2.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 perl-core), 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 be configured to automatically find a configuration and root certificates at `/usr/local/ssl`.
23//! This path can be overridden with an environment variable (see the manual section below).
24//! Alternatively, the `openssl-probe` crate can be used to find root certificates at runtime.
25//!
26//! ## Automatic
27//!
28//! The `openssl-sys` crate will automatically detect OpenSSL installations via Homebrew on macOS and vcpkg on Windows.
29//! Additionally, it will use `pkg-config` on Unix-like systems to find the system installation.
30//!
31//! ```not_rust
32//! # macOS (Homebrew)
33//! $ brew install openssl@3
34//!
35//! # macOS (MacPorts)
36//! $ sudo port install openssl
37//!
38//! # macOS (pkgsrc)
39//! $ sudo pkgin install openssl
40//!
41//! # Arch Linux
42//! $ sudo pacman -S pkgconf openssl
43//!
44//! # Debian and Ubuntu
45//! $ sudo apt-get install pkg-config libssl-dev
46//!
47//! # Fedora
48//! $ sudo dnf install pkgconf perl-FindBin perl-IPC-Cmd openssl-devel
49//!
50//! # Alpine Linux
51//! $ apk add pkgconf openssl-dev
52//!
53//! # openSUSE
54//! $ sudo zypper in libopenssl-devel
55//! ```
56//!
57//! ## Manual
58//!
59//! A set of environment variables can be used to point `openssl-sys` towards an OpenSSL installation. They will
60//! override the automatic detection logic.
61//!
62//! * `OPENSSL_DIR` - If specified, the directory of an OpenSSL installation. The directory should contain `lib` and
63//!   `include` subdirectories containing the libraries and headers respectively.
64//! * `OPENSSL_LIB_DIR` and `OPENSSL_INCLUDE_DIR` - If specified, the directories containing the OpenSSL libraries and
65//!   headers respectively. This can be used if the OpenSSL installation is split in a nonstandard directory layout.
66//! * `OPENSSL_STATIC` - If set, the crate will statically link to OpenSSL rather than dynamically link.
67//! * `OPENSSL_LIBS` - If set, a `:`-separated list of library names to link to (e.g. `ssl:crypto`). This can be used
68//!   if nonstandard library names were used for whatever reason.
69//! * `OPENSSL_NO_VENDOR` - If set, always find OpenSSL in the system, even if the `vendored` feature is enabled.
70//!
71//! If the `vendored` Cargo feature is enabled, the following environment variable can also be used to further configure
72//! the OpenSSL build.
73//!
74//! * `OPENSSL_CONFIG_DIR` - If set, the copy of OpenSSL built by the `openssl-src` crate will be configured to look for
75//!   configuration files and root certificates in this directory.
76//!
77//! Additionally, these variables can be prefixed with the upper-cased target architecture (e.g.
78//!     `X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR`), which can be useful when cross compiling.
79//!
80//! # Feature Detection
81//!
82//! APIs have been added to and removed from the various supported OpenSSL versions, and this library exposes the
83//! functionality available in the version being linked against. This means that methods, constants, and even modules
84//! will be present when building against one version of OpenSSL but not when building against another! APIs will
85//! document any version-specific availability restrictions.
86//!
87//! A build script can be used to detect the OpenSSL or LibreSSL version at compile time if needed. The `openssl-sys`
88//! crate propagates the version via the `DEP_OPENSSL_VERSION_NUMBER` and `DEP_OPENSSL_LIBRESSL_VERSION_NUMBER`
89//! environment variables to build scripts. The version format is a hex-encoding of the OpenSSL release version:
90//! `0xMNNFFPPS`. For example, version 1.0.2g's encoding is `0x1_00_02_07_0`.
91//!
92//! For example, let's say we want to adjust the TLSv1.3 cipher suites used by a client, but also want to compile
93//! against OpenSSL versions that don't support TLSv1.3:
94//!
95//! Cargo.toml:
96//!
97//! ```toml
98//! [dependencies]
99//! openssl-sys = "0.9"
100//! openssl = "0.10"
101//! ```
102//!
103//! build.rs:
104//!
105//! ```
106//! use std::env;
107//!
108//! fn main() {
109//!     if let Ok(v) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
110//!         let version = u64::from_str_radix(&v, 16).unwrap();
111//!
112//!         if version >= 0x1_01_01_00_0 {
113//!             println!("cargo:rustc-cfg=openssl111");
114//!         }
115//!     }
116//! }
117//! ```
118//!
119//! lib.rs:
120//!
121//! ```
122//! use openssl::ssl::{SslConnector, SslMethod};
123//!
124//! let mut ctx = SslConnector::builder(SslMethod::tls()).unwrap();
125//!
126//! // set_ciphersuites was added in OpenSSL 1.1.1, so we can only call it when linking against that version
127//! #[cfg(openssl111)]
128//! ctx.set_ciphersuites("TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256").unwrap();
129//! ```
130#![doc(html_root_url = "https://docs.rs/openssl/0.10")]
131#![warn(rust_2018_idioms)]
132#![allow(clippy::uninlined_format_args, clippy::needless_doctest_main)]
133
134#[doc(inline)]
135pub use ffi::init;
136
137use libc::c_int;
138#[cfg(ossl300)]
139use libc::c_long;
140
141use crate::error::ErrorStack;
142
143#[macro_use]
144mod macros;
145
146mod bio;
147#[macro_use]
148mod util;
149pub mod aes;
150pub mod asn1;
151pub mod base64;
152pub mod bn;
153pub mod cipher;
154pub mod cipher_ctx;
155#[cfg(all(not(libressl), not(osslconf = "OPENSSL_NO_CMS")))]
156pub mod cms;
157pub mod conf;
158pub mod derive;
159pub mod dh;
160pub mod dsa;
161pub mod ec;
162pub mod ecdsa;
163pub mod encrypt;
164#[cfg(not(any(boringssl, awslc)))]
165pub mod envelope;
166pub mod error;
167pub mod ex_data;
168#[cfg(not(any(libressl, ossl300)))]
169pub mod fips;
170pub mod hash;
171pub mod kdf;
172#[cfg(ossl300)]
173pub mod lib_ctx;
174pub mod md;
175pub mod md_ctx;
176pub mod memcmp;
177pub mod nid;
178#[cfg(not(osslconf = "OPENSSL_NO_OCSP"))]
179pub mod ocsp;
180#[cfg(ossl300)]
181mod ossl_param;
182pub mod pkcs12;
183pub mod pkcs5;
184#[cfg(not(any(boringssl, awslc)))]
185pub mod pkcs7;
186pub mod pkey;
187pub mod pkey_ctx;
188#[cfg(ossl300)]
189pub mod provider;
190pub mod rand;
191pub mod rsa;
192pub mod sha;
193pub mod sign;
194pub mod srtp;
195pub mod ssl;
196pub mod stack;
197pub mod string;
198pub mod symm;
199pub mod version;
200pub mod x509;
201
202#[cfg(any(boringssl, awslc))]
203type LenType = libc::size_t;
204#[cfg(not(any(boringssl, awslc)))]
205type LenType = libc::c_int;
206
207#[cfg(any(boringssl, awslc))]
208type SLenType = libc::ssize_t;
209#[cfg(not(any(boringssl, awslc)))]
210type SLenType = libc::c_int;
211
212#[inline]
213fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
214    if r.is_null() {
215        Err(ErrorStack::get())
216    } else {
217        Ok(r)
218    }
219}
220
221#[inline]
222fn cvt_p_const<T>(r: *const T) -> Result<*const T, ErrorStack> {
223    if r.is_null() {
224        Err(ErrorStack::get())
225    } else {
226        Ok(r)
227    }
228}
229
230#[inline]
231fn cvt(r: c_int) -> Result<c_int, ErrorStack> {
232    if r <= 0 {
233        Err(ErrorStack::get())
234    } else {
235        Ok(r)
236    }
237}
238
239// cvt_long is currently only used in functions that require openssl >= 3.0.0,
240// so this cfg statement is used to avoid "unused function" errors when
241// compiling with openssl < 3.0.0
242#[inline]
243#[cfg(ossl300)]
244fn cvt_long(r: c_long) -> Result<c_long, ErrorStack> {
245    if r <= 0 {
246        Err(ErrorStack::get())
247    } else {
248        Ok(r)
249    }
250}
251
252#[inline]
253fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
254    if r < 0 {
255        Err(ErrorStack::get())
256    } else {
257        Ok(r)
258    }
259}