forked from welthee/go-ethereum-aws-kms-tx-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
177 lines (143 loc) · 4.59 KB
/
signer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ethawskmssigner
import (
"bytes"
"crypto/ecdsa"
"encoding/asn1"
"encoding/hex"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/pkg/errors"
"math/big"
)
const awsKmsSignOperationMessageType = "DIGEST"
const awsKmsSignOperationSigningAlgorithm = "ECDSA_SHA_256"
var keyCache = newPubKeyCache()
var secp256k1N = crypto.S256().Params().N
var secp256k1HalfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
type asn1EcPublicKey struct {
EcPublicKeyInfo asn1EcPublicKeyInfo
PublicKey asn1.BitString
}
type asn1EcPublicKeyInfo struct {
Algorithm asn1.ObjectIdentifier
Parameters asn1.ObjectIdentifier
}
type asn1EcSig struct {
R asn1.RawValue
S asn1.RawValue
}
func NewAwsKmsTransactorWithChainID(svc *kms.KMS, keyId string, chainID *big.Int) (*bind.TransactOpts, error) {
pubkey, err := GetPubKey(svc, keyId)
if err != nil {
return nil, err
}
pubKeyBytes := secp256k1.S256().Marshal(pubkey.X, pubkey.Y)
keyAddr := crypto.PubkeyToAddress(*pubkey)
if chainID == nil {
return nil, bind.ErrNoChainID
}
signer := types.LatestSignerForChainID(chainID)
signerFn := func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, bind.ErrNotAuthorized
}
txHashBytes := signer.Hash(tx).Bytes()
rBytes, sBytes, err := getSignatureFromKms(svc, keyId, txHashBytes)
if err != nil {
return nil, err
}
// Adjust S value from signature according to Ethereum standard
sBigInt := new(big.Int).SetBytes(sBytes)
if sBigInt.Cmp(secp256k1HalfN) > 0 {
sBytes = new(big.Int).Sub(secp256k1N, sBigInt).Bytes()
}
signature, err := getEthereumSignature(pubKeyBytes, txHashBytes, rBytes, sBytes)
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
}
return &bind.TransactOpts{
From: keyAddr,
Signer: signerFn,
}, nil
}
func getPublicKeyDerBytesFromKMS(svc *kms.KMS, keyId string) ([]byte, error) {
getPubKeyOutput, err := svc.GetPublicKey(&kms.GetPublicKeyInput{
KeyId: aws.String(keyId),
})
if err != nil {
return nil, errors.Wrapf(err, "can not get public key from KMS for KeyId=%s", keyId)
}
var asn1pubk asn1EcPublicKey
_, err = asn1.Unmarshal(getPubKeyOutput.PublicKey, &asn1pubk)
if err != nil {
return nil, errors.Wrapf(err, "can not parse asn1 public key for KeyId=%s", keyId)
}
return asn1pubk.PublicKey.Bytes, nil
}
func getSignatureFromKms(svc *kms.KMS, keyId string, txHashBytes []byte) ([]byte, []byte, error) {
signInput := &kms.SignInput{
KeyId: aws.String(keyId),
SigningAlgorithm: aws.String(awsKmsSignOperationSigningAlgorithm),
MessageType: aws.String(awsKmsSignOperationMessageType),
Message: txHashBytes,
}
signOutput, err := svc.Sign(signInput)
if err != nil {
return nil, nil, err
}
var sigAsn1 asn1EcSig
_, err = asn1.Unmarshal(signOutput.Signature, &sigAsn1)
if err != nil {
return nil, nil, err
}
return sigAsn1.R.Bytes, sigAsn1.S.Bytes, nil
}
func getEthereumSignature(expectedPublicKeyBytes []byte, txHash []byte, r []byte, s []byte) ([]byte, error) {
rsSignature := append(adjustSignatureLength(r), adjustSignatureLength(s)...)
signature := append(rsSignature, []byte{0}...)
recoveredPublicKeyBytes, err := crypto.Ecrecover(txHash, signature)
if err != nil {
return nil, err
}
if hex.EncodeToString(recoveredPublicKeyBytes) != hex.EncodeToString(expectedPublicKeyBytes) {
signature = append(rsSignature, []byte{1}...)
recoveredPublicKeyBytes, err = crypto.Ecrecover(txHash, signature)
if err != nil {
return nil, err
}
if hex.EncodeToString(recoveredPublicKeyBytes) != hex.EncodeToString(expectedPublicKeyBytes) {
return nil, errors.New("can not reconstruct public key from sig")
}
}
return signature, nil
}
func GetPubKey(svc *kms.KMS, keyId string) (*ecdsa.PublicKey, error) {
pubkey := keyCache.Get(keyId)
if pubkey == nil {
pubKeyBytes, err := getPublicKeyDerBytesFromKMS(svc, keyId)
if err != nil {
return nil, err
}
pubkey, err = crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, errors.Wrap(err, "can not construct secp256k1 public key from key bytes")
}
keyCache.Add(keyId, pubkey)
}
return pubkey, nil
}
func adjustSignatureLength(buffer []byte) []byte {
buffer = bytes.TrimLeft(buffer, "\x00")
for len(buffer) < 32 {
zeroBuf := []byte{0}
buffer = append(zeroBuf, buffer...)
}
return buffer
}