Skip to content

Fix regression in key usage purpose encoding #369

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

Merged
merged 2 commits into from
Jul 20, 2025
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rcgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rcgen"
version = "0.14.2"
version = "0.14.3"
documentation = "https://docs.rs/rcgen"
description.workspace = true
repository.workspace = true
Expand Down
17 changes: 13 additions & 4 deletions rcgen/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@ impl CertificateParams {

/// Write a certificate's KeyUsage as defined in RFC 5280.
fn write_key_usage(&self, writer: DERWriter) {
// RFC 5280 defines 9 key usages, which we detail in our key usage enum
// We could use std::mem::variant_count here, but it's experimental
const KEY_USAGE_BITS: usize = 9;
if self.key_usages.is_empty() {
return;
}
Expand All @@ -227,7 +224,16 @@ impl CertificateParams {
let bit_string = self.key_usages.iter().fold(0u16, |bit_string, key_usage| {
bit_string | key_usage.to_u16()
});
writer.write_bitvec_bytes(&bit_string.to_be_bytes(), KEY_USAGE_BITS);

match u16::BITS - bit_string.trailing_zeros() {
bits @ 0..=8 => {
writer.write_bitvec_bytes(&bit_string.to_be_bytes()[..1], bits as usize)
},
bits @ 9..=16 => {
writer.write_bitvec_bytes(&bit_string.to_be_bytes(), bits as usize)
},
_ => unreachable!(),
}
});
}

Expand Down Expand Up @@ -1146,6 +1152,9 @@ mod tests {

for ext in cert.extensions() {
if key_usage_oid_str == ext.oid.to_id_string() {
// should have the minimal number of octets, and no extra trailing zero bytes
// ref. https://github.com/rustls/rcgen/issues/368
assert_eq!(ext.value, vec![0x03, 0x02, 0x05, 0xe0]);
if let x509_parser::extensions::ParsedExtension::KeyUsage(usage) =
ext.parsed_extension()
{
Expand Down
Loading