-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tile.go
178 lines (162 loc) · 5.21 KB
/
tile.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
package sunlight
import (
"fmt"
"math"
"strings"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/mod/sumdb/tlog"
)
const TileHeight = 8
const TileWidth = 1 << TileHeight
// TilePath returns a tile coordinate path describing t, according to
// c2sp.org/sunlight. It differs from [tlog.Tile.Path] in that it doesn't
// include an explicit tile height.
//
// If t.Height is not TileHeight, TilePath panics.
func TilePath(t tlog.Tile) string {
if t.H != TileHeight {
panic(fmt.Sprintf("unexpected tile height %d", t.H))
}
return "tile/" + strings.TrimPrefix(t.Path(), "tile/8/")
}
type LogEntry struct {
// Certificate is either the TimestampedEntry.signed_entry, or the
// PreCert.tbs_certificate for Precertificates.
// It must be at most 2^24-1 bytes long.
Certificate []byte
// IsPrecert is true if LogEntryType is precert_entry. Otherwise, the
// following three fields are zero and ignored.
IsPrecert bool
// IssuerKeyHash is the PreCert.issuer_key_hash.
IssuerKeyHash [32]byte
// ChainFingerprints are the SHA-256 hashes of the certificates in the
// X509ChainEntry.certificate_chain or
// PrecertChainEntry.precertificate_chain.
ChainFingerprints [][32]byte
// PreCertificate is the PrecertChainEntry.pre_certificate.
// It must be at most 2^24-1 bytes long.
PreCertificate []byte
// LeafIndex is the zero-based index of the leaf in the log.
// It must be between 0 and 2^40-1.
LeafIndex int64
// Timestamp is the TimestampedEntry.timestamp.
Timestamp int64
}
// MerkleTreeLeaf returns a RFC 6962 MerkleTreeLeaf.
func (e *LogEntry) MerkleTreeLeaf() []byte {
b := &cryptobyte.Builder{}
b.AddUint8(0 /* version = v1 */)
b.AddUint8(0 /* leaf_type = timestamped_entry */)
b.AddUint64(uint64(e.Timestamp))
if !e.IsPrecert {
b.AddUint16(0 /* entry_type = x509_entry */)
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(e.Certificate)
})
} else {
b.AddUint16(1 /* entry_type = precert_entry */)
b.AddBytes(e.IssuerKeyHash[:])
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(e.Certificate)
})
}
addExtensions(b, e.LeafIndex)
return b.BytesOrPanic()
}
// struct {
// TimestampedEntry timestamped_entry;
// select (entry_type) {
// case x509_entry: Empty;
// case precert_entry: ASN.1Cert pre_certificate;
// };
// Fingerprint certificate_chain<0..2^16-1>;
// } TileLeaf;
//
// opaque Fingerprint[32];
// ReadTileLeaf reads a LogEntry from a data tile, and returns the remaining
// data in the tile.
func ReadTileLeaf(tile []byte) (e *LogEntry, rest []byte, err error) {
e = &LogEntry{}
s := cryptobyte.String(tile)
var timestamp uint64
var entryType uint16
var extensions, fingerprints cryptobyte.String
if !s.ReadUint64(×tamp) || !s.ReadUint16(&entryType) || timestamp > math.MaxInt64 {
return nil, s, fmt.Errorf("invalid data tile")
}
e.Timestamp = int64(timestamp)
switch entryType {
case 0: // x509_entry
if !s.ReadUint24LengthPrefixed((*cryptobyte.String)(&e.Certificate)) ||
!s.ReadUint16LengthPrefixed(&extensions) ||
!s.ReadUint16LengthPrefixed(&fingerprints) {
return nil, s, fmt.Errorf("invalid data tile x509_entry")
}
case 1: // precert_entry
e.IsPrecert = true
if !s.CopyBytes(e.IssuerKeyHash[:]) ||
!s.ReadUint24LengthPrefixed((*cryptobyte.String)(&e.Certificate)) ||
!s.ReadUint16LengthPrefixed(&extensions) ||
!s.ReadUint24LengthPrefixed((*cryptobyte.String)(&e.PreCertificate)) ||
!s.ReadUint16LengthPrefixed(&fingerprints) {
return nil, s, fmt.Errorf("invalid data tile precert_entry")
}
default:
return nil, s, fmt.Errorf("invalid data tile: unknown type %d", entryType)
}
var extensionType uint8
var extensionData cryptobyte.String
if !extensions.ReadUint8(&extensionType) || extensionType != 0 ||
!extensions.ReadUint16LengthPrefixed(&extensionData) ||
!readUint40(&extensionData, &e.LeafIndex) || !extensionData.Empty() ||
!extensions.Empty() {
return nil, s, fmt.Errorf("invalid data tile extensions")
}
for !fingerprints.Empty() {
var f [32]byte
if !fingerprints.CopyBytes(f[:]) {
return nil, s, fmt.Errorf("invalid data tile fingerprints")
}
e.ChainFingerprints = append(e.ChainFingerprints, f)
}
return e, s, nil
}
// AppendTileLeaf appends a LogEntry to a data tile.
func AppendTileLeaf(t []byte, e *LogEntry) []byte {
b := cryptobyte.NewBuilder(t)
b.AddUint64(uint64(e.Timestamp))
if !e.IsPrecert {
b.AddUint16(0 /* entry_type = x509_entry */)
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(e.Certificate)
})
} else {
b.AddUint16(1 /* entry_type = precert_entry */)
b.AddBytes(e.IssuerKeyHash[:])
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(e.Certificate)
})
}
addExtensions(b, e.LeafIndex)
if e.IsPrecert {
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(e.PreCertificate)
})
}
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
for _, f := range e.ChainFingerprints {
b.AddBytes(f[:])
}
})
return b.BytesOrPanic()
}
func addExtensions(b *cryptobyte.Builder, leafIndex int64) {
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
ext, err := MarshalExtensions(Extensions{LeafIndex: leafIndex})
if err != nil {
b.SetError(err)
return
}
b.AddBytes(ext)
})
}