OSDN Git Service

7bbc355391b12d29ad6516c724bd5e41a4f9afb9
[pf3gnuchains/gcc-fork.git] / libjava / java / security / KeyFactory.java
1 /* KeyFactory.java --- Key Factory Class
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
26
27 package java.security;
28 import java.security.spec.KeySpec;
29 import java.security.spec.InvalidKeySpecException;
30
31 /**
32    Key factories are used to convert keys (opaque cryptographic 
33    keys of type Key) into key specifications (transparent 
34    representations of the underlying key material).
35
36    Key factories are bi-directional. They allow a key class 
37    to be converted into a key specification (key material) and
38    back again.
39
40    For example DSA public keys can be specified as 
41    DSAPublicKeySpec or X509EncodedKeySpec. The key factory
42    translate these key specifications. 
43
44    @since JDK 1.2
45    @author Mark Benvenuto
46  */
47 public class KeyFactory
48 {
49   private KeyFactorySpi keyFacSpi;
50   private Provider provider;
51   private String algorithm;
52
53   /**
54      Constructs a new keyFactory with the specified parameters.
55
56      @param keyFacSpi Key Factory SPI to use
57      @param provider the provider of the Key Factory SPI
58      @param algorithm the name of the key algorithm for this key factory
59    */
60   protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
61                        String algorithm)
62   {
63     this.keyFacSpi = keyFacSpi;
64     this.provider = provider;
65     this.algorithm = algorithm;
66   }
67
68   /** 
69      Gets an instance of the KeyFactory class representing
70      the specified key factory. If the algorithm is not 
71      found then, it throws NoSuchAlgorithmException.
72
73      @param algorithm the name of algorithm to choose
74      @return a KeyFactory repesenting the desired algorithm
75
76      @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
77    */
78   public static KeyFactory getInstance(String algorithm)
79     throws NoSuchAlgorithmException
80   {
81     Provider[] p = Security.getProviders();
82
83     for (int i = 0; i < p.length; i++)
84       {
85         String classname = p[i].getProperty("KeyFactory." + algorithm);
86         if (classname != null)
87           return getInstance(classname, algorithm, p[i]);
88       }
89
90     throw new NoSuchAlgorithmException(algorithm);
91   }
92
93   /** 
94      Gets an instance of the KeyFactory class representing
95      the specified key factory from the specified provider. 
96      If the algorithm is not found then, it throws 
97      NoSuchAlgorithmException. If the provider is not found, then 
98      it throws NoSuchProviderException.
99
100      @param algorithm the name of algorithm to choose
101      @param provider the name of the provider to find the algorithm in
102      @return a KeyFactory repesenting the desired algorithm
103
104      @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
105      @throws NoSuchProviderException if the provider is not found
106    */
107   public static KeyFactory getInstance(String algorithm, String provider)
108     throws NoSuchAlgorithmException, NoSuchProviderException
109   {
110     Provider p = Security.getProvider(provider);
111     if (p == null)
112       throw new NoSuchProviderException();
113
114     return getInstance(p.getProperty("KeyFactory." + algorithm),
115                        algorithm, p);
116   }
117
118   private static KeyFactory getInstance(String classname,
119                                         String algorithm,
120                                         Provider provider)
121     throws NoSuchAlgorithmException
122   {
123
124     try
125       {
126         return new KeyFactory((KeyFactorySpi) Class.forName(classname).
127                               newInstance(), provider, algorithm);
128       }
129     catch (ClassNotFoundException cnfe)
130       {
131         throw new NoSuchAlgorithmException("Class not found");
132       }
133     catch (InstantiationException ie)
134       {
135         throw new NoSuchAlgorithmException("Class instantiation failed");
136       }
137     catch (IllegalAccessException iae)
138       {
139         throw new NoSuchAlgorithmException("Illegal Access");
140       }
141   }
142
143   /**
144      Gets the provider that the class is from.
145
146      @return the provider of this class
147    */
148   public final Provider getProvider()
149   {
150     return provider;
151   }
152
153   /**
154      Returns the name of the algorithm used
155
156      @return A string with the name of the algorithm
157    */
158   public final String getAlgorithm()
159   {
160     return algorithm;
161   }
162
163   /**
164      Generates a public key from the provided key specification.
165
166      @param keySpec key specification
167
168      @return the public key
169
170      @throws InvalidKeySpecException invalid key specification for
171      this key factory to produce a public key
172    */
173   public final PublicKey generatePublic(KeySpec keySpec) throws
174     InvalidKeySpecException
175   {
176     return keyFacSpi.engineGeneratePublic(keySpec);
177   }
178
179   /**
180      Generates a private key from the provided key specification.
181
182      @param keySpec key specification
183
184      @return the private key
185
186      @throws InvalidKeySpecException invalid key specification for
187      this key factory to produce a private key
188    */
189   public final PrivateKey generatePrivate(KeySpec keySpec) throws
190     InvalidKeySpecException
191   {
192     return keyFacSpi.engineGeneratePrivate(keySpec);
193   }
194
195   /**
196      Returns a key specification for the given key. keySpec 
197      identifies the specification class to return the key 
198      material in.
199
200      @param key the key
201      @param keySpec the specification class to return the 
202      key material in.
203
204      @return the key specification in an instance of the requested
205      specification class
206
207      @throws InvalidKeySpecException the requested key specification
208      is inappropriate for this key or the key is 
209      unrecognized.
210    */
211   public final KeySpec getKeySpec(Key key, Class keySpec)
212     throws InvalidKeySpecException
213   {
214     return keyFacSpi.engineGetKeySpec(key, keySpec);
215   }
216
217   /**
218      Translates the key from an unknown or untrusted provider
219      into a key for this key factory.
220
221      @param the key from an unknown or untrusted provider
222
223      @return the translated key
224
225      @throws InvalidKeySpecException if the key cannot be 
226      processed by this key factory
227    */
228   public final Key translateKey(Key key) throws InvalidKeyException
229   {
230     return keyFacSpi.engineTranslateKey(key);
231   }
232 }