2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
using System . Diagnostics ;
5
+ using Newtonsoft . Json ;
5
6
using Amazon ;
7
+ using Amazon . DynamoDBv2 ;
6
8
using Amazon . KeyManagementService ;
9
+ using AWS . Cryptography . KeyStore ;
7
10
using AWS . Cryptography . MaterialProviders ;
11
+ using AWS . Cryptography . MaterialProvidersTestVectorKeys ;
8
12
9
13
using RSAEncryption ;
10
14
@@ -18,6 +22,7 @@ public enum CryptoOperation
18
22
public static class MaterialProviderFactory
19
23
{
20
24
private static readonly MaterialProviders materialProviders = new ( new MaterialProvidersConfig ( ) ) ;
25
+ private static KeyVectors singletonKeyVectors ;
21
26
22
27
public static ICryptographicMaterialsManager CreateDecryptCmm (
23
28
DecryptVector vector ,
@@ -160,6 +165,57 @@ private static IKeyring CreateKeyring(MasterKey keyInfo, Key key, CryptoOperatio
160
165
return materialProviders . CreateAwsKmsMrkDiscoveryKeyring ( createKeyringInput ) ;
161
166
}
162
167
168
+ if ( keyInfo . Type == "aws-kms-hierarchy" ) {
169
+ // Lazily create a singleton KeyVectors client.
170
+ // A KeyVectors manifest is only required if a test vector specifies a hierarchy keyring.
171
+ // This specification can only be determined at runtime while reading the test vector manifest.
172
+ if ( singletonKeyVectors == null ) {
173
+ string manifestPath ;
174
+ try
175
+ {
176
+ manifestPath = Utils . GetEnvironmentVariableOrError ( "DAFNY_AWS_ESDK_TEST_VECTOR_MANIFEST_PATH" ) ;
177
+ }
178
+ catch ( ArgumentException e )
179
+ {
180
+ throw new ArgumentException ( "Hierarchy keyring test vectors must supply a KeyVectors manifest" , e ) ;
181
+ }
182
+ DecryptManifest manifest = Utils . LoadObjectFromPath < DecryptManifest > ( manifestPath ) ;
183
+ KeyVectorsConfig keyVectorsConfig = new KeyVectorsConfig
184
+ {
185
+ KeyManifestPath = Utils . ManifestUriToPath ( manifest . KeysUri , manifestPath )
186
+ } ;
187
+ singletonKeyVectors = new ( keyVectorsConfig ) ;
188
+ }
189
+
190
+ // Convert JSON to bytes for KeyVectors input
191
+ string jsonString = JsonConvert . SerializeObject ( keyInfo ) ;
192
+
193
+ var stream = new MemoryStream ( ) ;
194
+ var writer = new StreamWriter ( stream ) ;
195
+ writer . Write ( jsonString ) ;
196
+ writer . Flush ( ) ;
197
+ stream . Position = 0 ;
198
+
199
+ // Create KeyVectors keyring
200
+ var getKeyDescriptionInput = new GetKeyDescriptionInput
201
+ {
202
+ Json = stream
203
+ } ;
204
+
205
+ var desc = singletonKeyVectors . GetKeyDescription ( getKeyDescriptionInput ) ;
206
+
207
+ var testVectorKeyringInput = new TestVectorKeyringInput
208
+ {
209
+ KeyDescription = desc . KeyDescription
210
+ } ;
211
+
212
+ var keyring = singletonKeyVectors . CreateTestVectorKeyring (
213
+ testVectorKeyringInput
214
+ ) ;
215
+
216
+ return keyring ! ;
217
+ }
218
+
163
219
if ( keyInfo . Type == "raw" && keyInfo . EncryptionAlgorithm == "aes" ) {
164
220
CreateRawAesKeyringInput createKeyringInput = new CreateRawAesKeyringInput
165
221
{
@@ -209,7 +265,7 @@ private static IKeyring CreateKeyring(MasterKey keyInfo, Key key, CryptoOperatio
209
265
// string operationStr = operation == CryptoOperation.ENCRYPT
210
266
// ? "encryption"
211
267
// : "decryption";
212
- throw new Exception ( $ "Unsupported keyring type for { operation } ") ;
268
+ throw new Exception ( $ "Unsupported keyring { keyInfo . Type } type for { operation } ") ;
213
269
}
214
270
215
271
private static AesWrappingAlg AesAlgorithmFromBits ( ushort bits ) {
0 commit comments