-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathverify_local_test.go
More file actions
220 lines (199 loc) · 6.63 KB
/
Copy pathverify_local_test.go
File metadata and controls
220 lines (199 loc) · 6.63 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
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
package peac
import (
"testing"
"time"
"github.com/peacprotocol/peac/sdks/go/jws"
)
func TestVerifyLocal_Valid(t *testing.T) {
key, _ := jws.GenerateSigningKey("test-key-1")
issued, err := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Clock: FixedClock{Time: time.Now()},
})
if err != nil {
t.Fatal(err)
}
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
})
if !result.Valid {
t.Fatalf("expected valid, got error: %s: %s", result.ErrorCode, result.ErrorMessage)
}
if result.Claims.Iss != "https://example.com" {
t.Errorf("iss = %s, want https://example.com", result.Claims.Iss)
}
if result.Kid != "test-key-1" {
t.Errorf("kid = %s, want test-key-1", result.Kid)
}
if result.WireVersion != PeacVersion {
t.Errorf("wire_version = %s, want %s", result.WireVersion, PeacVersion)
}
if result.ReceiptRef == "" || result.ReceiptRef[:7] != "sha256:" {
t.Errorf("receipt_ref = %s, want sha256:...", result.ReceiptRef)
}
if result.PolicyBinding != PolicyBindingUnavailable {
t.Errorf("policy_binding = %s, want unavailable", result.PolicyBinding)
}
}
func TestVerifyLocal_InvalidSignature(t *testing.T) {
key1, _ := jws.GenerateSigningKey("key-1")
key2, _ := jws.GenerateSigningKey("key-2")
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key1,
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key2.PublicKey(), // wrong key
})
if result.Valid {
t.Fatal("expected invalid for wrong key")
}
if result.ErrorCode != "E_INVALID_SIGNATURE" {
t.Errorf("code = %s, want E_INVALID_SIGNATURE", result.ErrorCode)
}
}
func TestVerifyLocal_Expired(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Exp: time.Now().Add(-1 * time.Hour).Unix(), // expired 1 hour ago
Clock: FixedClock{Time: time.Now().Add(-2 * time.Hour)},
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
})
if result.Valid {
t.Fatal("expected invalid for expired receipt")
}
if result.ErrorCode != "E_EXPIRED" {
t.Errorf("code = %s, want E_EXPIRED", result.ErrorCode)
}
}
func TestVerifyLocal_IssuerMismatch(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
Issuer: "https://other.example.com",
})
if result.Valid {
t.Fatal("expected invalid for issuer mismatch")
}
if result.ErrorCode != "E_INVALID_ISSUER" {
t.Errorf("code = %s, want E_INVALID_ISSUER", result.ErrorCode)
}
}
func TestVerifyLocal_PolicyBindingVerified(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
policy := []byte(`{"rule": "allow"}`)
policyDigest, _ := ComputePolicyDigest(policy)
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Policy: &PolicyBlock{Digest: policyDigest},
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
PolicyBytes: policy,
})
if !result.Valid {
t.Fatalf("expected valid, got: %s: %s", result.ErrorCode, result.ErrorMessage)
}
if result.PolicyBinding != PolicyBindingVerified {
t.Errorf("policy_binding = %s, want verified", result.PolicyBinding)
}
}
func TestVerifyLocal_PolicyBindingFailed(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
Policy: &PolicyBlock{Digest: "sha256:0000000000000000000000000000000000000000000000000000000000000000"},
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
PolicyBytes: []byte(`{"rule": "deny"}`),
})
if result.Valid {
t.Fatal("expected invalid for policy mismatch")
}
if result.ErrorCode != "E_POLICY_BINDING_FAILED" {
t.Errorf("code = %s, want E_POLICY_BINDING_FAILED", result.ErrorCode)
}
}
func TestVerifyLocal_RejectsWire01(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
// Sign with Wire 0.1 typ
payload := []byte(`{"iss":"https://example.com","iat":1700000000}`)
jwsStr, _ := key.SignWithType(payload, "peac-receipt/0.1")
result := VerifyLocal(jwsStr, VerifyLocalOptions{
PublicKey: key.PublicKey(),
})
if result.Valid {
t.Fatal("expected invalid for Wire 0.1 typ")
}
if result.ErrorCode != "E_UNSUPPORTED_WIRE_VERSION" {
t.Errorf("code = %s, want E_UNSUPPORTED_WIRE_VERSION", result.ErrorCode)
}
}
func TestVerifyLocal_JOSEHardening(t *testing.T) {
// These tests verify JOSE hardening rejects unsafe headers.
// Since we can't easily inject custom headers through Issue(),
// we test the checkJOSEHardening function directly.
tests := []struct {
name string
header string
wantErr bool
}{
{"clean header", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt"}`, false},
{"reject jwk", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","jwk":{}}`, true},
{"reject x5c", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","x5c":[]}`, true},
{"reject crit", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","crit":["b64"]}`, true},
{"reject b64:false", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","b64":false}`, true},
{"reject zip", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","zip":"DEF"}`, true},
{"allow b64:true", `{"alg":"EdDSA","kid":"k1","typ":"interaction-record+jwt","b64":true}`, false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := checkJOSEHardening([]byte(tc.header))
if (err != nil) != tc.wantErr {
t.Errorf("checkJOSEHardening() err = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func TestVerifyLocal_ReceiptRefMatchesSHA256(t *testing.T) {
key, _ := jws.GenerateSigningKey("key-1")
issued, _ := Issue(IssueOptions{
Iss: "https://example.com",
Kind: KindEvidence,
Type: "org.peacprotocol/test",
SigningKey: key,
})
result := VerifyLocal(issued.JWS, VerifyLocalOptions{
PublicKey: key.PublicKey(),
})
if !result.Valid {
t.Fatal("expected valid")
}
if len(result.ReceiptRef) != 71 {
t.Errorf("receipt_ref length = %d, want 71", len(result.ReceiptRef))
}
}