-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAwsCryptographyKeyStoreOperations.dfy
300 lines (263 loc) · 11.1 KB
/
AwsCryptographyKeyStoreOperations.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
include "../Model/AwsCryptographyKeyStoreTypes.dfy"
include "../../AwsCryptographicMaterialProviders/src/Keyrings/AwsKms/AwsKmsUtils.dfy"
include "GetKeys.dfy"
include "CreateKeyStoreTable.dfy"
include "CreateKeys.dfy"
include "Structure.dfy"
include "ErrorMessages.dfy"
include "KmsArn.dfy"
module AwsCryptographyKeyStoreOperations refines AbstractAwsCryptographyKeyStoreOperations {
import opened AwsKmsUtils
import KO = KMSKeystoreOperations
import KMS = ComAmazonawsKmsTypes
import DDB = ComAmazonawsDynamodbTypes
import MPL = AwsCryptographyMaterialProvidersTypes
import CreateKeys
import CreateKeyStoreTable
import GetKeys
import UUID
import Time
import Structure
import ErrorMessages = KeyStoreErrorMessages
import KmsArn
datatype Config = Config(
nameonly id: string,
nameonly ddbTableName: DDB.TableName,
nameonly logicalKeyStoreName: string,
nameonly kmsConfiguration: KMSConfiguration,
nameonly grantTokens: KMS.GrantTokenList,
nameonly kmsClient: ComAmazonawsKmsTypes.IKMSClient,
nameonly ddbClient: ComAmazonawsDynamodbTypes.IDynamoDBClient
)
type InternalConfig = Config
predicate ValidInternalConfig?(config: InternalConfig)
{
&& DDB.IsValid_TableName(config.ddbTableName)
&& (config.kmsConfiguration.kmsKeyArn? ==> KmsArn.ValidKmsArn?(config.kmsConfiguration.kmsKeyArn))
&& (config.kmsConfiguration.kmsMRKeyArn? ==> KmsArn.ValidKmsArn?(config.kmsConfiguration.kmsMRKeyArn))
&& config.kmsClient.ValidState()
&& config.ddbClient.ValidState()
&& config.ddbClient.Modifies !! config.kmsClient.Modifies
}
function ModifiesInternalConfig(config: InternalConfig) : set<object>
{
config.kmsClient.Modifies + config.ddbClient.Modifies
}
predicate GetKeyStoreInfoEnsuresPublicly(output: Result<GetKeyStoreInfoOutput, Error>)
{true}
method GetKeyStoreInfo(config: InternalConfig)
returns (output: Result<GetKeyStoreInfoOutput, Error>)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getkeystoreinfo
//= type=implication
//# This operation MUST return the keystore information in this keystore configuration.
ensures output.Success? ==>
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getkeystoreinfo
//= type=implication
//# This MUST include:
&& output.value.keyStoreId == config.id
&& output.value.keyStoreName == config.ddbTableName
&& output.value.logicalKeyStoreName == config.logicalKeyStoreName
&& output.value.grantTokens == config.grantTokens
&& output.value.kmsConfiguration == config.kmsConfiguration
{
output := Success(
Types.GetKeyStoreInfoOutput(
keyStoreId := config.id,
keyStoreName := config.ddbTableName,
logicalKeyStoreName := config.logicalKeyStoreName,
grantTokens := config.grantTokens,
kmsConfiguration := config.kmsConfiguration
)
);
}
predicate CreateKeyStoreEnsuresPublicly(input: CreateKeyStoreInput, output: Result<CreateKeyStoreOutput, Error>)
{true}
method CreateKeyStore ( config: InternalConfig, input: CreateKeyStoreInput )
returns (output: Result<CreateKeyStoreOutput, Error>)
ensures output.Success? ==>
&& AwsArnParsing.ParseAmazonDynamodbTableName(output.value.tableArn).Success?
&& AwsArnParsing.ParseAmazonDynamodbTableName(output.value.tableArn).value == config.ddbTableName
{
var ddbTableArn :- CreateKeyStoreTable.CreateKeyStoreTable(config.ddbTableName, config.ddbClient);
var tableName := AwsArnParsing.ParseAmazonDynamodbTableName(ddbTableArn);
:- Need(
&& tableName.Success?
&& tableName.value == config.ddbTableName,
Types.KeyStoreException(message := "Configured DDB Table Name does not match parsed Table Name from DDB Table Arn.")
);
output := Success(Types.CreateKeyStoreOutput(tableArn := ddbTableArn));
}
predicate CreateKeyEnsuresPublicly(input: CreateKeyInput , output: Result<CreateKeyOutput, Error>)
{true}
method CreateKey ( config: InternalConfig , input: CreateKeyInput )
returns (output: Result<CreateKeyOutput, Error>)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# If an optional branch key id is provided
//# and no encryption context is provided this operation MUST fail.
ensures
&& input.branchKeyIdentifier.Some?
&& input.encryptionContext.None?
==> output.Failure?
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//= type=implication
//# If the Keystore's KMS Configuration is `Discovery` or `MRDiscovery`,
//# this operation MUST fail.
ensures
&& !KO.HasKeyId(config.kmsConfiguration)
==> output.Failure?
{
:- Need(input.branchKeyIdentifier.Some? ==>
&& input.encryptionContext.Some?
&& 0 < |input.encryptionContext.value|,
Types.KeyStoreException(message := ErrorMessages.CUSTOM_BRANCH_KEY_ID_NEED_EC));
:- Need(
KO.HasKeyId(config.kmsConfiguration),
Types.KeyStoreException(
message := ErrorMessages.DISCOVERY_CREATE_KEY_NOT_SUPPORTED
)
);
var branchKeyIdentifier: string;
if input.branchKeyIdentifier.None? {
//= aws-encryption-sdk-specification/framework/branch-key-store.md#createkey
//# If no branch key id is provided,
//# then this operation MUST create a [version 4 UUID](https://www.ietf.org/rfc/rfc4122.txt)
//# to be used as the branch key id.
var maybeBranchKeyId := UUID.GenerateUUID();
branchKeyIdentifier :- maybeBranchKeyId
.MapFailure(e => Types.KeyStoreException(message := e));
} else {
:- Need(0 < |input.branchKeyIdentifier.value|, Types.KeyStoreException(message := "Custom branch key id can not be an empty string."));
branchKeyIdentifier := input.branchKeyIdentifier.value;
}
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//# - `timestamp`: a timestamp for the current time.
//# This timestamp MUST be in ISO8601 format in UTC, to microsecond precision (e.g. “YYYY-MM-DDTHH:mm:ss.ssssssZ“)
var timestamp? := Time.GetCurrentTimeStamp();
var timestamp :- timestamp?
.MapFailure(e => Types.KeyStoreException(message := e));
var maybeBranchKeyVersion := UUID.GenerateUUID();
//= aws-encryption-sdk-specification/framework/branch-key-store.md#branch-key-and-beacon-key-creation
//# - `version`: a new guid. This guid MUST be [version 4 UUID](https://www.ietf.org/rfc/rfc4122.txt)
var branchKeyVersion :- maybeBranchKeyVersion
.MapFailure(e => Types.KeyStoreException(message := e));
var unwrapEncryptionContext := input.encryptionContext.UnwrapOr(map[]);
var encodedEncryptionContext
:= set k <- unwrapEncryptionContext
::
(UTF8.Decode(k), UTF8.Decode(unwrapEncryptionContext[k]), k);
// This SHOULD be impossible
// A Dafny string SHOULD all be encodable
:- Need(forall i <- encodedEncryptionContext
::
&& i.0.Success?
&& i.1.Success?
&& DDB.IsValid_AttributeName(Structure.ENCRYPTION_CONTEXT_PREFIX + i.0.value)
// Dafny requires that I *prove* that k == Encode(Decode(k))
// Since UTF8 can be lossy in some implementations
// this is the simplest...
// A prove that ValidUTF8Seq(i) ==> Decode(i).Success?
// would improve the situation
&& var encoded := UTF8.Encode(i.0.value);
&& encoded.Success?
&& i.2 == encoded.value
,
Types.KeyStoreException( message := ErrorMessages.UTF8_ENCODING_ENCRYPTION_CONTEXT_ERROR));
output := CreateKeys.CreateBranchAndBeaconKeys(
branchKeyIdentifier,
map i <- encodedEncryptionContext :: i.0.value := i.1.value,
timestamp,
branchKeyVersion,
config.ddbTableName,
config.logicalKeyStoreName,
config.kmsConfiguration,
config.grantTokens,
config.kmsClient,
config.ddbClient
);
}
predicate VersionKeyEnsuresPublicly(input: VersionKeyInput, output: Result<VersionKeyOutput, Error>)
{true}
method VersionKey(config: InternalConfig, input: VersionKeyInput)
returns (output: Result<VersionKeyOutput, Error>)
//= aws-encryption-sdk-specification/framework/branch-key-store.md#versionkey
//= type=implication
//# If the Keystore's KMS Configuration is `Discovery` or `MRDiscovery`,
//# this operation MUST immediately fail.
ensures
&& !KO.HasKeyId(config.kmsConfiguration)
==> output.Failure?
{
:- Need(
KO.HasKeyId(config.kmsConfiguration),
Types.KeyStoreException(
message := ErrorMessages.DISCOVERY_VERSION_KEY_NOT_SUPPORTED
)
);
:- Need(0 < |input.branchKeyIdentifier|, Types.KeyStoreException(message := ErrorMessages.BRANCH_KEY_ID_NEEDED));
var timestamp? := Time.GetCurrentTimeStamp();
var timestamp :- timestamp?
.MapFailure(e => Types.KeyStoreException(message := e));
var maybeBranchKeyVersion := UUID.GenerateUUID();
var branchKeyVersion :- maybeBranchKeyVersion
.MapFailure(e => Types.KeyStoreException(message := e));
output := CreateKeys.VersionActiveBranchKey(
input,
timestamp,
branchKeyVersion,
config.ddbTableName,
config.logicalKeyStoreName,
config.kmsConfiguration,
config.grantTokens,
config.kmsClient,
config.ddbClient
);
}
predicate GetActiveBranchKeyEnsuresPublicly(input: GetActiveBranchKeyInput, output: Result<GetActiveBranchKeyOutput, Error>)
{true}
method GetActiveBranchKey(config: InternalConfig, input: GetActiveBranchKeyInput)
returns (output: Result<GetActiveBranchKeyOutput, Error>)
{
output := GetKeys.GetActiveKeyAndUnwrap(
input,
config.ddbTableName,
config.logicalKeyStoreName,
config.kmsConfiguration,
config.grantTokens,
config.kmsClient,
config.ddbClient
);
}
predicate GetBranchKeyVersionEnsuresPublicly(input: GetBranchKeyVersionInput, output: Result<GetBranchKeyVersionOutput, Error>)
{true}
method GetBranchKeyVersion(config: InternalConfig, input: GetBranchKeyVersionInput)
returns (output: Result<GetBranchKeyVersionOutput, Error>)
{
output := GetKeys.GetBranchKeyVersion(
input,
config.ddbTableName,
config.logicalKeyStoreName,
config.kmsConfiguration,
config.grantTokens,
config.kmsClient,
config.ddbClient
);
}
predicate GetBeaconKeyEnsuresPublicly(input: GetBeaconKeyInput, output: Result<GetBeaconKeyOutput, Error>)
{true}
method GetBeaconKey(config: InternalConfig, input: GetBeaconKeyInput)
returns (output: Result<GetBeaconKeyOutput, Error>)
{
output := GetKeys.GetBeaconKeyAndUnwrap(
input,
config.ddbTableName,
config.logicalKeyStoreName,
config.kmsConfiguration,
config.grantTokens,
config.kmsClient,
config.ddbClient
);
}
}