| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![cfg_attr(feature = "deny-warnings", deny(warnings))] |
| 8 |
#![warn(clippy::pedantic)] |
| 9 |
// Bindgen auto generated code |
| 10 |
// won't adhere to the clippy rules below |
| 11 |
#![allow(clippy::module_name_repetitions)] |
| 12 |
#![allow(clippy::unseparated_literal_suffix)] |
| 13 |
#![allow(clippy::used_underscore_binding)] |
| 14 |
|
| 15 |
#[macro_use] |
| 16 |
pub mod err; |
| 17 |
#[macro_use] |
| 18 |
mod exp; |
| 19 |
#[macro_use] |
| 20 |
mod util; |
| 21 |
|
| 22 |
pub mod aead; |
| 23 |
pub mod der; |
| 24 |
pub mod ec; |
| 25 |
pub mod hash; |
| 26 |
pub mod hkdf; |
| 27 |
pub mod hmac; |
| 28 |
pub mod p11; |
| 29 |
mod prio; |
| 30 |
mod ssl; |
| 31 |
pub mod time; |
| 32 |
|
| 33 |
pub use err::{secstatus_to_res, Error, IntoResult}; |
| 34 |
pub use p11::{PrivateKey, PublicKey, SymKey}; |
| 35 |
pub use util::*; |
| 36 |
|
| 37 |
use once_cell::sync::OnceCell; |
| 38 |
|
| 39 |
use std::ffi::CString; |
| 40 |
use std::path::{Path, PathBuf}; |
| 41 |
use std::ptr::null; |
| 42 |
|
| 43 |
const MINIMUM_NSS_VERSION: &str = "3.74"; |
| 44 |
|
| 45 |
#[allow(non_snake_case)] |
| 46 |
#[allow(non_upper_case_globals)] |
| 47 |
pub mod nss_prelude { |
| 48 |
pub use crate::prtypes::*; |
| 49 |
pub use _SECStatus::*; |
| 50 |
include!(concat!(env!("OUT_DIR"), "/nss_prelude.rs")); |
| 51 |
} |
| 52 |
pub use nss_prelude::{SECItem, SECItemArray, SECItemType, SECStatus}; |
| 53 |
|
| 54 |
#[allow(non_upper_case_globals, clippy::redundant_static_lifetimes)] |
| 55 |
#[allow(clippy::upper_case_acronyms)] |
| 56 |
#[allow(unknown_lints, clippy::borrow_as_ptr)] |
| 57 |
mod nss { |
| 58 |
use crate::nss_prelude::*; |
| 59 |
include!(concat!(env!("OUT_DIR"), "/nss_init.rs")); |
| 60 |
} |
| 61 |
|
| 62 |
pub mod prtypes; |
| 63 |
pub use prtypes::*; |
| 64 |
|
| 65 |
// Shadow these bindgen created values to correct their type. |
| 66 |
pub const PR_FALSE: PRBool = prtypes::PR_FALSE as PRBool; |
| 67 |
pub const PR_TRUE: PRBool = prtypes::PR_TRUE as PRBool; |
| 68 |
|
| 69 |
enum NssLoaded { |
| 70 |
External, |
| 71 |
NoDb, |
| 72 |
#[allow(dead_code)] |
| 73 |
Db(Box<Path>), |
| 74 |
} |
| 75 |
|
| 76 |
impl Drop for NssLoaded { |
| 77 |
fn drop(&mut self) { |
| 78 |
if !matches!(self, Self::External) { |
| 79 |
unsafe { |
| 80 |
secstatus_to_res(nss::NSS_Shutdown()).expect("NSS Shutdown failed"); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static INITIALIZED: OnceCell<NssLoaded> = OnceCell::new(); |
| 87 |
|
| 88 |
fn already_initialized() -> bool { |
| 89 |
unsafe { nss::NSS_IsInitialized() != 0 } |
| 90 |
} |
| 91 |
|
| 92 |
fn version_check() { |
| 93 |
let min_ver = CString::new(MINIMUM_NSS_VERSION).unwrap(); |
| 94 |
assert_ne!( |
| 95 |
unsafe { nss::NSS_VersionCheck(min_ver.as_ptr()) }, |
| 96 |
0, |
| 97 |
"Minimum NSS version of {} not supported", |
| 98 |
MINIMUM_NSS_VERSION, |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/// Initialize NSS. This only executes the initialization routines once, so if there is any chance that |
| 103 |
pub fn init() { |
| 104 |
// Set time zero. |
| 105 |
time::init(); |
| 106 |
INITIALIZED.get_or_init(|| unsafe { |
| 107 |
version_check(); |
| 108 |
if already_initialized() { |
| 109 |
return NssLoaded::External; |
| 110 |
} |
| 111 |
|
| 112 |
secstatus_to_res(nss::NSS_NoDB_Init(null())).expect("NSS_NoDB_Init failed"); |
| 113 |
secstatus_to_res(nss::NSS_SetDomesticPolicy()).expect("NSS_SetDomesticPolicy failed"); |
| 114 |
|
| 115 |
NssLoaded::NoDb |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
/// This enables SSLTRACE by calling a simple, harmless function to trigger its |
| 120 |
/// side effects. SSLTRACE is not enabled in NSS until a socket is made or |
| 121 |
/// global options are accessed. Reading an option is the least impact approach. |
| 122 |
/// This allows us to use SSLTRACE in all of our unit tests and programs. |
| 123 |
#[cfg(debug_assertions)] |
| 124 |
fn enable_ssl_trace() { |
| 125 |
let opt = ssl::Opt::Locking.as_int(); |
| 126 |
let mut v: ::std::os::raw::c_int = 0; |
| 127 |
secstatus_to_res(unsafe { ssl::SSL_OptionGetDefault(opt, &mut v) }) |
| 128 |
.expect("SSL_OptionGetDefault failed"); |
| 129 |
} |
| 130 |
|
| 131 |
/// Initialize with a database. |
| 132 |
/// # Panics |
| 133 |
/// If NSS cannot be initialized. |
| 134 |
pub fn init_db<P: Into<PathBuf>>(dir: P) { |
| 135 |
time::init(); |
| 136 |
INITIALIZED.get_or_init(|| unsafe { |
| 137 |
version_check(); |
| 138 |
if already_initialized() { |
| 139 |
return NssLoaded::External; |
| 140 |
} |
| 141 |
|
| 142 |
let path = dir.into(); |
| 143 |
assert!(path.is_dir()); |
| 144 |
let pathstr = path.to_str().expect("path converts to string").to_string(); |
| 145 |
let dircstr = CString::new(pathstr).unwrap(); |
| 146 |
let empty = CString::new("").unwrap(); |
| 147 |
secstatus_to_res(nss::NSS_Initialize( |
| 148 |
dircstr.as_ptr(), |
| 149 |
empty.as_ptr(), |
| 150 |
empty.as_ptr(), |
| 151 |
nss::SECMOD_DB.as_ptr().cast(), |
| 152 |
nss::NSS_INIT_READONLY, |
| 153 |
)) |
| 154 |
.expect("NSS_Initialize failed"); |
| 155 |
|
| 156 |
secstatus_to_res(nss::NSS_SetDomesticPolicy()).expect("NSS_SetDomesticPolicy failed"); |
| 157 |
secstatus_to_res(ssl::SSL_ConfigServerSessionIDCache( |
| 158 |
1024, |
| 159 |
0, |
| 160 |
0, |
| 161 |
dircstr.as_ptr(), |
| 162 |
)) |
| 163 |
.expect("SSL_ConfigServerSessionIDCache failed"); |
| 164 |
|
| 165 |
#[cfg(debug_assertions)] |
| 166 |
enable_ssl_trace(); |
| 167 |
|
| 168 |
NssLoaded::Db(path.into_boxed_path()) |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
/// # Panics |
| 173 |
/// If NSS isn't initialized. |
| 174 |
pub fn assert_initialized() { |
| 175 |
INITIALIZED |
| 176 |
.get() |
| 177 |
.expect("NSS not initialized with init or init_db"); |
| 178 |
} |