/
pgp.go
281 lines (236 loc) · 7.38 KB
/
pgp.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
/*
Copyright © 2020 Bob Callaway <bcallawa@redhat.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pgp
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"golang.org/x/crypto/openpgp"
)
// Signature Signature that follows the PGP standard; supports both armored & binary detached signatures
type Signature struct {
isArmored bool
signature []byte
}
// NewSignature creates and validates a PGP signature object
func NewSignature(r io.Reader) (*Signature, error) {
var s Signature
var inputBuffer bytes.Buffer
if _, err := io.Copy(&inputBuffer, r); err != nil {
return nil, fmt.Errorf("unable to read PGP signature: %w", err)
}
sigByteReader := bytes.NewReader(inputBuffer.Bytes())
var sigReader io.Reader
sigBlock, err := armor.Decode(sigByteReader)
if err == nil {
s.isArmored = true
if sigBlock.Type != openpgp.SignatureType {
return nil, fmt.Errorf("invalid PGP signature provided")
}
sigReader = sigBlock.Body
} else {
s.isArmored = false
if _, err := sigByteReader.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("unable to read binary PGP signature: %w", err)
}
sigReader = sigByteReader
}
sigPktReader := packet.NewReader(sigReader)
sigPkt, err := sigPktReader.Next()
if err != nil {
return nil, fmt.Errorf("invalid PGP signature: %w", err)
}
if _, ok := sigPkt.(*packet.Signature); !ok {
if _, ok := sigPkt.(*packet.SignatureV3); !ok {
return nil, fmt.Errorf("valid PGP signature was not detected")
}
}
s.signature = inputBuffer.Bytes()
return &s, nil
}
// FetchSignature implements pki.Signature interface
func FetchSignature(ctx context.Context, url string) (*Signature, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error initializing fetch for PGP signature: %w", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error fetching PGP signature: %w", err)
}
defer resp.Body.Close()
sig, err := NewSignature(resp.Body)
if err != nil {
return nil, err
}
return sig, nil
}
// CanonicalValue implements the pki.Signature interface
func (s Signature) CanonicalValue() ([]byte, error) {
if len(s.signature) == 0 {
return nil, fmt.Errorf("PGP signature has not been initialized")
}
if s.isArmored {
return s.signature, nil
}
var canonicalBuffer bytes.Buffer
// Use an inner function so we can defer the Close()
if err := func() error {
ew, err := armor.Encode(&canonicalBuffer, openpgp.SignatureType, nil)
if err != nil {
return fmt.Errorf("error encoding canonical value of PGP signature: %w", err)
}
defer ew.Close()
if _, err := io.Copy(ew, bytes.NewReader(s.signature)); err != nil {
return fmt.Errorf("error generating canonical value of PGP signature: %w", err)
}
return nil
}(); err != nil {
return nil, err
}
return canonicalBuffer.Bytes(), nil
}
// Verify implements the pki.Signature interface
func (s Signature) Verify(r io.Reader, k interface{}) error {
if len(s.signature) == 0 {
return fmt.Errorf("PGP signature has not been initialized")
}
key, ok := k.(*PublicKey)
if !ok {
return fmt.Errorf("cannot use Verify with a non-PGP signature")
}
if len(key.key) == 0 {
return fmt.Errorf("PGP public key has not been initialized")
}
verifyFn := openpgp.CheckDetachedSignature
if s.isArmored {
verifyFn = openpgp.CheckArmoredDetachedSignature
}
if _, err := verifyFn(key.key, r, bytes.NewReader(s.signature)); err != nil {
return err
}
return nil
}
// PublicKey Public Key that follows the PGP standard; supports both armored & binary detached signatures
type PublicKey struct {
key openpgp.EntityList
}
// NewPublicKey implements the pki.PublicKey interface
func NewPublicKey(r io.Reader) (*PublicKey, error) {
var k PublicKey
var inputBuffer bytes.Buffer
startToken := []byte(`-----BEGIN PGP`)
endToken := []byte(`-----END PGP`)
bufferedReader := bufio.NewReader(r)
armorCheck, err := bufferedReader.Peek(len(startToken))
if err != nil {
return nil, fmt.Errorf("unable to read PGP public key: %w", err)
}
if bytes.Equal(startToken, armorCheck) {
// looks like we have armored input
scan := bufio.NewScanner(bufferedReader)
scan.Split(bufio.ScanLines)
for scan.Scan() {
line := scan.Bytes()
inputBuffer.Write(line)
fmt.Fprintf(&inputBuffer, "\n")
if bytes.HasPrefix(line, endToken) {
// we have a complete armored message; process it
keyBlock, err := armor.Decode(&inputBuffer)
if err == nil {
if keyBlock.Type != openpgp.PublicKeyType && keyBlock.Type != openpgp.PrivateKeyType {
return nil, fmt.Errorf("invalid PGP type detected")
}
keys, err := openpgp.ReadKeyRing(keyBlock.Body)
if err != nil {
return nil, fmt.Errorf("error reading PGP public key: %w", err)
}
if k.key == nil {
k.key = keys
} else {
k.key = append(k.key, keys...)
}
inputBuffer.Reset()
} else {
return nil, fmt.Errorf("invalid PGP public key provided: %w", err)
}
}
}
} else {
// process as binary
k.key, err = openpgp.ReadKeyRing(bufferedReader)
if err != nil {
return nil, fmt.Errorf("error reading binary PGP public key: %w", err)
}
}
if len(k.key) == len(k.key.DecryptionKeys()) {
return nil, fmt.Errorf("no PGP public keys could be read")
}
return &k, nil
}
// FetchPublicKey implements pki.PublicKey interface
func FetchPublicKey(ctx context.Context, url string) (*PublicKey, error) {
//TODO: detect if url is hkp and adjust accordingly
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error fetching PGP public key: %w", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error fetching PGP public key: %w", err)
}
defer resp.Body.Close()
key, err := NewPublicKey(resp.Body)
if err != nil {
return nil, err
}
return key, nil
}
// CanonicalValue implements the pki.PublicKey interface
func (k PublicKey) CanonicalValue() ([]byte, error) {
if k.key == nil {
return nil, fmt.Errorf("PGP public key has not been initialized")
}
var canonicalBuffer bytes.Buffer
// Use an inner function so we can defer the close()
if err := func() error {
armoredWriter, err := armor.Encode(&canonicalBuffer, openpgp.PublicKeyType, nil)
if err != nil {
return fmt.Errorf("error generating canonical value of PGP public key: %w", err)
}
defer armoredWriter.Close()
for _, entity := range k.key {
if err := entity.Serialize(armoredWriter); err != nil {
return fmt.Errorf("error generating canonical value of PGP public key: %w", err)
}
}
return nil
}(); err != nil {
return nil, err
}
return canonicalBuffer.Bytes(), nil
}
func (k PublicKey) KeyRing() (openpgp.KeyRing, error) {
if k.key == nil {
return nil, errors.New("PGP public key has not been initialized")
}
return k.key, nil
}