Skip to content

Commit 999ae15

Browse files
rishav-karanjittexastony
authored andcommitted
chore(dafny): Add helper method to decrypt branch key item (#1439)
Hitting DDB Race condition bug in .NET.
1 parent be086cd commit 999ae15

File tree

1 file changed

+65
-87
lines changed
  • AwsCryptographicMaterialProviders/dafny/AwsCryptographyKeyStore/src

1 file changed

+65
-87
lines changed

AwsCryptographicMaterialProviders/dafny/AwsCryptographyKeyStore/src/GetKeys.dfy

Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,9 @@ module GetKeys {
176176
KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, branchKeyItemFromStorage.EncryptionContext[Structure.KMS_FIELD]),
177177
Types.KeyStoreException( message := ErrorMessages.GET_KEY_ARN_DISAGREEMENT)
178178
);
179-
var plainTextKey: seq<uint8>;
180-
if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_1) {
181-
var kmsRes: KMS.DecryptResponse :- KMSKeystoreOperations.DecryptKeyForHv1(
182-
branchKeyItemFromStorage,
183-
kmsConfiguration,
184-
grantTokens,
185-
kmsClient
186-
);
187-
plainTextKey := kmsRes.Plaintext.value;
188-
} else if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_2) {
189-
:- Need(
190-
HvUtils.HasUniqueTransformedKeys?(branchKeyItemFromStorage.EncryptionContext),
191-
Types.KeyStoreException(
192-
message := ErrorMessages.NOT_UNIQUE_BRANCH_KEY_CONTEXT_KEYS
193-
)
194-
);
195-
plainTextKey :- DecryptAndValidateKeyForHV2 (
196-
branchKeyItemFromStorage,
197-
kmsConfiguration,
198-
grantTokens,
199-
kmsClient
200-
);
201-
} else {
202-
// This else block will never be reached because we have check for hierarchical keyring version before if-else.
203-
var e := Types.KeyStoreException(
204-
message := ErrorMessages.INVALID_HIERARCHY_VERSION
205-
);
206-
return Failure(e);
207-
}
179+
180+
var plainTextKey :- DecryptBranchKeyItem(branchKeyItemFromStorage, kmsConfiguration, grantTokens, kmsClient);
181+
208182
var branchKeyMaterials :- Structure.ToBranchKeyMaterials(
209183
branchKeyItemFromStorage,
210184
plainTextKey
@@ -382,35 +356,7 @@ module GetKeys {
382356
)
383357
);
384358

385-
var plainTextKey: seq<uint8>;
386-
if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_1) {
387-
var kmsRes: KMS.DecryptResponse :- KMSKeystoreOperations.DecryptKeyForHv1(
388-
branchKeyItemFromStorage,
389-
kmsConfiguration,
390-
grantTokens,
391-
kmsClient
392-
);
393-
plainTextKey := kmsRes.Plaintext.value;
394-
} else if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_2) {
395-
:- Need(
396-
HvUtils.HasUniqueTransformedKeys?(branchKeyItemFromStorage.EncryptionContext),
397-
Types.KeyStoreException(
398-
message := ErrorMessages.NOT_UNIQUE_BRANCH_KEY_CONTEXT_KEYS
399-
)
400-
);
401-
plainTextKey :- DecryptAndValidateKeyForHV2 (
402-
branchKeyItemFromStorage,
403-
kmsConfiguration,
404-
grantTokens,
405-
kmsClient
406-
);
407-
} else {
408-
// This else block will never be reached because we have check for hierarchical keyring version before if-else.
409-
var e := Types.KeyStoreException(
410-
message := ErrorMessages.INVALID_HIERARCHY_VERSION
411-
);
412-
return Failure(e);
413-
}
359+
var plainTextKey :- DecryptBranchKeyItem(branchKeyItemFromStorage, kmsConfiguration, grantTokens, kmsClient);
414360
var branchKeyMaterials :- Structure.ToBranchKeyMaterials(
415361
branchKeyItemFromStorage,
416362
plainTextKey
@@ -572,35 +518,7 @@ module GetKeys {
572518
message := ErrorMessages.INVALID_BRANCH_KEY_CONTEXT
573519
)
574520
);
575-
var plainTextKey: seq<uint8>;
576-
if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_1) {
577-
var kmsRes: KMS.DecryptResponse :- KMSKeystoreOperations.DecryptKeyForHv1(
578-
branchKeyItemFromStorage,
579-
kmsConfiguration,
580-
grantTokens,
581-
kmsClient
582-
);
583-
plainTextKey := kmsRes.Plaintext.value;
584-
} else if (branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION] == Structure.HIERARCHY_VERSION_VALUE_2) {
585-
:- Need(
586-
HvUtils.HasUniqueTransformedKeys?(branchKeyItemFromStorage.EncryptionContext),
587-
Types.KeyStoreException(
588-
message := ErrorMessages.NOT_UNIQUE_BRANCH_KEY_CONTEXT_KEYS
589-
)
590-
);
591-
plainTextKey :- DecryptAndValidateKeyForHV2 (
592-
branchKeyItemFromStorage,
593-
kmsConfiguration,
594-
grantTokens,
595-
kmsClient
596-
);
597-
} else {
598-
// This else block will never be reached because we have check for hierarchical keyring version before if-else.
599-
var e := Types.KeyStoreException(
600-
message := ErrorMessages.INVALID_HIERARCHY_VERSION
601-
);
602-
return Failure(e);
603-
}
521+
var plainTextKey :- DecryptBranchKeyItem(branchKeyItemFromStorage, kmsConfiguration, grantTokens, kmsClient);
604522
var branchKeyMaterials :- Structure.ToBeaconKeyMaterials(
605523
branchKeyItemFromStorage,
606524
plainTextKey
@@ -809,4 +727,64 @@ module GetKeys {
809727
&& activeBranchKeyOutput.beaconKeyMaterials == beaconKeyMaterials.value
810728
&& activeBranchKeyOutput.beaconKeyMaterials.beaconKeyIdentifier == expectedIdentifier
811729
}
730+
731+
method DecryptBranchKeyItem(
732+
branchKeyItemFromStorage: Types.EncryptedHierarchicalKey,
733+
kmsConfiguration: Types.KMSConfiguration,
734+
grantTokens: KMS.GrantTokenList,
735+
kmsClient: KMS.IKMSClient
736+
) returns (result: Result<seq<uint8>, Types.Error>)
737+
requires kmsClient.ValidState()
738+
modifies kmsClient.Modifies
739+
ensures kmsClient.ValidState()
740+
741+
requires Structure.BranchKeyContext?(branchKeyItemFromStorage.EncryptionContext)
742+
requires Structure.EncryptedHierarchicalKeyFromStorage?(branchKeyItemFromStorage)
743+
requires KmsArn.ValidKmsArn?(branchKeyItemFromStorage.KmsArn)
744+
requires KMSKeystoreOperations.AttemptKmsOperation?(kmsConfiguration, branchKeyItemFromStorage.KmsArn)
745+
746+
ensures result.Success?
747+
==>
748+
&& |kmsClient.History.Decrypt| == |old(kmsClient.History.Decrypt)| + 1
749+
&& var hv := branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION];
750+
&& ValidateKmsDecryption(branchKeyItemFromStorage, kmsConfiguration, grantTokens, kmsClient, hv)
751+
&& var decryptResponse := Seq.Last(kmsClient.History.Decrypt).output.value;
752+
&& if hv == Structure.HIERARCHY_VERSION_VALUE_2 then
753+
&& HvUtils.HasUniqueTransformedKeys?(branchKeyItemFromStorage.EncryptionContext)
754+
&& result.value == decryptResponse.Plaintext.value[Structure.BKC_DIGEST_LENGTH..]
755+
else
756+
&& result.value == decryptResponse.Plaintext.value
757+
{
758+
var hierarchyVersion := branchKeyItemFromStorage.EncryptionContext[Structure.HIERARCHY_VERSION];
759+
var plainTextKey: seq<uint8>;
760+
761+
if hierarchyVersion == Structure.HIERARCHY_VERSION_VALUE_1 {
762+
var kmsRes :- KMSKeystoreOperations.DecryptKeyForHv1(
763+
branchKeyItemFromStorage,
764+
kmsConfiguration,
765+
grantTokens,
766+
kmsClient
767+
);
768+
plainTextKey := kmsRes.Plaintext.value;
769+
return Success(plainTextKey);
770+
} else if hierarchyVersion == Structure.HIERARCHY_VERSION_VALUE_2 {
771+
if !HvUtils.HasUniqueTransformedKeys?(branchKeyItemFromStorage.EncryptionContext) {
772+
return Failure(Types.KeyStoreException(
773+
message := ErrorMessages.NOT_UNIQUE_BRANCH_KEY_CONTEXT_KEYS
774+
));
775+
}
776+
777+
var decryptResult :- DecryptAndValidateKeyForHV2(
778+
branchKeyItemFromStorage,
779+
kmsConfiguration,
780+
grantTokens,
781+
kmsClient
782+
);
783+
return Success(decryptResult);
784+
} else {
785+
return Failure(Types.KeyStoreException(
786+
message := ErrorMessages.INVALID_HIERARCHY_VERSION
787+
));
788+
}
789+
}
812790
}

0 commit comments

Comments
 (0)