|
14 | 14 | package com.amazonaws.encryptionsdk.keyrings;
|
15 | 15 |
|
16 | 16 | import com.amazonaws.encryptionsdk.EncryptedDataKey;
|
| 17 | +import com.amazonaws.encryptionsdk.exception.AwsCryptoException; |
17 | 18 | import com.amazonaws.encryptionsdk.keyrings.RawRsaKeyringBuilder.RsaPaddingScheme;
|
18 | 19 | import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
|
19 | 20 | import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
|
|
34 | 35 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
35 | 36 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
36 | 37 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
| 38 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
37 | 39 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
38 | 40 |
|
39 | 41 | class RawRsaKeyringTest {
|
@@ -134,4 +136,47 @@ void testEncryptDecryptGenerateDataKey() {
|
134 | 136 | assertTrue(decryptionMaterials.getKeyringTrace().getEntries().get(0).getFlags().contains(KeyringTraceFlag.DECRYPTED_DATA_KEY));
|
135 | 137 | }
|
136 | 138 |
|
| 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 | + |
137 | 182 | }
|
0 commit comments