Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / jce / sig / RSAKeyFactory.java
blob674e2afb6a504acc29823b004bec8598c80b3a14
1 /* RSAKeyFactory.java -- RSA key-factory JCE Adapter
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.jce.sig;
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.rsa.GnuRSAPrivateKey;
43 import gnu.java.security.key.rsa.GnuRSAPublicKey;
44 import gnu.java.security.key.rsa.RSAKeyPairPKCS8Codec;
45 import gnu.java.security.key.rsa.RSAKeyPairX509Codec;
47 import java.math.BigInteger;
48 import java.security.InvalidKeyException;
49 import java.security.Key;
50 import java.security.KeyFactorySpi;
51 import java.security.PrivateKey;
52 import java.security.PublicKey;
53 import java.security.interfaces.RSAPrivateCrtKey;
54 import java.security.interfaces.RSAPrivateKey;
55 import java.security.interfaces.RSAPublicKey;
56 import java.security.spec.InvalidKeySpecException;
57 import java.security.spec.KeySpec;
58 import java.security.spec.PKCS8EncodedKeySpec;
59 import java.security.spec.RSAPrivateCrtKeySpec;
60 import java.security.spec.RSAPrivateKeySpec;
61 import java.security.spec.RSAPublicKeySpec;
62 import java.security.spec.X509EncodedKeySpec;
64 public class RSAKeyFactory
65 extends KeyFactorySpi
67 // implicit 0-arguments constructor
69 protected PublicKey engineGeneratePublic(KeySpec keySpec)
70 throws InvalidKeySpecException
72 if (keySpec instanceof RSAPublicKeySpec)
74 RSAPublicKeySpec spec = (RSAPublicKeySpec) keySpec;
75 BigInteger n = spec.getModulus();
76 BigInteger e = spec.getPublicExponent();
77 return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
80 if (keySpec instanceof X509EncodedKeySpec)
82 X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
83 byte[] encoded = spec.getEncoded();
84 PublicKey result;
85 try
87 return new RSAKeyPairX509Codec().decodePublicKey(encoded);
89 catch (RuntimeException x)
91 InvalidKeySpecException y = new InvalidKeySpecException();
92 y.initCause(x);
93 throw y;
97 throw new InvalidKeySpecException("Unsupported (public) key specification");
100 protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
101 throws InvalidKeySpecException
103 if (keySpec instanceof RSAPrivateCrtKeySpec)
105 RSAPrivateCrtKeySpec spec = (RSAPrivateCrtKeySpec) keySpec;
106 BigInteger n = spec.getModulus();
107 BigInteger e = spec.getPublicExponent();
108 BigInteger d = spec.getPrivateExponent();
109 BigInteger p = spec.getPrimeP();
110 BigInteger q = spec.getPrimeQ();
111 BigInteger dP = spec.getPrimeExponentP();
112 BigInteger dQ = spec.getPrimeExponentQ();
113 BigInteger qInv = spec.getCrtCoefficient();
114 return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
115 n, e, d, p, q, dP, dQ, qInv);
118 // if (keySpec instanceof RSAPrivateKeySpec)
119 // {
120 // RSAPrivateKeySpec spec = (RSAPrivateKeySpec) keySpec;
121 // BigInteger n = spec.getModulus();
122 // BigInteger d = spec.getPrivateExponent();
123 // return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
124 // n, null, d, null, null, null, null, null);
125 // }
127 if (keySpec instanceof PKCS8EncodedKeySpec)
129 PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
130 byte[] encoded = spec.getEncoded();
131 PrivateKey result;
134 return new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
136 catch (RuntimeException x)
138 InvalidKeySpecException y = new InvalidKeySpecException();
139 y.initCause(x);
140 throw y;
144 throw new InvalidKeySpecException("Unsupported (private) key specification");
147 protected KeySpec engineGetKeySpec(Key key, Class keySpec)
148 throws InvalidKeySpecException
150 if (key instanceof RSAPublicKey)
152 if (keySpec.isAssignableFrom(RSAPublicKeySpec.class))
154 RSAPublicKey rsaKey = (RSAPublicKey) key;
155 BigInteger n = rsaKey.getModulus();
156 BigInteger e = rsaKey.getPublicExponent();
157 return new RSAPublicKeySpec(n, e);
160 if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
162 if (key instanceof GnuRSAPublicKey)
164 GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
165 byte[] encoded = rsaKey.getEncoded(Registry.X509_ENCODING_ID);
166 return new X509EncodedKeySpec(encoded);
169 if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
171 byte[] encoded = key.getEncoded();
172 return new X509EncodedKeySpec(encoded);
175 throw new InvalidKeySpecException("Wrong key type or unsupported (public) key specification");
178 throw new InvalidKeySpecException("Unsupported (public) key specification");
181 if ((key instanceof RSAPrivateCrtKey)
182 && keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class))
184 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
185 BigInteger n = rsaKey.getModulus();
186 BigInteger e = rsaKey.getPublicExponent();
187 BigInteger d = rsaKey.getPrivateExponent();
188 BigInteger p = rsaKey.getPrimeP();
189 BigInteger q = rsaKey.getPrimeQ();
190 BigInteger dP = rsaKey.getPrimeExponentP();
191 BigInteger dQ = rsaKey.getPrimeExponentQ();
192 BigInteger qInv = rsaKey.getCrtCoefficient();
193 return new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv);
196 if ((key instanceof RSAPrivateKey)
197 && keySpec.isAssignableFrom(RSAPrivateKeySpec.class))
199 RSAPrivateKey rsaKey = (RSAPrivateKey) key;
200 BigInteger n = rsaKey.getModulus();
201 BigInteger d = rsaKey.getPrivateExponent();
202 return new RSAPrivateKeySpec(n, d);
205 if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
207 if (key instanceof GnuRSAPrivateKey)
209 GnuRSAPrivateKey rsaKey = (GnuRSAPrivateKey) key;
210 byte[] encoded = rsaKey.getEncoded(Registry.PKCS8_ENCODING_ID);
211 return new PKCS8EncodedKeySpec(encoded);
214 if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
216 byte[] encoded = key.getEncoded();
217 return new PKCS8EncodedKeySpec(encoded);
220 throw new InvalidKeySpecException("Wrong key type or unsupported (private) key specification");
223 throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
226 protected Key engineTranslateKey(Key key) throws InvalidKeyException
228 if ((key instanceof GnuRSAPublicKey) || (key instanceof GnuRSAPrivateKey))
229 return key;
231 if (key instanceof RSAPublicKey)
233 RSAPublicKey rsaKey = (RSAPublicKey) key;
234 BigInteger n = rsaKey.getModulus();
235 BigInteger e = rsaKey.getPublicExponent();
236 return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
239 if (key instanceof RSAPrivateCrtKey)
241 RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
242 BigInteger n = rsaKey.getModulus();
243 BigInteger e = rsaKey.getPublicExponent();
244 BigInteger d = rsaKey.getPrivateExponent();
245 BigInteger p = rsaKey.getPrimeP();
246 BigInteger q = rsaKey.getPrimeQ();
247 BigInteger dP = rsaKey.getPrimeExponentP();
248 BigInteger dQ = rsaKey.getPrimeExponentQ();
249 BigInteger qInv = rsaKey.getCrtCoefficient();
250 return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
251 n, e, d, p, q, dP, dQ, qInv);
254 // if (key instanceof RSAPrivateKey)
255 // {
256 // RSAPrivateKey rsaKey = (RSAPrivateKey) key;
257 // BigInteger n = rsaKey.getModulus();
258 // BigInteger d = rsaKey.getPrivateExponent();
259 // return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
260 // n, null, d, null, null, null, null, null);
261 // }
263 throw new InvalidKeyException("Unsupported key type");