@@ -62,7 +62,7 @@ public class RawEcdhKeyringExample
62
62
private static async Task RawEcdhKeyringExampleGetItemPutItem ( )
63
63
{
64
64
var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
65
-
65
+
66
66
// 1. Load key pair from UTF-8 encoded PEM files.
67
67
// You may provide your own PEM files to use here.
68
68
// If you do not, the main method in this class will generate PEM
@@ -77,7 +77,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
77
77
{
78
78
throw new IOException ( "Exception while reading private key from file" , e ) ;
79
79
}
80
-
80
+
81
81
MemoryStream publicKeyRecipientUtf8EncodedByteBuffer ;
82
82
try
83
83
{
@@ -89,7 +89,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
89
89
{
90
90
throw new IOException ( "Exception while reading public key from file" , e ) ;
91
91
}
92
-
92
+
93
93
// 2. Create the keyring.
94
94
// The DynamoDb encryption client uses this to encrypt and decrypt items.
95
95
var keyringInput = new CreateRawEcdhKeyringInput
@@ -108,21 +108,21 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
108
108
{
109
109
// Must be a UTF8 PEM-encoded private key
110
110
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.
112
112
RecipientPublicKey = publicKeyRecipientUtf8EncodedByteBuffer
113
113
}
114
114
}
115
115
} ;
116
116
var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
117
117
var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
118
-
118
+
119
119
await PutGetExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
120
120
}
121
121
122
122
private static async Task EphemeralRawEcdhKeyringPutItem ( )
123
123
{
124
124
var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
125
-
125
+
126
126
// 1. Load key pair from UTF-8 encoded PEM files.
127
127
// You may provide your own PEM files to use here.
128
128
// If you do not, the RawEcdhKeyringExamples method in this class will generate PEM
@@ -138,7 +138,7 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
138
138
{
139
139
throw new IOException ( "Exception while reading public key from file" , e ) ;
140
140
}
141
-
141
+
142
142
// 2. Create the keyring.
143
143
// The DynamoDb encryption client uses this to encrypt and decrypt items.
144
144
var keyringInput = new CreateRawEcdhKeyringInput
@@ -161,17 +161,17 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
161
161
} ;
162
162
var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
163
163
var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
164
-
164
+
165
165
// A raw ecdh keyring with Ephemeral configuration cannot decrypt data since the key pair
166
166
// used as the sender is ephemeral. This means that at decrypt time it does not have
167
167
// the private key that corresponds to the public key that is stored on the message.
168
168
await PutExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
169
169
}
170
-
170
+
171
171
private static async Task DiscoveryRawEcdhKeyringGetItem ( )
172
172
{
173
173
var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
174
-
174
+
175
175
// 1. Load key pair from UTF-8 encoded PEM files.
176
176
// You may provide your own PEM files to use here.
177
177
// If you do not, the main method in this class will generate PEM
@@ -186,7 +186,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
186
186
{
187
187
throw new IOException ( "Exception while reading private key from file" , e ) ;
188
188
}
189
-
189
+
190
190
// 2. Create the keyring.
191
191
// The DynamoDb encryption client uses this to encrypt and decrypt items.
192
192
var keyringInput = new CreateRawEcdhKeyringInput
@@ -210,7 +210,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
210
210
} ;
211
211
var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
212
212
var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
213
-
213
+
214
214
await GetExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
215
215
}
216
216
@@ -403,7 +403,7 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
403
403
404
404
// Demonstrate that PutItem succeeded
405
405
Debug . Assert ( putResponse . HttpStatusCode == HttpStatusCode . OK ) ;
406
-
406
+
407
407
// 8. Try to get the item and assert that the ephemeral keyring configuration
408
408
// cannot decrypt data.
409
409
var keyToGet = new Dictionary < String , AttributeValue >
@@ -426,9 +426,9 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
426
426
{
427
427
Debug . Assert ( e . Message . Contains ( "EphemeralPrivateKeyToStaticPublicKey Key Agreement Scheme is forbidden on decrypt." ) ) ;
428
428
}
429
-
429
+
430
430
}
431
-
431
+
432
432
private static async Task GetExampleWithKeyring ( IKeyring rawEcdhKeyring , string ddbTableName )
433
433
{
434
434
// 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
515
515
{
516
516
Debug . Assert ( e . Message . Contains ( "PublicKeyDiscovery Key Agreement Scheme is forbidden on encrypt." ) ) ;
517
517
}
518
-
518
+
519
519
// 8. Get the item back from our table using the same client.
520
520
// The client will decrypt the item client-side, and return
521
521
// back the original item.
@@ -550,7 +550,7 @@ public static async Task RawEcdhKeyringExamples()
550
550
{
551
551
GenerateEccKeyPairs ( ) ;
552
552
}
553
-
553
+
554
554
await RawEcdhKeyringExampleGetItemPutItem ( ) ;
555
555
await EphemeralRawEcdhKeyringPutItem ( ) ;
556
556
await DiscoveryRawEcdhKeyringGetItem ( ) ;
@@ -560,44 +560,44 @@ public static async Task RawEcdhKeyringExamples()
560
560
private static bool ShouldGenerateNewEccKeys ( )
561
561
{
562
562
// 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 )
564
564
&& File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
565
565
&& File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
566
566
{
567
567
return false ;
568
568
}
569
569
570
570
// 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 )
572
572
&& File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
573
573
&& File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
574
574
{
575
575
throw new ApplicationException ( "Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER ) ;
576
576
}
577
-
577
+
578
578
// 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 )
580
580
&& File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
581
581
&& ! File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
582
582
{
583
583
throw new ApplicationException ( "Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) ;
584
584
}
585
-
585
+
586
586
// 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 )
588
588
&& ! File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
589
589
&& File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
590
590
{
591
591
throw new ApplicationException ( "Missing public key file at: " + EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT ) ;
592
592
}
593
-
593
+
594
594
return true ;
595
595
}
596
-
596
+
597
597
private static void GenerateEccKeyPairs ( )
598
598
{
599
599
// 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 )
601
601
|| File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
602
602
|| File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
603
603
{
@@ -620,7 +620,7 @@ private static void GenerateEccKeyPairs()
620
620
generator = new ECKeyPairGenerator ( ) ;
621
621
SecureRandom rng = new SecureRandom ( ) ;
622
622
X9ECParameters p = ECNamedCurveTable . GetByName ( "secp256r1" ) ;
623
-
623
+
624
624
var domainParameters = new ECDomainParameters ( p . Curve , p . G , p . N , p . H ) ;
625
625
generator . Init ( new ECKeyGenerationParameters ( domainParameters , rng ) ) ;
626
626
@@ -630,10 +630,10 @@ private static void GenerateEccKeyPairs()
630
630
Console . WriteLine ( e ) ;
631
631
throw ;
632
632
}
633
-
633
+
634
634
AsymmetricCipherKeyPair senderKeyPair = generator . GenerateKeyPair ( ) ;
635
635
AsymmetricCipherKeyPair recipientKeyPair = generator . GenerateKeyPair ( ) ;
636
-
636
+
637
637
WritePrivateKey ( senderKeyPair . Private , EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER ) ;
638
638
WritePrivateKey ( recipientKeyPair . Private , EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) ;
639
639
WritePublicKey ( recipientKeyPair , "secp256r1" , EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT ) ;
@@ -650,19 +650,19 @@ private static void WritePrivateKey(AsymmetricKeyParameter privateKey, string fi
650
650
fc . Write ( privateKeyUtf8EncodedBytes ) ;
651
651
fc . Close ( ) ;
652
652
}
653
-
653
+
654
654
private static void WritePublicKey ( AsymmetricCipherKeyPair publicKey , string curveName , string fileName )
655
655
{
656
656
var ecdhCurveSpecFromCurveName = ToEcdhCurveSpec ( curveName ) ;
657
657
var spki = KeyGeneration . SerializePublicKey ( publicKey , ecdhCurveSpecFromCurveName ) . CloneAsArray ( ) ;
658
-
658
+
659
659
var publicKeyStringWriter = new StringWriter ( ) ;
660
660
var publicKeyPemWriter = new PemWriter ( publicKeyStringWriter ) ;
661
661
publicKeyPemWriter . WriteObject ( new PemObject ( "PUBLIC KEY" , spki ) ) ;
662
662
var publicKeyUtf8EncodedBytes = Encoding . UTF8 . GetBytes ( publicKeyStringWriter . ToString ( ) ) ;
663
663
var fc = new FileStream ( fileName , FileMode . Create , FileAccess . Write ) ;
664
664
fc . Write ( publicKeyUtf8EncodedBytes ) ;
665
- fc . Close ( ) ;
665
+ fc . Close ( ) ;
666
666
}
667
667
668
668
private static _IECDHCurveSpec ToEcdhCurveSpec ( string curveName )
0 commit comments