cargo fmt
This commit is contained in:
parent
b9a3e3086f
commit
06bebeaae3
|
@ -18,17 +18,17 @@ mod crypto;
|
|||
pub mod errors;
|
||||
mod header;
|
||||
mod keys;
|
||||
mod pem_decoder;
|
||||
mod serialization;
|
||||
mod validation;
|
||||
mod pem_decoder;
|
||||
|
||||
pub use algorithms::Algorithm;
|
||||
pub use crypto::{sign, verify};
|
||||
pub use header::Header;
|
||||
pub use keys::Key;
|
||||
pub use pem_decoder::PemEncodedKey;
|
||||
pub use serialization::TokenData;
|
||||
pub use validation::Validation;
|
||||
pub use pem_decoder::PemEncodedKey;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::ser::Serialize;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::errors::{ErrorKind, Result};
|
||||
use crate::keys::Key;
|
||||
use crate::errors::{Result, ErrorKind};
|
||||
|
||||
extern crate pem;
|
||||
extern crate simple_asn1;
|
||||
|
||||
use simple_asn1::{OID, BigUint};
|
||||
use simple_asn1::{BigUint, OID};
|
||||
|
||||
/// Supported PEM files for EC and RSA Public and Private Keys
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -41,6 +41,7 @@ enum Classification {
|
|||
/// Documentation about these formats is at
|
||||
/// PKCS#1: https://tools.ietf.org/html/rfc8017
|
||||
/// PKCS#8: https://tools.ietf.org/html/rfc5958
|
||||
#[derive(Debug)]
|
||||
pub struct PemEncodedKey {
|
||||
content: Vec<u8>,
|
||||
asn1: Vec<simple_asn1::ASN1Block>,
|
||||
|
@ -79,8 +80,7 @@ impl PemEncodedKey {
|
|||
// "there is no such thing as a "PKCS#1 format" for elliptic curve (EC) keys"
|
||||
|
||||
// This handles PKCS#8 private keys
|
||||
"PRIVATE KEY" => {
|
||||
match classify_pem(&asn1_content) {
|
||||
"PRIVATE KEY" => match classify_pem(&asn1_content) {
|
||||
Some(Classification::EC) => Ok(PemEncodedKey {
|
||||
content: pem_contents,
|
||||
asn1: asn1_content,
|
||||
|
@ -94,12 +94,10 @@ impl PemEncodedKey {
|
|||
encoded_with: PemEncodedWith::PKCS8,
|
||||
}),
|
||||
_ => return Err(ErrorKind::InvalidKeyFormat)?,
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// This handles PKCS#8 public keys
|
||||
"PUBLIC KEY" => {
|
||||
match classify_pem(&asn1_content) {
|
||||
"PUBLIC KEY" => match classify_pem(&asn1_content) {
|
||||
Some(Classification::EC) => Ok(PemEncodedKey {
|
||||
content: pem_contents,
|
||||
asn1: asn1_content,
|
||||
|
@ -113,13 +111,12 @@ impl PemEncodedKey {
|
|||
encoded_with: PemEncodedWith::PKCS8,
|
||||
}),
|
||||
_ => return Err(ErrorKind::InvalidKeyFormat)?,
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// Unknown type
|
||||
_ => return Err(ErrorKind::InvalidKeyFormat)?,
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(_) => return Err(ErrorKind::InvalidKeyFormat)?,
|
||||
}
|
||||
}
|
||||
|
@ -131,13 +128,11 @@ impl PemEncodedKey {
|
|||
pub fn as_key(&self) -> Result<Key<'_>> {
|
||||
match self.encoded_with {
|
||||
PemEncodedWith::PKCS1 => Ok(Key::Der(self.content.as_slice())),
|
||||
PemEncodedWith::PKCS8 => {
|
||||
match self.pem_type {
|
||||
PemEncodedWith::PKCS8 => match self.pem_type {
|
||||
PemType::RSAPrivateKey => Ok(Key::Der(extract_first_bitstring(&self.asn1)?)),
|
||||
PemType::RSAPublicKey => Ok(Key::Der(extract_first_bitstring(&self.asn1)?)),
|
||||
PemType::ECPrivateKey => Ok(Key::Pkcs8(self.content.as_slice())),
|
||||
PemType::ECPublicKey => Ok(Key::Pkcs8(extract_first_bitstring(&self.asn1)?)),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -162,10 +157,10 @@ fn extract_first_bitstring(asn1: &Vec<simple_asn1::ASN1Block>) -> Result<&[u8]>
|
|||
simple_asn1::ASN1Block::OctetString(_, value) => {
|
||||
return Ok(value.as_ref());
|
||||
}
|
||||
_ => ()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
return Err(ErrorKind::InvalidEcdsaKey)?
|
||||
return Err(ErrorKind::InvalidEcdsaKey)?;
|
||||
}
|
||||
|
||||
fn classify_pem(asn1: &Vec<simple_asn1::ASN1Block>) -> Option<Classification> {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::collections::HashSet;
|
||||
use chrono::Utc;
|
||||
use serde_json::map::Map;
|
||||
use serde_json::{from_value, Value};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::algorithms::Algorithm;
|
||||
use crate::errors::{new_error, ErrorKind, Result};
|
||||
|
|
Loading…
Reference in New Issue