|
@@ -968,7 +968,8 @@ impl Default for SymKeyKind {
|
|
|
|
|
|
#[repr(u32)]
|
|
|
#[derive(Debug, Display, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
-pub enum KeyLen {
|
|
|
+pub enum BitLen {
|
|
|
+ Bits128 = 16,
|
|
|
Bits256 = 32,
|
|
|
Bits512 = 64,
|
|
|
Bits2048 = 256,
|
|
@@ -976,13 +977,14 @@ pub enum KeyLen {
|
|
|
Bits4096 = 512,
|
|
|
}
|
|
|
|
|
|
-impl KeyLen {
|
|
|
+impl BitLen {
|
|
|
const fn bits(self) -> u32 {
|
|
|
8 * self as u32
|
|
|
}
|
|
|
|
|
|
fn try_from_u32(value: u32) -> Result<Self> {
|
|
|
match value {
|
|
|
+ 16 => Ok(Self::Bits128),
|
|
|
32 => Ok(Self::Bits256),
|
|
|
64 => Ok(Self::Bits512),
|
|
|
256 => Ok(Self::Bits2048),
|
|
@@ -993,7 +995,7 @@ impl KeyLen {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl TryFrom<u32> for KeyLen {
|
|
|
+impl TryFrom<u32> for BitLen {
|
|
|
type Error = crate::Error;
|
|
|
fn try_from(value: u32) -> std::result::Result<Self, Self::Error> {
|
|
|
Self::try_from_u32(value)
|
|
@@ -1012,7 +1014,7 @@ pub trait Scheme:
|
|
|
fn public_from_der(self, der: &[u8]) -> Result<PKey<Public>>;
|
|
|
fn private_from_der(self, der: &[u8]) -> Result<PKey<Private>>;
|
|
|
fn generate(self) -> Result<AsymKeyPair<Self::Kind>>;
|
|
|
- fn key_len(self) -> KeyLen;
|
|
|
+ fn key_len(self) -> BitLen;
|
|
|
|
|
|
fn message_digest(&self) -> MessageDigest {
|
|
|
self.hash_kind().into()
|
|
@@ -1066,7 +1068,7 @@ impl Scheme for Encrypt {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fn key_len(self) -> KeyLen {
|
|
|
+ fn key_len(self) -> BitLen {
|
|
|
match self {
|
|
|
Encrypt::RsaEsOaep(inner) => inner.key_len(),
|
|
|
}
|
|
@@ -1075,12 +1077,12 @@ impl Scheme for Encrypt {
|
|
|
|
|
|
impl Encrypt {
|
|
|
pub const RSA_OAEP_2048_SHA_256: Encrypt = Encrypt::RsaEsOaep(RsaEsOaep {
|
|
|
- key_len: KeyLen::Bits2048,
|
|
|
+ key_len: BitLen::Bits2048,
|
|
|
hash_kind: HashKind::Sha2_256,
|
|
|
});
|
|
|
|
|
|
pub const RSA_OAEP_3072_SHA_256: Encrypt = Encrypt::RsaEsOaep(RsaEsOaep {
|
|
|
- key_len: KeyLen::Bits3072,
|
|
|
+ key_len: BitLen::Bits3072,
|
|
|
hash_kind: HashKind::Sha2_256,
|
|
|
});
|
|
|
}
|
|
@@ -1133,23 +1135,23 @@ impl Scheme for Sign {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fn key_len(self) -> KeyLen {
|
|
|
+ fn key_len(self) -> BitLen {
|
|
|
self.key_len_const()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
impl Sign {
|
|
|
pub const RSA_PSS_2048_SHA_256: Sign = Sign::RsaSsaPss(RsaSsaPss {
|
|
|
- key_bits: KeyLen::Bits2048,
|
|
|
+ key_bits: BitLen::Bits2048,
|
|
|
hash_kind: HashKind::Sha2_256,
|
|
|
});
|
|
|
|
|
|
pub const RSA_PSS_3072_SHA_256: Sign = Sign::RsaSsaPss(RsaSsaPss {
|
|
|
- key_bits: KeyLen::Bits3072,
|
|
|
+ key_bits: BitLen::Bits3072,
|
|
|
hash_kind: HashKind::Sha2_256,
|
|
|
});
|
|
|
|
|
|
- const fn key_len_const(self) -> KeyLen {
|
|
|
+ const fn key_len_const(self) -> BitLen {
|
|
|
match self {
|
|
|
Sign::RsaSsaPss(inner) => inner.key_bits,
|
|
|
}
|
|
@@ -1175,7 +1177,7 @@ impl Rsa {
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Copy)]
|
|
|
pub struct RsaEsOaep {
|
|
|
- key_len: KeyLen,
|
|
|
+ key_len: BitLen,
|
|
|
hash_kind: HashKind,
|
|
|
}
|
|
|
|
|
@@ -1206,7 +1208,7 @@ impl Scheme for RsaEsOaep {
|
|
|
Rsa::generate(self.into())
|
|
|
}
|
|
|
|
|
|
- fn key_len(self) -> KeyLen {
|
|
|
+ fn key_len(self) -> BitLen {
|
|
|
self.key_len
|
|
|
}
|
|
|
}
|
|
@@ -1219,7 +1221,7 @@ impl From<RsaEsOaep> for Encrypt {
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Copy)]
|
|
|
pub struct RsaSsaPss {
|
|
|
- key_bits: KeyLen,
|
|
|
+ key_bits: BitLen,
|
|
|
hash_kind: HashKind,
|
|
|
}
|
|
|
|
|
@@ -1250,7 +1252,7 @@ impl Scheme for RsaSsaPss {
|
|
|
Rsa::generate(self.into())
|
|
|
}
|
|
|
|
|
|
- fn key_len(self) -> KeyLen {
|
|
|
+ fn key_len(self) -> BitLen {
|
|
|
self.key_bits
|
|
|
}
|
|
|
}
|
|
@@ -1454,6 +1456,10 @@ impl Signer for AsymKey<Private, Sign> {
|
|
|
signer.sign(signature.as_mut_slice())?;
|
|
|
Ok(signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ self.scheme
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl Verifier for AsymKey<Public, Sign> {
|
|
@@ -1477,6 +1483,10 @@ impl Verifier for AsymKey<Public, Sign> {
|
|
|
Err(bterr!(Error::InvalidSignature))
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ self.scheme
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
@@ -1528,6 +1538,9 @@ impl Signer for AsymKeyPair<Sign> {
|
|
|
fn sign<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I) -> Result<Signature> {
|
|
|
self.private.sign(parts)
|
|
|
}
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ self.private.kind()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl Verifier for AsymKeyPair<Sign> {
|
|
@@ -1540,27 +1553,31 @@ impl Verifier for AsymKeyPair<Sign> {
|
|
|
fn verify<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I, signature: &[u8]) -> Result<()> {
|
|
|
self.public.verify(parts, signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ self.public.kind()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
-pub struct PublicCreds {
|
|
|
+pub struct ConcretePub {
|
|
|
sign: AsymKeyPub<Sign>,
|
|
|
enc: AsymKeyPub<Encrypt>,
|
|
|
}
|
|
|
|
|
|
-impl Principaled for PublicCreds {
|
|
|
+impl Principaled for ConcretePub {
|
|
|
fn principal_of_kind(&self, kind: HashKind) -> Principal {
|
|
|
self.sign.principal_of_kind(kind)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Encrypter for PublicCreds {
|
|
|
+impl Encrypter for ConcretePub {
|
|
|
fn encrypt(&self, slice: &[u8]) -> Result<Vec<u8>> {
|
|
|
self.enc.encrypt(slice)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Verifier for PublicCreds {
|
|
|
+impl Verifier for ConcretePub {
|
|
|
type Op<'v> = OsslVerifyOp<'v>;
|
|
|
|
|
|
fn init_verify(&self) -> Result<Self::Op<'_>> {
|
|
@@ -1570,15 +1587,19 @@ impl Verifier for PublicCreds {
|
|
|
fn verify<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I, signature: &[u8]) -> Result<()> {
|
|
|
self.sign.verify(parts, signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ self.sign.kind()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-impl CredsPub for PublicCreds {
|
|
|
+impl CredsPub for ConcretePub {
|
|
|
fn public_sign(&self) -> &AsymKey<Public, Sign> {
|
|
|
&self.sign
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl PartialEq for PublicCreds {
|
|
|
+impl PartialEq for ConcretePub {
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
self.principal() == other.principal()
|
|
|
}
|
|
@@ -1633,6 +1654,10 @@ impl Verifier for ConcreteCreds {
|
|
|
fn verify<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I, signature: &[u8]) -> Result<()> {
|
|
|
self.sign.verify(parts, signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ Verifier::kind(&self.sign)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl Encrypter for ConcreteCreds {
|
|
@@ -1663,6 +1688,10 @@ impl Signer for ConcreteCreds {
|
|
|
fn sign<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I) -> Result<Signature> {
|
|
|
self.sign.sign(parts)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ Signer::kind(&self.sign)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl Decrypter for ConcreteCreds {
|
|
@@ -1677,8 +1706,6 @@ impl CredsPriv for ConcreteCreds {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Creds for ConcreteCreds {}
|
|
|
-
|
|
|
pub trait Encrypter {
|
|
|
fn encrypt(&self, slice: &[u8]) -> Result<Vec<u8>>;
|
|
|
}
|
|
@@ -1825,6 +1852,8 @@ pub trait Signer {
|
|
|
write_to(value, &mut *buf)?;
|
|
|
self.sign(std::iter::once(buf.as_slice()))
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign;
|
|
|
}
|
|
|
|
|
|
impl<T: Signer> Signer for &T {
|
|
@@ -1837,6 +1866,10 @@ impl<T: Signer> Signer for &T {
|
|
|
fn sign<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I) -> Result<Signature> {
|
|
|
(*self).sign(parts)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ (*self).kind()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub trait VerifyOp: Sized {
|
|
@@ -1943,6 +1976,8 @@ pub trait Verifier {
|
|
|
let data = to_vec(value)?;
|
|
|
self.verify(std::iter::once(data.as_slice()), signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign;
|
|
|
}
|
|
|
|
|
|
impl<T: Verifier> Verifier for &T {
|
|
@@ -1955,12 +1990,20 @@ impl<T: Verifier> Verifier for &T {
|
|
|
fn verify<'a, I: Iterator<Item = &'a [u8]>>(&self, parts: I, signature: &[u8]) -> Result<()> {
|
|
|
(*self).verify(parts, signature)
|
|
|
}
|
|
|
+
|
|
|
+ fn kind(&self) -> Sign {
|
|
|
+ (*self).kind()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// Trait for types which can be used as public credentials.
|
|
|
pub trait CredsPub: Verifier + Encrypter + Principaled {
|
|
|
/// Returns a reference to the public signing key which can be used to verify signatures.
|
|
|
fn public_sign(&self) -> &AsymKey<Public, Sign>;
|
|
|
+
|
|
|
+ fn sign_kind(&self) -> Sign {
|
|
|
+ Verifier::kind(self)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl<T: CredsPub> CredsPub for &T {
|
|
@@ -1974,6 +2017,10 @@ pub trait CredsPriv: Decrypter + Signer {
|
|
|
/// Returns a reference to the writecap associated with these credentials, if one has been
|
|
|
/// issued.
|
|
|
fn writecap(&self) -> Option<&Writecap>;
|
|
|
+
|
|
|
+ fn sign_kind(&self) -> Sign {
|
|
|
+ Signer::kind(self)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl<T: CredsPriv> CredsPriv for &T {
|
|
@@ -2004,19 +2051,18 @@ pub trait Creds: CredsPriv + CredsPub + Clone {
|
|
|
next: self.writecap().map(|e| Box::new(e.to_owned())),
|
|
|
})
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-impl<C: Creds> Creds for &C {
|
|
|
- fn issue_writecap(
|
|
|
- &self,
|
|
|
- issued_to: Principal,
|
|
|
- path_components: Vec<String>,
|
|
|
- expires: Epoch,
|
|
|
- ) -> Result<Writecap> {
|
|
|
- (*self).issue_writecap(issued_to, path_components, expires)
|
|
|
+ fn pub_sign_kind(&self) -> Sign {
|
|
|
+ CredsPub::sign_kind(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ fn priv_sign_kind(&self) -> Sign {
|
|
|
+ CredsPriv::sign_kind(self)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+impl<C: CredsPriv + CredsPub + Clone> Creds for C {}
|
|
|
+
|
|
|
/// A trait for types which store credentials.
|
|
|
pub trait CredStore {
|
|
|
type CredHandle: Creds;
|