8028431: NullPointerException in DerValue.equals(DerValue)
authorasmotrak
Tue, 21 Jan 2014 16:54:26 -0500
changeset 22563 2e5b6242e863
parent 22357 bbc7015fe560
child 22564 6fec37edea45
8028431: NullPointerException in DerValue.equals(DerValue) Reviewed-by: mullan
jdk/src/share/classes/sun/security/util/DerValue.java
jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java
jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem
--- a/jdk/src/share/classes/sun/security/util/DerValue.java	Tue Jan 21 10:52:48 2014 -0500
+++ b/jdk/src/share/classes/sun/security/util/DerValue.java	Tue Jan 21 16:54:26 2014 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -746,29 +746,21 @@
     }
 
     /**
-     * Returns true iff the other object is a DER value which
-     * is bitwise equal to this one.
-     *
-     * @param other the object being compared with this one
-     */
-    public boolean equals(Object other) {
-        if (other instanceof DerValue)
-            return equals((DerValue)other);
-        else
-            return false;
-    }
-
-    /**
      * Bitwise equality comparison.  DER encoded values have a single
      * encoding, so that bitwise equality of the encoded values is an
      * efficient way to establish equivalence of the unencoded values.
      *
      * @param other the object being compared with this one
      */
-    public boolean equals(DerValue other) {
-        if (this == other) {
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
             return true;
         }
+        if (!(o instanceof DerValue)) {
+            return false;
+        }
+        DerValue other = (DerValue) o;
         if (tag != other.tag) {
             return false;
         }
@@ -801,6 +793,7 @@
      *
      * @return printable representation of the value
      */
+    @Override
     public String toString() {
         try {
 
@@ -928,6 +921,7 @@
      *
      * @return a hashcode for this DerValue.
      */
+    @Override
     public int hashCode() {
         return toString().hashCode();
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java	Tue Jan 21 16:54:26 2014 -0500
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8028431
+ * @summary Make sure that proper CertificateException is thrown
+ *          when loading bad x509 certificate
+ * @author Artem Smotrakov
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.security.cert.CertificateException;
+
+public class X509BadCertificate {
+
+    public static void main(String[] args) throws Exception {
+        test("bad-cert-1.pem");
+    }
+
+    /**
+     * Parse X509 certificates.
+     */
+    static void test(String filename) throws Exception {
+        try {
+            System.out.println("Parse file " + filename);
+            File f = new File(System.getProperty("test.src", "."), filename);
+            try (FileInputStream fis = new FileInputStream(f)) {
+                CertificateFactory cf = CertificateFactory.getInstance("X509");
+                X509Certificate cert = (X509Certificate)
+                cf.generateCertificate(fis);
+            }
+            throw new Exception("Test failed: " +
+                "expected CertificateParsingException was not thrown");
+        } catch (CertificateException e) {
+            System.out.println("Test passed: expected exception was thrown: " +
+                e.toString());
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem	Tue Jan 21 16:54:26 2014 -0500
@@ -0,0 +1,21 @@
+-----BEGIN CERTIFICATE-----
+MIIDZzCCAk+gAwIBAgIJAJYB3qu9C2kiMA0GCSqGSIb3DQEBBQUAMEoxDTALBgNV
+BAMMBFRlc3QxDTALBgNVBAsMBEphdmExDzANBgNVBAoMBk9yYWNsZTEMMAoGA1UE
+BwwDU1BCMQswCQYDVQQGEwJSVTAeFw0xMzEyMjMwNzA4MDhaFw0yMzEyMjEwNzA4
+MDhaMEoxDTALBgNVBAMMBFRlc3QxDTALBgNVBAsMBEphdmExDzANBgNVBAoMBk9y
+YWNsZTEMMAoGA1UMBwwDU1BCMQswCQYDVQQGEwJSVTCCASIwDQYJKoZIhvcNAQEB
+BQADggEPADCCAQoCggEBAOqiCN4gFxehl547Q7/VNGbGApr+wszLdanHPucAH6Wf
+LtcRhKNUSqtBAQxEpFrTpMNEqm2GElAjiPa6m48qIjLVSvOb/9w3G/yXB8zyZbIm
+/Nfp2sT4OEaa1JSEZSpolhS4FfqYzjGQp5cn4Xn4zKjDgiceHgfLls5x2dRydQZO
+Yf91qSIioZxVHUtlo8yztkieiSaqPWt3nJ4PIwhFbsu1HVmWaYZD+nBYCKgVHqrS
+cueO98Ca4Doz73O27X1dVbQBdLS0JI7qVAG8LD388iPL8qbsOkgWPzmEQ+kLRKO4
+g7RpuwlXuwaMSh95NWaxlu4Ob6GRJQmpconYoe13+7ECAwEAAaNQME4wHQYDVR0O
+BBYEFIG8TPobXcbNbDi+zKudd9whpxoNMB8GA1UdIwQYMBaAFIG8TPobXcbNbDi+
+zKudd9whpxoNMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFRQADggEBAAynN+e7
+h+ufT5SBKN/gBuJAnF1mKIPESiipuv5KoYUGZOY8ShgYLcwY+qnbuHYFUlvq6Zns
+K4/e+x/16h32vD7dEPkNvukbvER4YJQQiN6osDfXpTPzixYftWdmtX0u8xQfwb/g
+R8DS7bazz99jVXk+jTK4yWBY+gMwEat+LyNQ5cyq8Qhi1oBKUbGRbiOts19B97fn
+Rv8TsyXN3INLGYhdVxZoD7E5tyG1ydSFmOMadulAC2epBXDHOXZnz2UWauJc0XW5
+1L/YQVri47VkdHS3tisBzELEJdLmdMDb+5tAU+lItXmTXe2/PB53WIvsEIb4t+eQ
+wY0hCj9lVJlajTQ=
+-----END CERTIFICATE-----