-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTestGetKeys.dfy
522 lines (447 loc) · 22.3 KB
/
TestGetKeys.dfy
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../src/Index.dfy"
include "Fixtures.dfy"
include "CleanupItems.dfy"
include "../src/ErrorMessages.dfy"
// TODO-HV2-M1: add more HV2 tests.
// TODO-HV2-M1: Maybe rename this module to TestGetHv1Keys and create another TestGetHv2Keys
module TestGetKeys {
import Types = AwsCryptographyKeyStoreTypes
import ComAmazonawsKmsTypes
import KMS = Com.Amazonaws.Kms
import DDB = Com.Amazonaws.Dynamodb
import KeyStore
import ComAmazonawsDynamodbTypes
import opened Wrappers
import opened Fixtures
import opened StandardLibrary.UInt
import UTF8
import ErrorMessages = KeyStoreErrorMessages
import UUID
const incorrectLogicalName := "MySuperAwesomeTableName"
method VerifyGetKeys(
identifier : string,
keyStore : Types.IKeyStoreClient,
storage : Types.IKeyStorageInterface
)
requires
keyStore.ValidState() && storage.ValidState()
modifies
keyStore.Modifies, storage.Modifies
ensures
keyStore.ValidState() && storage.ValidState()
{
testBeaconKeyHappyCase(keyStore, identifier);
var activeResult := testAndGetActiveBranchKeyHappyCase(keyStore, identifier);
var branchKeyVersion :- expect UTF8.Decode(activeResult.branchKeyMaterials.branchKeyVersion);
testBranchKeyVersionHappyCase(keyStore, identifier, branchKeyVersion, activeResult.branchKeyMaterials.branchKeyVersion);
VerifyGetKeysFromStorage(identifier, storage);
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=test
//# This guid MUST be [version 4 UUID](https://www.ietf.org/rfc/rfc4122.txt)
var versionString :- expect UTF8.Decode(activeResult.branchKeyMaterials.branchKeyVersion);
var versionByteUUID :- expect UUID.ToByteArray(versionString);
var versionRoundTrip :- expect UUID.FromByteArray(versionByteUUID);
expect versionRoundTrip == versionString;
}
method {:test} TestGetKeysHappyCase()
{
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var kmsConfig := Types.KMSConfiguration.kmsKeyArn(keyArn);
var keyStore :- expect DefaultKeyStore(kmsId := keyArn, physicalName := branchKeyStoreName, logicalName := logicalKeyStoreName, ddbClient? := Some(ddbClient), kmsClient? := Some(kmsClient));
testBeaconKeyHappyCase(keyStore, branchKeyId);
testBeaconKeyHappyCase(keyStore, hv2BranchKeyId);
testActiveBranchKeyHappyCase(keyStore, branchKeyId, branchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(keyStore, hv2BranchKeyId, hv2BranchKeyIdActiveVersionUtf8Bytes);
testBranchKeyVersionHappyCase(keyStore, branchKeyId, branchKeyIdActiveVersion, branchKeyIdActiveVersionUtf8Bytes);
testBranchKeyVersionHappyCase(keyStore, hv2BranchKeyId, hv2BranchKeyVersion, hv2BranchKeyIdActiveVersionUtf8Bytes);
}
// TODO-HV2-M1: Add MRK test for hv2
method {:test} {:isolate_assertions} TestGetActiveMrkKey()
{
var ddbClient :- expect DDB.DynamoDBClient();
var westKeyStore :- expect KeyStoreWithOptionalClient(
kmsId := MrkArnWest,
physicalName := branchKeyStoreName,
logicalName := logicalKeyStoreName,
ddbClient? := Some(ddbClient)
);
var eastKeyStore :- expect KeyStoreWithOptionalClient(
kmsId := MrkArnEast,
physicalName := branchKeyStoreName,
logicalName := logicalKeyStoreName,
ddbClient? := Some(ddbClient)
);
var westMrkKeyStore :- expect KeyStoreWithOptionalClient(
kmsId := MrkArnWest,
physicalName := branchKeyStoreName,
logicalName := logicalKeyStoreName,
ddbClient? := Some(ddbClient),
srkKey := false,
mrkKey := true
);
var eastMrkKeyStore :- expect KeyStoreWithOptionalClient(
kmsId := MrkArnEast,
physicalName := branchKeyStoreName,
logicalName := logicalKeyStoreName,
ddbClient? := Some(ddbClient),
srkKey := false,
mrkKey := true
);
var apMrkKeyStore :- expect KeyStoreWithOptionalClient(
kmsId := MrkArnAP,
physicalName := branchKeyStoreName,
logicalName := logicalKeyStoreName,
ddbClient? := Some(ddbClient),
srkKey := false,
mrkKey := true
);
// All four set of keys (branch, beacon and version) should work when the regions match
testActiveBranchKeyHappyCase(westKeyStore, WestBranchKey, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(westKeyStore, WestBranchKey);
testBranchKeyVersionHappyCase(westKeyStore, WestBranchKey, WestBranchKeyIdActiveVersion, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(eastKeyStore, EastBranchKey, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(eastKeyStore, EastBranchKey);
testBranchKeyVersionHappyCase(eastKeyStore, EastBranchKey, EastBranchKeyIdActiveVersion, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(westMrkKeyStore, WestBranchKey, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(westMrkKeyStore, WestBranchKey);
testBranchKeyVersionHappyCase(westMrkKeyStore, WestBranchKey, WestBranchKeyIdActiveVersion, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(eastMrkKeyStore, EastBranchKey, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(eastMrkKeyStore, EastBranchKey);
testBranchKeyVersionHappyCase(eastMrkKeyStore, EastBranchKey, EastBranchKeyIdActiveVersion, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
// MRK Configuration should work with the other region
testActiveBranchKeyHappyCase(westMrkKeyStore, EastBranchKey, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(westMrkKeyStore, EastBranchKey);
testBranchKeyVersionHappyCase(westMrkKeyStore, EastBranchKey, EastBranchKeyIdActiveVersion, EastBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(eastMrkKeyStore, WestBranchKey, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(eastMrkKeyStore, WestBranchKey);
testBranchKeyVersionHappyCase(eastMrkKeyStore, WestBranchKey, WestBranchKeyIdActiveVersion, WestBranchKeyBranchKeyIdActiveVersionUtf8Bytes);
// Plain Configuration should fail with the other region
GetActiveKeyWithIncorrectKmsKeyArnHelper(westKeyStore, EastBranchKey);
GetBeaconKeyWithIncorrectKmsKeyArnHelper(westKeyStore, EastBranchKey);
GetBranchKeyVersionWithIncorrectKmsKeyArnHelper(westKeyStore, EastBranchKey, EastBranchKeyIdActiveVersion);
GetActiveKeyWithIncorrectKmsKeyArnHelper(eastKeyStore, WestBranchKey);
GetBeaconKeyWithIncorrectKmsKeyArnHelper(eastKeyStore, WestBranchKey);
GetBranchKeyVersionWithIncorrectKmsKeyArnHelper(eastKeyStore, WestBranchKey, WestBranchKeyIdActiveVersion);
// apMrkKeyStore should always fail
testActiveBranchKeyKMSFailureCase(apMrkKeyStore, WestBranchKey);
testBranchKeyVersionKMSFailureCase(apMrkKeyStore, WestBranchKey, WestBranchKeyIdActiveVersion);
testBeaconKeyKMSFailureCase(apMrkKeyStore, WestBranchKey);
}
method {:test} TestKeyWithIncorrectKmsKeyArn() {
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var keyStore :- expect DefaultKeyStore(kmsId := postalHornKeyArn, physicalName := branchKeyStoreName, logicalName := logicalKeyStoreName, ddbClient? := Some(ddbClient), kmsClient? := Some(kmsClient));
GetActiveKeyWithIncorrectKmsKeyArnHelper(keyStore, branchKeyId);
GetActiveKeyWithIncorrectKmsKeyArnHelper(keyStore, hv2BranchKeyId);
GetBeaconKeyWithIncorrectKmsKeyArnHelper(keyStore, branchKeyId);
GetBeaconKeyWithIncorrectKmsKeyArnHelper(keyStore, hv2BranchKeyId);
GetBranchKeyVersionWithIncorrectKmsKeyArnHelper(keyStore, branchKeyId, branchKeyIdActiveVersion);
GetBranchKeyVersionWithIncorrectKmsKeyArnHelper(keyStore, hv2BranchKeyId, hv2BranchKeyVersion);
}
method {:test} TestGetActiveKeyWrongLogicalKeyStoreName() {
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var keyStore :- expect DefaultKeyStore(kmsId:=keyArn, physicalName := branchKeyStoreName, logicalName := incorrectLogicalName, ddbClient? := Some(ddbClient), kmsClient? := Some(kmsClient));
GetActiveKeyWrongLogicalKeyStoreName(keyStore, branchKeyId, Types.HierarchyVersion.v1);
GetActiveKeyWrongLogicalKeyStoreName(keyStore, hv2BranchKeyId, Types.HierarchyVersion.v2);
GetBeaconKeyWrongLogicalKeyStoreName(keyStore, branchKeyId, Types.HierarchyVersion.v1);
GetBeaconKeyWrongLogicalKeyStoreName(keyStore, hv2BranchKeyId, Types.HierarchyVersion.v2);
GetVersionKeyWrongLogicalKeyStoreName(keyStore, branchKeyId, branchKeyIdActiveVersion, Types.HierarchyVersion.v1);
GetVersionKeyWrongLogicalKeyStoreName(keyStore, hv2BranchKeyId, hv2BranchKeyVersion, Types.HierarchyVersion.v2);
}
method {:test} TestGetKeyDoesNotExistFails()
{
var kmsClient :- expect KMS.KMSClient();
var ddbClient :- expect DDB.DynamoDBClient();
var keyStore :- expect DefaultKeyStore(kmsId := keyArn, physicalName := branchKeyStoreName, logicalName := logicalKeyStoreName, ddbClient? := Some(ddbClient), kmsClient? := Some(kmsClient));
GetActiveKeyDoesNotExistFailsHelper(keyStore, "Robbie");
GetBeaconKeyDoesNotExistFailsHelper(keyStore, "Robbie");
GetBranchKeyVersionDoesNotExistFailsHelper(keyStore, "Robbie", branchKeyIdActiveVersion);
}
method {:test} TestGetKeysWithNoClients() {
var kmsConfig := Types.KMSConfiguration.kmsKeyArn(keyArn);
var keyStore :- expect KeyStoreWithOptionalClient(kmsId:=keyArn, physicalName:=branchKeyStoreName, logicalName := logicalKeyStoreName);
testActiveBranchKeyHappyCase(keyStore, branchKeyId, branchKeyIdActiveVersionUtf8Bytes);
testActiveBranchKeyHappyCase(keyStore, hv2BranchKeyId, hv2BranchKeyIdActiveVersionUtf8Bytes);
testBeaconKeyHappyCase(keyStore, branchKeyId);
testBeaconKeyHappyCase(keyStore, hv2BranchKeyId);
testBranchKeyVersionHappyCase(keyStore, branchKeyId, branchKeyIdActiveVersion, branchKeyIdActiveVersionUtf8Bytes);
testBranchKeyVersionHappyCase(keyStore, hv2BranchKeyId, hv2BranchKeyVersion, hv2BranchKeyIdActiveVersionUtf8Bytes);
}
method GetActiveKeyWrongLogicalKeyStoreName(keyStore: Types.IKeyStoreClient, branchKeyId: string, hv: Types.HierarchyVersion)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var activeResult := keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect activeResult.Failure?;
match hv {
case v1 =>
match activeResult.error {
case ComAmazonawsKms(nestedError) =>
expect nestedError.InvalidCiphertextException?;
case _ => expect false, "Wrong Logical Keystore Name SHOULD Fail with KMS InvalidCiphertextException for HV-1.";
}
case v2 => expect activeResult.error == Types.Error.BranchKeyCiphertextException(message := ErrorMessages.MD_DIGEST_SHA_NOT_MATCHED), "Wrong Logical Keystore Name SHOULD Fail with BranchKeyCiphertextException for HV-2.";
}
}
method GetBeaconKeyWrongLogicalKeyStoreName(keyStore: Types.IKeyStoreClient, branchKeyId: string, hv: Types.HierarchyVersion)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var beaconKeyResult := keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId
));
expect beaconKeyResult.Failure?;
match hv {
case v1 =>
match beaconKeyResult.error {
case ComAmazonawsKms(nestedError) =>
expect nestedError.InvalidCiphertextException?;
case _ => expect false, "Wrong Logical Keystore Name SHOULD Fail with KMS InvalidCiphertextException for HV-1.";
}
case v2 => expect beaconKeyResult.error == Types.Error.BranchKeyCiphertextException(message := ErrorMessages.MD_DIGEST_SHA_NOT_MATCHED), "Wrong Logical Keystore Name SHOULD Fail with BranchKeyCiphertextException for HV-2.";
}
}
method GetVersionKeyWrongLogicalKeyStoreName(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersion: string, hv: Types.HierarchyVersion)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var versionResult := keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId,
branchKeyVersion := branchKeyIdActiveVersion
));
expect versionResult.Failure?;
match hv {
case v1 =>
match versionResult.error {
case ComAmazonawsKms(nestedError) =>
expect nestedError.InvalidCiphertextException?;
case _ => expect false, "Wrong Logical Keystore Name SHOULD Fail with KMS InvalidCiphertextException for HV-1.";
}
case v2 => expect versionResult.error == Types.Error.BranchKeyCiphertextException(message := ErrorMessages.MD_DIGEST_SHA_NOT_MATCHED), "Wrong Logical Keystore Name SHOULD Fail with BranchKeyCiphertextException for HV-2.";
}
}
method GetActiveKeyDoesNotExistFailsHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var activeResult := keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect activeResult.Failure?;
expect activeResult.error == Types.KeyStoreException(message := ErrorMessages.NO_CORRESPONDING_BRANCH_KEY);
}
method GetBeaconKeyDoesNotExistFailsHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var beaconKeyResult := keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId
));
expect beaconKeyResult.Failure?;
expect beaconKeyResult.error == Types.KeyStoreException(message := ErrorMessages.NO_CORRESPONDING_BRANCH_KEY);
}
method GetBranchKeyVersionDoesNotExistFailsHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersion: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var versionResult := keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId,
branchKeyVersion := branchKeyIdActiveVersion
));
expect versionResult.Failure?;
expect versionResult.error == Types.KeyStoreException(message := ErrorMessages.NO_CORRESPONDING_BRANCH_KEY);
}
method GetActiveKeyWithIncorrectKmsKeyArnHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var activeResult := keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect activeResult.Failure?;
expect activeResult.error == Types.KeyStoreException(message := ErrorMessages.GET_KEY_ARN_DISAGREEMENT);
}
method GetBeaconKeyWithIncorrectKmsKeyArnHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var beaconKeyResult := keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId
));
expect beaconKeyResult.Failure?;
expect beaconKeyResult.error == Types.KeyStoreException(message := ErrorMessages.GET_KEY_ARN_DISAGREEMENT);
}
method GetBranchKeyVersionWithIncorrectKmsKeyArnHelper(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersion: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var versionResult := keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId,
branchKeyVersion := branchKeyIdActiveVersion
));
expect versionResult.Failure?;
expect versionResult.error == Types.KeyStoreException(message := ErrorMessages.GET_KEY_ARN_DISAGREEMENT);
}
method testBeaconKeyHappyCase(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var beaconKeyResult :- expect keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId
));
expect isValidBeaconResult?(beaconKeyResult, branchKeyId);
}
predicate method isValidBeaconResult?(beaconKeyResult: Types.GetBeaconKeyOutput, branchKeyId: string) {
&& beaconKeyResult.beaconKeyMaterials.beaconKeyIdentifier == branchKeyId
&& beaconKeyResult.beaconKeyMaterials.beaconKey.Some?
&& |beaconKeyResult.beaconKeyMaterials.beaconKey.value| == 32
}
method testActiveBranchKeyHappyCase(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersionUtf8Bytes: seq<uint8>)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var branchKeyResult :- expect keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect isValidActiveBranchKeyResult?(branchKeyResult, branchKeyId, Some(branchKeyIdActiveVersionUtf8Bytes));
}
method testAndGetActiveBranchKeyHappyCase(keyStore: Types.IKeyStoreClient, branchKeyId: string, nameonly branchKeyIdActiveVersionUtf8Bytes: Option<seq<uint8>> := None)
returns (output: Types.GetActiveBranchKeyOutput)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var branchKeyResult :- expect keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect isValidActiveBranchKeyResult?(branchKeyResult, branchKeyId, branchKeyIdActiveVersionUtf8Bytes);
return branchKeyResult;
}
predicate method isValidActiveBranchKeyResult?(branchKeyResult: Types.GetActiveBranchKeyOutput, branchKeyId: string, branchKeyIdActiveVersionUtf8Bytes: Option<seq<uint8>>) {
&& branchKeyResult.branchKeyMaterials.branchKeyIdentifier == branchKeyId
&& (branchKeyIdActiveVersionUtf8Bytes.None? ||
branchKeyResult.branchKeyMaterials.branchKeyVersion == branchKeyIdActiveVersionUtf8Bytes.value)
&& |branchKeyResult.branchKeyMaterials.branchKey| == 32
}
method testBranchKeyVersionHappyCase(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersion: string, branchKeyIdActiveVersionUtf8Bytes: seq<uint8>)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var versionResult :- expect keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId,
branchKeyVersion := branchKeyIdActiveVersion
));
var testBytes :- expect UTF8.Encode(branchKeyIdActiveVersion);
expect isValidBranchKeyVersionResult?(versionResult, branchKeyId, branchKeyIdActiveVersionUtf8Bytes, testBytes);
}
predicate method isValidBranchKeyVersionResult?(versionResult: Types.GetBranchKeyVersionOutput, branchKeyId: string, branchKeyIdActiveVersionUtf8Bytes: seq<uint8>, testBytes: UTF8.ValidUTF8Bytes) {
&& versionResult.branchKeyMaterials.branchKeyIdentifier == branchKeyId
&& versionResult.branchKeyMaterials.branchKeyVersion == branchKeyIdActiveVersionUtf8Bytes == testBytes
&& |versionResult.branchKeyMaterials.branchKey| == 32
}
method testActiveBranchKeyKMSFailureCase(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var branchKeyResult := keyStore.GetActiveBranchKey(
Types.GetActiveBranchKeyInput(
branchKeyIdentifier := branchKeyId
));
expect branchKeyResult.Failure?;
expect branchKeyResult.error.ComAmazonawsKms?;
expect branchKeyResult.error.ComAmazonawsKms.OpaqueWithText?;
}
method testBranchKeyVersionKMSFailureCase(keyStore: Types.IKeyStoreClient, branchKeyId: string, branchKeyIdActiveVersion: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var versionResult := keyStore.GetBranchKeyVersion(
Types.GetBranchKeyVersionInput(
branchKeyIdentifier := branchKeyId,
branchKeyVersion := branchKeyIdActiveVersion
));
expect versionResult.Failure?;
expect versionResult.error.ComAmazonawsKms?;
expect versionResult.error.ComAmazonawsKms.OpaqueWithText?;
}
method testBeaconKeyKMSFailureCase(keyStore: Types.IKeyStoreClient, branchKeyId: string)
requires keyStore.ValidState()
modifies keyStore.Modifies
{
var beaconKeyResult := keyStore.GetBeaconKey(
Types.GetBeaconKeyInput(
branchKeyIdentifier := branchKeyId
));
expect beaconKeyResult.Failure?;
expect beaconKeyResult.error.ComAmazonawsKms?;
expect beaconKeyResult.error.ComAmazonawsKms.OpaqueWithText?;
}
method VerifyGetKeysFromStorage(
identifier : string,
storage : Types.IKeyStorageInterface
)
requires storage.ValidState()
modifies storage.Modifies
ensures storage.ValidState()
{
var encryptedActiveFromStorage :- expect storage.GetEncryptedActiveBranchKey(
Types.GetEncryptedActiveBranchKeyInput(
Identifier := identifier
)
);
var encryptedBeaconFromStorage :- expect storage.GetEncryptedBeaconKey(
Types.GetEncryptedBeaconKeyInput(
Identifier := identifier
)
);
expect encryptedActiveFromStorage.Item.Type.ActiveHierarchicalSymmetricVersion?;
var encryptedVersionFromStorage :- expect storage.GetEncryptedBranchKeyVersion(
Types.GetEncryptedBranchKeyVersionInput(
Identifier := identifier,
Version := encryptedActiveFromStorage.Item.Type.ActiveHierarchicalSymmetricVersion.Version
)
);
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//= type=test
//# This timestamp MUST be in ISO 8601 format in UTC, to microsecond precision (e.g. “YYYY-MM-DDTHH:mm:ss.ssssssZ“)
expect ISO8601?(encryptedActiveFromStorage.Item.CreateTime);
expect ISO8601?(encryptedBeaconFromStorage.Item.CreateTime);
expect ISO8601?(encryptedVersionFromStorage.Item.CreateTime);
}
lemma ISO8601Test()
{
assert ISO8601?("2024-08-06T17:23:25.000874Z");
}
predicate method ISO8601?(
CreateTime: string
)
{
// “YYYY-MM-DDTHH:mm:ss.ssssssZ“
&& |CreateTime| == 27
&& CreateTime[4] == '-'
&& CreateTime[7] == '-'
&& CreateTime[10] == 'T'
&& CreateTime[13] == ':'
&& CreateTime[16] == ':'
&& CreateTime[19] == '.'
&& CreateTime[26] == 'Z'
}
}