|
1 | 1 | // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -include "DynamoToStruct.dfy" |
5 |
| -include "Util.dfy" |
| 4 | +include "../../DynamoDbItemEncryptor/src/Util.dfy" |
6 | 5 |
|
7 | 6 | module DynamoDbEncryptionBranchKeyIdSupplier {
|
8 | 7 | import opened AwsCryptographyDbEncryptionSdkDynamoDbTypes
|
9 |
| - import MPL = AwsCryptographyMaterialProvidersTypes |
10 |
| - import DDB = ComAmazonawsDynamodbTypes |
11 |
| - import opened Seq |
12 | 8 | import opened Wrappers
|
13 |
| - import opened StandardLibrary.UInt |
14 |
| - import DynamoToStruct |
15 |
| - import Base64 |
16 |
| - import DynamoDbEncryptionUtil |
17 |
| - |
18 |
| - const MPL_EC_PARTITION_NAME: UTF8.ValidUTF8Bytes := UTF8.EncodeAscii("aws-crypto-partition-name") |
19 |
| - const MPL_EC_SORT_NAME: UTF8.ValidUTF8Bytes := UTF8.EncodeAscii("aws-crypto-sort-name") |
| 9 | + import MPL = AwsCryptographyMaterialProvidersTypes |
| 10 | + import DynamoDbItemEncryptorUtil |
20 | 11 |
|
21 | 12 | class DynamoDbEncryptionBranchKeyIdSupplier
|
22 | 13 | extends MPL.IBranchKeyIdSupplier
|
23 |
| - { |
| 14 | + { |
24 | 15 | const ddbKeyBranchKeyIdSupplier: IDynamoDbKeyBranchKeyIdSupplier
|
25 | 16 |
|
26 | 17 | predicate ValidState()
|
@@ -48,76 +39,31 @@ module DynamoDbEncryptionBranchKeyIdSupplier {
|
48 | 39 | {true}
|
49 | 40 |
|
50 | 41 | method GetBranchKeyId'(input: MPL.GetBranchKeyIdInput)
|
51 |
| - returns (output: Result<MPL.GetBranchKeyIdOutput, MPL.Error>) |
| 42 | + returns (output: Result<MPL.GetBranchKeyIdOutput, MPL.Error>) |
52 | 43 | requires ValidState()
|
53 | 44 | modifies Modifies - {History}
|
54 | 45 | decreases Modifies - {History}
|
55 | 46 | ensures ValidState()
|
56 | 47 | ensures GetBranchKeyIdEnsuresPublicly(input, output)
|
57 | 48 | ensures unchanged(History)
|
58 | 49 | {
|
59 |
| - var context := input.encryptionContext; |
60 |
| - var attrMap: DDB.AttributeMap := map[]; |
| 50 | + var attrMapR := DynamoDbItemEncryptorUtil.ConvertContextForSelector(input.encryptionContext); |
| 51 | + var attrMap :- attrMapR.MapFailure(e => MPL.AwsCryptographicMaterialProvidersException(message:=e)); |
61 | 52 |
|
62 |
| - // Add partition key to map |
63 |
| - :- Need(MPL_EC_PARTITION_NAME in context.Keys, |
64 |
| - MPL.AwsCryptographicMaterialProvidersException( |
65 |
| - message := "Invalid encryption context: Missing partition name")); |
66 |
| - var partitionName := context[MPL_EC_PARTITION_NAME]; |
67 |
| - var partitionValueKey := DynamoDbEncryptionUtil.DDBEC_EC_ATTR_PREFIX + partitionName; |
68 |
| - :- Need(partitionValueKey in context.Keys, |
69 |
| - MPL.AwsCryptographicMaterialProvidersException( |
70 |
| - message := "Invalid encryption context: Missing partition value")); |
71 |
| - attrMap :- AddAttributeToMap(partitionValueKey, context[partitionValueKey], attrMap); |
72 |
| - |
73 |
| - if MPL_EC_SORT_NAME in context.Keys { |
74 |
| - var sortName := context[MPL_EC_SORT_NAME]; |
75 |
| - var sortValueKey := DynamoDbEncryptionUtil.DDBEC_EC_ATTR_PREFIX + sortName; |
76 |
| - :- Need(sortValueKey in context.Keys, |
77 |
| - MPL.AwsCryptographicMaterialProvidersException( |
78 |
| - message := "Invalid encryption context: Missing sort value")); |
79 |
| - attrMap :- AddAttributeToMap(sortValueKey, context[sortValueKey], attrMap); |
80 |
| - } |
81 |
| - |
82 | 53 | // Get branch key id from these DDB attributes
|
83 | 54 | var branchKeyIdR := ddbKeyBranchKeyIdSupplier.GetBranchKeyIdFromDdbKey(
|
84 |
| - GetBranchKeyIdFromDdbKeyInput(ddbKey := attrMap) |
85 |
| - ); |
| 55 | + GetBranchKeyIdFromDdbKeyInput(ddbKey := attrMap) |
| 56 | + ); |
| 57 | + //= specification/dynamodb-encryption-client/ddb-encryption-branch-key-id-supplier.md#behavior |
| 58 | + //# - Otherwise, this operation MUST fail. |
86 | 59 | var branchKeyIdOut :- branchKeyIdR.MapFailure(ConvertToMplError);
|
87 | 60 |
|
| 61 | + //= specification/dynamodb-encryption-client/ddb-encryption-branch-key-id-supplier.md#behavior |
| 62 | + //# - If successful, the resulting string MUST be outputted by this operation. |
88 | 63 | return Success(MPL.GetBranchKeyIdOutput(branchKeyId:=branchKeyIdOut.branchKeyId));
|
89 | 64 | }
|
90 | 65 | }
|
91 | 66 |
|
92 |
| - function method AddAttributeToMap(ddbAttrKey: seq<uint8>, encodedAttrValue: seq<uint8>, attrMap: DDB.AttributeMap) |
93 |
| - : (res: Result<DDB.AttributeMap, MPL.Error>) |
94 |
| - requires |ddbAttrKey| >= |DynamoDbEncryptionUtil.DDBEC_EC_ATTR_PREFIX| |
95 |
| - { |
96 |
| - // Obtain attribute name from EC kvPair key |
97 |
| - var ddbAttrNameBytes := ddbAttrKey[|DynamoDbEncryptionUtil.DDBEC_EC_ATTR_PREFIX|..]; |
98 |
| - var ddbAttrName :- UTF8.Decode(ddbAttrNameBytes) |
99 |
| - .MapFailure(e => MPL.AwsCryptographicMaterialProvidersException(message:=e)); |
100 |
| - :- Need(DDB.IsValid_AttributeName(ddbAttrName), |
101 |
| - MPL.AwsCryptographicMaterialProvidersException( |
102 |
| - message := "Invalid serialization of DDB Attribute in encryption context.")); |
103 |
| - |
104 |
| - // Obtain attribute value from EC kvPair value |
105 |
| - var utf8DecodedVal :- UTF8.Decode(encodedAttrValue) |
106 |
| - .MapFailure(e => MPL.AwsCryptographicMaterialProvidersException(message:=e)); |
107 |
| - var base64DecodedVal :- Base64.Decode(utf8DecodedVal) |
108 |
| - .MapFailure(e => MPL.AwsCryptographicMaterialProvidersException(message:=e)); |
109 |
| - :- Need(|base64DecodedVal| >= 2, |
110 |
| - MPL.AwsCryptographicMaterialProvidersException( |
111 |
| - message := "Invalid serialization of DDB Attribute in encryption context.")); |
112 |
| - var typeId := base64DecodedVal[..2]; |
113 |
| - var serializedValue := base64DecodedVal[2..]; |
114 |
| - var ddbAttrValue :- DynamoToStruct.BytesToAttr(serializedValue, typeId, false) |
115 |
| - .MapFailure(e => MPL.AwsCryptographicMaterialProvidersException(message:=e)); |
116 |
| - |
117 |
| - // Add to our AttributeMap |
118 |
| - Success(attrMap[ddbAttrName := ddbAttrValue.val]) |
119 |
| - } |
120 |
| - |
121 | 67 | function method ConvertToMplError(err: Error)
|
122 | 68 | :(ret: MPL.Error)
|
123 | 69 | {
|
|
0 commit comments