Skip to content

Commit db9a62b

Browse files
committed
lib: use From trait for KeyUsagePurpose -> u16
1 parent 964f9c6 commit db9a62b

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

rcgen/src/certificate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl CertificateParams {
439439
write_x509_extension(writer, oid::KEY_USAGE, true, |writer| {
440440
// u16 is large enough to encode the largest possible key usage (two-bytes)
441441
let bit_string = self.key_usages.iter().fold(0u16, |bit_string, key_usage| {
442-
bit_string | key_usage.to_u16()
442+
bit_string | u16::from(*key_usage)
443443
});
444444
writer.write_bitvec_bytes(&bit_string.to_be_bytes(), KEY_USAGE_BITS);
445445
});

rcgen/src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -436,23 +436,6 @@ pub enum KeyUsagePurpose {
436436
}
437437

438438
impl KeyUsagePurpose {
439-
/// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
440-
/// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
441-
fn to_u16(&self) -> u16 {
442-
const FLAG: u16 = 0b1000_0000_0000_0000;
443-
FLAG >> match self {
444-
KeyUsagePurpose::DigitalSignature => 0,
445-
KeyUsagePurpose::ContentCommitment => 1,
446-
KeyUsagePurpose::KeyEncipherment => 2,
447-
KeyUsagePurpose::DataEncipherment => 3,
448-
KeyUsagePurpose::KeyAgreement => 4,
449-
KeyUsagePurpose::KeyCertSign => 5,
450-
KeyUsagePurpose::CrlSign => 6,
451-
KeyUsagePurpose::EncipherOnly => 7,
452-
KeyUsagePurpose::DecipherOnly => 8,
453-
}
454-
}
455-
456439
/// Parse a collection of key usages from a [`u16`] representing the value
457440
/// of a KeyUsage BIT STRING as defined by RFC 5280.
458441
#[cfg(feature = "x509-parser")]
@@ -470,13 +453,32 @@ impl KeyUsagePurpose {
470453
]
471454
.iter()
472455
.filter_map(|key_usage| {
473-
let present = key_usage.to_u16() & value != 0;
456+
let present = u16::from(key_usage) & value != 0;
474457
present.then_some(*key_usage)
475458
})
476459
.collect()
477460
}
478461
}
479462

463+
/// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
464+
/// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
465+
impl From<KeyUsagePurpose> for u16 {
466+
fn from(value: KeyUsagePurpose) -> Self {
467+
const FLAG: u16 = 0b1000_0000_0000_0000;
468+
FLAG >> match value {
469+
KeyUsagePurpose::DigitalSignature => 0,
470+
KeyUsagePurpose::ContentCommitment => 1,
471+
KeyUsagePurpose::KeyEncipherment => 2,
472+
KeyUsagePurpose::DataEncipherment => 3,
473+
KeyUsagePurpose::KeyAgreement => 4,
474+
KeyUsagePurpose::KeyCertSign => 5,
475+
KeyUsagePurpose::CrlSign => 6,
476+
KeyUsagePurpose::EncipherOnly => 7,
477+
KeyUsagePurpose::DecipherOnly => 8,
478+
}
479+
}
480+
}
481+
480482
/// Method to generate key identifiers from public keys.
481483
///
482484
/// Key identifiers should be derived from the public key data. [RFC 7093] defines

0 commit comments

Comments
 (0)