-
Notifications
You must be signed in to change notification settings - Fork 45
/
testing.go
249 lines (212 loc) · 5.67 KB
/
testing.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package wrapping
import (
"context"
"fmt"
"github.com/hashicorp/go-kms-wrapping/v2/internal/xor"
)
// TestWrapper is a wrapper that can be used for tests
type TestWrapper struct {
wrapperType WrapperType
secret []byte
keyId string
envelope bool
ReturnKeyIdError error
ReturnDecryptError error
ReturnEncryptError error
}
type TestInitFinalizer struct {
*TestWrapper
}
type TestInitFinalizerHmacComputer struct {
*TestInitFinalizer
}
var (
_ Wrapper = (*TestWrapper)(nil)
_ KeyExporter = (*TestWrapper)(nil)
_ InitFinalizer = (*TestInitFinalizer)(nil)
_ KeyExporter = (*TestInitFinalizer)(nil)
_ InitFinalizer = (*TestInitFinalizerHmacComputer)(nil)
_ HmacComputer = (*TestInitFinalizerHmacComputer)(nil)
_ KeyExporter = (*TestInitFinalizerHmacComputer)(nil)
)
// NewTestWrapper constructs a test wrapper
func NewTestWrapper(secret []byte) *TestWrapper {
return &TestWrapper{
wrapperType: WrapperTypeTest,
secret: secret,
keyId: "static-key",
}
}
// NewTestInitFinalizer constructs a test wrapper
func NewTestInitFinalizer(secret []byte) *TestInitFinalizer {
return &TestInitFinalizer{
TestWrapper: &TestWrapper{
wrapperType: WrapperTypeTest,
secret: secret,
keyId: "static-key",
},
}
}
// NewTestInitFinalizerHmacComputer constructs a test wrapper
func NewTestInitFinalizerHmacComputer(secret []byte) *TestInitFinalizerHmacComputer {
return &TestInitFinalizerHmacComputer{
TestInitFinalizer: &TestInitFinalizer{
TestWrapper: &TestWrapper{
wrapperType: WrapperTypeTest,
secret: secret,
keyId: "static-key",
},
},
}
}
// NewTestWrapper constructs a test wrapper
func NewTestEnvelopeWrapper(secret []byte) *TestWrapper {
return &TestWrapper{
wrapperType: WrapperTypeTest,
secret: secret,
keyId: "static-key",
envelope: true,
}
}
// HmacKeyId returns the HMAC key id
func (t *TestInitFinalizerHmacComputer) HmacKeyId(_ context.Context) (string, error) {
return "hmac-key", nil
}
// Init initializes the test wrapper
func (t *TestInitFinalizer) Init(_ context.Context, _ ...Option) error {
return nil
}
// Finalize finalizes the test wrapper
func (t *TestInitFinalizer) Finalize(_ context.Context, _ ...Option) error {
return nil
}
// Type returns the type of the test wrapper
func (t *TestWrapper) Type(_ context.Context) (WrapperType, error) {
return t.wrapperType, nil
}
// KeyId returns the configured key ID
func (t *TestWrapper) KeyId(_ context.Context) (string, error) {
if t.ReturnKeyIdError != nil {
return "", t.ReturnKeyIdError
}
return t.keyId, nil
}
// SetConfig sets config, and currently it only supports the WithKeyId option
// for test wrappers
func (t *TestWrapper) SetConfig(_ context.Context, opt ...Option) (*WrapperConfig, error) {
opts, err := GetOpts(opt...)
if err != nil {
return nil, err
}
t.keyId = opts.WithKeyId
return nil, nil
}
// HmacKeyId returns the configured HMAC key ID
func (t *TestWrapper) HmacKeyId(_ context.Context) string {
return ""
}
// SetKeyID allows setting the test wrapper's key ID
func (t *TestWrapper) SetKeyId(k string) {
t.keyId = k
}
// KeyBytes returns the current key bytes
func (t *TestWrapper) KeyBytes(context.Context) ([]byte, error) {
if t.secret == nil {
return nil, fmt.Errorf("missing bytes: %w", ErrInvalidParameter)
}
return t.secret, nil
}
// Encrypt allows encrypting via the test wrapper
func (t *TestWrapper) Encrypt(ctx context.Context, plaintext []byte, opts ...Option) (*BlobInfo, error) {
if t.ReturnEncryptError != nil {
return nil, t.ReturnEncryptError
}
switch t.envelope {
case true:
env, err := EnvelopeEncrypt(plaintext, nil)
if err != nil {
return nil, fmt.Errorf("error wrapping data: %w", err)
}
ct, err := t.obscureBytes(env.Key)
if err != nil {
return nil, err
}
keyId, err := t.KeyId(ctx)
if err != nil {
return nil, err
}
return &BlobInfo{
Ciphertext: env.Ciphertext,
Iv: env.Iv,
KeyInfo: &KeyInfo{
KeyId: keyId,
WrappedKey: ct,
KeyPurposes: []KeyPurpose{KeyPurpose_Encrypt},
},
}, nil
default:
ct, err := t.obscureBytes(plaintext)
if err != nil {
return nil, err
}
keyId, err := t.KeyId(ctx)
if err != nil {
return nil, err
}
return &BlobInfo{
Ciphertext: ct,
KeyInfo: &KeyInfo{
KeyId: keyId,
KeyPurposes: []KeyPurpose{KeyPurpose_Encrypt},
},
}, nil
}
}
// Decrypt allows decrypting via the test wrapper
func (t *TestWrapper) Decrypt(_ context.Context, dwi *BlobInfo, opts ...Option) ([]byte, error) {
if t.ReturnDecryptError != nil {
return nil, t.ReturnDecryptError
}
switch t.envelope {
case true:
keyPlaintext, err := t.obscureBytes(dwi.KeyInfo.WrappedKey)
if err != nil {
return nil, err
}
envInfo := &EnvelopeInfo{
Key: keyPlaintext,
Iv: dwi.Iv,
Ciphertext: dwi.Ciphertext,
}
plaintext, err := EnvelopeDecrypt(envInfo, nil)
if err != nil {
return nil, fmt.Errorf("error decrypting data with envelope: %w", err)
}
return plaintext, nil
default:
return t.obscureBytes(dwi.Ciphertext)
}
}
// obscureBytes is a helper to simulate "encryption/decryption"
// on protected values.
func (t *TestWrapper) obscureBytes(in []byte) ([]byte, error) {
out := make([]byte, len(in))
if len(t.secret) != 0 {
// make sure they are the same length
localSecret := make([]byte, len(in))
copy(localSecret, t.secret)
var err error
out, err = xor.XorBytes(in, localSecret)
if err != nil {
return nil, err
}
} else {
// if there is no secret, simply reverse the string
for i := 0; i < len(in); i++ {
out[i] = in[len(in)-1-i]
}
}
return out, nil
}