Skip to content

Commit 997e80b

Browse files
Throw a more useful exception when trying to use the RawRsaKeyring to encrypt without a public key (#180)
* Throw a more useful exception when trying to use the RawRsaKeyring to encrypt without a public key * Add a test for trying to decrypt without a private key
1 parent d2c379c commit 997e80b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package com.amazonaws.encryptionsdk.keyrings;
1515

1616
import com.amazonaws.encryptionsdk.EncryptedDataKey;
17+
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
1718
import com.amazonaws.encryptionsdk.internal.JceKeyCipher;
1819
import com.amazonaws.encryptionsdk.keyrings.RawRsaKeyringBuilder.RsaPaddingScheme;
20+
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
1921

2022
import java.security.PrivateKey;
2123
import java.security.PublicKey;
@@ -29,8 +31,20 @@
2931
*/
3032
class RawRsaKeyring extends RawKeyring {
3133

34+
private final boolean validToEncrypt;
35+
3236
RawRsaKeyring(String keyNamespace, String keyName, PublicKey publicKey, PrivateKey privateKey, RsaPaddingScheme rsaPaddingScheme) {
3337
super(keyNamespace, keyName, JceKeyCipher.rsa(publicKey, privateKey, rsaPaddingScheme.getTransformation()));
38+
validToEncrypt = publicKey != null;
39+
}
40+
41+
@Override
42+
public EncryptionMaterials onEncrypt(EncryptionMaterials encryptionMaterials) {
43+
if(!validToEncrypt) {
44+
throw new AwsCryptoException("A public key is required to encrypt");
45+
}
46+
47+
return super.onEncrypt(encryptionMaterials);
3448
}
3549

3650
@Override

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

+45
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.amazonaws.encryptionsdk.keyrings;
1515

1616
import com.amazonaws.encryptionsdk.EncryptedDataKey;
17+
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
1718
import com.amazonaws.encryptionsdk.keyrings.RawRsaKeyringBuilder.RsaPaddingScheme;
1819
import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
1920
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
@@ -34,6 +35,7 @@
3435
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3536
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3739
import static org.junit.jupiter.api.Assertions.assertTrue;
3840

3941
class RawRsaKeyringTest {
@@ -134,4 +136,47 @@ void testEncryptDecryptGenerateDataKey() {
134136
assertTrue(decryptionMaterials.getKeyringTrace().getEntries().get(0).getFlags().contains(KeyringTraceFlag.DECRYPTED_DATA_KEY));
135137
}
136138

139+
@Test
140+
void testEncryptWithNoPublicKey() throws Exception {
141+
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
142+
keyPairGenerator.initialize(2048);
143+
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
144+
145+
Keyring noPublicKey = new RawRsaKeyring(KEYNAMESPACE, KEYNAME, null, keyPair.getPrivate(), PADDING_SCHEME);
146+
147+
EncryptionMaterials encryptionMaterials = EncryptionMaterials.newBuilder()
148+
.setAlgorithm(ALGORITHM)
149+
.setCleartextDataKey(DATA_KEY)
150+
.setEncryptionContext(ENCRYPTION_CONTEXT)
151+
.build();
152+
153+
assertThrows(AwsCryptoException.class, () -> noPublicKey.onEncrypt(encryptionMaterials));
154+
}
155+
156+
@Test
157+
void testDecryptWithNoPrivateKey() throws Exception {
158+
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
159+
keyPairGenerator.initialize(2048);
160+
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
161+
162+
Keyring noPrivateKey = new RawRsaKeyring(KEYNAMESPACE, KEYNAME, keyPair.getPublic(), null, PADDING_SCHEME);
163+
164+
EncryptionMaterials encryptionMaterials = EncryptionMaterials.newBuilder()
165+
.setAlgorithm(ALGORITHM)
166+
.setCleartextDataKey(DATA_KEY)
167+
.setEncryptionContext(ENCRYPTION_CONTEXT)
168+
.build();
169+
170+
encryptionMaterials = noPrivateKey.onEncrypt(encryptionMaterials);
171+
172+
DecryptionMaterials decryptionMaterials = DecryptionMaterials.newBuilder()
173+
.setAlgorithm(ALGORITHM)
174+
.setEncryptionContext(ENCRYPTION_CONTEXT)
175+
.build();
176+
177+
DecryptionMaterials resultDecryptionMaterials = noPrivateKey.onDecrypt(decryptionMaterials, encryptionMaterials.getEncryptedDataKeys());
178+
179+
assertEquals(decryptionMaterials, resultDecryptionMaterials);
180+
}
181+
137182
}

0 commit comments

Comments
 (0)