summaryrefslogtreecommitdiff
path: root/ojluni/src/test/java/security/cert/PKIXCertPathValidatorValidity.java
blob: fd68b447aad3c2888c96dd55bafa60f393844dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


/**
 * @test
 * @bug 8021804
 * @summary CertPath should validate even if the validity period of the
 *          root cert does not include the validity period of a subordinate
 *          cert.
 */
// Android-changed: Adapted from
// jdk/test/sun/security/provider/certpath/PKIXCertPathValidator/Validity.java
// Android-changed: Added package & Test import
package test.java.security.cert;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.security.cert.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

// Android-changed: s/Validity/PKIXCertPathValidatorValidity
public class PKIXCertPathValidatorValidity {

    /*
     * Subject: OU=TestOrg, CN=TestCA
     * Issuer: OU=TestOrg, CN=TestCA
     * Validity
     *     Not Before: Feb 26 21:33:55 2014 GMT
           Not After : Feb 26 21:33:55 2024 GMT
     * Version 1
     */
    static String CACertStr =
        "-----BEGIN CERTIFICATE-----\n" +
        "MIIBvTCCASYCCQCQRiTo4lBCFjANBgkqhkiG9w0BAQUFADAjMRAwDgYDVQQLDAdU\n" +
        "ZXN0T3JnMQ8wDQYDVQQDDAZUZXN0Q0EwHhcNMTQwMjI2MjEzMzU1WhcNMjQwMjI2\n" +
        "MjEzMzU1WjAjMRAwDgYDVQQLDAdUZXN0T3JnMQ8wDQYDVQQDDAZUZXN0Q0EwgZ8w\n" +
        "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOtKS4ZrsM3ansd61ZxitcrN0w184I+A\n" +
        "z0kyrSP1eMtlam+cC2U91NpTz11FYV4XUfBhqqxaXW043AWTUer8pS90Pt4sCrUX\n" +
        "COx1+QA1M3ZhbZ4sTM7XQ90JbGaBJ/sEza9mlQP7hQ2yQO/hATKbP6J5qvgG2sT2\n" +
        "S2WYjEgwNwmFAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAQ/CXEpnx2WY4LJtv4jwE\n" +
        "4jIVirur3pdzV5oBhPyqqHMsyhQBkukCfX7uD7L5wN1+xuM81DfANpIxlnUfybp5\n" +
        "CpjcmktLpmyK4kJ6XnSd2blbLOIpsr9x6FqxPxpVDlyw/ySHYrIG/GZdsLHgmzGn\n" +
        "B06jeYzH8OLf879VxAxSsPc=\n" +
        "-----END CERTIFICATE-----";

    /*
     * Subject: OU=TestOrg, CN=TestEE0
     * Issuer: OU=TestOrg, CN=TestCA
     * Validity
     *     Not Before: Feb 26 22:55:12 2014 GMT
     *     Not After : Feb 25 22:55:12 2025 GMT
     * Version 1
     */
    static String EECertStr =
        "-----BEGIN CERTIFICATE-----\n" +
        "MIIBtjCCAR8CAQQwDQYJKoZIhvcNAQEFBQAwIzEQMA4GA1UECwwHVGVzdE9yZzEP\n" +
        "MA0GA1UEAwwGVGVzdENBMB4XDTE0MDIyNjIyNTUxMloXDTI1MDIyNTIyNTUxMlow\n" +
        "JDEQMA4GA1UECwwHVGVzdE9yZzEQMA4GA1UEAwwHVGVzdEVFMDCBnzANBgkqhkiG\n" +
        "9w0BAQEFAAOBjQAwgYkCgYEAt8xz9W3ruCTHjSOtTX6cxsUZ0nRP6EavEfzgcOYh\n" +
        "CXGA0gr+viSHq3c2vQBxiRny2hm5rLcqpPo+2OxZtw/ajxfyrV6d/r8YyQLBvyl3\n" +
        "xdCZdOkG1DCM1oFAQDaSRt9wN5Zm5kyg7uMig5Y4L45fP9Yee4x6Xyh36qYbsR89\n" +
        "rFMCAwEAATANBgkqhkiG9w0BAQUFAAOBgQDZrPqSo08va1m9TOWOztTuWilGdjK/\n" +
        "2Ed2WXg8utIpy6uAV+NaOYtHQ7ULQBVRNmwg9nKghbVbh+E/xpoihjl1x7OXass4\n" +
        "TbwXA5GKFIFpNtDvATQ/QQZoCuCzw1FW/mH0Q7UEQ/9/iJdDad6ebkapeMwtj/8B\n" +
        "s2IZV7s85CEOXw==\n" +
        "-----END CERTIFICATE-----";

    // Android-changed: Removed args & added @Test
    @Test
    public static void main() throws Exception {

        String[] certStrs = {EECertStr};
        String[] trustedCertStrs = {CACertStr};
        runTest(certStrs, trustedCertStrs);

        System.out.println("Test passed.");
    }

    private static void runTest(String[] certStrs,
                                String[] trustedCertStrs)
            throws Exception {

        CertificateFactory cf = CertificateFactory.getInstance("X509");

        // Generate the CertPath from the certs named in certStrs
        ArrayList<X509Certificate> certs = new ArrayList<>();
        for (String certStr : certStrs) {
            certs.add(generateCert(certStr, cf));
        }
        CertPath cp = cf.generateCertPath(certs);

        // Generate the set of Trust Anchors from the certs named in
        // trustedCertStrs
        Set<TrustAnchor> trustAnchors = new HashSet<>();
        for (String trustedCertStr : trustedCertStrs) {
            TrustAnchor ta = new TrustAnchor(generateCert(trustedCertStr, cf),
                                             null);
            trustAnchors.add(ta);
        }
        PKIXParameters params = new PKIXParameters(trustAnchors);
        params.setDate(new Date(114, 3, 1));   // 2014-03-01
        params.setRevocationEnabled(false);

        // Attempt to validate the CertPath. If no exception thrown, successful.
        CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
        cpv.validate(cp, params);
        System.out.println("CertPath validation successful.");
    }

    private static X509Certificate generateCert(String certStr,
                                                CertificateFactory cf)
            throws Exception {
        ByteArrayInputStream stream
                = new ByteArrayInputStream(certStr.getBytes());
        return (X509Certificate) cf.generateCertificate(stream);

    }
}