Path: blob/master/test/jdk/java/security/cert/CertPathBuilder/targetConstraints/BuildEEBasicConstraints.java
41161 views
/*1* Copyright (c) 2008, 2019, 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// This test case relies on updated static security property, no way to re-use24// security property in samevm/agentvm mode.2526/**27* @test28* @bug 671484229* @library /test/lib30* @run main/othervm BuildEEBasicConstraints31* @summary make sure a PKIX CertPathBuilder builds a path to an32* end entity certificate when the setBasicConstraints method of the33* X509CertSelector of the targetConstraints PKIXBuilderParameters34* parameter is set to -2.35*/3637import java.security.Security;38import java.security.cert.Certificate;39import java.security.cert.CertPath;40import java.security.cert.CertStore;41import java.security.cert.CollectionCertStoreParameters;42import java.security.cert.PKIXBuilderParameters;43import java.security.cert.PKIXCertPathBuilderResult;44import java.security.cert.TrustAnchor;45import java.security.cert.X509Certificate;46import java.security.cert.X509CertSelector;47import java.util.ArrayList;48import java.util.Collections;49import java.util.List;50import jdk.test.lib.security.CertUtils;5152public final class BuildEEBasicConstraints {5354public static void main(String[] args) throws Exception {55// reset the security property to make sure that the algorithms56// and keys used in this test are not disabled.57Security.setProperty("jdk.certpath.disabledAlgorithms", "MD2");5859X509Certificate rootCert = CertUtils.getCertFromFile("anchor.cer");60TrustAnchor anchor = new TrustAnchor61(rootCert.getSubjectX500Principal(), rootCert.getPublicKey(), null);62X509CertSelector sel = new X509CertSelector();63sel.setBasicConstraints(-2);64PKIXBuilderParameters params = new PKIXBuilderParameters65(Collections.singleton(anchor), sel);66params.setRevocationEnabled(false);67X509Certificate eeCert = CertUtils.getCertFromFile("ee.cer");68X509Certificate caCert = CertUtils.getCertFromFile("ca.cer");69ArrayList<X509Certificate> certs = new ArrayList<X509Certificate>();70certs.add(caCert);71certs.add(eeCert);72CollectionCertStoreParameters ccsp =73new CollectionCertStoreParameters(certs);74CertStore cs = CertStore.getInstance("Collection", ccsp);75params.addCertStore(cs);76PKIXCertPathBuilderResult res = CertUtils.build(params);77CertPath cp = res.getCertPath();78// check that first certificate is an EE cert79List<? extends Certificate> certList = cp.getCertificates();80X509Certificate cert = (X509Certificate) certList.get(0);81if (cert.getBasicConstraints() != -1) {82throw new Exception("Target certificate is not an EE certificate");83}84}85}868788