Skip to content

Commit a7466e6

Browse files
test(test_vectors): Support reading manifests that specify a hierarchy keyring (#649)
1 parent 56fcd13 commit a7466e6

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

.github/workflows/library_interop_tests.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ jobs:
7676
# This works because `node` is installed by default on GHA runners
7777
CORES=$(node -e 'console.log(os.cpus().length)')
7878
make transpile_net CORES=$CORES
79+
80+
- name: Compile MPL TestVectors implementation
81+
shell: bash
82+
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
83+
run: |
84+
# This works because `node` is installed by default on GHA runners
85+
CORES=$(node -e 'console.log(os.cpus().length)')
86+
make transpile_net CORES=$CORES
7987
8088
- name: Fetch Python 2.3.0 Test Vectors
8189
working-directory: ./
@@ -166,6 +174,15 @@ jobs:
166174
# This works because `node` is installed by default on GHA runners
167175
CORES=$(node -e 'console.log(os.cpus().length)')
168176
make transpile_net CORES=$CORES
177+
178+
179+
- name: Compile MPL TestVectors implementation
180+
shell: bash
181+
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
182+
run: |
183+
# This works because `node` is installed by default on GHA runners
184+
CORES=$(node -e 'console.log(os.cpus().length)')
185+
make transpile_net CORES=$CORES
169186
170187

171188
# # TODO: Fix Zip file creation on Windows

.github/workflows/library_net_tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ jobs:
9292
CORES=$(node -e 'console.log(os.cpus().length)')
9393
make transpile_net CORES=$CORES
9494
95+
96+
- name: Compile MPL TestVectors implementation
97+
shell: bash
98+
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
99+
run: |
100+
# This works because `node` is installed by default on GHA runners
101+
CORES=$(node -e 'console.log(os.cpus().length)')
102+
make transpile_net CORES=$CORES
103+
95104
- name: Test .NET Framework net48
96105
working-directory: ./AwsEncryptionSDK
97106
if: matrix.os == 'windows-latest'

AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/AWSEncryptionSDKTestVectorLib.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<ItemGroup>
1111
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1212
<ProjectReference Include="../../ESDK.csproj" />
13+
14+
<!-- TODO: Reference published MPL TestVectors project -->
15+
<ProjectReference Include="../../../../../mpl/TestVectorsAwsCryptographicMaterialProviders/runtimes/net/TestVectors.csproj" />
1316
</ItemGroup>
1417

1518
</Project>

AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics;
5+
using Newtonsoft.Json;
56
using Amazon;
7+
using Amazon.DynamoDBv2;
68
using Amazon.KeyManagementService;
9+
using AWS.Cryptography.KeyStore;
710
using AWS.Cryptography.MaterialProviders;
11+
using AWS.Cryptography.MaterialProvidersTestVectorKeys;
812

913
using RSAEncryption;
1014

@@ -18,6 +22,7 @@ public enum CryptoOperation
1822
public static class MaterialProviderFactory
1923
{
2024
private static readonly MaterialProviders materialProviders = new(new MaterialProvidersConfig());
25+
private static KeyVectors singletonKeyVectors;
2126

2227
public static ICryptographicMaterialsManager CreateDecryptCmm(
2328
DecryptVector vector,
@@ -160,6 +165,57 @@ private static IKeyring CreateKeyring(MasterKey keyInfo, Key key, CryptoOperatio
160165
return materialProviders.CreateAwsKmsMrkDiscoveryKeyring(createKeyringInput);
161166
}
162167

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+
163219
if (keyInfo.Type == "raw" && keyInfo.EncryptionAlgorithm == "aes") {
164220
CreateRawAesKeyringInput createKeyringInput = new CreateRawAesKeyringInput
165221
{
@@ -209,7 +265,7 @@ private static IKeyring CreateKeyring(MasterKey keyInfo, Key key, CryptoOperatio
209265
// string operationStr = operation == CryptoOperation.ENCRYPT
210266
// ? "encryption"
211267
// : "decryption";
212-
throw new Exception($"Unsupported keyring type for {operation}");
268+
throw new Exception($"Unsupported keyring {keyInfo.Type} type for {operation}");
213269
}
214270

215271
private static AesWrappingAlg AesAlgorithmFromBits(ushort bits) {

AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/TestVectorTypes.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class Key {
2525
public string? Encoding { get; set; }
2626
[JsonProperty("material")]
2727
public string? Material { get; set; }
28+
[JsonProperty("branchKeyVersion")]
29+
public string? BranchKeyVersion { get; set; }
30+
[JsonProperty("branchKey")]
31+
public string? BranchKey { get; set; }
32+
[JsonProperty("beaconKey")]
33+
public string? BeaconKey { get; set; }
2834
}
2935

3036
public class KeyManifest

0 commit comments

Comments
 (0)