test/jdk/java/security/cert/CertPathBuilder/targetConstraints/BuildEEBasicConstraints.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 23010 jdk/test/java/security/cert/CertPathBuilder/targetConstraints/BuildEEBasicConstraints.java@6dadb192ad81
child 58013 d80e4bce4588
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14929
diff changeset
     2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     4
 *
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     8
 *
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    13
 * accompanied this code).
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    14
 *
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 792
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 792
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 792
diff changeset
    21
 * questions.
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    22
 */
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    23
14929
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    24
// This test case relies on updated static security property, no way to re-use
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    25
// security property in samevm/agentvm mode.
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    26
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    27
/**
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    28
 * @test
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    29
 * @bug 6714842
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    30
 * @library ../../../testlibrary
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    31
 * @build CertUtils
14929
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    32
 * @run main/othervm BuildEEBasicConstraints
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    33
 * @summary make sure a PKIX CertPathBuilder builds a path to an
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    34
 *      end entity certificate when the setBasicConstraints method of the
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    35
 *      X509CertSelector of the targetConstraints PKIXBuilderParameters
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    36
 *      parameter is set to -2.
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    37
 */
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    38
14929
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    39
import java.security.Security;
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    40
import java.security.cert.Certificate;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    41
import java.security.cert.CertPath;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    42
import java.security.cert.CertStore;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    43
import java.security.cert.CollectionCertStoreParameters;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    44
import java.security.cert.PKIXBuilderParameters;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    45
import java.security.cert.PKIXCertPathBuilderResult;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    46
import java.security.cert.TrustAnchor;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    47
import java.security.cert.X509Certificate;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    48
import java.security.cert.X509CertSelector;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    49
import java.util.ArrayList;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    50
import java.util.Collections;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    51
import java.util.List;
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    52
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    53
public final class BuildEEBasicConstraints {
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    54
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
14929
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    56
        // reset the security property to make sure that the algorithms
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    57
        // and keys used in this test are not disabled.
59377f4b9919 7109274: Restrict the use of certificates with RSA keys less than 1024 bits
xuelei
parents: 5506
diff changeset
    58
        Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");
792
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    59
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    60
        X509Certificate rootCert = CertUtils.getCertFromFile("anchor.cer");
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    61
        TrustAnchor anchor = new TrustAnchor
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    62
            (rootCert.getSubjectX500Principal(), rootCert.getPublicKey(), null);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    63
        X509CertSelector sel = new X509CertSelector();
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    64
        sel.setBasicConstraints(-2);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    65
        PKIXBuilderParameters params = new PKIXBuilderParameters
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    66
            (Collections.singleton(anchor), sel);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    67
        params.setRevocationEnabled(false);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    68
        X509Certificate eeCert = CertUtils.getCertFromFile("ee.cer");
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    69
        X509Certificate caCert = CertUtils.getCertFromFile("ca.cer");
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    70
        ArrayList<X509Certificate> certs = new ArrayList<X509Certificate>();
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    71
        certs.add(caCert);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    72
        certs.add(eeCert);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    73
        CollectionCertStoreParameters ccsp =
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    74
            new CollectionCertStoreParameters(certs);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    75
        CertStore cs = CertStore.getInstance("Collection", ccsp);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    76
        params.addCertStore(cs);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    77
        PKIXCertPathBuilderResult res = CertUtils.build(params);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    78
        CertPath cp = res.getCertPath();
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    79
        // check that first certificate is an EE cert
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    80
        List<? extends Certificate> certList = cp.getCertificates();
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    81
        X509Certificate cert = (X509Certificate) certList.get(0);
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    82
        if (cert.getBasicConstraints() != -1) {
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    83
            throw new Exception("Target certificate is not an EE certificate");
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    84
        }
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    85
    }
214933bde360 6714842: CertPathBuilder returns incorrect CertPath for BasicConstraints in builderParams
mullan
parents:
diff changeset
    86
}