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/ca/CertificateAuthority.java | 12 +- .../cms/publish/publishers/FileBasedPublisher.java | 18 ++- .../src/com/netscape/cmscore/cert/CertUtils.java | 35 ++-- .../cmscore/ldapconn/LdapJssSSLSocketFactory.java | 7 + .../com/netscape/cmscore/security/PWsdrCache.java | 23 ++- .../com/netscape/cmscore/util/FileAsString.java | 23 +-- .../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 ++- base/kra/src/com/netscape/kra/StorageKeyUnit.java | 13 +- .../cmsutil/password/PlainPasswordFile.java | 25 ++- .../cmsutil/password/PlainPasswordReader.java | 12 +- .../cmsutil/password/PlainPasswordWriter.java | 24 ++- base/util/src/com/netscape/cmsutil/util/Utils.java | 27 +++- .../security/extensions/GenericASN1Extension.java | 52 +++--- .../extensions/PresenceServerExtension.java | 84 ++++++---- .../security/x509/CRLDistributionPoint.java | 11 +- .../x509/CRLDistributionPointsExtension.java | 14 +- .../security/x509/FreshestCRLExtension.java | 14 +- .../security/x509/IssuingDistributionPoint.java | 12 +- .../x509/IssuingDistributionPointExtension.java | 13 +- base/util/src/netscape/security/x509/OIDMap.java | 23 ++- 24 files changed, 537 insertions(+), 285 deletions(-) (limited to 'base') diff --git a/base/ca/src/com/netscape/ca/CertificateAuthority.java b/base/ca/src/com/netscape/ca/CertificateAuthority.java index 321fd9410..feecec6a8 100644 --- a/base/ca/src/com/netscape/ca/CertificateAuthority.java +++ b/base/ca/src/com/netscape/ca/CertificateAuthority.java @@ -1328,10 +1328,14 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori File file = new File(path); Long l = Long.valueOf(file.length()); byte[] b = new byte[l.intValue()]; - FileInputStream in = new FileInputStream(path); - in.read(b); - in.close(); - + FileInputStream in = null; + try { + in = new FileInputStream(path); + in.read(b); + } finally { + if (in != null) + in.close(); + } return b; } diff --git a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java index c41025b54..59effbe81 100644 --- a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java +++ b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java @@ -317,7 +317,7 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { String baseName = mDir + File.separator + namePrefix[0]; String tempFile = baseName + ".temp"; FileOutputStream fos; - ZipOutputStream zos; + ZipOutputStream zos = null; byte[] encodedArray = null; File destFile = null; String destName = null; @@ -329,12 +329,16 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { fos.write(encodedArray); fos.close(); if (mZipCRL) { - zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); - zos.setLevel(mZipLevel); - zos.putNextEntry(new ZipEntry(baseName + ".der")); - zos.write(encodedArray, 0, encodedArray.length); - zos.closeEntry(); - zos.close(); + try { + zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); + zos.setLevel(mZipLevel); + zos.putNextEntry(new ZipEntry(baseName + ".der")); + zos.write(encodedArray, 0, encodedArray.length); + zos.closeEntry(); + } finally { + if (zos != null) + zos.close(); + } } destName = baseName + ".der"; destFile = new File(destName); diff --git a/base/common/src/com/netscape/cmscore/cert/CertUtils.java b/base/common/src/com/netscape/cmscore/cert/CertUtils.java index d443781ae..ee1e1568c 100644 --- a/base/common/src/com/netscape/cmscore/cert/CertUtils.java +++ b/base/common/src/com/netscape/cmscore/cert/CertUtils.java @@ -525,23 +525,34 @@ public class CertUtils { public static byte[] readFromFile(String fileName) throws IOException { - FileInputStream fin = new FileInputStream(fileName); - int available = fin.available(); - byte[] ba = new byte[available]; - int nRead = fin.read(ba); - - if (nRead != available) - throw new IOException("Error reading data from file: " + fileName); - fin.close(); - return ba; + FileInputStream fin = null; + try { + fin = new FileInputStream(fileName); + int available = fin.available(); + byte[] ba = new byte[available]; + int nRead = fin.read(ba); + + if (nRead != available) + throw new IOException("Error reading data from file: " + fileName); + + return ba; + } finally { + if (fin != null) + fin.close(); + } } public static void storeInFile(String fileName, byte[] ba) throws IOException { - FileOutputStream fout = new FileOutputStream(fileName); + FileOutputStream fout = null; + try { + fout = new FileOutputStream(fileName); - fout.write(ba); - fout.close(); + fout.write(ba); + } finally { + if (fout != null) + fout.close(); + } } public static String toMIME64(X509CertImpl cert) { diff --git a/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java b/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java index bad9e3b23..38651d0ba 100644 --- a/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java +++ b/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java @@ -78,6 +78,13 @@ public class LdapJssSSLSocketFactory implements LDAPSSLSocketFactoryExt { throw new LDAPException( "Cannot Create JSS SSL Socket - Unknown host"); } catch (IOException e) { + if (s != null) { + try { + s.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_LDAPCONN_IO_ERROR", e.toString())); throw new LDAPException("IO Error creating JSS SSL Socket"); } diff --git a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java index 17cc9f0a7..b0445bd6d 100644 --- a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java +++ b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java @@ -325,13 +325,21 @@ public class PWsdrCache { bos.write(readbuf, 0, numRead); totalRead += numRead; } - inputs.close(); + } catch (FileNotFoundException e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); } catch (IOException e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); + } finally { + if (inputs != null) { + try { + inputs.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } if (totalRead > 0) { @@ -354,6 +362,7 @@ public class PWsdrCache { * encrypts and writes the whole String buf into pwcache.db */ public void writePWcache(String bufs) throws EBaseException { + FileOutputStream outstream = null; try { Encryptor sdr = new Encryptor(mToken, mKeyID, Encryptor.DEFAULT_ENCRYPTION_ALG); @@ -376,10 +385,10 @@ public class PWsdrCache { tmpPWcache.delete(); tmpPWcache = new File(mPWcachedb + ".tmp"); } - FileOutputStream outstream = new FileOutputStream(mPWcachedb + ".tmp"); + outstream = new FileOutputStream(mPWcachedb + ".tmp"); outstream.write(writebuf); - outstream.close(); + File origFile = new File(mPWcachedb); @@ -427,6 +436,14 @@ public class PWsdrCache { } catch (Exception e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); + } finally { + if (outstream != null) { + try { + outstream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/common/src/com/netscape/cmscore/util/FileAsString.java b/base/common/src/com/netscape/cmscore/util/FileAsString.java index 7853346f5..ea8fb796d 100644 --- a/base/common/src/com/netscape/cmscore/util/FileAsString.java +++ b/base/common/src/com/netscape/cmscore/util/FileAsString.java @@ -52,16 +52,19 @@ public class FileAsString { BufferedReader br = createBufferedReader(mFilename); StringBuffer buf = new StringBuffer(); int bytesread = 0; - - do { - char cbuf[] = new char[16]; - - bytesread = br.read(cbuf, 0, cbuf.length); - if (bytesread > 0) { - buf.append(cbuf, 0, bytesread); - } - } while (bytesread != -1); - br.close(); + try { + do { + char cbuf[] = new char[16]; + + bytesread = br.read(cbuf, 0, cbuf.length); + if (bytesread > 0) { + buf.append(cbuf, 0, bytesread); + } + } while (bytesread != -1); + } finally { + if (br != null) + br.close(); + } fileContents = new String(buf); } 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(); + } } } diff --git a/base/kra/src/com/netscape/kra/StorageKeyUnit.java b/base/kra/src/com/netscape/kra/StorageKeyUnit.java index 3d679e3d8..f968a8c44 100644 --- a/base/kra/src/com/netscape/kra/StorageKeyUnit.java +++ b/base/kra/src/com/netscape/kra/StorageKeyUnit.java @@ -261,19 +261,28 @@ public class StorageKeyUnit extends EncryptionUnit implements if (mKeySplitting) { // read private key from the file + FileInputStream fi = null; try { File priFile = new File(mConfig.getString(PROP_KEYDB)); mPrivateKeyData = new byte[ (Long.valueOf(priFile.length())).intValue()]; - FileInputStream fi = new FileInputStream(priFile); + fi = new FileInputStream(priFile); fi.read(mPrivateKeyData); - fi.close(); + } catch (IOException e) { mKRA.log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_KRA_STORAGE_READ_PRIVATE", e.toString())); throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_KEY_1", e.toString())); + } finally { + if (fi != null) { + try { + fi.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java index adad6ee37..990d0c156 100644 --- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java +++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java @@ -37,10 +37,15 @@ public class PlainPasswordFile implements IPasswordStore { mPwdStore = new Properties(); // initialize mPwdStore mPwdPath = pwdPath; - - FileInputStream file = new FileInputStream(mPwdPath); - mPwdStore.load(file); - file.close(); + FileInputStream file = null; + try { + file = new FileInputStream(mPwdPath); + mPwdStore.load(file); + } finally { + if (file != null) { + file.close(); + } + } } public String getPassword(String tag) { @@ -63,8 +68,14 @@ public class PlainPasswordFile implements IPasswordStore { public void commit() throws IOException, ClassCastException, NullPointerException { - FileOutputStream file = new FileOutputStream(mPwdPath); - mPwdStore.store(file, PASSWORD_WRITER_HEADER); - file.close(); + FileOutputStream file = null; + try { + file = new FileOutputStream(mPwdPath); + mPwdStore.store(file, PASSWORD_WRITER_HEADER); + } finally { + if (file != null) { + file.close(); + } + } } } diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java index 3a75097ec..d4de53bca 100644 --- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java +++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java @@ -37,9 +37,15 @@ public class PlainPasswordReader implements IPasswordReader { mPwdPath = pwdPath; mPwdStore = new Properties(); - FileInputStream file = new FileInputStream(mPwdPath); - mPwdStore.load(file); - file.close(); + FileInputStream file = null; + try { + file = new FileInputStream(mPwdPath); + mPwdStore.load(file); + } finally { + if (file != null) { + file.close(); + } + } } public String getPassword(String tag) { diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java index 3ceac4bd6..0965b7b31 100644 --- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java +++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java @@ -37,9 +37,15 @@ public class PlainPasswordWriter implements IPasswordWriter { mPwdPath = pwdPath; mPwdStore = new Properties(); - FileInputStream file = new FileInputStream(mPwdPath); - mPwdStore.load(file); - file.close(); + FileInputStream file = null; + try { + file = new FileInputStream(mPwdPath); + mPwdStore.load(file); + } finally { + if (file != null) { + file.close(); + } + } } public Object putPassword(String tag, String password) { @@ -48,9 +54,13 @@ public class PlainPasswordWriter implements IPasswordWriter { public void commit() throws IOException, ClassCastException, NullPointerException { - FileOutputStream file = new FileOutputStream(mPwdPath); - mPwdStore.store(file, PASSWORD_WRITER_HEADER); - file.close(); + FileOutputStream file = null; + try { + file = new FileOutputStream(mPwdPath); + mPwdStore.store(file, PASSWORD_WRITER_HEADER); + } finally { + if (file != null) + file.close(); + } } - } diff --git a/base/util/src/com/netscape/cmsutil/util/Utils.java b/base/util/src/com/netscape/cmsutil/util/Utils.java index 75e63f2b0..c8d6b438d 100644 --- a/base/util/src/com/netscape/cmsutil/util/Utils.java +++ b/base/util/src/com/netscape/cmsutil/util/Utils.java @@ -17,8 +17,6 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cmsutil.util; -import org.apache.commons.codec.binary.Base64; - import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; @@ -36,6 +34,8 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; +import org.apache.commons.codec.binary.Base64; + public class Utils { /** * Checks if this is NT. @@ -152,10 +152,12 @@ public class Utils { InetAddress.getByName(hostname); } - public static void copy(String orig, String dest) { + public static void copy(String orig, String dest) throws Exception { + BufferedReader in = null; + PrintWriter out = null; try { - BufferedReader in = new BufferedReader(new FileReader(orig)); - PrintWriter out = new PrintWriter( + in = new BufferedReader(new FileReader(orig)); + out = new PrintWriter( new BufferedWriter(new FileWriter(dest))); String line = ""; while (in.ready()) { @@ -163,9 +165,20 @@ public class Utils { if (line != null) out.println(line); } - in.close(); - out.close(); } catch (Exception ee) { + ee.printStackTrace(); + throw ee; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (out != null) { + out.close(); + } } } diff --git a/base/util/src/netscape/security/extensions/GenericASN1Extension.java b/base/util/src/netscape/security/extensions/GenericASN1Extension.java index 78d294573..b51846498 100644 --- a/base/util/src/netscape/security/extensions/GenericASN1Extension.java +++ b/base/util/src/netscape/security/extensions/GenericASN1Extension.java @@ -419,31 +419,37 @@ public class GenericASN1Extension extends Extension } FileInputStream fis = new FileInputStream(fname); - int n = 0; - while ((n = fis.available()) > 0) { - buff = new byte[n]; - int result = fis.read(buff); - if (result == -1) - break; - s = new String(buff); - } + try { + int n = 0; + while ((n = fis.available()) > 0) { + buff = new byte[n]; + int result = fis.read(buff); + if (result == -1) + break; + s = new String(buff); + } - for (i = 0, j = 0; j < s.length(); j++) { - int ch = s.charAt(j); - if (ch == 10 || ch == 13 || ch == 9) - continue; - i++; - } - buff = new byte[i]; - for (i = 0, j = 0; j < s.length(); j++) { - int ch = s.charAt(j); - if (ch == 10 || ch == 13 || ch == 9) - continue; - buff[i++] = (byte) ch; - } + for (i = 0, j = 0; j < s.length(); j++) { + int ch = s.charAt(j); + if (ch == 10 || ch == 13 || ch == 9) + continue; + i++; + } + buff = new byte[i]; + for (i = 0, j = 0; j < s.length(); j++) { + int ch = s.charAt(j); + if (ch == 10 || ch == 13 || ch == 9) + continue; + buff[i++] = (byte) ch; + } - s = new String(buff); + s = new String(buff); - return s; + return s; + } finally { + if (fis != null) { + fis.close(); + } + } } } diff --git a/base/util/src/netscape/security/extensions/PresenceServerExtension.java b/base/util/src/netscape/security/extensions/PresenceServerExtension.java index 276c91547..5ee802468 100644 --- a/base/util/src/netscape/security/extensions/PresenceServerExtension.java +++ b/base/util/src/netscape/security/extensions/PresenceServerExtension.java @@ -259,7 +259,7 @@ public class PresenceServerExtension extends Extension implements CertAttrSet { public void setOID(String oid) { } - public static void main(String args[]) throws Exception { + public static void main(String args[]) { /* 0 30 115: SEQUENCE { 2 06 9: OBJECT IDENTIFIER '2 16 840 1 113730 1 100' @@ -285,36 +285,56 @@ public class PresenceServerExtension extends Extension implements CertAttrSet { : } : } */ - boolean critical = false; - int version = 1; - String streetAddress = "401E Middlefield Rd.,MV,CA94041"; - String telephoneNumber = "650-111-1111"; - String rfc822Name = "admin@netscape.com"; - String ID = "ps-capitol"; - String hostName = "capitol"; - int portNumber = 80; - int maxUsers = 10; - int serviceLevel = 1; - - PresenceServerExtension ext = new PresenceServerExtension( - critical, - version, streetAddress, telephoneNumber, - rfc822Name, ID, hostName, portNumber, - maxUsers, serviceLevel); - - // encode - - ByteArrayOutputStream dos = new ByteArrayOutputStream(); - ext.encode(dos); - FileOutputStream fos = new FileOutputStream("pse.der"); - fos.write(dos.toByteArray()); - fos.close(); - - Extension ext1 = new Extension(new DerValue(dos.toByteArray())); - - @SuppressWarnings("unused") - PresenceServerExtension ext2 = new PresenceServerExtension( - Boolean.valueOf(false), ext1.getExtensionValue()); - + ByteArrayOutputStream dos = null; + FileOutputStream fos = null; + try { + boolean critical = false; + int version = 1; + String streetAddress = "401E Middlefield Rd.,MV,CA94041"; + String telephoneNumber = "650-111-1111"; + String rfc822Name = "admin@netscape.com"; + String ID = "ps-capitol"; + String hostName = "capitol"; + int portNumber = 80; + int maxUsers = 10; + int serviceLevel = 1; + + PresenceServerExtension ext = new PresenceServerExtension( + critical, + version, streetAddress, telephoneNumber, + rfc822Name, ID, hostName, portNumber, + maxUsers, serviceLevel); + + // encode + + dos = new ByteArrayOutputStream(); + ext.encode(dos); + fos = new FileOutputStream("pse.der"); + fos.write(dos.toByteArray()); + Extension ext1 = new Extension(new DerValue(dos.toByteArray())); + + @SuppressWarnings("unused") + PresenceServerExtension ext2 = new PresenceServerExtension( + Boolean.valueOf(false), ext1.getExtensionValue()); + } catch (IOException e) { + e.printStackTrace(); + } catch (CertificateException e) { + e.printStackTrace(); + } finally { + if (dos != null) { + try { + dos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } } diff --git a/base/util/src/netscape/security/x509/CRLDistributionPoint.java b/base/util/src/netscape/security/x509/CRLDistributionPoint.java index 442957449..2c70bf3e6 100644 --- a/base/util/src/netscape/security/x509/CRLDistributionPoint.java +++ b/base/util/src/netscape/security/x509/CRLDistributionPoint.java @@ -252,13 +252,14 @@ public class CRLDistributionPoint implements ASN1Value { } public static void main(String args[]) { + ByteArrayOutputStream bos = null; try { if (args.length != 1) { System.out.println("Usage: CRLDistributionPoint "); System.exit(-1); } - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + bos = new ByteArrayOutputStream(); SEQUENCE cdps = new SEQUENCE(); @@ -336,6 +337,14 @@ public class CRLDistributionPoint implements ASN1Value { } catch (Exception e) { e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java index 4180473d4..d8dee03a7 100644 --- a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java +++ b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java @@ -243,7 +243,7 @@ public class CRLDistributionPointsExtension extends Extension * Test driver. */ public static void main(String args[]) { - + BufferedOutputStream bos = null; try { if (args.length != 1) { @@ -252,7 +252,7 @@ public class CRLDistributionPointsExtension extends Extension System.exit(-1); } - BufferedOutputStream bos = new BufferedOutputStream( + bos = new BufferedOutputStream( new FileOutputStream(args[0])); // URI only @@ -291,10 +291,16 @@ public class CRLDistributionPointsExtension extends Extension crldpExt.setCritical(true); crldpExt.encode(bos); - bos.close(); - } catch (Exception e) { e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/util/src/netscape/security/x509/FreshestCRLExtension.java b/base/util/src/netscape/security/x509/FreshestCRLExtension.java index f108b4599..222a5e959 100644 --- a/base/util/src/netscape/security/x509/FreshestCRLExtension.java +++ b/base/util/src/netscape/security/x509/FreshestCRLExtension.java @@ -248,7 +248,7 @@ public class FreshestCRLExtension extends Extension * Test driver. */ public static void main(String args[]) { - + BufferedOutputStream bos = null; try { if (args.length != 1) { @@ -257,7 +257,7 @@ public class FreshestCRLExtension extends Extension System.exit(-1); } - BufferedOutputStream bos = new BufferedOutputStream( + bos = new BufferedOutputStream( new FileOutputStream(args[0])); // URI only @@ -296,10 +296,16 @@ public class FreshestCRLExtension extends Extension crldpExt.setCritical(true); crldpExt.encode(bos); - bos.close(); - } catch (Exception e) { e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/util/src/netscape/security/x509/IssuingDistributionPoint.java b/base/util/src/netscape/security/x509/IssuingDistributionPoint.java index 504587bf4..06638f2dd 100644 --- a/base/util/src/netscape/security/x509/IssuingDistributionPoint.java +++ b/base/util/src/netscape/security/x509/IssuingDistributionPoint.java @@ -272,6 +272,7 @@ public class IssuingDistributionPoint implements ASN1Value { } public static void main(String args[]) { + BufferedOutputStream bos = null; try { if (args.length != 1) { @@ -279,7 +280,7 @@ public class IssuingDistributionPoint implements ASN1Value { System.exit(-1); } - BufferedOutputStream bos = new BufferedOutputStream( + bos = new BufferedOutputStream( new FileOutputStream(args[0])); SEQUENCE idps = new SEQUENCE(); @@ -306,9 +307,16 @@ public class IssuingDistributionPoint implements ASN1Value { idps.addElement(idp); idps.encode(bos); - bos.close(); } catch (Exception e) { e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java index 0f8c8835c..b78a891c2 100644 --- a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java +++ b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java @@ -361,6 +361,7 @@ public class IssuingDistributionPointExtension extends Extension */ public static void main(String args[]) { + BufferedOutputStream bos = null; try { if (args.length != 1) { @@ -369,7 +370,7 @@ public class IssuingDistributionPointExtension extends Extension System.exit(-1); } - BufferedOutputStream bos = new BufferedOutputStream( + bos = new BufferedOutputStream( new FileOutputStream(args[0])); // URI only @@ -410,10 +411,18 @@ public class IssuingDistributionPointExtension extends Extension idpExt.setCritical(false); idpExt.encode(bos); - bos.close(); + } catch (Exception e) { e.printStackTrace(); + } finally { + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } } diff --git a/base/util/src/netscape/security/x509/OIDMap.java b/base/util/src/netscape/security/x509/OIDMap.java index 17573a019..4cebcf2c8 100644 --- a/base/util/src/netscape/security/x509/OIDMap.java +++ b/base/util/src/netscape/security/x509/OIDMap.java @@ -165,12 +165,20 @@ public class OIDMap { if (!namesMap.exists()) { loadNamesDefault(props); } else { + FileInputStream fis = null; try { - FileInputStream fis = new FileInputStream(namesMap); + fis = new FileInputStream(namesMap); props.load(fis); - fis.close(); } catch (IOException e) { loadNamesDefault(props); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } @@ -193,11 +201,20 @@ public class OIDMap { if (!classMap.exists()) { loadClassDefault(props); } else { + FileInputStream fis = null; try { - FileInputStream fis = new FileInputStream(classMap); + fis = new FileInputStream(classMap); props.load(fis); } catch (IOException e) { loadClassDefault(props); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } -- cgit