-
Notifications
You must be signed in to change notification settings - Fork 15
/
signing.go
210 lines (183 loc) · 5.3 KB
/
signing.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package signing
import (
"crypto"
"crypto/dsa" //nolint:staticcheck
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"encoding/binary"
"github.com/google/uuid"
"github.com/michaelquigley/pfxlog"
"github.com/pkg/errors"
"math/big"
"reflect"
)
const (
Format1Rsa byte = 1
Format2Dsa byte = 2
Format3Ecdsa byte = 3
)
func AssertIdentityWithSecret(privateKey interface{}) ([]byte, error) {
var result []byte
nonceUUID := uuid.New()
nonce := nonceUUID[:]
hashF := crypto.SHA512.New()
_, _ = hashF.Write(nonce)
nonce = hashF.Sum(nil)
if rsaPk, ok := privateKey.(*rsa.PrivateKey); ok {
result = append(result, Format1Rsa)
result = appendSizedSlice(result, nonce)
signature, err := rsa.SignPSS(rand.Reader, rsaPk, crypto.SHA512, nonce, nil)
if err != nil {
return nil, err
}
result = appendSizedSlice(result, signature)
return result, nil
}
if dsaPk, ok := privateKey.(*dsa.PrivateKey); ok {
result = append(result, Format2Dsa)
result = appendSizedSlice(result, nonce)
r, s, err := dsa.Sign(rand.Reader, dsaPk, nonce[:])
if err != nil {
return nil, err
}
result = appendSizedSlice(result, r.Bytes())
result = appendSizedSlice(result, s.Bytes())
return result, nil
}
if ecdsaPk, ok := privateKey.(*ecdsa.PrivateKey); ok {
result = append(result, Format3Ecdsa)
result = appendSizedSlice(result, nonce)
r, s, err := ecdsa.Sign(rand.Reader, ecdsaPk, nonce[:])
if err != nil {
return nil, err
}
result = appendSizedSlice(result, r.Bytes())
result = appendSizedSlice(result, s.Bytes())
return result, nil
}
return nil, errors.Errorf("unhandled private key type %v", reflect.TypeOf(privateKey))
}
func GetVerifier(val []byte) (Verifier, error) {
if len(val) < 1 {
return nil, errors.Errorf("can't verify empty identity secret")
}
secretType := val[0]
val = val[1:]
nonce, val, err := consumeBytesValue("nonce", val)
if err != nil {
return nil, err
}
if secretType == Format1Rsa {
signature, val, err := consumeBytesValue("signature", val)
if err != nil {
return nil, err
}
if len(val) != 0 {
return nil, errors.Errorf("encoding error: still %v unconsumed bytes remaining in identity secret", len(val))
}
return &rsaVerifier{
nonce: nonce,
signature: signature,
}, nil
}
if secretType == Format2Dsa {
rBytes, val, err := consumeBytesValue("dsa signature r component", val)
if err != nil {
return nil, err
}
sBytes, val, err := consumeBytesValue("dsa signature s component", val)
if err != nil {
return nil, err
}
if len(val) != 0 {
return nil, errors.Errorf("encoding error: still %v unconsumed bytes remaining in identity secret", len(val))
}
r := big.NewInt(0).SetBytes(rBytes)
s := big.NewInt(0).SetBytes(sBytes)
return &dsaVerifier{
nonce: nonce,
r: r,
s: s,
}, nil
}
if secretType == Format3Ecdsa {
rBytes, val, err := consumeBytesValue("ecdsa signature r component", val)
if err != nil {
return nil, err
}
sBytes, val, err := consumeBytesValue("ecdsa signature s component", val)
if err != nil {
return nil, err
}
if len(val) != 0 {
return nil, errors.Errorf("encoding error: still %v unconsumed bytes remaining in identity secret", len(val))
}
r := big.NewInt(0).SetBytes(rBytes)
s := big.NewInt(0).SetBytes(sBytes)
return &ecdsaVerifier{
nonce: nonce,
r: r,
s: s,
}, nil
}
return nil, errors.Errorf("unsupported identity secret format %v", secretType)
}
type Verifier interface {
Verify(publicKey interface{}) bool
}
type rsaVerifier struct {
nonce []byte
signature []byte
}
func (v *rsaVerifier) Verify(publicKey interface{}) bool {
if rsaPubKey, ok := publicKey.(*rsa.PublicKey); ok {
return nil == rsa.VerifyPSS(rsaPubKey, crypto.SHA512, v.nonce, v.signature, nil)
}
pfxlog.Logger().Warnf("incorrect public key type. expected rsa.PublicKey, but was %v", reflect.TypeOf(publicKey))
return false
}
type dsaVerifier struct {
nonce []byte
r *big.Int
s *big.Int
}
func (v *dsaVerifier) Verify(publicKey interface{}) bool {
if dsaPubKey, ok := publicKey.(*dsa.PublicKey); ok {
return dsa.Verify(dsaPubKey, v.nonce, v.r, v.s)
}
pfxlog.Logger().Warnf("incorrect public key type. expected dsa.PublicKey, but was %v", reflect.TypeOf(publicKey))
return false
}
type ecdsaVerifier struct {
nonce []byte
r *big.Int
s *big.Int
}
func (v *ecdsaVerifier) Verify(publicKey interface{}) bool {
if ecdsaPubKey, ok := publicKey.(*ecdsa.PublicKey); ok {
return ecdsa.Verify(ecdsaPubKey, v.nonce, v.r, v.s)
}
pfxlog.Logger().Warnf("incorrect public key type. expected ecdsa.PublicKey, but was %v", reflect.TypeOf(publicKey))
return false
}
func appendSizedSlice(slice []byte, val []byte) []byte {
size := len(val)
sizeBuf := make([]byte, 4)
binary.LittleEndian.PutUint32(sizeBuf, uint32(size))
slice = append(slice, sizeBuf...)
return append(slice, val...)
}
func consumeBytesValue(name string, val []byte) ([]byte, []byte, error) {
if len(val) < 4 {
return nil, nil, errors.Errorf("encoding error, not enough data to read len(%v)", name)
}
size := binary.LittleEndian.Uint32(val[:4])
val = val[4:]
if len(val) < int(size) {
return nil, nil, errors.Errorf("encoding error, not enough data to read %v, which has size %v. Only %v bytes available", name, size, len(val))
}
result := val[0:size]
val = val[size:]
return result, val, nil
}