-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor x509 parsing code into module separate from yubikey (#7)
- Loading branch information
Showing
7 changed files
with
127 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "sshcerts" | ||
version = "0.7.0" | ||
version = "0.8.0" | ||
authors = ["Mitchell Grenier <[email protected]>"] | ||
edition = "2018" | ||
license-file = "LICENSE" | ||
|
@@ -13,11 +13,12 @@ categories = ["authentication"] | |
[features] | ||
default = ["rsa-signing"] | ||
|
||
all = ["encrypted-keys", "rsa-signing", "yubikey_support"] | ||
all = ["encrypted-keys", "rsa-signing", "x509-support", "yubikey-support"] | ||
|
||
yubikey_support = ["der-parser", "log", "rcgen", "x509", "x509-parser", "yubikey"] | ||
rsa-signing = ["simple_asn1", "num-bigint"] | ||
encrypted-keys = ["aes", "bcrypt-pbkdf", "ctr"] | ||
rsa-signing = ["simple_asn1", "num-bigint"] | ||
x509-support = ["der-parser", "x509", "x509-parser"] | ||
yubikey-support = ["der-parser", "log", "rcgen", "x509", "x509-parser", "yubikey"] | ||
|
||
[dependencies] | ||
base64 = "0.13" | ||
|
@@ -51,15 +52,15 @@ criterion = "0.3" | |
[[bench]] | ||
name = "certs_per_second" | ||
harness = false | ||
required-features = ["yubikey_support"] | ||
required-features = ["yubikey-support"] | ||
|
||
[[example]] | ||
name = "yk-fingerprint" | ||
required-features = ["yubikey_support"] | ||
required-features = ["yubikey-support"] | ||
|
||
[[example]] | ||
name = "yk-provision" | ||
required-features = ["yubikey_support"] | ||
required-features = ["yubikey-support"] | ||
|
||
[[example]] | ||
name = "ssh-pkey-info" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use x509_parser::prelude::FromDer; | ||
|
||
use crate::error::Error; | ||
use crate::ssh::{ | ||
Curve, | ||
EcdsaPublicKey, | ||
KeyType, | ||
PublicKey, | ||
PublicKeyKind, | ||
}; | ||
|
||
const OID_RSA_ENCRYPTION: &str = "1.2.840.113549.1.1.1"; | ||
const OID_EC_PUBLIC_KEY: &str = "1.2.840.10045.2.1"; | ||
const OID_NIST_P256: &str = "1.2.840.10045.3.1.7"; | ||
const OID_NIST_P384: &str = "1.3.132.0.34"; | ||
|
||
/// This function is used to extract an SSH public key from an x509 | ||
/// certificate signing request | ||
pub fn extract_ssh_pubkey_from_x509_certificate(cert: &[u8]) -> Result<PublicKey, Error> { | ||
let parsed_cert = match x509_parser::parse_x509_certificate(&cert) { | ||
Ok((_, c)) => c, | ||
Err(_) => return Err(Error::ParsingError) | ||
}; | ||
let pki = &parsed_cert.tbs_certificate.subject_pki; | ||
convert_x509_pki_to_pubkey(pki) | ||
} | ||
|
||
/// This function is used to extract an SSH public key from an x509 | ||
/// certificate signing request | ||
pub fn extract_ssh_pubkey_from_x509_csr(csr: &[u8]) -> Result<PublicKey, Error> { | ||
let parsed_csr = match x509_parser::certification_request::X509CertificationRequest::from_der(&csr) { | ||
Ok((_, csr)) => csr, | ||
Err(_) => return Err(Error::ParsingError) | ||
}; | ||
let pki = &parsed_csr.certification_request_info.subject_pki; | ||
convert_x509_pki_to_pubkey(pki) | ||
} | ||
|
||
fn convert_x509_pki_to_pubkey(pki: &x509_parser::x509::SubjectPublicKeyInfo<'_>) -> Result<PublicKey, Error> { | ||
return match pki.algorithm.algorithm.to_string().as_str() { | ||
OID_RSA_ENCRYPTION => { | ||
Err(Error::Unsupported) | ||
}, | ||
OID_EC_PUBLIC_KEY => { | ||
let key_bytes = &pki.subject_public_key.data; | ||
let algorithm_parameters = pki | ||
.algorithm | ||
.parameters | ||
.as_ref() | ||
.ok_or(Error::ParsingError)?; | ||
|
||
let curve_oid = algorithm_parameters.as_oid_val().map_err(|_| Error::ParsingError)?; | ||
|
||
match curve_oid.to_string().as_str() { | ||
OID_NIST_P256 => { | ||
let key_type = KeyType::from_name("ecdsa-sha2-nistp256").unwrap(); | ||
let curve = Curve::from_identifier("nistp256").unwrap(); | ||
let kind = EcdsaPublicKey { | ||
curve, | ||
key: key_bytes.to_vec(), | ||
}; | ||
|
||
Ok(PublicKey { | ||
key_type, | ||
kind: PublicKeyKind::Ecdsa(kind), | ||
comment: None, | ||
}) | ||
}, | ||
OID_NIST_P384 => { | ||
let key_type = KeyType::from_name("ecdsa-sha2-nistp384").unwrap(); | ||
let curve = Curve::from_identifier("nistp384").unwrap(); | ||
let kind = EcdsaPublicKey { | ||
curve, | ||
key: key_bytes.to_vec(), | ||
}; | ||
|
||
Ok(PublicKey { | ||
key_type, | ||
kind: PublicKeyKind::Ecdsa(kind), | ||
comment: None, | ||
}) | ||
} | ||
_ => Err(Error::KeyTypeMismatch), | ||
} | ||
} | ||
_ => Err(Error::ParsingError), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters