Skip to content

Commit 1191c98

Browse files
djcctz
authored andcommitted
Move check_key_usage() into KeyUsageMode impl
1 parent 241e9bd commit 1191c98

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/verify_cert.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn check_crls(
211211
.map_err(crl_signature_err)?;
212212

213213
// Verify that if the issuer has a KeyUsage bitstring it asserts cRLSign.
214-
check_key_usage(issuer_ku, KeyUsageMode::CrlSign)?;
214+
KeyUsageMode::CrlSign.check(issuer_ku)?;
215215

216216
// Try to find the cert serial in the verified CRL contents.
217217
let cert_serial = cert.serial.as_slice_less_safe();
@@ -430,6 +430,7 @@ pub(crate) static EKU_OCSP_SIGNING: KeyPurposeId =
430430

431431
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3
432432
#[repr(u8)]
433+
#[derive(Clone, Copy)]
433434
enum KeyUsageMode {
434435
// DigitalSignature = 0,
435436
// ContentCommitment = 1,
@@ -442,24 +443,23 @@ enum KeyUsageMode {
442443
// DecipherOnly = 8,
443444
}
444445

445-
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3
446-
fn check_key_usage(
447-
input: Option<untrusted::Input>,
448-
required_ku_bit_if_present: KeyUsageMode,
449-
) -> Result<(), Error> {
450-
let bit_string = match input {
451-
Some(input) => input,
452-
// While RFC 5280 requires KeyUsage be present, historically the absence of a KeyUsage
453-
// has been treated as "Any Usage". We follow that convention here and assume the absence
454-
// of KeyUsage implies the required_ku_bit_if_present we're checking for.
455-
None => return Ok(()),
456-
};
446+
impl KeyUsageMode {
447+
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3
448+
fn check(self, input: Option<untrusted::Input>) -> Result<(), Error> {
449+
let bit_string = match input {
450+
Some(input) => input,
451+
// While RFC 5280 requires KeyUsage be present, historically the absence of a KeyUsage
452+
// has been treated as "Any Usage". We follow that convention here and assume the absence
453+
// of KeyUsage implies the required_ku_bit_if_present we're checking for.
454+
None => return Ok(()),
455+
};
457456

458-
let flags = der::bit_string_flags(&mut untrusted::Reader::new(bit_string))?;
459-
#[allow(clippy::as_conversions)] // u8 always fits in usize.
460-
match flags.bit_set(required_ku_bit_if_present as usize) {
461-
true => Ok(()),
462-
false => Err(Error::IssuerNotCrlSigner),
457+
let flags = der::bit_string_flags(&mut untrusted::Reader::new(bit_string))?;
458+
#[allow(clippy::as_conversions)] // u8 always fits in usize.
459+
match flags.bit_set(self as usize) {
460+
true => Ok(()),
461+
false => Err(Error::IssuerNotCrlSigner),
462+
}
463463
}
464464
}
465465

0 commit comments

Comments
 (0)