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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
package krl
import (
"crypto/sha1"
"fmt"
"golang.org/x/crypto/ssh"
)
// Hundreds of millions, or 32MB of bitmap
const krlMaxBitmapSize = 0x10000000
// KRLSigningErrors is a slice of error messages which correspond one-to-one
// with KRL.SigningKeys.
type KRLSigningErrors []error
func (k KRLSigningErrors) Error() string {
return fmt.Sprintf("krl: bad signatures: %v", []error(k))
}
func (k KRLSigningErrors) err() error {
for _, err := range k {
if err != nil {
return k
}
}
return nil
}
// ParseKRL parses a KRL. If the KRL was signed by one or more authorities,
// those signatures will be checked, and any verification errors will be
// returned.
func ParseKRL(in []byte) (*KRL, error) {
orig := in
var header krlHeader
if err := ssh.Unmarshal(in, &header); err != nil {
return nil, fmt.Errorf("krl: while parsing header: %v", err)
}
if header.KRLMagic != krlMagic {
return nil, fmt.Errorf("krl: bad magic value %x", header.KRLMagic)
}
if header.KRLFormatVersion != 1 {
return nil, fmt.Errorf("krl: bad format version %v", header.KRLFormatVersion)
}
krl := &KRL{
Version: header.KRLVersion,
GeneratedDate: header.GeneratedDate,
Comment: header.Comment,
}
in = header.Rest
for len(in) > 0 && in[0] != 4 { // // KRL_SECTION_SIGNATURE
var sdata krlSection
if err := ssh.Unmarshal(in, &sdata); err != nil {
return nil, fmt.Errorf("krl: malformed section: %v", err)
}
in = sdata.Rest
var err error
var section KRLSection
switch sdata.SectionType {
case 1: // KRL_SECTION_CERTIFICATES
section, err = parseCertificateSection(sdata.SectionData)
case 2: // KRL_SECTION_EXPLICIT_KEY
section, err = parseExplicitKeySection(sdata.SectionData)
case 3: // KRL_SECTION_FINGERPRINT_SHA1
section, err = parseFingerprintSection(sdata.SectionData)
default:
return nil, fmt.Errorf("krl: unexpected section type %d", sdata.SectionType)
}
if err != nil {
return nil, err
}
krl.Sections = append(krl.Sections, section)
}
var signingErrors KRLSigningErrors
for len(in) > 0 {
var sigHeader krlSignatureHeader
if err := ssh.Unmarshal(in, &sigHeader); err != nil {
return nil, fmt.Errorf("krl: malfored signature header: %v", err)
}
in = sigHeader.Rest
key, err := ssh.ParsePublicKey(sigHeader.SignatureKey)
if err != nil {
return nil, fmt.Errorf("krl: malformed signing key: %v", err)
}
var sig krlSignature
if err := ssh.Unmarshal(in, &sig); err != nil {
return nil, fmt.Errorf("krl: malfored signature wrapper: %v", err)
}
in = sig.Rest
sshsig := new(ssh.Signature)
if err := ssh.Unmarshal(sig.Signature, sshsig); err != nil {
return nil, fmt.Errorf("krl: malformed signature: %v", err)
}
// The entire KRL up until immediately after the signature
// header is signed.
data := orig[:len(orig)-len(sigHeader.Rest)]
krl.SigningKeys = append(krl.SigningKeys, key)
signingErrors = append(signingErrors, key.Verify(data, sshsig))
}
return krl, signingErrors.err()
}
func parseCertificateSection(in []byte) (*KRLCertificateSection, error) {
var header krlCertificateSectionHeader
if err := ssh.Unmarshal(in, &header); err != nil {
return nil, fmt.Errorf("krl: while parsing certificate section header: %v", err)
}
ca, err := ssh.ParsePublicKey(header.CAKey)
if err != nil {
return nil, fmt.Errorf("krl: while parsing CA key: %v", err)
}
k := &KRLCertificateSection{CA: ca}
in = header.Rest
for len(in) > 0 {
var section krlCertificateSection
if err := ssh.Unmarshal(in, §ion); err != nil {
return nil, fmt.Errorf("krl: malformed certificate section: %v", err)
}
in = section.Rest
var err error
var subsection KRLCertificateSubsection
switch section.CertSectionType {
case krlSectionCertSerialList:
subsection, err = parseCertSerialList(section.CertSectionData)
case krlSectionCertSerialRange:
subsection, err = parseCertSerialRange(section.CertSectionData)
case krlSectionCertSerialBitmap:
subsection, err = parseCertSerialBitmap(section.CertSectionData)
case krlSectionCertKeyId:
subsection, err = parseCertKeyID(section.CertSectionData)
default:
return nil, fmt.Errorf("krl: unexpected cert section type %x", in[0])
}
if err != nil {
return nil, err
}
k.Sections = append(k.Sections, subsection)
}
return k, nil
}
func parseCertSerialList(in []byte) (*KRLCertificateSerialList, error) {
s := &KRLCertificateSerialList{}
for len(in) > 0 {
var list krlSerialList
if err := ssh.Unmarshal(in, &list); err != nil {
return nil, fmt.Errorf("krl: while parsing serial in list: %v", err)
}
in = list.Rest
*s = append(*s, list.RevokedCertSerial)
}
return s, nil
}
func parseCertSerialRange(in []byte) (*KRLCertificateSerialRange, error) {
var s krlSerialRange
if err := ssh.Unmarshal(in, &s); err != nil {
return nil, fmt.Errorf("krl: while parsing serial range: %v", err)
}
return &KRLCertificateSerialRange{
Min: s.SerialMin,
Max: s.SerialMax,
}, nil
}
func parseCertSerialBitmap(in []byte) (*KRLCertificateSerialBitmap, error) {
var s krlSerialBitmap
if err := ssh.Unmarshal(in, &s); err != nil {
return nil, fmt.Errorf("krl: while parsing serial bitmap: %v", err)
}
if bl := s.RevokedKeysBitmap.BitLen(); bl > krlMaxBitmapSize {
return nil, fmt.Errorf("krl: serial bitmap too wide: %v", bl)
}
return &KRLCertificateSerialBitmap{
Offset: s.SerialOffset,
Bitmap: s.RevokedKeysBitmap,
}, nil
}
func parseCertKeyID(in []byte) (*KRLCertificateKeyID, error) {
s := &KRLCertificateKeyID{}
for len(in) > 0 {
var list krlKeyID
if err := ssh.Unmarshal(in, &list); err != nil {
return nil, fmt.Errorf("krl: while parsing key id in list: %v", err)
}
in = list.Rest
*s = append(*s, list.KeyID)
}
return s, nil
}
func parseExplicitKeySection(in []byte) (*KRLExplicitKeySection, error) {
s := &KRLExplicitKeySection{}
for len(in) > 0 {
var list krlExplicitKey
if err := ssh.Unmarshal(in, &list); err != nil {
return nil, fmt.Errorf("krl: while parsing explicit key in list: %v", err)
}
in = list.Rest
key, err := ssh.ParsePublicKey(list.PublicKeyBlob)
if err != nil {
return nil, fmt.Errorf("krl: while parsing explicit key: %v", err)
}
*s = append(*s, key)
}
return s, nil
}
func parseFingerprintSection(in []byte) (*KRLFingerprintSection, error) {
s := &KRLFingerprintSection{}
for len(in) > 0 {
var list krlFingerprintSHA1
if err := ssh.Unmarshal(in, &list); err != nil {
return nil, fmt.Errorf("krl: while parsing fingerprint in list: %v", err)
}
in = list.Rest
if len(list.PublicKeyHash) != sha1.Size {
return nil, fmt.Errorf("krl: key fingerprint wrong length for SHA1: %x", list.PublicKeyHash)
}
var sha [sha1.Size]byte
copy(sha[:], list.PublicKeyHash)
*s = append(*s, sha)
}
return s, nil
}
|