Skip to content

Throw a more useful exception when trying to use the RawRsaKeyring to encrypt without a public key #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package com.amazonaws.encryptionsdk.keyrings;

import com.amazonaws.encryptionsdk.EncryptedDataKey;
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
import com.amazonaws.encryptionsdk.internal.JceKeyCipher;
import com.amazonaws.encryptionsdk.keyrings.RawRsaKeyringBuilder.RsaPaddingScheme;
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;

import java.security.PrivateKey;
import java.security.PublicKey;
Expand All @@ -29,8 +31,20 @@
*/
class RawRsaKeyring extends RawKeyring {

private final boolean validToEncrypt;

RawRsaKeyring(String keyNamespace, String keyName, PublicKey publicKey, PrivateKey privateKey, RsaPaddingScheme rsaPaddingScheme) {
super(keyNamespace, keyName, JceKeyCipher.rsa(publicKey, privateKey, rsaPaddingScheme.getTransformation()));
validToEncrypt = publicKey != null;
}

@Override
public EncryptionMaterials onEncrypt(EncryptionMaterials encryptionMaterials) {
if(!validToEncrypt) {
throw new AwsCryptoException("A public key is required to encrypt");
}

return super.onEncrypt(encryptionMaterials);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.amazonaws.encryptionsdk.keyrings;

import com.amazonaws.encryptionsdk.EncryptedDataKey;
import com.amazonaws.encryptionsdk.exception.AwsCryptoException;
import com.amazonaws.encryptionsdk.keyrings.RawRsaKeyringBuilder.RsaPaddingScheme;
import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
Expand All @@ -34,6 +35,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

@Test
void testEncryptWithNoPublicKey() throws Exception {
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
final KeyPair keyPair = keyPairGenerator.generateKeyPair();

Keyring noPublicKey = new RawRsaKeyring(KEYNAMESPACE, KEYNAME, null, keyPair.getPrivate(), PADDING_SCHEME);

EncryptionMaterials encryptionMaterials = EncryptionMaterials.newBuilder()
.setAlgorithm(ALGORITHM)
.setCleartextDataKey(DATA_KEY)
.setEncryptionContext(ENCRYPTION_CONTEXT)
.build();

assertThrows(AwsCryptoException.class, () -> noPublicKey.onEncrypt(encryptionMaterials));
}

@Test
void testDecryptWithNoPrivateKey() throws Exception {
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
final KeyPair keyPair = keyPairGenerator.generateKeyPair();

Keyring noPrivateKey = new RawRsaKeyring(KEYNAMESPACE, KEYNAME, keyPair.getPublic(), null, PADDING_SCHEME);

EncryptionMaterials encryptionMaterials = EncryptionMaterials.newBuilder()
.setAlgorithm(ALGORITHM)
.setCleartextDataKey(DATA_KEY)
.setEncryptionContext(ENCRYPTION_CONTEXT)
.build();

encryptionMaterials = noPrivateKey.onEncrypt(encryptionMaterials);

DecryptionMaterials decryptionMaterials = DecryptionMaterials.newBuilder()
.setAlgorithm(ALGORITHM)
.setEncryptionContext(ENCRYPTION_CONTEXT)
.build();

DecryptionMaterials resultDecryptionMaterials = noPrivateKey.onDecrypt(decryptionMaterials, encryptionMaterials.getEncryptedDataKeys());

assertEquals(decryptionMaterials, resultDecryptionMaterials);
}

}