OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / security / KeyPairGenerator.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.AlgorithmParameterSpec;
21 import org.apache.harmony.security.fortress.Engine;
22
23
24 /**
25  * {@code KeyPairGenerator} is an engine class which is capable of generating a
26  * private key and its related public key utilizing the algorithm it was
27  * initialized with.
28  *
29  * @see KeyPairGeneratorSpi
30  */
31 public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
32
33     // Store KeyPairGenerator SERVICE name
34     private static final String SERVICE = "KeyPairGenerator";
35
36     // Used to access common engine functionality
37     private static Engine engine = new Engine(SERVICE);
38
39     // Store SecureRandom
40     private static SecureRandom random = new SecureRandom();
41
42     // Store used provider
43     private Provider provider;
44
45     // Store used algorithm
46     private String algorithm;
47
48     /**
49      * Constructs a new instance of {@code KeyPairGenerator} with the name of
50      * the algorithm to use.
51      *
52      * @param algorithm
53      *            the name of algorithm to use
54      */
55     protected KeyPairGenerator(String algorithm) {
56         this.algorithm = algorithm;
57     }
58
59     /**
60      * Returns the name of the algorithm of this {@code KeyPairGenerator}.
61      *
62      * @return the name of the algorithm of this {@code KeyPairGenerator}
63      */
64     public String getAlgorithm() {
65         return algorithm;
66     }
67
68     /**
69      * Returns a new instance of {@code KeyPairGenerator} that utilizes the
70      * specified algorithm.
71      *
72      * @param algorithm
73      *            the name of the algorithm to use
74      * @return a new instance of {@code KeyPairGenerator} that utilizes the
75      *         specified algorithm
76      * @throws NoSuchAlgorithmException if the specified algorithm is not available
77      * @throws NullPointerException
78      *             if {@code algorithm} is {@code null}
79      */
80     public static KeyPairGenerator getInstance(String algorithm)
81             throws NoSuchAlgorithmException {
82         if (algorithm == null) {
83             throw new NullPointerException();
84         }
85         KeyPairGenerator result;
86         synchronized (engine) {
87             engine.getInstance(algorithm, null);
88             if (engine.spi instanceof KeyPairGenerator) {
89                 result = (KeyPairGenerator) engine.spi;
90                 result.algorithm = algorithm;
91                 result.provider = engine.provider;
92                 return result;
93             }
94             result = new KeyPairGeneratorImpl((KeyPairGeneratorSpi) engine.spi,
95                     engine.provider, algorithm);
96             return result;
97         }
98     }
99
100     /**
101      * Returns a new instance of {@code KeyPairGenerator} that utilizes the
102      * specified algorithm from the specified provider.
103      *
104      * @param algorithm
105      *            the name of the algorithm to use
106      * @param provider
107      *            the name of the provider
108      * @return a new instance of {@code KeyPairGenerator} that utilizes the
109      *         specified algorithm from the specified provider
110      * @throws NoSuchAlgorithmException if the specified algorithm is not available
111      * @throws NoSuchProviderException if the specified provider is not available
112      * @throws NullPointerException
113      *             if {@code algorithm} is {@code null}
114      * @throws IllegalArgumentException if {@code provider == null || provider.isEmpty()}
115      */
116     public static KeyPairGenerator getInstance(String algorithm, String provider)
117             throws NoSuchAlgorithmException, NoSuchProviderException {
118         if (provider == null || provider.isEmpty()) {
119             throw new IllegalArgumentException();
120         }
121         Provider impProvider = Security.getProvider(provider);
122         if (impProvider == null) {
123             throw new NoSuchProviderException(provider);
124         }
125         return getInstance(algorithm, impProvider);
126     }
127
128     /**
129      * Returns a new instance of {@code KeyPairGenerator} that utilizes the
130      * specified algorithm from the specified provider.
131      *
132      * @param algorithm
133      *            the name of the algorithm to use
134      * @param provider
135      *            the provider
136      * @return a new instance of {@code KeyPairGenerator} that utilizes the
137      *         specified algorithm from the specified provider
138      * @throws NoSuchAlgorithmException if the specified algorithm is not available
139      * @throws NullPointerException
140      *             if {@code algorithm} is {@code null}
141      * @throws IllegalArgumentException if {@code provider == null}
142      */
143     public static KeyPairGenerator getInstance(String algorithm,
144             Provider provider) throws NoSuchAlgorithmException {
145         if (provider == null) {
146             throw new IllegalArgumentException();
147         }
148         if (algorithm == null) {
149             throw new NullPointerException();
150         }
151         KeyPairGenerator result;
152         synchronized (engine) {
153             engine.getInstance(algorithm, provider, null);
154             if (engine.spi instanceof KeyPairGenerator) {
155                 result = (KeyPairGenerator) engine.spi;
156                 result.algorithm = algorithm;
157                 result.provider = provider;
158                 return result;
159             }
160             result = new KeyPairGeneratorImpl((KeyPairGeneratorSpi) engine.spi,
161                     provider, algorithm);
162             return result;
163         }
164     }
165
166     /**
167      * Returns the provider associated with this {@code KeyPairGenerator}.
168      *
169      * @return the provider associated with this {@code KeyPairGenerator}
170      */
171     public final Provider getProvider() {
172         return provider;
173     }
174
175     /**
176      * Initializes this {@code KeyPairGenerator} with the given key size. The
177      * default parameter set and a default {@code SecureRandom} instance will be
178      * used.
179      *
180      * @param keysize
181      *            the size of the key (number of bits)
182      */
183     public void initialize(int keysize) {
184         initialize(keysize, random);
185     }
186
187     /**
188      * Initializes this {@code KeyPairGenerator} with the given {@code
189      * AlgorithmParameterSpec}. A default {@code SecureRandom} instance will be
190      * used.
191      *
192      * @param param
193      *            the parameters to use
194      * @throws InvalidAlgorithmParameterException
195      *             if the specified parameters are not supported
196      */
197     public void initialize(AlgorithmParameterSpec param)
198             throws InvalidAlgorithmParameterException {
199         initialize(param, random);
200     }
201
202     /**
203      * Computes and returns a new unique {@code KeyPair} each time this method
204      * is called.
205      * <p>
206      * This does exactly the same as {@link #generateKeyPair()}.
207      *
208      * @return a new unique {@code KeyPair} each time this method is called
209      */
210     public final KeyPair genKeyPair() {
211         return generateKeyPair();
212     }
213
214     /**
215      * Computes and returns a new unique {@code KeyPair} each time this method
216      * is called.
217      * <p>
218      * This does exactly the same as {@link #genKeyPair()}.
219      *
220      * @return a new unique {@code KeyPair} each time this method is called
221      */
222     @Override
223     public KeyPair generateKeyPair() {
224         return null;
225     }
226
227     /**
228      * Initializes this {@code KeyPairGenerator} with the given key size and the
229      * given {@code SecureRandom}. The default parameter set will be used.
230      *
231      * @param keysize
232      *            the key size
233      * @param random
234      *            the source of randomness
235      */
236     @Override
237     public void initialize(int keysize, SecureRandom random) {
238     }
239
240     /**
241      * Initializes this {@code KeyPairGenerator} with the given {@code
242      * AlgorithmParameterSpec} and the given {@code SecureRandom}.
243      *
244      * @param param
245      *            the parameters to use
246      * @param random
247      *            the source of randomness
248      * @throws InvalidAlgorithmParameterException
249      *             if the specified parameters are not supported
250      */
251     @Override
252     public void initialize(AlgorithmParameterSpec param, SecureRandom random)
253             throws InvalidAlgorithmParameterException {
254     }
255
256     /**
257      *
258      * Internal class: KeyPairGenerator implementation
259      *
260      */
261     private static class KeyPairGeneratorImpl extends KeyPairGenerator {
262         // Save KeyPairGeneratorSpi
263         private KeyPairGeneratorSpi spiImpl;
264
265         // Implementation of KeyPaiGenerator constructor
266         //
267         // @param KeyPairGeneratorSpi
268         // @param provider
269         // @param algorithm
270         private KeyPairGeneratorImpl(KeyPairGeneratorSpi keyPairGeneratorSpi,
271                 Provider provider, String algorithm) {
272             super(algorithm);
273             super.provider = provider;
274             spiImpl = keyPairGeneratorSpi;
275         }
276
277         // implementation of initialize(int keysize, SecureRandom random)
278         // using corresponding spi initialize() method
279         @Override
280         public void initialize(int keysize, SecureRandom random) {
281             spiImpl.initialize(keysize, random);
282         }
283
284         // implementation of generateKeyPair()
285         // using corresponding spi generateKeyPair() method
286         @Override
287         public KeyPair generateKeyPair() {
288             return spiImpl.generateKeyPair();
289         }
290
291         // implementation of initialize(int keysize, SecureRandom random)
292         // using corresponding spi initialize() method
293         @Override
294         public void initialize(AlgorithmParameterSpec param, SecureRandom random)
295                 throws InvalidAlgorithmParameterException {
296             spiImpl.initialize(param, random);
297         }
298
299     }
300
301 }