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
/
selfsigs.go
152 lines (123 loc) · 3.93 KB
/
selfsigs.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
/*
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"
"time"
)
var now = time.Now
// CheckSig represents the result of checking a self-signature.
type CheckSig struct {
PrimaryKey *PrimaryKey
Signature *Signature
Error error
}
// SelfSigs holds self-signatures on OpenPGP targets, which may be keys, user
// IDs, or user attributes.
type SelfSigs struct {
Revocations []*CheckSig
Certifications []*CheckSig
Expirations []*CheckSig
Primaries []*CheckSig
Errors []*CheckSig
target packetNode
}
type checkSigCreationAsc []*CheckSig
func (s checkSigCreationAsc) Len() int { return len(s) }
func (s checkSigCreationAsc) Less(i, j int) bool {
return s[i].Signature.Creation.Unix() < s[j].Signature.Creation.Unix()
}
func (s checkSigCreationAsc) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type checkSigCreationDesc []*CheckSig
func (s checkSigCreationDesc) Len() int { return len(s) }
func (s checkSigCreationDesc) Less(i, j int) bool {
return s[j].Signature.Creation.Unix() < s[i].Signature.Creation.Unix()
}
func (s checkSigCreationDesc) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type checkSigExpirationDesc []*CheckSig
func (s checkSigExpirationDesc) Len() int { return len(s) }
func (s checkSigExpirationDesc) Less(i, j int) bool {
return s[j].Signature.Expiration.Unix() < s[i].Signature.Expiration.Unix()
}
func (s checkSigExpirationDesc) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s *SelfSigs) resolve() {
// Revocations cancel all other certifications
if len(s.Revocations) > 0 {
s.Certifications = nil
s.Expirations = nil
s.Primaries = nil
}
// Sort signatures
sort.Sort(checkSigCreationAsc(s.Revocations))
sort.Sort(checkSigCreationDesc(s.Certifications))
sort.Sort(checkSigExpirationDesc(s.Expirations))
sort.Sort(checkSigCreationDesc(s.Primaries))
}
var zeroTime time.Time
func (s *SelfSigs) RevokedSince() (time.Time, bool) {
if len(s.Revocations) > 0 {
return s.Revocations[0].Signature.Creation, true
}
return zeroTime, false
}
func (s *SelfSigs) ExpiresAt() (time.Time, bool) {
if len(s.Expirations) > 0 {
return s.Expirations[0].Signature.Expiration, true
}
return zeroTime, false
}
func (s *SelfSigs) Valid() bool {
revoked := len(s.Revocations) > 0
expiration, okExpiration := s.ExpiresAt()
_, okValid := s.ValidSince()
return (!revoked && // target has no revocations
// target does not expire or hasn't expired yet
(!okExpiration || expiration.Unix() > now().Unix()) &&
// target has non-expired self-signatures
okValid)
}
func (s *SelfSigs) ValidSince() (time.Time, bool) {
if len(s.Revocations) > 0 {
return zeroTime, false
}
if pubkey, ok := s.target.(*PrimaryKey); ok {
return pubkey.Creation, true
}
for _, checkSig := range s.Certifications {
// Return the first non-expired self-signature creation time.
expiresAt := checkSig.Signature.Expiration
if expiresAt.IsZero() || expiresAt.Unix() > now().Unix() {
return checkSig.Signature.Creation, true
}
}
return zeroTime, false
}
func (s *SelfSigs) PrimarySince() (time.Time, bool) {
if len(s.Revocations) > 0 {
return zeroTime, false
}
for _, checkSig := range s.Primaries {
expiresAt := checkSig.Signature.Expiration
if expiresAt.IsZero() || expiresAt.Unix() > now().Unix() {
return checkSig.Signature.Creation, true
}
}
return zeroTime, false
}