This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort.go
151 lines (125 loc) · 3.76 KB
/
sort.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
/*
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 "sort"
func lessSelfSigs(i, j *SelfSigs) (bool, bool) {
iValid := i.Valid()
jValid := j.Valid()
if iValid != jValid {
// Valid comes before invalid
return iValid, true
}
if !iValid {
_, iRevokedOk := i.RevokedSince()
_, jRevokedOk := j.RevokedSince()
if iRevokedOk != jRevokedOk {
// Non-revoked comes before revoked
return !iRevokedOk, true
}
}
iPrimarySince, iPrimaryOk := i.PrimarySince()
jPrimarySince, jPrimaryOk := j.PrimarySince()
if iPrimaryOk != jPrimaryOk {
// Primary comes before non-primary
return iPrimaryOk, true
}
if iPrimaryOk {
// Most recent primary certification comes first
return jPrimarySince.Unix() < iPrimarySince.Unix(), true
}
iValidSince, iValidOk := i.ValidSince()
jValidSince, jValidOk := j.ValidSince()
if iValidOk != jValidOk {
// Self-certified comes before non-self-certified
return iValidOk, true
}
if iValidOk {
// Most recently certified comes first
return jValidSince.Unix() < iValidSince.Unix(), true
}
return false, false
}
type uidSorter struct {
*PrimaryKey
}
func (s *uidSorter) Len() int { return len(s.UserIDs) }
func (s *uidSorter) Less(i, j int) bool {
iss := s.UserIDs[i].SelfSigs(s.PrimaryKey)
jss := s.UserIDs[j].SelfSigs(s.PrimaryKey)
less, ok := lessSelfSigs(iss, jss)
if ok {
return less
}
return s.UserIDs[i].Keywords < s.UserIDs[j].Keywords
}
func (s *uidSorter) Swap(i, j int) {
s.UserIDs[i], s.UserIDs[j] = s.UserIDs[j], s.UserIDs[i]
}
type uatSorter struct {
*PrimaryKey
}
func (s *uatSorter) Len() int { return len(s.UserAttributes) }
func (s *uatSorter) Less(i, j int) bool {
iss := s.UserAttributes[i].SelfSigs(s.PrimaryKey)
jss := s.UserAttributes[j].SelfSigs(s.PrimaryKey)
less, _ := lessSelfSigs(iss, jss)
return less
}
func (s *uatSorter) Swap(i, j int) {
s.UserAttributes[i], s.UserAttributes[j] = s.UserAttributes[j], s.UserAttributes[i]
}
type subkeySorter struct {
*PrimaryKey
}
func (s *subkeySorter) Len() int { return len(s.SubKeys) }
func (s *subkeySorter) Less(i, j int) bool {
iss := s.SubKeys[i].SelfSigs(s.PrimaryKey)
jss := s.SubKeys[j].SelfSigs(s.PrimaryKey)
less, ok := lessSelfSigs(iss, jss)
if ok {
return less
}
return s.SubKeys[i].Creation.Unix() < s.SubKeys[j].Creation.Unix()
}
func (s *subkeySorter) Swap(i, j int) {
s.SubKeys[i], s.SubKeys[j] = s.SubKeys[j], s.SubKeys[i]
}
type sigSorter struct {
sigs []*Signature
}
func (s *sigSorter) Len() int { return len(s.sigs) }
func (s *sigSorter) Less(i, j int) bool {
return s.sigs[i].Creation.Unix() < s.sigs[j].Creation.Unix()
}
func (s *sigSorter) Swap(i, j int) {
s.sigs[i], s.sigs[j] = s.sigs[j], s.sigs[i]
}
// Sort reorders the key material based on precedence rules.
func Sort(pubkey *PrimaryKey) {
for _, node := range pubkey.contents() {
switch p := node.(type) {
case *PrimaryKey:
sort.Sort(&sigSorter{p.Signatures})
sort.Sort(&uidSorter{p})
sort.Sort(&uatSorter{p})
sort.Sort(&subkeySorter{p})
case *SubKey:
sort.Sort(&sigSorter{p.Signatures})
case *UserID:
sort.Sort(&sigSorter{p.Signatures})
case *UserAttribute:
sort.Sort(&sigSorter{p.Signatures})
}
}
}