Skip to content
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

Use proper try-with-resources for resources to prevent DoS attacks #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 22 additions & 20 deletions src/main/java/com/vexsoftware/votifier/crypto/RSAIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ public static void save(File directory, KeyPair keyPair) throws Exception {
// Store the public key.
X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(
publicKey.getEncoded());
FileOutputStream out = new FileOutputStream(directory + "/public.key");
out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded())
.getBytes());
out.close();
try (FileOutputStream out = new FileOutputStream(directory + "/public.key")) {
out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded())
.getBytes());
}

// Store the private key.
PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(
privateKey.getEncoded());
out = new FileOutputStream(directory + "/private.key");
out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded())
.getBytes());
out.close();
try (FileOutputStream out = new FileOutputStream(directory + "/private.key")) {
out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded())
.getBytes());
}
}

/**
Expand All @@ -81,21 +81,23 @@ public static void save(File directory, KeyPair keyPair) throws Exception {
public static KeyPair load(File directory) throws Exception {
// Read the public key file.
File publicKeyFile = new File(directory + "/public.key");
FileInputStream in = new FileInputStream(directory + "/public.key");
byte[] encodedPublicKey = new byte[(int) publicKeyFile.length()];
in.read(encodedPublicKey);
encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(
encodedPublicKey));
in.close();
byte[] encodedPublicKey;
try (FileInputStream in = new FileInputStream(publicKeyFile)) {
encodedPublicKey = new byte[(int) publicKeyFile.length()];
in.read(encodedPublicKey);
encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(
encodedPublicKey));
}

// Read the private key file.
File privateKeyFile = new File(directory + "/private.key");
in = new FileInputStream(directory + "/private.key");
byte[] encodedPrivateKey = new byte[(int) privateKeyFile.length()];
in.read(encodedPrivateKey);
encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(
encodedPrivateKey));
in.close();
byte[] encodedPrivateKey;
try (FileInputStream in = new FileInputStream(privateKeyFile)) {
encodedPrivateKey = new byte[(int) privateKeyFile.length()];
in.read(encodedPrivateKey);
encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(
encodedPrivateKey));
}

// Instantiate and return the key pair.
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/vexsoftware/votifier/net/VoteReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public void run() {

// Main loop.
while (running) {
try {
Socket socket = server.accept();
try (Socket socket = server.accept()) {
socket.setSoTimeout(5000); // Don't hang on slow connections.
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
Expand Down