forked from hockeypuck/openpgp
/
userid.go
181 lines (158 loc) · 4.14 KB
/
userid.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
/*
Hockeypuck - OpenPGP key server
Copyright (C) 2012-2014 Casey Marshall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package openpgp
import (
"bytes"
"strings"
"unicode/utf8"
"golang.org/x/crypto/openpgp/packet"
"gopkg.in/errgo.v1"
)
type UserID struct {
Packet
Keywords string
Signatures []*Signature
Others []*Packet
}
const uidTag = "{uid}"
// contents implements the packetNode interface for user IDs.
func (uid *UserID) contents() []packetNode {
result := []packetNode{uid}
for _, sig := range uid.Signatures {
result = append(result, sig.contents()...)
}
for _, p := range uid.Others {
result = append(result, p.contents()...)
}
return result
}
// appendSignature implements signable.
func (uid *UserID) appendSignature(sig *Signature) {
uid.Signatures = append(uid.Signatures, sig)
}
func (uid *UserID) removeDuplicate(parent packetNode, dup packetNode) error {
pubkey, ok := parent.(*PrimaryKey)
if !ok {
return errgo.Newf("invalid uid parent: %+v", parent)
}
dupUserID, ok := dup.(*UserID)
if !ok {
return errgo.Newf("invalid uid duplicate: %+v", dup)
}
uid.Signatures = append(uid.Signatures, dupUserID.Signatures...)
uid.Others = append(uid.Others, dupUserID.Others...)
pubkey.UserIDs = uidSlice(pubkey.UserIDs).without(dupUserID)
return nil
}
type uidSlice []*UserID
func (us uidSlice) without(target *UserID) []*UserID {
var result []*UserID
for _, uid := range us {
if uid != target {
result = append(result, uid)
}
}
return result
}
func ParseUserID(op *packet.OpaquePacket, parentID string) (*UserID, error) {
var buf bytes.Buffer
if err := op.Serialize(&buf); err != nil {
return nil, errgo.Mask(err)
}
uid := &UserID{
Packet: Packet{
UUID: scopedDigest([]string{parentID}, uidTag, buf.Bytes()),
Tag: op.Tag,
Packet: buf.Bytes(),
},
}
p, err := op.Parse()
if err != nil {
return nil, errgo.Mask(err)
}
u, ok := p.(*packet.UserId)
if !ok {
return nil, ErrInvalidPacketType
}
err = uid.setUserID(u)
if err != nil {
return nil, errgo.Mask(err)
}
uid.Parsed = true
return uid, nil
}
func (uid *UserID) userIDPacket() (*packet.UserId, error) {
op, err := uid.opaquePacket()
if err != nil {
return nil, errgo.Mask(err)
}
p, err := op.Parse()
if err != nil {
return nil, errgo.Mask(err)
}
u, ok := p.(*packet.UserId)
if !ok {
return nil, errgo.Newf("expected user ID packet, got %T", p)
}
return u, nil
}
func (uid *UserID) setUserID(u *packet.UserId) error {
uid.Keywords = cleanUtf8(u.Id)
return nil
}
func cleanUtf8(s string) string {
var runes []rune
for _, r := range s {
if r == utf8.RuneError {
r = '?'
}
if r < 0x20 || r == 0x7f {
continue
}
runes = append(runes, r)
}
return string(runes)
}
func (uid *UserID) SelfSigs(pubkey *PrimaryKey) *SelfSigs {
result := &SelfSigs{target: uid}
for _, sig := range uid.Signatures {
// Skip non-self-certifications.
if !strings.HasPrefix(pubkey.UUID, sig.RIssuerKeyID) {
continue
}
checkSig := &CheckSig{
PrimaryKey: pubkey,
Signature: sig,
Error: pubkey.verifyUserIDSelfSig(uid, sig),
}
if checkSig.Error != nil {
result.Errors = append(result.Errors, checkSig)
continue
}
switch sig.SigType {
case 0x30: // packet.SigTypeCertRevocation
result.Revocations = append(result.Revocations, checkSig)
case 0x10, 0x11, 0x12, 0x13:
result.Certifications = append(result.Certifications, checkSig)
if !sig.Expiration.IsZero() {
result.Expirations = append(result.Expirations, checkSig)
}
if sig.Primary {
result.Primaries = append(result.Primaries, checkSig)
}
}
}
result.resolve()
return result
}