|
@ -1,34 +1,18 @@ |
|
|
use super::jwk::{EdDsaEd25519Jwk, Es256Jwk, Jwk};
|
|
|
|
|
|
|
|
|
use super::jwk::{Es256Jwk, Jwk};
|
|
|
use acme_common::crypto::{gen_keypair, KeyPair, KeyType};
|
|
|
use acme_common::crypto::{gen_keypair, KeyPair, KeyType};
|
|
|
use acme_common::error::Error;
|
|
|
use acme_common::error::Error;
|
|
|
use std::fmt;
|
|
|
use std::fmt;
|
|
|
use std::str::FromStr;
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
|
|
pub enum EdDsaVariant {
|
|
|
|
|
|
Ed25519,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for EdDsaVariant {
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
|
let s = match self {
|
|
|
|
|
|
EdDsaVariant::Ed25519 => "Ed25519",
|
|
|
|
|
|
};
|
|
|
|
|
|
write!(f, "{}", s)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
pub enum SignatureAlgorithm {
|
|
|
pub enum SignatureAlgorithm {
|
|
|
Es256,
|
|
|
Es256,
|
|
|
EdDsa(EdDsaVariant),
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl fmt::Display for SignatureAlgorithm {
|
|
|
impl fmt::Display for SignatureAlgorithm {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
let s = match self {
|
|
|
let s = match self {
|
|
|
SignatureAlgorithm::Es256 => "ES256",
|
|
|
SignatureAlgorithm::Es256 => "ES256",
|
|
|
SignatureAlgorithm::EdDsa(_) => "EdDSA",
|
|
|
|
|
|
};
|
|
|
};
|
|
|
write!(f, "{}", s)
|
|
|
write!(f, "{}", s)
|
|
|
}
|
|
|
}
|
|
@ -40,7 +24,6 @@ impl FromStr for SignatureAlgorithm { |
|
|
fn from_str(data: &str) -> Result<Self, Self::Err> {
|
|
|
fn from_str(data: &str) -> Result<Self, Self::Err> {
|
|
|
match data.to_lowercase().as_str() {
|
|
|
match data.to_lowercase().as_str() {
|
|
|
"es256" => Ok(SignatureAlgorithm::Es256),
|
|
|
"es256" => Ok(SignatureAlgorithm::Es256),
|
|
|
"eddsa-ed25519" => Ok(SignatureAlgorithm::EdDsa(EdDsaVariant::Ed25519)),
|
|
|
|
|
|
_ => Err(format!("{}: unknown signature algorithm", data).into()),
|
|
|
_ => Err(format!("{}: unknown signature algorithm", data).into()),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -63,14 +46,13 @@ impl SignatureAlgorithm { |
|
|
pub fn gen_key_pair(&self) -> Result<KeyPair, Error> {
|
|
|
pub fn gen_key_pair(&self) -> Result<KeyPair, Error> {
|
|
|
match self {
|
|
|
match self {
|
|
|
SignatureAlgorithm::Es256 => gen_keypair(KeyType::EcdsaP256),
|
|
|
SignatureAlgorithm::Es256 => gen_keypair(KeyType::EcdsaP256),
|
|
|
SignatureAlgorithm::EdDsa(EdDsaVariant::Ed25519) => Err("Not implemented".into()),
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
|
mod tests {
|
|
|
use super::{EdDsaVariant, SignatureAlgorithm};
|
|
|
|
|
|
|
|
|
use super::SignatureAlgorithm;
|
|
|
use acme_common::crypto::KeyPair;
|
|
|
use acme_common::crypto::KeyPair;
|
|
|
use std::str::FromStr;
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
@ -102,12 +84,6 @@ mod tests { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_eddsa_ed25519_to_str() {
|
|
|
|
|
|
let a = SignatureAlgorithm::EdDsa(EdDsaVariant::Ed25519);
|
|
|
|
|
|
assert_eq!(a.to_string().as_str(), "EdDSA");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
#[test]
|
|
|
fn test_from_p256() {
|
|
|
fn test_from_p256() {
|
|
|
let pem = b"-----BEGIN PRIVATE KEY-----
|
|
|
let pem = b"-----BEGIN PRIVATE KEY-----
|
|
|