boring/
lib.rs

1//! Bindings to BoringSSL
2//!
3//! This crate provides a safe interface to the BoringSSL cryptography library.
4//!
5//! # Versioning
6//!
7//! ## Crate versioning
8//!
9//! The crate and all the related crates (FFI bindings, etc.) are released simultaneously and all
10//! bumped to the same version disregard whether particular crate has any API changes or not.
11//! However, semantic versioning guarantees still hold, as all the crate versions will be updated
12//! based on the crate with most significant changes.
13//!
14//! ## BoringSSL version
15//!
16//! By default, the crate aims to statically link with the latest BoringSSL master branch.
17//! *Note*: any BoringSSL revision bumps will be released as a major version update of all crates.
18//!
19//! # Compilation and linking options
20//!
21//! ## Environment variables
22//!
23//! This crate uses various environment variables to tweak how boring is built. The variables
24//! are all prefixed by `BORING_BSSL_` for non-FIPS builds, and by `BORING_BSSL_FIPS_` for FIPS builds.
25//!
26//! ## Support for pre-built binaries or custom source
27//!
28//! While this crate can build BoringSSL on its own, you may want to provide pre-built binaries instead.
29//! To do so, specify the environment variable `BORING_BSSL{,_FIPS}_PATH` with the path to the binaries.
30//!
31//! You can also provide specific headers by setting `BORING_BSSL{,_FIPS}_INCLUDE_PATH`.
32//!
33//! _Notes_: The crate will look for headers in the`$BORING_BSSL{,_FIPS}_INCLUDE_PATH/openssl/`
34//! folder, make sure to place your headers there.
35//!
36//! In alternative a different path for the BoringSSL source code directory can be specified by setting
37//! `BORING_BSSL{,_FIPS}_SOURCE_PATH` which will automatically be compiled during the build process.
38//!
39//! _Warning_: When providing a different version of BoringSSL make sure to use a compatible one, the
40//! crate relies on the presence of certain functions.
41//!
42//! ## Building with a FIPS-validated module
43//!
44//! Only BoringCrypto module version `853ca1ea1168dff08011e5d42d94609cc0ca2e27`, as certified with
45//! [FIPS 140-2 certificate 4407](https://csrc.nist.gov/projects/cryptographic-module-validation-program/certificate/4407)
46//! is supported by this crate. Support is enabled by this crate's `fips` feature.
47//!
48//! `boring-sys` comes with a test that FIPS is enabled/disabled depending on the feature flag. You can run it as follows:
49//!
50//! ```bash
51//! $ cargo test --features fips fips::is_enabled
52//! ```
53//!
54//! ## Linking current BoringSSL version with precompiled FIPS-validated module (`bcm.o`)
55//!
56//! It's possible to link latest supported version of BoringSSL with FIPS-validated crypto module
57//! (`bcm.o`). To enable this compilation option one should enable `fips-link-precompiled`
58//! compilation feature and provide a `BORING_BSSL_FIPS_PRECOMPILED_BCM_O` env variable with a path to the
59//! precompiled FIPS-validated `bcm.o` module.
60//!
61//! Note that `BORING_BSSL_PRECOMPILED_BCM_O` is never used, as linking BoringSSL with precompiled non-FIPS
62//! module is not supported.
63//!
64//! ## Linking with a C++ standard library
65//!
66//! Recent versions of boringssl require some C++ standard library features, so boring needs to link
67//! with a STL implementation. This can be controlled using the BORING_BSSL_RUST_CPPLIB variable. If
68//! no library is specified, libc++ is used on macOS and iOS whereas libstdc++ is used on other Unix
69//! systems.
70//!
71//! # Optional patches
72//!
73//! ## Raw Public Key
74//!
75//! The crate can be compiled with [RawPublicKey](https://datatracker.ietf.org/doc/html/rfc7250)
76//! support by turning on `rpk` compilation feature.
77//!
78//! ## Experimental post-quantum cryptography
79//!
80//! The crate can be compiled with [post-quantum cryptography](https://blog.cloudflare.com/post-quantum-for-all/)
81//! support by turning on `post-quantum` compilation feature.
82//!
83//! Upstream BoringSSL support the post-quantum hybrid key agreement `X25519Kyber768Draft00`. Most
84//! users should stick to that one for now. Enabling this feature, adds a few other post-quantum key
85//! agreements:
86//!
87//! - `X25519MLKEM768` is the successor of `X25519Kyber768Draft00`. We expect servers to switch
88//!   before the end of 2024.
89//! - `X25519Kyber768Draft00Old` is the same as `X25519Kyber768Draft00`, but under its old codepoint.
90//! - `X25519Kyber512Draft00`. Similar to `X25519Kyber768Draft00`, but uses level 1 parameter set for
91//!   Kyber. Not recommended. It's useful to test whether the shorter ClientHello upsets fewer middle
92//!   boxes.
93//! - `P256Kyber768Draft00`. Similar again to `X25519Kyber768Draft00`, but uses P256 as classical
94//!   part. It uses a non-standard codepoint. Not recommended.
95//!
96//! Presently all these key agreements are deployed by Cloudflare, but we do not guarantee continued
97//! support for them.
98
99#[macro_use]
100extern crate bitflags;
101#[macro_use]
102extern crate foreign_types;
103extern crate boring_sys as ffi;
104extern crate libc;
105
106#[cfg(test)]
107extern crate hex;
108
109use std::ffi::{c_long, c_void};
110
111#[doc(inline)]
112pub use crate::ffi::init;
113
114use libc::{c_int, size_t};
115
116use crate::error::ErrorStack;
117
118#[macro_use]
119mod macros;
120
121mod bio;
122#[macro_use]
123mod util;
124pub mod aes;
125pub mod asn1;
126pub mod base64;
127pub mod bn;
128pub mod conf;
129pub mod derive;
130pub mod dh;
131pub mod dsa;
132pub mod ec;
133pub mod ecdsa;
134pub mod error;
135pub mod ex_data;
136pub mod fips;
137pub mod hash;
138#[cfg(not(feature = "fips"))]
139pub mod hpke;
140pub mod memcmp;
141pub mod nid;
142pub mod pkcs12;
143pub mod pkcs5;
144pub mod pkey;
145pub mod rand;
146pub mod rsa;
147pub mod sha;
148pub mod sign;
149pub mod srtp;
150pub mod ssl;
151pub mod stack;
152pub mod string;
153pub mod symm;
154pub mod version;
155pub mod x509;
156
157fn cvt_p<T>(r: *mut T) -> Result<*mut T, ErrorStack> {
158    if r.is_null() {
159        Err(ErrorStack::get())
160    } else {
161        Ok(r)
162    }
163}
164
165fn cvt_0(r: size_t) -> Result<size_t, ErrorStack> {
166    if r == 0 {
167        Err(ErrorStack::get())
168    } else {
169        Ok(r)
170    }
171}
172
173fn cvt_0i(r: c_int) -> Result<c_int, ErrorStack> {
174    if r == 0 {
175        Err(ErrorStack::get())
176    } else {
177        Ok(r)
178    }
179}
180
181fn cvt(r: c_int) -> Result<c_int, ErrorStack> {
182    if r <= 0 {
183        Err(ErrorStack::get())
184    } else {
185        Ok(r)
186    }
187}
188
189fn cvt_n(r: c_int) -> Result<c_int, ErrorStack> {
190    if r < 0 {
191        Err(ErrorStack::get())
192    } else {
193        Ok(r)
194    }
195}
196
197unsafe extern "C" fn free_data_box<T>(
198    _parent: *mut c_void,
199    ptr: *mut c_void,
200    _ad: *mut ffi::CRYPTO_EX_DATA,
201    _idx: c_int,
202    _argl: c_long,
203    _argp: *mut c_void,
204) {
205    if !ptr.is_null() {
206        drop(Box::<T>::from_raw(ptr as *mut T));
207    }
208}