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