-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode_test.go
More file actions
304 lines (283 loc) · 7.29 KB
/
Copy pathencode_decode_test.go
File metadata and controls
304 lines (283 loc) · 7.29 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package securly
import (
"context"
"crypto/x509"
"errors"
"testing"
"time"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type encodeDecodeTest struct {
desc string
encOpts []SignOption
input Message
encErr error
decOpts []DecoderOption
decErr error
output *Message // set if different than the input
}
var simpleWorking = encodeDecodeTest{
desc: "simple, working",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
RequirePolicies("1.2.100"),
},
}
var complexWorking = encodeDecodeTest{
desc: "complex, working",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
Files: map[string]File{
"file1": {
Data: []byte("file1 contents"),
},
"file2": {
Data: []byte("file2 contents"),
},
},
Response: &Encryption{
Alg: jwa.ECDH_ES,
Key: mustFromRaw(chainA.Leaf().Public.PublicKey),
},
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
RequirePolicies("1.2.100"),
Require(
// Show this verifier is called and works.
VerifierFunc(
func(_ context.Context, _ []*x509.Certificate, _ time.Time) error {
return nil
})),
},
}
var encodeDecodeTests = []encodeDecodeTest{
simpleWorking,
complexWorking,
{
desc: "No trusted roots",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decErr: errUnknown,
}, {
desc: "Require() not met",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
RequirePolicies("1.2.100"),
Require(
VerifierFunc(
func(_ context.Context, _ []*x509.Certificate, _ time.Time) error {
return errors.New("custom verifier failed")
})),
},
decErr: errUnknown,
}, {
desc: "NoSignature()/NoVerification(), working",
encOpts: []SignOption{
NoSignature(),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
NoVerification(),
},
}, {
desc: "Signature with NoVerification(), working",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
NoVerification(),
},
}, {
desc: "NoSignature() with verification, should fail",
encOpts: []SignOption{
NoSignature(),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
},
decErr: errUnknown,
}, {
desc: "Decoder is invalid since TrustRootCAs() and NoVerification() are both set",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
NoVerification(),
},
decErr: errUnknown,
}, {
desc: "Decoder is invalid since neither TrustRootCAs() or NoVerification() are set",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decErr: errUnknown,
},
{
desc: "Try using signing algorith none, should fail",
encOpts: []SignOption{
SignWith("none", nil, nil),
},
input: Message{
Payload: []byte("Hello, world."),
},
encErr: ErrInvalidSignAlg,
}, {
desc: "Try setting multiple signing algorithms, should fail",
encOpts: []SignOption{
NoSignature(),
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
encErr: ErrInvalidSignAlg,
}, {
desc: "invalid response encryption algorithm",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{
Alg: "invalid",
Key: mustFromRaw([]byte("a clear text key, which is not allowed")),
},
},
encErr: ErrInvalidEncryptionAlg,
}, {
desc: "unsafe response encryption algorithm in the clear is not allowed",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{
Alg: jwa.PBES2_HS256_A128KW,
Key: mustFromRaw([]byte("a clear text key, which is not allowed")),
},
},
encErr: ErrUnsafeAlgorithm,
}, {
desc: "invalid response encryption key/alg combination",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainA.Leaf().Public, chainA.Leaf().Private, chainA.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{
Alg: jwa.RSA_OAEP,
Key: mustFromRaw([]byte("invalid key form")),
},
},
encErr: errUnknown,
}, {
desc: "untrusted chain",
encOpts: []SignOption{
SignWithRaw(jwa.ES256, chainB.Leaf().Public, chainB.Leaf().Private, chainB.Included()...),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecoderOption{
TrustRootCAs(chainA.Root().Public),
},
decErr: errUnknown,
},
}
func TestSigningTampering(t *testing.T) {
// Require everything to prevent 100s of duplicate errors.
require := require.New(t)
buf, err := complexWorking.input.Sign(complexWorking.encOpts...)
require.NoError(err)
require.NotNil(buf)
for i := 0; i < len(buf); i++ {
tmp := make([]byte, len(buf))
copy(tmp, buf)
tmp[i] = tmp[i] ^ 0xff
msg, err := Decode(tmp, complexWorking.decOpts...)
// Sometimes changing gzip data can result in the original message.
if err == nil && msg != nil {
require.Equal(&complexWorking.input, msg)
continue
}
require.Nil(msg, "idx=%d 0x%02x '%c'", i, tmp[i]^0xff, tmp[i]^0xff)
require.Error(err)
}
}
func TestEncDec(t *testing.T) {
for _, tt := range encodeDecodeTests {
runEncDecTest(t, tt)
}
}
func runEncDecTest(t *testing.T, tt encodeDecodeTest) {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
buf, err := tt.input.Sign(tt.encOpts...)
if tt.encErr != nil {
assert.Nil(buf)
require.Error(err)
if !errors.Is(errUnknown, tt.encErr) {
require.ErrorIs(err, tt.encErr)
}
return
}
require.NoError(err)
require.NotNil(buf)
msg, err := Decode(buf, tt.decOpts...)
if tt.decErr != nil {
assert.Nil(msg)
require.Error(err)
if !errors.Is(errUnknown, tt.decErr) {
require.ErrorIs(err, tt.decErr)
}
return
}
require.NoError(err)
require.NotNil(msg)
want := &tt.input
if tt.output != nil {
want = tt.output
}
require.Equal(want, msg)
})
}