0N/A/*
3909N/A * Copyright (c) 2000, 2011, 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
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/Apackage java.security.cert;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.InvalidAlgorithmParameterException;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.NoSuchProviderException;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.Provider;
0N/Aimport java.security.Security;
0N/Aimport sun.security.util.Debug;
0N/A
0N/Aimport sun.security.jca.*;
0N/Aimport sun.security.jca.GetInstance.Instance;
0N/A
0N/A/**
0N/A * A class for building certification paths (also known as certificate chains).
0N/A * <p>
0N/A * This class uses a provider-based architecture.
0N/A * To create a <code>CertPathBuilder</code>, call
0N/A * one of the static <code>getInstance</code> methods, passing in the
0N/A * algorithm name of the <code>CertPathBuilder</code> desired and optionally
0N/A * the name of the provider desired.
0N/A * <p>
0N/A * Once a <code>CertPathBuilder</code> object has been created, certification
0N/A * paths can be constructed by calling the {@link #build build} method and
0N/A * passing it an algorithm-specific set of parameters. If successful, the
0N/A * result (including the <code>CertPath</code> that was built) is returned
0N/A * in an object that implements the <code>CertPathBuilderResult</code>
0N/A * interface.
3465N/A *
3465N/A * <p> Every implementation of the Java platform is required to support the
3465N/A * following standard <code>CertPathBuilder</code> algorithm:
3465N/A * <ul>
3465N/A * <li><tt>PKIX</tt></li>
3465N/A * </ul>
3465N/A * This algorithm is described in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathBuilder">
3465N/A * CertPathBuilder section</a> of the
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation.
3465N/A * Consult the release documentation for your implementation to see if any
3465N/A * other algorithms are supported.
3465N/A *
0N/A * <p>
0N/A * <b>Concurrent Access</b>
0N/A * <p>
0N/A * The static methods of this class are guaranteed to be thread-safe.
0N/A * Multiple threads may concurrently invoke the static methods defined in
0N/A * this class with no ill effects.
0N/A * <p>
0N/A * However, this is not true for the non-static methods defined by this class.
0N/A * Unless otherwise documented by a specific provider, threads that need to
0N/A * access a single <code>CertPathBuilder</code> instance concurrently should
0N/A * synchronize amongst themselves and provide the necessary locking. Multiple
0N/A * threads each manipulating a different <code>CertPathBuilder</code> instance
0N/A * need not synchronize.
0N/A *
0N/A * @see CertPath
0N/A *
0N/A * @since 1.4
0N/A * @author Sean Mullan
0N/A * @author Yassir Elley
0N/A */
0N/Apublic class CertPathBuilder {
0N/A
0N/A /*
0N/A * Constant to lookup in the Security properties file to determine
0N/A * the default certpathbuilder type. In the Security properties file,
0N/A * the default certpathbuilder type is given as:
0N/A * <pre>
0N/A * certpathbuilder.type=PKIX
0N/A * </pre>
0N/A */
0N/A private static final String CPB_TYPE = "certpathbuilder.type";
0N/A private static final Debug debug = Debug.getInstance("certpath");
0N/A private CertPathBuilderSpi builderSpi;
0N/A private Provider provider;
0N/A private String algorithm;
0N/A
0N/A /**
0N/A * Creates a <code>CertPathBuilder</code> object of the given algorithm,
0N/A * and encapsulates the given provider implementation (SPI object) in it.
0N/A *
0N/A * @param builderSpi the provider implementation
0N/A * @param provider the provider
0N/A * @param algorithm the algorithm name
0N/A */
0N/A protected CertPathBuilder(CertPathBuilderSpi builderSpi, Provider provider,
0N/A String algorithm)
0N/A {
0N/A this.builderSpi = builderSpi;
0N/A this.provider = provider;
0N/A this.algorithm = algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * <p> This method traverses the list of registered security Providers,
0N/A * starting with the most preferred Provider.
0N/A * A new CertPathBuilder object encapsulating the
0N/A * CertPathBuilderSpi implementation from the first
0N/A * Provider that supports the specified algorithm is returned.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param algorithm the name of the requested <code>CertPathBuilder</code>
3465N/A * algorithm. See the CertPathBuilder section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathBuilder">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
3465N/A * for information about standard algorithm names.
0N/A *
0N/A * @return a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * @throws NoSuchAlgorithmException if no Provider supports a
0N/A * CertPathBuilderSpi implementation for the
0N/A * specified algorithm.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static CertPathBuilder getInstance(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A Instance instance = GetInstance.getInstance("CertPathBuilder",
0N/A CertPathBuilderSpi.class, algorithm);
0N/A return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * <p> A new CertPathBuilder object encapsulating the
0N/A * CertPathBuilderSpi implementation from the specified provider
0N/A * is returned. The specified provider must be registered
0N/A * in the security provider list.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param algorithm the name of the requested <code>CertPathBuilder</code>
3465N/A * algorithm. See the CertPathBuilder section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathBuilder">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
3465N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a CertPathBuilderSpi
0N/A * implementation for the specified algorithm is not
0N/A * available from the specified provider.
0N/A *
0N/A * @throws NoSuchProviderException if the specified provider is not
0N/A * registered in the security provider list.
0N/A *
0N/A * @exception IllegalArgumentException if the <code>provider</code> is
0N/A * null or empty.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static CertPathBuilder getInstance(String algorithm, String provider)
0N/A throws NoSuchAlgorithmException, NoSuchProviderException {
0N/A Instance instance = GetInstance.getInstance("CertPathBuilder",
0N/A CertPathBuilderSpi.class, algorithm, provider);
0N/A return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * <p> A new CertPathBuilder object encapsulating the
0N/A * CertPathBuilderSpi implementation from the specified Provider
0N/A * object is returned. Note that the specified Provider object
0N/A * does not have to be registered in the provider list.
0N/A *
0N/A * @param algorithm the name of the requested <code>CertPathBuilder</code>
3465N/A * algorithm. See the CertPathBuilder section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#CertPathBuilder">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
3465N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider the provider.
0N/A *
0N/A * @return a <code>CertPathBuilder</code> object that implements the
0N/A * specified algorithm.
0N/A *
0N/A * @exception NoSuchAlgorithmException if a CertPathBuilderSpi
0N/A * implementation for the specified algorithm is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @exception IllegalArgumentException if the <code>provider</code> is
0N/A * null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static CertPathBuilder getInstance(String algorithm,
0N/A Provider provider) throws NoSuchAlgorithmException {
0N/A Instance instance = GetInstance.getInstance("CertPathBuilder",
0N/A CertPathBuilderSpi.class, algorithm, provider);
0N/A return new CertPathBuilder((CertPathBuilderSpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this <code>CertPathBuilder</code>.
0N/A *
0N/A * @return the provider of this <code>CertPathBuilder</code>
0N/A */
0N/A public final Provider getProvider() {
0N/A return this.provider;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the algorithm of this <code>CertPathBuilder</code>.
0N/A *
0N/A * @return the name of the algorithm of this <code>CertPathBuilder</code>
0N/A */
0N/A public final String getAlgorithm() {
0N/A return this.algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Attempts to build a certification path using the specified algorithm
0N/A * parameter set.
0N/A *
0N/A * @param params the algorithm parameters
0N/A * @return the result of the build algorithm
0N/A * @throws CertPathBuilderException if the builder is unable to construct
0N/A * a certification path that satisfies the specified parameters
0N/A * @throws InvalidAlgorithmParameterException if the specified parameters
0N/A * are inappropriate for this <code>CertPathBuilder</code>
0N/A */
0N/A public final CertPathBuilderResult build(CertPathParameters params)
0N/A throws CertPathBuilderException, InvalidAlgorithmParameterException
0N/A {
0N/A return builderSpi.engineBuild(params);
0N/A }
0N/A
0N/A /**
0N/A * Returns the default <code>CertPathBuilder</code> type as specified in
0N/A * the Java security properties file, or the string &quot;PKIX&quot;
0N/A * if no such property exists. The Java security properties file is
0N/A * located in the file named &lt;JAVA_HOME&gt;/lib/security/java.security.
0N/A * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
0N/A * and specifies the directory where the JRE is installed.
0N/A *
0N/A * <p>The default <code>CertPathBuilder</code> type can be used by
0N/A * applications that do not want to use a hard-coded type when calling one
0N/A * of the <code>getInstance</code> methods, and want to provide a default
0N/A * type in case a user does not specify its own.
0N/A *
0N/A * <p>The default <code>CertPathBuilder</code> type can be changed by
0N/A * setting the value of the "certpathbuilder.type" security property
0N/A * (in the Java security properties file) to the desired type.
0N/A *
0N/A * @return the default <code>CertPathBuilder</code> type as specified
0N/A * in the Java security properties file, or the string &quot;PKIX&quot;
0N/A * if no such property exists.
0N/A */
0N/A public final static String getDefaultType() {
0N/A String cpbtype;
0N/A cpbtype = AccessController.doPrivileged(new PrivilegedAction<String>() {
0N/A public String run() {
0N/A return Security.getProperty(CPB_TYPE);
0N/A }
0N/A });
0N/A if (cpbtype == null) {
0N/A cpbtype = "PKIX";
0N/A }
0N/A return cpbtype;
0N/A }
0N/A}