>From 25eb5c8b932c0cc8aafcfd84982e9ed5f16c667b Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Mon, 18 Jun 2012 14:08:21 -0400
Subject: [PATCH] Fixes for Resource Leaks shown in Coverity for DogTag 10

---
 .../src/com/netscape/cmstools/DRMTool.java         |   27 ++++
 .../src/com/netscape/cmstools/PKCS12Export.java    |    1 +
 .../src/com/netscape/pkisilent/ConfigureCA.java    |  117 ++++++++++-----
 .../src/com/netscape/pkisilent/ConfigureDRM.java   |   43 +++++-
 .../src/com/netscape/pkisilent/ConfigureOCSP.java  |   25 +++-
 .../src/com/netscape/pkisilent/ConfigureTKS.java   |   26 +++-
 .../com/netscape/pkisilent/common/Con2Agent.java   |  125 +++++++++++-----
 .../com/netscape/pkisilent/common/DirEnroll.java   |   60 +++++---
 .../com/netscape/pkisilent/common/ServerInfo.java  |   14 +-
 .../com/netscape/pkisilent/common/TestClient.java  |   25 ++--
 .../com/netscape/pkisilent/common/UserEnroll.java  |   62 ++++++--
 .../com/netscape/pkisilent/common/Utilities.java   |  105 ++++++++++---
 .../com/netscape/pkisilent/http/HTTPClient.java    |  156 ++++++++++++--------
 13 files changed, 572 insertions(+), 214 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/DRMTool.java b/base/java-tools/src/com/netscape/cmstools/DRMTool.java
index 0eaf26ee10b58c6be004b8e0316d9ec5105b81b5..9fbab5da63fac946cfb5776fe81696d1c7900e9c 100644
--- a/base/java-tools/src/com/netscape/cmstools/DRMTool.java
+++ b/base/java-tools/src/com/netscape/cmstools/DRMTool.java
@@ -1657,6 +1657,14 @@ public class DRMTool {
                             + "'"
                             + NEWLINE, true);
                     System.exit(0);
+                } finally {
+                    if (in != null) {
+                        try {
+                            in.close();
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
                 }
             }
         } catch (Exception exUninitialized) {
@@ -4291,6 +4299,17 @@ public class DRMTool {
                     + "'"
                     + NEWLINE, true);
             return FAILURE;
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (writer != null) {
+                writer.close();
+            }
         }
 
         return SUCCESS;
@@ -4401,6 +4420,14 @@ public class DRMTool {
                     + "'"
                     + NEWLINE, true);
             return FAILURE;
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         return SUCCESS;
diff --git a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
index 53250586738efcac63c8cb6a27b574cf5e9fba44..8e985bbe0f46b22ca02336dfc7fba771dacf1d20 100644
--- a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
+++ b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
@@ -239,6 +239,7 @@ public class PKCS12Export {
             if (pk12pwd == null) {
                 pk12pwd = "";
             }
+            in.close();
         } catch (Exception e) {
             debug("Failed to read the keydb password from the file. Exception: " + e.toString());
             System.exit(1);
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
index f8c90df38b0969dcf5d6ba7fd1b00d8a184d226e..0af43d03dc86a3e754b116407adb2ed8868f9b42 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureCA.java
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.net.URLEncoder;
@@ -662,10 +663,12 @@ public class ConfigureCA {
                         return false;
                     }
                 } else {
+                    FileInputStream fis = null;
+                    BufferedReader in = null;
                     try {
                         ca_cert_cert = "";
-                        FileInputStream fis = new FileInputStream(ext_ca_cert_file);
-                        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+                        fis = new FileInputStream(ext_ca_cert_file);
+                        in = new BufferedReader(new InputStreamReader(fis));
                         String line;
                         while ((line = in.readLine()) != null) {
                             ca_cert_cert += line;
@@ -678,13 +681,27 @@ public class ConfigureCA {
                         while ((line = in.readLine()) != null) {
                             signing_cc += line;
                         }
-                        in.close();
                         return true;
                     } catch (Exception e) {
                         System.out.println(
                                "CertSubjectPanel: Unable to read in external approved CA cert or certificate chain.");
                         System.out.println(e.toString());
                         return false;
+                    } finally {
+                        if (fis != null) {
+                            try {
+                                fis.close();
+                            } catch (IOException e) {
+                                e.printStackTrace();
+                            }
+                        }
+                        if (in != null) {
+                            try {
+                                in.close();
+                            } catch (IOException e) {
+                                e.printStackTrace();
+                            }
+                        }
                     }
                 }
             }
@@ -865,6 +882,9 @@ public class ConfigureCA {
     }
 
     public boolean BackupPanel() {
+        FileOutputStream fos = null;
+        BufferedReader br = null;
+        FileInputStream fis = null;
         try {
             HTTPResponse hr = null;
 
@@ -884,43 +904,38 @@ public class ConfigureCA {
 
                 // dump hr.getResponseData() to file
 
-                try {
-                    FileOutputStream fos = new FileOutputStream(backup_fname);
-
-                    fos.write(hr.getResponseData());
-                    fos.close();
-
-                    // set file to permissions 600
-                    String rtParams[] = { "chmod", "600", backup_fname };
-                    Process proc = Runtime.getRuntime().exec(rtParams);
-
-                    BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-                    String line = null;
-                    while ((line = br.readLine()) != null)
-                        System.out.println("Error: " + line);
-                    proc.waitFor();
-
-                    // verify p12 file
-                    // Decode the P12 file
-                    FileInputStream fis = new FileInputStream(backup_fname);
-                    PFX.Template pfxt = new PFX.Template();
-                    PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
-
-                    System.out.println("Decoded PFX");
-
-                    // now peruse it for interesting info
-                    System.out.println("Version: " + pfx.getVersion());
-                    AuthenticatedSafes authSafes = pfx.getAuthSafes();
-                    SEQUENCE asSeq = authSafes.getSequence();
-
-                    System.out.println(
-                            "AuthSafes has " + asSeq.size() + " SafeContents");
-
-                    fis.close();
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    return false;
-                }
+                fos = new FileOutputStream(backup_fname);
+
+                fos.write(hr.getResponseData());
+
+                // set file to permissions 600
+                String rtParams[] = { "chmod", "600", backup_fname };
+                Process proc = Runtime.getRuntime().exec(rtParams);
+
+                br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+                String line = null;
+                while ((line = br.readLine()) != null)
+                    System.out.println("Error: " + line);
+                proc.waitFor();
+
+                // verify p12 file
+                // Decode the P12 file
+                fis = new FileInputStream(backup_fname);
+                PFX.Template pfxt = new PFX.Template();
+                PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
+
+                System.out.println("Decoded PFX");
+
+                // now peruse it for interesting info
+                System.out.println("Version: " + pfx.getVersion());
+                AuthenticatedSafes authSafes = pfx.getAuthSafes();
+                SEQUENCE asSeq = authSafes.getSequence();
+
+                System.out.println(
+                        "AuthSafes has " + asSeq.size() + " SafeContents");
+
+                fis.close();
+
             }
 
             return true;
@@ -928,6 +943,28 @@ public class ConfigureCA {
             System.out.println("Exception in BackupPanel(): " + e.toString());
             e.printStackTrace();
             return false;
+        } finally {
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
index ca35818690af1c744ae548760e247d77b4169e2f..7d5988baa76743478cf7e2b78a88497165431724 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureDRM.java
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -661,16 +662,18 @@ public class ConfigureDRM {
         hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
+        FileOutputStream fos = null;
+        BufferedReader br = null;
+        FileInputStream fis = null;
         try {
-            FileOutputStream fos = new FileOutputStream(backup_fname);
+            fos = new FileOutputStream(backup_fname);
             fos.write(hr.getResponseData());
-            fos.close();
 
             // set file to permissions 600
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
 
-            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
             String line = null;
             while ((line = br.readLine()) != null)
                 System.out.println("Error: " + line);
@@ -679,7 +682,7 @@ public class ConfigureDRM {
             // verify p12 file
 
             // Decode the P12 file
-            FileInputStream fis = new FileInputStream(backup_fname);
+            fis = new FileInputStream(backup_fname);
             PFX.Template pfxt = new PFX.Template();
             PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
             System.out.println("Decoded PFX");
@@ -691,8 +694,6 @@ public class ConfigureDRM {
             System.out.println("AuthSafes has " +
                     asSeq.size() + " SafeContents");
 
-            fis.close();
-
             if (clone) {
                 query_string = "p=12" + "&op=next" + "&xml=true";
                 hr = hc.sslConnect(cs_hostname, cs_port, wizard_uri, query_string);
@@ -705,6 +706,36 @@ public class ConfigureDRM {
         } catch (Exception e) {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
+        } finally {
+
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (bais != null) {
+                try {
+                    bais.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
index 0ca0a082a811ed1cfd7ea19d1ee6d8196efb8436..8eb0e611666ba18e2344cda335508efbe72738fc 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureOCSP.java
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -553,9 +554,11 @@ public class ConfigureOCSP {
         hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
-
+        FileOutputStream fos = null;
+        BufferedReader br = null;
+        FileInputStream fis = null;
         try {
-            FileOutputStream fos = new FileOutputStream(backup_fname);
+            fos = new FileOutputStream(backup_fname);
             fos.write(hr.getResponseData());
             fos.close();
 
@@ -563,7 +566,7 @@ public class ConfigureOCSP {
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
 
-            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
             String line = null;
             while ((line = br.readLine()) != null)
                 System.out.println("Error: " + line);
@@ -572,7 +575,7 @@ public class ConfigureOCSP {
             // verify p12 file
 
             // Decode the P12 file
-            FileInputStream fis = new FileInputStream(backup_fname);
+            fis = new FileInputStream(backup_fname);
             PFX.Template pfxt = new PFX.Template();
             PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
             System.out.println("Decoded PFX");
@@ -588,6 +591,20 @@ public class ConfigureOCSP {
         } catch (Exception e) {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
+        } finally {
+            try {
+                if (fos != null) {
+                    fos.close();
+                }
+                if (br != null) {
+                    br.close();
+                }
+                if (fis != null) {
+                    fis.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
 
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
index aa8dccee843f1a43052a7bf27356c5a83b154c64..e4b8ab6c4a524c0e16ced027c054d32eec47a8cc 100644
--- a/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
+++ b/base/silent/src/com/netscape/pkisilent/ConfigureTKS.java
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -524,9 +525,11 @@ public class ConfigureTKS {
         HTTPResponse hr = hc.sslConnect(cs_hostname, cs_port, pkcs12_uri, query_string);
 
         // dump hr.getResponseData() to file
-
+        FileOutputStream fos = null;
+        BufferedReader br = null;
+        FileInputStream fis = null;
         try {
-            FileOutputStream fos = new FileOutputStream(backup_fname);
+            fos = new FileOutputStream(backup_fname);
             fos.write(hr.getResponseData());
             fos.close();
 
@@ -534,7 +537,7 @@ public class ConfigureTKS {
             String rtParams[] = { "chmod", "600", backup_fname };
             Process proc = Runtime.getRuntime().exec(rtParams);
 
-            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
+            br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
             String line = null;
             while ((line = br.readLine()) != null)
                 System.out.println("Error: " + line);
@@ -543,7 +546,7 @@ public class ConfigureTKS {
             // verify p12 file
 
             // Decode the P12 file
-            FileInputStream fis = new FileInputStream(backup_fname);
+            fis = new FileInputStream(backup_fname);
             PFX.Template pfxt = new PFX.Template();
             PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(fis, 2048));
             System.out.println("Decoded PFX");
@@ -555,10 +558,23 @@ public class ConfigureTKS {
             System.out.println("AuthSafes has " +
                     asSeq.size() + " SafeContents");
 
-            fis.close();
         } catch (Exception e) {
             System.out.println("ERROR: Exception=" + e.getMessage());
             return false;
+        } finally {
+            try {
+                if (fos != null) {
+                    fos.close();
+                }
+                if (br != null) {
+                    br.close();
+                }
+                if (fis != null) {
+                    fis.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
 
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
index 6d02fcd339dd1ded7a2a6af46f3afbc63f479e64..4ea389647fab629b48b2d55e8c03507d5e2edb17 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
 
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
@@ -157,13 +158,18 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
     // Submit requests
 
     public boolean Send() {
+        SSLSocket socket = null;
+        OutputStream rawos = null;
+        BufferedOutputStream os = null;
+        PrintStream ps = null;
+        BufferedReader stdin1 = null;
         try {
 
             if (!loginCertDB()) {
                 return false;
             }
 
-            SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+            socket = new SSLSocket(host, port, null, 0, this, null);
 
             System.out.println("Con2Agent.java: host = " + host);
             System.out.println("Con2Agent.java: port = " + port);
@@ -172,9 +178,9 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
             socket.setClientCertNickname(certnickname);
             System.out.println("Connected to the socket");
 
-            OutputStream rawos = socket.getOutputStream();
-            BufferedOutputStream os = new BufferedOutputStream(rawos);
-            PrintStream ps = new PrintStream(os);
+            rawos = socket.getOutputStream();
+            os = new BufferedOutputStream(rawos);
+            ps = new PrintStream(os);
 
             System.out.println(ACTIONURL);
             System.out.println("Query :" + query);
@@ -185,9 +191,7 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
             ps.println("");
             ps.println(query);
             ps.println("\r");
-            ps.flush();
-            os.flush();
-            BufferedReader stdin1 = new BufferedReader(
+            stdin1 = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
             String line;
 
@@ -195,32 +199,53 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
                 stdout.append(line + "\n");
                 System.out.println(line);
             }
+            ps.println("Connection: close");
+            ps.flush();
+            os.flush();
 
+        } catch (Exception e) {
+            System.out.println("some exception: in Send routine" + e);
+            return false;
+        } finally {
             // Send Connection: close to let the server close the connection.
             // Else the socket on the server side continues to remain in TIME_WAIT state
-
-            ps.println("Connection: close");
-            ps.flush();
-            os.flush();
-            os.close();
-            rawos.close();
-            ps.close();
-            stdin1.close();
-            socket.close();
-
+            if (ps != null)
+                ps.close();
+            if (stdin1 != null) {
+                try {
+                    stdin1.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (rawos != null) {
+                try {
+                    rawos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
             if (socket.isClosed()) {
                 System.out.println("Con2Agent.java : Socket is Closed");
             } else {
                 System.out.println("Con2Agent.java : Socket not Closed");
             }
-
-        } catch (Exception e) {
-            System.out.println("some exception: in Send routine" + e);
-            return false;
         }
-
         return true;
-
     }
 
     private boolean loginCertDB() {
@@ -271,6 +296,11 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
     }
 
     public boolean Send_withGET() {
+        SSLSocket socket = null;
+        OutputStream rawos = null;
+        BufferedOutputStream os = null;
+        PrintStream ps = null;
+        BufferedReader stdin2 = null;
 
         try {
 
@@ -278,14 +308,14 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
                 return false;
             }
 
-            SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+            socket = new SSLSocket(host, port, null, 0, this, null);
 
             socket.setClientCertNickname(certnickname);
             System.out.println("Connected to the socket");
 
-            OutputStream rawos = socket.getOutputStream();
-            BufferedOutputStream os = new BufferedOutputStream(rawos);
-            PrintStream ps = new PrintStream(os);
+            rawos = socket.getOutputStream();
+            os = new BufferedOutputStream(rawos);
+            ps = new PrintStream(os);
 
             System.out.println("Query in con2agent :" + query);
             System.out.println("ACTIONURL in con2agent : " + ACTIONURL);
@@ -295,24 +325,51 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
             ps.println("\r");
             ps.flush();
             os.flush();
-            BufferedReader stdin2 = new BufferedReader(
+            stdin2 = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
             String line;
 
             while ((line = stdin2.readLine()) != null) {
                 stdout.append(line + "\n");
             }
-            stdin2.close();
-
-            socket.close();
-
         } catch (Exception e) {
             System.err.println("some exception: in Send routine" + e);
             return false;
+        } finally {
+
+            if (ps != null)
+                ps.close();
+            if (stdin2 != null) {
+                try {
+                    stdin2.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (rawos != null) {
+                try {
+                    rawos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
         }
-
         return true;
-
     }
 
 } // end of class
diff --git a/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java b/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
index 809a65c5be0ee4ec6c55b1691333eb4fd9aa517c..6ab1fb190581e07b649b3163b1151ae1bb85a640 100644
--- a/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
+++ b/base/silent/src/com/netscape/pkisilent/common/DirEnroll.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
 
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
@@ -260,7 +261,11 @@ public class DirEnroll extends TestClient {
 
     private boolean Send() {
         boolean st = false;
-
+        SSLSocket socket = null;
+        OutputStream rawos = null;
+        BufferedOutputStream os = null;
+        PrintStream ps = null;
+        BufferedReader stdin = null;
         try {
             if (debug) {
                 System.out.println("Step 3 : Socket initialize");
@@ -273,13 +278,13 @@ public class DirEnroll extends TestClient {
             GregorianCalendar begin = new GregorianCalendar();
 
             // SSLSocket socket = new SSLSocket(host,port);
-            SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+            socket = new SSLSocket(host, port, null, 0, this, null);
 
             socket.setUseClientMode(true);
 
-            OutputStream rawos = socket.getOutputStream();
-            BufferedOutputStream os = new BufferedOutputStream(rawos);
-            PrintStream ps = new PrintStream(os);
+            rawos = socket.getOutputStream();
+            os = new BufferedOutputStream(rawos);
+            ps = new PrintStream(os);
 
             ps.println("POST /enrollment HTTP/1.0");
             ps.println("Connection: Keep-Alive");
@@ -290,7 +295,7 @@ public class DirEnroll extends TestClient {
             ps.println("\r");
             ps.flush();
             os.flush();
-            BufferedReader stdin = new BufferedReader(
+            stdin = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
 
             if (debug) {
@@ -327,16 +332,6 @@ public class DirEnroll extends TestClient {
                 }
 
             }
-            stdin.close();
-            socket.close();
-            os.close();
-            rawos.close();
-            ps.close();
-            os = null;
-            rawos = null;
-            stdin = null;
-            ps = null;
-            line = null;
 
             GregorianCalendar end = new GregorianCalendar();
             long diff = calculateElapsedTime(begin, end);
@@ -346,10 +341,39 @@ public class DirEnroll extends TestClient {
         } catch (Exception e) {
             System.err.println("some exception: in Send routine" + e);
             return false;
+        } finally {
+            if (ps != null)
+                ps.close();
+            if (stdin != null) {
+                try {
+                    stdin.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (rawos != null) {
+                try {
+                    rawos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
-
         return st;
-
     }
 
     private void buildquery() throws UnsupportedEncodingException {
diff --git a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
index 637f0c1ee2bc0e76735777406c5f046317ee057e..1be4d0a301f5880095ee60268d4e6838050400c8 100644
--- a/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
+++ b/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
@@ -21,6 +21,7 @@ package com.netscape.pkisilent.common;
 import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.io.FileReader;
+import java.io.IOException;
 import java.net.InetAddress;
 import java.util.StringTokenizer;
 
@@ -260,9 +261,9 @@ public class ServerInfo {
         String ldapHostStr = "ldapHost:";
         String ldapPortStr = "ldapPort:";
         String adminPortStr = "port:";
-
+        FileInputStream fis = null;
         try {
-            FileInputStream fis = new FileInputStream(AdminConfigFile);
+            fis = new FileInputStream(AdminConfigFile);
             int size = fis.available();
             byte b[] = new byte[size];
 
@@ -298,8 +299,15 @@ public class ServerInfo {
             fis.close();
         } catch (Exception e) {
             System.out.println("exception " + e.getMessage());
+        } finally {
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
-
     }
 
     private void readCMSConfig() {
diff --git a/base/silent/src/com/netscape/pkisilent/common/TestClient.java b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
index 22275ede5c0847c67517671382a11aab89fc8ca3..f3aaabf4199450fb3ff177ba4af432bf28b7a522 100644
--- a/base/silent/src/com/netscape/pkisilent/common/TestClient.java
+++ b/base/silent/src/com/netscape/pkisilent/common/TestClient.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
 
 import java.io.BufferedReader;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.ServerSocket;
 import java.util.Properties;
@@ -357,14 +358,12 @@ public class TestClient implements SSLCertificateApprovalCallback {
      */
 
     public void getProperties(String fileName) throws Exception {
-        try {
-            FileInputStream fis = new FileInputStream(fileName);
+        FileInputStream fis = new FileInputStream(fileName);
 
-            props = new Properties();
-            props.load(fis);
-        } catch (Exception e) {
-            System.out.println("exception " + e.getMessage());
-        }
+        props = new Properties();
+        props.load(fis);
+        if (fis != null)
+            fis.close();
 
     }
 
@@ -488,8 +487,9 @@ public class TestClient implements SSLCertificateApprovalCallback {
      **/
 
     public String readRequest(String filename) {
+        FileInputStream f1 = null;
         try {
-            FileInputStream f1 = new FileInputStream(filename);
+            f1 = new FileInputStream(filename);
             int size = f1.available();
             byte b[] = new byte[size];
 
@@ -497,13 +497,20 @@ public class TestClient implements SSLCertificateApprovalCallback {
                 return null;
             }
 
-            f1.close();
             String s = new String(b);
 
             return s;
         } catch (Exception e) {
             System.out.println("exception " + e.getMessage());
             return null;
+        } finally {
+            if (f1 != null) {
+                try {
+                    f1.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
diff --git a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
index 0e31eeb57ac815440ab111669d00689432e85fab..6b400ef0f562c18b312483beef6c4778627337dc 100644
--- a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
+++ b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
 
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
@@ -218,7 +219,11 @@ public class UserEnroll extends TestClient {
 
     private boolean Send() {
         boolean st = false;
-
+        SSLSocket socket = null;
+        OutputStream rawos = null;
+        BufferedOutputStream os = null;
+        PrintStream ps = null;
+        BufferedReader stdin = null;
         try {
 
             if (debug) {
@@ -232,12 +237,12 @@ public class UserEnroll extends TestClient {
             GregorianCalendar begin = new GregorianCalendar();
 
             // SSLSocket socket = new SSLSocket(host,port);
-            SSLSocket socket = new SSLSocket(host, port, null, 0, this, null);
+            socket = new SSLSocket(host, port, null, 0, this, null);
 
             socket.setUseClientMode(true);
-            OutputStream rawos = socket.getOutputStream();
-            BufferedOutputStream os = new BufferedOutputStream(rawos);
-            PrintStream ps = new PrintStream(os);
+            rawos = socket.getOutputStream();
+            os = new BufferedOutputStream(rawos);
+            ps = new PrintStream(os);
 
             ps.println("POST /enrollment HTTP/1.0");
             ps.println("Connection: Keep-Alive");
@@ -248,7 +253,7 @@ public class UserEnroll extends TestClient {
             ps.println("\r");
             ps.flush();
             os.flush();
-            BufferedReader stdin = new BufferedReader(
+            stdin = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
 
             if (debug) {
@@ -274,16 +279,6 @@ public class UserEnroll extends TestClient {
                 }
 
             }
-            stdin.close();
-            socket.close();
-            os.close();
-            rawos.close();
-            ps.close();
-            os = null;
-            rawos = null;
-            stdin = null;
-            ps = null;
-            line = null;
             GregorianCalendar end = new GregorianCalendar();
             long diff = calculateElapsedTime(begin, end);
 
@@ -292,6 +287,41 @@ public class UserEnroll extends TestClient {
         } catch (Exception e) {
             System.err.println("some exception: in Send routine" + e);
             return false;
+        } finally {
+            if (ps != null) {
+                ps.close();
+            }
+            if (stdin != null) {
+                try {
+                    stdin.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+            if (rawos != null) {
+                try {
+                    rawos.close();
+                } catch (IOException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
         }
 
         return st;
diff --git a/base/silent/src/com/netscape/pkisilent/common/Utilities.java b/base/silent/src/com/netscape/pkisilent/common/Utilities.java
index 23fd2c54e365e797689bf11b90e10c8032dc8929..79712eb11b42f6cbbf89887ad0dea986c52903a8 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Utilities.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Utilities.java
@@ -20,6 +20,7 @@ package com.netscape.pkisilent.common;
 
 import java.io.BufferedReader;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
 
 import netscape.security.x509.CertificateSerialNumber;
@@ -162,10 +163,11 @@ public class Utilities {
      */
     public String getcertfromfile(String filename) {
         StringBuffer tempBuffer = new StringBuffer();
-
+        BufferedReader in = null;
+        FileInputStream fis = null;
         try {
-            FileInputStream fis = new FileInputStream(filename);
-            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+            fis = new FileInputStream(filename);
+            in = new BufferedReader(new InputStreamReader(fis));
 
             String temp;
             while ((temp = in.readLine()) != null) {
@@ -180,17 +182,33 @@ public class Utilities {
             return tempBuffer.toString();
         } catch (Exception e) {
             System.out.println("ERROR: getcertfromfile" + e.toString());
-            return null;
+            e.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
-
+        return null;
     }
 
     public String getcertfromfile_withheaders(String filename) {
         StringBuffer tempBuffer = new StringBuffer();
-
+        BufferedReader in = null;
+        FileInputStream fis = null;
         try {
-            FileInputStream fis = new FileInputStream(filename);
-            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+            fis = new FileInputStream(filename);
+            in = new BufferedReader(new InputStreamReader(fis));
 
             String temp;
             while ((temp = in.readLine()) != null) {
@@ -200,8 +218,24 @@ public class Utilities {
         } catch (Exception e) {
             System.out.println(
                     "ERROR: getcertfromfile_withheaders" + e.toString());
-            return null;
+            e.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
+        return null;
     }
 
     /*
@@ -212,10 +246,11 @@ public class Utilities {
      */
     public String getcrlfromfile(String filename) {
         StringBuffer tempBuffer = new StringBuffer();
-
+        BufferedReader in = null;
+        FileInputStream fis = null;
         try {
-            FileInputStream fis = new FileInputStream(filename);
-            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+            fis = new FileInputStream(filename);
+            in = new BufferedReader(new InputStreamReader(fis));
 
             String temp;
             while ((temp = in.readLine()) != null) {
@@ -225,9 +260,26 @@ public class Utilities {
             return tempBuffer.toString();
         } catch (Exception e) {
             System.out.println("ERROR: getcrlfromfile" + e.toString());
-            return null;
+            e.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    System.out.println("ERROR: Unable to close the input reader");
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
+        return null;
     }
 
     /*
@@ -238,10 +290,11 @@ public class Utilities {
      */
     public String getcafromfile(String filename) {
         StringBuffer tempBuffer = new StringBuffer();
-
+        BufferedReader in = null;
+        FileInputStream fis = null;
         try {
-            FileInputStream fis = new FileInputStream(filename);
-            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+            fis = new FileInputStream(filename);
+            in = new BufferedReader(new InputStreamReader(fis));
 
             String temp;
             while ((temp = in.readLine()) != null) {
@@ -251,9 +304,25 @@ public class Utilities {
             return tempBuffer.toString();
         } catch (Exception e) {
             System.out.println("ERROR: getcafromfile" + e.toString());
-            return null;
+            e.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    System.out.println("ERROR: Unable to close the input reader");
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
-
+        return null;
     }
 
     /*
diff --git a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
index 01ee0f251bed289000d2af865c434428fd084497..8ba9e522bb53edb1c66f01ca33c465e8092bbfbf 100644
--- a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
+++ b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
@@ -192,7 +192,11 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
 
         boolean st = true;
         HTTPResponse hr = null;
-
+        PrintStream ps = null;
+        SSLSocket socket = null;
+        Socket js = null;
+        OutputStream rawos = null;
+        BufferedOutputStream os = null;
         try {
 
             System.out.println("#############################################");
@@ -210,8 +214,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             // Client Cert for Auth is set here
             certSelectionCallback.setClientCert(client_cert);
 
-            Socket js = new Socket(InetAddress.getByName(hostname), port);
-            SSLSocket socket = new SSLSocket(js, hostname, approvalCallback,
+            js = new Socket(InetAddress.getByName(hostname), port);
+            socket = new SSLSocket(js, hostname, approvalCallback,
                         certSelectionCallback);
             disableSSL2(socket);
             setCipherPref(socket);
@@ -225,9 +229,9 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
                                 "/" + url +
                                 "?" + query);
 
-            OutputStream rawos = socket.getOutputStream();
-            BufferedOutputStream os = new BufferedOutputStream(rawos);
-            PrintStream ps = new PrintStream(os);
+            rawos = socket.getOutputStream();
+            os = new BufferedOutputStream(rawos);
+            ps = new PrintStream(os);
 
             ps.println("POST " + url + " HTTP/1.0");
             ps.println("Connection: Keep-Alive");
@@ -235,34 +239,44 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             ps.println("Content-length: " + query.length());
             ps.println("");
             ps.print(query);
-            ps.flush();
-            os.flush();
-
-            try {
-                hr = readResponse(socket.getInputStream());
-                hr.parseContent();
-
-            } catch (Exception e) {
-                System.out.println("Exception");
-                e.printStackTrace();
-                st = false;
-            }
-
-            socket.close();
-            os.close();
-            rawos.close();
-            ps.close();
-
-            os = null;
-            rawos = null;
-            ps = null;
-
+            hr = readResponse(socket.getInputStream());
+            hr.parseContent();
         }
 
         catch (Exception e) {
             System.err.println("Exception: Unable to Send Request:" + e);
             e.printStackTrace();
             st = false;
+        } finally {
+            if (ps != null) {
+                ps.close();
+                ps = null;
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (os != null)
+                try {
+                    os.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            if (rawos != null)
+                try {
+                    rawos.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            if (js != null)
+                try {
+                    js.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
         }
 
         if (!st)
@@ -387,7 +401,6 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
         OutputStream rawos = null;
         BufferedOutputStream os = null;
         PrintStream ps = null;
-
         try {
 
             System.out.println("#############################################");
@@ -948,7 +961,9 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
 
         boolean st = true;
         HTTPResponse hr = null;
-
+        DataOutputStream dos = null;
+        SSLSocket socket = null;
+        Socket js = null;
         try {
 
             System.out.println("#############################################");
@@ -963,8 +978,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             SSLClientCertificateSelectionCallback certSelectionCallback =
                                 new TestClientCertificateSelectionCallback();
 
-            Socket js = new Socket(InetAddress.getByName(hostname), port);
-            SSLSocket socket = new SSLSocket(js, hostname, approvalCallback,
+            js = new Socket(InetAddress.getByName(hostname), port);
+            socket = new SSLSocket(js, hostname, approvalCallback,
                         certSelectionCallback);
             setCipherPref(socket);
             disableSSL2(socket);
@@ -972,26 +987,15 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             System.out.println("Connected.");
             socket.setUseClientMode(true);
 
-            DataOutputStream dos =
-                    new DataOutputStream(socket.getOutputStream());
+            dos = new DataOutputStream(socket.getOutputStream());
             dos.writeBytes("POST /ocsp HTTP/1.0\r\n");
             dos.writeBytes("Content-length: " + data.length + "\r\n");
             dos.writeBytes("\r\n");
             dos.write(data);
             dos.writeBytes("\r\n");
             dos.flush();
-
-            try {
-                hr = readResponse(socket.getInputStream());
-                hr.parseContent();
-            } catch (Exception e) {
-                System.out.println("Exception");
-                e.printStackTrace();
-                st = false;
-            }
-
-            socket.close();
-            dos.close();
+            hr = readResponse(socket.getInputStream());
+            hr.parseContent();
 
         }
 
@@ -999,6 +1003,29 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             System.err.println("Exception: Unable to Send Request:" + e);
             e.printStackTrace();
             st = false;
+        } finally {
+            if (dos != null) {
+                try {
+                    dos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+                dos = null;
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (js != null) {
+                try {
+                    js.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         if (!st)
@@ -1015,7 +1042,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
 
         boolean st = true;
         HTTPResponse hr = null;
-
+        DataOutputStream dos = null;
+        Socket socket = null;
         try {
 
             System.out.println("#############################################");
@@ -1025,7 +1053,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             Integer x = new Integer(portnumber);
             int port = x.intValue();
 
-            Socket socket = new Socket(hostname, port);
+            socket = new Socket(hostname, port);
 
             System.out.println("Posting Query = " +
                                 "http://" + hostname +
@@ -1034,8 +1062,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
 
             System.out.println("Connected.");
 
-            DataOutputStream dos =
-                    new DataOutputStream(socket.getOutputStream());
+            dos = new DataOutputStream(socket.getOutputStream());
             dos.writeBytes("POST " + url + " HTTP/1.0\r\n");
             dos.writeBytes("Content-length: " + data.length + "\r\n");
             dos.writeBytes("\r\n");
@@ -1043,17 +1070,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             dos.writeBytes("\r\n");
             dos.flush();
 
-            try {
-                hr = readResponse(socket.getInputStream());
-                hr.parseContent();
-            } catch (Exception e) {
-                System.out.println("Exception");
-                e.printStackTrace();
-                st = false;
-            }
-
-            socket.close();
-            dos.close();
+            hr = readResponse(socket.getInputStream());
+            hr.parseContent();
 
         }
 
@@ -1061,6 +1079,22 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             System.err.println("Exception: Unable to Send Request:" + e);
             e.printStackTrace();
             st = false;
+        } finally {
+            if (dos != null) {
+                try {
+                    dos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+                dos = null;
+            }
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
 
         if (!st)
-- 
1.7.10.2

