Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc6c9a9

Browse files
committedNov 6, 2024·
format
1 parent cf4f403 commit fc6c9a9

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed
 

‎Examples/runtimes/net/src/Examples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Program
1111
static async Task Main()
1212
{
1313
ItemEncryptDecryptExample.PutItemGetItem();
14-
14+
1515
await BasicPutGetExample.PutItemGetItem();
1616
await ScanErrorExample.ScanError();
1717
await GetEncryptedDataKeyDescriptionExample.GetEncryptedDataKeyDescription();
@@ -33,7 +33,7 @@ static async Task Main()
3333
Thread.Sleep(5000);
3434

3535
await HierarchicalKeyringExample.HierarchicalKeyringGetItemPutItem(keyId, keyId2);
36-
36+
3737
await BasicSearchableEncryptionExample.PutItemQueryItemWithBeacon(keyId);
3838
await CompoundBeaconSearchableEncryptionExample.PutItemQueryItemWithCompoundBeacon(keyId);
3939
await VirtualBeaconSearchableEncryptionExample.PutItemQueryItemWithVirtualBeacon(keyId);

‎Examples/runtimes/net/src/TestUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class TestUtils
2323

2424
public static readonly string TEST_KMS_RSA_KEY_ID =
2525
"arn:aws:kms:us-west-2:658956600833:key/8b432da4-dde4-4bc3-a794-c7d68cbab5a6";
26-
27-
public static readonly string TEST_KMS_ECDH_KEY_ID_P256_SENDER =
26+
27+
public static readonly string TEST_KMS_ECDH_KEY_ID_P256_SENDER =
2828
"arn:aws:kms:us-west-2:370957321024:key/eabdf483-6be2-4d2d-8ee4-8c2583d416e9";
29-
30-
public static readonly string TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT =
29+
30+
public static readonly string TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT =
3131
"arn:aws:kms:us-west-2:370957321024:key/0265c8e9-5b6a-4055-8f70-63719e09fda5";
3232

3333
public static readonly string TEST_MRK_REPLICA_KEY_ID_US_EAST_1 =

‎Examples/runtimes/net/src/keyring/KmsEcdhKeyringExample.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static async Task KmsEcdhKeyringGetItemPutItem()
2828
{
2929
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
3030
var ecdhKeyArnSender = TestUtils.TEST_KMS_ECDH_KEY_ID_P256_SENDER;
31-
32-
31+
32+
3333
// 1. Load public keys from UTF-8 encoded PEM files.
3434
// You may provide your own PEM files to use here.
3535
// If you do not, the main method in this class will generate PEM
@@ -45,7 +45,7 @@ public static async Task KmsEcdhKeyringGetItemPutItem()
4545
{
4646
throw new IOException("Exception while reading private key from file", e);
4747
}
48-
48+
4949
MemoryStream publicKeyRecipientUtf8EncodedByteBuffer;
5050
try
5151
{
@@ -57,7 +57,7 @@ public static async Task KmsEcdhKeyringGetItemPutItem()
5757
{
5858
throw new IOException("Exception while reading public key from file", e);
5959
}
60-
60+
6161
// 2. Create the keyring.
6262
// The DynamoDb encryption client uses this to encrypt and decrypt items.
6363
var keyringInput = new CreateAwsKmsEcdhKeyringInput
@@ -88,12 +88,12 @@ public static async Task KmsEcdhKeyringGetItemPutItem()
8888

8989
await PutItemGetItemWithKeyring(kmsEcdhKeyring, ddbTableName);
9090
}
91-
91+
9292
private static async Task KmsEcdhKeyringDiscoveryGetItem()
9393
{
9494
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
9595
var ecdhKeyArnRecipient = TestUtils.TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT;
96-
96+
9797
// 1. Create a KMS ECDH keyring.
9898
// This keyring takes in:
9999
// - kmsClient
@@ -202,7 +202,7 @@ private static async Task GetItemWithKeyring(IKeyring kmsEcdhKeyring, string ddb
202202
var returnedItem = getResponse.Item;
203203
Debug.Assert(returnedItem["sensitive_data"].S.Equals("encrypt and sign me!"));
204204
}
205-
205+
206206
private static async Task PutItemGetItemWithKeyring(IKeyring kmsEcdhKeyring, string ddbTableName)
207207
{
208208
// 3. Configure which attributes are encrypted and/or signed when writing new items.
@@ -309,7 +309,7 @@ private static async Task PutItemGetItemWithKeyring(IKeyring kmsEcdhKeyring, str
309309
var returnedItem = getResponse.Item;
310310
Debug.Assert(returnedItem["sensitive_data"].S.Equals("encrypt and sign me!"));
311311
}
312-
312+
313313
public static async Task KmsEcdhKeyringExamples()
314314
{
315315
// You may provide your own ECC Keys in the files located at
@@ -325,7 +325,7 @@ public static async Task KmsEcdhKeyringExamples()
325325
await KmsEcdhKeyringGetItemPutItem();
326326
await KmsEcdhKeyringDiscoveryGetItem();
327327
}
328-
328+
329329
private static async Task WritePublicKeyPemForEccKey(string kmsEcdhKeyArn, string exampleEccPublicKeySenderFilename)
330330
{
331331
// Safety check: Validate file is not present
@@ -341,11 +341,11 @@ private static async Task WritePublicKeyPemForEccKey(string kmsEcdhKeyArn, strin
341341
new GetPublicKeyRequest { KeyId = kmsEcdhKeyArn }
342342
);
343343
var publicKeyByteArray = response.PublicKey.ToArray();
344-
344+
345345
StringWriter publicKeyStringWriter = new StringWriter();
346346
PemWriter publicKeyPemWriter = new PemWriter(publicKeyStringWriter);
347347
publicKeyPemWriter.WriteObject(new PemObject("PUBLIC KEY", publicKeyByteArray));
348-
348+
349349
var publicKeyUtf8EncodedByteBuffer = Encoding.UTF8.GetBytes(publicKeyStringWriter.ToString());
350350
var fc = new FileStream(exampleEccPublicKeySenderFilename, FileMode.Create, FileAccess.Write);
351351
fc.Write(publicKeyUtf8EncodedByteBuffer);
@@ -355,7 +355,7 @@ private static async Task WritePublicKeyPemForEccKey(string kmsEcdhKeyArn, strin
355355
private static bool ShouldGetNewEccKeys()
356356
{
357357
// If keys already exists; do not overwrite existing keys.
358-
if (File.Exists(EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME)
358+
if (File.Exists(EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME)
359359
|| File.Exists(EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME))
360360
{
361361
return false;
@@ -366,13 +366,13 @@ private static bool ShouldGetNewEccKeys()
366366
{
367367
throw new ApplicationException("Missing public key file at: " + EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME);
368368
}
369-
369+
370370
if (File.Exists(EXAMPLE_ECC_PUBLIC_KEY_SENDER_FILENAME)
371371
&& !File.Exists(EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME))
372372
{
373373
throw new ApplicationException("Missing public key file at: " + EXAMPLE_ECC_PUBLIC_KEY_RECIPIENT_FILENAME);
374374
}
375-
375+
376376
return true;
377377
}
378378
}

‎Examples/runtimes/net/src/keyring/RawEcdhKeyringExample.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class RawEcdhKeyringExample
6262
private static async Task RawEcdhKeyringExampleGetItemPutItem()
6363
{
6464
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
65-
65+
6666
// 1. Load key pair from UTF-8 encoded PEM files.
6767
// You may provide your own PEM files to use here.
6868
// If you do not, the main method in this class will generate PEM
@@ -77,7 +77,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
7777
{
7878
throw new IOException("Exception while reading private key from file", e);
7979
}
80-
80+
8181
MemoryStream publicKeyRecipientUtf8EncodedByteBuffer;
8282
try
8383
{
@@ -89,7 +89,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
8989
{
9090
throw new IOException("Exception while reading public key from file", e);
9191
}
92-
92+
9393
// 2. Create the keyring.
9494
// The DynamoDb encryption client uses this to encrypt and decrypt items.
9595
var keyringInput = new CreateRawEcdhKeyringInput
@@ -108,21 +108,21 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
108108
{
109109
// Must be a UTF8 PEM-encoded private key
110110
SenderStaticPrivateKey = privateKeySenderUtf8EncodedByteBuffer,
111-
// Must be a UTF8 DER-encoded X.509 public key also known as SubjectPublicKeyInfo.
111+
// Must be a UTF8 DER-encoded X.509 public key also known as SubjectPublicKeyInfo.
112112
RecipientPublicKey = publicKeyRecipientUtf8EncodedByteBuffer
113113
}
114114
}
115115
};
116116
var matProv = new MaterialProviders(new MaterialProvidersConfig());
117117
var rawEcdhKeyring = matProv.CreateRawEcdhKeyring(keyringInput);
118-
118+
119119
await PutGetExampleWithKeyring(rawEcdhKeyring, ddbTableName);
120120
}
121121

122122
private static async Task EphemeralRawEcdhKeyringPutItem()
123123
{
124124
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
125-
125+
126126
// 1. Load key pair from UTF-8 encoded PEM files.
127127
// You may provide your own PEM files to use here.
128128
// If you do not, the RawEcdhKeyringExamples method in this class will generate PEM
@@ -138,7 +138,7 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
138138
{
139139
throw new IOException("Exception while reading public key from file", e);
140140
}
141-
141+
142142
// 2. Create the keyring.
143143
// The DynamoDb encryption client uses this to encrypt and decrypt items.
144144
var keyringInput = new CreateRawEcdhKeyringInput
@@ -161,17 +161,17 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
161161
};
162162
var matProv = new MaterialProviders(new MaterialProvidersConfig());
163163
var rawEcdhKeyring = matProv.CreateRawEcdhKeyring(keyringInput);
164-
164+
165165
// A raw ecdh keyring with Ephemeral configuration cannot decrypt data since the key pair
166166
// used as the sender is ephemeral. This means that at decrypt time it does not have
167167
// the private key that corresponds to the public key that is stored on the message.
168168
await PutExampleWithKeyring(rawEcdhKeyring, ddbTableName);
169169
}
170-
170+
171171
private static async Task DiscoveryRawEcdhKeyringGetItem()
172172
{
173173
var ddbTableName = TestUtils.TEST_DDB_TABLE_NAME;
174-
174+
175175
// 1. Load key pair from UTF-8 encoded PEM files.
176176
// You may provide your own PEM files to use here.
177177
// If you do not, the main method in this class will generate PEM
@@ -186,7 +186,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
186186
{
187187
throw new IOException("Exception while reading private key from file", e);
188188
}
189-
189+
190190
// 2. Create the keyring.
191191
// The DynamoDb encryption client uses this to encrypt and decrypt items.
192192
var keyringInput = new CreateRawEcdhKeyringInput
@@ -210,7 +210,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
210210
};
211211
var matProv = new MaterialProviders(new MaterialProvidersConfig());
212212
var rawEcdhKeyring = matProv.CreateRawEcdhKeyring(keyringInput);
213-
213+
214214
await GetExampleWithKeyring(rawEcdhKeyring, ddbTableName);
215215
}
216216

@@ -403,7 +403,7 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
403403

404404
// Demonstrate that PutItem succeeded
405405
Debug.Assert(putResponse.HttpStatusCode == HttpStatusCode.OK);
406-
406+
407407
// 8. Try to get the item and assert that the ephemeral keyring configuration
408408
// cannot decrypt data.
409409
var keyToGet = new Dictionary<String, AttributeValue>
@@ -426,9 +426,9 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
426426
{
427427
Debug.Assert(e.Message.Contains("EphemeralPrivateKeyToStaticPublicKey Key Agreement Scheme is forbidden on decrypt."));
428428
}
429-
429+
430430
}
431-
431+
432432
private static async Task GetExampleWithKeyring(IKeyring rawEcdhKeyring, string ddbTableName)
433433
{
434434
// 3. Configure which attributes are encrypted and/or signed when writing new items.
@@ -515,7 +515,7 @@ private static async Task GetExampleWithKeyring(IKeyring rawEcdhKeyring, string
515515
{
516516
Debug.Assert(e.Message.Contains("PublicKeyDiscovery Key Agreement Scheme is forbidden on encrypt."));
517517
}
518-
518+
519519
// 8. Get the item back from our table using the same client.
520520
// The client will decrypt the item client-side, and return
521521
// back the original item.
@@ -550,7 +550,7 @@ public static async Task RawEcdhKeyringExamples()
550550
{
551551
GenerateEccKeyPairs();
552552
}
553-
553+
554554
await RawEcdhKeyringExampleGetItemPutItem();
555555
await EphemeralRawEcdhKeyringPutItem();
556556
await DiscoveryRawEcdhKeyringGetItem();
@@ -560,44 +560,44 @@ public static async Task RawEcdhKeyringExamples()
560560
private static bool ShouldGenerateNewEccKeys()
561561
{
562562
// If keys already exists; do not overwrite existing keys.
563-
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
563+
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
564564
&& File.Exists(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT)
565565
&& File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))
566566
{
567567
return false;
568568
}
569569

570570
// If only two keys are present; throw exception
571-
if (!File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
571+
if (!File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
572572
&& File.Exists(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT)
573573
&& File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))
574574
{
575575
throw new ApplicationException("Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER);
576576
}
577-
577+
578578
// If only two keys are present; throw exception
579-
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
579+
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
580580
&& File.Exists(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT)
581581
&& !File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))
582582
{
583583
throw new ApplicationException("Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT);
584584
}
585-
585+
586586
// If only two keys are present; throw exception
587-
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
587+
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
588588
&& !File.Exists(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT)
589589
&& File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))
590590
{
591591
throw new ApplicationException("Missing public key file at: " + EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT);
592592
}
593-
593+
594594
return true;
595595
}
596-
596+
597597
private static void GenerateEccKeyPairs()
598598
{
599599
// Safety check; Validate neither file is present
600-
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
600+
if (File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER)
601601
|| File.Exists(EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT)
602602
|| File.Exists(EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT))
603603
{
@@ -620,7 +620,7 @@ private static void GenerateEccKeyPairs()
620620
generator = new ECKeyPairGenerator();
621621
SecureRandom rng = new SecureRandom();
622622
X9ECParameters p = ECNamedCurveTable.GetByName("secp256r1");
623-
623+
624624
var domainParameters = new ECDomainParameters(p.Curve, p.G, p.N, p.H);
625625
generator.Init(new ECKeyGenerationParameters(domainParameters, rng));
626626

@@ -630,10 +630,10 @@ private static void GenerateEccKeyPairs()
630630
Console.WriteLine(e);
631631
throw;
632632
}
633-
633+
634634
AsymmetricCipherKeyPair senderKeyPair = generator.GenerateKeyPair();
635635
AsymmetricCipherKeyPair recipientKeyPair = generator.GenerateKeyPair();
636-
636+
637637
WritePrivateKey(senderKeyPair.Private, EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER);
638638
WritePrivateKey(recipientKeyPair.Private, EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT);
639639
WritePublicKey(recipientKeyPair, "secp256r1", EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT);
@@ -650,19 +650,19 @@ private static void WritePrivateKey(AsymmetricKeyParameter privateKey, string fi
650650
fc.Write(privateKeyUtf8EncodedBytes);
651651
fc.Close();
652652
}
653-
653+
654654
private static void WritePublicKey(AsymmetricCipherKeyPair publicKey, string curveName, string fileName)
655655
{
656656
var ecdhCurveSpecFromCurveName = ToEcdhCurveSpec(curveName);
657657
var spki = KeyGeneration.SerializePublicKey(publicKey, ecdhCurveSpecFromCurveName).CloneAsArray();
658-
658+
659659
var publicKeyStringWriter = new StringWriter();
660660
var publicKeyPemWriter = new PemWriter(publicKeyStringWriter);
661661
publicKeyPemWriter.WriteObject(new PemObject("PUBLIC KEY", spki));
662662
var publicKeyUtf8EncodedBytes = Encoding.UTF8.GetBytes(publicKeyStringWriter.ToString());
663663
var fc = new FileStream(fileName, FileMode.Create, FileAccess.Write);
664664
fc.Write(publicKeyUtf8EncodedBytes);
665-
fc.Close();
665+
fc.Close();
666666
}
667667

668668
private static _IECDHCurveSpec ToEcdhCurveSpec(string curveName)

0 commit comments

Comments
 (0)
Please sign in to comment.