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
|
package types
import (
"bytes"
"crypto"
"fmt"
"io"
"reflect"
"testing"
"git.sigsum.org/sigsum-go/internal/mocks/signer"
"git.sigsum.org/sigsum-go/pkg/hex"
"git.sigsum.org/sigsum-go/pkg/merkle"
)
func TestTreeHeadToBinary(t *testing.T) {
desc := "valid"
kh := merkle.Hash{}
if got, want := validTreeHead(t).ToBinary(&kh), validTreeHeadBytes(t, &kh); !bytes.Equal(got, want) {
t.Errorf("got tree head\n\t%v\nbut wanted\n\t%v\nin test %q\n", got, want, desc)
}
}
func TestTreeHeadSign(t *testing.T) {
for _, table := range []struct {
desc string
th *TreeHead
signer crypto.Signer
wantSig *Signature
wantErr bool
}{
{
desc: "invalid: signer error",
th: validTreeHead(t),
signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], fmt.Errorf("signing error")},
wantErr: true,
},
{
desc: "valid",
th: validTreeHead(t),
signer: &signer.Signer{newPubBufferInc(t)[:], newSigBufferInc(t)[:], nil},
wantSig: newSigBufferInc(t),
},
} {
logKey := PublicKey{}
sth, err := table.th.Sign(table.signer, merkle.HashFn(logKey[:]))
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
}
if err != nil {
continue
}
wantSTH := &SignedTreeHead{
TreeHead: *table.th,
Signature: *table.wantSig,
}
if got, want := sth, wantSTH; !reflect.DeepEqual(got, want) {
t.Errorf("got sth\n\t%v\nbut wanted\n\t%v\nin test %q", got, want, table.desc)
}
}
}
func TestSignedTreeHeadToASCII(t *testing.T) {
desc := "valid"
buf := bytes.NewBuffer(nil)
if err := validSignedTreeHead(t).ToASCII(buf); err != nil {
t.Fatalf("got error true but wanted false in test %q: %v", desc, err)
}
if got, want := string(buf.Bytes()), validSignedTreeHeadASCII(t); got != want {
t.Errorf("got signed tree head\n\t%v\nbut wanted\n\t%v\nin test %q\n", got, want, desc)
}
}
func TestSignedTreeHeadFromASCII(t *testing.T) {
for _, table := range []struct {
desc string
serialized io.Reader
wantErr bool
want *SignedTreeHead
}{
{
desc: "invalid: not a signed tree head (unexpected key-value pair)",
serialized: bytes.NewBuffer(append(
[]byte(validSignedTreeHeadASCII(t)),
[]byte("key=4")...),
),
wantErr: true,
},
{
desc: "valid",
serialized: bytes.NewBuffer([]byte(validSignedTreeHeadASCII(t))),
want: validSignedTreeHead(t),
},
} {
var sth SignedTreeHead
err := sth.FromASCII(table.serialized)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
}
if err != nil {
continue
}
if got, want := &sth, table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got signed tree head\n\t%v\nbut wanted\n\t%v\nin test %q\n", got, want, table.desc)
}
}
}
func TestSignedTreeHeadVerify(t *testing.T) {
th := validTreeHead(t)
signer, pub := newKeyPair(t)
kh := merkle.HashFn(pub[:])
sth, err := th.Sign(signer, kh)
if err != nil {
t.Fatal(err)
}
if !sth.Verify(&pub, kh) {
t.Errorf("failed verifying a valid signed tree head")
}
sth.TreeSize += 1
if sth.Verify(&pub, kh) {
t.Errorf("succeeded verifying an invalid signed tree head")
}
}
func TestCosignedTreeHeadToASCII(t *testing.T) {
desc := "valid"
buf := bytes.NewBuffer(nil)
if err := validCosignedTreeHead(t).ToASCII(buf); err != nil {
t.Fatalf("got error true but wanted false in test %q: %v", desc, err)
}
if got, want := string(buf.Bytes()), validCosignedTreeHeadASCII(t); got != want {
t.Errorf("got cosigned tree head\n\t%v\nbut wanted\n\t%v\nin test %q\n", got, want, desc)
}
}
func TestCosignedTreeHeadFromASCII(t *testing.T) {
for _, table := range []struct {
desc string
serialized io.Reader
wantErr bool
want *CosignedTreeHead
}{
{
desc: "invalid: not a cosigned tree head (unexpected key-value pair)",
serialized: bytes.NewBuffer(append(
[]byte(validCosignedTreeHeadASCII(t)),
[]byte("key=4")...),
),
wantErr: true,
},
{
desc: "invalid: not a cosigned tree head (not enough cosignatures)",
serialized: bytes.NewBuffer(append(
[]byte(validCosignedTreeHeadASCII(t)),
[]byte(fmt.Sprintf("key_hash=%x\n", merkle.Hash{}))...,
)),
wantErr: true,
},
{
desc: "valid",
serialized: bytes.NewBuffer([]byte(validCosignedTreeHeadASCII(t))),
want: validCosignedTreeHead(t),
},
} {
var cth CosignedTreeHead
err := cth.FromASCII(table.serialized)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
}
if err != nil {
continue
}
if got, want := &cth, table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got cosigned tree head\n\t%v\nbut wanted\n\t%v\nin test %q\n", got, want, table.desc)
}
}
}
func validTreeHead(t *testing.T) *TreeHead {
return &TreeHead{
Timestamp: 72623859790382856,
TreeSize: 257,
RootHash: *newHashBufferInc(t),
}
}
func validTreeHeadBytes(t *testing.T, keyHash *merkle.Hash) []byte {
ns := fmt.Sprintf("tree_head:v0:%s@sigsum.org", hex.Serialize(keyHash[:]))
th := bytes.Join([][]byte{
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01},
newHashBufferInc(t)[:],
}, nil)
return bytes.Join([][]byte{
[]byte("SSHSIG"),
[]byte{0, 0, 0, 88}, []byte(ns),
[]byte{0, 0, 0, 0},
[]byte{0, 0, 0, 6}, []byte("sha256"),
[]byte{0, 0, 0, 32}, (*merkle.HashFn(th))[:],
}, nil)
}
func validSignedTreeHead(t *testing.T) *SignedTreeHead {
t.Helper()
return &SignedTreeHead{
TreeHead: TreeHead{
Timestamp: 1,
TreeSize: 2,
RootHash: *newHashBufferInc(t),
},
Signature: *newSigBufferInc(t),
}
}
func validSignedTreeHeadASCII(t *testing.T) string {
t.Helper()
return fmt.Sprintf("%s=%d\n%s=%d\n%s=%x\n%s=%x\n",
"timestamp", 1,
"tree_size", 2,
"root_hash", newHashBufferInc(t)[:],
"signature", newSigBufferInc(t)[:],
)
}
func validCosignedTreeHead(t *testing.T) *CosignedTreeHead {
t.Helper()
return &CosignedTreeHead{
SignedTreeHead: SignedTreeHead{
TreeHead: TreeHead{
Timestamp: 1,
TreeSize: 2,
RootHash: *newHashBufferInc(t),
},
Signature: *newSigBufferInc(t),
},
Cosignature: []Signature{
Signature{},
*newSigBufferInc(t),
},
KeyHash: []merkle.Hash{
merkle.Hash{},
*newHashBufferInc(t),
},
}
}
func validCosignedTreeHeadASCII(t *testing.T) string {
t.Helper()
return fmt.Sprintf("%s=%d\n%s=%d\n%s=%x\n%s=%x\n%s=%x\n%s=%x\n%s=%x\n%s=%x\n",
"timestamp", 1,
"tree_size", 2,
"root_hash", newHashBufferInc(t)[:],
"signature", newSigBufferInc(t)[:],
"cosignature", Signature{},
"cosignature", newSigBufferInc(t)[:],
"key_hash", merkle.Hash{},
"key_hash", newHashBufferInc(t)[:],
)
}
|