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 java.security.UnrecoverableKeyException;
     28 import org.apache.harmony.security.fortress.Engine;
     29 
     30 /**
     31  * The public API for {@code KeyManagerFactory} implementations.
     32  */
     33 public class KeyManagerFactory {
     34     // Store KeyManagerFactory service name
     35     private static final String SERVICE = "KeyManagerFactory";
     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 PROPERTY_NAME = "ssl.KeyManagerFactory.algorithm";
     42 
     43     // Default value of KeyManagerFactory type.
     44     private static final String DEFAULT_PROPERTY = "PKIX";
     45 
     46     /**
     47      * Returns the default key manager factory algorithm name.
     48      * <p>
     49      * The default algorithm name is specified by the security property:
     50      * {@code 'ssl.KeyManagerFactory.algorithm'}.
     51      *
     52      * @return the default algorithm name.
     53      */
     54     public static final String getDefaultAlgorithm() {
     55         String algorithm = Security.getProperty(PROPERTY_NAME);
     56         return (algorithm != null ? algorithm : DEFAULT_PROPERTY);
     57     }
     58 
     59     /**
     60      * Creates a new {@code KeyManagerFactory} instance for the specified key
     61      * management algorithm.
     62      *
     63      * @param algorithm
     64      *            the name of the requested key management algorithm.
     65      * @return a key manager factory for the requested algorithm.
     66      * @throws NoSuchAlgorithmException
     67      *             if no installed provider can provide the requested algorithm.
     68      * @throws NullPointerException
     69      *             if {@code algorithm} is {@code null} (instead of
     70      *             NoSuchAlgorithmException as in 1.4 release)
     71      */
     72     public static final KeyManagerFactory getInstance(String algorithm)
     73             throws NoSuchAlgorithmException {
     74         if (algorithm == null) {
     75             throw new NullPointerException("algorithm == null");
     76         }
     77         Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
     78         return new KeyManagerFactory((KeyManagerFactorySpi) sap.spi, sap.provider, algorithm);
     79     }
     80 
     81     /**
     82      * Creates a new {@code KeyManagerFactory} instance for the specified key
     83      * management algorithm from the specified provider.
     84      *
     85      * @param algorithm
     86      *            the name of the requested key management algorithm name.
     87      * @param provider
     88      *            the name of the provider that provides the requested
     89      *            algorithm.
     90      * @return a key manager factory for the requested algorithm.
     91      * @throws NoSuchAlgorithmException
     92      *             if the specified provider cannot provide the requested
     93      *             algorithm.
     94      * @throws NoSuchProviderException
     95      *             if the specified provider does not exist.
     96      * @throws NullPointerException
     97      *             if {@code algorithm} is {@code null} (instead of
     98      *             NoSuchAlgorithmException as in 1.4 release)
     99      */
    100     public static final KeyManagerFactory getInstance(String algorithm, String provider)
    101             throws NoSuchAlgorithmException, NoSuchProviderException {
    102         if ((provider == null) || (provider.length() == 0)) {
    103             throw new IllegalArgumentException("Provider is null or empty");
    104         }
    105         Provider impProvider = Security.getProvider(provider);
    106         if (impProvider == null) {
    107             throw new NoSuchProviderException(provider);
    108         }
    109         return getInstance(algorithm, impProvider);
    110     }
    111 
    112     /**
    113      * Creates a new {@code KeyManagerFactory} instance for the specified key
    114      * management algorithm from the specified provider.
    115      *
    116      * @param algorithm
    117      *            the name of the requested key management algorithm name.
    118      * @param provider
    119      *            the provider that provides the requested algorithm.
    120      * @return a key manager factory for the requested algorithm.
    121      * @throws NoSuchAlgorithmException
    122      *             if the specified provider cannot provide the requested
    123      *             algorithm.
    124      * @throws NullPointerException
    125      *             if {@code algorithm} is {@code null} (instead of
    126      *             NoSuchAlgorithmException as in 1.4 release)
    127      */
    128     public static final KeyManagerFactory getInstance(String algorithm, Provider provider)
    129             throws NoSuchAlgorithmException {
    130         if (provider == null) {
    131             throw new IllegalArgumentException("Provider is null");
    132         }
    133         if (algorithm == null) {
    134             throw new NullPointerException("algorithm == null");
    135         }
    136         Object spi = ENGINE.getInstance(algorithm, provider, null);
    137         return new KeyManagerFactory((KeyManagerFactorySpi) spi, provider, algorithm);
    138     }
    139 
    140     // Store used provider
    141     private final Provider provider;
    142 
    143     // Store used KeyManagerFactorySpi implementation
    144     private final KeyManagerFactorySpi spiImpl;
    145 
    146     // Store used algorithm
    147     private final String algorithm;
    148 
    149     /**
    150      * Creates a new {@code KeyManagerFactory}.
    151      *
    152      * @param factorySpi
    153      *            the implementation delegate.
    154      * @param provider
    155      *            the provider.
    156      * @param algorithm
    157      *            the key management algorithm name.
    158      */
    159     protected KeyManagerFactory(KeyManagerFactorySpi factorySpi, Provider provider,
    160                                 String algorithm) {
    161         this.provider = provider;
    162         this.algorithm = algorithm;
    163         this.spiImpl = factorySpi;
    164     }
    165 
    166     /**
    167      * Returns the name of the key management algorithm.
    168      *
    169      * @return the name of the key management algorithm.
    170      */
    171     public final String getAlgorithm() {
    172         return algorithm;
    173     }
    174 
    175     /**
    176      * Returns the provider for this {@code KeyManagerFactory} instance.
    177      *
    178      * @return the provider for this {@code KeyManagerFactory} instance.
    179      */
    180     public final Provider getProvider() {
    181         return provider;
    182     }
    183 
    184     /**
    185      * Initializes this instance with the specified key store and password.
    186      *
    187      * @param ks
    188      *            the key store or {@code null} to use the default key store.
    189      * @param password
    190      *            the password for the specified key store or {@code null} if no
    191      *            key store is provided.
    192      * @throws KeyStoreException
    193      *             if initializing this key manager factory fails.
    194      * @throws NoSuchAlgorithmException
    195      *             if a required algorithm is not available.
    196      * @throws UnrecoverableKeyException
    197      *             if a key cannot be recovered.
    198      */
    199     public final void init(KeyStore ks, char[] password) throws KeyStoreException,
    200             NoSuchAlgorithmException, UnrecoverableKeyException {
    201         spiImpl.engineInit(ks, password);
    202     }
    203 
    204     /**
    205      * Initializes this instance with the specified factory parameters.
    206      *
    207      * @param spec
    208      *            the factory parameters.
    209      * @throws InvalidAlgorithmParameterException
    210      *             if an error occurs.
    211      */
    212     public final void init(ManagerFactoryParameters spec)
    213             throws InvalidAlgorithmParameterException {
    214         spiImpl.engineInit(spec);
    215     }
    216 
    217     /**
    218      * Returns a list of key managers, one instance for each type of key in the
    219      * key store.
    220      *
    221      * @return a list of key managers.
    222      */
    223     public final KeyManager[] getKeyManagers() {
    224         return spiImpl.engineGetKeyManagers();
    225     }
    226 }
    227