004-11-15 Andreas Tobler <a.tobler@schweiz.ch>
[official-gcc.git] / libjava / gnu / java / security / provider / RSAKeyFactory.java
blob33c8c2287e4813ed2802bca5b0fc44c9076336cb
1 /* RSAKeyFactory.java -- RSA key factory.
2 Copyright (C) 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.provider;
41 import java.security.InvalidKeyException;
42 import java.security.Key;
43 import java.security.KeyFactorySpi;
44 import java.security.PrivateKey;
45 import java.security.PublicKey;
47 import java.security.interfaces.RSAPrivateCrtKey;
48 import java.security.interfaces.RSAPrivateKey;
49 import java.security.interfaces.RSAPublicKey;
51 import java.security.spec.InvalidKeySpecException;
52 import java.security.spec.KeySpec;
53 import java.security.spec.PKCS8EncodedKeySpec;
54 import java.security.spec.RSAPrivateCrtKeySpec;
55 import java.security.spec.RSAPrivateKeySpec;
56 import java.security.spec.RSAPublicKeySpec;
57 import java.security.spec.X509EncodedKeySpec;
59 public class RSAKeyFactory extends KeyFactorySpi
62 // Default constructor.
63 // -------------------------------------------------------------------------
65 // Instance methods.
66 // -------------------------------------------------------------------------
68 protected PrivateKey engineGeneratePrivate(KeySpec spec)
69 throws InvalidKeySpecException
71 if (spec instanceof RSAPrivateCrtKeySpec)
73 return new GnuRSAPrivateKey((RSAPrivateCrtKeySpec) spec);
75 if (spec instanceof RSAPrivateKeySpec)
77 return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
78 ((RSAPrivateKeySpec) spec).getModulus(), null,
79 ((RSAPrivateKeySpec) spec).getPrivateExponent(), null,
80 null, null, null, null));
82 if (spec instanceof PKCS8EncodedKeySpec)
84 EncodedKeyFactory ekf = new EncodedKeyFactory();
85 PrivateKey pk = ekf.engineGeneratePrivate(spec);
86 if (pk instanceof RSAPrivateKey)
87 return pk;
89 throw new InvalidKeySpecException();
92 protected PublicKey engineGeneratePublic(KeySpec spec)
93 throws InvalidKeySpecException
95 if (spec instanceof RSAPublicKeySpec)
97 return new GnuRSAPublicKey((RSAPublicKeySpec) spec);
99 if (spec instanceof X509EncodedKeySpec)
101 EncodedKeyFactory ekf = new EncodedKeyFactory();
102 PublicKey pk = ekf.engineGeneratePublic(spec);
103 if (pk instanceof RSAPublicKey)
104 return pk;
106 throw new InvalidKeySpecException();
109 protected KeySpec engineGetKeySpec(Key key, Class keySpec)
110 throws InvalidKeySpecException
112 if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class)
113 && (key instanceof RSAPrivateCrtKey))
115 return new RSAPrivateCrtKeySpec(
116 ((RSAPrivateCrtKey) key).getModulus(),
117 ((RSAPrivateCrtKey) key).getPublicExponent(),
118 ((RSAPrivateCrtKey) key).getPrivateExponent(),
119 ((RSAPrivateCrtKey) key).getPrimeP(),
120 ((RSAPrivateCrtKey) key).getPrimeQ(),
121 ((RSAPrivateCrtKey) key).getPrimeExponentP(),
122 ((RSAPrivateCrtKey) key).getPrimeExponentQ(),
123 ((RSAPrivateCrtKey) key).getCrtCoefficient());
125 if (keySpec.isAssignableFrom(RSAPrivateKeySpec.class)
126 && (key instanceof RSAPrivateKey))
128 return new RSAPrivateKeySpec(
129 ((RSAPrivateCrtKey) key).getModulus(),
130 ((RSAPrivateCrtKey) key).getPrivateExponent());
132 if (keySpec.isAssignableFrom(RSAPublicKeySpec.class)
133 && (key instanceof RSAPublicKey))
135 return new RSAPublicKeySpec(
136 ((RSAPrivateCrtKey) key).getModulus(),
137 ((RSAPrivateCrtKey) key).getPublicExponent());
139 if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)
140 && key.getFormat().equalsIgnoreCase("PKCS#8"))
142 return new PKCS8EncodedKeySpec(key.getEncoded());
144 if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)
145 && key.getFormat().equalsIgnoreCase("X.509"))
147 return new X509EncodedKeySpec(key.getEncoded());
149 throw new InvalidKeySpecException();
152 protected Key engineTranslateKey(Key key) throws InvalidKeyException
154 if (key instanceof RSAPrivateCrtKey)
156 return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
157 ((RSAPrivateCrtKey) key).getModulus(),
158 ((RSAPrivateCrtKey) key).getPublicExponent(),
159 ((RSAPrivateCrtKey) key).getPrivateExponent(),
160 ((RSAPrivateCrtKey) key).getPrimeP(),
161 ((RSAPrivateCrtKey) key).getPrimeQ(),
162 ((RSAPrivateCrtKey) key).getPrimeExponentP(),
163 ((RSAPrivateCrtKey) key).getPrimeExponentQ(),
164 ((RSAPrivateCrtKey) key).getCrtCoefficient()));
166 if (key instanceof RSAPrivateKey)
168 return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
169 ((RSAPrivateKey) key).getModulus(), null,
170 ((RSAPrivateKey) key).getPrivateExponent(), null,
171 null, null, null, null));
173 if (key instanceof RSAPublicKey)
175 return new GnuRSAPublicKey(new RSAPublicKeySpec(
176 ((RSAPrivateCrtKey) key).getModulus(),
177 ((RSAPrivateCrtKey) key).getPublicExponent()));
179 throw new InvalidKeyException();