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

Fix leading serial number zeroes not being included in the database #245

Merged
merged 2 commits into from
Aug 10, 2017
Merged

Fix leading serial number zeroes not being included in the database #245

merged 2 commits into from
Aug 10, 2017

Conversation

autrilla
Copy link
Contributor

@autrilla autrilla commented Aug 10, 2017

Fixes #218

This patch changes the way we store the serial number in the database. Currently, we store the hex representation of the serial number. This patch stores the hex representation of the DER-encoded serial number. A few examples of what currently happens and what will happen after the patch:

Serial Number (Decimal) Currently stored Stored after patch
1 1 01
-1 -1 FF
0 0 00
201 C9 00C9
-201 -C9 FF37

You can try more examples here: https://play.golang.org/p/9BErIzLKCF

A possible improvement would be batching the updates in tools/fixserialnumbers.go to reduce the number of round trips.

@jvehent
Copy link
Contributor

jvehent commented Aug 10, 2017

This is a nice shortcut if doing a roundtrip through ASN1 does what we want. Would it be worthwhile to put the marshaling into a separate function and add tests to make sure we always get the expected values?

r? @jcjones & @mozkeeler

@autrilla
Copy link
Contributor Author

I think tests for this would be useful, yeah. I'll work on that.

}

// getSerialNumberASN1Value returns the ASN1-encoded serial number for the given certificate, excluding the tag and length.
func getSerialNumberASN1Value(cert *x509.Certificate) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is the function we want to move to the certificate package. I'd make it GetHexSerial and return the hexadecimal string, since this is what we always manipulate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, done. Less duplication this way.

Copy link

@jcjones jcjones left a comment

Choose a reason for hiding this comment

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

I can't really comment on the integration or DB-migration code, but the GetHexASN1Serial and its unit test look good to me. 👍

Copy link
Contributor

@mozkeeler mozkeeler left a comment

Choose a reason for hiding this comment

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

In general, this looks like it should work (can't really speak to the DB code). I do think we should confirm the 0-padding works as expected, though (see second comment).

@@ -447,6 +448,20 @@ func getPublicKeyInfo(cert *x509.Certificate) (SubjectPublicKeyInfo, error) {

}

func GetHexASN1Serial(cert *x509.Certificate) (serial string, err error) {
m, err := asn1.Marshal(cert.SerialNumber)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm - presumably go's asn1 parser wouldn't parse the certificate if the serial number were encoded incorrectly in the first place? (e.g. if it had too much leading padding?) That's the only situation I can think of where round-tripping this through asn1 wouldn't result in the exact same bytes as encoded in the serial number of the certificate.

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 parse certificates with crypto/x509's ParseCertificate which blows up on a cert with incorrect encoding. Example by @jcjones here https://play.golang.org/p/shRibwS6_4

if err != nil {
return
}
serial = fmt.Sprintf("%X", rawValue.Bytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

We've confirmed this 0-pads odd-length hex values? (e.g. if rawValue.Bytes is [0c, ff] this will result in "0CFF" and not "CFF"?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@autrilla autrilla merged commit 5bdb36f into mozilla:master Aug 10, 2017
@autrilla autrilla deleted the leading-zeroes branch August 10, 2017 20:51
rhysxevans added a commit to rhysxevans/blackbox_exporter that referenced this pull request Jan 2, 2025
`%x` - https://go.dev/play/p/Fylce70N2Zl
Leading Zeros - mozilla/tls-observatory#245

Signed-off-by: Rhys Evans <rhys.rr@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing leading zero in serial number
4 participants