-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyring.go
76 lines (64 loc) · 1.75 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
package gpg
import (
"bytes"
"io/ioutil"
"github.com/effective-security/xpki/armor"
"github.com/pkg/errors"
"golang.org/x/crypto/openpgp"
)
// KeyRing reads a openpgp.KeyRing from the given io.Reader which may then be
// used to validate GPG keys in RPM packages.
func KeyRing(data []byte) (openpgp.EntityList, error) {
keyring := make(openpgp.EntityList, 0)
for {
block, rest := armor.Decode(data)
if block == nil {
logger.Tracef("reason=no_block, data=[%s...]", string(data))
break
}
if block.Type == openpgp.PublicKeyType {
// extract keys
el, err := openpgp.ReadKeyRing(bytes.NewReader(block.Bytes))
if err != nil {
return nil, errors.WithStack(err)
}
// append keyring
keyring = append(keyring, el...)
}
if rest == nil || len(rest) == 0 {
break
}
data = rest
}
return keyring, nil
}
// KeyRingFromFile reads a openpgp.KeyRing from the given file path which may
// then be used to validate GPG keys in RPM packages.
func KeyRingFromFile(path string) (openpgp.EntityList, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
k, err := KeyRing(data)
if err != nil {
return nil, errors.WithStack(err)
}
return k, nil
}
// KeyRingFromFiles reads a openpgp.KeyRing from the given file paths which may
// then be used to validate GPG keys in RPM packages.
//
// This function might typically be used to read all keys in /etc/pki/rpm-gpg.
func KeyRingFromFiles(files []string) (openpgp.EntityList, error) {
keyring := make(openpgp.EntityList, 0)
for _, path := range files {
// read keyring in file
el, err := KeyRingFromFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
// append keyring
keyring = append(keyring, el...)
}
return keyring, nil
}