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