Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(test_vectors): Support reading manifests that specify a hierarchy keyring #649

Merged
merged 31 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
94b86b0
[DO NOT MERGE] run hkeyring test vectors
lucasmcdonald3 May 6, 2024
efe212a
cleanup
lucasmcdonald3 May 6, 2024
cf24fc2
cleanup
lucasmcdonald3 May 6, 2024
07f8efa
cleanup
lucasmcdonald3 May 6, 2024
e4e7503
wip
lucasmcdonald3 Jun 26, 2024
a46051d
debug
lucasmcdonald3 Jun 26, 2024
96d6d60
add keys.json to repo
RitvikKapila Jun 28, 2024
ead2339
Merge branch 'mainline' into lucmcdon/hkeyring-vectors
RitvikKapila Jun 28, 2024
d9da67b
fix
RitvikKapila Jun 28, 2024
c61d3e0
debug
RitvikKapila Jun 28, 2024
8ee95df
fix: add curr dir path
RitvikKapila Jun 28, 2024
e415a5b
fix
RitvikKapila Jun 28, 2024
ec7d644
fix
RitvikKapila Jun 28, 2024
955c409
fix
RitvikKapila Jun 28, 2024
581c46b
add ManifestUriToPath
RitvikKapila Jul 1, 2024
e25bd46
fix
RitvikKapila Jul 1, 2024
ffbba92
fix
RitvikKapila Jul 1, 2024
d85bb1e
static
RitvikKapila Jul 1, 2024
594933d
private
RitvikKapila Jul 1, 2024
956c206
set DAFNY_AWS_ESDK_TEST_VECTOR_MANIFEST_PATH in generate_vectors
RitvikKapila Jul 1, 2024
1d9cdd1
fix
RitvikKapila Jul 1, 2024
463fc27
correct manifest path
RitvikKapila Jul 1, 2024
368b6b9
download py23 test vectors for generate_vectors
RitvikKapila Jul 1, 2024
107b1e0
Merge branch 'mainline' into lucmcdon/hkeyring-vectors
lucasmcdonald3 Oct 2, 2024
200bed8
Update AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/…
lucasmcdonald3 Oct 2, 2024
6db2fa9
m
lucasmcdonald3 Oct 2, 2024
18694c1
m
lucasmcdonald3 Oct 2, 2024
dfb6308
m
lucasmcdonald3 Oct 2, 2024
2728c90
m
lucasmcdonald3 Oct 2, 2024
5b97a16
m
lucasmcdonald3 Oct 2, 2024
7b81bbd
Merge branch 'mainline' into lucmcdon/hkeyring-vectors
lucasmcdonald3 Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/library_interop_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ jobs:
# This works because `node` is installed by default on GHA runners
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES

- name: Compile MPL TestVectors implementation
shell: bash
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
run: |
# This works because `node` is installed by default on GHA runners
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES

- name: Fetch Python 2.3.0 Test Vectors
working-directory: ./
Expand Down Expand Up @@ -166,6 +174,15 @@ jobs:
# This works because `node` is installed by default on GHA runners
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES


- name: Compile MPL TestVectors implementation
shell: bash
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
run: |
# This works because `node` is installed by default on GHA runners
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES


# # TODO: Fix Zip file creation on Windows
Expand All @@ -177,6 +194,16 @@ jobs:
# # Set-Location -Path "$env:GITHUB_WORKSPACE\net41\vectors"
# # Compress-Archive -Path "$env:GITHUB_WORKSPACE\net41\vectors\*" -DestinationPath "$env:GITHUB_WORKSPACE\net41\vectors\net41.zip"

- name: Fetch Python 2.3.0 Test Vectors
working-directory: ./
shell: bash
run: |
PYTHON_23_VECTOR_PATH=$GITHUB_WORKSPACE/python23/vectors
mkdir -p $PYTHON_23_VECTOR_PATH
DOWNLOAD_NAME=python23.zip
curl --no-progress-meter --output $DOWNLOAD_NAME --location $VECTORS_URL
unzip -o -qq $DOWNLOAD_NAME -d $PYTHON_23_VECTOR_PATH
rm $DOWNLOAD_NAME

- name: Generate Test Vectors with .NET Framework net6.0
# TODO Post-#619: Fix Zip file creation on Windows
Expand All @@ -187,6 +214,8 @@ jobs:
NET_41_VECTOR_PATH=net41/vectors
mkdir -p $NET_41_VECTOR_PATH
GEN_PATH=runtimes/net/TestVectorsNative/TestVectorGenerator
PYTHON_23_VECTOR_PATH=$GITHUB_WORKSPACE/python23/vectors
DAFNY_AWS_ESDK_TEST_VECTOR_MANIFEST_PATH="$PYTHON_23_VECTOR_PATH/manifest.json" \
dotnet run --project $GEN_PATH --framework net6.0 -- \
--encrypt-manifest $GEN_PATH/resources/0006-awses-message-decryption-generation.v2.json \
--output-dir $NET_41_VECTOR_PATH
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/library_net_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ jobs:
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES


- name: Compile MPL TestVectors implementation
shell: bash
working-directory: ./mpl/TestVectorsAwsCryptographicMaterialProviders
run: |
# This works because `node` is installed by default on GHA runners
CORES=$(node -e 'console.log(os.cpus().length)')
make transpile_net CORES=$CORES

- name: Test .NET Framework net48
working-directory: ./AwsEncryptionSDK
if: matrix.os == 'windows-latest'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<ProjectReference Include="../../ESDK.csproj" />

<!-- TODO: Reference published MPL TestVectors project -->
<ProjectReference Include="../../../../../mpl/TestVectorsAwsCryptographicMaterialProviders/runtimes/net/TestVectors.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using Newtonsoft.Json;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.KeyManagementService;
using AWS.Cryptography.KeyStore;
using AWS.Cryptography.MaterialProviders;
using AWS.Cryptography.MaterialProvidersTestVectorKeys;

using RSAEncryption;

Expand All @@ -18,6 +22,14 @@
public static class MaterialProviderFactory
{
private static readonly MaterialProviders materialProviders = new(new MaterialProvidersConfig());
// TODO: Get this from CLI or something?
lucasmcdonald3 marked this conversation as resolved.
Show resolved Hide resolved
private static string manifestPath = Utils.GetEnvironmentVariableOrError("DAFNY_AWS_ESDK_TEST_VECTOR_MANIFEST_PATH");
private static DecryptManifest manifest = Utils.LoadObjectFromPath<DecryptManifest>(manifestPath);
private static readonly KeyVectorsConfig keyVectorsConfig = new KeyVectorsConfig
{
KeyManifestPath = Utils.ManifestUriToPath(manifest.KeysUri, manifestPath)
};
private static KeyVectors keyVectors = new(keyVectorsConfig);

public static ICryptographicMaterialsManager CreateDecryptCmm(
DecryptVector vector,
Expand Down Expand Up @@ -55,11 +67,11 @@
private static IKeyring CreateDecryptKeyring(DecryptVector vector, Dictionary<string, Key> keys) {
List<IKeyring> children = new List<IKeyring>();
Debug.Assert(vector.MasterKeys != null, "vector.MasterKeys != null");
foreach (MasterKey keyInfo in vector.MasterKeys)

Check warning on line 70 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Dereference of a possibly null reference.
{
// Some keyrings, like discovery KMS keyrings, do not specify keys
Key key = keyInfo.Key == null ? null : keys[keyInfo.Key];

Check warning on line 73 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Converting null literal or possible null value to non-nullable type.
children.Add(CreateKeyring(keyInfo, key, CryptoOperation.DECRYPT));

Check warning on line 74 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Possible null reference argument for parameter 'key' in 'IKeyring MaterialProviderFactory.CreateKeyring(MasterKey keyInfo, Key key, CryptoOperation operation)'.
}
CreateMultiKeyringInput createMultiKeyringInput = new CreateMultiKeyringInput
{
Expand Down Expand Up @@ -102,14 +114,14 @@
IList<MasterKey> masterKeys = vector.Scenario.MasterKeys;
Debug.Assert(masterKeys.Count >= 1);

Key generatorKey = keys[masterKeys[0].Key];

Check warning on line 117 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Possible null reference argument for parameter 'key' in 'Key Dictionary<string, Key>.this[string key]'.
IKeyring generatorKeyring = CreateKeyring(masterKeys[0], generatorKey, CryptoOperation.ENCRYPT);

List<IKeyring> children = masterKeys
.Skip(1)
.Select(masterKey =>
{
Key key = keys[masterKey.Key];

Check warning on line 124 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Possible null reference argument for parameter 'key' in 'Key Dictionary<string, Key>.this[string key]'.
return CreateKeyring(masterKey, key, CryptoOperation.ENCRYPT);
})
.ToList();
Expand Down Expand Up @@ -141,12 +153,12 @@
}

if (keyInfo.Type == "aws-kms-mrk-aware-discovery" && operation == CryptoOperation.DECRYPT) {
AWS.Cryptography.MaterialProviders.DiscoveryFilter filter = null;

Check warning on line 156 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Converting null literal or possible null value to non-nullable type.
if (keyInfo.AwsKmsDiscoveryFilter != null)
{
filter = new AWS.Cryptography.MaterialProviders.DiscoveryFilter
{
AccountIds = (List<string>)keyInfo.AwsKmsDiscoveryFilter.AccountIds,

Check warning on line 161 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Converting null literal or possible null value to non-nullable type.
Partition = keyInfo.AwsKmsDiscoveryFilter.Partition,
};
}
Expand All @@ -160,6 +172,36 @@
return materialProviders.CreateAwsKmsMrkDiscoveryKeyring(createKeyringInput);
}

if (keyInfo.Type == "aws-kms-hierarchy") {
// Convert JSON to bytes for KeyVectors input
string jsonString = JsonConvert.SerializeObject(keyInfo);

var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(jsonString);
writer.Flush();
stream.Position = 0;

// Create KeyVectors keyring
var getKeyDescriptionInput = new GetKeyDescriptionInput
{
Json = stream
};

var desc = keyVectors.GetKeyDescription(getKeyDescriptionInput);

var testVectorKeyringInput = new TestVectorKeyringInput
{
KeyDescription = desc.KeyDescription
};

var keyring = keyVectors.CreateTestVectorKeyring(
testVectorKeyringInput
);

return keyring!;
}

if (keyInfo.Type == "raw" && keyInfo.EncryptionAlgorithm == "aes") {
CreateRawAesKeyringInput createKeyringInput = new CreateRawAesKeyringInput
{
Expand All @@ -173,7 +215,7 @@
}

if (keyInfo.Type == "raw" && keyInfo.EncryptionAlgorithm == "rsa" && key.Type == "private") {
PaddingScheme padding = RSAPaddingFromStrings(keyInfo.PaddingAlgorithm, keyInfo.PaddingHash);

Check warning on line 218 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Possible null reference argument for parameter 'strAlg' in 'PaddingScheme MaterialProviderFactory.RSAPaddingFromStrings(string strAlg, string strHash)'.

Check warning on line 218 in AwsEncryptionSDK/runtimes/net/TestVectorsNative/TestVectorLib/MaterialProviderFactory.cs

View workflow job for this annotation

GitHub Actions / pr-ci-net / testDotNet (windows-latest)

Possible null reference argument for parameter 'strHash' in 'PaddingScheme MaterialProviderFactory.RSAPaddingFromStrings(string strAlg, string strHash)'.
byte[] privateKey = RSA.ParsePEMString(key.Material);
CreateRawRsaKeyringInput createKeyringInput = new CreateRawRsaKeyringInput
{
Expand Down Expand Up @@ -209,7 +251,7 @@
// string operationStr = operation == CryptoOperation.ENCRYPT
// ? "encryption"
// : "decryption";
throw new Exception($"Unsupported keyring type for {operation}");
throw new Exception($"Unsupported keyring {keyInfo.Type} type for {operation}");
}

private static AesWrappingAlg AesAlgorithmFromBits(ushort bits) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class Key {
public string? Encoding { get; set; }
[JsonProperty("material")]
public string? Material { get; set; }
[JsonProperty("branchKeyVersion")]
public string? BranchKeyVersion { get; set; }
[JsonProperty("branchKey")]
public string? BranchKey { get; set; }
[JsonProperty("beaconKey")]
public string? BeaconKey { get; set; }
}

public class KeyManifest
Expand Down
Loading