Skip to content

Commit 793122b

Browse files
BiagioFestaest31
andauthored
ensure default serial generation fits 20 bytes (#203)
By default, s/n is generated taking digest of pub-key of the certificate. However, if the slice number representation is a negative number, then `write_bigint_bytes` is going to append an additional `0`-byte to ensure the positive sign. See: https://github.com/qnighy/yasna.rs/blob/b7e65f9a4c317494cce2d18ea02b3d6eaaea7985/src/writer/mod.rs#L493-L495 So it is possible the bigint encoding will take 21 bytes instead of 20. This CR sets MSB of digest to `0` to ensure encoding will take exactly 20 bytes Co-authored-by: est31 <[email protected]>
1 parent 44bb7c7 commit 793122b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

rcgen/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,9 @@ impl CertificateParams {
937937
} else {
938938
let hash = digest::digest(&digest::SHA256, pub_key.raw_bytes());
939939
// RFC 5280 specifies at most 20 bytes for a serial number
940-
let sl = &hash.as_ref()[0..20];
941-
writer.next().write_bigint_bytes(sl, true);
940+
let mut sl = hash.as_ref()[0..20].to_vec();
941+
sl[0] = sl[0] & 0x7f; // MSB must be 0 to ensure encoding bignum in 20 bytes
942+
writer.next().write_bigint_bytes(&sl, true);
942943
};
943944
// Write signature
944945
ca.params.alg.write_alg_ident(writer.next());

0 commit comments

Comments
 (0)