Commit
Author: Dirkjan Ochtman [dirkjan@ochtman.nl]
Hash: 30619ecbc91203fa8367e0e9eade2da88f11c14c
Timestamp: Wed, 14 Dec 2022 13:59:07 +0000 (1 year ago)

+24 -15 +/-1 browse
Move verify key construction into separate methods
1diff --git a/src/common/crypto.rs b/src/common/crypto.rs
2index ea6aa3d..b5e353d 100644
3--- a/src/common/crypto.rs
4+++ b/src/common/crypto.rs
5 @@ -134,21 +134,11 @@ impl VerifyingKeyType {
6 pub(crate) fn verifying_key(
7 &self,
8 bytes: &[u8],
9- ) -> Result<Box<dyn VerifyingKey + Sync + Send>> {
10- Ok(match self {
11- Self::Rsa => {
12- let inner =
13- <rsa::RsaPublicKey as rsa::pkcs8::DecodePublicKey>::from_public_key_der(bytes)
14- .or_else(|_| rsa::pkcs1::DecodeRsaPublicKey::from_pkcs1_der(bytes))
15- .map_err(|err| Error::CryptoError(err.to_string()))?;
16-
17- Box::new(RsaPublicKey { inner }) as Box<dyn VerifyingKey + Sync + Send>
18- }
19- Self::Ed25519 => Box::new(Ed25519PublicKey {
20- inner: ed25519_dalek::PublicKey::from_bytes(bytes)
21- .map_err(|err| Error::CryptoError(err.to_string()))?,
22- }),
23- })
24+ ) -> Result<Box<dyn VerifyingKey + Send + Sync>> {
25+ match self {
26+ Self::Rsa => RsaPublicKey::verifying_key_from_bytes(bytes),
27+ Self::Ed25519 => Ed25519PublicKey::verifying_key_from_bytes(bytes),
28+ }
29 }
30 }
31
32 @@ -156,6 +146,16 @@ pub(crate) struct RsaPublicKey {
33 inner: rsa::RsaPublicKey,
34 }
35
36+ impl RsaPublicKey {
37+ fn verifying_key_from_bytes(bytes: &[u8]) -> Result<Box<dyn VerifyingKey + Send + Sync>> {
38+ Ok(Box::new(RsaPublicKey {
39+ inner: <rsa::RsaPublicKey as rsa::pkcs8::DecodePublicKey>::from_public_key_der(bytes)
40+ .or_else(|_| rsa::pkcs1::DecodeRsaPublicKey::from_pkcs1_der(bytes))
41+ .map_err(|err| Error::CryptoError(err.to_string()))?,
42+ }))
43+ }
44+ }
45+
46 impl VerifyingKey for RsaPublicKey {
47 fn verify<'a>(
48 &self,
49 @@ -194,6 +194,15 @@ pub(crate) struct Ed25519PublicKey {
50 inner: ed25519_dalek::PublicKey,
51 }
52
53+ impl Ed25519PublicKey {
54+ fn verifying_key_from_bytes(bytes: &[u8]) -> Result<Box<dyn VerifyingKey + Send + Sync>> {
55+ Ok(Box::new(Ed25519PublicKey {
56+ inner: ed25519_dalek::PublicKey::from_bytes(bytes)
57+ .map_err(|err| Error::CryptoError(err.to_string()))?,
58+ }))
59+ }
60+ }
61+
62 impl VerifyingKey for Ed25519PublicKey {
63 fn verify<'a>(
64 &self,