Skip to content

Commit f14b4aa

Browse files
committed
Output the tree in tlog-tiles format
1 parent 7006b82 commit f14b4aa

2 files changed

Lines changed: 97 additions & 6 deletions

File tree

demo/main.go

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"log"
1010
"os"
1111
"path/filepath"
12+
"strconv"
13+
14+
"golang.org/x/crypto/cryptobyte"
1215
)
1316

1417
var (
@@ -24,6 +27,31 @@ type certificateInfo struct {
2427
num int
2528
}
2629

30+
func makeDirsAndWriteFile(name string, data []byte) error {
31+
if err := os.MkdirAll(filepath.Dir(name), 0755); err != nil {
32+
return err
33+
}
34+
return os.WriteFile(name, data, 0644)
35+
}
36+
37+
func tlogIndex(n int, partial bool) string {
38+
if n < 0 {
39+
panic("negative tlog index")
40+
}
41+
var s string
42+
if partial {
43+
s = fmt.Sprintf("%03d.p", n%1000)
44+
} else {
45+
s = fmt.Sprintf("%03d", n%1000)
46+
}
47+
n /= 1000
48+
for n != 0 {
49+
s = filepath.Join(fmt.Sprintf("x%03d", n%1000), s)
50+
n /= 1000
51+
}
52+
return s
53+
}
54+
2755
func do() error {
2856
// Load the config.
2957
configBytes, err := os.ReadFile(*flagConfig)
@@ -128,7 +156,7 @@ func do() error {
128156
issuanceLog := NewMerkleTree(entries)
129157

130158
// Construct certificates.
131-
if err := os.MkdirAll(*flagOutDir, 0750); err != nil {
159+
if err := os.MkdirAll(*flagOutDir, 0755); err != nil {
132160
log.Fatal(err)
133161
}
134162
for _, info := range certInfos {
@@ -145,7 +173,50 @@ func do() error {
145173
}
146174
}
147175

148-
// TODO: Also write out tiles and a checkpoint in tlog-checkpoint format.
176+
// Write out the tree in tlog-tiles format.
177+
tileDir := filepath.Join(*flagOutDir, "tile")
178+
179+
entryDir := filepath.Join(tileDir, "entries")
180+
for i := 0; 256*i < len(entries); i++ {
181+
bundle := cryptobyte.NewBuilder(nil)
182+
for j := 256 * i; j < 256*(i+1) && j < len(entries); j++ {
183+
bundle.AddUint16LengthPrefixed(func(child *cryptobyte.Builder) { child.AddBytes(entries[j]) })
184+
}
185+
var path string
186+
if 256*(i+1) <= len(entries) {
187+
path = filepath.Join(entryDir, tlogIndex(i, false))
188+
} else {
189+
path = filepath.Join(entryDir, tlogIndex(i, true), strconv.Itoa(len(entries)-256*i))
190+
}
191+
data, err := bundle.Bytes()
192+
if err != nil {
193+
return err
194+
}
195+
if err := makeDirsAndWriteFile(path, data); err != nil {
196+
return err
197+
}
198+
}
199+
200+
for l := 0; l < len(issuanceLog.levels); l += 8 {
201+
level := issuanceLog.levels[l]
202+
for i := 0; 256*i < len(level); i++ {
203+
var tile []byte
204+
for j := 256 * i; j < 256*(i+1) && j < len(level); j++ {
205+
tile = append(tile, level[j][:]...)
206+
}
207+
var path string
208+
if 256*(i+1) <= len(level) {
209+
path = filepath.Join(tileDir, strconv.Itoa(l/8), tlogIndex(i, false))
210+
} else {
211+
path = filepath.Join(tileDir, strconv.Itoa(l/8), tlogIndex(i, true), strconv.Itoa(len(level)-256*i))
212+
}
213+
if err := makeDirsAndWriteFile(path, tile); err != nil {
214+
return err
215+
}
216+
}
217+
}
218+
219+
// TODO: Also write a checkpoint in tlog-checkpoint format.
149220
return nil
150221
}
151222

demo/mtc.json

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
2424
"NotBefore": "2020-01-01T00:00:00Z",
2525
"NotAfter": "2030-12-31T23:59:59Z",
26-
"DNSNames": ["a.example", "b.example", "*.c.example"],
26+
"DNSNames": ["a.example"],
2727
"KeyUsage": ["DigitalSignature"],
2828
"ExtKeyUsage": ["ServerAuth"]
2929
},
3030
{
3131
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
3232
"NotBefore": "2020-01-01T00:00:00Z",
3333
"NotAfter": "2030-12-31T23:59:59Z",
34-
"DNSNames": ["a.example", "b.example", "*.c.example"],
34+
"DNSNames": ["b.example", "*.blah.b.example"],
3535
"KeyUsage": ["DigitalSignature"],
3636
"ExtKeyUsage": ["ServerAuth"],
3737
"Checkpoints": ["fast"],
@@ -45,7 +45,7 @@
4545
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
4646
"NotBefore": "2020-01-01T00:00:00Z",
4747
"NotAfter": "2030-12-31T23:59:59Z",
48-
"DNSNames": ["a.example", "b.example", "*.c.example"],
48+
"DNSNames": ["c.example"],
4949
"KeyUsage": ["DigitalSignature"],
5050
"ExtKeyUsage": ["ServerAuth"]
5151
},
@@ -63,7 +63,27 @@
6363
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
6464
"NotBefore": "2020-01-01T00:00:00Z",
6565
"NotAfter": "2030-12-31T23:59:59Z",
66-
"DNSNames": ["a.example", "b.example", "*.c.example"],
66+
"DNSNames": ["d.example"],
67+
"KeyUsage": ["DigitalSignature"],
68+
"ExtKeyUsage": ["ServerAuth"],
69+
"Checkpoints": ["fast", "landmark"],
70+
"Certificates": [
71+
{"Checkpoint": "fast", "Cosigners": ["32473.2.1", "32473.2.3"]},
72+
{"Checkpoint": "landmark"}
73+
]
74+
},
75+
{
76+
"Repeat": 2000,
77+
"Subject": {"CommonName": "Some more filler certificates"},
78+
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
79+
"NotBefore": "2020-01-01T00:00:00Z",
80+
"NotAfter": "2030-12-31T23:59:59Z"
81+
},
82+
{
83+
"PublicKey": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyxiC16lts+SOL87OwHhGDzK9/dyUd7sM80jAf53VL+AV38HoHKpmA7LJoYLf4yVhcVleNcxufsAfcPueTLX0Vg==",
84+
"NotBefore": "2020-01-01T00:00:00Z",
85+
"NotAfter": "2030-12-31T23:59:59Z",
86+
"DNSNames": ["e.example"],
6787
"KeyUsage": ["DigitalSignature"],
6888
"ExtKeyUsage": ["ServerAuth"],
6989
"Checkpoints": ["fast", "landmark"],

0 commit comments

Comments
 (0)