KeyFactory.java revision 6cdb6b7e6939270ccd21790ec95e42197cefc0c3
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
18package java.security;
19
20import java.security.spec.InvalidKeySpecException;
21import java.security.spec.KeySpec;
22import org.apache.harmony.security.fortress.Engine;
23
24/**
25 * {@code KeyFactory} is an engine class that can be used to translate between
26 * public and private key objects and convert keys between their external
27 * representation, that can be easily transported and their internal
28 * representation.
29 */
30public class KeyFactory {
31    // The service name.
32    private static final String SERVICE = "KeyFactory";
33
34    // Used to access common engine functionality
35    private static final Engine ENGINE = new Engine(SERVICE);
36
37    // The provider
38    private final Provider provider;
39
40    // The SPI implementation.
41    private final KeyFactorySpi spiImpl;
42
43    // The algorithm.
44    private final String algorithm;
45
46    /**
47     * Constructs a new instance of {@code KeyFactory} with the specified
48     * arguments.
49     *
50     * @param keyFacSpi
51     *            the concrete key factory service.
52     * @param provider
53     *            the provider.
54     * @param algorithm
55     *            the algorithm to use.
56     */
57    protected KeyFactory(KeyFactorySpi keyFacSpi,
58                         Provider provider,
59                         String algorithm) {
60        this.provider = provider;
61        this.algorithm = algorithm;
62        this.spiImpl = keyFacSpi;
63    }
64
65    /**
66     * Returns a new instance of {@code KeyFactory} that utilizes the specified
67     * algorithm.
68     *
69     * @param algorithm
70     *            the name of the algorithm.
71     * @return a new instance of {@code KeyFactory} that utilizes the specified
72     *         algorithm.
73     * @throws NoSuchAlgorithmException
74     *             if no provider provides the requested algorithm.
75     */
76    public static KeyFactory getInstance(String algorithm)
77            throws NoSuchAlgorithmException {
78        if (algorithm == null) {
79            throw new NullPointerException();
80        }
81        Engine.SpiAndProvider sap = ENGINE.getInstance(algorithm, null);
82        return new KeyFactory((KeyFactorySpi) sap.spi, sap.provider, algorithm);
83    }
84
85    /**
86     * Returns a new instance of {@code KeyFactory} that utilizes the specified
87     * algorithm from the specified provider.
88     *
89     * @param algorithm
90     *            the name of the algorithm.
91     * @param provider
92     *            the name of the provider.
93     * @return a new instance of {@code KeyFactory} that utilizes the specified
94     *         algorithm from the specified provider.
95     * @throws NoSuchAlgorithmException
96     *             if the provider does not provide the requested algorithm.
97     * @throws NoSuchProviderException
98     *             if the requested provider is not available.
99     * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
100     */
101    @SuppressWarnings("nls")
102    public static KeyFactory getInstance(String algorithm, String provider)
103                                throws NoSuchAlgorithmException, NoSuchProviderException {
104        if (provider == null || provider.isEmpty()) {
105            throw new IllegalArgumentException();
106        }
107        Provider p = Security.getProvider(provider);
108        if (p == null) {
109            throw new NoSuchProviderException(provider);
110        }
111        return getInstance(algorithm, p);
112    }
113
114    /**
115     * Returns a new instance of {@code KeyFactory} that utilizes the specified
116     * algorithm from the specified provider.
117     *
118     * @param algorithm
119     *            the name of the algorithm.
120     * @param provider
121     *            the security provider.
122     * @return a new instance of {@code KeyFactory} that utilizes the specified
123     *         algorithm from the specified provider.
124     * @throws NoSuchAlgorithmException
125     *             if the provider does not provide the requested algorithm.
126     * @throws IllegalArgumentException if {@code provider == null}
127     */
128    public static KeyFactory getInstance(String algorithm, Provider provider)
129                                 throws NoSuchAlgorithmException {
130        if (provider == null) {
131            throw new IllegalArgumentException();
132        }
133        if (algorithm == null) {
134            throw new NullPointerException();
135        }
136        Object spi = ENGINE.getInstance(algorithm, provider, null);
137        return new KeyFactory((KeyFactorySpi) spi, provider, algorithm);
138    }
139
140    /**
141     * Returns the provider associated with this {@code KeyFactory}.
142     *
143     * @return the provider associated with this {@code KeyFactory}.
144     */
145    public final Provider getProvider() {
146        return provider;
147    }
148
149    /**
150     * Returns the name of the algorithm associated with this {@code
151     * KeyFactory}.
152     *
153     * @return the name of the algorithm associated with this {@code
154     *         KeyFactory}.
155     */
156    public final String getAlgorithm() {
157        return algorithm;
158    }
159
160    /**
161     * Generates a instance of {@code PublicKey} from the given key
162     * specification.
163     *
164     * @param keySpec
165     *            the specification of the public key
166     * @return the public key
167     * @throws InvalidKeySpecException
168     *             if the specified {@code keySpec} is invalid
169     */
170    public final PublicKey generatePublic(KeySpec keySpec)
171                                throws InvalidKeySpecException {
172        return spiImpl.engineGeneratePublic(keySpec);
173    }
174
175    /**
176     * Generates a instance of {@code PrivateKey} from the given key
177     * specification.
178     *
179     * @param keySpec
180     *            the specification of the private key.
181     * @return the private key.
182     * @throws InvalidKeySpecException
183     *             if the specified {@code keySpec} is invalid.
184     */
185    public final PrivateKey generatePrivate(KeySpec keySpec)
186                                throws InvalidKeySpecException {
187        return spiImpl.engineGeneratePrivate(keySpec);
188    }
189
190    /**
191     * Returns the key specification for the specified key.
192     *
193     * @param key
194     *            the key from which the specification is requested.
195     * @param keySpec
196     *            the type of the requested {@code KeySpec}.
197     * @return the key specification for the specified key.
198     * @throws InvalidKeySpecException
199     *             if the key can not be processed, or the requested requested
200     *             {@code KeySpec} is inappropriate for the given key.
201     */
202    public final <T extends KeySpec> T getKeySpec(Key key,
203                                    Class<T> keySpec)
204                            throws InvalidKeySpecException {
205        return spiImpl.engineGetKeySpec(key, keySpec);
206    }
207
208    /**
209     * Translates the given key into a key from this key factory.
210     *
211     * @param key
212     *            the key to translate.
213     * @return the translated key.
214     * @throws InvalidKeyException
215     *             if the specified key can not be translated by this key
216     *             factory.
217     */
218    public final Key translateKey(Key key)
219                        throws InvalidKeyException {
220        return spiImpl.engineTranslateKey(key);
221    }
222}
223