>From 930a9761d8f121852ec2636d67dc9367e6cd0ca2 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Fri, 22 Jun 2012 12:07:33 -0400
Subject: [PATCH] Fixes for Endi's Review for patch 15

---
 .../src/com/netscape/ca/CertificateAuthority.java  |    3 +-
 .../com/netscape/cmscore/security/PWsdrCache.java  |   20 +-
 .../src/com/netscape/cmstools/HttpClient.java      |  253 ++++++++++----------
 .../src/com/netscape/cmstools/OCSPClient.java      |   12 +-
 .../src/com/netscape/cmstools/PasswordCache.java   |   10 +-
 base/kra/src/com/netscape/kra/StorageKeyUnit.java  |   10 +-
 base/util/src/com/netscape/cmsutil/util/Utils.java |   23 +-
 .../extensions/PresenceServerExtension.java        |   27 +--
 .../security/x509/CRLDistributionPoint.java        |   13 +-
 .../x509/CRLDistributionPointsExtension.java       |   13 +-
 .../security/x509/FreshestCRLExtension.java        |   13 +-
 .../security/x509/IssuingDistributionPoint.java    |   13 +-
 .../x509/IssuingDistributionPointExtension.java    |   13 +-
 13 files changed, 227 insertions(+), 196 deletions(-)

diff --git a/base/ca/src/com/netscape/ca/CertificateAuthority.java b/base/ca/src/com/netscape/ca/CertificateAuthority.java
index b10c8df1b9fea80dcb0dcff9ca6467222178b932..feecec6a87a3aeffb7dda871648f336fa1b7a37a 100644
--- a/base/ca/src/com/netscape/ca/CertificateAuthority.java
+++ b/base/ca/src/com/netscape/ca/CertificateAuthority.java
@@ -1333,7 +1333,8 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             in = new FileInputStream(path);
             in.read(b);
         } finally {
-            in.close();
+            if (in != null)
+                in.close();
         }
         return b;
     }
diff --git a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java
index f4dbb1866f2dadab4fe56fa0ab03d1187f28d6fd..b0445bd6d10ff52f0594bbf40319ea75f9723139 100644
--- a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java
+++ b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java
@@ -333,10 +333,12 @@ public class PWsdrCache {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString()));
             throw new EBaseException(e.toString() + ": " + mPWcachedb);
         } finally {
-            try {
-                inputs.close();
-            } catch (IOException e) {
-                e.printStackTrace();
+            if (inputs != null) {
+                try {
+                    inputs.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
         }
 
@@ -435,10 +437,12 @@ public class PWsdrCache {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString()));
             throw new EBaseException(e.toString() + ": " + mPWcachedb);
         } finally {
-            try {
-                outstream.close();
-            } catch (IOException e) {
-                e.printStackTrace();
+            if (outstream != null) {
+                try {
+                    outstream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
         }
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/HttpClient.java b/base/java-tools/src/com/netscape/cmstools/HttpClient.java
index 69937e32c03904a8f407424818d47dbee868fcd2..d56b7df70423fae17e6ef4538b58f75edecd84b2 100644
--- a/base/java-tools/src/com/netscape/cmstools/HttpClient.java
+++ b/base/java-tools/src/com/netscape/cmstools/HttpClient.java
@@ -87,12 +87,17 @@ public class HttpClient {
 
         int offset = 0;
         int numRead = 0;
-        is = new FileInputStream(file);
-        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();
+            }
         }
-        is.close();
         if (offset < bytes.length) {
             throw new IOException("Could not completely read file " + filename);
         }
@@ -106,135 +111,129 @@ public class HttpClient {
         InputStream is = null;
         PrintStream ps = null;
         ByteArrayOutputStream bs = null;
-        try{
-        byte[] b = getBytesFromFile(ifilename);
+        SSLSocket sslSocket = null;
+        Socket socket = null;
+        try {
+            byte[] b = getBytesFromFile(ifilename);
 
-        System.out.println("Total number of bytes read = " + b.length);
-        if (_secure) {
-            SSLSocket socket = null;
+            System.out.println("Total number of bytes read = " + b.length);
+            if (_secure) {
+                try {
+                    CryptoManager.InitializationValues vals =
+                            new CryptoManager.InitializationValues(dbdir, "", "", "secmod.db");
+                    CryptoManager.initialize(vals);
+                    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 {
+                            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 {
+                            sslSocket.setCipherPreference(i, true);
+                        } catch (SocketException e) {
+                        }
+                    }
+                    for (i = 0; cipherSuites[i] != 0; ++i) {
+                        try {
+                            sslSocket.setCipherPreference(cipherSuites[i], true);
+                        } catch (SocketException e) {
+                        }
+                    }
+                    SSLHandshakeCompletedListener listener = new ClientHandshakeCB(this);
+                    sslSocket.addHandshakeCompletedListener(listener);
+
+                    if (clientmode != null && clientmode.equals("true")) {
+                        CryptoManager cm = CryptoManager.getInstance();
+                        CryptoToken token = cm.getInternalKeyStorageToken();
+                        Password pass = new Password(password.toCharArray());
+                        token.login(pass);
+                        X509Certificate cert = cm.findCertByNickname(nickname);
+                        if (cert == null)
+                            System.out.println("client cert is null");
+                        else
+                            System.out.println("client cert is not null");
+                        sslSocket.setUseClientMode(true);
+                        sslSocket.setClientCertNickname(nickname);
+                    }
+
+                    sslSocket.forceHandshake();
+                    dos = new DataOutputStream(sslSocket.getOutputStream());
+                    is = sslSocket.getInputStream();
+                } catch (Exception e) {
+                    System.out.println("Exception: " + e.toString());
+                    return;
+                }
+            } else {
+
+                try {
+                    socket = new Socket(_host, _port);
+                    dos = new DataOutputStream(socket.getOutputStream());
+                    is = socket.getInputStream();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    return;
+                }
+            }
+
+            // 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 {
-                CryptoManager.InitializationValues vals =
-                        new CryptoManager.InitializationValues(dbdir, "", "", "secmod.db");
-                CryptoManager.initialize(vals);
-                socket = 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);
-                    } 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);
-                    } catch (SocketException e) {
-                    }
-                }
-                for (i = 0; cipherSuites[i] != 0; ++i) {
-                    try {
-                        socket.setCipherPreference(cipherSuites[i], true);
-                    } catch (SocketException e) {
+                while (true) {
+                    int r = is.read();
+                    if (r == -1)
+                        break;
+                    if (r == 10) {
+                        sum++;
                     }
-                }
-                SSLHandshakeCompletedListener listener = new ClientHandshakeCB(this);
-                socket.addHandshakeCompletedListener(listener);
-
-                if (clientmode != null && clientmode.equals("true")) {
-                    CryptoManager cm = CryptoManager.getInstance();
-                    CryptoToken token = cm.getInternalKeyStorageToken();
-                    Password pass = new Password(password.toCharArray());
-                    token.login(pass);
-                    X509Certificate cert = cm.findCertByNickname(nickname);
-                    if (cert == null)
-                        System.out.println("client cert is null");
-                    else
-                        System.out.println("client cert is not null");
-                    socket.setUseClientMode(true);
-                    socket.setClientCertNickname(nickname);
-                }
-
-                socket.forceHandshake();
-                dos = new DataOutputStream(socket.getOutputStream());
-                is = socket.getInputStream();
-            } catch (Exception e) {
-                System.out.println("Exception: " + e.toString());
-                return;
-            } finally {
-                if (socket != null) {
-                    socket.close();
-                }
-            }
-        } else {
-            Socket socket = null;
-            try{
-            socket = new Socket(_host, _port);
-            dos = new DataOutputStream(socket.getOutputStream());
-            is = socket.getInputStream();
-            }catch(IOException e){
-                e.printStackTrace();
-                return;
-            } finally {
-                if (socket != null)
-                    socket.close();
-            }
-        }
-
-        // 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);
+                    if (sum == 6) {
+                        startSaving = true;
+                        continue;
                     }
-                    if (hack == false) {
-                        hack = true;
+                    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);
 
-        bs = new ByteArrayOutputStream();
-        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("");
-        }catch(Exception e){
+            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 {
@@ -250,6 +249,12 @@ public class HttpClient {
             if (ps != null) {
                 ps.close();
             }
+            if (sslSocket != null) {
+                sslSocket.close();
+            }
+            if (socket != null) {
+                socket.close();
+            }
         }
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/OCSPClient.java b/base/java-tools/src/com/netscape/cmstools/OCSPClient.java
index 021f728f56bf937b7768a55bd34c4a526088b995..ce0e853cdfeb98c4521d743e3f092205f342a2ca 100644
--- a/base/java-tools/src/com/netscape/cmstools/OCSPClient.java
+++ b/base/java-tools/src/com/netscape/cmstools/OCSPClient.java
@@ -249,18 +249,26 @@ 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);
-                fis.close();
             } catch (Exception e1) {
                 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 098d8ddae240f1b91a43a00c90411e10b4ed97d9..a08e0c9ad8377c407680927fdce01ce259c4f937 100644
--- a/base/java-tools/src/com/netscape/cmstools/PasswordCache.java
+++ b/base/java-tools/src/com/netscape/cmstools/PasswordCache.java
@@ -192,13 +192,19 @@ 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();
-                    r.close();
                 } catch (Exception e) {
                     System.out.println("Error: " + e.toString());
                     System.exit(1);
+                } finally {
+                    try {
+                        r.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
                 }
 
                 if (mKeyIdString != null) {
diff --git a/base/kra/src/com/netscape/kra/StorageKeyUnit.java b/base/kra/src/com/netscape/kra/StorageKeyUnit.java
index 67789e6720ec1e2744861fe35676023dabc95b8f..f968a8c44e12ba8a3850959fb0d58890eb683e6e 100644
--- a/base/kra/src/com/netscape/kra/StorageKeyUnit.java
+++ b/base/kra/src/com/netscape/kra/StorageKeyUnit.java
@@ -276,10 +276,12 @@ public class StorageKeyUnit extends EncryptionUnit implements
                             CMS.getLogMessage("CMSCORE_KRA_STORAGE_READ_PRIVATE", e.toString()));
                     throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_KEY_1", e.toString()));
                 } finally {
-                    try {
-                        fi.close();
-                    } catch (IOException e) {
-                        e.printStackTrace();
+                    if (fi != null) {
+                        try {
+                            fi.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
                     }
                 }
             }
diff --git a/base/util/src/com/netscape/cmsutil/util/Utils.java b/base/util/src/com/netscape/cmsutil/util/Utils.java
index 9ced208cd1603ba885d2b98974f730e0e7e9f00c..c8d6b438dec11d5667870ee4ccba23382a9c3868 100644
--- a/base/util/src/com/netscape/cmsutil/util/Utils.java
+++ b/base/util/src/com/netscape/cmsutil/util/Utils.java
@@ -152,7 +152,7 @@ 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 {
@@ -167,17 +167,18 @@ public class Utils {
             }
         } catch (Exception ee) {
             ee.printStackTrace();
-        }
-        if(in != null){
-            try {
-                in.close();
-            } catch (IOException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
+            throw ee;
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (out != null) {
+                out.close();
             }
-        }
-        if (out != null){
-            out.close();
         }
     }
 
diff --git a/base/util/src/netscape/security/extensions/PresenceServerExtension.java b/base/util/src/netscape/security/extensions/PresenceServerExtension.java
index aff755460f389f9a2ba81c53f840a86e49bd364a..5ee802468ec5aad6de9b32b44085e8447e72ea10 100644
--- a/base/util/src/netscape/security/extensions/PresenceServerExtension.java
+++ b/base/util/src/netscape/security/extensions/PresenceServerExtension.java
@@ -311,8 +311,6 @@ public class PresenceServerExtension extends Extension implements CertAttrSet {
             ext.encode(dos);
             fos = new FileOutputStream("pse.der");
             fos.write(dos.toByteArray());
-            fos.close();
-
             Extension ext1 = new Extension(new DerValue(dos.toByteArray()));
 
             @SuppressWarnings("unused")
@@ -322,19 +320,20 @@ public class PresenceServerExtension extends Extension implements CertAttrSet {
             e.printStackTrace();
         } catch (CertificateException e) {
             e.printStackTrace();
-        }
-        if (dos != null) {
-            try {
-                dos.close();
-            } catch (IOException 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();
+            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 51e6e42b88df5288e33dbe582b912e23e8dfdda9..2c70bf3e625591a24a5ba20cb879b0802cbb326d 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPoint.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPoint.java
@@ -337,12 +337,13 @@ public class CRLDistributionPoint implements ASN1Value {
 
         } catch (Exception e) {
             e.printStackTrace();
-        }
-        if (bos != null) {
-            try {
-                bos.close();
-            } catch (IOException 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 0ca5131ab4171f9cf3fb77cc19e21227b9d8fbc1..d8dee03a7f3fcbc21c9392d55d577acbfaa907c8 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
@@ -293,12 +293,13 @@ public class CRLDistributionPointsExtension extends Extension
 
         } catch (Exception e) {
             e.printStackTrace();
-        }
-        if (bos != null) {
-            try {
-                bos.close();
-            } catch (IOException 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 7a2028c17454a45cb36807d7eb82ed716244da49..222a5e9594539395dd816c5bd73eee2a8b0fac05 100644
--- a/base/util/src/netscape/security/x509/FreshestCRLExtension.java
+++ b/base/util/src/netscape/security/x509/FreshestCRLExtension.java
@@ -298,12 +298,13 @@ public class FreshestCRLExtension extends Extension
 
         } catch (Exception e) {
             e.printStackTrace();
-        }
-        if (bos != null) {
-            try {
-                bos.close();
-            } catch (IOException 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 efff188672d3132df0da75517a41f48c2a7f7aaf..06638f2dd4eb070bf567554cf0fe33e355d2c57c 100644
--- a/base/util/src/netscape/security/x509/IssuingDistributionPoint.java
+++ b/base/util/src/netscape/security/x509/IssuingDistributionPoint.java
@@ -309,12 +309,13 @@ public class IssuingDistributionPoint implements ASN1Value {
             idps.encode(bos);
         } catch (Exception e) {
             e.printStackTrace();
-        }
-        if (bos != null) {
-            try {
-                bos.close();
-            } catch (IOException 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 0e53f554651198628020e94fe963bda33e3cba8b..b78a891c230e74d5105bbc31770918729d5c20bb 100644
--- a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
+++ b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
@@ -415,12 +415,13 @@ public class IssuingDistributionPointExtension extends Extension
 
         } catch (Exception e) {
             e.printStackTrace();
-        }
-        if (bos != null) {
-            try {
-                bos.close();
-            } catch (IOException e) {
-                e.printStackTrace();
+        } finally {
+            if (bos != null) {
+                try {
+                    bos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
         }
     }
-- 
1.7.10.2

