Skip to content

Commit

Permalink
Implement FromDer for Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jul 31, 2023
1 parent 46667b1 commit 08f4632
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'a> Cert<'a> {
der::Tag::Sequence,
Error::BadDer,
|extension| {
remember_cert_extension(&mut cert, &Extension::parse(extension)?)
remember_cert_extension(&mut cert, &Extension::from_der(extension)?)
},
)
},
Expand Down
4 changes: 2 additions & 2 deletions src/crl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl<'a> FromDer<'a> for BorrowedCertRevocationList<'a> {
// that the application cannot process, then the application MUST NOT
// use that CRL to determine the status of certificates. However,
// applications may ignore unrecognized non-critical extensions.
crl.remember_extension(&Extension::parse(extension)?)
crl.remember_extension(&Extension::from_der(extension)?)
},
)
},
Expand Down Expand Up @@ -537,7 +537,7 @@ impl<'a> FromDer<'a> for BorrowedRevokedCert<'a> {
// process, then the application MUST NOT use that CRL to determine the
// status of any certificates. However, applications may ignore
// unrecognized non-critical CRL entry extensions.
revoked_cert.remember_extension(&Extension::parse(ext_der)?)
revoked_cert.remember_extension(&Extension::from_der(ext_der)?)
})?;
if reader.at_end() {
break;
Expand Down
24 changes: 13 additions & 11 deletions src/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ pub(crate) struct Extension<'a> {
}

impl<'a> Extension<'a> {
pub(crate) fn parse(der: &mut untrusted::Reader<'a>) -> Result<Extension<'a>, Error> {
let id = der::expect_tag_and_get_value(der, der::Tag::OID)?;
let critical = bool::from_der(der)?;
let value = der::expect_tag_and_get_value(der, der::Tag::OctetString)?;
Ok(Extension {
id,
critical,
value,
})
}

pub(crate) fn unsupported(&self) -> Result<(), Error> {
match self.critical {
true => Err(Error::UnsupportedCriticalExtension),
Expand All @@ -42,6 +31,19 @@ impl<'a> Extension<'a> {
}
}

impl<'a> FromDer<'a> for Extension<'a> {
fn from_der(reader: &mut untrusted::Reader<'a>) -> Result<Self, Error> {
let id = der::expect_tag_and_get_value(reader, der::Tag::OID)?;
let critical = bool::from_der(reader)?;
let value = der::expect_tag_and_get_value(reader, der::Tag::OctetString)?;
Ok(Extension {
id,
critical,
value,
})
}
}

pub(crate) fn set_extension_once<T>(
destination: &mut Option<T>,
parser: impl Fn() -> Result<T, Error>,
Expand Down

0 comments on commit 08f4632

Please sign in to comment.