-
Notifications
You must be signed in to change notification settings - Fork 6
/
keyservice.go
219 lines (204 loc) · 6.11 KB
/
keyservice.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
package gpg
//go:generate mockgen -source=keyservice.go -destination=../../mock/mock_keyservice.go -package=mock
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"fmt"
openpgpecdsa "github.com/ProtonMail/go-crypto/openpgp/ecdsa"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"go.uber.org/zap"
)
// retries is the passphrase attempt limit when decrypting GPG keyfiles
const retries = 3
// PINEntryService provides an interface to talk to a pinentry program.
type PINEntryService interface {
GetPassphrase(string, string, int) ([]byte, error)
}
type privateKeyfile struct {
uid *packet.UserId
keys []*packet.PrivateKey
}
// KeyService implements an interface for getting cryptographic keys from
// keyfiles on disk.
type KeyService struct {
// cache passphrases used for keyfile decryption
passphrases [][]byte
privKeys []privateKeyfile
log *zap.Logger
pinentry PINEntryService
}
// New returns a keyservice initialised with keys found at path.
// Path can be a file or directory.
func New(l *zap.Logger, pe PINEntryService, path string) *KeyService {
p, err := keyfilePrivateKeys(path)
if err != nil {
l.Info("couldn't load keyfiles", zap.String("path", path), zap.Error(err))
}
return &KeyService{
privKeys: p,
log: l,
pinentry: pe,
}
}
// Name returns the name of the keyservice.
func (*KeyService) Name() string {
return "GPG Keyfile"
}
// doDecrypt prompts for a passphrase via pinentry and uses the passphrase to
// decrypt the given private key
func (g *KeyService) doDecrypt(k *packet.PrivateKey, uid string) error {
var pass []byte
var err error
for i := 0; i < retries; i++ {
pass, err = g.pinentry.GetPassphrase(
fmt.Sprintf("UserID: %s\rFingerprint: %X %X %X %X", uid,
k.Fingerprint[:5], k.Fingerprint[5:10], k.Fingerprint[10:15],
k.Fingerprint[15:]),
uid, retries-i)
if err != nil {
return fmt.Errorf("couldn't get passphrase for key %s: %v",
k.KeyIdString(), err)
}
if err = k.Decrypt(pass); err == nil {
g.passphrases = append(g.passphrases, pass)
return nil
}
}
return fmt.Errorf("couldn't decrypt key %s: %v", k.KeyIdString(), err)
}
// decryptPrivateKey decrypts the given private key.
// Returns nil if successful, or an error if the key could not be decrypted.
func (g *KeyService) decryptPrivateKey(k *packet.PrivateKey, uid string) error {
var err error
if k.Encrypted {
// try existing passphrases
for _, pass := range g.passphrases {
if err = k.Decrypt(pass); err == nil {
g.log.Debug("decrypted using cached passphrase",
zap.String("fingerprint", k.KeyIdString()))
break
}
}
}
if k.Encrypted {
if err := g.doDecrypt(k, uid); err != nil {
return err
}
g.log.Debug("decrypted using passphrase",
zap.String("fingerprint", k.KeyIdString()))
}
return nil
}
// getRSAKey returns a matching private RSA key if the keygrip matches. If a key
// is returned err will be nil. If no key is found, both values may be nil.
func (g *KeyService) getRSAKey(keygrip []byte) (*rsa.PrivateKey, error) {
for _, pk := range g.privKeys {
for _, k := range pk.keys {
pubKey, ok := k.PublicKey.PublicKey.(*rsa.PublicKey)
if !ok {
continue
}
pubKeygrip, err := keygripRSA(pubKey)
if err != nil {
return nil, fmt.Errorf("couldn't get RSA keygrip: %v", err)
}
if !bytes.Equal(keygrip, pubKeygrip) {
continue
}
err = g.decryptPrivateKey(k,
fmt.Sprintf("%s (%s) <%s>",
pk.uid.Name, pk.uid.Comment, pk.uid.Email))
if err != nil {
return nil, err
}
privKey, ok := k.PrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("not an RSA key %s: %v",
k.KeyIdString(), err)
}
return privKey, nil
}
}
return nil, nil
}
// getECDSAKey returns a matching private ECDSA key if the keygrip matches. If
// a key is returned err will be nil. If no key is found, both values will be
// nil.
func (g *KeyService) getECDSAKey(keygrip []byte) (*ecdsa.PrivateKey, error) {
for _, pk := range g.privKeys {
for _, k := range pk.keys {
openpgpPubKey, ok := k.PublicKey.PublicKey.(*openpgpecdsa.PublicKey)
if !ok {
continue
}
pubKey, err := ecdsaPublicKey(openpgpPubKey)
if err != nil {
return nil,
fmt.Errorf("couldn't convert openpgp to stdlib ecdsa public key: %v", err)
}
pubKeygrip, err := KeygripECDSA(pubKey)
if err != nil {
return nil, fmt.Errorf("couldn't get ECDSA keygrip: %v", err)
}
if !bytes.Equal(keygrip, pubKeygrip) {
continue
}
err = g.decryptPrivateKey(k,
fmt.Sprintf("%s (%s) <%s>",
pk.uid.Name, pk.uid.Comment, pk.uid.Email))
if err != nil {
return nil, err
}
openpgpPrivKey, ok := k.PrivateKey.(*openpgpecdsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("not an ECDSA key %s: %v",
k.KeyIdString(), err)
}
privKey, err := ecdsaPrivateKey(openpgpPrivKey)
if err != nil {
return nil,
fmt.Errorf("couldn't convert openpgp to stdlib ecdsa private key: %v", err)
}
return privKey, nil
}
}
return nil, nil
}
// GetSigner returns a crypto.Signer associated with the given keygrip.
func (g *KeyService) GetSigner(keygrip []byte) (crypto.Signer, error) {
rsaPrivKey, err := g.getRSAKey(keygrip)
if err != nil {
return nil, fmt.Errorf("couldn't getRSAKey: %v", err)
}
if rsaPrivKey != nil {
return &RSAKey{rsa: rsaPrivKey}, nil
}
ecdsaPrivKey, err := g.getECDSAKey(keygrip)
if err != nil {
return nil, fmt.Errorf("couldn't getECDSAKey: %v", err)
}
if ecdsaPrivKey != nil {
return ecdsaPrivKey, nil
}
return nil, fmt.Errorf("couldn't get signer for keygrip %X", keygrip)
}
// GetDecrypter returns a crypto.Decrypter associated with the given keygrip.
func (g *KeyService) GetDecrypter(keygrip []byte) (crypto.Decrypter, error) {
rsaPrivKey, err := g.getRSAKey(keygrip)
if err != nil {
return nil, fmt.Errorf("couldn't getRSAKey: %v", err)
}
if rsaPrivKey != nil {
return &RSAKey{rsa: rsaPrivKey}, nil
}
ecdsaPrivKey, err := g.getECDSAKey(keygrip)
if err != nil {
return nil, fmt.Errorf("couldn't getECDSAKey: %v", err)
}
if ecdsaPrivKey != nil {
return &ECDHKey{ecdsa: ecdsaPrivKey}, nil
}
return nil, fmt.Errorf("couldn't get decrypter for keygrip %X", keygrip)
}