-
Notifications
You must be signed in to change notification settings - Fork 113
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
Conversation
944d3b7
to
2dfa308
Compare
As per briansmith/webpki#232, webpki does not accept such serials. For truly random serials, 50% of the generated certificates have 1 at that position. So I wonder why this doesn't result in CI errors? |
Do you have in mind a particular test that could fail? |
@@ -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(); |
There was a problem hiding this comment.
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 andmut
, 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
This was changed in rustls/webpki: rustls/webpki#36 and that's what CI uses The diff in this branch looks correct to me on first glance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you
most of the tests in Eventually I'm fine with merging this even with this discrepancy unresolved, given that we have large degree of freedom to set the serial number, but I'd still like to find out why CI wasn't failing. |
I went back to dde0e47, the last rev before the switch to Parsing the same DER with Tldr; Serial number handling was further relaxed in |
Rebasing on |
thanks for analyzing this. I have approved the PR. We can merge it now. |
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 additional0
-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