Skip to content

Commit acbaa2d

Browse files
authored
Make JceMasterKey Case Insensitive (#62)
* Make JceMasterKey Case Insensitive Algorithm names in the JCA spec are not case-sensitive. This makes JceMasterKey algorithm names case insensitive. Adds a test for JceMasterKey getInstance to ensure that the method is case insensitive.
1 parent 51487aa commit acbaa2d

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ target/
44
.project
55
.classpath
66
/bin/
7+
.idea/

src/main/java/com/amazonaws/encryptionsdk/jce/JceMasterKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public abstract class JceMasterKey extends MasterKey<JceMasterKey> {
7575
*/
7676
public static JceMasterKey getInstance(final SecretKey key, final String provider, final String keyId,
7777
final String wrappingAlgorithm) {
78-
switch (wrappingAlgorithm) {
79-
case "AES/GCM/NoPadding":
78+
switch (wrappingAlgorithm.toUpperCase()) {
79+
case "AES/GCM/NOPADDING":
8080
return new AesGcm(key, provider, keyId);
8181
default:
8282
throw new IllegalArgumentException("Right now only AES/GCM/NoPadding is supported");

src/test/java/com/amazonaws/encryptionsdk/AllTestsSuite.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.amazonaws.encryptionsdk;
22

3+
import com.amazonaws.encryptionsdk.jce.JceMasterKeyTest;
34
import org.junit.runner.RunWith;
45
import org.junit.runners.Suite;
56

@@ -52,7 +53,8 @@
5253
LocalCryptoMaterialsCacheThreadStormTest.class,
5354
UtilsTest.class,
5455
MultipleMasterKeyTest.class,
55-
KMSProviderBuilderMockTests.class
56+
KMSProviderBuilderMockTests.class,
57+
JceMasterKeyTest.class
5658
})
5759
public class AllTestsSuite {
5860
}

src/test/java/com/amazonaws/encryptionsdk/internal/StaticMasterKey.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class StaticMasterKey extends MasterKey<StaticMasterKey> {
5555
/**
5656
* Encryption algorithm for the randomly generated data key
5757
*/
58-
private static final String DATA_KEY_ENCRHYPTION_ALGORITHM = "AES";
58+
private static final String DATA_KEY_ENCRYPTION_ALGORITHM = "AES";
5959

6060
/**
6161
* The ID of the master key
@@ -104,7 +104,7 @@ public StaticMasterKey(@Nonnull final String keyId) {
104104

105105
masterKeyEncryptionCipher_ = Cipher.getInstance(MASTER_KEY_ENCRYPTION_ALGORITHM);
106106
masterKeyEncryptionCipher_.init(Cipher.ENCRYPT_MODE, pubKey);
107-
107+
108108
masterKeyDecryptionCipher_ = Cipher.getInstance(MASTER_KEY_ENCRYPTION_ALGORITHM);
109109
masterKeyDecryptionCipher_.init(Cipher.DECRYPT_MODE, privKey);
110110

@@ -135,7 +135,7 @@ public String getKeyId() {
135135
public DataKey<StaticMasterKey> generateDataKey(CryptoAlgorithm algorithm,
136136
Map<String, String> encryptionContext) {
137137
try {
138-
this.keyGenerator_ = KeyGenerator.getInstance(DATA_KEY_ENCRHYPTION_ALGORITHM);
138+
this.keyGenerator_ = KeyGenerator.getInstance(DATA_KEY_ENCRYPTION_ALGORITHM);
139139
this.keyGenerator_.init(algorithm.getDataKeyLength() * 8, SRAND);
140140
SecretKey key = new SecretKeySpec(keyGenerator_.generateKey().getEncoded(), algorithm.getDataKeyAlgo());
141141
byte[] encryptedKey = masterKeyEncryptionCipher_.doFinal(key.getEncoded());
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.amazonaws.encryptionsdk.jce;
2+
3+
import org.junit.Test;
4+
5+
import javax.crypto.SecretKey;
6+
import javax.crypto.spec.SecretKeySpec;
7+
import java.security.*;
8+
9+
public class JceMasterKeyTest {
10+
11+
private static final SecretKey SECRET_KEY = new SecretKeySpec(new byte[1], "AES");
12+
private static final PrivateKey PRIVATE_KEY;
13+
private static final PublicKey PUBLIC_KEY;
14+
15+
static {
16+
try {
17+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
18+
KeyPair keyPair = keyPairGenerator.generateKeyPair();
19+
PUBLIC_KEY = keyPair.getPublic();
20+
PRIVATE_KEY = keyPair.getPrivate();
21+
} catch (NoSuchAlgorithmException e) {
22+
throw new RuntimeException(e);
23+
}
24+
}
25+
26+
private JceMasterKey jceGetInstance(final String algorithmName) {
27+
return JceMasterKey.getInstance(SECRET_KEY, "mockProvider", "mockKey", algorithmName);
28+
}
29+
30+
private JceMasterKey jceGetInstanceAsymmetric(final String algorithmName) {
31+
return JceMasterKey.getInstance(PUBLIC_KEY, PRIVATE_KEY, "mockProvider", "mockKey",
32+
algorithmName);
33+
}
34+
35+
@Test(expected = IllegalArgumentException.class)
36+
public void testGetInstanceInvalidWrappingAlgorithm() {
37+
jceGetInstance("blatently/unsupported/algorithm");
38+
}
39+
40+
41+
@Test(expected = UnsupportedOperationException.class)
42+
public void testGetInstanceAsymmetricInvalidWrappingAlgorithm() {
43+
jceGetInstanceAsymmetric("rsa/ec/unsupportedAlgorithm");
44+
}
45+
46+
/**
47+
* Calls JceMasterKey.getInstance with differently cased wrappingAlgorithm names.
48+
* Passes if no Exception is thrown.
49+
* Relies on passing an invalid algorithm name to result in an Exception.
50+
*/
51+
@Test
52+
public void testGetInstanceAllLowercase() {
53+
jceGetInstance("aes/gcm/nopadding");
54+
}
55+
56+
@Test
57+
public void testGetInstanceMixedCasing() {
58+
jceGetInstance("AES/GCm/NOpadding");
59+
}
60+
61+
@Test
62+
public void testGetInstanceAsymmetricAllLowercase() {
63+
jceGetInstanceAsymmetric("rsa/ecb/oaepwithsha-256andmgf1padding");
64+
}
65+
66+
@Test
67+
public void testGetInstanceAsymmetricMixedCasing() {
68+
jceGetInstanceAsymmetric("RSA/ECB/OAepwithsha-256andmgf1padding");
69+
}
70+
}

0 commit comments

Comments
 (0)