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

ensure default serial generation fits 20 bytes #203

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,9 @@ impl CertificateParams {
} else {
let hash = digest::digest(&digest::SHA256, pub_key.raw_bytes());
// RFC 5280 specifies at most 20 bytes for a serial number
let sl = &hash.as_ref()[0..20];
writer.next().write_bigint_bytes(sl, true);
let mut sl = hash.as_ref()[0..20].to_vec();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can obtain the same result (more or less) with alternatives:

  • let sl = &hash.as_ref()[..19]. This will avoid one line and mut, but we are "wasting" 7 bits
  • Avoid heap allocation with array and memcpy.

I opted for the most coincided approach with Vec, considering I don't expect hot-path here

sl[0] = sl[0] & 0x7f; // MSB must be 0 to ensure encoding bignum in 20 bytes
writer.next().write_bigint_bytes(&sl, true);
};
// Write signature
ca.params.alg.write_alg_ident(writer.next());
Expand Down