/
file_signer.go
221 lines (187 loc) · 5.5 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
// Package file provides a PEM file backed signer.
package file
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/oasisprotocol/ed25519"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/pem"
)
const (
privateKeyPemType = "ED25519 PRIVATE KEY"
filePerm = 0o600
// SignerName is the name used to identify the file backed signer.
SignerName = "file"
)
var (
_ signature.SignerFactoryCtor = NewFactory
_ signature.SignerFactory = (*Factory)(nil)
_ signature.Signer = (*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"
// FileConsensusKey is the consensus key filename.
FileConsensusKey = "consensus.pem"
rolePEMFiles = map[signature.SignerRole]string{
signature.SignerEntity: FileEntityKey,
signature.SignerNode: FileIdentityKey,
signature.SignerP2P: FileP2PKey,
signature.SignerConsensus: FileConsensusKey,
}
)
// 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
}
return signer, nil
}
// 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) doLoad(fn string, role signature.SignerRole) (signature.Signer, 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)
}
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var signer Signer
if err = signer.unmarshalPEM(buf); err != nil {
return nil, err
}
signer.role = role
return &signer, nil
}
// Signer is a PEM file backed Signer.
type Signer struct {
privateKey ed25519.PrivateKey
role signature.SignerRole
}
// 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
}
}
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
}