forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
keyring.go
255 lines (199 loc) · 4.5 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
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
package proton
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io"
"strings"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/bradenaw/juniper/xslices"
)
func ExtractSignatures(kr *crypto.KeyRing, arm string) ([]Signature, error) {
entities := xslices.Map(kr.GetKeys(), func(key *crypto.Key) *openpgp.Entity {
return key.GetEntity()
})
p, err := armor.Decode(strings.NewReader(arm))
if err != nil {
return nil, err
}
msg, err := openpgp.ReadMessage(p.Body, openpgp.EntityList(entities), nil, nil)
if err != nil {
return nil, err
}
if _, err := io.ReadAll(msg.UnverifiedBody); err != nil {
return nil, err
}
if !msg.IsSigned {
return nil, nil
}
var signatures []Signature
for _, signature := range msg.UnverifiedSignatures {
buf := new(bytes.Buffer)
if err := signature.Serialize(buf); err != nil {
return nil, err
}
signatures = append(signatures, Signature{
Hash: signature.Hash.String(),
Data: crypto.NewPGPSignature(buf.Bytes()),
})
}
return signatures, nil
}
type Key struct {
ID string
PrivateKey []byte
Token string
Signature string
Primary Bool
Active Bool
Flags KeyState
}
func (key *Key) UnmarshalJSON(data []byte) error {
type Alias Key
aux := &struct {
PrivateKey string
*Alias
}{
Alias: (*Alias)(key),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
privKey, err := crypto.NewKeyFromArmored(aux.PrivateKey)
if err != nil {
return err
}
raw, err := privKey.Serialize()
if err != nil {
return err
}
key.PrivateKey = raw
return nil
}
func (key Key) MarshalJSON() ([]byte, error) {
privKey, err := crypto.NewKey(key.PrivateKey)
if err != nil {
return nil, err
}
arm, err := privKey.Armor()
if err != nil {
return nil, err
}
type Alias Key
aux := &struct {
PrivateKey string
*Alias
}{
PrivateKey: arm,
Alias: (*Alias)(&key),
}
return json.Marshal(aux)
}
type Keys []Key
func (keys Keys) Primary() Key {
for _, key := range keys {
if key.Primary {
return key
}
}
panic("no primary key available")
}
func (keys Keys) ByID(keyID string) Key {
for _, key := range keys {
if key.ID == keyID {
return key
}
}
panic("no primary key available")
}
func (keys Keys) Unlock(passphrase []byte, userKR *crypto.KeyRing) (*crypto.KeyRing, error) {
kr, err := crypto.NewKeyRing(nil)
if err != nil {
return nil, err
}
for _, key := range xslices.Filter(keys, func(key Key) bool { return bool(key.Active) }) {
unlocked, err := key.Unlock(passphrase, userKR)
if err != nil {
log.WithField("KeyID", key.ID).WithError(err).Warning("Cannot unlock key")
continue
}
if err := kr.AddKey(unlocked); err != nil {
return nil, err
}
}
if kr.CountEntities() == 0 {
return nil, errors.New("not able to unlock any key")
}
return kr, nil
}
func (keys Keys) TryUnlock(passphrase []byte, userKR *crypto.KeyRing) *crypto.KeyRing {
kr, err := keys.Unlock(passphrase, userKR)
if err != nil {
return nil
}
return kr
}
func (key Key) Unlock(passphrase []byte, userKR *crypto.KeyRing) (*crypto.Key, error) {
var secret []byte
if key.Token == "" || key.Signature == "" {
secret = passphrase
} else {
var err error
if secret, err = key.getPassphraseFromToken(userKR); err != nil {
return nil, err
}
}
return key.unlock(secret)
}
func (key Key) getPassphraseFromToken(kr *crypto.KeyRing) ([]byte, error) {
if kr == nil {
return nil, errors.New("no user key was provided")
}
msg, err := crypto.NewPGPMessageFromArmored(key.Token)
if err != nil {
return nil, err
}
sig, err := crypto.NewPGPSignatureFromArmored(key.Signature)
if err != nil {
return nil, err
}
token, err := kr.Decrypt(msg, nil, 0)
if err != nil {
return nil, err
}
if err = kr.VerifyDetached(token, sig, 0); err != nil {
return nil, err
}
return token.GetBinary(), nil
}
func (key Key) unlock(passphrase []byte) (*crypto.Key, error) {
lk, err := crypto.NewKey(key.PrivateKey)
if err != nil {
return nil, err
}
defer lk.ClearPrivateParams()
uk, err := lk.Unlock(passphrase)
if err != nil {
return nil, err
}
ok, err := uk.Check()
if err != nil {
return nil, err
} else if !ok {
return nil, errors.New("private and public keys do not match")
}
return uk, nil
}
func DecodeKeyPacket(packet string) []byte {
if packet == "" {
return nil
}
raw, err := base64.StdEncoding.DecodeString(packet)
if err != nil {
panic(err)
}
return raw
}