-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpolicy_test.go
More file actions
157 lines (143 loc) · 3.98 KB
/
Copy pathpolicy_test.go
File metadata and controls
157 lines (143 loc) · 3.98 KB
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
package torchwood_test
import (
"crypto/ed25519"
"crypto/rand"
"testing"
"filippo.io/mldsa"
"filippo.io/torchwood"
"golang.org/x/mod/sumdb/note"
"golang.org/x/mod/sumdb/tlog"
)
func TestParsePolicyLogKey(t *testing.T) {
origin := "example.com/log"
checkpoint := torchwood.Checkpoint{
Origin: origin,
Tree: tlog.Tree{N: 123, Hash: tlog.RecordHash([]byte("test data"))},
}
t.Run("Ed25519", func(t *testing.T) {
skey, vkey, err := note.GenerateKey(rand.Reader, origin)
if err != nil {
t.Fatal(err)
}
signer, err := note.NewSigner(skey)
if err != nil {
t.Fatal(err)
}
signed, err := note.Sign(¬e.Note{Text: checkpoint.String()}, signer)
if err != nil {
t.Fatal(err)
}
policy, err := torchwood.ParsePolicy([]byte("log " + vkey + "\nquorum none\n"))
if err != nil {
t.Fatal(err)
}
c, _, err := torchwood.VerifyCheckpoint(signed, policy)
if err != nil {
t.Fatal(err)
}
if c.Origin != origin {
t.Errorf("origin = %q; want %q", c.Origin, origin)
}
})
t.Run("ML-DSA", func(t *testing.T) {
k, err := mldsa.GenerateKey(mldsa.MLDSA44())
if err != nil {
t.Fatal(err)
}
signer, err := torchwood.NewCosignatureSigner(origin, k)
if err != nil {
t.Fatal(err)
}
signed, err := note.Sign(¬e.Note{Text: checkpoint.String()}, signer)
if err != nil {
t.Fatal(err)
}
policy, err := torchwood.ParsePolicy([]byte("log " + signer.Verifier().String() + "\nquorum none\n"))
if err != nil {
t.Fatal(err)
}
c, _, err := torchwood.VerifyCheckpoint(signed, policy)
if err != nil {
t.Fatal(err)
}
if c.Origin != origin {
t.Errorf("origin = %q; want %q", c.Origin, origin)
}
// A checkpoint for a different origin doesn't satisfy the policy, even
// if the signature verifies.
other := torchwood.Checkpoint{
Origin: "example.com/other",
Tree: checkpoint.Tree,
}
signedOther, err := note.Sign(¬e.Note{Text: other.String()}, signer)
if err != nil {
t.Fatal(err)
}
if _, _, err := torchwood.VerifyCheckpoint(signedOther, policy); err == nil {
t.Error("expected error verifying checkpoint with wrong origin")
}
})
t.Run("Ed25519 cosignature key rejected", func(t *testing.T) {
_, k, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
signer, err := torchwood.NewCosignatureSigner(origin, k)
if err != nil {
t.Fatal(err)
}
_, err = torchwood.ParsePolicy([]byte("log " + signer.Verifier().String() + "\nquorum none\n"))
if err == nil {
t.Error("expected error parsing log line with Ed25519 cosignature vkey")
}
})
}
func TestParsePolicyMLDSAWithWitness(t *testing.T) {
origin := "example.com/log"
checkpoint := torchwood.Checkpoint{
Origin: origin,
Tree: tlog.Tree{N: 123, Hash: tlog.RecordHash([]byte("test data"))},
}
logKey, err := mldsa.GenerateKey(mldsa.MLDSA44())
if err != nil {
t.Fatal(err)
}
logSigner, err := torchwood.NewCosignatureSigner(origin, logKey)
if err != nil {
t.Fatal(err)
}
witnessKey, err := mldsa.GenerateKey(mldsa.MLDSA44())
if err != nil {
t.Fatal(err)
}
witnessSigner, err := torchwood.NewCosignatureSigner("witness.example/w1", witnessKey)
if err != nil {
t.Fatal(err)
}
policy, err := torchwood.ParsePolicy([]byte(
"log " + logSigner.Verifier().String() + "\n" +
"witness W1 " + witnessSigner.Verifier().String() + "\n" +
"quorum W1\n"))
if err != nil {
t.Fatal(err)
}
signed, err := note.Sign(¬e.Note{Text: checkpoint.String()}, logSigner, witnessSigner)
if err != nil {
t.Fatal(err)
}
c, _, err := torchwood.VerifyCheckpoint(signed, policy)
if err != nil {
t.Fatal(err)
}
if c.Origin != origin {
t.Errorf("origin = %q; want %q", c.Origin, origin)
}
// Without the witness cosignature, the quorum is not satisfied.
signedLogOnly, err := note.Sign(¬e.Note{Text: checkpoint.String()}, logSigner)
if err != nil {
t.Fatal(err)
}
if _, _, err := torchwood.VerifyCheckpoint(signedLogOnly, policy); err == nil {
t.Error("expected error verifying checkpoint without witness cosignature")
}
}