Path: blob/master/test/jdk/sun/security/provider/certpath/PKIXCertPathValidator/Validity.java
41155 views
/*1* Copyright (c) 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*/222324/**25* @test26* @bug 802180427* @summary CertPath should validate even if the validity period of the28* root cert does not include the validity period of a subordinate29* cert.30*/3132import java.io.ByteArrayInputStream;33import java.security.cert.*;34import java.util.ArrayList;35import java.util.Date;36import java.util.HashSet;37import java.util.Set;3839public class Validity {4041/*42* Subject: OU=TestOrg, CN=TestCA43* Issuer: OU=TestOrg, CN=TestCA44* Validity45* Not Before: Feb 26 21:33:55 2014 GMT46Not After : Feb 26 21:33:55 2024 GMT47* Version 148*/49static String CACertStr =50"-----BEGIN CERTIFICATE-----\n" +51"MIIBvTCCASYCCQCQRiTo4lBCFjANBgkqhkiG9w0BAQUFADAjMRAwDgYDVQQLDAdU\n" +52"ZXN0T3JnMQ8wDQYDVQQDDAZUZXN0Q0EwHhcNMTQwMjI2MjEzMzU1WhcNMjQwMjI2\n" +53"MjEzMzU1WjAjMRAwDgYDVQQLDAdUZXN0T3JnMQ8wDQYDVQQDDAZUZXN0Q0EwgZ8w\n" +54"DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOtKS4ZrsM3ansd61ZxitcrN0w184I+A\n" +55"z0kyrSP1eMtlam+cC2U91NpTz11FYV4XUfBhqqxaXW043AWTUer8pS90Pt4sCrUX\n" +56"COx1+QA1M3ZhbZ4sTM7XQ90JbGaBJ/sEza9mlQP7hQ2yQO/hATKbP6J5qvgG2sT2\n" +57"S2WYjEgwNwmFAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAQ/CXEpnx2WY4LJtv4jwE\n" +58"4jIVirur3pdzV5oBhPyqqHMsyhQBkukCfX7uD7L5wN1+xuM81DfANpIxlnUfybp5\n" +59"CpjcmktLpmyK4kJ6XnSd2blbLOIpsr9x6FqxPxpVDlyw/ySHYrIG/GZdsLHgmzGn\n" +60"B06jeYzH8OLf879VxAxSsPc=\n" +61"-----END CERTIFICATE-----";6263/*64* Subject: OU=TestOrg, CN=TestEE065* Issuer: OU=TestOrg, CN=TestCA66* Validity67* Not Before: Feb 26 22:55:12 2014 GMT68* Not After : Feb 25 22:55:12 2025 GMT69* Version 170*/71static String EECertStr =72"-----BEGIN CERTIFICATE-----\n" +73"MIIBtjCCAR8CAQQwDQYJKoZIhvcNAQEFBQAwIzEQMA4GA1UECwwHVGVzdE9yZzEP\n" +74"MA0GA1UEAwwGVGVzdENBMB4XDTE0MDIyNjIyNTUxMloXDTI1MDIyNTIyNTUxMlow\n" +75"JDEQMA4GA1UECwwHVGVzdE9yZzEQMA4GA1UEAwwHVGVzdEVFMDCBnzANBgkqhkiG\n" +76"9w0BAQEFAAOBjQAwgYkCgYEAt8xz9W3ruCTHjSOtTX6cxsUZ0nRP6EavEfzgcOYh\n" +77"CXGA0gr+viSHq3c2vQBxiRny2hm5rLcqpPo+2OxZtw/ajxfyrV6d/r8YyQLBvyl3\n" +78"xdCZdOkG1DCM1oFAQDaSRt9wN5Zm5kyg7uMig5Y4L45fP9Yee4x6Xyh36qYbsR89\n" +79"rFMCAwEAATANBgkqhkiG9w0BAQUFAAOBgQDZrPqSo08va1m9TOWOztTuWilGdjK/\n" +80"2Ed2WXg8utIpy6uAV+NaOYtHQ7ULQBVRNmwg9nKghbVbh+E/xpoihjl1x7OXass4\n" +81"TbwXA5GKFIFpNtDvATQ/QQZoCuCzw1FW/mH0Q7UEQ/9/iJdDad6ebkapeMwtj/8B\n" +82"s2IZV7s85CEOXw==\n" +83"-----END CERTIFICATE-----";8485public static void main(String[] args) throws Exception {8687String[] certStrs = {EECertStr};88String[] trustedCertStrs = {CACertStr};89runTest(certStrs, trustedCertStrs);9091System.out.println("Test passed.");92}9394private static void runTest(String[] certStrs,95String[] trustedCertStrs)96throws Exception {9798CertificateFactory cf = CertificateFactory.getInstance("X509");99100// Generate the CertPath from the certs named in certStrs101ArrayList<X509Certificate> certs = new ArrayList<>();102for (String certStr : certStrs) {103certs.add(generateCert(certStr, cf));104}105CertPath cp = cf.generateCertPath(certs);106107// Generate the set of Trust Anchors from the certs named in108// trustedCertStrs109Set<TrustAnchor> trustAnchors = new HashSet<>();110for (String trustedCertStr : trustedCertStrs) {111TrustAnchor ta = new TrustAnchor(generateCert(trustedCertStr, cf),112null);113trustAnchors.add(ta);114}115PKIXParameters params = new PKIXParameters(trustAnchors);116params.setDate(new Date(114, 3, 1)); // 2014-03-01117params.setRevocationEnabled(false);118119// Attempt to validate the CertPath. If no exception thrown, successful.120CertPathValidator cpv = CertPathValidator.getInstance("PKIX");121cpv.validate(cp, params);122System.out.println("CertPath validation successful.");123}124125private static X509Certificate generateCert(String certStr,126CertificateFactory cf)127throws Exception {128ByteArrayInputStream stream129= new ByteArrayInputStream(certStr.getBytes());130return (X509Certificate) cf.generateCertificate(stream);131132}133}134135136