certutil.ParseHexFormatted fails parsing 80 hex and above (#18018)

- Switch to using ParseUint of 8 bits to parse the hex values properly
   as ParseInt limited to 8 bits will only handle values up to 127
   decimal or 7F.
This commit is contained in:
Steven Clark 2022-11-17 15:04:37 -05:00 committed by GitHub
parent aa74bd7ed7
commit 610a4ede82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -26,7 +26,7 @@ import (
// Tests converting back and forth between a CertBundle and a ParsedCertBundle.
//
// Also tests the GetSubjKeyID, GetHexFormatted, and
// Also tests the GetSubjKeyID, GetHexFormatted, ParseHexFormatted and
// ParsedCertBundle.getSigner functions.
func TestCertBundleConversion(t *testing.T) {
cbuts := []*CertBundle{
@ -245,6 +245,10 @@ func compareCertBundleToParsedCertBundle(cbut *CertBundle, pcbut *ParsedCertBund
return fmt.Errorf("bundle serial number does not match")
}
if !bytes.Equal(pcbut.Certificate.SerialNumber.Bytes(), ParseHexFormatted(cb.SerialNumber, ":")) {
return fmt.Errorf("failed re-parsing hex formatted number %s", cb.SerialNumber)
}
switch {
case len(pcbut.CAChain) > 0 && len(cb.CAChain) == 0:
return fmt.Errorf("parsed bundle ca chain has certs when cert bundle does not")

View File

@ -100,13 +100,13 @@ func GetHexFormatted(buf []byte, sep string) string {
func ParseHexFormatted(in, sep string) []byte {
var ret bytes.Buffer
var err error
var inBits int64
var inBits uint64
inBytes := strings.Split(in, sep)
for _, inByte := range inBytes {
if inBits, err = strconv.ParseInt(inByte, 16, 8); err != nil {
if inBits, err = strconv.ParseUint(inByte, 16, 8); err != nil {
return nil
}
ret.WriteByte(byte(inBits))
ret.WriteByte(uint8(inBits))
}
return ret.Bytes()
}