0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4459538
0N/A * @summary make sure a PKIX CertPathBuilder throws an
0N/A * InvalidAlgorithmParameterException if the target constraints
0N/A * specified in the PKIXBuilderParameters is not an instance of
0N/A * X509CertSelector.
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.security.cert.CertificateFactory;
0N/Aimport java.security.cert.CertPathBuilder;
0N/Aimport java.security.cert.CertPathBuilderResult;
0N/Aimport java.security.cert.PKIXBuilderParameters;
0N/Aimport java.security.cert.TrustAnchor;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport java.security.cert.CertSelector;
0N/A
0N/Aimport java.util.Collections;
0N/Aimport java.util.Set;
0N/A
0N/A/**
0N/A * BuildOddSel tries to perform a simple build of a certification path
0N/A * using the PKIX algorithm and a bogus target constraints CertSelector
0N/A * (one that is not an instance of X509CertSelector). On success, it should
0N/A * throw an InvalidAlgorithmParameterException.
0N/A *
0N/A * @author Steve Hanna
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class BuildOddSel {
0N/A
0N/A private static PKIXBuilderParameters params;
0N/A private static CertSelector sel;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A try {
0N/A createParams();
0N/A build(params);
0N/A throw new Exception
0N/A ("CertPath should not have been built successfully");
0N/A } catch (InvalidAlgorithmParameterException iape) {
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * CertSelector class that should cause SunCertPathBuilder to
0N/A * throw an InvalidAlgorithmParameterException.
0N/A */
0N/A static class OddSel implements CertSelector {
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A throw new UnknownError();
0N/A }
0N/A }
0N/A public boolean match(Certificate cert) {
0N/A return(false);
0N/A }
0N/A }
0N/A
0N/A public static void createParams() throws Exception {
0N/A TrustAnchor anchor = new TrustAnchor(getCertFromFile("sun.cer"), null);
0N/A Set anchors = Collections.singleton(anchor);
0N/A // Create odd CertSelector
0N/A sel = new OddSel();
0N/A params = new PKIXBuilderParameters(anchors, sel);
0N/A params.setRevocationEnabled(false);
0N/A }
0N/A
0N/A /**
0N/A * Get a DER-encoded X.509 certificate from a file.
0N/A *
0N/A * @param certFilePath path to file containing DER-encoded certificate
0N/A * @return X509Certificate
0N/A * @throws IOException on error
0N/A */
0N/A public static X509Certificate getCertFromFile(String certFilePath)
0N/A throws IOException {
0N/A X509Certificate cert = null;
0N/A try {
0N/A File certFile = new File(System.getProperty("test.src", "."),
0N/A certFilePath);
0N/A FileInputStream certFileInputStream =
0N/A new FileInputStream(certFile);
0N/A CertificateFactory cf = CertificateFactory.getInstance("X509");
0N/A cert = (X509Certificate)
0N/A cf.generateCertificate(certFileInputStream);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A throw new IOException("Can't construct X509Certificate: " +
0N/A e.getMessage());
0N/A }
0N/A return cert;
0N/A }
0N/A
0N/A /**
0N/A * Perform a PKIX build.
0N/A *
0N/A * @param params PKIXBuilderParameters to use in building
0N/A * @throws Exception on error
0N/A */
0N/A public static void build(PKIXBuilderParameters params)
0N/A throws Exception {
0N/A CertPathBuilder builder =
0N/A CertPathBuilder.getInstance("PKIX");
0N/A CertPathBuilderResult cpbr = builder.build(params);
0N/A }
0N/A}