| 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 |
mod err; |
| 8 |
#[macro_use] |
| 9 |
mod p11; |
| 10 |
pub mod aead; |
| 11 |
pub mod hkdf; |
| 12 |
pub mod hpke; |
| 13 |
|
| 14 |
pub use self::p11::{random, PrivateKey, PublicKey}; |
| 15 |
use err::secstatus_to_res; |
| 16 |
pub use err::Error; |
| 17 |
use lazy_static::lazy_static; |
| 18 |
use std::ptr::null; |
| 19 |
|
| 20 |
#[allow(clippy::pedantic, non_upper_case_globals, clippy::upper_case_acronyms)] |
| 21 |
mod nss_init { |
| 22 |
include!(concat!(env!("OUT_DIR"), "/nss_init.rs")); |
| 23 |
} |
| 24 |
|
| 25 |
use nss_init::SECStatus; |
| 26 |
#[allow(non_upper_case_globals)] |
| 27 |
const SECSuccess: SECStatus = nss_init::_SECStatus_SECSuccess; |
| 28 |
#[cfg(test)] |
| 29 |
#[allow(non_upper_case_globals)] |
| 30 |
const SECFailure: SECStatus = nss_init::_SECStatus_SECFailure; |
| 31 |
|
| 32 |
#[derive(PartialEq, Eq)] |
| 33 |
enum NssLoaded { |
| 34 |
External, |
| 35 |
NoDb, |
| 36 |
} |
| 37 |
|
| 38 |
impl Drop for NssLoaded { |
| 39 |
fn drop(&mut self) { |
| 40 |
if matches!(self, Self::NoDb) { |
| 41 |
unsafe { |
| 42 |
secstatus_to_res(nss_init::NSS_Shutdown()).expect("NSS Shutdown failed"); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
lazy_static! { |
| 49 |
static ref INITIALIZED: NssLoaded = { |
| 50 |
if already_initialized() { |
| 51 |
return NssLoaded::External; |
| 52 |
} |
| 53 |
|
| 54 |
secstatus_to_res(unsafe { nss_init::NSS_NoDB_Init(null()) }).expect("NSS_NoDB_Init failed"); |
| 55 |
|
| 56 |
NssLoaded::NoDb |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
fn already_initialized() -> bool { |
| 61 |
unsafe { nss_init::NSS_IsInitialized() != 0 } |
| 62 |
} |
| 63 |
|
| 64 |
/// Initialize NSS. This only executes the initialization routines once. |
| 65 |
pub fn init() { |
| 66 |
lazy_static::initialize(&INITIALIZED); |
| 67 |
} |