-
Notifications
You must be signed in to change notification settings - Fork 670
/
verify.go
94 lines (85 loc) · 2.89 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package staking
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/x509"
"errors"
"fmt"
)
var (
ErrUnsupportedAlgorithm = errors.New("staking: cannot verify signature: unsupported algorithm")
ErrPublicKeyAlgoMismatch = errors.New("staking: signature algorithm specified different public key type")
ErrInvalidECDSAPublicKey = errors.New("staking: invalid ECDSA public key")
ErrECDSAVerificationFailure = errors.New("staking: ECDSA verification failure")
)
// CheckSignature verifies that the signature is a valid signature over signed
// from the certificate.
//
// Ref: https://github.com/golang/go/blob/go1.19.12/src/crypto/x509/x509.go#L793-L797
// Ref: https://github.com/golang/go/blob/go1.19.12/src/crypto/x509/x509.go#L816-L879
func CheckSignature(cert *Certificate, msg []byte, signature []byte) error {
hasher := crypto.SHA256.New()
_, err := hasher.Write(msg)
if err != nil {
return err
}
hashed := hasher.Sum(nil)
switch pub := cert.PublicKey.(type) {
case *rsa.PublicKey:
return rsa.VerifyPKCS1v15(pub, crypto.SHA256, hashed, signature)
case *ecdsa.PublicKey:
if !ecdsa.VerifyASN1(pub, hashed, signature) {
return ErrECDSAVerificationFailure
}
return nil
default:
return ErrUnsupportedAlgorithm
}
}
// ValidateCertificate verifies that this certificate conforms to the required
// staking format assuming that it was already able to be parsed.
//
// TODO: Remove after v1.11.x activates.
func ValidateCertificate(cert *Certificate) error {
if len(cert.Raw) > MaxCertificateLen {
return ErrCertificateTooLarge
}
pubkeyAlgo, ok := signatureAlgorithmVerificationDetails[cert.SignatureAlgorithm]
if !ok {
return ErrUnsupportedAlgorithm
}
switch pub := cert.PublicKey.(type) {
case *rsa.PublicKey:
if pubkeyAlgo != x509.RSA {
return signaturePublicKeyAlgoMismatchError(pubkeyAlgo, pub)
}
if bitLen := pub.N.BitLen(); bitLen != allowedRSALargeModulusLen && bitLen != allowedRSASmallModulusLen {
return fmt.Errorf("%w: %d", ErrUnsupportedRSAModulusBitLen, bitLen)
}
if pub.N.Bit(0) == 0 {
return ErrRSAModulusIsEven
}
if pub.E != allowedRSAPublicExponentValue {
return fmt.Errorf("%w: %d", ErrUnsupportedRSAPublicExponent, pub.E)
}
return nil
case *ecdsa.PublicKey:
if pubkeyAlgo != x509.ECDSA {
return signaturePublicKeyAlgoMismatchError(pubkeyAlgo, pub)
}
if pub.Curve != elliptic.P256() {
return ErrInvalidECDSAPublicKey
}
return nil
default:
return ErrUnsupportedAlgorithm
}
}
// Ref: https://github.com/golang/go/blob/go1.19.12/src/crypto/x509/x509.go#L812-L814
func signaturePublicKeyAlgoMismatchError(expectedPubKeyAlgo x509.PublicKeyAlgorithm, pubKey any) error {
return fmt.Errorf("%w: expected an %s public key, but have public key of type %T", ErrPublicKeyAlgoMismatch, expectedPubKeyAlgo, pubKey)
}