Skip to content

Commit

Permalink
csr: support basic constraints -> IsCA from CSR
Browse files Browse the repository at this point in the history
  • Loading branch information
cpu committed Oct 3, 2023
1 parent e8348ab commit ae702ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{CustomExtension, DistinguishedName, SanType};
use pem::Pem;
use std::hash::Hash;

use crate::{Certificate, CertificateParams, PublicKeyData, RcgenError, SignatureAlgorithm};
use crate::{
BasicConstraints, Certificate, CertificateParams, IsCa, PublicKeyData, RcgenError,
SignatureAlgorithm,
};

/// A public key, extracted from a CSR
#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -101,6 +104,20 @@ impl CertificateSigningRequest {
params.key_identifier = ski.0.to_vec();
true
},
x509_parser::extensions::ParsedExtension::BasicConstraints(bc) => {
params.is_ca = match (bc.ca, bc.path_len_constraint) {
(false, _) => IsCa::ExplicitNoCa,
(true, None) => IsCa::Ca(BasicConstraints::Unconstrained),
(true, Some(len_constraint)) => {
IsCa::Ca(BasicConstraints::Constrained(
len_constraint.try_into().map_err(|_| {
RcgenError::UnsupportedBasicConstraintsPathLen
})?,
))
},
};
true
},
_ => false,
};
if !supported {
Expand All @@ -114,7 +131,6 @@ impl CertificateSigningRequest {
}

// Not yet handled:
// * is_ca
// * extended_key_usages
// * name_constraints
// and any other extensions.
Expand Down
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub enum RcgenError {
RingUnspecified,
/// Time conversion related errors
Time,
/// Unsupported basic constraints extension path length in CSR
#[cfg(feature = "x509-parser")]
UnsupportedBasicConstraintsPathLen,
/// Unsupported extension requested in CSR
#[cfg(feature = "x509-parser")]
UnsupportedExtension,
Expand Down Expand Up @@ -97,6 +100,11 @@ impl fmt::Display for RcgenError {
DuplicateExtension(oid) => {
write!(f, "Extension with OID {oid} present multiple times")?
},
#[cfg(feature = "x509-parser")]
UnsupportedBasicConstraintsPathLen => write!(
f,
"Unsupported basic constraints extension path length constraint in CSR"
)?,
};
Ok(())
}
Expand Down

0 comments on commit ae702ea

Please sign in to comment.