[vault] Signing: the signing body member (release and test key sets, scope, sign-only API, append-only audit log); custos-sign draws from SysRng; the wasm build links getrandom 0.2 to the browser CSPRNG
This commit is contained in:
@@ -15,7 +15,8 @@ use std::time::Duration;
|
||||
use blake2::{Blake2b512, Digest};
|
||||
use data_encoding::BASE64;
|
||||
use ed25519_dalek::{Signer as _, SigningKey};
|
||||
use rand_core::{OsRng, RngCore};
|
||||
use rand::rand_core::UnwrapErr;
|
||||
use rand::rngs::SysRng;
|
||||
use rsa::RsaPrivateKey;
|
||||
use rsa::pkcs1v15;
|
||||
use rsa::pkcs8::{DecodePrivateKey, EncodePrivateKey, EncodePublicKey};
|
||||
@@ -103,6 +104,37 @@ impl fmt::Debug for RsaSecret {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fills `buf` from the OS CSPRNG through `rand`'s `SysRng`, as the rest of the workspace does (the browser build
|
||||
/// routes it to `crypto.getRandomValues`).
|
||||
fn fill(buf: &mut [u8]) {
|
||||
rand::Rng::fill_bytes(&mut UnwrapErr(SysRng), buf);
|
||||
}
|
||||
|
||||
/// rand_core 0.6's interface, which `rsa` 0.9 generates keys with, over [`fill`].
|
||||
struct OsRng06;
|
||||
|
||||
impl rand_core::RngCore for OsRng06 {
|
||||
fn next_u32(&mut self) -> u32 {
|
||||
let mut b = [0u8; 4];
|
||||
fill(&mut b);
|
||||
u32::from_le_bytes(b)
|
||||
}
|
||||
fn next_u64(&mut self) -> u64 {
|
||||
let mut b = [0u8; 8];
|
||||
fill(&mut b);
|
||||
u64::from_le_bytes(b)
|
||||
}
|
||||
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
||||
fill(dest);
|
||||
}
|
||||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
||||
fill(dest);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl rand_core::CryptoRng for OsRng06 {}
|
||||
|
||||
// ---- minisign ----
|
||||
|
||||
/// A new Ed25519 key with a random minisign key id.
|
||||
@@ -114,9 +146,9 @@ pub struct NewEd25519 {
|
||||
|
||||
pub fn new_ed25519() -> NewEd25519 {
|
||||
let mut seed = Zeroizing::new([0u8; 32]);
|
||||
OsRng.fill_bytes(&mut *seed);
|
||||
fill(&mut *seed);
|
||||
let mut key_id = [0u8; 8];
|
||||
OsRng.fill_bytes(&mut key_id);
|
||||
fill(&mut key_id);
|
||||
let secret = Ed25519Secret(seed);
|
||||
let public = secret.public();
|
||||
NewEd25519 { secret, public, key_id }
|
||||
@@ -257,14 +289,14 @@ pub struct NewCert {
|
||||
}
|
||||
|
||||
fn new_rsa() -> Result<(RsaPrivateKey, RsaSecret), Error> {
|
||||
let key = RsaPrivateKey::new(&mut OsRng, RSA_BITS).map_err(|_| Error::BadKey)?;
|
||||
let key = RsaPrivateKey::new(&mut OsRng06, RSA_BITS).map_err(|_| Error::BadKey)?;
|
||||
let der = key.to_pkcs8_der().map_err(|_| Error::BadKey)?.as_bytes().to_vec();
|
||||
Ok((key, RsaSecret(Zeroizing::new(der))))
|
||||
}
|
||||
|
||||
fn serial() -> Result<SerialNumber, Error> {
|
||||
let mut bytes = [0u8; 16];
|
||||
OsRng.fill_bytes(&mut bytes);
|
||||
fill(&mut bytes);
|
||||
bytes[0] &= 0x7f; // positive
|
||||
bytes[0] |= 0x01; // and without a leading zero byte
|
||||
SerialNumber::new(&bytes).map_err(|_| Error::Certificate)
|
||||
|
||||
Reference in New Issue
Block a user