Path: blob/master/test/jdk/java/security/cert/CertPathBuilder/selfIssued/StatusLoopDependency.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 685274432* @summary PIT b61: PKI test suite fails because self signed certificates33* are being rejected34* @modules java.base/sun.security.util35* @run main/othervm StatusLoopDependency subca36* @run main/othervm StatusLoopDependency subci37* @run main/othervm StatusLoopDependency 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* In the test case, the delegated CRL issuers have keyCertSign bit set, and60* the CAs have the cRLSign bit set also. If we cannot identify the delegated61* CRL issuer from its issuer, there is a potential loop to find the correct62* CRL.63*64* And when revocation enabled, needs to check the status of the delegated65* CRL issuers. If the delegated CRL issuer issues itself status, there is66* a potential loop to verify the CRL and check the status of delegated CRL67* issuer.68*69* The fix of 6852744 should addresses above issues.70*/71public final class StatusLoopDependency {7273// the trust anchor74static String selfSignedCertStr =75"-----BEGIN CERTIFICATE-----\n" +76"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +77"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA2MjgxMzMyMThaFw0zMDA2MDgxMzMyMTha\n" +78"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +79"AQUAA4GNADCBiQKBgQDInJhXi0655bPXAVkz1n5I6fAcZejzPnOPuwq3hU3OxFw8\n" +80"81Uf6o9oKI1h4w4XAD8u1cUNOgiX+wPwojronlp68bIfO6FVhNf287pLtLhNJo+7\n" +81"m6Qxw3ymFvEKy+PVj20CHSggdKHxUa4MBZBmHMFNBuxfYmjwzn+yTMmCCXOvSwID\n" +82"AQABo4GJMIGGMB0GA1UdDgQWBBSQ52Dpau+gtL+Kc31dusYnKj16ZTBHBgNVHSME\n" +83"QDA+gBSQ52Dpau+gtL+Kc31dusYnKj16ZaEjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +84"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYw\n" +85"DQYJKoZIhvcNAQEEBQADgYEAjBt6ea65HCqbGsS2rs/HhlGusYXtThRVC5vwXSey\n" +86"ZFYwSgukuq1KDzckqZFu1meNImEwdZjwxdN0e2p/nVREPC42rZliSj6V1ThayKXj\n" +87"DWEZW1U5aR8T+3NYfDrdKcJGx4Hzfz0qKz1j4ssV1M9ptJxYYv4y2Da+592IN1S9\n" +88"v/E=\n" +89"-----END CERTIFICATE-----";9091// the sub-ca92static String subCaCertStr =93"-----BEGIN CERTIFICATE-----\n" +94"MIICUDCCAbmgAwIBAgIBAzANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +95"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA2MjgxMzMyMjRaFw0yOTAzMTUxMzMyMjRa\n" +96"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +97"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPFv24SK78VI0gWlyIrq/X\n" +98"srl1431K5hJJxMYZtaQunyPmrYg3oI9KvKFykxnR0N4XDPaIi75p9dXGppVu80BA\n" +99"+csvIPBwlBQoNmKDQWTziDOqfK4tE+IMuL/Y7pxnH6CDMY7VGpvatty2zcmH+m/v\n" +100"E/n+HPyeELJQT2rT/3T+7wIDAQABo4GJMIGGMB0GA1UdDgQWBBRidC8Dt3dBzYES\n" +101"KpR2tR560sZ0+zBHBgNVHSMEQDA+gBSQ52Dpau+gtL+Kc31dusYnKj16ZaEjpCEw\n" +102"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +103"AwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEEBQADgYEAMeMKqrMr5d3eTQsv\n" +104"MYOD15Dl3THQGLAa4ad5Eyq5/1eUeEOpztzCgDfi0iPD8YCubIEVasBTSqTiGXqb\n" +105"RpGuPHOwwfWvHrTeHSludiFBAUiKj7aEV+oQa0FBn4U4TT8HA62HQ93FhzTDI3jP\n" +106"iil34GktVl6gfMKGzUEW/Dh8OM4=\n" +107"-----END CERTIFICATE-----";108109// a delegated CRL issuer, it's a self-issued certificate of trust anchor110static String topCrlIssuerCertStr =111"-----BEGIN CERTIFICATE-----\n" +112"MIICPjCCAaegAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +113"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA2MjgxMzMyMjNaFw0yOTAzMTUxMzMyMjNa\n" +114"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +115"AQUAA4GNADCBiQKBgQC99u93trf+WmpfiqunJy/P31ej1l4rESxft2JSGNjKuLFN\n" +116"/BO3SAugGJSkCARAwXjB0c8eeXhXWhVVWdNpbKepRJTxrjDfnFIavLgtUvmFwn/3\n" +117"hPXe+RQeA8+AJ99Y+o+10kY8JAZLa2j93C2FdmwOjUbo8aIz85yhbiV1tEDjLwID\n" +118"AQABo4GJMIGGMB0GA1UdDgQWBBSyFyA3XWLbdL6W6hksmBn7RKsQmDBHBgNVHSME\n" +119"QDA+gBSQ52Dpau+gtL+Kc31dusYnKj16ZaEjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +120"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAQYw\n" +121"DQYJKoZIhvcNAQEEBQADgYEAHTm8aRTeakgCfEBCgSWK9wvMW1c18ANGMm8OFDBk\n" +122"xabVy9BT0MVFHlaneh89oIxTZN0FMTpg21GZMAvIzhEt7DGdO7HLsW7JniN7/OZ0\n" +123"rACmpK5frmZrLS03zUm8c+rTbazNfYLoZVG3/mDZbKIi+4y8IGnFcgLVsHsYoBNP\n" +124"G0c=\n" +125"-----END CERTIFICATE-----";126127// a delegated CRL issuer, it's a self-issued certificate of sub-ca128static String subCrlIssuerCertStr =129"-----BEGIN CERTIFICATE-----\n" +130"MIICUDCCAbmgAwIBAgIBBDANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +131"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA2MjgxMzMyMjdaFw0yOTAzMTUxMzMyMjda\n" +132"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +133"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+8AcLJtGAVUWvv3ifcyQw\n" +134"OGqwzcPrBw/XCs6vTMlcdtFzcH1M+Z3/QHN9+5VT1gqeTIZ+b8g9005Og3XKy/HX\n" +135"obXZeLv20VZsr+jm52ySghEYOVCTJ9OyFOAp5adp6nf0cA66Feh3LsmVhpTEcDOG\n" +136"GnyntQm0DBYxRoOT/GBlvQIDAQABo4GJMIGGMB0GA1UdDgQWBBSRWhMuZLQoHSDN\n" +137"xhxr+vdDmfAY8jBHBgNVHSMEQDA+gBSQ52Dpau+gtL+Kc31dusYnKj16ZaEjpCEw\n" +138"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +139"AwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEEBQADgYEAMIDZLdOLFiPyS1bh\n" +140"Ch4eUYHT+K1WG93skbga3kVYg3GSe+gctwkKwKK13bwfi8zc7wwz6MtmQwEYhppc\n" +141"pKKKEwi5QirBCP54rihLCvRQaj6ZqUJ6VP+zPAqHYMDbzlBbHtVF/1lQUP30I6SV\n" +142"Fu987DvLmZ2GuQA9FKJsnlD9pbU=\n" +143"-----END CERTIFICATE-----";144145// the target EE certificate146static String targetCertStr =147"-----BEGIN CERTIFICATE-----\n" +148"MIICNzCCAaCgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQ\n" +149"MA4GA1UEChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMTAeFw0wOTA2MjgxMzMy\n" +150"MzBaFw0yOTAzMTUxMzMyMzBaMEExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFt\n" +151"cGxlMRAwDgYDVQQLEwdDbGFzcy0xMQ4wDAYDVQQDEwVBbGljZTCBnzANBgkqhkiG\n" +152"9w0BAQEFAAOBjQAwgYkCgYEA7wnsvR4XEOfVznf40l8ClLod+7L0y2/+smVV+GM/\n" +153"T1/QF/stajAJxXNy08gK00WKZ6ruTHhR9vh/Z6+EQM2RZDCpU0A7LPa3kLE/XTmS\n" +154"1MLDu8ntkdlpURpvhdDWem+rl2HU5oZgzV8Jkcov9vXuSjqEDfr45FlPuV40T8+7\n" +155"cxsCAwEAAaNPME0wCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBSBwsAhi6Z1kriOs3ty\n" +156"uSIujv9a3DAfBgNVHSMEGDAWgBRidC8Dt3dBzYESKpR2tR560sZ0+zANBgkqhkiG\n" +157"9w0BAQQFAAOBgQDEiBqd5AMy2SQopFaS3dYkzj8MHlwtbCSoNVYkOfDnewcatrbk\n" +158"yFcp6FX++PMdOQFHWvvnDdkCUAzZQp8kCkF9tGLVLBtOK7XxQ1us1LZym7kOPzsd\n" +159"G93Dcf0U1JRO77juc61Br5paAy8Bok18Y/MeG7uKgB2MAEJYKhGKbCrfMw==\n" +160"-----END CERTIFICATE-----";161162// CRL issued by the delegated CRL issuer, topCrlIssuerCertStr163static String topCrlStr =164"-----BEGIN X509 CRL-----\n" +165"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +166"ChMHRXhhbXBsZRcNMDkwNjI4MTMzMjM4WhcNMjgwODI3MTMzMjM4WjAiMCACAQUX\n" +167"DTA5MDYyODEzMzIzN1owDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQEwDQYJ\n" +168"KoZIhvcNAQEEBQADgYEAVUIeu2x7ZwsliafoCBOg+u8Q4S/VFfTe/SQnRyTM3/V1\n" +169"v+Vn5Acc7eo8Rh4AHcnFFbLNk38n6lllov/CaVR0IPZ6hnrNHVa7VYkNlRAwV2aN\n" +170"GUUhkMMOLVLnN25UOrN9J637SHmRE6pB+TRMaEQ73V7UNlWxuSMK4KofWen0A34=\n" +171"-----END X509 CRL-----";172173// CRL issued by the delegated CRL issuer, subCrlIssuerCertStr174static String subCrlStr =175"-----BEGIN X509 CRL-----\n" +176"MIIBLTCBlwIBATANBgkqhkiG9w0BAQQFADAxMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +177"ChMHRXhhbXBsZTEQMA4GA1UECxMHQ2xhc3MtMRcNMDkwNjI4MTMzMjQzWhcNMjgw\n" +178"ODI3MTMzMjQzWjAiMCACAQQXDTA5MDYyODEzMzIzOFowDDAKBgNVHRUEAwoBBKAO\n" +179"MAwwCgYDVR0UBAMCAQEwDQYJKoZIhvcNAQEEBQADgYEACQZEf6ydb3fKTMPJ8DBO\n" +180"oo630MsrT3P0x0AC4+aQOueCBaGpNqW/H379uZxXAad7yr+aXUBwaeBMYVKUbwOe\n" +181"5TrN5QWPe2eCkU+MSQvh1SHASDDMH4jhWFMRdO3aPMDKKPlO/Q3s0G72eD7Zo5dr\n" +182"N9AvUXxGxU4DruoJuFPcrCI=\n" +183"-----END X509 CRL-----";184185private static Set<TrustAnchor> generateTrustAnchors()186throws CertificateException {187// generate certificate from cert string188CertificateFactory cf = CertificateFactory.getInstance("X.509");189190ByteArrayInputStream is =191new ByteArrayInputStream(selfSignedCertStr.getBytes());192Certificate selfSignedCert = cf.generateCertificate(is);193194// generate a trust anchor195TrustAnchor anchor =196new TrustAnchor((X509Certificate)selfSignedCert, null);197198return Collections.singleton(anchor);199}200201private static CertStore generateCertificateStore() throws Exception {202Collection entries = new HashSet();203204// generate certificate from certificate string205CertificateFactory cf = CertificateFactory.getInstance("X.509");206207ByteArrayInputStream is;208209is = new ByteArrayInputStream(targetCertStr.getBytes());210Certificate cert = cf.generateCertificate(is);211entries.add(cert);212213is = new ByteArrayInputStream(subCaCertStr.getBytes());214cert = cf.generateCertificate(is);215entries.add(cert);216217is = new ByteArrayInputStream(selfSignedCertStr.getBytes());218cert = cf.generateCertificate(is);219entries.add(cert);220221is = new ByteArrayInputStream(topCrlIssuerCertStr.getBytes());222cert = cf.generateCertificate(is);223entries.add(cert);224225is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());226cert = cf.generateCertificate(is);227entries.add(cert);228229// generate CRL from CRL string230is = new ByteArrayInputStream(topCrlStr.getBytes());231Collection mixes = cf.generateCRLs(is);232entries.addAll(mixes);233234is = new ByteArrayInputStream(subCrlStr.getBytes());235mixes = cf.generateCRLs(is);236entries.addAll(mixes);237238return CertStore.getInstance("Collection",239new CollectionCertStoreParameters(entries));240}241242private static X509CertSelector generateSelector(String name)243throws Exception {244X509CertSelector selector = new X509CertSelector();245246// generate certificate from certificate string247CertificateFactory cf = CertificateFactory.getInstance("X.509");248ByteArrayInputStream is = null;249if (name.equals("subca")) {250is = new ByteArrayInputStream(subCaCertStr.getBytes());251} else if (name.equals("subci")) {252is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());253} else {254is = new ByteArrayInputStream(targetCertStr.getBytes());255}256257X509Certificate target = (X509Certificate)cf.generateCertificate(is);258byte[] extVal = target.getExtensionValue("2.5.29.14");259if (extVal != null) {260DerInputStream in = new DerInputStream(extVal);261byte[] subjectKID = in.getOctetString();262selector.setSubjectKeyIdentifier(subjectKID);263} else {264// unlikely to happen.265throw new Exception("unexpected certificate: no SKID extension");266}267268return selector;269}270271private static boolean match(String name, Certificate cert)272throws Exception {273X509CertSelector selector = new X509CertSelector();274275// generate certificate from certificate string276CertificateFactory cf = CertificateFactory.getInstance("X.509");277ByteArrayInputStream is = null;278if (name.equals("subca")) {279is = new ByteArrayInputStream(subCaCertStr.getBytes());280} else if (name.equals("subci")) {281is = new ByteArrayInputStream(subCrlIssuerCertStr.getBytes());282} else {283is = new ByteArrayInputStream(targetCertStr.getBytes());284}285X509Certificate target = (X509Certificate)cf.generateCertificate(is);286287return target.equals(cert);288}289290291public static void main(String[] args) throws Exception {292// MD5 is used in this test case, don't disable MD5 algorithm.293Security.setProperty(294"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");295296CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");297298X509CertSelector selector = generateSelector(args[0]);299300Set<TrustAnchor> anchors = generateTrustAnchors();301CertStore certs = generateCertificateStore();302303304PKIXBuilderParameters params =305new PKIXBuilderParameters(anchors, selector);306params.addCertStore(certs);307params.setRevocationEnabled(true);308params.setDate(new Date(109, 7, 1)); // 2009-07-01309Security.setProperty("ocsp.enable", "false");310System.setProperty("com.sun.security.enableCRLDP", "true");311312PKIXCertPathBuilderResult result =313(PKIXCertPathBuilderResult)builder.build(params);314315if (!match(args[0], result.getCertPath().getCertificates().get(0))) {316throw new Exception("unexpected certificate");317}318}319}320321322