-
Notifications
You must be signed in to change notification settings - Fork 5
Move To Yubikey 8 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
obelisk
wants to merge
6
commits into
main
Choose a base branch
from
yk8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2,15 +2,23 @@ use crate::PublicKey; | |
|
||
use ring::digest; | ||
|
||
use yubikey::certificate::{Certificate, PublicKeyInfo}; | ||
use yubikey::certificate::Certificate; | ||
use yubikey::piv::{attest, sign_data as yk_sign_data, AlgorithmId, SlotId}; | ||
use yubikey::{MgmKey, YubiKey}; | ||
use yubikey::{PinPolicy, TouchPolicy}; | ||
|
||
use x509::RelativeDistinguishedName; | ||
|
||
use super::{Error, Result}; | ||
|
||
use x509_cert::{ | ||
der::{oid::ObjectIdentifier, Encode}, | ||
name::Name, | ||
serial_number::SerialNumber, | ||
time::Validity, | ||
}; | ||
|
||
use std::str::FromStr; | ||
use yubikey::certificate::yubikey_signer; | ||
|
||
#[derive(Debug)] | ||
/// A struct that allows the generation of CSRs via the rcgen library. This is | ||
/// only used when calling the `generate_csr` function. | ||
|
@@ -21,16 +29,44 @@ pub struct CSRSigner { | |
algorithm: AlgorithmId, | ||
} | ||
|
||
const NISTP256_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7"); | ||
const SECP384_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.132.0.34"); | ||
|
||
impl CSRSigner { | ||
/// Create a new certificate signer based on a Yubikey serial | ||
/// and slot | ||
pub fn new(serial: u32, slot: SlotId) -> Self { | ||
let mut yk = super::Yubikey::open(serial).unwrap(); | ||
let pki = yk.configured(&slot).unwrap(); | ||
let (public_key, algorithm) = match pki { | ||
PublicKeyInfo::Rsa { pubkey: _, .. } => panic!("RSA keys not supported"), | ||
PublicKeyInfo::EcP256(pubkey) => (pubkey.as_bytes().to_vec(), AlgorithmId::EccP256), | ||
PublicKeyInfo::EcP384(pubkey) => (pubkey.as_bytes().to_vec(), AlgorithmId::EccP384), | ||
let cert = yk.configured(&slot).unwrap(); | ||
let pki = cert.subject_pki(); | ||
let oid_alg = pki.algorithm.parameters_oid().unwrap(); | ||
|
||
let (public_key, algorithm) = match oid_alg { | ||
NISTP256_OID => { | ||
// This is the OID for ECDSA with SHA256 | ||
( | ||
pki.subject_public_key | ||
.raw_bytes() | ||
.to_vec() | ||
.to_der() | ||
.unwrap(), | ||
AlgorithmId::EccP256, | ||
) | ||
} | ||
SECP384_OID => { | ||
// This is the OID for ECDSA with SHA384 | ||
( | ||
pki.subject_public_key | ||
.raw_bytes() | ||
.to_vec() | ||
.to_der() | ||
.unwrap(), | ||
AlgorithmId::EccP384, | ||
) | ||
} | ||
_ => { | ||
panic!("Unsupported algorithm"); | ||
} | ||
}; | ||
|
||
Self { | ||
|
@@ -54,7 +90,8 @@ impl rcgen::RemoteKeyPair for CSRSigner { | |
return Err(rcgen::RcgenError::RemoteKeyError); | ||
}; | ||
|
||
yk.sign_data(message, self.algorithm, &self.slot).map_err(|_| rcgen::RcgenError::RemoteKeyError) | ||
yk.sign_data(message, self.algorithm, &self.slot) | ||
.map_err(|_| rcgen::RcgenError::RemoteKeyError) | ||
} | ||
|
||
fn algorithm(&self) -> &'static rcgen::SignatureAlgorithm { | ||
|
@@ -108,9 +145,9 @@ impl super::Yubikey { | |
} | ||
|
||
/// Check to see that a provided Yubikey and slot is configured for signing | ||
pub fn configured(&mut self, slot: &SlotId) -> Result<PublicKeyInfo> { | ||
pub fn configured(&mut self, slot: &SlotId) -> Result<Certificate> { | ||
let cert = Certificate::read(&mut self.yk, *slot)?; | ||
Ok(cert.subject_pki().clone()) | ||
Ok(cert) | ||
} | ||
|
||
/// Check to see that a provided Yubikey and slot is configured for signing | ||
|
@@ -122,7 +159,9 @@ impl super::Yubikey { | |
/// Fetch the certificate from a given Yubikey slot. | ||
pub fn fetch_certificate(&mut self, slot: &SlotId) -> Result<Vec<u8>> { | ||
let cert = Certificate::read(&mut self.yk, *slot)?; | ||
Ok(cert.as_ref().to_vec()) | ||
Ok(cert.cert.to_der().map_err(|e| { | ||
Error::InternalYubiKeyError(format!("Failed to encode certificate: {}", e)) | ||
})?) | ||
} | ||
|
||
/// Write the certificate from a given Yubikey slot. | ||
|
@@ -142,45 +181,57 @@ impl super::Yubikey { | |
/// Generate CSR for slot | ||
pub fn generate_csr(&mut self, slot: &SlotId, common_name: &str) -> Result<Vec<u8>> { | ||
let mut params = rcgen::CertificateParams::new(vec![]); | ||
params.alg = match self.configured(slot)? { | ||
PublicKeyInfo::EcP256(_) => &rcgen::PKCS_ECDSA_P256_SHA256, | ||
PublicKeyInfo::EcP384(_) => &rcgen::PKCS_ECDSA_P384_SHA384, | ||
let cert = self.configured(&slot).unwrap(); | ||
let pki = cert.subject_pki(); | ||
let oid_alg = pki | ||
.algorithm | ||
.parameters_oid() | ||
.map_err(|_| Error::Unsupported)?; | ||
|
||
params.alg = match oid_alg { | ||
NISTP256_OID => &rcgen::PKCS_ECDSA_P256_SHA256, | ||
SECP384_OID => &rcgen::PKCS_ECDSA_P384_SHA384, | ||
_ => return Err(Error::Unsupported), | ||
}; | ||
params | ||
.distinguished_name | ||
.push(rcgen::DnType::CommonName, common_name.to_string()); | ||
|
||
let csr_signer = CSRSigner::new(self.yk.serial().into(), *slot); | ||
params.key_pair = Some(rcgen::KeyPair::from_remote(Box::new(csr_signer)).map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?); | ||
params.key_pair = Some( | ||
rcgen::KeyPair::from_remote(Box::new(csr_signer)) | ||
.map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?, | ||
); | ||
|
||
let csr = rcgen::Certificate::from_params(params).map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?; | ||
let csr = csr.serialize_request_der().map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?; | ||
let csr = rcgen::Certificate::from_params(params) | ||
.map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?; | ||
let csr = csr | ||
.serialize_request_der() | ||
.map_err(|e| Error::InternalYubiKeyError(format!("{}", e)))?; | ||
|
||
Ok(csr) | ||
} | ||
|
||
/// Provisions the YubiKey with a new certificate generated on the device. | ||
/// Only keys that are generated this way can use the attestation functionality. | ||
pub fn provision( | ||
pub fn provision<KT: yubikey_signer::KeyType>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we hide this away and just provide two non-generic functions for p256 and p384 |
||
&mut self, | ||
slot: &SlotId, | ||
common_name: &str, | ||
alg: AlgorithmId, | ||
touch_policy: TouchPolicy, | ||
pin_policy: PinPolicy, | ||
) -> Result<PublicKey> { | ||
let key_info = yubikey::piv::generate(&mut self.yk, *slot, alg, pin_policy, touch_policy)?; | ||
let extensions: &[x509::Extension<'_, &[u64]>] = &[]; | ||
let key_info = | ||
yubikey::piv::generate(&mut self.yk, *slot, KT::ALGORITHM, pin_policy, touch_policy)?; | ||
// Generate a self-signed certificate for the new key. | ||
Certificate::generate_self_signed( | ||
Certificate::generate_self_signed::<_, KT>( | ||
&mut self.yk, | ||
*slot, | ||
[0u8; 20], | ||
None, | ||
&[RelativeDistinguishedName::common_name(common_name)], | ||
SerialNumber::new(&[0; 20]).unwrap(), | ||
Validity::from_now(std::time::Duration::new(3600 * 24 * 3650, 0)).unwrap(), | ||
Name::from_str(&format!("CN={}", common_name)).unwrap(), | ||
key_info, | ||
extensions, | ||
|_builder| Ok(()), | ||
)?; | ||
|
||
self.ssh_cert_fetch_pubkey(slot) | ||
|
@@ -191,11 +242,17 @@ impl super::Yubikey { | |
/// If the requested algorithm doesn't match the key in the slot (or the slot | ||
/// is empty) this will error. | ||
pub fn sign_data(&mut self, data: &[u8], alg: AlgorithmId, slot: &SlotId) -> Result<Vec<u8>> { | ||
let (slot_alg, hash_alg) = match self.configured(slot) { | ||
Ok(PublicKeyInfo::EcP256(_)) => (AlgorithmId::EccP256, &digest::SHA256), | ||
Ok(PublicKeyInfo::EcP384(_)) => (AlgorithmId::EccP384, &digest::SHA384), | ||
Ok(_) => (AlgorithmId::Rsa2048, &digest::SHA256), // RSAish | ||
Err(_) => return Err(Error::Unprovisioned), | ||
let cert = self.configured(&slot).unwrap(); | ||
let pki = cert.subject_pki(); | ||
let oid_alg = pki | ||
.algorithm | ||
.parameters_oid() | ||
.map_err(|_| Error::Unprovisioned)?; | ||
|
||
let (slot_alg, hash_alg) = match oid_alg { | ||
NISTP256_OID => (AlgorithmId::EccP256, &digest::SHA256), | ||
SECP384_OID => (AlgorithmId::EccP384, &digest::SHA384), | ||
_ => return Err(Error::Unprovisioned), | ||
}; | ||
|
||
if slot_alg != alg { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hasn't been tested