-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_objects_test.go
372 lines (318 loc) · 9.29 KB
/
base_objects_test.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package kmip
import (
"bufio"
"crypto/tls"
"testing"
"github.com/baum/kmip-go/kmip14"
"github.com/baum/kmip-go/ttlv"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// clientConn returns a connection to the test kmip server. Should be closed at end of test.
func clientConn(t *testing.T) *tls.Conn {
t.Helper()
cert, err := tls.LoadX509KeyPair("./pykmip-server/server.cert", "./pykmip-server/server.key")
require.NoError(t, err)
// the containerized pykmip we're using requires a very specific cipher suite, which isn't
// enabled by go by default.
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
},
}
conn, err := tls.Dial("tcp", "127.0.0.1:5696", tlsConfig)
require.NoError(t, err)
return conn
}
func TestCreateKey(t *testing.T) {
conn := clientConn(t)
defer conn.Close()
biID := uuid.New()
payload := CreateRequestPayload{
ObjectType: kmip14.ObjectTypeSymmetricKey,
}
payload.TemplateAttribute.Append(kmip14.TagCryptographicAlgorithm, kmip14.CryptographicAlgorithmAES)
payload.TemplateAttribute.Append(kmip14.TagCryptographicLength, 256)
payload.TemplateAttribute.Append(kmip14.TagCryptographicUsageMask, kmip14.CryptographicUsageMaskEncrypt|kmip14.CryptographicUsageMaskDecrypt)
payload.TemplateAttribute.Append(kmip14.TagName, Name{
NameValue: "Key1",
NameType: kmip14.NameTypeUninterpretedTextString,
})
msg := RequestMessage{
RequestHeader: RequestHeader{
ProtocolVersion: ProtocolVersion{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 4,
},
BatchCount: 1,
},
BatchItem: []RequestBatchItem{
{
UniqueBatchItemID: biID[:],
Operation: kmip14.OperationCreate,
RequestPayload: &payload,
},
},
}
req, err := ttlv.Marshal(msg)
require.NoError(t, err)
t.Log(req)
_, err = conn.Write(req)
require.NoError(t, err)
decoder := ttlv.NewDecoder(bufio.NewReader(conn))
resp, err := decoder.NextTTLV()
require.NoError(t, err)
t.Log(resp)
var respMsg ResponseMessage
err = decoder.DecodeValue(&respMsg, resp)
require.NoError(t, err)
assert.Equal(t, 1, respMsg.ResponseHeader.BatchCount)
assert.Len(t, respMsg.BatchItem, 1)
bi := respMsg.BatchItem[0]
assert.Equal(t, kmip14.OperationCreate, bi.Operation)
assert.NotEmpty(t, bi.UniqueBatchItemID)
assert.Equal(t, kmip14.ResultStatusSuccess, bi.ResultStatus)
var respPayload CreateResponsePayload
err = decoder.DecodeValue(&respPayload, bi.ResponsePayload.(ttlv.TTLV))
require.NoError(t, err)
assert.Equal(t, kmip14.ObjectTypeSymmetricKey, respPayload.ObjectType)
assert.NotEmpty(t, respPayload.UniqueIdentifier)
}
func TestCreateKeyPair(t *testing.T) {
conn := clientConn(t)
defer conn.Close()
biID := uuid.New()
payload := CreateKeyPairRequestPayload{}
payload.CommonTemplateAttribute = &TemplateAttribute{}
payload.CommonTemplateAttribute.Append(kmip14.TagCryptographicAlgorithm, kmip14.CryptographicAlgorithmRSA)
payload.CommonTemplateAttribute.Append(kmip14.TagCryptographicLength, 1024)
payload.CommonTemplateAttribute.Append(kmip14.TagCryptographicUsageMask, kmip14.CryptographicUsageMaskSign|kmip14.CryptographicUsageMaskVerify)
msg := RequestMessage{
RequestHeader: RequestHeader{
ProtocolVersion: ProtocolVersion{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 4,
},
BatchCount: 1,
},
BatchItem: []RequestBatchItem{
{
UniqueBatchItemID: biID[:],
Operation: kmip14.OperationCreateKeyPair,
RequestPayload: &payload,
},
},
}
req, err := ttlv.Marshal(msg)
require.NoError(t, err)
t.Log(req)
_, err = conn.Write(req)
require.NoError(t, err)
decoder := ttlv.NewDecoder(bufio.NewReader(conn))
resp, err := decoder.NextTTLV()
require.NoError(t, err)
t.Log(resp)
var respMsg ResponseMessage
err = decoder.DecodeValue(&respMsg, resp)
require.NoError(t, err)
assert.Equal(t, 1, respMsg.ResponseHeader.BatchCount)
assert.Len(t, respMsg.BatchItem, 1)
bi := respMsg.BatchItem[0]
assert.Equal(t, kmip14.OperationCreateKeyPair, bi.Operation)
assert.NotEmpty(t, bi.UniqueBatchItemID)
assert.Equal(t, kmip14.ResultStatusSuccess, bi.ResultStatus)
var respPayload CreateKeyPairResponsePayload
err = decoder.DecodeValue(&respPayload, bi.ResponsePayload.(ttlv.TTLV))
require.NoError(t, err)
assert.NotEmpty(t, respPayload.PrivateKeyUniqueIdentifier)
assert.NotEmpty(t, respPayload.PublicKeyUniqueIdentifier)
}
func TestRequest(t *testing.T) {
conn := clientConn(t)
defer conn.Close()
biID := uuid.New()
msg := RequestMessage{
RequestHeader: RequestHeader{
ProtocolVersion: ProtocolVersion{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 2,
},
BatchCount: 1,
},
BatchItem: []RequestBatchItem{
{
UniqueBatchItemID: biID[:],
Operation: kmip14.OperationDiscoverVersions,
RequestPayload: DiscoverVersionsRequestPayload{
ProtocolVersion: []ProtocolVersion{
{ProtocolVersionMajor: 1, ProtocolVersionMinor: 2},
},
},
},
},
}
req, err := ttlv.Marshal(msg)
require.NoError(t, err)
t.Log(req)
_, err = conn.Write(req)
require.NoError(t, err)
decoder := ttlv.NewDecoder(bufio.NewReader(conn))
resp, err := decoder.NextTTLV()
require.NoError(t, err)
t.Log(resp)
var respMsg ResponseMessage
err = decoder.DecodeValue(&respMsg, resp)
require.NoError(t, err)
assert.Equal(t, 1, respMsg.ResponseHeader.BatchCount)
assert.Len(t, respMsg.BatchItem, 1)
bi := respMsg.BatchItem[0]
assert.Equal(t, kmip14.OperationDiscoverVersions, bi.Operation)
assert.NotEmpty(t, bi.UniqueBatchItemID)
assert.Equal(t, kmip14.ResultStatusSuccess, bi.ResultStatus)
var discVerRespPayload struct {
ProtocolVersion ProtocolVersion
}
err = decoder.DecodeValue(&discVerRespPayload, bi.ResponsePayload.(ttlv.TTLV))
require.NoError(t, err)
assert.Equal(t, ProtocolVersion{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 2,
}, discVerRespPayload.ProtocolVersion)
}
func TestTemplateAttribute_marshal(t *testing.T) {
tests := []struct {
name string
in TemplateAttribute
inF func() TemplateAttribute
expected ttlv.Value
}{
{
name: "basic",
in: TemplateAttribute{
Name: []Name{
{
NameValue: "first",
NameType: kmip14.NameTypeUninterpretedTextString,
},
{
NameValue: "this is a uri",
NameType: kmip14.NameTypeURI,
},
},
Attribute: []Attribute{
{
AttributeName: kmip14.TagAlwaysSensitive.CanonicalName(),
AttributeIndex: 5,
AttributeValue: true,
},
},
},
expected: s(kmip14.TagTemplateAttribute,
s(kmip14.TagName,
v(kmip14.TagNameValue, "first"),
v(kmip14.TagNameType, kmip14.NameTypeUninterpretedTextString),
),
s(kmip14.TagName,
v(kmip14.TagNameValue, "this is a uri"),
v(kmip14.TagNameType, kmip14.NameTypeURI),
),
s(kmip14.TagAttribute,
v(kmip14.TagAttributeName, kmip14.TagAlwaysSensitive.CanonicalName()),
v(kmip14.TagAttributeIndex, 5),
v(kmip14.TagAttributeValue, true),
),
),
},
{
name: "noname",
in: TemplateAttribute{Attribute: []Attribute{
{
AttributeName: kmip14.TagAlwaysSensitive.CanonicalName(),
AttributeIndex: 5,
AttributeValue: true,
},
}},
expected: s(kmip14.TagTemplateAttribute,
s(kmip14.TagAttribute,
v(kmip14.TagAttributeName, kmip14.TagAlwaysSensitive.CanonicalName()),
v(kmip14.TagAttributeIndex, 5),
v(kmip14.TagAttributeValue, true),
),
),
},
{
name: "noattribute",
in: TemplateAttribute{
Name: []Name{
{
NameValue: "first",
NameType: kmip14.NameTypeUninterpretedTextString,
},
},
},
expected: s(kmip14.TagTemplateAttribute,
s(kmip14.TagName,
v(kmip14.TagNameValue, "first"),
v(kmip14.TagNameType, kmip14.NameTypeUninterpretedTextString),
),
),
},
{
name: "omitzeroindex",
in: TemplateAttribute{
Attribute: []Attribute{
{
AttributeName: kmip14.TagAlwaysSensitive.CanonicalName(),
AttributeValue: true,
},
},
},
expected: s(kmip14.TagTemplateAttribute,
s(kmip14.TagAttribute,
v(kmip14.TagAttributeName, kmip14.TagAlwaysSensitive.CanonicalName()),
v(kmip14.TagAttributeValue, true),
),
),
},
{
name: "use canonical names",
inF: func() TemplateAttribute {
var ta TemplateAttribute
ta.Append(kmip14.TagCryptographicAlgorithm, ttlv.EnumValue(kmip14.CryptographicAlgorithmBlowfish))
return ta
},
expected: s(kmip14.TagTemplateAttribute,
s(kmip14.TagAttribute,
v(kmip14.TagAttributeName, "Cryptographic Algorithm"),
v(kmip14.TagAttributeValue, ttlv.EnumValue(kmip14.CryptographicAlgorithmBlowfish)),
),
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
in := test.in
if test.inF != nil {
in = test.inF()
}
out, err := ttlv.Marshal(&in)
require.NoError(t, err)
expected, err := ttlv.Marshal(test.expected)
require.NoError(t, err)
require.Equal(t, out, expected)
var ta TemplateAttribute
err = ttlv.Unmarshal(expected, &ta)
require.NoError(t, err)
require.Equal(t, in, ta)
})
}
}
func v(tag ttlv.Tag, val interface{}) ttlv.Value {
return ttlv.NewValue(tag, val)
}
func s(tag ttlv.Tag, vals ...ttlv.Value) ttlv.Value {
return ttlv.NewStruct(tag, vals...)
}