Path: blob/master/test/jdk/java/security/cert/CertPathBuilder/selfIssued/KeyUsageMatters.java
41161 views
/*1* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223//24// Security properties, once set, cannot revert to unset. To avoid25// conflicts with tests running in the same VM isolate this test by26// running it in otherVM mode.27//2829/**30* @test31* @bug 6852744 813348932* @summary PIT b61: PKI test suite fails because self signed certificates33* are being rejected34* @modules java.base/sun.security.util35* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters subca36* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters subci37* @run main/othervm -Djava.security.debug=certpath KeyUsageMatters alice38* @author Xuelei Fan39*/4041import java.io.*;42import java.net.SocketException;43import java.util.*;44import java.security.Security;45import java.security.cert.*;46import java.security.cert.CertPathValidatorException.BasicReason;47import sun.security.util.DerInputStream;4849/**50* KeyUsage extension plays a important rule during looking for the issuer51* of a certificate or CRL. A certificate issuer should have the keyCertSign52* bit set, and a CRL issuer should have the cRLSign bit set.53*54* Sometime, a delegated CRL issuer would also have the keyCertSign bit set,55* as would be troublesome to find the proper CRL issuer during certificate56* path build if the delegated CRL issuer is a self-issued certificate, for57* it is hard to identify it from its issuer by the "issuer" field only.58*59* The fix of 6852744 should addresses above issue, and allow a delegated CRL60* issuer to have keyCertSign bit set.61*62* In the test case, the delegated CRL issuers have cRLSign bit set only, and63* the CAs have the keyCertSign bit set only, it is expected to work before64* and after the bug fix of 6852744.65*/66public final class KeyUsageMatters {6768// the trust anchor69static String selfSignedCertStr =70"-----BEGIN CERTIFICATE-----\n" +71"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +72"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzJaFw0zMDA0MDcwMjI0MzJa\n" +73"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +74"AQUAA4GNADCBiQKBgQC4OTag24sTxL2tXTNuvpmUEtdxrYAZoFsslFQ60T+WD9wQ\n" +75"Jeiw87FSPsR2vxRuv0j8DNm2a4h7LNNIFcLurfNldbz5pvgZ7VqdbbUMPE9qP85n\n" +76"jgDl4woyRTSUeRI4A7O0CO6NpES21dtbdhroWQrEkHxpnrDPxsxrz5gf2m3gqwID\n" +77"AQABo4GJMIGGMB0GA1UdDgQWBBSCJd0hpl5PdAD9IZS+Hzng4lXLGzBHBgNVHSME\n" +78"QDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +79"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAgQw\n" +80"DQYJKoZIhvcNAQEEBQADgYEAluy6HIjWcq009lTLmhp+Np6dxU78pInBK8RZkza0\n" +81"484qGaxFGD3UGyZkI5uWmsH2XuMbuox5khfIq6781gmkPBHXBIEtJN8eLusOHEye\n" +82"iE8h7WI+N3qa6Pj56WionMrioqC/3X+b06o147bbhx8U0vkYv/HyPaITOFfMXTdz\n" +83"Vjw=\n" +84"-----END CERTIFICATE-----";8586// the sub-ca87static String subCaCertStr =88"-----BEGIN CERTIFICATE-----\n" +89"MIICUDCCAbmgAwIBAgIBAzANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +90"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzRaFw0yOTAxMTIwMjI0MzRa\n" +91"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +92"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiAJnAQW2ad3ZMKUhSJVZj\n" +93"8pBqxTcHSTwAVguQkDglsN/OIwUpvR5Jgp3lpRWUEt6idEp0FZzORpvtjt3pr5MG\n" +94"Eg2CDptekC5BSPS+fIAIKlncB3HwOiFFhH6b3wTydDCdEd2fvsi4QMOSVrIYMeA8\n" +95"P/mCz6kRhfUQPE0CMmOUewIDAQABo4GJMIGGMB0GA1UdDgQWBBT0/nNP8WpyxmYr\n" +96"IBp4tN8y08jw2jBHBgNVHSMEQDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEw\n" +97"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +98"AwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcNAQEEBQADgYEAS9PzI6B39R/U9fRj\n" +99"UExzN1FXNP5awnAPtiv34kSCL6n6MryqkfG+8aaAOdZsSjmTylNFaF7cW/Xp1VBF\n" +100"hq0bg/SbEAbK7+UwL8GSC3crhULHLbh+1iFdVTEwxCw5YmB8ji3BaZ/WKW/PkjCZ\n" +101"7cXP6VDeZMG6oRQ4hbOcixoFPXo=\n" +102"-----END CERTIFICATE-----";103104// a delegated CRL issuer, it's a self-issued certificate of trust anchor105static String topCrlIssuerCertStr =106"-----BEGIN CERTIFICATE-----\n" +107"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +108"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +109"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +110"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +111"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +112"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +113"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +114"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +115"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +116"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +117"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +118"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +119"-----END CERTIFICATE-----";120121// a delegated CRL issuer, it's a self-issued certificate of sub-ca122static String subCrlIssuerCertStr =123"-----BEGIN CERTIFICATE-----\n" +124"MIICPTCCAaagAwIBAgIBBDANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +125"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzRaFw0yOTAxMTIwMjI0MzRa\n" +126"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +127"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWUtDQx2MB/7arDiquMJyd\n" +128"LWwSg6p8sg5z6wKrC1v47MT4DBhFX+0RUgTMUdQgYpgxGpczn+6y4zfV76064S0N\n" +129"4L/IQ+SunTW1w4yRGjB+xkyyJmWAqijG1nr+Dgkv5nxPI+9Er5lHcoVWVMEcvvRm\n" +130"6jIBQdldVlSgv+VgUnFm5wIDAQABo3cwdTAdBgNVHQ4EFgQUkV3Qqtk7gIot9n60\n" +131"jX6dloxrfMEwRwYDVR0jBEAwPoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8x\n" +132"CzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjAN\n" +133"BgkqhkiG9w0BAQQFAAOBgQADu4GM8EdmIKhC7FRvk5jF90zfvZ38wbXBzCjKI4jX\n" +134"QJrhne1bfyeNNm5c1w+VKidT+XzBzBGH7ZqYzoZmzRIfcbLKX2brEBKiukeeAyL3\n" +135"bctQtbp19tX+uu2dQberD188AAysKTkHcJUV+rRsTwVJ9vcYKxoRxKk8DhH7ZS3M\n" +136"rg==\n" +137"-----END CERTIFICATE-----";138139// the target EE certificate140static String targetCertStr =141"-----BEGIN CERTIFICATE-----\n" +142"MIICNzCCAaCgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQ\n" +143"MA4GA1UEChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMTAeFw0wOTA0MjcwMjI0\n" +144"MzZaFw0yOTAxMTIwMjI0MzZaMEExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFt\n" +145"cGxlMRAwDgYDVQQLEwdDbGFzcy0xMQ4wDAYDVQQDEwVBbGljZTCBnzANBgkqhkiG\n" +146"9w0BAQEFAAOBjQAwgYkCgYEAvYSaU3oiE4Pxp/aUIXwMqOwSiWkZ+O3aTu13hRtK\n" +147"ZyR+Wtj63IuvaigAC4uC+zBypF93ThjwCzVR2qKDQaQzV8CLleO96gStt7Y+i3G2\n" +148"V3IUGgrVCqeK7N6nNYu0wW84sibcPqG/TIy0UoaQMqgB21xtRF+1DUVlFh4Z89X/\n" +149"pskCAwEAAaNPME0wCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBSynMEdcal/e9TmvlNE\n" +150"4suXGA4+hjAfBgNVHSMEGDAWgBT0/nNP8WpyxmYrIBp4tN8y08jw2jANBgkqhkiG\n" +151"9w0BAQQFAAOBgQB/jru7E/+piSmUwByw5qbZsoQZVcgR97pd2TErNJpJMAX2oIHR\n" +152"wJH6w4NuYs27+fEAX7wK4whc6EUH/w1SI6o28F2rG6HqYQPPZ2E2WqwbBQL9nYE3\n" +153"Vfzu/G9axTUQXFbf90h80UErA+mZVxqc2xtymLuH0YEaMZImtRZ2MXHfXg==\n" +154"-----END CERTIFICATE-----";155156// CRL issued by the delegated CRL issuer, topCrlIssuerCertStr157static String topCrlStr =158"-----BEGIN X509 CRL-----\n" +159"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +160"ChMHRXhhbXBsZRcNMDkwNDI3MDIzODA0WhcNMjgwNjI2MDIzODA0WjAiMCACAQUX\n" +161"DTA5MDQyNzAyMzgwMFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJ\n" +162"KoZIhvcNAQEEBQADgYEAoarfzXEtw3ZDi4f9U8eSvRIipHSyxOrJC7HR/hM5VhmY\n" +163"CErChny6x9lBVg9s57tfD/P9PSzBLusCcHwHMAbMOEcTltVVKUWZnnbumpywlYyg\n" +164"oKLrE9+yCOkYUOpiRlz43/3vkEL5hjIKMcDSZnPKBZi1h16Yj2hPe9GMibNip54=\n" +165"-----END X509 CRL-----";166167// CRL issued by the delegated CRL issuer, subCrlIssuerCertStr168static String subCrlStr =169"-----BEGIN X509 CRL-----\n" +170"MIIBLTCBlwIBATANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +171"ChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMRcNMDkwNDI3MDIzODA0WhcNMjgw\n" +172"NjI2MDIzODA0WjAiMCACAQQXDTA5MDQyNzAyMzgwMVowDDAKBgNVHRUEAwoBBKAO\n" +173"MAwwCgYDVR0UBAMCAQIwDQYJKoZIhvcNAQEEBQADgYEAeS+POqYEIHIIJcsLxuUr\n" +174"aJFzQ/ujH0QmnyMNEL3Uavyq4VQuAahF+w6aTPb5UBzms0uX8NAvD2vNoUJvmJOX\n" +175"nGKuq4Q1DFj82E7/9d25nXdWGOmFvFCRVO+St2Xe5n8CJuZNBiz388FDSIOiFSCa\n" +176"ARGr6Qu68MYGtLMC6ZqP3u0=\n" +177"-----END X509 CRL-----";178179private static Set<TrustAnchor> generateTrustAnchors()180throws CertificateException {181// generate certificate from cert string182CertificateFactory cf = CertificateFactory.getInstance("X.509");183184ByteArrayInputStream is =185new ByteArrayInputStream(selfSignedCertStr.getBytes());186Certificate selfSignedCert = cf.generateCertificate(is);187188// generate a trust anchor189TrustAnchor anchor =190new TrustAnchor((X509Certificate)selfSignedCert, null);191192return Collections.singleton(anchor);193}194195private static CertStore generateCertificateStore() throws Exception {196Collection entries = new HashSet();197198// generate certificate from certificate string199CertificateFactory cf = CertificateFactory.getInstance("X.509");200201ByteArrayInputStream is;202203is = new ByteArrayInputStream(targetCertStr.getBytes());204Certificate cert = cf.generateCertificate(is);205entries.add(cert);206207is = new ByteArrayInputStream(subCaCertStr.getBytes());208cert = cf.generateCertificate(is);209entries.add(cert);210211is = new ByteArrayInputStream(selfSignedCertStr.getBytes());212cert = cf.generateCertificate(is);213entries.add(cert);214215is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());216cert = cf.generateCertificate(is);217entries.add(cert);218219is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());220cert = cf.generateCertificate(is);221entries.add(cert);222223// generate CRL from CRL string224is = new ByteArrayInputStream(topCrlStr.getBytes());225Collection mixes = cf.generateCRLs(is);226entries.addAll(mixes);227228is = new ByteArrayInputStream(subCrlStr.getBytes());229mixes = cf.generateCRLs(is);230entries.addAll(mixes);231232return CertStore.getInstance("Collection",233new CollectionCertStoreParameters(entries));234}235236private static X509CertSelector generateSelector(String name)237throws Exception {238X509CertSelector selector = new X509CertSelector();239240// generate certificate from certificate string241CertificateFactory cf = CertificateFactory.getInstance("X.509");242ByteArrayInputStream is = null;243if (name.equals("subca")) {244is = new ByteArrayInputStream(subCaCertStr.getBytes());245} else if (name.equals("subci")) {246is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());247} else {248is = new ByteArrayInputStream(targetCertStr.getBytes());249}250251X509Certificate target = (X509Certificate)cf.generateCertificate(is);252byte[] extVal = target.getExtensionValue("2.5.29.14");253if (extVal != null) {254DerInputStream in = new DerInputStream(extVal);255byte[] subjectKID = in.getOctetString();256selector.setSubjectKeyIdentifier(subjectKID);257} else {258// unlikely to happen.259throw new Exception("unexpected certificate: no SKID extension");260}261262return selector;263}264265private static boolean match(String name, Certificate cert)266throws Exception {267X509CertSelector selector = new X509CertSelector();268269// generate certificate from certificate string270CertificateFactory cf = CertificateFactory.getInstance("X.509");271ByteArrayInputStream is = null;272if (name.equals("subca")) {273is = new ByteArrayInputStream(subCaCertStr.getBytes());274} else if (name.equals("subci")) {275is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());276} else {277is = new ByteArrayInputStream(targetCertStr.getBytes());278}279X509Certificate target = (X509Certificate)cf.generateCertificate(is);280281return target.equals(cert);282}283284285public static void main(String[] args) throws Exception {286// MD5 is used in this test case, don't disable MD5 algorithm.287Security.setProperty(288"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");289290CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");291292X509CertSelector selector = generateSelector(args[0]);293294Set<TrustAnchor> anchors = generateTrustAnchors();295CertStore certs = generateCertificateStore();296297298PKIXBuilderParameters params =299new PKIXBuilderParameters(anchors, selector);300params.addCertStore(certs);301params.setRevocationEnabled(true);302params.setDate(new Date(109, 5, 1)); // 2009-05-01303Security.setProperty("ocsp.enable", "false");304System.setProperty("com.sun.security.enableCRLDP", "true");305306PKIXCertPathBuilderResult result =307(PKIXCertPathBuilderResult)builder.build(params);308309if (!match(args[0], result.getCertPath().getCertificates().get(0))) {310throw new Exception("unexpected certificate");311}312}313}314315316