-
Notifications
You must be signed in to change notification settings - Fork 107
/
file_signer.go
323 lines (275 loc) · 8.43 KB
/
file_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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Package file provides a PEM file backed signer.
package file
import (
"crypto/rand"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/pem"
)
const (
privateKeyPemType = "ED25519 PRIVATE KEY"
staticEntropyPemType = "STATIC ENTROPY"
filePerm = 0o600
// SignerName is the name used to identify the file backed signer.
SignerName = "file"
// StaticEntropySize is the size of the provided static entropy.
StaticEntropySize = 32
)
var (
_ signature.SignerFactoryCtor = NewFactory
_ signature.SignerFactory = (*Factory)(nil)
_ signature.Signer = (*Signer)(nil)
_ signature.VRFSigner = (*Signer)(nil)
_ signature.StaticEntropyProvider = (*Signer)(nil)
// FileEntityKey is the entity key filename.
FileEntityKey = "entity.pem"
// FileIdentityKey is the identity key filename.
FileIdentityKey = "identity.pem"
// FileP2PKey is the P2P key filename.
FileP2PKey = "p2p.pem"
// FileP2PStaticEntropy is the static P2P entropy filename.
FileP2PStaticEntropy = "p2p_entropy.pem"
// FileConsensusKey is the consensus key filename.
FileConsensusKey = "consensus.pem"
// FileVRFKey is the vrf key filename.
FileVRFKey = "vrf.pem"
rolePEMFiles = map[signature.SignerRole]string{
signature.SignerEntity: FileEntityKey,
signature.SignerNode: FileIdentityKey,
signature.SignerP2P: FileP2PKey,
signature.SignerConsensus: FileConsensusKey,
signature.SignerVRF: FileVRFKey,
}
)
// NewFactory creates a new factory with the specified roles, with the
// specified dataDir.
func NewFactory(config interface{}, roles ...signature.SignerRole) (signature.SignerFactory, error) {
dataDir, ok := config.(string)
if !ok {
return nil, errors.New("signature/signer/file: invalid file signer configuration provided")
}
return &Factory{
roles: append([]signature.SignerRole{}, roles...),
dataDir: dataDir,
}, nil
}
// Factory is a PEM file backed SignerFactory.
type Factory struct {
roles []signature.SignerRole
dataDir string
}
// EnsureRole ensures that the SignerFactory is configured for the given
// role.
func (fac *Factory) EnsureRole(role signature.SignerRole) error {
for _, v := range fac.roles {
if v == role {
return nil
}
}
return signature.ErrRoleMismatch
}
// Generate will generate and persist a new private key corresponding to the
// role, and return a Signer ready for use, using entropy from `rng`.
func (fac *Factory) Generate(role signature.SignerRole, rng io.Reader) (signature.Signer, error) {
if err := fac.EnsureRole(role); err != nil {
return nil, err
}
// Ensure that we aren't trying to overrwrite an existing key.
fn := rolePEMFiles[role]
fn = filepath.Join(fac.dataDir, fn)
f, err := os.Open(fn)
if err == nil {
f.Close()
return nil, errors.New("signature/signer/file: key already exists")
}
if !os.IsNotExist(err) {
return nil, err
}
// Generate a new private key.
_, privateKey, err := ed25519.GenerateKey(rng)
if err != nil {
return nil, err
}
// Persist the private key.
signer := &Signer{
privateKey: privateKey,
role: role,
}
buf, err := signer.marshalPEM()
if err != nil {
return nil, err
}
if err = ioutil.WriteFile(fn, buf, filePerm); err != nil {
return nil, err
}
switch role {
case signature.SignerP2P:
// Generate new static entropy for P2P signers.
if err = fac.generateStaticEntropy(FileP2PStaticEntropy, signer, rng); err != nil {
return nil, err
}
default:
}
return signer, nil
}
func (fac *Factory) generateStaticEntropy(fn string, signer *Signer, rng io.Reader) error {
if _, err := rng.Read(signer.staticEntropy[:]); err != nil {
return err
}
// Persist the entropy.
buf, err := signer.marshalStaticEntropyPEM()
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(fac.dataDir, fn), buf, filePerm)
}
// Load will load the private key corresponding to the role, and return a Signer
// ready for use.
func (fac *Factory) Load(role signature.SignerRole) (signature.Signer, error) {
if err := fac.EnsureRole(role); err != nil {
return nil, err
}
fn := rolePEMFiles[role]
return fac.doLoad(filepath.Join(fac.dataDir, fn), role)
}
// ForceLoad is evil and should be destroyed, however that requires
// fixing deployment, and the entity key for node registration mess.
func (fac *Factory) ForceLoad(fn string) (signature.Signer, error) {
return fac.doLoad(fn, signature.SignerUnknown)
}
func (fac *Factory) loadPEM(fn string) ([]byte, error) {
f, err := os.Open(fn)
if err != nil {
if os.IsNotExist(err) {
return nil, signature.ErrNotExist
}
return nil, err
}
defer f.Close()
// Ensure the PEM file has correct permissions.
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.Mode().Perm() != filePerm {
return nil, fmt.Errorf("signature/signer/file: invalid PEM file permissions %o on %s", fi.Mode(), fn)
}
return ioutil.ReadAll(f)
}
func (fac *Factory) doLoad(fn string, role signature.SignerRole) (signature.Signer, error) {
buf, err := fac.loadPEM(fn)
if err != nil {
return nil, err
}
var signer Signer
if err = signer.unmarshalPEM(buf); err != nil {
return nil, err
}
signer.role = role
switch role {
case signature.SignerP2P:
// Load static entropy for P2P signers.
err = fac.loadStaticEntropy(FileP2PStaticEntropy, &signer)
switch err {
case nil:
case signature.ErrNotExist:
// Old versions of the file signer didn't provide static entropy, generate some now.
if err = fac.generateStaticEntropy(FileP2PStaticEntropy, &signer, rand.Reader); err != nil {
return nil, err
}
default:
return nil, err
}
default:
}
return &signer, nil
}
func (fac *Factory) loadStaticEntropy(fn string, signer *Signer) error {
buf, err := fac.loadPEM(filepath.Join(fac.dataDir, fn))
if err != nil {
return err
}
return signer.unmarshalStaticEntropyPEM(buf)
}
// Signer is a PEM file backed Signer.
type Signer struct {
privateKey ed25519.PrivateKey
role signature.SignerRole
staticEntropy [StaticEntropySize]byte
}
// Public returns the PublicKey corresponding to the signer.
func (s *Signer) Public() signature.PublicKey {
var pk signature.PublicKey
_ = pk.UnmarshalBinary(s.privateKey.Public().(ed25519.PublicKey))
return pk
}
// ContextSign generates a signature with the private key over the context and
// message.
func (s *Signer) ContextSign(context signature.Context, message []byte) ([]byte, error) {
data, err := signature.PrepareSignerMessage(context, message)
if err != nil {
return nil, err
}
return ed25519.Sign(s.privateKey, data), nil
}
// String returns anything but the actual private key backing the Signer.
func (s *Signer) String() string {
return "[redacted private key]"
}
// Reset tears down the Signer and obliterates any sensitive state if any.
func (s *Signer) Reset() {
for idx := range s.privateKey {
s.privateKey[idx] = 0
}
}
// Prove generates a VRF proof with the private key over the alpha.
func (s *Signer) Prove(alphaString []byte) ([]byte, error) {
if s.role != signature.SignerVRF {
return nil, signature.ErrInvalidRole
}
return ecvrf.Prove_v10(s.privateKey, alphaString), nil
}
// StaticEntropy returns PrivateKeySize bytes of cryptographic entropy that
// is independent from the Signer's private key. The value of this entropy
// is constant for the lifespan of the signer's underlying key pair.
func (s *Signer) StaticEntropy() ([]byte, error) {
if s.role != signature.SignerP2P {
return nil, signature.ErrInvalidRole
}
return s.staticEntropy[:], nil
}
func (s *Signer) marshalPEM() ([]byte, error) {
return pem.Marshal(privateKeyPemType, s.privateKey[:])
}
func (s *Signer) unmarshalPEM(data []byte) error {
data, err := pem.Unmarshal(privateKeyPemType, data)
if err != nil {
return err
}
if len(data) != ed25519.PrivateKeySize {
return signature.ErrMalformedPrivateKey
}
s.privateKey = ed25519.PrivateKey(data)
return nil
}
func (s *Signer) marshalStaticEntropyPEM() ([]byte, error) {
return pem.Marshal(staticEntropyPemType, s.staticEntropy[:])
}
func (s *Signer) unmarshalStaticEntropyPEM(data []byte) error {
data, err := pem.Unmarshal(staticEntropyPemType, data)
if err != nil {
return err
}
if len(data) != StaticEntropySize {
return signature.ErrMalformedPrivateKey
}
copy(s.staticEntropy[:], data)
return nil
}