>From 01017313566911c6c91fa41c5ec048ec81197431 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 11 Apr 2016 18:17:30 +0200
Subject: [PATCH] Fixed exception handling in CertificateExtensions.

The CertificateExtensions.parseExtension() and some extension
classes have been modified to chain the original exception.

https://fedorahosted.org/pki/ticket/1654
---
 .../security/x509/AuthorityKeyIdentifierExtension.java  |  4 ++--
 .../security/x509/CRLDistributionPointsExtension.java   |  8 ++++----
 .../netscape/security/x509/CertificateExtensions.java   | 17 ++++++++++++-----
 .../security/x509/CertificateIssuerExtension.java       |  5 ++---
 .../netscape/security/x509/FreshestCRLExtension.java    |  8 ++++----
 .../security/x509/IssuerAlternativeNameExtension.java   |  5 ++---
 .../x509/IssuingDistributionPointExtension.java         | 17 +++++++----------
 .../security/x509/SubjectAlternativeNameExtension.java  |  2 +-
 8 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/base/util/src/netscape/security/x509/AuthorityKeyIdentifierExtension.java b/base/util/src/netscape/security/x509/AuthorityKeyIdentifierExtension.java
index ec15be4a006d9757c98e1e2084391284fd94caca..a71a3b69ae289868975044a180e89e4448d70640 100644
--- a/base/util/src/netscape/security/x509/AuthorityKeyIdentifierExtension.java
+++ b/base/util/src/netscape/security/x509/AuthorityKeyIdentifierExtension.java
@@ -100,7 +100,7 @@ public class AuthorityKeyIdentifierExtension extends Extension
                             true, TAG_NAMES), tmp1);
                 }
             } catch (Exception e) {
-                throw new IOException(e.toString());
+                throw new IOException(e);
             }
             if (serialNum != null) {
                 DerOutputStream tmp1 = new DerOutputStream();
@@ -199,7 +199,7 @@ public class AuthorityKeyIdentifierExtension extends Extension
                     opt.resetTag(DerValue.tag_Sequence);
                     names = new GeneralNames(opt);
                 } catch (GeneralNamesException e) {
-                    throw new IOException(e.toString());
+                    throw new IOException(e);
                 }
 
             } else if (opt.isContextSpecific(TAG_SERIAL_NUM) &&
diff --git a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
index 1cfb770df60b26a7a849b8bc4bea70424e9416df..0cd6505c39fb75768b7362d2bf76318fa168b0ea 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
@@ -27,13 +27,13 @@ import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
 
-import netscape.security.util.BitArray;
-import netscape.security.util.DerOutputStream;
-
 import org.mozilla.jss.asn1.ASN1Util;
 import org.mozilla.jss.asn1.InvalidBERException;
 import org.mozilla.jss.asn1.SEQUENCE;
 
+import netscape.security.util.BitArray;
+import netscape.security.util.DerOutputStream;
+
 /**
  * An extension that tells applications where to find the CRL for
  * this certificate.
@@ -95,7 +95,7 @@ public class CRLDistributionPointsExtension extends Extension
                 distributionPoints =
                         (SEQUENCE) ASN1Util.decode(seqOfCRLDP, extensionValue);
             } catch (InvalidBERException e) {
-                throw new IOException("Invalid BER-encoding: " + e.toString());
+                throw new IOException("Invalid BER-encoding: " + e, e);
             }
         } catch (IOException e) {
             System.out.println("Big error");
diff --git a/base/util/src/netscape/security/x509/CertificateExtensions.java b/base/util/src/netscape/security/x509/CertificateExtensions.java
index 3a21c2f83cf76df8f60331aeecc526298342e9b3..d76bfe196d2a1a7524e2d6015185237895418be3 100644
--- a/base/util/src/netscape/security/x509/CertificateExtensions.java
+++ b/base/util/src/netscape/security/x509/CertificateExtensions.java
@@ -88,12 +88,19 @@ public class CertificateExtensions extends Vector<Extension>
                 map.put(certExt.getName(), (Extension) certExt);
                 addElement((Extension) certExt);
             }
-        } catch (NoSuchMethodException nosuch) {
-            throw new IOException(nosuch.toString());
-        } catch (InvocationTargetException invk) {
-            throw new IOException(invk.getTargetException().toString());
+
+        } catch (NoSuchMethodException e) {
+            throw new IOException(e);
+
+        } catch (InvocationTargetException e) {
+            Throwable t = e.getTargetException();
+            if (t instanceof IOException) {
+                throw (IOException)t;
+            }
+            throw new IOException(t);
+
         } catch (Exception e) {
-            throw new IOException(e.toString());
+            throw new IOException(e);
         }
     }
 
diff --git a/base/util/src/netscape/security/x509/CertificateIssuerExtension.java b/base/util/src/netscape/security/x509/CertificateIssuerExtension.java
index 4f20cb685a726ddec7bb19645c9d524ec3af0ca1..ae4c2093a7cdd39fe43233ad50f1ce8ab44ddf53 100644
--- a/base/util/src/netscape/security/x509/CertificateIssuerExtension.java
+++ b/base/util/src/netscape/security/x509/CertificateIssuerExtension.java
@@ -73,7 +73,7 @@ public class CertificateIssuerExtension extends Extension
         try {
             names.encode(os);
         } catch (GeneralNamesException e) {
-            throw new IOException(e.toString());
+            throw new IOException(e);
         }
         this.extensionValue = os.toByteArray();
     }
@@ -139,8 +139,7 @@ public class CertificateIssuerExtension extends Extension
         try {
             names = new GeneralNames(val);
         } catch (GeneralNamesException e) {
-            throw new IOException("CertificateIssuerExtension: " +
-                                  e.toString());
+            throw new IOException("CertificateIssuerExtension: " + e, e);
         }
     }
 
diff --git a/base/util/src/netscape/security/x509/FreshestCRLExtension.java b/base/util/src/netscape/security/x509/FreshestCRLExtension.java
index 222a5e9594539395dd816c5bd73eee2a8b0fac05..c084956b5ecdcbe697ee78cb02bb81186b3a6d1c 100644
--- a/base/util/src/netscape/security/x509/FreshestCRLExtension.java
+++ b/base/util/src/netscape/security/x509/FreshestCRLExtension.java
@@ -27,13 +27,13 @@ import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
 
-import netscape.security.util.BitArray;
-import netscape.security.util.DerOutputStream;
-
 import org.mozilla.jss.asn1.ASN1Util;
 import org.mozilla.jss.asn1.InvalidBERException;
 import org.mozilla.jss.asn1.SEQUENCE;
 
+import netscape.security.util.BitArray;
+import netscape.security.util.DerOutputStream;
+
 /**
  * An extension that tells applications where to find
  * the latest (freshest) delta CRL for this certificate
@@ -115,7 +115,7 @@ public class FreshestCRLExtension extends Extension
                 distributionPoints =
                         (SEQUENCE) ASN1Util.decode(seqOfCRLDP, extensionValue);
             } catch (InvalidBERException e) {
-                throw new IOException("Invalid BER-encoding: " + e.toString());
+                throw new IOException("Invalid BER-encoding: " + e, e);
             }
         } catch (IOException e) {
             System.out.println("Big error");
diff --git a/base/util/src/netscape/security/x509/IssuerAlternativeNameExtension.java b/base/util/src/netscape/security/x509/IssuerAlternativeNameExtension.java
index 62b3ecf94d1118d1b1b4867dec9579a2dd20df0e..469e4722b6064a81777cc26cc0afbda46b1f6c17 100644
--- a/base/util/src/netscape/security/x509/IssuerAlternativeNameExtension.java
+++ b/base/util/src/netscape/security/x509/IssuerAlternativeNameExtension.java
@@ -71,7 +71,7 @@ public class IssuerAlternativeNameExtension
         try {
             names.encode(os);
         } catch (GeneralNamesException e) {
-            throw new IOException(e.toString());
+            throw new IOException(e);
         }
         this.extensionValue = os.toByteArray();
     }
@@ -136,8 +136,7 @@ public class IssuerAlternativeNameExtension
         try {
             names = new GeneralNames(val);
         } catch (GeneralNamesException e) {
-            throw new IOException("IssuerAlternativeNameExtension"
-                                  + e.toString());
+            throw new IOException("IssuerAlternativeNameExtension: " + e, e);
         }
     }
 
diff --git a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
index b78a891c230e74d5105bbc31770918729d5c20bb..d65dc44f0aefccd87d5db18253c93e252eb347a6 100644
--- a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
+++ b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
@@ -26,12 +26,12 @@ import java.security.cert.CertificateException;
 import java.util.Enumeration;
 import java.util.Vector;
 
+import org.mozilla.jss.asn1.ASN1Util;
+
 import netscape.security.util.BitArray;
 import netscape.security.util.DerOutputStream;
 import netscape.security.util.DerValue;
 
-import org.mozilla.jss.asn1.ASN1Util;
-
 /**
  * A critical CRL extension that identifies the CRL distribution point
  * for a particular CRL
@@ -162,11 +162,9 @@ public class IssuingDistributionPointExtension extends Extension
                                                     issuingDistributionPoint.setFullName(fullName);
                                                 }
                                             } catch (GeneralNamesException e) {
-                                                throw new IOException("Invalid encoding of IssuingDistributionPoint "
-                                                        + e);
+                                                throw new IOException("Invalid encoding of IssuingDistributionPoint " + e, e);
                                             } catch (IOException e) {
-                                                throw new IOException("Invalid encoding of IssuingDistributionPoint "
-                                                        + e);
+                                                throw new IOException("Invalid encoding of IssuingDistributionPoint " + e, e);
                                             }
                                         } else {
                                             throw new IOException("Invalid encoding of IssuingDistributionPoint");
@@ -182,8 +180,7 @@ public class IssuingDistributionPointExtension extends Extension
                                                     issuingDistributionPoint.setRelativeName(relativeName);
                                                 }
                                             } catch (IOException e) {
-                                                throw new IOException("Invalid encoding of IssuingDistributionPoint "
-                                                        + e);
+                                                throw new IOException("Invalid encoding of IssuingDistributionPoint " + e, e);
                                             }
                                         } else {
                                             throw new IOException("Invalid encoding of IssuingDistributionPoint");
@@ -203,7 +200,7 @@ public class IssuingDistributionPointExtension extends Extension
                                     @SuppressWarnings("unused")
                                     byte[] a = reasons.toByteArray(); // check for errors
                                 } catch (IOException e) {
-                                    throw new IOException("Invalid encoding of IssuingDistributionPoint " + e);
+                                    throw new IOException("Invalid encoding of IssuingDistributionPoint " + e, e);
                                 }
 
                             } else {
@@ -218,7 +215,7 @@ public class IssuingDistributionPointExtension extends Extension
                                         issuingDistributionPoint.setIndirectCRL(b);
                                     }
                                 } catch (IOException e) {
-                                    throw new IOException("Invalid encoding of IssuingDistributionPoint " + e);
+                                    throw new IOException("Invalid encoding of IssuingDistributionPoint " + e, e);
                                 }
                             }
                         } else {
diff --git a/base/util/src/netscape/security/x509/SubjectAlternativeNameExtension.java b/base/util/src/netscape/security/x509/SubjectAlternativeNameExtension.java
index cdeff8f651c0721f9edbc5904345a39709e52bdc..d96c821604308c11723644e8842e1dcc6f224034 100644
--- a/base/util/src/netscape/security/x509/SubjectAlternativeNameExtension.java
+++ b/base/util/src/netscape/security/x509/SubjectAlternativeNameExtension.java
@@ -139,7 +139,7 @@ public class SubjectAlternativeNameExtension extends Extension
         try {
             names = new GeneralNames(val);
         } catch (GeneralNamesException e) {
-            throw new IOException("SubjectAlternativeName: " + e);
+            throw new IOException("SubjectAlternativeName: " + e, e);
         }
     }
 
-- 
2.4.11

