Skip to content

Commit c165542

Browse files
jkakavasdanielmitterdorfer
authored andcommitted
Ensure KeyStoreWrapper decryption exceptions are handled (#32472)
* Ensure decryption related exceptions are handled This commit ensures that all possible Exceptions in KeyStoreWrapper#decrypt() are handled. More specifically, in the case that a wrong password is used for secure settings, calling readX on the DataInputStream that wraps the CipherInputStream can throw an IOException. It also adds a test for loading a KeyStoreWrapper with a wrong password. This is a backport of #32464
1 parent 65aae99 commit c165542

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

server/src/main/java/org/elasticsearch/common/settings/KeyStoreWrapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void decrypt(char[] password) throws GeneralSecurityException, IOExceptio
359359
if (input.read() != -1) {
360360
throw new SecurityException("Keystore has been corrupted or tampered with");
361361
}
362-
} catch (EOFException e) {
362+
} catch (IOException e) {
363363
throw new SecurityException("Keystore has been corrupted or tampered with", e);
364364
}
365365
}

server/src/test/java/org/elasticsearch/action/admin/ReloadSecureSettingsIT.java

-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.plugins.ReloadablePlugin;
3333
import org.elasticsearch.test.ESIntegTestCase;
3434

35-
import java.io.IOException;
3635
import java.io.InputStream;
3736
import java.nio.file.Files;
3837
import java.nio.file.StandardCopyOption;
@@ -205,14 +204,7 @@ public void onResponse(NodesReloadSecureSettingsResponse nodesReloadResponse) {
205204
assertThat(nodesMap.size(), equalTo(cluster().size()));
206205
for (final NodesReloadSecureSettingsResponse.NodeResponse nodeResponse : nodesReloadResponse.getNodes()) {
207206
assertThat(nodeResponse.reloadException(), notNullValue());
208-
// Running in a JVM with a BouncyCastle FIPS Security Provider, decrypting the Keystore with the wrong
209-
// password returns a SecurityException if the DataInputStream can't be fully consumed
210-
if (inFipsJvm()) {
211207
assertThat(nodeResponse.reloadException(), instanceOf(SecurityException.class));
212-
} else {
213-
assertThat(nodeResponse.reloadException(), instanceOf(IOException.class));
214-
}
215-
216208
}
217209
} catch (final AssertionError e) {
218210
reloadSettingsError.set(e);

server/src/test/java/org/elasticsearch/common/settings/KeyStoreWrapperTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ public void testCreate() throws Exception {
9999
assertTrue(keystore.getSettingNames().contains(KeyStoreWrapper.SEED_SETTING.getKey()));
100100
}
101101

102+
public void testDecryptKeyStoreWithWrongPassword() throws Exception {
103+
KeyStoreWrapper keystore = KeyStoreWrapper.create();
104+
keystore.save(env.configFile(), new char[0]);
105+
final KeyStoreWrapper loadedkeystore = KeyStoreWrapper.load(env.configFile());
106+
final SecurityException exception = expectThrows(SecurityException.class,
107+
() -> loadedkeystore.decrypt(new char[]{'i', 'n', 'v', 'a', 'l', 'i', 'd'}));
108+
assertThat(exception.getMessage(), containsString("Keystore has been corrupted or tampered with"));
109+
}
110+
102111
public void testCannotReadStringFromClosedKeystore() throws Exception {
103112
KeyStoreWrapper keystore = KeyStoreWrapper.create();
104113
assertThat(keystore.getSettingNames(), Matchers.hasItem(KeyStoreWrapper.SEED_SETTING.getKey()));

0 commit comments

Comments
 (0)