Skip to content

Commit

Permalink
Add certificate functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
obelisk committed Mar 18, 2021
1 parent 36baee6 commit ac34a18
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sshcerts"
version = "0.3.13"
version = "0.3.14"
authors = ["Mitchell Grenier <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
14 changes: 14 additions & 0 deletions src/yubikey/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ fn subject(yk: &mut YubiKey, slot: SlotId) -> Result<String, Error> {
}
}

/// Fetch the certificate from a given Yubikey slot. If there is not one, this
/// will fail
pub fn fetch_certificate(slot: SlotId) -> Result<Vec<u8>, Error> {
let mut yk = match YubiKey::open() {
Ok(yk) => yk,
Err(e) => return Err(Error::InternalYubiKeyError(e)),
};

match yubikey_piv::certificate::Certificate::read(&mut yk, slot) {
Ok(cert) => {Ok(cert.as_ref().to_vec())},
Err(e) => Err(Error::InternalYubiKeyError(e)),
}
}

/// Fetch a public key from the provided slot. If there is not exactly one
/// Yubikey this will fail.
pub fn fetch_pubkey(slot: SlotId) -> Result<PublicKeyInfo, Error> {
Expand Down
10 changes: 9 additions & 1 deletion src/yubikey/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ mod management;
pub use yubikey_piv::key::{AlgorithmId, RetiredSlotId, SlotId};

pub use management::Error;
pub use management::{configured, fetch_attestation, fetch_pubkey, fetch_subject, provision, sign_data};
pub use management::{
configured,
fetch_attestation,
fetch_certificate,
fetch_pubkey,
fetch_subject,
provision,
sign_data,
};
16 changes: 15 additions & 1 deletion src/yubikey/ssh.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use yubikey_piv::key::{AlgorithmId, SlotId};
use yubikey_piv::certificate::PublicKeyInfo;
use yubikey_piv::certificate::{Certificate, PublicKeyInfo};

use crate::yubikey::management::{fetch_pubkey, sign_data};

Expand Down Expand Up @@ -51,6 +51,20 @@ pub fn convert_to_ssh_pubkey(pki: &PublicKeyInfo) -> Option<PublicKey> {
}
}

/// This function is used to convert a der encoded certificate to the internal
/// PublicKey type.
pub fn convert_x509_to_ssh_pubkey(certificate: &[u8]) -> Option<PublicKey> {
let certificate = match Certificate::from_bytes(certificate.to_vec()) {
Ok(c) => c,
Err(e) => {
error!("Parsing Error: {:?}", e);
return None
}
};
convert_to_ssh_pubkey(certificate.subject_pki())
}


/// Pull the public key from the YubiKey and wrap it in a sshcerts
/// PublicKey object.
pub fn ssh_cert_fetch_pubkey(slot: SlotId) -> Option<PublicKey> {
Expand Down

0 comments on commit ac34a18

Please sign in to comment.