mirror of https://github.com/breard-r/acmed.git
Browse Source
Refactor the JWS signature algorithm management
Refactor the JWS signature algorithm management
Being tied with the key type, the signature algorithm should therefore be at the same place than the key type, hence `acme_common::crypto`. This reorganization will allow to specify the account key type as well as the signature algorithm in the configuration.pull/39/head
Rodolphe Breard
4 years ago
8 changed files with 120 additions and 128 deletions
-
2acme_common/src/crypto.rs
-
60acme_common/src/crypto/jws_signature_algorithm.rs
-
30acme_common/src/crypto/key_type.rs
-
17acme_common/src/crypto/openssl_keys.rs
-
9acmed/src/acme_proto/account.rs
-
24acmed/src/jws.rs
-
104acmed/src/jws/algorithms.rs
-
2acmed/src/main.rs
@ -0,0 +1,60 @@ |
|||||
|
use crate::error::Error;
|
||||
|
use std::fmt;
|
||||
|
use std::str::FromStr;
|
||||
|
|
||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
|
pub enum JwsSignatureAlgorithm {
|
||||
|
Rs256,
|
||||
|
Es256,
|
||||
|
Es384,
|
||||
|
Ed25519,
|
||||
|
}
|
||||
|
|
||||
|
impl FromStr for JwsSignatureAlgorithm {
|
||||
|
type Err = Error;
|
||||
|
|
||||
|
fn from_str(s: &str) -> Result<Self, Error> {
|
||||
|
match s.to_lowercase().as_str() {
|
||||
|
"rs256" => Ok(JwsSignatureAlgorithm::Rs256),
|
||||
|
"es256" => Ok(JwsSignatureAlgorithm::Es256),
|
||||
|
"es384" => Ok(JwsSignatureAlgorithm::Es384),
|
||||
|
"ed25519" => Ok(JwsSignatureAlgorithm::Ed25519),
|
||||
|
_ => Err(format!("{}: unknown algorithm.", s).into()),
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl fmt::Display for JwsSignatureAlgorithm {
|
||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
let s = match self {
|
||||
|
JwsSignatureAlgorithm::Rs256 => "RS256",
|
||||
|
JwsSignatureAlgorithm::Es256 => "ES256",
|
||||
|
JwsSignatureAlgorithm::Es384 => "ES384",
|
||||
|
JwsSignatureAlgorithm::Ed25519 => "Ed25519",
|
||||
|
};
|
||||
|
write!(f, "{}", s)
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
#[cfg(test)]
|
||||
|
mod tests {
|
||||
|
use super::JwsSignatureAlgorithm;
|
||||
|
use std::str::FromStr;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test_es256_from_str() {
|
||||
|
let variants = ["ES256", "Es256", "es256"];
|
||||
|
for v in variants.iter() {
|
||||
|
let a = JwsSignatureAlgorithm::from_str(v);
|
||||
|
assert!(a.is_ok());
|
||||
|
let a = a.unwrap();
|
||||
|
assert_eq!(a, JwsSignatureAlgorithm::Es256);
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test_es256_to_str() {
|
||||
|
let a = JwsSignatureAlgorithm::Es256;
|
||||
|
assert_eq!(a.to_string().as_str(), "ES256");
|
||||
|
}
|
||||
|
}
|
@ -1,104 +0,0 @@ |
|||||
use acme_common::crypto::{gen_keypair, KeyPair, KeyType};
|
|
||||
use acme_common::error::Error;
|
|
||||
use std::fmt;
|
|
||||
use std::str::FromStr;
|
|
||||
|
|
||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||
pub enum SignatureAlgorithm {
|
|
||||
Rs256,
|
|
||||
Es256,
|
|
||||
Es384,
|
|
||||
}
|
|
||||
|
|
||||
impl fmt::Display for SignatureAlgorithm {
|
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||
let s = match self {
|
|
||||
SignatureAlgorithm::Rs256 => "RS256",
|
|
||||
SignatureAlgorithm::Es256 => "ES256",
|
|
||||
SignatureAlgorithm::Es384 => "ES384",
|
|
||||
};
|
|
||||
write!(f, "{}", s)
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
impl FromStr for SignatureAlgorithm {
|
|
||||
type Err = Error;
|
|
||||
|
|
||||
fn from_str(data: &str) -> Result<Self, Self::Err> {
|
|
||||
match data.to_lowercase().as_str() {
|
|
||||
"rs256" => Ok(SignatureAlgorithm::Rs256),
|
|
||||
"es256" => Ok(SignatureAlgorithm::Es256),
|
|
||||
"es384" => Ok(SignatureAlgorithm::Es384),
|
|
||||
_ => Err(format!("{}: unknown signature algorithm", data).into()),
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
impl SignatureAlgorithm {
|
|
||||
pub fn from_pkey(key_pair: &KeyPair) -> Result<Self, Error> {
|
|
||||
match key_pair.key_type {
|
|
||||
KeyType::Rsa2048 => Ok(SignatureAlgorithm::Rs256),
|
|
||||
KeyType::Rsa4096 => Ok(SignatureAlgorithm::Rs256),
|
|
||||
KeyType::EcdsaP256 => Ok(SignatureAlgorithm::Es256),
|
|
||||
KeyType::EcdsaP384 => Ok(SignatureAlgorithm::Es384),
|
|
||||
t => Err(format!("{}: unsupported key type", t).into()),
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
pub fn gen_key_pair(&self) -> Result<KeyPair, Error> {
|
|
||||
match self {
|
|
||||
SignatureAlgorithm::Rs256 => gen_keypair(KeyType::Rsa2048),
|
|
||||
SignatureAlgorithm::Es256 => gen_keypair(KeyType::EcdsaP256),
|
|
||||
SignatureAlgorithm::Es384 => gen_keypair(KeyType::EcdsaP384),
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
#[cfg(test)]
|
|
||||
mod tests {
|
|
||||
use super::SignatureAlgorithm;
|
|
||||
use acme_common::crypto::KeyPair;
|
|
||||
use std::str::FromStr;
|
|
||||
|
|
||||
#[test]
|
|
||||
fn test_es256_from_str() {
|
|
||||
let variants = ["ES256", "Es256", "es256"];
|
|
||||
for v in variants.iter() {
|
|
||||
let a = SignatureAlgorithm::from_str(v);
|
|
||||
assert!(a.is_ok());
|
|
||||
let a = a.unwrap();
|
|
||||
assert_eq!(a, SignatureAlgorithm::Es256);
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn test_es256_to_str() {
|
|
||||
let a = SignatureAlgorithm::Es256;
|
|
||||
assert_eq!(a.to_string().as_str(), "ES256");
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn test_eddsa_ed25519_from_str() {
|
|
||||
let variants = ["ES256", "Es256", "es256"];
|
|
||||
for v in variants.iter() {
|
|
||||
let a = SignatureAlgorithm::from_str(v);
|
|
||||
assert!(a.is_ok());
|
|
||||
let a = a.unwrap();
|
|
||||
assert_eq!(a, SignatureAlgorithm::Es256);
|
|
||||
}
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn test_from_p256() {
|
|
||||
let pem = b"-----BEGIN PRIVATE KEY-----
|
|
||||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg6To1BW8qTehGhPca
|
|
||||
0eMcW8iQU4yA02dvtKkuqfny4HChRANCAAQwxx+j3wYGzD5LSFNBTLlT7J+7rWrq
|
|
||||
4BGdR8705iwpBeOQgMpLj+9vuFutlVtmoYpJSYa9+49Hxz8aCe1AQeWt
|
|
||||
-----END PRIVATE KEY-----";
|
|
||||
let k = KeyPair::from_pem(pem).unwrap();
|
|
||||
let s = SignatureAlgorithm::from_pkey(&k);
|
|
||||
assert!(s.is_ok());
|
|
||||
let s = s.unwrap();
|
|
||||
assert_eq!(s, SignatureAlgorithm::Es256)
|
|
||||
}
|
|
||||
}
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue