Home | History | Annotate | Download | only in ssl
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package javax.net.ssl;
     19 
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.KeyStore;
     22 import java.security.KeyStoreException;
     23 import java.security.NoSuchAlgorithmException;
     24 import java.security.NoSuchProviderException;
     25 import java.security.Provider;
     26 import java.security.Security;
     27 import org.apache.harmony.security.fortress.Engine;
     28 
     29 /**
     30  * The factory for {@code TrustManager}s based on {@code KeyStore} or provider
     31  * specific implementation.
     32  */
     33 public class TrustManagerFactory {
     34     // Store TrustManager service name
     35     private static final String SERVICE = "TrustManagerFactory";
     36 
     37     // Used to access common engine functionality
     38     private static final Engine ENGINE = new Engine(SERVICE);
     39 
     40     // Store default property name
     41     private static final String PROPERTYNAME = "ssl.TrustManagerFactory.algorithm";
     42 
     43     /**
     44      * Returns the default algorithm name for the {@code TrustManagerFactory}. The
     45      * default algorithm name is specified by the security property
     46      * {@code 'ssl.TrustManagerFactory.algorithm'}.
     47      *
     48      * @return the default algorithm name.
     49      */
     50     public static final String getDefaultAlgorithm() {
     51         return Security.getProperty(PROPERTYNAME);
     52     }
     53 
     54     /**
     55      * Creates a new {@code TrustManagerFactory} instance for the specified
     56      * trust management algorithm.
     57      *
     58      * @param algorithm
     59      *            the name of the requested trust management algorithm.
     60      * @return a trust manager factory for the requested algorithm.
     61      * @throws NoSuchAlgorithmException
     62      *             if no installed provider can provide the requested algorithm.
     63      * @throws NullPointerException
     64      *             if {@code algorithm} is {@code null} (instead of
     65      *             NoSuchAlgorithmException as in 1.4 release)
     66      */
     67     public static final TrustManagerFactory getInstance(String algorithm)
     68             throws NoSuchAlgorithmException {
     69         if (algorithm == null) {
     70             throw new NullPointerException("algorithm == null");
     71         }
     72         Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
     73         return new TrustManagerFactory((TrustManagerFactorySpi) sap.spi, sap.provider, algorithm);
     74     }
     75 
     76     /**
     77      * Creates a new {@code TrustManagerFactory} instance for the specified
     78      * trust management algorithm from the specified provider.
     79      *
     80      * @param algorithm
     81      *            the name of the requested trust management algorithm name.
     82      * @param provider
     83      *            the name of the provider that provides the requested
     84      *            algorithm.
     85      * @return a trust manager factory for the requested algorithm.
     86      * @throws NoSuchAlgorithmException
     87      *             if the specified provider cannot provide the requested
     88      *             algorithm.
     89      * @throws NoSuchProviderException
     90      *             if the specified provider does not exist.
     91      * @throws NullPointerException
     92      *             if {@code algorithm} is {@code null} (instead of
     93      *             NoSuchAlgorithmException as in 1.4 release)
     94      */
     95     public static final TrustManagerFactory getInstance(String algorithm, String provider)
     96             throws NoSuchAlgorithmException, NoSuchProviderException {
     97         if ((provider == null) || (provider.length() == 0)) {
     98             throw new IllegalArgumentException("Provider is null or empty");
     99         }
    100         Provider impProvider = Security.getProvider(provider);
    101         if (impProvider == null) {
    102             throw new NoSuchProviderException(provider);
    103         }
    104         return getInstance(algorithm, impProvider);
    105     }
    106 
    107     /**
    108      * Creates a new {@code TrustManagerFactory} instance for the specified
    109      * trust management algorithm from the specified provider.
    110      *
    111      * @param algorithm
    112      *            the name of the requested key management algorithm name.
    113      * @param provider
    114      *            the provider that provides the requested algorithm.
    115      * @return a key manager factory for the requested algorithm.
    116      * @throws NoSuchAlgorithmException
    117      *             if the specified provider cannot provide the requested
    118      *             algorithm.
    119      * @throws NullPointerException
    120      *             if {@code algorithm} is {@code null} (instead of
    121      *             NoSuchAlgorithmException as in 1.4 release)
    122      */
    123     public static final TrustManagerFactory getInstance(String algorithm, Provider provider)
    124             throws NoSuchAlgorithmException {
    125         if (provider == null) {
    126             throw new IllegalArgumentException("Provider is null");
    127         }
    128         if (algorithm == null) {
    129             throw new NullPointerException("algorithm == null");
    130         }
    131         Object spi = ENGINE.getInstance(algorithm, provider, null);
    132         return new TrustManagerFactory((TrustManagerFactorySpi) spi, provider, algorithm);
    133     }
    134 
    135     // Store used provider
    136     private final Provider provider;
    137 
    138     // Store used TrustManagerFactorySpi implementation
    139     private final TrustManagerFactorySpi spiImpl;
    140 
    141     // Store used algorithm
    142     private final String algorithm;
    143 
    144     /**
    145      * Creates a new {@code TrustManagerFactory} instance.
    146      *
    147      * @param factorySpi
    148      *            the implementation delegate.
    149      * @param provider
    150      *            the provider
    151      * @param algorithm
    152      *            the algorithm name.
    153      */
    154     protected TrustManagerFactory(TrustManagerFactorySpi factorySpi, Provider provider,
    155             String algorithm) {
    156         this.provider = provider;
    157         this.algorithm = algorithm;
    158         this.spiImpl = factorySpi;
    159     }
    160 
    161     /**
    162      * Returns the name of this {@code TrustManagerFactory} algorithm
    163      * implementation.
    164      *
    165      * @return the name of this {@code TrustManagerFactory} algorithm
    166      *         implementation.
    167      */
    168     public final String getAlgorithm() {
    169         return algorithm;
    170     }
    171 
    172     /**
    173      * Returns the provider for this {@code TrustManagerFactory} instance.
    174      *
    175      * @return the provider for this {@code TrustManagerFactory} instance.
    176      */
    177     public final Provider getProvider() {
    178         return provider;
    179     }
    180 
    181     /**
    182      * Initializes this factory instance with the specified keystore as source
    183      * of certificate authorities and trust material.
    184      *
    185      * @param ks
    186      *            the keystore or {@code null}.
    187      * @throws KeyStoreException
    188      *             if the initialization fails.
    189      */
    190     public final void init(KeyStore ks) throws KeyStoreException {
    191         spiImpl.engineInit(ks);
    192     }
    193 
    194     /**
    195      * Initializes this factory instance with the specified provider-specific
    196      * parameters for a source of trust material.
    197      *
    198      * @param spec
    199      *            the provider-specific parameters.
    200      * @throws InvalidAlgorithmParameterException
    201      *             if the initialization fails.
    202      */
    203     public final void init(ManagerFactoryParameters spec)
    204             throws InvalidAlgorithmParameterException {
    205         spiImpl.engineInit(spec);
    206     }
    207 
    208     /**
    209      * Returns the list of {@code TrustManager}s with one entry for each type
    210      * of trust material.
    211      *
    212      * @return the list of {@code TrustManager}s
    213      */
    214     public final TrustManager[] getTrustManagers() {
    215         return spiImpl.engineGetTrustManagers();
    216     }
    217 
    218 }
    219