Skip to content

Commit b65226b

Browse files
Moving factory methods to StandardKeyrings and correcting RawAes trace.
1 parent c2bcffe commit b65226b

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

src/main/java/com/amazonaws/encryptionsdk/keyrings/Keyring.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,4 @@ public interface Keyring {
4242
*/
4343
void onDecrypt(DecryptionMaterials decryptionMaterials, List<? extends EncryptedDataKey> encryptedDataKeys);
4444

45-
/**
46-
* Constructs a {@code Keyring} which does local AES-GCM encryption
47-
* decryption of data keys using the provided wrapping key.
48-
*
49-
* @param keyNamespace A UTF-8 encoded value that, together with the key name, identifies the wrapping key.
50-
* @param keyName A UTF-8 encoded value that, together with the key namespace, identifies the wrapping key.
51-
* @param wrappingKey The AES key input to AES-GCM to encrypt plaintext data keys.
52-
* @return The {@link Keyring}
53-
*/
54-
static Keyring rawAes(String keyNamespace, String keyName, SecretKey wrappingKey) {
55-
return new RawAesKeyring(keyNamespace, keyName, wrappingKey);
56-
}
57-
58-
/**
59-
* Constructs a {@code Keyring} which does local RSA encryption and decryption of data keys using the
60-
* provided public and private keys. If {@code privateKey} is {@code null} then the returned {@code Keyring}
61-
* can only be used for encryption.
62-
*
63-
* @param keyNamespace A UTF-8 encoded value that, together with the key name, identifies the wrapping key.
64-
* @param keyName A UTF-8 encoded value that, together with the key namespace, identifies the wrapping key.
65-
* @param publicKey The RSA public key used by this keyring to encrypt data keys.
66-
* @param privateKey The RSA private key used by this keyring to decrypt data keys.
67-
* @param wrappingAlgorithm The RSA algorithm to use with this keyring.
68-
* @return The {@link Keyring}
69-
*/
70-
static Keyring rawRsa(String keyNamespace, String keyName, PublicKey publicKey, PrivateKey privateKey, String wrappingAlgorithm) {
71-
return new RawRsaKeyring(keyNamespace, keyName, publicKey, privateKey, wrappingAlgorithm);
72-
}
7345
}

src/main/java/com/amazonaws/encryptionsdk/keyrings/RawAesKeyring.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* A {@code Keyring} which does local AES-GCM encryption
2424
* decryption of data keys using the provided wrapping key.
2525
*
26-
* Instantiate by using the {@code Keyring.rawAes(...)} factory method.
26+
* Instantiate by using the {@code StandardKeyrings.rawAes(...)} factory method.
2727
*/
2828
class RawAesKeyring extends RawKeyring {
2929

@@ -53,15 +53,13 @@ boolean validToDecrypt(EncryptedDataKey encryptedDataKey) {
5353
void traceOnEncrypt(KeyringTrace keyringTrace) {
5454
keyringTrace.add(keyNamespace, keyName,
5555
KeyringTraceFlag.ENCRYPTED_DATA_KEY,
56-
KeyringTraceFlag.SIGNED_ENCRYPTION_CONTEXT,
57-
KeyringTraceFlag.VERIFIED_ENCRYPTION_CONTEXT);
56+
KeyringTraceFlag.SIGNED_ENCRYPTION_CONTEXT);
5857
}
5958

6059
@Override
6160
void traceOnDecrypt(KeyringTrace keyringTrace) {
6261
keyringTrace.add(keyNamespace, keyName,
6362
KeyringTraceFlag.DECRYPTED_DATA_KEY,
64-
KeyringTraceFlag.SIGNED_ENCRYPTION_CONTEXT,
6563
KeyringTraceFlag.VERIFIED_ENCRYPTION_CONTEXT);
6664
}
6765
}

src/main/java/com/amazonaws/encryptionsdk/keyrings/RawRsaKeyring.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* A {@link Keyring} which does local RSA encryption and decryption of data keys using the
2525
* provided public and private keys.
2626
*
27-
* Instantiate by using the {@code Keyring.rawRsa(...)} factory method.
27+
* Instantiate by using the {@code StandardKeyrings.rawRsa(...)} factory method.
2828
*/
2929
class RawRsaKeyring extends RawKeyring {
3030

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
5+
* in compliance with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.amazonaws.encryptionsdk.keyrings;
15+
16+
import javax.crypto.SecretKey;
17+
import java.security.PrivateKey;
18+
import java.security.PublicKey;
19+
20+
/**
21+
* Factory methods for instantiating the standard {@code Keyring}s provided by the AWS Encryption SDK.
22+
*/
23+
public class StandardKeyrings {
24+
25+
private StandardKeyrings() {
26+
}
27+
28+
/**
29+
* Constructs a {@code Keyring} which does local AES-GCM encryption
30+
* decryption of data keys using the provided wrapping key.
31+
*
32+
* @param keyNamespace A UTF-8 encoded value that, together with the key name, identifies the wrapping key.
33+
* @param keyName A UTF-8 encoded value that, together with the key namespace, identifies the wrapping key.
34+
* @param wrappingKey The AES key input to AES-GCM to encrypt plaintext data keys.
35+
* @return The {@link Keyring}
36+
*/
37+
public static Keyring rawAes(String keyNamespace, String keyName, SecretKey wrappingKey) {
38+
return new RawAesKeyring(keyNamespace, keyName, wrappingKey);
39+
}
40+
41+
/**
42+
* Constructs a {@code Keyring} which does local RSA encryption and decryption of data keys using the
43+
* provided public and private keys. If {@code privateKey} is {@code null} then the returned {@code Keyring}
44+
* can only be used for encryption.
45+
*
46+
* @param keyNamespace A UTF-8 encoded value that, together with the key name, identifies the wrapping key.
47+
* @param keyName A UTF-8 encoded value that, together with the key namespace, identifies the wrapping key.
48+
* @param publicKey The RSA public key used by this keyring to encrypt data keys.
49+
* @param privateKey The RSA private key used by this keyring to decrypt data keys.
50+
* @param wrappingAlgorithm The RSA algorithm to use with this keyring.
51+
* @return The {@link Keyring}
52+
*/
53+
public static Keyring rawRsa(String keyNamespace, String keyName, PublicKey publicKey, PrivateKey privateKey, String wrappingAlgorithm) {
54+
return new RawRsaKeyring(keyNamespace, keyName, publicKey, privateKey, wrappingAlgorithm);
55+
}
56+
}

src/test/java/com/amazonaws/encryptionsdk/keyrings/RawAesKeyringTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public void testTraceOnEncrypt() {
6161
assertEquals(1, trace.getEntries().size());
6262
assertEquals(KEYNAME, trace.getEntries().get(0).getKeyName());
6363
assertEquals(KEYNAMESPACE, trace.getEntries().get(0).getKeyNamespace());
64-
assertEquals(3, trace.getEntries().get(0).getFlags().size());
64+
assertEquals(2, trace.getEntries().get(0).getFlags().size());
6565
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.ENCRYPTED_DATA_KEY));
6666
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.SIGNED_ENCRYPTION_CONTEXT));
67-
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.VERIFIED_ENCRYPTION_CONTEXT));
6867
}
6968

7069
@Test
@@ -77,9 +76,8 @@ public void testTraceOnDecrypt() {
7776
assertEquals(1, trace.getEntries().size());
7877
assertEquals(KEYNAME, trace.getEntries().get(0).getKeyName());
7978
assertEquals(KEYNAMESPACE, trace.getEntries().get(0).getKeyNamespace());
80-
assertEquals(3, trace.getEntries().get(0).getFlags().size());
79+
assertEquals(2, trace.getEntries().get(0).getFlags().size());
8180
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.DECRYPTED_DATA_KEY));
82-
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.SIGNED_ENCRYPTION_CONTEXT));
8381
assertTrue(trace.getEntries().get(0).getFlags().contains(KeyringTraceFlag.VERIFIED_ENCRYPTION_CONTEXT));
8482
}
8583

0 commit comments

Comments
 (0)