Skip to content
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

Fix: mark SAN as critical when subject is empty #311

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rcgen/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,10 @@ impl CertificateParams {
return;
}

write_x509_extension(writer, oid::SUBJECT_ALT_NAME, false, |writer| {
// Per https://tools.ietf.org/html/rfc5280#section-4.1.2.6, SAN must be marked
// as critical if subject is empty.
let critical = self.distinguished_name.entries.is_empty();
write_x509_extension(writer, oid::SUBJECT_ALT_NAME, critical, |writer| {
writer.write_sequence(|writer| {
for san in self.subject_alt_names.iter() {
writer.next().write_tagged_implicit(
Expand Down
51 changes: 51 additions & 0 deletions rcgen/tests/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,54 @@ mod test_csr {
assert_eq!(*params, csrp.params);
}
}

#[cfg(feature = "x509-parser")]
mod test_subject_alternative_name_criticality {
use x509_parser::certificate::X509Certificate;
use x509_parser::extensions::X509Extension;
use x509_parser::{oid_registry, parse_x509_certificate};

use crate::util::default_params;

#[test]
fn with_subject_sans_not_critical() {
let (params, keypair) = default_params();
assert!(
!params
.distinguished_name
.iter()
.collect::<Vec<_>>()
.is_empty(),
"non-empty subject required for test"
);
cpu marked this conversation as resolved.
Show resolved Hide resolved

let cert = params.self_signed(&keypair).unwrap();
let cert = cert.der();
let (_, parsed) = parse_x509_certificate(cert).unwrap();
assert!(
!san_ext(&parsed).critical,
"with subject, SAN ext should not be critical"
);
}

#[test]
fn without_subject_sans_critical() {
let (mut params, keypair) = default_params();
params.distinguished_name = Default::default();

let cert = params.self_signed(&keypair).unwrap();
let cert = cert.der();
let (_, parsed) = parse_x509_certificate(cert).unwrap();
assert!(
san_ext(&parsed).critical,
"without subject, SAN ext should be critical"
);
}

fn san_ext<'cert>(cert: &'cert X509Certificate) -> &'cert X509Extension<'cert> {
cert.extensions()
.iter()
.find(|ext| ext.oid == oid_registry::OID_X509_EXT_SUBJECT_ALT_NAME)
.expect("missing SAN extension")
}
}
Loading