Path: blob/master/test/jdk/java/security/cert/CertPathValidator/OCSP/FailoverToCRL.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 638309532* @summary CRL revoked certificate failures masked by OCSP failures33* @run main/othervm FailoverToCRL34* @author Xuelei Fan35*/3637/*38* Note that the certificate validity is from Mar 16 14:55:35 2009 GMT to39* Dec 1 14:55:35 2028 GMT, please update it with newer certificate if40* expires.41*/4243/*44* Certificates used in the test.45*46* end entity certificate:47* Data:48* Version: 3 (0x2)49* Serial Number: 25 (0x19)50* Signature Algorithm: md5WithRSAEncryption51* Issuer: C=US, ST=Some-State, L=Some-City, O=Some-Org52* Validity53* Not Before: Mar 16 14:55:35 2009 GMT54* Not After : Dec 1 14:55:35 2028 GMT55* Subject: C=US, ST=Some-State, L=Some-City, O=Some-Org, OU=SSL-Client,56* CN=localhost57* Subject Public Key Info:58* Public Key Algorithm: rsaEncryption59* RSA Public Key: (1024 bit)60* Modulus (1024 bit):61* 00:bb:f0:40:36:ac:26:54:4e:f4:a3:5a:00:2f:69:62* 21:6f:b9:7a:3a:93:ec:a2:f6:e1:8e:c7:63:d8:2f:63* 12:30:99:2e:b0:f2:8f:f8:27:2d:24:78:28:84:f7:64* 01:bf:8d:44:79:dd:3b:d2:55:f3:ce:3c:b2:5b:21:65* 7d:ef:fd:33:4a:b1:a3:ff:c6:c8:9b:b9:0f:7c:41:66* 35:97:f9:db:3a:05:60:05:15:af:59:17:92:a3:10:67* ad:16:1c:e4:07:53:af:a8:76:a2:56:2a:92:d3:f9:68* 28:e0:78:cf:5e:1f:48:ab:5c:19:dd:e1:67:43:ba:69* 75:8d:f5:82:ac:43:92:44:1b70* Exponent: 65537 (0x10001)71* X509v3 extensions:72* X509v3 Basic Constraints:73* CA:FALSE74* X509v3 Key Usage:75* Digital Signature, Non Repudiation, Key Encipherment76* X509v3 Subject Key Identifier:77* CD:BB:C8:85:AA:91:BD:FD:1D:BE:CD:67:7C:FF:B3:E9:4C:A8:22:E678* X509v3 Authority Key Identifier:79* keyid:FA:B9:51:BF:4C:E7:D9:86:98:33:F9:E7:CB:1E:F1:33:49:F7:A8:1480* Signature Algorithm: md5WithRSAEncryption81*82*83* trusted certificate authority:84* Data:85* Version: 3 (0x2)86* Serial Number: 0 (0x0)87* Signature Algorithm: md5WithRSAEncryption88* Issuer: C=US, ST=Some-State, L=Some-City, O=Some-Org89* Validity90* Not Before: Dec 8 02:43:36 2008 GMT91* Not After : Aug 25 02:43:36 2028 GMT92* Subject: C=US, ST=Some-State, L=Some-City, O=Some-Org93* Subject Public Key Info:94* Public Key Algorithm: rsaEncryption95* RSA Public Key: (1024 bit)96* Modulus (1024 bit):97* 00:cb:c4:38:20:07:be:88:a7:93:b0:a1:43:51:2d:98* d7:8e:85:af:54:dd:ad:a2:7b:23:5b:cf:99:13:53:99* 99:45:7d:ee:6d:ba:2d:bf:e3:ad:6e:3d:9f:1a:f9:100* 03:97:e0:17:55:ae:11:26:57:de:01:29:8e:05:3f:101* 21:f7:e7:36:e8:2e:37:d7:48:ac:53:d6:60:0e:c7:102* 50:6d:f6:c5:85:f7:8b:a6:c5:91:35:72:3c:94:ee:103* f1:17:f0:71:e3:ec:1b:ce:ca:4e:40:42:b0:6d:ee:104* 6a:0e:d6:e5:ad:3c:0f:c9:ba:82:4f:78:f8:89:97:105* 89:2a:95:12:4c:d8:09:2a:e9106* Exponent: 65537 (0x10001)107* X509v3 extensions:108* X509v3 Subject Key Identifier:109* FA:B9:51:BF:4C:E7:D9:86:98:33:F9:E7:CB:1E:F1:33:49:F7:A8:14110* X509v3 Authority Key Identifier:111* keyid:FA:B9:51:BF:4C:E7:D9:86:98:33:F9:E7:CB:1E:F1:33:49:F7:A8:14112* DirName:/C=US/ST=Some-State/L=Some-City/O=Some-Org113* X509v3 Basic Constraints:114* CA:TRUE115* Signature Algorithm: md5WithRSAEncryption116*117* CRL:118* Certificate Revocation List (CRL):119* Version 2 (0x1)120* Signature Algorithm: md5WithRSAEncryption121* Issuer: /C=US/ST=Some-State/L=Some-City/O=Some-Org122* Last Update: Mar 16 16:27:14 2009 GMT123* Next Update: May 15 16:27:14 2028 GMT124* CRL extensions:125* X509v3 CRL Number:126* 2127* Revoked Certificates:128* Serial Number: 19129* Revocation Date: Mar 16 16:22:08 2009 GMT130* CRL entry extensions:131* X509v3 CRL Reason Code:132* Superseded133* Signature Algorithm: md5WithRSAEncryption134*/135136import java.io.*;137import java.net.SocketException;138import java.util.*;139import java.security.Security;140import java.security.cert.*;141import java.security.InvalidAlgorithmParameterException;142import java.security.cert.CertPathValidatorException.BasicReason;143144public class FailoverToCRL {145146static String trusedCertStr =147"-----BEGIN CERTIFICATE-----\n" +148"MIICrDCCAhWgAwIBAgIBADANBgkqhkiG9w0BAQQFADBJMQswCQYDVQQGEwJVUzET\n" +149"MBEGA1UECBMKU29tZS1TdGF0ZTESMBAGA1UEBxMJU29tZS1DaXR5MREwDwYDVQQK\n" +150"EwhTb21lLU9yZzAeFw0wODEyMDgwMjQzMzZaFw0yODA4MjUwMjQzMzZaMEkxCzAJ\n" +151"BgNVBAYTAlVTMRMwEQYDVQQIEwpTb21lLVN0YXRlMRIwEAYDVQQHEwlTb21lLUNp\n" +152"dHkxETAPBgNVBAoTCFNvbWUtT3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n" +153"gQDLxDggB76Ip5OwoUNRLdeOha9U3a2ieyNbz5kTU5lFfe5tui2/461uPZ8a+QOX\n" +154"4BdVrhEmV94BKY4FPyH35zboLjfXSKxT1mAOx1Bt9sWF94umxZE1cjyU7vEX8HHj\n" +155"7BvOyk5AQrBt7moO1uWtPA/JuoJPePiJl4kqlRJM2Akq6QIDAQABo4GjMIGgMB0G\n" +156"A1UdDgQWBBT6uVG/TOfZhpgz+efLHvEzSfeoFDBxBgNVHSMEajBogBT6uVG/TOfZ\n" +157"hpgz+efLHvEzSfeoFKFNpEswSTELMAkGA1UEBhMCVVMxEzARBgNVBAgTClNvbWUt\n" +158"U3RhdGUxEjAQBgNVBAcTCVNvbWUtQ2l0eTERMA8GA1UEChMIU29tZS1PcmeCAQAw\n" +159"DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQQFAAOBgQBcIm534U123Hz+rtyYO5uA\n" +160"ofd81G6FnTfEAV8Kw9fGyyEbQZclBv34A9JsFKeMvU4OFIaixD7nLZ/NZ+IWbhmZ\n" +161"LovmJXyCkOufea73pNiZ+f/4/ScZaIlM/PRycQSqbFNd4j9Wott+08qxHPLpsf3P\n" +162"6Mvf0r1PNTY2hwTJLJmKtg==\n" +163"-----END CERTIFICATE-----";164165static String targetCertStr =166"-----BEGIN CERTIFICATE-----\n" +167"MIICizCCAfSgAwIBAgIBGTANBgkqhkiG9w0BAQQFADBJMQswCQYDVQQGEwJVUzET\n" +168"MBEGA1UECBMKU29tZS1TdGF0ZTESMBAGA1UEBxMJU29tZS1DaXR5MREwDwYDVQQK\n" +169"EwhTb21lLU9yZzAeFw0wOTAzMTYxNDU1MzVaFw0yODEyMDExNDU1MzVaMHIxCzAJ\n" +170"BgNVBAYTAlVTMRMwEQYDVQQIEwpTb21lLVN0YXRlMRIwEAYDVQQHEwlTb21lLUNp\n" +171"dHkxETAPBgNVBAoTCFNvbWUtT3JnMRMwEQYDVQQLEwpTU0wtQ2xpZW50MRIwEAYD\n" +172"VQQDEwlsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALvwQDas\n" +173"JlRO9KNaAC9pIW+5ejqT7KL24Y7HY9gvEjCZLrDyj/gnLSR4KIT3Ab+NRHndO9JV\n" +174"8848slshfe/9M0qxo//GyJu5D3xBNZf52zoFYAUVr1kXkqMQrRYc5AdTr6h2olYq\n" +175"ktP5KOB4z14fSKtcGd3hZ0O6dY31gqxDkkQbAgMBAAGjWjBYMAkGA1UdEwQCMAAw\n" +176"CwYDVR0PBAQDAgXgMB0GA1UdDgQWBBTNu8iFqpG9/R2+zWd8/7PpTKgi5jAfBgNV\n" +177"HSMEGDAWgBT6uVG/TOfZhpgz+efLHvEzSfeoFDANBgkqhkiG9w0BAQQFAAOBgQBv\n" +178"p7JjCDOrMBNun46xs4Gz7Y4ygM5VHaFP0oO7369twvRSu0pCuIdZd5OIMPFeRqQw\n" +179"PA68ZdhYVR0pG5W7isV+jB+Dfge/IOgOA85sZ/6FlP3PBRW+YMQKKdRr5So3ook9\n" +180"PimQ7rbxRAofPECv20IUKFBbOUkU+gFcn+WbTKYxBw==\n" +181"-----END CERTIFICATE-----";182183static String crlStr =184"-----BEGIN X509 CRL-----\n" +185"MIIBRTCBrwIBATANBgkqhkiG9w0BAQQFADBJMQswCQYDVQQGEwJVUzETMBEGA1UE\n" +186"CBMKU29tZS1TdGF0ZTESMBAGA1UEBxMJU29tZS1DaXR5MREwDwYDVQQKEwhTb21l\n" +187"LU9yZxcNMDkwMzE2MTYyNzE0WhcNMjgwNTE1MTYyNzE0WjAiMCACARkXDTA5MDMx\n" +188"NjE2MjIwOFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJKoZIhvcN\n" +189"AQEEBQADgYEAMixJI9vBwYpOGosn46+T/MTEtlm2S5pIVT/xPDrHkCPfw8l4Zrgp\n" +190"dGPuUkglWdrGdxY9MNRUj2YFNfdZi6zZ7JF6XbkDHYOAKYgPDJRjS/0VcBntn5RJ\n" +191"sQfZsBqc9fFSP8gknRRn3LT41kr9xNRxTT1t3YYjv7J3zkMYyInqeUA=\n" +192"-----END X509 CRL-----";193194195private static CertPath generateCertificatePath()196throws CertificateException {197// generate certificate from cert strings198CertificateFactory cf = CertificateFactory.getInstance("X.509");199200ByteArrayInputStream is =201new ByteArrayInputStream(targetCertStr.getBytes());202Certificate targetCert = cf.generateCertificate(is);203204// generate certification path205List<Certificate> list = Arrays.asList(new Certificate[] {targetCert});206207return cf.generateCertPath(list);208}209210private static Set<TrustAnchor> generateTrustAnchors()211throws CertificateException {212// generate certificate from cert string213CertificateFactory cf = CertificateFactory.getInstance("X.509");214215ByteArrayInputStream is =216new ByteArrayInputStream(trusedCertStr.getBytes());217Certificate trusedCert = cf.generateCertificate(is);218219// generate a trust anchor220TrustAnchor anchor = new TrustAnchor((X509Certificate)trusedCert, null);221222return Collections.singleton(anchor);223}224225private static CertStore generateCertificateStore() throws Exception {226// generate CRL from CRL string227CertificateFactory cf = CertificateFactory.getInstance("X.509");228229ByteArrayInputStream is =230new ByteArrayInputStream(crlStr.getBytes());231232// generate a cert store233Collection<? extends CRL> crls = cf.generateCRLs(is);234return CertStore.getInstance("Collection",235new CollectionCertStoreParameters(crls));236}237238public static void main(String args[]) throws Exception {239// MD5 is used in this test case, don't disable MD5 algorithm.240Security.setProperty(241"jdk.certpath.disabledAlgorithms", "MD2, RSA keySize < 1024");242243CertPath path = generateCertificatePath();244Set<TrustAnchor> anchors = generateTrustAnchors();245CertStore crls = generateCertificateStore();246247PKIXParameters params = new PKIXParameters(anchors);248249// add the CRL store250params.addCertStore(crls);251252// Activate certificate revocation checking253params.setRevocationEnabled(true);254255// Activate OCSP256Security.setProperty("ocsp.enable", "true");257System.setProperty("com.sun.security.enableCRLDP", "true");258259// Ensure that the ocsp.responderURL property is not set.260if (Security.getProperty("ocsp.responderURL") != null) {261throw new262Exception("The ocsp.responderURL property must not be set");263}264265CertPathValidator validator = CertPathValidator.getInstance("PKIX");266267try {268validator.validate(path, params);269} catch (CertPathValidatorException cpve) {270if (cpve.getReason() != BasicReason.REVOKED) {271throw new Exception(272"unexpected exception, should be a REVOKED CPVE", cpve);273}274}275}276}277278279