-
Notifications
You must be signed in to change notification settings - Fork 172
x509-cert: builder updates #1001
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d733ea
x509-cert: builder: implement std::error::Error for Error
lumag fec22bc
x509-cert: builder: disable DigitalSignature usage on Root and SubCA …
lumag dd9374c
x509-cert: builder: use DynSignatureAlgorithmIdentifier for S
lumag 26b0c58
x509-cert: builder: don't require mutability of the signer
lumag da6cd22
x509-cert: builder: make keyEncipherment usage optional
lumag 77536b3
x509-cert: test-support/zlint: implement Eq in addition to PartialEq
lumag 6d6160b
x509-cert: don't require signature's 'derive' feature
lumag 0032002
x509-cert: builder: follow rules from RFC5280 to set certificate's ve…
lumag e560912
x509-cert/tests/builder: remove extra to_der/from_der conversion
lumag 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,9 @@ pub enum Error { | |
| Signature(signature::Error), | ||
| } | ||
|
|
||
| #[cfg(feature = "std")] | ||
| impl std::error::Error for Error {} | ||
|
|
||
| impl fmt::Display for Error { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
|
|
@@ -87,6 +90,8 @@ pub enum Profile { | |
| issuer: Name, | ||
| /// should the key agreement flag of KeyUsage be enabled | ||
| enable_key_agreement: bool, | ||
| /// should the key encipherment flag of KeyUsage be enabled | ||
| enable_key_encipherment: bool, | ||
| }, | ||
| #[cfg(feature = "hazmat")] | ||
| /// Opt-out of the default extensions | ||
|
|
@@ -161,20 +166,18 @@ impl Profile { | |
| // Build Key Usage extension | ||
| match self { | ||
| Profile::Root | Profile::SubCA { .. } => { | ||
| extensions.push( | ||
| KeyUsage( | ||
| KeyUsages::DigitalSignature | KeyUsages::KeyCertSign | KeyUsages::CRLSign, | ||
| ) | ||
| .to_extension(tbs)?, | ||
| ); | ||
| extensions | ||
| .push(KeyUsage(KeyUsages::KeyCertSign | KeyUsages::CRLSign).to_extension(tbs)?); | ||
| } | ||
| Profile::Leaf { | ||
| enable_key_agreement, | ||
| enable_key_encipherment, | ||
| .. | ||
| } => { | ||
| let mut key_usage = KeyUsages::DigitalSignature | ||
| | KeyUsages::NonRepudiation | ||
| | KeyUsages::KeyEncipherment; | ||
| let mut key_usage = KeyUsages::DigitalSignature | KeyUsages::NonRepudiation; | ||
| if *enable_key_encipherment { | ||
| key_usage |= KeyUsages::KeyEncipherment; | ||
| } | ||
| if *enable_key_agreement { | ||
| key_usage |= KeyUsages::KeyAgreement; | ||
| } | ||
|
|
@@ -194,7 +197,6 @@ impl Profile { | |
| /// ``` | ||
| /// use der::Decode; | ||
| /// use x509_cert::spki::SubjectPublicKeyInfoOwned; | ||
| /// use x509_cert::certificate::Version; | ||
| /// use x509_cert::builder::{CertificateBuilder, Profile}; | ||
| /// use x509_cert::name::Name; | ||
| /// use x509_cert::serial_number::SerialNumber; | ||
|
|
@@ -223,35 +225,32 @@ impl Profile { | |
| /// let mut signer = rsa_signer(); | ||
| /// let mut builder = CertificateBuilder::new( | ||
| /// profile, | ||
| /// Version::V3, | ||
| /// serial_number, | ||
| /// validity, | ||
| /// subject, | ||
| /// pub_key, | ||
| /// &mut signer, | ||
| /// &signer, | ||
| /// ) | ||
| /// .expect("Create certificate"); | ||
| /// ``` | ||
| pub struct CertificateBuilder<'s, S> { | ||
| tbs: TbsCertificate, | ||
| signer: &'s mut S, | ||
| signer: &'s S, | ||
| } | ||
|
|
||
| impl<'s, S> CertificateBuilder<'s, S> | ||
| where | ||
| S: Keypair, | ||
| S: Keypair + DynSignatureAlgorithmIdentifier, | ||
| S::VerifyingKey: EncodePublicKey, | ||
| S::VerifyingKey: DynSignatureAlgorithmIdentifier, | ||
| { | ||
| /// Creates a new certificate builder | ||
| pub fn new<Signature>( | ||
| profile: Profile, | ||
| version: Version, | ||
|
Member
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. good call. Thank you! |
||
| serial_number: SerialNumber, | ||
| mut validity: Validity, | ||
| subject: Name, | ||
| subject_public_key_info: SubjectPublicKeyInfoOwned, | ||
| signer: &'s mut S, | ||
| signer: &'s S, | ||
| ) -> Result<Self> | ||
| where | ||
| S: Signer<Signature>, | ||
|
|
@@ -261,14 +260,14 @@ where | |
| .to_public_key_der()? | ||
| .decode_msg::<SubjectPublicKeyInfoOwned>()?; | ||
|
|
||
| let signature_alg = verifying_key.signature_algorithm_identifier()?; | ||
| let signature_alg = signer.signature_algorithm_identifier()?; | ||
| let issuer = profile.get_issuer(&subject); | ||
|
|
||
| validity.not_before.rfc5280_adjust_utc_time()?; | ||
| validity.not_after.rfc5280_adjust_utc_time()?; | ||
|
|
||
| let mut tbs = TbsCertificate { | ||
| version, | ||
| version: Version::V3, | ||
| serial_number, | ||
| signature: signature_alg, | ||
| issuer, | ||
|
|
@@ -286,15 +285,13 @@ where | |
| subject_unique_id: None, | ||
| }; | ||
|
|
||
| if tbs.version == Version::V3 { | ||
| let extensions = profile.build_extensions( | ||
| tbs.subject_public_key_info.owned_to_ref(), | ||
| signer_pub.owned_to_ref(), | ||
| &tbs, | ||
| )?; | ||
| if !extensions.is_empty() { | ||
| tbs.extensions = Some(extensions); | ||
| } | ||
| let extensions = profile.build_extensions( | ||
| tbs.subject_public_key_info.owned_to_ref(), | ||
| signer_pub.owned_to_ref(), | ||
| &tbs, | ||
| )?; | ||
| if !extensions.is_empty() { | ||
| tbs.extensions = Some(extensions); | ||
| } | ||
|
|
||
| Ok(Self { tbs, signer }) | ||
|
|
@@ -317,17 +314,24 @@ where | |
| } | ||
|
|
||
| /// Run the certificate through the signer and build the end certificate. | ||
| pub fn build<Signature>(&mut self) -> Result<Certificate> | ||
| pub fn build<Signature>(mut self) -> Result<Certificate> | ||
| where | ||
| S: Signer<Signature>, | ||
| Signature: SignatureEncoding, | ||
| { | ||
| if self.tbs.extensions.is_none() { | ||
| if self.tbs.issuer_unique_id.is_some() || self.tbs.subject_unique_id.is_some() { | ||
| self.tbs.version = Version::V2; | ||
| } else { | ||
| self.tbs.version = Version::V1; | ||
| } | ||
| } | ||
| let signature = self.signer.try_sign(&self.tbs.to_der()?)?; | ||
| let signature = BitString::from_bytes(signature.to_bytes().as_ref())?; | ||
|
|
||
| let cert = Certificate { | ||
| tbs_certificate: self.tbs.clone(), | ||
| signature_algorithm: self.tbs.signature.clone(), | ||
| signature_algorithm: self.tbs.signature, | ||
| signature, | ||
| }; | ||
|
|
||
|
|
||
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
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.
KeyUsages::DigitalSignatureis getting dropped, I don't remember why I added it in the first place.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.
@baloo it is reasoned in the commit message. Please review by the commit.