nss/pk11/
slot.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::{
6    error::*,
7    pk11::types::Slot,
8    util::{assert_nss_initialized, map_nss_secstatus, ScopedPtr},
9};
10
11pub fn generate_random(data: &mut [u8]) -> Result<()> {
12    // `NSS_Init` will initialize the RNG with data from `/dev/urandom`.
13    assert_nss_initialized();
14    let len = i32::try_from(data.len())?;
15    map_nss_secstatus(|| unsafe { nss_sys::PK11_GenerateRandom(data.as_mut_ptr(), len) })?;
16    Ok(())
17}
18
19/// Safe wrapper around `PK11_GetInternalSlot` that
20/// de-allocates memory when the slot goes out of
21/// scope.
22pub(crate) fn get_internal_slot() -> Result<Slot> {
23    unsafe { Slot::from_ptr(nss_sys::PK11_GetInternalSlot()) }
24}
25
26/// Safe wrapper around `PK11_GetInternalKeySlot` that
27/// de-allocates memory when the slot goes out of
28/// scope.
29#[cfg(feature = "keydb")]
30pub(crate) fn get_internal_key_slot() -> Result<Slot> {
31    unsafe { Slot::from_ptr(nss_sys::PK11_GetInternalKeySlot()) }
32}