Skip to content
Permalink
Browse files

refactor: Clean up loading of reading of keydata file in LazyKeyManag…

…er (#1372)
  • Loading branch information...
sehrope authored and davecramer committed Dec 11, 2018
1 parent bac4bc1 commit 9b45e709c20623ac1d4233bf1cc0369e8ddbe076
Showing with 13 additions and 14 deletions.
  1. +13 −14 pgjdbc/src/main/java/org/postgresql/ssl/LazyKeyManager.java
@@ -9,7 +9,6 @@
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -160,9 +159,19 @@ public String chooseServerAlias(String keyType, Principal[] issuers, Socket sock
return (alias == null ? new String[]{} : new String[]{alias});
}

private static byte[] readFileFully(String path) throws IOException {
RandomAccessFile raf = new RandomAccessFile(path, "r");
try {
byte[] ret = new byte[(int) raf.length()];
raf.readFully(ret);
return ret;
} finally {
raf.close();
}
}

@Override
public PrivateKey getPrivateKey(String alias) {
RandomAccessFile raf = null;
try {
if (key == null && keyfile != null) {
// If keyfile is null, we do not load the key
@@ -173,19 +182,16 @@ public PrivateKey getPrivateKey(String alias) {
}
}

byte[] keydata;
try {
raf = new RandomAccessFile(new File(keyfile), "r"); // NOSONAR
keydata = readFileFully(keyfile);
} catch (FileNotFoundException ex) {
if (!defaultfile) {
// It is not an error if there is no file at the default location
throw ex;
}
return null;
}
byte[] keydata = new byte[(int) raf.length()];
raf.readFully(keydata);
raf.close();
raf = null;

KeyFactory kf = KeyFactory.getInstance(cert[0].getPublicKey().getAlgorithm());
try {
@@ -241,13 +247,6 @@ public PrivateKey getPrivateKey(String alias) {
}
}
} catch (IOException ioex) {
if (raf != null) {
try {
raf.close();
} catch (IOException ex) {
}
}

error = new PSQLException(GT.tr("Could not read SSL key file {0}.", keyfile),
PSQLState.CONNECTION_FAILURE, ioex);
} catch (NoSuchAlgorithmException ex) {

0 comments on commit 9b45e70

Please sign in to comment.
You can’t perform that action at this time.