Path: blob/master/test/jdk/java/security/cert/CertPathValidator/indirectCRL/CircularCRLOneLevel.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*32* @bug 672072133* @summary CRL check with circular depency support needed34* @run main/othervm CircularCRLOneLevel35* @author Xuelei Fan36*/3738import java.io.*;39import java.net.SocketException;40import java.util.*;41import java.security.Security;42import java.security.cert.*;43import java.security.cert.CertPathValidatorException.BasicReason;4445public class CircularCRLOneLevel {4647static String selfSignedCertStr =48"-----BEGIN CERTIFICATE-----\n" +49"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +50"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzJaFw0zMDA0MDcwMjI0MzJa\n" +51"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +52"AQUAA4GNADCBiQKBgQC4OTag24sTxL2tXTNuvpmUEtdxrYAZoFsslFQ60T+WD9wQ\n" +53"Jeiw87FSPsR2vxRuv0j8DNm2a4h7LNNIFcLurfNldbz5pvgZ7VqdbbUMPE9qP85n\n" +54"jgDl4woyRTSUeRI4A7O0CO6NpES21dtbdhroWQrEkHxpnrDPxsxrz5gf2m3gqwID\n" +55"AQABo4GJMIGGMB0GA1UdDgQWBBSCJd0hpl5PdAD9IZS+Hzng4lXLGzBHBgNVHSME\n" +56"QDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +57"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAgQw\n" +58"DQYJKoZIhvcNAQEEBQADgYEAluy6HIjWcq009lTLmhp+Np6dxU78pInBK8RZkza0\n" +59"484qGaxFGD3UGyZkI5uWmsH2XuMbuox5khfIq6781gmkPBHXBIEtJN8eLusOHEye\n" +60"iE8h7WI+N3qa6Pj56WionMrioqC/3X+b06o147bbhx8U0vkYv/HyPaITOFfMXTdz\n" +61"Vjw=\n" +62"-----END CERTIFICATE-----";6364static String subCaCertStr =65"-----BEGIN CERTIFICATE-----\n" +66"MIICUDCCAbmgAwIBAgIBAzANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +67"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzRaFw0yOTAxMTIwMjI0MzRa\n" +68"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +69"cy0xMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiAJnAQW2ad3ZMKUhSJVZj\n" +70"8pBqxTcHSTwAVguQkDglsN/OIwUpvR5Jgp3lpRWUEt6idEp0FZzORpvtjt3pr5MG\n" +71"Eg2CDptekC5BSPS+fIAIKlncB3HwOiFFhH6b3wTydDCdEd2fvsi4QMOSVrIYMeA8\n" +72"P/mCz6kRhfUQPE0CMmOUewIDAQABo4GJMIGGMB0GA1UdDgQWBBT0/nNP8WpyxmYr\n" +73"IBp4tN8y08jw2jBHBgNVHSMEQDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEw\n" +74"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +75"AwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcNAQEEBQADgYEAS9PzI6B39R/U9fRj\n" +76"UExzN1FXNP5awnAPtiv34kSCL6n6MryqkfG+8aaAOdZsSjmTylNFaF7cW/Xp1VBF\n" +77"hq0bg/SbEAbK7+UwL8GSC3crhULHLbh+1iFdVTEwxCw5YmB8ji3BaZ/WKW/PkjCZ\n" +78"7cXP6VDeZMG6oRQ4hbOcixoFPXo=\n" +79"-----END CERTIFICATE-----";8081static String targetCertStr = subCaCertStr;8283static String crlIssuerCertStr =84"-----BEGIN CERTIFICATE-----\n" +85"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +86"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +87"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +88"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +89"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +90"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +91"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +92"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +93"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +94"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +95"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +96"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +97"-----END CERTIFICATE-----";9899static String crlStr =100"-----BEGIN X509 CRL-----\n" +101"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +102"ChMHRXhhbXBsZRcNMDkwNDI3MDIzODA0WhcNMjgwNjI2MDIzODA0WjAiMCACAQUX\n" +103"DTA5MDQyNzAyMzgwMFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJ\n" +104"KoZIhvcNAQEEBQADgYEAoarfzXEtw3ZDi4f9U8eSvRIipHSyxOrJC7HR/hM5VhmY\n" +105"CErChny6x9lBVg9s57tfD/P9PSzBLusCcHwHMAbMOEcTltVVKUWZnnbumpywlYyg\n" +106"oKLrE9+yCOkYUOpiRlz43/3vkEL5hjIKMcDSZnPKBZi1h16Yj2hPe9GMibNip54=\n" +107"-----END X509 CRL-----";108109private static CertPath generateCertificatePath()110throws CertificateException {111// generate certificate from cert strings112CertificateFactory cf = CertificateFactory.getInstance("X.509");113114ByteArrayInputStream is;115116is = new ByteArrayInputStream(targetCertStr.getBytes());117Certificate targetCert = cf.generateCertificate(is);118119is = new ByteArrayInputStream(selfSignedCertStr.getBytes());120Certificate selfSignedCert = cf.generateCertificate(is);121122// generate certification path123List<Certificate> list = Arrays.asList(new Certificate[] {124targetCert, selfSignedCert});125126return cf.generateCertPath(list);127}128129private static Set<TrustAnchor> generateTrustAnchors()130throws CertificateException {131// generate certificate from cert string132CertificateFactory cf = CertificateFactory.getInstance("X.509");133134ByteArrayInputStream is =135new ByteArrayInputStream(selfSignedCertStr.getBytes());136Certificate selfSignedCert = cf.generateCertificate(is);137138// generate a trust anchor139TrustAnchor anchor =140new TrustAnchor((X509Certificate)selfSignedCert, null);141142return Collections.singleton(anchor);143}144145private static CertStore generateCertificateStore() throws Exception {146// generate CRL from CRL string147CertificateFactory cf = CertificateFactory.getInstance("X.509");148149ByteArrayInputStream is =150new ByteArrayInputStream(crlStr.getBytes());151152// generate a cert store153Collection crls = cf.generateCRLs(is);154155is = new ByteArrayInputStream(crlIssuerCertStr.getBytes());156Collection certs = cf.generateCertificates(is);157158Collection entries = new HashSet();159entries.addAll(crls);160entries.addAll(certs);161162return CertStore.getInstance("Collection",163new CollectionCertStoreParameters(entries));164}165166public static void main(String args[]) throws Exception {167// MD5 is used in this test case, don't disable MD5 algorithm.168Security.setProperty(169"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");170171CertPath path = generateCertificatePath();172Set<TrustAnchor> anchors = generateTrustAnchors();173CertStore crls = generateCertificateStore();174175PKIXParameters params = new PKIXParameters(anchors);176177// add the CRL store178params.addCertStore(crls);179180// Activate certificate revocation checking181params.setRevocationEnabled(true);182183// set the validation time184params.setDate(new Date(109, 5, 1)); // 2009-05-01185186// disable OCSP checker187Security.setProperty("ocsp.enable", "false");188189// enable CRL checker190System.setProperty("com.sun.security.enableCRLDP", "true");191192CertPathValidator validator = CertPathValidator.getInstance("PKIX");193194try {195validator.validate(path, params);196} catch (CertPathValidatorException cpve) {197if (cpve.getReason() != BasicReason.REVOKED) {198throw new Exception(199"unexpect exception, should be a REVOKED CPVE", cpve);200}201}202}203}204205206