-
Notifications
You must be signed in to change notification settings - Fork 670
/
verify.go
105 lines (93 loc) · 3.34 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
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2019-2023, 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"
"github.com/ava-labs/avalanchego/utils/units"
)
// MaxRSAKeyBitLen is the maximum RSA key size in bits that we are willing to
// parse.
//
// https://github.com/golang/go/blob/go1.19.12/src/crypto/tls/handshake_client.go#L860-L862
const (
MaxCertificateLen = 16 * units.KiB
MaxRSAKeyByteLen = units.KiB
MaxRSAKeyBitLen = 8 * MaxRSAKeyByteLen
)
var (
ErrCertificateTooLarge = fmt.Errorf("staking: certificate length is greater than %d", MaxCertificateLen)
ErrUnsupportedAlgorithm = errors.New("staking: cannot verify signature: unsupported algorithm")
ErrPublicKeyAlgoMismatch = errors.New("staking: signature algorithm specified different public key type")
ErrInvalidRSAPublicKey = errors.New("staking: invalid RSA public key")
ErrInvalidECDSAPublicKey = errors.New("staking: invalid ECDSA public key")
ErrECDSAVerificationFailure = errors.New("staking: ECDSA verification failure")
ErrED25519VerificationFailure = errors.New("staking: Ed25519 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 {
if err := ValidateCertificate(cert); err != nil {
return err
}
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.
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 > MaxRSAKeyBitLen {
return fmt.Errorf("%w: bitLen=%d > maxBitLen=%d", ErrInvalidRSAPublicKey, bitLen, MaxRSAKeyBitLen)
}
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)
}