From f85948cc0c53fd79b819da72fe1b28d0c2b98155 Mon Sep 17 00:00:00 2001 From: Artem Smotrakov Date: Tue, 21 Jan 2014 16:54:26 -0500 Subject: [PATCH] 8028431: NullPointerException in DerValue.equals(DerValue) Reviewed-by: mullan --- .../classes/sun/security/util/DerValue.java | 26 +++----- .../X509Certificate/X509BadCertificate.java | 63 +++++++++++++++++++ .../cert/X509Certificate/bad-cert-1.pem | 21 +++++++ 3 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java create mode 100644 jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem diff --git a/jdk/src/share/classes/sun/security/util/DerValue.java b/jdk/src/share/classes/sun/security/util/DerValue.java index beb51c19ae3..abb45cc6558 100644 --- a/jdk/src/share/classes/sun/security/util/DerValue.java +++ b/jdk/src/share/classes/sun/security/util/DerValue.java @@ -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 @@ -745,19 +745,6 @@ public class DerValue { return buffer.getGeneralizedTime(data.available()); } - /** - * 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 @@ -765,10 +752,15 @@ public class DerValue { * * @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 @@ public class DerValue { * * @return printable representation of the value */ + @Override public String toString() { try { @@ -928,6 +921,7 @@ public class DerValue { * * @return a hashcode for this DerValue. */ + @Override public int hashCode() { return toString().hashCode(); } diff --git a/jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java b/jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java new file mode 100644 index 00000000000..b2a6c6923fa --- /dev/null +++ b/jdk/test/java/security/cert/X509Certificate/X509BadCertificate.java @@ -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()); + } + } +} diff --git a/jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem b/jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem new file mode 100644 index 00000000000..0e6c355d0fd --- /dev/null +++ b/jdk/test/java/security/cert/X509Certificate/bad-cert-1.pem @@ -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-----