/
keyring.go
150 lines (124 loc) · 2.86 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
package openpgp
import (
"fmt"
"os"
"strings"
"golang.org/x/crypto/openpgp"
)
func ReadPubRing(path string, keyIds []string) (*openpgp.EntityList, error) {
var pubKeys *openpgp.EntityList
var matchedKeys openpgp.EntityList
var unmatchedKeyIds []string
var hprefs, sprefs []uint8
var err error
pubKeys, err = ReadKeyRing(path)
if err != nil {
return nil, err
}
// For each provided key ids, check if it exists
// through the gnupg pub ring
for _, keyId := range keyIds {
var matched bool = false
for _, entity := range *pubKeys {
if isEntityKey(keyId, entity) {
pi := primaryIdentity(entity)
ss := pi.SelfSignature
hprefs = intersectPreferences(hprefs, ss.PreferredHash)
sprefs = intersectPreferences(sprefs, ss.PreferredSymmetric)
matchedKeys = append(matchedKeys, entity)
matched = true
}
}
if matched == false {
unmatchedKeyIds = append(unmatchedKeyIds, keyId)
}
}
if len(unmatchedKeyIds) != 0 {
errMsg := fmt.Sprintf("The following keys could not be found "+
"in the public keyring: %s", strings.Join(unmatchedKeyIds, ", "))
return nil, NewOpenPGPError(
ERR_DECRYPTION_KEYS,
errMsg,
)
}
if len(hprefs) == 0 {
return nil, NewOpenPGPError(
ERR_DECRYPTION_HASHES,
"No common hashes for encryption keys",
)
}
if len(sprefs) == 0 {
return nil, NewOpenPGPError(
ERR_DECRYPTION_CIPHERS,
"No common symmetric ciphers for encryption keys",
)
}
return &matchedKeys, nil
}
func ReadSecRing(path string) (*openpgp.EntityList, error) {
return ReadKeyRing(path)
}
func ReadKeyRing(path string) (*openpgp.EntityList, error) {
var keys openpgp.EntityList
var err error
f, err := os.Open(path)
if err != nil {
return nil, NewOpenPGPError(
ERR_KEYRING,
fmt.Sprintf("Unable to open gnupg keyring: %v", err),
)
}
defer f.Close()
keys, err = openpgp.ReadKeyRing(f)
if err != nil {
return nil, NewOpenPGPError(
ERR_KEYRING,
fmt.Sprintf("Unable to read from gnupg keyring: %v", err),
)
}
return &keys, nil
}
func isEntityKey(keyId string, e *openpgp.Entity) bool {
if e.PrimaryKey.KeyIdShortString() == keyId {
return true
} else {
for _, identity := range e.Identities {
if identity.UserId.Email == keyId {
return true
}
}
}
return false
}
func intersectPreferences(a []uint8, b []uint8) (intersection []uint8) {
if a == nil {
return b
}
var j int
for _, v := range a {
for _, v2 := range b {
if v == v2 {
a[j] = v
j++
break
}
}
}
return a[:j]
}
func primaryIdentity(e *openpgp.Entity) *openpgp.Identity {
var firstIdentity *openpgp.Identity
for _, ident := range e.Identities {
if firstIdentity == nil {
firstIdentity = ident
}
if ident.SelfSignature.IsPrimaryId != nil && *ident.SelfSignature.IsPrimaryId {
return ident
}
}
return firstIdentity
}
func UserIds() []string {
var userIds []string
return userIds
}