Fix Option handling

This commit is contained in:
Vincent Prouillet 2019-11-03 12:36:52 +00:00
parent 571898252f
commit b9a3e3086f
1 changed files with 34 additions and 37 deletions

View File

@ -6,6 +6,27 @@ extern crate simple_asn1;
use simple_asn1::{OID, BigUint}; use simple_asn1::{OID, BigUint};
/// Supported PEM files for EC and RSA Public and Private Keys
#[derive(Debug, PartialEq)]
enum PemType {
ECPublicKey,
ECPrivateKey,
RSAPublicKey,
RSAPrivateKey,
}
#[derive(Debug, PartialEq)]
enum PemEncodedWith {
PKCS1,
PKCS8,
}
#[derive(Debug, PartialEq)]
enum Classification {
EC,
RSA,
}
/// The return type of a successful PEM encoded key with `decode_pem` /// The return type of a successful PEM encoded key with `decode_pem`
/// ///
/// This struct gives a way to parse a string to a key for use in jsonwebtoken. /// This struct gives a way to parse a string to a key for use in jsonwebtoken.
@ -60,13 +81,13 @@ impl PemEncodedKey {
// 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) {
Option::Some(Classification::EC) => Ok(PemEncodedKey { Some(Classification::EC) => Ok(PemEncodedKey {
content: pem_contents, content: pem_contents,
asn1: asn1_content, asn1: asn1_content,
pem_type: PemType::ECPrivateKey, pem_type: PemType::ECPrivateKey,
encoded_with: PemEncodedWith::PKCS8, encoded_with: PemEncodedWith::PKCS8,
}), }),
Option::Some(Classification::RSA) => Ok(PemEncodedKey { Some(Classification::RSA) => Ok(PemEncodedKey {
content: pem_contents, content: pem_contents,
asn1: asn1_content, asn1: asn1_content,
pem_type: PemType::RSAPrivateKey, pem_type: PemType::RSAPrivateKey,
@ -79,13 +100,13 @@ impl PemEncodedKey {
// 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) {
Option::Some(Classification::EC) => Ok(PemEncodedKey { Some(Classification::EC) => Ok(PemEncodedKey {
content: pem_contents, content: pem_contents,
asn1: asn1_content, asn1: asn1_content,
pem_type: PemType::ECPublicKey, pem_type: PemType::ECPublicKey,
encoded_with: PemEncodedWith::PKCS8, encoded_with: PemEncodedWith::PKCS8,
}), }),
Option::Some(Classification::RSA) => Ok(PemEncodedKey { Some(Classification::RSA) => Ok(PemEncodedKey {
content: pem_contents, content: pem_contents,
asn1: asn1_content, asn1: asn1_content,
pem_type: PemType::RSAPublicKey, pem_type: PemType::RSAPublicKey,
@ -122,30 +143,6 @@ impl PemEncodedKey {
} }
} }
#[derive(Debug)]
#[derive(PartialEq)]
/// Supported PEM files for EC and RSA Public and Private Keys
enum PemType {
ECPublicKey,
ECPrivateKey,
RSAPublicKey,
RSAPrivateKey,
}
#[derive(Debug)]
#[derive(PartialEq)]
enum PemEncodedWith {
PKCS1,
PKCS8,
}
#[derive(Debug)]
#[derive(PartialEq)]
enum Classification {
EC,
RSA,
}
// This really just finds and returns the first bitstring or octet string // This really just finds and returns the first bitstring or octet string
// Which is the x coordinate for EC public keys // Which is the x coordinate for EC public keys
// And the DER contents of an RSA key // And the DER contents of an RSA key
@ -186,13 +183,13 @@ fn classify_pem(asn1: &Vec<simple_asn1::ASN1Block>) -> Option<Classification> {
} }
simple_asn1::ASN1Block::ObjectIdentifier(_, oid) => { simple_asn1::ASN1Block::ObjectIdentifier(_, oid) => {
if oid == ec_public_key_oid { if oid == ec_public_key_oid {
return Option::Some(Classification::EC); return Some(Classification::EC);
} else if oid == rsa_public_key_oid { } else if oid == rsa_public_key_oid {
return Option::Some(Classification::RSA); return Some(Classification::RSA);
} }
} }
_ => {} _ => {}
} }
} }
return Option::default(); None
} }