Skip to content

Commit 0a2cd05

Browse files
committed
Prepare to allow extensions under different OID arcs
1 parent 6b809a7 commit 0a2cd05

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/cert.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use crate::error::{DerTypeId, Error};
2121
use crate::public_values_eq;
2222
use crate::signed_data::SignedData;
2323
use crate::subject_name::{GeneralName, NameIterator, WildcardDnsNameRef};
24-
use crate::x509::{DistributionPointName, Extension, remember_extension, set_extension_once};
24+
use crate::x509::{
25+
DistributionPointName, Extension, ExtensionOid, remember_extension, set_extension_once,
26+
};
2527

2628
/// A parsed X509 certificate.
2729
pub struct Cert<'a> {
@@ -263,25 +265,27 @@ fn remember_cert_extension<'a>(
263265
// all policy-related stuff. We assume that the policy-related extensions
264266
// are not marked critical.
265267

268+
use ExtensionOid::*;
269+
266270
remember_extension(extension, |id| {
267271
let out = match id {
268272
// id-ce-keyUsage 2.5.29.15.
269-
15 => &mut cert.key_usage,
273+
Standard(15) => &mut cert.key_usage,
270274

271275
// id-ce-subjectAltName 2.5.29.17
272-
17 => &mut cert.subject_alt_name,
276+
Standard(17) => &mut cert.subject_alt_name,
273277

274278
// id-ce-basicConstraints 2.5.29.19
275-
19 => &mut cert.basic_constraints,
279+
Standard(19) => &mut cert.basic_constraints,
276280

277281
// id-ce-nameConstraints 2.5.29.30
278-
30 => &mut cert.name_constraints,
282+
Standard(30) => &mut cert.name_constraints,
279283

280284
// id-ce-cRLDistributionPoints 2.5.29.31
281-
31 => &mut cert.crl_distribution_points,
285+
Standard(31) => &mut cert.crl_distribution_points,
282286

283287
// id-ce-extKeyUsage 2.5.29.37
284-
37 => &mut cert.eku,
288+
Standard(37) => &mut cert.eku,
285289

286290
// Unsupported extension
287291
_ => return extension.unsupported(),
@@ -291,7 +295,7 @@ fn remember_cert_extension<'a>(
291295
extension.value.read_all(Error::BadDer, |value| match id {
292296
// Unlike the other extensions we remember KU is a BitString and not a Sequence. We
293297
// read the raw bytes here and parse at the time of use.
294-
15 => Ok(value.read_bytes_to_end()),
298+
Standard(15) => Ok(value.read_bytes_to_end()),
295299
// All other remembered certificate extensions are wrapped in a Sequence.
296300
_ => der::expect_tag(value, Tag::Sequence),
297301
})

src/crl/types.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,12 @@ impl<'a> BorrowedCertRevocationList<'a> {
264264
}
265265

266266
fn remember_extension(&mut self, extension: &Extension<'a>) -> Result<(), Error> {
267+
use crate::x509::ExtensionOid::*;
268+
267269
remember_extension(extension, |id| {
268270
match id {
269271
// id-ce-cRLNumber 2.5.29.20 - RFC 5280 §5.2.3
270-
20 => {
272+
Standard(20) => {
271273
// RFC 5280 §5.2.3:
272274
// CRL verifiers MUST be able to handle CRLNumber values
273275
// up to 20 octets. Conforming CRL issuers MUST NOT use CRLNumber
@@ -289,17 +291,17 @@ impl<'a> BorrowedCertRevocationList<'a> {
289291

290292
// id-ce-deltaCRLIndicator 2.5.29.27 - RFC 5280 §5.2.4
291293
// We explicitly do not support delta CRLs.
292-
27 => Err(Error::UnsupportedDeltaCrl),
294+
Standard(27) => Err(Error::UnsupportedDeltaCrl),
293295

294296
// id-ce-issuingDistributionPoint 2.5.29.28 - RFC 5280 §5.2.4
295297
// We recognize the extension and retain its value for use.
296-
28 => {
298+
Standard(28) => {
297299
set_extension_once(&mut self.issuing_distribution_point, || Ok(extension.value))
298300
}
299301

300302
// id-ce-authorityKeyIdentifier 2.5.29.35 - RFC 5280 §5.2.1, §4.2.1.1
301303
// We recognize the extension but don't retain its value for use.
302-
35 => Ok(()),
304+
Standard(35) => Ok(()),
303305

304306
// Unsupported extension
305307
_ => extension.unsupported(),
@@ -739,13 +741,17 @@ impl<'a> BorrowedRevokedCert<'a> {
739741
}
740742

741743
fn remember_extension(&mut self, extension: &Extension<'a>) -> Result<(), Error> {
744+
use crate::x509::ExtensionOid::*;
745+
742746
remember_extension(extension, |id| {
743747
match id {
744748
// id-ce-cRLReasons 2.5.29.21 - RFC 5280 §5.3.1.
745-
21 => set_extension_once(&mut self.reason_code, || der::read_all(extension.value)),
749+
Standard(21) => {
750+
set_extension_once(&mut self.reason_code, || der::read_all(extension.value))
751+
}
746752

747753
// id-ce-invalidityDate 2.5.29.24 - RFC 5280 §5.3.2.
748-
24 => set_extension_once(&mut self.invalidity_date, || {
754+
Standard(24) => set_extension_once(&mut self.invalidity_date, || {
749755
extension.value.read_all(Error::BadDer, UnixTime::from_der)
750756
}),
751757

@@ -756,7 +762,7 @@ impl<'a> BorrowedRevokedCert<'a> {
756762
// extension.
757763
// We choose not to support indirect CRLs and so turn this into a more specific
758764
// error rather than simply letting it fail as an unsupported critical extension.
759-
29 => Err(Error::UnsupportedIndirectCrl),
765+
Standard(29) => Err(Error::UnsupportedIndirectCrl),
760766

761767
// Unsupported extension
762768
_ => extension.unsupported(),

src/x509.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ pub(crate) fn set_extension_once<T>(
6363

6464
pub(crate) fn remember_extension(
6565
extension: &Extension<'_>,
66-
mut handler: impl FnMut(u8) -> Result<(), Error>,
66+
mut handler: impl FnMut(ExtensionOid) -> Result<(), Error>,
6767
) -> Result<(), Error> {
6868
match extension.id.as_slice_less_safe() {
69-
[first, second, x] if [*first, *second] == ID_CE => handler(*x),
69+
[first, second, x] if [*first, *second] == ID_CE => handler(ExtensionOid::Standard(*x)),
7070
_ => extension.unsupported(),
7171
}
7272
}
@@ -109,3 +109,10 @@ impl<'a> FromDer<'a> for DistributionPointName<'a> {
109109

110110
const TYPE_ID: DerTypeId = DerTypeId::DistributionPointName;
111111
}
112+
113+
/// Simplified representation of supported extension OIDs.
114+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
115+
pub(crate) enum ExtensionOid {
116+
/// Extensions whose OID is under `id-ce` arc.
117+
Standard(u8),
118+
}

0 commit comments

Comments
 (0)