-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
keyring.go
164 lines (147 loc) · 3.93 KB
/
keyring.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
package openpgp
import (
"context"
"fmt"
"os"
"strings"
"golang.org/x/crypto/openpgp"
"github.com/pkg/errors"
)
// ListPublicKeyIDs does nothing
func (g *GPG) ListPublicKeyIDs(context.Context) ([]string, error) {
if g.pubring == nil {
return nil, fmt.Errorf("pubring is not initialized")
}
ids := listKeyIDs(g.pubring)
if g.secring != nil {
ids = append(ids, listKeyIDs(g.secring)...)
}
return ids, nil
}
// FindPublicKeys does nothing
func (g *GPG) FindPublicKeys(ctx context.Context, keys ...string) ([]string, error) {
kl, err := g.ListPublicKeyIDs(ctx)
if err != nil {
return nil, err
}
for i, key := range keys {
if strings.HasPrefix(key, "0x") {
key = strings.TrimPrefix(key, "0x")
}
keys[i] = strings.ToUpper(key)
}
matches := make([]string, 0, len(keys))
for _, key := range kl {
for _, needle := range keys {
if strings.HasSuffix(key, needle) {
matches = append(matches, key)
}
}
}
if m, err := g.FindPrivateKeys(ctx, keys...); err == nil {
matches = append(matches, m...)
}
return matches, nil
}
// ListPrivateKeyIDs does nothing
func (g *GPG) ListPrivateKeyIDs(context.Context) ([]string, error) {
if g.secring == nil {
return nil, fmt.Errorf("secring is not initialized")
}
return listKeyIDs(g.secring), nil
}
// FindPrivateKeys does nothing
func (g *GPG) FindPrivateKeys(ctx context.Context, keys ...string) ([]string, error) {
kl, err := g.ListPrivateKeyIDs(ctx)
if err != nil {
return nil, err
}
for i, key := range keys {
if strings.HasPrefix(key, "0x") {
key = strings.TrimPrefix(key, "0x")
}
keys[i] = strings.ToUpper(key)
}
matches := make([]string, 0, len(keys))
for _, key := range kl {
for _, needle := range keys {
if strings.HasSuffix(key, needle) {
matches = append(matches, key)
}
}
}
return matches, nil
}
func (g *GPG) savePubring() error {
pubfnTmp := g.pubfn + ".tmp"
if err := g.writePubring(pubfnTmp); err != nil {
os.Remove(pubfnTmp)
return errors.Wrapf(err, "failed to write pubring")
}
return os.Rename(pubfnTmp, g.pubfn)
}
func (g *GPG) writePubring(fn string) error {
fh, err := os.OpenFile(fn, os.O_WRONLY, 0644)
if err != nil {
return errors.Wrapf(err, "unable to save pubring, failed to open file: %s", err)
}
defer fh.Close()
for _, e := range g.pubring {
if err := e.Serialize(fh); err != nil {
return err
}
}
return nil
}
func (g *GPG) saveSecring() error {
secfnTmp := g.secfn + ".tmp"
if err := g.writeSecring(secfnTmp); err != nil {
os.Remove(secfnTmp)
return errors.Wrapf(err, "failed to write secring")
}
return os.Rename(secfnTmp, g.secfn)
}
func (g *GPG) writeSecring(fn string) error {
fh, err := os.OpenFile(fn, os.O_WRONLY, 0644)
if err != nil {
return errors.Wrapf(err, "unable to save secring, failed to open file: %s", err)
}
defer fh.Close()
for _, e := range g.secring {
if err := e.SerializePrivate(fh, nil); err != nil {
return err
}
}
return nil
}
// revive:disable
// KeysById implements openpgp.Keyring
func (g *GPG) KeysById(id uint64) []openpgp.Key {
return append(g.secring.KeysById(id), g.pubring.KeysById(id)...)
}
// KeysByIdUsage implements openpgp.Keyring
func (g *GPG) KeysByIdUsage(id uint64, requiredUsage byte) []openpgp.Key {
return append(g.secring.KeysByIdUsage(id, requiredUsage), g.pubring.KeysByIdUsage(id, requiredUsage)...)
}
// revive:enable
// DecryptionKeys implements openpgp.Keyring
func (g *GPG) DecryptionKeys() []openpgp.Key {
return append(g.secring.DecryptionKeys(), g.pubring.DecryptionKeys()...)
}
// SigningKeys returns a list of signing keys
func (g *GPG) SigningKeys() []openpgp.Key {
keys := []openpgp.Key{}
for _, e := range g.secring {
for _, subKey := range e.Subkeys {
if subKey.PrivateKey != nil && (!subKey.Sig.FlagsValid || subKey.Sig.FlagSign) {
keys = append(keys, openpgp.Key{
Entity: e,
PublicKey: subKey.PublicKey,
PrivateKey: subKey.PrivateKey,
SelfSignature: subKey.Sig,
})
}
}
}
return keys
}