Path: blob/master/test/jdk/java/security/cert/CertPathValidator/indirectCRL/CircularCRLOneLevelRevoked.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 CircularCRLOneLevelRevoked35* @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 CircularCRLOneLevelRevoked {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 dumCaCertStr =65"-----BEGIN CERTIFICATE-----\n" +66"MIICUDCCAbmgAwIBAgIBBTANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +67"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzVaFw0yOTAxMTIwMjI0MzVa\n" +68"MDExCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMRAwDgYDVQQLEwdDbGFz\n" +69"cy1EMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAwfZ3wIYzdCkiFIKjrUKc\n" +70"0B32HaRkUeVJthadinLmoAVruCi3GRkLZUIPXDD9b7dFBbdeT1+8qDHV5wu/ES8W\n" +71"bgfirO8ng8h2hRuJbZgtfljNnVc3fptjxo7x73aP++w2oIcmjzVwaV08sgahoaY4\n" +72"f249t4EXbvjJQ8kuj1I8qQIDAQABo4GJMIGGMB0GA1UdDgQWBBR3fwdjpP4WiuyL\n" +73"/MDVrXUORrarXDBHBgNVHSMEQDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEw\n" +74"HzELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUw\n" +75"AwEB/zALBgNVHQ8EBAMCAgQwDQYJKoZIhvcNAQEEBQADgYEAp/2sXI/XLtXu+X05\n" +76"EISyBPQqdE3kgN3dmXOuoK9J7Io8jhgetdbr9S1WTSGBonaXZgc52FNsaaDU+VIp\n" +77"TGTYU5SFloUyOu/e095eAf9Q867pAPcE5zArfKpXEBLbJwhLFwrsKPk/WZM7Yaxs\n" +78"mihnXyZWWTA1sPZlVJu7/abJ2v0=\n" +79"-----END CERTIFICATE-----";8081// a revoked certificate82static String targetCertStr = dumCaCertStr;8384static String crlIssuerCertStr =85"-----BEGIN CERTIFICATE-----\n" +86"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +87"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +88"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +89"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +90"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +91"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +92"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +93"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +94"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +95"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +96"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +97"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +98"-----END CERTIFICATE-----";99100static String crlStr =101"-----BEGIN X509 CRL-----\n" +102"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +103"ChMHRXhhbXBsZRcNMDkwNDI3MDIzODA0WhcNMjgwNjI2MDIzODA0WjAiMCACAQUX\n" +104"DTA5MDQyNzAyMzgwMFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJ\n" +105"KoZIhvcNAQEEBQADgYEAoarfzXEtw3ZDi4f9U8eSvRIipHSyxOrJC7HR/hM5VhmY\n" +106"CErChny6x9lBVg9s57tfD/P9PSzBLusCcHwHMAbMOEcTltVVKUWZnnbumpywlYyg\n" +107"oKLrE9+yCOkYUOpiRlz43/3vkEL5hjIKMcDSZnPKBZi1h16Yj2hPe9GMibNip54=\n" +108"-----END X509 CRL-----";109110private static CertPath generateCertificatePath()111throws CertificateException {112// generate certificate from cert strings113CertificateFactory cf = CertificateFactory.getInstance("X.509");114115ByteArrayInputStream is;116117is = new ByteArrayInputStream(targetCertStr.getBytes());118Certificate targetCert = cf.generateCertificate(is);119120is = new ByteArrayInputStream(selfSignedCertStr.getBytes());121Certificate selfSignedCert = cf.generateCertificate(is);122123// generate certification path124List<Certificate> list = Arrays.asList(new Certificate[] {125targetCert, selfSignedCert});126127return cf.generateCertPath(list);128}129130private static Set<TrustAnchor> generateTrustAnchors()131throws CertificateException {132// generate certificate from cert string133CertificateFactory cf = CertificateFactory.getInstance("X.509");134135ByteArrayInputStream is =136new ByteArrayInputStream(selfSignedCertStr.getBytes());137Certificate selfSignedCert = cf.generateCertificate(is);138139// generate a trust anchor140TrustAnchor anchor =141new TrustAnchor((X509Certificate)selfSignedCert, null);142143return Collections.singleton(anchor);144}145146private static CertStore generateCertificateStore() throws Exception {147// generate CRL from CRL string148CertificateFactory cf = CertificateFactory.getInstance("X.509");149150ByteArrayInputStream is =151new ByteArrayInputStream(crlStr.getBytes());152153// generate a cert store154Collection crls = cf.generateCRLs(is);155156is = new ByteArrayInputStream(crlIssuerCertStr.getBytes());157Collection certs = cf.generateCertificates(is);158159Collection entries = new HashSet();160entries.addAll(crls);161entries.addAll(certs);162163return CertStore.getInstance("Collection",164new CollectionCertStoreParameters(entries));165}166167public static void main(String args[]) throws Exception {168// MD5 is used in this test case, don't disable MD5 algorithm.169Security.setProperty(170"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");171172CertPath path = generateCertificatePath();173Set<TrustAnchor> anchors = generateTrustAnchors();174CertStore crls = generateCertificateStore();175176PKIXParameters params = new PKIXParameters(anchors);177178// add the CRL store179params.addCertStore(crls);180181// Activate certificate revocation checking182params.setRevocationEnabled(true);183184// set the validation time185params.setDate(new Date(109, 5, 1)); // 2009-05-01186187// disable OCSP checker188Security.setProperty("ocsp.enable", "false");189190// enable CRL checker191System.setProperty("com.sun.security.enableCRLDP", "true");192193CertPathValidator validator = CertPathValidator.getInstance("PKIX");194195try {196validator.validate(path, params);197throw new Exception("unexpected status, should be REVOKED");198} catch (CertPathValidatorException cpve) {199if (cpve.getReason() != BasicReason.REVOKED) {200throw new Exception(201"unexpected exception, should be a REVOKED CPVE", cpve);202}203}204205}206}207208209