forked from stripe/krl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krl.go
433 lines (383 loc) · 10.3 KB
/
krl.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Package krl provides functionality for reading and writing SSH Key Revocation
// Lists (KRLs).
//
// References:
// https://raw.githubusercontent.com/openssh/openssh-portable/master/PROTOCOL.krl
package krl
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"io"
"math/big"
"sort"
"time"
"golang.org/x/crypto/ssh"
)
// KRL, or Key Revocation List, is a list of revoked keys, certificates, and
// identities, possibly signed by some authority. The zero value of KRL is
// appropriate for use, and represents an empty list.
type KRL struct {
// Version is a number that increases every time the KRL is modified.
// When marshaling a KRL, if Version is zero GeneratedDate will be used
// instead.
Version uint64
// GeneratedDate is the Unix timestamp the KRL was generated at. When
// marshaling a KRL, if GeneratedDate is zero the current Unix timestamp
// will be used instead.
GeneratedDate uint64
// Comment is an optional comment for the KRL.
Comment string
// Sections is a list of public key and certificate selectors that this
// KRL applies to.
Sections []KRLSection
// SigningKeys is set by ParseKRL and Marshal to the list of Signers
// that signed (or which claimed to sign) the KRL in the order they
// appeared (i.e., innermost-first).
SigningKeys []ssh.PublicKey
}
/*
KRLSection describes a section of a KRL, which selects certain certificates and
keys for revocation. The concrete types KRLCertificateSection,
KRLExplicitKeySection, and KRLFingerprintSection satisfy this interface, and
correspond to the three types of KRL sections currently defined.
*/
type KRLSection interface {
isRevoked(key ssh.PublicKey) bool
marshal() []byte
}
/*
KRLCertificateSection revokes SSH certificates by certificate authority and
either serial numbers or key ids.
*/
type KRLCertificateSection struct {
// CA is the certificate authority whose keys are being revoked by this
// section. If CA is nil, this section applies to keys signed by any
// certificate authority.
CA ssh.PublicKey
// Sections is a list of certificate selectors.
Sections []KRLCertificateSubsection
}
func (k *KRLCertificateSection) isRevoked(key ssh.PublicKey) bool {
cert, ok := key.(*ssh.Certificate)
if !ok {
return false
}
if k.CA != nil {
sk := cert.SignatureKey.Marshal()
ca := k.CA.Marshal()
if !bytes.Equal(sk, ca) {
return false
}
}
for _, section := range k.Sections {
if section.isRevoked(cert) {
return true
}
}
return false
}
func (k *KRLCertificateSection) marshal() []byte {
var buf bytes.Buffer
var ca []byte
if k.CA != nil {
ca = k.CA.Marshal()
}
buf.Write(ssh.Marshal(krlCertificateSectionHeader{CAKey: ca}))
headerLen := buf.Len()
for _, section := range k.Sections {
buf.Write(section.marshal())
}
// All subsections were empty; we should be empty too.
if buf.Len() == headerLen {
return nil
}
return ssh.Marshal(krlSection{
SectionType: 1,
SectionData: buf.Bytes(),
})
}
/*
KRLCertificateSubsection describes a subsection of a KRL certificate selection,
and selects certain certificates for revocation. The concrete types
KRLCertificateSerialList, KRLCertificateSerialRange, KRLCertificateSerialBitmap,
and KRLCertificateSerialBitmap satisfy this interface, and correspond to the
four subsections currently defined.
*/
type KRLCertificateSubsection interface {
isRevoked(cert *ssh.Certificate) bool
marshal() []byte
}
// KRLCertificateSerialList revokes certificates by listing their serial
// numbers.
type KRLCertificateSerialList []uint64
func (k *KRLCertificateSerialList) isRevoked(cert *ssh.Certificate) bool {
for _, serial := range *k {
if serial == cert.Serial {
return true
}
}
return false
}
func (k *KRLCertificateSerialList) marshal() []byte {
if len(*k) == 0 {
return nil
}
var buf bytes.Buffer
for _, serial := range *k {
buf.Write(ssh.Marshal(krlSerialList{
RevokedCertSerial: serial,
}))
}
return ssh.Marshal(krlCertificateSection{
CertSectionType: krlSectionCertSerialList,
CertSectionData: buf.Bytes(),
})
}
// KRLCertificateSerialRange revokes all certificates with serial numbers in the
// range between Min and Max, inclusive.
type KRLCertificateSerialRange struct {
Min, Max uint64
}
func (k *KRLCertificateSerialRange) isRevoked(cert *ssh.Certificate) bool {
return k.Min <= cert.Serial && cert.Serial <= k.Max
}
func (k *KRLCertificateSerialRange) marshal() []byte {
return ssh.Marshal(krlCertificateSection{
CertSectionType: krlSectionCertSerialRange,
CertSectionData: ssh.Marshal(krlSerialRange{
SerialMin: k.Min,
SerialMax: k.Max,
}),
})
}
// KRLCertificateSerialBitmap revokes certificates densely using a bitmap. If
// bit N of the bitmap is set, the certificate with serial Offset + N is
// revoked.
type KRLCertificateSerialBitmap struct {
Offset uint64
Bitmap *big.Int
}
func (k *KRLCertificateSerialBitmap) isRevoked(cert *ssh.Certificate) bool {
if cert.Serial < k.Offset {
return false
}
if cert.Serial-k.Offset > krlMaxBitmapSize {
return false
}
return k.Bitmap.Bit(int(cert.Serial-k.Offset)) == 1
}
func (k *KRLCertificateSerialBitmap) marshal() []byte {
return ssh.Marshal(krlCertificateSection{
CertSectionType: krlSectionCertSerialBitmap,
CertSectionData: ssh.Marshal(krlSerialBitmap{
SerialOffset: k.Offset,
RevokedKeysBitmap: k.Bitmap,
}),
})
}
// KRLCertificateKeyID revokes certificates by listing key ids. This may be
// useful in revoking all certificates associated with a particular identity,
// for instance hosts or users.
type KRLCertificateKeyID []string
func (k *KRLCertificateKeyID) isRevoked(cert *ssh.Certificate) bool {
for _, id := range *k {
if id == cert.KeyId {
return true
}
}
return false
}
func (k *KRLCertificateKeyID) marshal() []byte {
if len(*k) == 0 {
return nil
}
var buf bytes.Buffer
for _, id := range *k {
buf.Write(ssh.Marshal(krlKeyID{
KeyID: id,
}))
}
return ssh.Marshal(krlCertificateSection{
CertSectionType: krlSectionCertKeyId,
CertSectionData: buf.Bytes(),
})
}
// ssh.PublicKey objects might be certificates, which have a different wire
// format.
func marshalPubkey(key ssh.PublicKey) []byte {
switch v := key.(type) {
case *ssh.Certificate:
return marshalPubkey(v.Key)
default:
return key.Marshal()
}
}
// KRLExplicitKeySection revokes keys by explicitly listing them.
type KRLExplicitKeySection []ssh.PublicKey
func (k *KRLExplicitKeySection) isRevoked(key ssh.PublicKey) bool {
kbuf := marshalPubkey(key)
for _, key := range *k {
if bytes.Equal(kbuf, marshalPubkey(key)) {
return true
}
}
return false
}
func (k *KRLExplicitKeySection) marshal() []byte {
if len(*k) == 0 {
return nil
}
var buf bytes.Buffer
for _, key := range *k {
buf.Write(ssh.Marshal(krlExplicitKey{
PublicKeyBlob: marshalPubkey(key),
}))
}
return ssh.Marshal(krlSection{
SectionType: 2,
SectionData: buf.Bytes(),
})
}
type bigEndian [][]byte
func (b bigEndian) Len() int {
return len(b)
}
func (b bigEndian) Less(i, j int) bool {
return bytes.Compare(b[i][:], b[j][:]) == -1
}
func (b bigEndian) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
type hashList interface {
slices() [][]byte
}
func parseBigEndian(l hashList) bigEndian {
b := l.slices()
// For some reason SSH insists that keys revoked by fingerprint must be
// sorted as if they were big-endian integers (i.e., lexicographically).
be := make(bigEndian, len(b))
for i, hash := range b {
be[i] = make([]byte, len(hash))
copy(be[i], hash[:])
}
sort.Sort(be)
return be
}
// KRLFingerprintSection revokes keys by their SHA1 fingerprints. It is
// semantically equivalent to--but is more space efficient than--
// KRLExplicitKeySection.
type KRLFingerprintSection [][sha1.Size]byte
func (k *KRLFingerprintSection) isRevoked(key ssh.PublicKey) bool {
sha := sha1.Sum(marshalPubkey(key))
for _, hash := range *k {
if hash == sha {
return true
}
}
return false
}
func (k *KRLFingerprintSection) slices() [][]byte {
s := make([][]byte, len(*k))
for i, hash := range *k {
s[i] = make([]byte, len(hash))
copy(s[i], hash[:])
}
return s
}
func (k *KRLFingerprintSection) marshal() []byte {
if len(*k) == 0 {
return nil
}
var buf bytes.Buffer
for _, hash := range parseBigEndian(k) {
buf.Write(ssh.Marshal(krlFingerprintSHA1{
PublicKeyHash: hash[:],
}))
}
return ssh.Marshal(krlSection{
SectionType: 3,
SectionData: buf.Bytes(),
})
}
// KRLFingerprintSHA256Section revokes keys by their SHA256 fingerprints. It is
// semantically equivalent to--but is more space efficient than--
// KRLExplicitKeySection.
type KRLFingerprintSHA256Section [][sha256.Size]byte
func (k *KRLFingerprintSHA256Section) isRevoked(key ssh.PublicKey) bool {
sha := sha256.Sum256(marshalPubkey(key))
for _, hash := range *k {
if hash == sha {
return true
}
}
return false
}
func (k *KRLFingerprintSHA256Section) slices() [][]byte {
s := make([][]byte, len(*k))
for i, hash := range *k {
s[i] = make([]byte, len(hash))
copy(s[i], hash[:])
}
return s
}
func (k *KRLFingerprintSHA256Section) marshal() []byte {
if len(*k) == 0 {
return nil
}
var buf bytes.Buffer
for _, hash := range parseBigEndian(k) {
buf.Write(ssh.Marshal(krlFingerprintSHA1{
PublicKeyHash: hash[:],
}))
}
return ssh.Marshal(krlSection{
SectionType: 5,
SectionData: buf.Bytes(),
})
}
// Marshal serializes the KRL and optionally signs it with one or more authority
// keys.
func (k *KRL) Marshal(rand io.Reader, keys ...ssh.Signer) ([]byte, error) {
if k.GeneratedDate == 0 {
k.GeneratedDate = uint64(time.Now().Unix())
}
if k.Version == 0 {
k.Version = k.GeneratedDate
}
k.SigningKeys = nil
var buf bytes.Buffer
buf.Write(ssh.Marshal(krlHeader{
KRLMagic: krlMagic,
KRLFormatVersion: 1,
KRLVersion: k.Version,
GeneratedDate: k.GeneratedDate,
Comment: k.Comment,
}))
for _, section := range k.Sections {
buf.Write(section.marshal())
}
for _, key := range keys {
buf.Write(ssh.Marshal(krlSignatureHeader{
SignatureKey: key.PublicKey().Marshal(),
}))
sig, err := key.Sign(rand, buf.Bytes())
if err != nil {
return nil, err
}
buf.Write(ssh.Marshal(krlSignature{
Signature: ssh.Marshal(sig),
}))
k.SigningKeys = append(k.SigningKeys, key.PublicKey())
}
return buf.Bytes(), nil
}
// IsRevoked returns true if the given key has been revoked by this KRL.
func (k *KRL) IsRevoked(key ssh.PublicKey) bool {
for _, section := range k.Sections {
if section.isRevoked(key) {
return true
}
}
return false
}