Skip to content
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

lib: use From trait for KeyUsagePurpose -> u16 #289

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rcgen/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ impl CertificateParams {
write_x509_extension(writer, oid::KEY_USAGE, true, |writer| {
// u16 is large enough to encode the largest possible key usage (two-bytes)
let bit_string = self.key_usages.iter().fold(0u16, |bit_string, key_usage| {
bit_string | key_usage.to_u16()
bit_string | u16::from(*key_usage)
});
writer.write_bitvec_bytes(&bit_string.to_be_bytes(), KEY_USAGE_BITS);
});
Expand Down
38 changes: 20 additions & 18 deletions rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,23 +436,6 @@ pub enum KeyUsagePurpose {
}

impl KeyUsagePurpose {
/// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
/// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
fn to_u16(&self) -> u16 {
const FLAG: u16 = 0b1000_0000_0000_0000;
FLAG >> match self {
KeyUsagePurpose::DigitalSignature => 0,
KeyUsagePurpose::ContentCommitment => 1,
KeyUsagePurpose::KeyEncipherment => 2,
KeyUsagePurpose::DataEncipherment => 3,
KeyUsagePurpose::KeyAgreement => 4,
KeyUsagePurpose::KeyCertSign => 5,
KeyUsagePurpose::CrlSign => 6,
KeyUsagePurpose::EncipherOnly => 7,
KeyUsagePurpose::DecipherOnly => 8,
}
}

/// Parse a collection of key usages from a [`u16`] representing the value
/// of a KeyUsage BIT STRING as defined by RFC 5280.
#[cfg(feature = "x509-parser")]
Expand All @@ -470,13 +453,32 @@ impl KeyUsagePurpose {
]
.iter()
.filter_map(|key_usage| {
let present = key_usage.to_u16() & value != 0;
let present = u16::from(*key_usage) & value != 0;
present.then_some(*key_usage)
})
.collect()
}
}

/// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
/// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
impl From<KeyUsagePurpose> for u16 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dissuaded the OP from this because I thought we'd prefer to keep this interface private.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok! I don't feel strongly in that case. Apologies for the noise.

fn from(value: KeyUsagePurpose) -> Self {
const FLAG: u16 = 0b1000_0000_0000_0000;
FLAG >> match value {
KeyUsagePurpose::DigitalSignature => 0,
KeyUsagePurpose::ContentCommitment => 1,
KeyUsagePurpose::KeyEncipherment => 2,
KeyUsagePurpose::DataEncipherment => 3,
KeyUsagePurpose::KeyAgreement => 4,
KeyUsagePurpose::KeyCertSign => 5,
KeyUsagePurpose::CrlSign => 6,
KeyUsagePurpose::EncipherOnly => 7,
KeyUsagePurpose::DecipherOnly => 8,
}
}
}

/// Method to generate key identifiers from public keys.
///
/// Key identifiers should be derived from the public key data. [RFC 7093] defines
Expand Down