1
2
3
4
5 package upki
6
7 import (
8 "crypto/sha256"
9 "encoding/binary"
10 "errors"
11 "fmt"
12 "math/bits"
13
14 "golang.org/x/crypto/cryptobyte"
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 var (
41 errUnsupportedFormat = errors.New("upki: unsupported clubcard version")
42 errDeserialize = errors.New("upki: failed to deserialize clubcard")
43 )
44
45
46
47
48 type revocationFilter struct {
49 coverage coverage
50 filter filter
51 }
52
53
54
55
56
57 func (c *revocationFilter) contains(key *lookupKey, scts []SCT) filterStatus {
58 for _, sct := range scts {
59 status := c.containsAt(key, sct)
60 if status == statusNotCovered {
61 continue
62 }
63
64 return status
65 }
66
67 return statusNotCovered
68 }
69
70
71
72 type lookupKey struct {
73 issuer issuerSPKIHash
74 serial []byte
75 issuerSerialHash [32]byte
76 }
77
78
79 type issuerSPKIHash [32]byte
80
81
82 func newLookupKey(issuer issuerSPKIHash, serial []byte) lookupKey {
83 h := sha256.New()
84 h.Write(issuer[:])
85 h.Write(serial)
86 var sum [32]byte
87 h.Sum(sum[:0])
88
89 return lookupKey{issuer: issuer, serial: serial, issuerSerialHash: sum}
90 }
91
92
93 type filterStatus int
94
95 const (
96
97 statusGood filterStatus = iota
98
99
100
101
102 statusNotCovered
103
104
105
106
107 statusNotEnrolled
108
109 statusRevoked
110 )
111
112
113 func (c *revocationFilter) containsAt(key *lookupKey, sct SCT) filterStatus {
114 iv, ok := c.coverage[sct.logID]
115 if !ok || sct.timestamp < iv.low || sct.timestamp > iv.high {
116 return statusNotCovered
117 }
118
119 meta, ok := c.filter.blocks[key.issuer]
120 if !ok {
121 return statusNotEnrolled
122 }
123
124 if c.filter.contains(&meta, key.issuerSerialHash, key.serial) {
125 return statusRevoked
126 }
127
128 return statusGood
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 func parseFilter(data []byte) (*revocationFilter, error) {
151 s := cryptobyte.String(data)
152
153
154 var version, reserved0 uint8
155 if !s.ReadUint8(&version) || !s.ReadUint8(&reserved0) {
156 return nil, fmt.Errorf("%w: version", errDeserialize)
157 }
158
159
160 if version != 4 {
161 return nil, fmt.Errorf("%w: %d", errUnsupportedFormat, version)
162 }
163
164
165 if reserved0 != 0 {
166 return nil, fmt.Errorf("%w: non-zero reserved0 byte %d", errDeserialize, reserved0)
167 }
168
169 cov, err := readCoverage(&s)
170 if err != nil {
171 return nil, err
172 }
173
174 filter, err := readFilter(&s)
175 if err != nil {
176 return nil, err
177 }
178
179 if !s.Empty() {
180 return nil, fmt.Errorf("%w: %d trailing bytes", errDeserialize, len(s))
181 }
182
183 return &revocationFilter{coverage: cov, filter: filter}, nil
184 }
185
186
187
188
189
190
191
192
193
194 func readCoverage(s *cryptobyte.String) (coverage, error) {
195 var count uint16
196 if !s.ReadUint16(&count) {
197 return nil, fmt.Errorf("%w: coverage count", errDeserialize)
198 }
199
200
201
202 const coverageEntrySize = 32 + 8 + 8
203 if uint64(count)*coverageEntrySize > uint64(len(*s)) {
204 return nil, fmt.Errorf("%w: coverage count %d exceeds input", errDeserialize, count)
205 }
206
207 cov := make(coverage, count)
208 for i := uint16(0); i < count; i++ {
209 id, err := readLogId(s)
210 if err != nil {
211 return nil, err
212 }
213
214 iv, err := readTimestampInterval(s)
215 if err != nil {
216 return nil, err
217 }
218
219
220
221 if _, dup := cov[id]; dup {
222 return nil, fmt.Errorf("%w: duplicate coverage log id", errDeserialize)
223 }
224
225 cov[id] = iv
226 }
227
228 return cov, nil
229 }
230
231
232
233 type coverage map[logID]timestampInterval
234
235
236 type timestampInterval struct {
237 low timestamp
238 high timestamp
239 }
240
241
242
243
244 func readLogId(s *cryptobyte.String) (logID, error) {
245 var id logID
246 if !s.CopyBytes(id[:]) {
247 return id, fmt.Errorf("%w: log id", errDeserialize)
248 }
249
250 return id, nil
251 }
252
253
254
255
256
257
258
259
260
261 func readTimestampInterval(s *cryptobyte.String) (timestampInterval, error) {
262 var low, high uint64
263 if !s.ReadUint64(&low) || !s.ReadUint64(&high) {
264 return timestampInterval{}, fmt.Errorf("%w: timestamp interval", errDeserialize)
265 }
266
267 return timestampInterval{low: timestamp(low), high: timestamp(high)}, nil
268 }
269
270
271 func readFilter(s *cryptobyte.String) (filter, error) {
272 var f filter
273
274 blocks, err := readBlocks(s)
275 if err != nil {
276 return f, err
277 }
278 f.blocks = blocks
279
280 var columnCount uint8
281 if !s.ReadUint8(&columnCount) {
282 return f, fmt.Errorf("%w: approx filter column count", errDeserialize)
283 }
284
285 f.x = make([][]uint64, columnCount)
286 for i := range f.x {
287 col, err := readU64Seq(s)
288 if err != nil {
289 return f, fmt.Errorf("approx filter column %d/%d: %w", i, columnCount, err)
290 }
291
292 f.x[i] = col
293 }
294
295 f.y, err = readU64Seq(s)
296 if err != nil {
297 return f, fmt.Errorf("exact filter: %w", err)
298 }
299
300 if err := validateBlocks(&f); err != nil {
301 return f, err
302 }
303
304 return f, nil
305 }
306
307
308
309 func readU64Seq(s *cryptobyte.String) ([]uint64, error) {
310 var count uint32
311 if !s.ReadUint32(&count) {
312 return nil, fmt.Errorf("%w: u64 seq count", errDeserialize)
313 }
314
315
316
317 if uint64(count)*8 > uint64(len(*s)) {
318 return nil, fmt.Errorf("%w: u64 seq count %d exceeds input", errDeserialize, count)
319 }
320
321 items := make([]uint64, count)
322 for i := range items {
323 if !s.ReadUint64(&items[i]) {
324 return nil, fmt.Errorf("%w: u64 seq item %d/%d", errDeserialize, i, count)
325 }
326 }
327
328 return items, nil
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 func readBlocks(s *cryptobyte.String) (map[[32]byte]blockMeta, error) {
345 var count uint32
346 if !s.ReadUint32(&count) {
347 return nil, fmt.Errorf("%w: index count", errDeserialize)
348 }
349
350
351
352
353 const minIndexEntrySize = 32 +
354 4 +
355 1 +
356 4 +
357 4 +
358 4 +
359 1 +
360 2
361 if uint64(count)*minIndexEntrySize > uint64(len(*s)) {
362 return nil, fmt.Errorf("%w: index count %d exceeds input", errDeserialize, count)
363 }
364
365 blocks := make(map[[32]byte]blockMeta, count)
366 for i := uint32(0); i < count; i++ {
367 var id [32]byte
368 if !s.CopyBytes(id[:]) {
369 return nil, fmt.Errorf("%w: index block id %d/%d", errDeserialize, i, count)
370 }
371
372 meta, err := readBlockMeta(s)
373 if err != nil {
374 return nil, err
375 }
376
377 if _, dup := blocks[id]; dup {
378 return nil, fmt.Errorf("%w: duplicate index block id", errDeserialize)
379 }
380
381 blocks[id] = meta
382 }
383
384 return blocks, nil
385 }
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403 func readBlockMeta(s *cryptobyte.String) (blockMeta, error) {
404 var (
405 hMod, hOff, gMod, gOff uint32
406 rank, inverted uint8
407 )
408 if !s.ReadUint32(&hMod) ||
409 !s.ReadUint8(&rank) ||
410 !s.ReadUint32(&hOff) ||
411 !s.ReadUint32(&gMod) ||
412 !s.ReadUint32(&gOff) ||
413 !s.ReadUint8(&inverted) {
414 return blockMeta{}, fmt.Errorf("%w: block meta header", errDeserialize)
415 }
416
417
418 if inverted > 1 {
419 return blockMeta{}, fmt.Errorf("%w: inverted flag %d not 0 or 1", errDeserialize, inverted)
420 }
421
422 var count uint16
423 if !s.ReadUint16(&count) {
424 return blockMeta{}, fmt.Errorf("%w: block meta exception count", errDeserialize)
425 }
426
427
428 if int(count) > len(*s) {
429 return blockMeta{}, fmt.Errorf("%w: block meta exception count %d exceeds input", errDeserialize, count)
430 }
431
432
433
434 var exceptions map[string]struct{}
435 if count > 0 {
436 exceptions = make(map[string]struct{}, count)
437 }
438 for i := uint16(0); i < count; i++ {
439 var serial cryptobyte.String
440 if !s.ReadUint8LengthPrefixed(&serial) {
441 return blockMeta{}, fmt.Errorf("%w: block meta exception %d/%d", errDeserialize, i, count)
442 }
443
444 exceptions[string(serial)] = struct{}{}
445 }
446
447 return blockMeta{
448 hOffset: uint64(hOff),
449 hModulus: uint64(hMod),
450 hRank: rank,
451 gOffset: uint64(gOff),
452 gModulus: uint64(gMod),
453 exceptions: exceptions,
454 inverted: inverted != 0,
455 }, nil
456 }
457
458
459
460
461
462
463
464
465
466
467
468 func validateBlocks(f *filter) error {
469 for id, meta := range f.blocks {
470 if int(meta.hRank) > len(f.x) {
471 return fmt.Errorf("%w: block %x rank %d exceeds column count %d",
472 errDeserialize, id, meta.hRank, len(f.x))
473 }
474 }
475
476 return nil
477 }
478
479
480
481
482
483
484
485
486
487
488 type filter struct {
489 x [][]uint64
490 y []uint64
491 blocks map[[32]byte]blockMeta
492 }
493
494
495
496
497
498
499
500
501 func (f *filter) contains(meta *blockMeta, a [32]byte, serial []byte) bool {
502
503
504
505
506
507 if meta.hModulus == 0 {
508 return meta.inverted
509 }
510
511 aLimbs := bytesToLimbs(a)
512
513
514
515
516 aLimbs[0] |= 1
517 t := aLimbs[3]
518
519 hRow := meta.hOffset + (t % meta.hModulus)
520 for i := uint8(0); i < meta.hRank; i++ {
521 if !bitsXorIsZero(f.x[i], hRow, aLimbs) {
522 return meta.inverted
523 }
524 }
525
526
527
528
529
530
531 gRow := meta.gOffset + (t % max(1, meta.gModulus))
532 if !bitsXorIsZero(f.y, gRow, aLimbs) {
533 return meta.inverted
534 }
535
536 if _, isException := meta.exceptions[string(serial)]; isException {
537 return meta.inverted
538 }
539
540 return !meta.inverted
541 }
542
543
544
545
546
547
548
549
550
551
552
553
554
555 type blockMeta struct {
556 hOffset uint64
557 hModulus uint64
558 hRank uint8
559
560 gOffset uint64
561 gModulus uint64
562
563
564
565
566
567
568 exceptions map[string]struct{}
569
570
571
572 inverted bool
573 }
574
575
576
577
578 func bytesToLimbs(a [32]byte) [ribbonWidth]uint64 {
579 return [ribbonWidth]uint64{
580 binary.LittleEndian.Uint64(a[0:8]),
581 binary.LittleEndian.Uint64(a[8:16]),
582 binary.LittleEndian.Uint64(a[16:24]),
583 binary.LittleEndian.Uint64(a[24:32]),
584 }
585 }
586
587
588
589
590
591 func bitsXorIsZero(column []uint64, rowOffset uint64, coef [ribbonWidth]uint64) bool {
592 limb := rowOffset / 64
593 shift := rowOffset % 64
594
595 var r uint64
596 for i := limb; i < min(uint64(len(column)), limb+ribbonWidth); i++ {
597 tmp := column[i] >> shift
598 if shift != 0 && i+1 < uint64(len(column)) {
599 tmp |= column[i+1] << (64 - shift)
600 }
601 r ^= tmp & coef[i-limb]
602 }
603
604 return bits.OnesCount64(r)&1 == 0
605 }
606
607
608 const ribbonWidth = 4
609
View as plain text