~cmars/hockeypuck/master

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
/*
   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"
	"strings"
)

type uidSorter struct {
	*Pubkey
}

func (s *uidSorter) Len() int { return len(s.userIds) }

func (s *uidSorter) Less(i, j int) bool {
	iSig := maxSelfSig(s.Pubkey, s.userIds[i].signatures)
	jSig := maxSelfSig(s.Pubkey, s.userIds[j].signatures)
	return sigLess(iSig, jSig)
}

func sigLess(iSig *Signature, jSig *Signature) bool {
	if iSig != nil && jSig != nil {
		if iSig.IsPrimary() != jSig.IsPrimary() {
			return iSig.IsPrimary()
		}
		return iSig.Creation.Unix() > jSig.Creation.Unix()
	}
	return iSig != nil
}

func maxSelfSig(pubkey *Pubkey, sigs []*Signature) (recent *Signature) {
	for _, sig := range sigs {
		if strings.HasPrefix(pubkey.RFingerprint, sig.RIssuerKeyId) && (recent == nil || sig.Creation.Unix() > recent.Creation.Unix()) {
			recent = sig
		}
	}
	return
}

func (s *uidSorter) Swap(i, j int) {
	s.userIds[i], s.userIds[j] = s.userIds[j], s.userIds[i]
}

type uatSorter struct {
	*Pubkey
}

func (s *uatSorter) Len() int { return len(s.userAttributes) }

func (s *uatSorter) Less(i, j int) bool {
	iSig := maxSelfSig(s.Pubkey, s.userAttributes[i].signatures)
	jSig := maxSelfSig(s.Pubkey, s.userAttributes[j].signatures)
	return sigLess(iSig, jSig)
}

func (s *uatSorter) Swap(i, j int) {
	s.userAttributes[i], s.userAttributes[j] = s.userAttributes[j], s.userAttributes[i]
}

type subkeySorter struct {
	*Pubkey
}

func (s *subkeySorter) Len() int { return len(s.subkeys) }

func (s *subkeySorter) Less(i, j int) bool {
	if (s.subkeys[i].revSig == nil) != (s.subkeys[j].revSig == nil) {
		return s.subkeys[i].revSig != nil
	}
	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
func Sort(pubkey *Pubkey) {
	pubkey.Visit(func(rec PacketRecord) error {
		switch r := rec.(type) {
		case *UserId:
			sort.Sort(&sigSorter{r.signatures})
		case *UserAttribute:
			sort.Sort(&sigSorter{r.signatures})
		case *Subkey:
			sort.Sort(&sigSorter{r.signatures})
		}
		return nil
	})
	sort.Sort(&uidSorter{pubkey})
	sort.Sort(&uatSorter{pubkey})
	sort.Sort(&subkeySorter{pubkey})
}