OSDN Git Service

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