Skip to content

Commit

Permalink
certutil: select appropriate hash algorithm for ECDSA signature
Browse files Browse the repository at this point in the history
Select the appropriate signature algorithm for certificates signed
with an ECDSA private key.

The algorithm is selected based on the curve:

- P-256 -> x509.ECDSAWithSHA256
- P-384 -> x509.ECDSAWithSHA384
- P-521 -> x509.ECDSAWithSHA512
- Other -> x509.ECDSAWithSHA256

fixes #11006
  • Loading branch information
oncilla committed Mar 26, 2021
1 parent ffde6c6 commit 3541ac5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions sdk/helper/certutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func createCertificate(data *CreationBundle, randReader io.Reader) (*ParsedCertB
case RSAPrivateKey:
certTemplate.SignatureAlgorithm = x509.SHA256WithRSA
case ECPrivateKey:
certTemplate.SignatureAlgorithm = x509.ECDSAWithSHA256
certTemplate.SignatureAlgorithm = selectSignatureAlgorithmForECDSA(data.SigningBundle.PrivateKey.Public())
}

caCert := data.SigningBundle.Certificate
Expand All @@ -620,7 +620,7 @@ func createCertificate(data *CreationBundle, randReader io.Reader) (*ParsedCertB
case "rsa":
certTemplate.SignatureAlgorithm = x509.SHA256WithRSA
case "ec":
certTemplate.SignatureAlgorithm = x509.ECDSAWithSHA256
certTemplate.SignatureAlgorithm = selectSignatureAlgorithmForECDSA(result.PrivateKey.Public())
}

certTemplate.AuthorityKeyId = subjKeyID
Expand Down Expand Up @@ -655,6 +655,23 @@ func createCertificate(data *CreationBundle, randReader io.Reader) (*ParsedCertB
return result, nil
}

func selectSignatureAlgorithmForECDSA(pub crypto.PublicKey) x509.SignatureAlgorithm {
key, ok := pub.(*ecdsa.PublicKey)
if !ok {
return x509.ECDSAWithSHA256
}
switch key.Curve {
case elliptic.P224(), elliptic.P256():
return x509.ECDSAWithSHA256
case elliptic.P384():
return x509.ECDSAWithSHA384
case elliptic.P521():
return x509.ECDSAWithSHA512
default:
return x509.ECDSAWithSHA256
}
}

var oidExtensionBasicConstraints = []int{2, 5, 29, 19}

// CreateCSR creates a CSR with the default rand.Reader to
Expand Down

0 comments on commit 3541ac5

Please sign in to comment.