From 8a8436f73d4c4edd10b43313b724f32ccb7b957e Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Wed, 20 Jun 2012 13:56:57 -0400 Subject: Fixes for Coverity issues of type Resource Leaks - Remaining --- .../src/com/netscape/cmstools/CRMFPopClient.java | 11 +- .../src/com/netscape/cmstools/ExtJoiner.java | 10 +- .../src/com/netscape/cmstools/HttpClient.java | 176 ++++++++++++--------- .../src/com/netscape/cmstools/OCSPClient.java | 159 +++++++++++-------- .../src/com/netscape/cmstools/PasswordCache.java | 24 ++- 5 files changed, 228 insertions(+), 152 deletions(-) (limited to 'base/java-tools') diff --git a/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java b/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java index 1ecaa6bd5..5c4110b1d 100644 --- a/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java +++ b/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java @@ -264,14 +264,23 @@ public class CRMFPopClient { boolean foundTransport = false; String transportCert = null; + BufferedReader br = null; try { - BufferedReader br = new BufferedReader(new FileReader("./transport.txt")); + br = new BufferedReader(new FileReader("./transport.txt")); transportCert = br.readLine(); foundTransport = true; } catch (Exception e) { System.out.println("ERROR: cannot find ./transport.txt, so no key archival"); return; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } try { diff --git a/base/java-tools/src/com/netscape/cmstools/ExtJoiner.java b/base/java-tools/src/com/netscape/cmstools/ExtJoiner.java index 95c3162f4..5aac69e60 100644 --- a/base/java-tools/src/com/netscape/cmstools/ExtJoiner.java +++ b/base/java-tools/src/com/netscape/cmstools/ExtJoiner.java @@ -20,11 +20,11 @@ package com.netscape.cmstools; import java.io.FileInputStream; import java.io.IOException; -import com.netscape.cmsutil.util.Utils; - import netscape.security.util.DerOutputStream; import netscape.security.util.DerValue; +import com.netscape.cmsutil.util.Utils; + /** * This program joins a sequence of extensions together * so that the final output can be used in configuration @@ -93,11 +93,13 @@ public class ExtJoiner { throws IOException { FileInputStream fis = new FileInputStream(fileName); - byte data[] = new byte[fis.available()]; + byte data[] = null; try { + data = new byte[fis.available()]; fis.read(data); } finally { - fis.close(); + if (fis != null) + fis.close(); } return Utils.base64decode(new String(data)); } diff --git a/base/java-tools/src/com/netscape/cmstools/HttpClient.java b/base/java-tools/src/com/netscape/cmstools/HttpClient.java index 2188737a3..a3e27c70e 100644 --- a/base/java-tools/src/com/netscape/cmstools/HttpClient.java +++ b/base/java-tools/src/com/netscape/cmstools/HttpClient.java @@ -74,7 +74,7 @@ public class HttpClient { public static byte[] getBytesFromFile(String filename) throws IOException { File file = new File(filename); - FileInputStream is = new FileInputStream(file); + FileInputStream is = null; long length = file.length(); @@ -87,57 +87,64 @@ public class HttpClient { int offset = 0; int numRead = 0; - while (offset < bytes.length - && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { - offset += numRead; + try { + is = new FileInputStream(file); + while (offset < bytes.length + && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { + offset += numRead; + } + } finally { + if (is != null) { + is.close(); + } } - if (offset < bytes.length) { throw new IOException("Could not completely read file " + filename); } - - is.close(); return bytes; } public void send(String ifilename, String ofilename, String dbdir, String nickname, String password, String servlet, String clientmode) throws Exception { - byte[] b = getBytesFromFile(ifilename); - - System.out.println("Total number of bytes read = " + b.length); - DataOutputStream dos = null; InputStream is = null; - if (_secure) { - try { + PrintStream ps = null; + ByteArrayOutputStream bs = null; + SSLSocket sslSocket = null; + Socket socket = null; + try { + byte[] b = getBytesFromFile(ifilename); + + System.out.println("Total number of bytes read = " + b.length); + if (_secure) { CryptoManager.InitializationValues vals = new CryptoManager.InitializationValues(dbdir, "", "", "secmod.db"); CryptoManager.initialize(vals); - SSLSocket socket = new SSLSocket(_host, _port); + sslSocket = new SSLSocket(_host, _port); int i; for (i = SSLSocket.SSL2_RC4_128_WITH_MD5; i <= SSLSocket.SSL2_RC2_128_CBC_EXPORT40_WITH_MD5; ++i) { try { - socket.setCipherPreference(i, true); + sslSocket.setCipherPreference(i, true); } catch (SocketException e) { } } //skip SSL_EN_IDEA_128_EDE3_CBC_WITH_MD5 for (i = SSLSocket.SSL2_DES_64_CBC_WITH_MD5; i <= SSLSocket.SSL2_DES_192_EDE3_CBC_WITH_MD5; ++i) { try { - socket.setCipherPreference(i, true); + sslSocket.setCipherPreference(i, true); } catch (SocketException e) { } } for (i = 0; cipherSuites[i] != 0; ++i) { try { - socket.setCipherPreference(cipherSuites[i], true); + sslSocket.setCipherPreference(cipherSuites[i], true); } catch (SocketException e) { } } SSLHandshakeCompletedListener listener = new ClientHandshakeCB(this); - socket.addHandshakeCompletedListener(listener); + sslSocket.addHandshakeCompletedListener(listener); if (clientmode != null && clientmode.equals("true")) { CryptoManager cm = CryptoManager.getInstance(); @@ -149,76 +156,95 @@ public class HttpClient { System.out.println("client cert is null"); else System.out.println("client cert is not null"); - socket.setUseClientMode(true); - socket.setClientCertNickname(nickname); + sslSocket.setUseClientMode(true); + sslSocket.setClientCertNickname(nickname); } - socket.forceHandshake(); + sslSocket.forceHandshake(); + dos = new DataOutputStream(sslSocket.getOutputStream()); + is = sslSocket.getInputStream(); + } else { + socket = new Socket(_host, _port); dos = new DataOutputStream(socket.getOutputStream()); is = socket.getInputStream(); - } catch (Exception e) { - System.out.println("Exception: " + e.toString()); - return; } - } else { - Socket socket = new Socket(_host, _port); - dos = new DataOutputStream(socket.getOutputStream()); - is = socket.getInputStream(); - } - // send request - if (servlet == null) { - System.out.println("Missing servlet name."); - printUsage(); - } else { - String s = "POST " + servlet + " HTTP/1.0\r\n"; - dos.writeBytes(s); - } - dos.writeBytes("Content-length: " + b.length + "\r\n"); - dos.writeBytes("\r\n"); - dos.write(b); - dos.flush(); - - FileOutputStream fof = new FileOutputStream(ofilename); - boolean startSaving = false; - int sum = 0; - boolean hack = false; - try { - while (true) { - int r = is.read(); - if (r == -1) - break; - if (r == 10) { - sum++; - } - if (sum == 6) { - startSaving = true; - continue; - } - if (startSaving) { - if (hack) { - fof.write(r); + // send request + if (servlet == null) { + System.out.println("Missing servlet name."); + printUsage(); + } else { + String s = "POST " + servlet + " HTTP/1.0\r\n"; + dos.writeBytes(s); + } + dos.writeBytes("Content-length: " + b.length + "\r\n"); + dos.writeBytes("\r\n"); + dos.write(b); + dos.flush(); + + FileOutputStream fof = new FileOutputStream(ofilename); + boolean startSaving = false; + int sum = 0; + boolean hack = false; + try { + while (true) { + int r = is.read(); + if (r == -1) + break; + if (r == 10) { + sum++; } - if (hack == false) { - hack = true; + if (sum == 6) { + startSaving = true; + continue; + } + if (startSaving) { + if (hack) { + fof.write(r); + } + if (hack == false) { + hack = true; + } } } + } catch (IOException e) { } - } catch (IOException e) { - } - fof.close(); + fof.close(); - byte[] bout = getBytesFromFile(ofilename); - System.out.println("Total number of bytes read = " + bout.length); + byte[] bout = getBytesFromFile(ofilename); + System.out.println("Total number of bytes read = " + bout.length); - ByteArrayOutputStream bs = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(bs); - ps.print(Utils.base64encode(bout)); - System.out.println(bs.toString()); + bs = new ByteArrayOutputStream(); + ps = new PrintStream(bs); + ps.print(Utils.base64encode(bout)); + System.out.println(bs.toString()); - System.out.println(""); - System.out.println("The response in binary format is stored in " + ofilename); - System.out.println(""); + System.out.println(""); + System.out.println("The response in binary format is stored in " + ofilename); + System.out.println(""); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (is != null) { + is.close(); + } + if (dos != null) { + dos.close(); + } + if (bs != null) { + bs.close(); + } + if (ps != null) { + ps.close(); + } + if (sslSocket != null) { + sslSocket.close(); + } + if (socket != null) { + socket.close(); + } + } } static void printUsage() { diff --git a/base/java-tools/src/com/netscape/cmstools/OCSPClient.java b/base/java-tools/src/com/netscape/cmstools/OCSPClient.java index 7d581fdb8..ce0e853cd 100644 --- a/base/java-tools/src/com/netscape/cmstools/OCSPClient.java +++ b/base/java-tools/src/com/netscape/cmstools/OCSPClient.java @@ -115,80 +115,96 @@ public class OCSPClient { } public void sendOCSPRequest(String uri, String host, int port, - byte request_data[], String output) throws Exception { - Socket socket = new Socket(host, port); + byte request_data[], String output) throws Exception { + Socket socket = null; + DataOutputStream dos = null; + InputStream iiss = null; + FileOutputStream fof = null; + BufferedInputStream fis = null; + try { + socket = new Socket(host, port); - // send request - System.out.println("URI: " + uri); + // send request + System.out.println("URI: " + uri); - DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); - dos.writeBytes("POST " + uri + " HTTP/1.0\r\n"); - dos.writeBytes("Content-length: " + request_data.length + "\r\n"); - dos.writeBytes("\r\n"); - dos.write(request_data); - dos.flush(); + dos = new DataOutputStream(socket.getOutputStream()); + dos.writeBytes("POST " + uri + " HTTP/1.0\r\n"); + dos.writeBytes("Content-length: " + request_data.length + "\r\n"); + dos.writeBytes("\r\n"); + dos.write(request_data); + dos.flush(); - System.out.println("Data Length: " + request_data.length); - System.out.println("Data: " + Utils.base64encode(request_data)); + System.out.println("Data Length: " + request_data.length); + System.out.println("Data: " + Utils.base64encode(request_data)); - InputStream iiss = socket.getInputStream(); - FileOutputStream fof = new FileOutputStream(output); - boolean startSaving = false; - int sum = 0; - boolean hack = false; - try { - while (true) { - int r = iiss.read(); - if (r == -1) - break; - if (r == 10) { - sum++; - } - if (sum == 6) { - startSaving = true; - continue; - } - if (startSaving) { - if (hack) { - fof.write(r); + iiss = socket.getInputStream(); + fof = new FileOutputStream(output); + boolean startSaving = false; + int sum = 0; + boolean hack = false; + try { + while (true) { + int r = iiss.read(); + if (r == -1) + break; + if (r == 10) { + sum++; } - if (hack == false) { - hack = true; + if (sum == 6) { + startSaving = true; + continue; } - } - } // while - } catch (IOException e) { - } - fof.close(); - - // parse OCSPResponse - BufferedInputStream fis = - new BufferedInputStream( - new FileInputStream(output)); - OCSPResponse resp = (OCSPResponse) - OCSPResponse.getTemplate().decode(fis); - ResponseBytes bytes = resp.getResponseBytes(); - BasicOCSPResponse basic = (BasicOCSPResponse) - BasicOCSPResponse.getTemplate().decode( - new ByteArrayInputStream(bytes.getResponse().toByteArray())); - ResponseData rd = basic.getResponseData(); - for (int i = 0; i < rd.getResponseCount(); i++) { - SingleResponse rd1 = rd.getResponseAt(i); - if (rd1 == null) { - throw new Exception("No OCSP Response data."); - } - System.out.println("CertID.serialNumber=" + - rd1.getCertID().getSerialNumber()); - CertStatus status1 = rd1.getCertStatus(); - if (status1 instanceof GoodInfo) { - System.out.println("CertStatus=Good"); - } - if (status1 instanceof UnknownInfo) { - System.out.println("CertStatus=Unknown"); + if (startSaving) { + if (hack) { + fof.write(r); + } + if (hack == false) { + hack = true; + } + } + } // while + } catch (IOException e) { } - if (status1 instanceof RevokedInfo) { - System.out.println("CertStatus=Revoked"); + // parse OCSPResponse + fis = new BufferedInputStream( + new FileInputStream(output)); + OCSPResponse resp = (OCSPResponse) + OCSPResponse.getTemplate().decode(fis); + ResponseBytes bytes = resp.getResponseBytes(); + BasicOCSPResponse basic = (BasicOCSPResponse) + BasicOCSPResponse.getTemplate().decode( + new ByteArrayInputStream(bytes.getResponse().toByteArray())); + ResponseData rd = basic.getResponseData(); + for (int i = 0; i < rd.getResponseCount(); i++) { + SingleResponse rd1 = rd.getResponseAt(i); + if (rd1 == null) { + throw new Exception("No OCSP Response data."); + } + System.out.println("CertID.serialNumber=" + + rd1.getCertID().getSerialNumber()); + CertStatus status1 = rd1.getCertStatus(); + if (status1 instanceof GoodInfo) { + System.out.println("CertStatus=Good"); + } + if (status1 instanceof UnknownInfo) { + System.out.println("CertStatus=Unknown"); + } + if (status1 instanceof RevokedInfo) { + System.out.println("CertStatus=Revoked"); + } } + } finally { + if (socket != null) + socket.close(); + if (dos != null) + dos.close(); + if (iiss != null) + iiss.close(); + if (fof != null) + fof.close(); + if (fis != null) + fis.close(); + } } @@ -233,10 +249,11 @@ public class OCSPClient { try { serialno = Integer.parseInt(args[4]); } catch (Exception e) { + FileInputStream fis = null; try { System.out.println("Warning: Serial Number not found. It may be a filename."); /* it could be a file name */ - FileInputStream fis = new FileInputStream(args[4]); + fis = new FileInputStream(args[4]); System.out.println("File Size: " + fis.available()); data = new byte[fis.available()]; fis.read(data); @@ -244,6 +261,14 @@ public class OCSPClient { System.out.println("Error: Invalid Serial Number or File Name"); printUsage(); System.exit(0); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } } } String output = args[5]; diff --git a/base/java-tools/src/com/netscape/cmstools/PasswordCache.java b/base/java-tools/src/com/netscape/cmstools/PasswordCache.java index f5b4e2c2c..dbc52a02a 100644 --- a/base/java-tools/src/com/netscape/cmstools/PasswordCache.java +++ b/base/java-tools/src/com/netscape/cmstools/PasswordCache.java @@ -192,12 +192,21 @@ public class PasswordCache { if (++i >= argv.length) usage(); String keyFile = argv[i]; + BufferedReader r = null; try { - BufferedReader r = new BufferedReader(new FileReader(keyFile)); + r = new BufferedReader(new FileReader(keyFile)); mKeyIdString = r.readLine(); } catch (Exception e) { System.out.println("Error: " + e.toString()); System.exit(1); + } finally { + if (r != null) { + try { + r.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } if (mKeyIdString != null) { @@ -541,13 +550,15 @@ class PWsdrCache { bos.write(readbuf, 0, numRead); totalRead += numRead; } - inputs.close(); } catch (FileNotFoundException e) { System.out.println("Failed for file " + mPWcachedb + " " + e.toString()); throw new IOException(e.toString() + ": " + mPWcachedb); } catch (IOException e) { System.out.println("Failed for file " + mPWcachedb + " " + e.toString()); throw new IOException(e.toString() + ": " + mPWcachedb); + } finally { + if (inputs != null) + inputs.close(); } if (totalRead > 0) { @@ -578,7 +589,7 @@ class PWsdrCache { * encrypts and writes the whole String buf into pwcache.db */ public void writePWcache(String bufs) throws IOException { - + FileOutputStream outstream = null; try { Encryptor sdr = new Encryptor(mToken, mKeyID, Encryptor.DEFAULT_ENCRYPTION_ALG); @@ -600,10 +611,9 @@ class PWsdrCache { // it wasn't removed? tmpPWcache.delete(); } - FileOutputStream outstream = new FileOutputStream(mPWcachedb + ".tmp"); + outstream = new FileOutputStream(mPWcachedb + ".tmp"); outstream.write(writebuf); - outstream.close(); // Make certain that this temporary file has // the correct permissions. @@ -662,6 +672,10 @@ class PWsdrCache { } catch (Exception e) { System.out.println("sdrPWcache: Error " + e.toString()); throw new IOException(e.toString()); + } finally { + if (outstream != null) { + outstream.close(); + } } } -- cgit