-
Notifications
You must be signed in to change notification settings - Fork 192
crypto: Implement issued check for symcrypt, improve error handling #3444
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
smalis-msft
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
smalis-msft:x509-issued
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
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,8 +72,71 @@ impl X509CertificateInner { | |
| Ok(true) | ||
| } | ||
|
|
||
| pub fn issued(&self, subject: &X509CertificateInner) -> bool { | ||
| self.0.tbs_certificate().subject() == subject.0.tbs_certificate().issuer() | ||
| pub fn issued(&self, subject: &X509CertificateInner) -> Result<bool, X509Error> { | ||
|
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. Is this some set of standard checks? Why isn't this logic in symcrypt itself? |
||
| use x509_cert::ext::pkix::AuthorityKeyIdentifier; | ||
| use x509_cert::ext::pkix::KeyUsage; | ||
| use x509_cert::ext::pkix::SubjectKeyIdentifier; | ||
| use x509_cert::ext::pkix::name::GeneralName; | ||
|
|
||
| let issuer_tbs = self.0.tbs_certificate(); | ||
| let subject_tbs = subject.0.tbs_certificate(); | ||
|
|
||
| // The subject's issuer name must match the issuer's subject name. | ||
| if subject_tbs.issuer() != issuer_tbs.subject() { | ||
| return Ok(false); | ||
| } | ||
|
|
||
| // If this certificate has a KeyUsage extension, it must permit | ||
| // signing other certificates. | ||
| let ku = issuer_tbs | ||
| .get_extension::<KeyUsage>() | ||
| .map_err(|e| der_err(e, "parsing KeyUsage extension"))?; | ||
| if let Some((_crit, ku)) = ku | ||
| && !ku.key_cert_sign() | ||
| { | ||
| return Ok(false); | ||
| } | ||
|
smalis-msft marked this conversation as resolved.
|
||
|
|
||
| // If the subject carries an AuthorityKeyIdentifier, validate its | ||
| // populated fields against this certificate (the candidate issuer). | ||
| let akid = subject_tbs | ||
| .get_extension::<AuthorityKeyIdentifier>() | ||
| .map_err(|e| der_err(e, "parsing AuthorityKeyIdentifier extension"))?; | ||
| if let Some((_crit, akid)) = akid { | ||
| if let Some(akid_key_id) = &akid.key_identifier { | ||
| let skid = issuer_tbs | ||
| .get_extension::<SubjectKeyIdentifier>() | ||
| .map_err(|e| der_err(e, "parsing SubjectKeyIdentifier extension"))?; | ||
| match skid { | ||
| Some((_crit, ski)) => { | ||
| if akid_key_id != &ski.0 { | ||
| return Ok(false); | ||
| } | ||
| } | ||
| None => return Ok(false), | ||
| } | ||
| } | ||
| if let Some(akid_serial) = &akid.authority_cert_serial_number { | ||
| if akid_serial != issuer_tbs.serial_number() { | ||
| return Ok(false); | ||
| } | ||
| } | ||
| if let Some(gens) = &akid.authority_cert_issuer { | ||
| let mut has_dn = false; | ||
| let has_matching_dn = gens.iter().any(|g| match g { | ||
| GeneralName::DirectoryName(dn) => { | ||
| has_dn = true; | ||
| dn == issuer_tbs.subject() | ||
| } | ||
| _ => false, | ||
| }); | ||
| if has_dn && !has_matching_dn { | ||
| return Ok(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(true) | ||
| } | ||
|
|
||
| pub fn to_der(&self) -> Result<Vec<u8>, X509Error> { | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.