Skip to content

Commit

Permalink
Fix: Don't forget to write EKUs in CSRs
Browse files Browse the repository at this point in the history
  • Loading branch information
lvkv committed Nov 9, 2024
1 parent cafc9fd commit 2d924af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 7 additions & 4 deletions rcgen/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ impl CertificateParams {
return Err(Error::UnsupportedInCsr);
}

// Whether or not to write an extension request attribute
let write_extension_request = !key_usages.is_empty()
|| !subject_alt_names.is_empty()
|| !extended_key_usages.is_empty()
|| !custom_extensions.is_empty();

let der = subject_key.sign_der(|writer| {
// Write version
writer.next().write_u8(0);
Expand All @@ -556,10 +562,7 @@ impl CertificateParams {
// Write extensions
// According to the spec in RFC 2986, even if attributes are empty we need the empty attribute tag
writer.next().write_tagged(Tag::context(0), |writer| {
if !key_usages.is_empty()
|| !subject_alt_names.is_empty()
|| !custom_extensions.is_empty()
{
if write_extension_request {
writer.write_sequence(|writer| {
let oid = ObjectIdentifier::from_slice(oid::PKCS_9_AT_EXTENSION_REQUEST);
writer.next().write_oid(&oid);
Expand Down
23 changes: 22 additions & 1 deletion rcgen/tests/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mod test_parse_other_name_alt_name {

#[cfg(feature = "x509-parser")]
mod test_csr_extension_request {
use rcgen::{CertificateParams, KeyPair, KeyUsagePurpose};
use rcgen::{CertificateParams, ExtendedKeyUsagePurpose, KeyPair, KeyUsagePurpose};
use x509_parser::prelude::{FromDer, ParsedExtension};

#[test]
Expand All @@ -379,6 +379,27 @@ mod test_csr_extension_request {
assert!(!(matches!(requested_ext, ParsedExtension::SubjectAlternativeName(_))));
});
}

#[test]
fn write_extension_request_if_ekus_are_present() {
let mut params = CertificateParams::default();
params
.extended_key_usages
.push(ExtendedKeyUsagePurpose::ClientAuth);
let key_pair = KeyPair::generate().unwrap();
let csr = params.serialize_request(&key_pair).unwrap();
let (_, parsed_csr) =
x509_parser::certification_request::X509CertificationRequest::from_der(csr.der())
.unwrap();
let requested_extensions = parsed_csr
.requested_extensions()
.unwrap()
.collect::<Vec<_>>();
assert!(matches!(
requested_extensions.first().unwrap(),
ParsedExtension::ExtendedKeyUsage(_)
));
}
}

#[cfg(feature = "x509-parser")]
Expand Down

0 comments on commit 2d924af

Please sign in to comment.