Implement ecdsa.VerifyASN1 to be compatible with go < 1.15

This commit is contained in:
Mariano Cano 2021-01-27 20:35:42 -08:00
parent d9da150a5f
commit 35bf9b787e

View file

@ -10,10 +10,13 @@ import (
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"math/big"
"reflect" "reflect"
"testing" "testing"
"github.com/smallstep/certificates/kms/apiv1" "github.com/smallstep/certificates/kms/apiv1"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/crypto/cryptobyte/asn1"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
@ -478,7 +481,7 @@ func TestPKCS11_CreateSigner(t *testing.T) {
} }
case apiv1.ECDSAWithSHA256, apiv1.ECDSAWithSHA384, apiv1.ECDSAWithSHA512: case apiv1.ECDSAWithSHA256, apiv1.ECDSAWithSHA384, apiv1.ECDSAWithSHA512:
pub := got.Public().(*ecdsa.PublicKey) pub := got.Public().(*ecdsa.PublicKey)
if !ecdsa.VerifyASN1(pub, digest, sig) { if !VerifyASN1(pub, digest, sig) {
t.Error("ecdsa.VerifyASN1() failed") t.Error("ecdsa.VerifyASN1() failed")
} }
default: default:
@ -642,3 +645,21 @@ func TestPKCS11_StoreCertificate(t *testing.T) {
}) })
} }
} }
// VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the
// public key, pub. Its return value records whether the signature is valid.
func VerifyASN1(pub *ecdsa.PublicKey, hash, sig []byte) bool {
var (
r, s = &big.Int{}, &big.Int{}
inner cryptobyte.String
)
input := cryptobyte.String(sig)
if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
input.Empty() ||
inner.ReadASN1Integer(r) ||
inner.ReadASN1Integer(s) ||
inner.Empty() {
return false
}
return ecdsa.Verify(pub, hash, r, s)
}