1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: public class RSAKeyFactory
65: extends KeyFactorySpi
66: {
67:
68:
69: protected PublicKey engineGeneratePublic(KeySpec keySpec)
70: throws InvalidKeySpecException
71: {
72: if (keySpec instanceof RSAPublicKeySpec)
73: {
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);
78: }
79: if (keySpec instanceof X509EncodedKeySpec)
80: {
81: X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
82: byte[] encoded = spec.getEncoded();
83: PublicKey result;
84: try
85: {
86: return new RSAKeyPairX509Codec().decodePublicKey(encoded);
87: }
88: catch (RuntimeException x)
89: {
90: throw new InvalidKeySpecException(x.getMessage(), x);
91: }
92: }
93: throw new InvalidKeySpecException("Unsupported (public) key specification");
94: }
95:
96: protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
97: throws InvalidKeySpecException
98: {
99: if (keySpec instanceof RSAPrivateCrtKeySpec)
100: {
101: RSAPrivateCrtKeySpec spec = (RSAPrivateCrtKeySpec) keySpec;
102: BigInteger n = spec.getModulus();
103: BigInteger e = spec.getPublicExponent();
104: BigInteger d = spec.getPrivateExponent();
105: BigInteger p = spec.getPrimeP();
106: BigInteger q = spec.getPrimeQ();
107: BigInteger dP = spec.getPrimeExponentP();
108: BigInteger dQ = spec.getPrimeExponentQ();
109: BigInteger qInv = spec.getCrtCoefficient();
110: return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
111: n, e, d, p, q, dP, dQ, qInv);
112: }
113: if (keySpec instanceof PKCS8EncodedKeySpec)
114: {
115: PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
116: byte[] encoded = spec.getEncoded();
117: PrivateKey result;
118: try
119: {
120: return new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
121: }
122: catch (RuntimeException x)
123: {
124: throw new InvalidKeySpecException(x.getMessage(), x);
125: }
126: }
127: throw new InvalidKeySpecException("Unsupported (private) key specification");
128: }
129:
130: protected KeySpec engineGetKeySpec(Key key, Class keySpec)
131: throws InvalidKeySpecException
132: {
133: if (key instanceof RSAPublicKey)
134: {
135: if (keySpec.isAssignableFrom(RSAPublicKeySpec.class))
136: {
137: RSAPublicKey rsaKey = (RSAPublicKey) key;
138: BigInteger n = rsaKey.getModulus();
139: BigInteger e = rsaKey.getPublicExponent();
140: return new RSAPublicKeySpec(n, e);
141: }
142: if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
143: {
144: if (key instanceof GnuRSAPublicKey)
145: {
146: GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
147: byte[] encoded = rsaKey.getEncoded(Registry.X509_ENCODING_ID);
148: return new X509EncodedKeySpec(encoded);
149: }
150:
151: if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
152: {
153: byte[] encoded = key.getEncoded();
154: return new X509EncodedKeySpec(encoded);
155: }
156: throw new InvalidKeySpecException(
157: "Wrong key type or unsupported (public) key specification");
158: }
159: throw new InvalidKeySpecException("Unsupported (public) key specification");
160: }
161: if ((key instanceof RSAPrivateCrtKey)
162: && keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class))
163: {
164: RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
165: BigInteger n = rsaKey.getModulus();
166: BigInteger e = rsaKey.getPublicExponent();
167: BigInteger d = rsaKey.getPrivateExponent();
168: BigInteger p = rsaKey.getPrimeP();
169: BigInteger q = rsaKey.getPrimeQ();
170: BigInteger dP = rsaKey.getPrimeExponentP();
171: BigInteger dQ = rsaKey.getPrimeExponentQ();
172: BigInteger qInv = rsaKey.getCrtCoefficient();
173: return new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv);
174: }
175: if ((key instanceof RSAPrivateKey)
176: && keySpec.isAssignableFrom(RSAPrivateKeySpec.class))
177: {
178: RSAPrivateKey rsaKey = (RSAPrivateKey) key;
179: BigInteger n = rsaKey.getModulus();
180: BigInteger d = rsaKey.getPrivateExponent();
181: return new RSAPrivateKeySpec(n, d);
182: }
183: if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
184: {
185: if (key instanceof GnuRSAPrivateKey)
186: {
187: GnuRSAPrivateKey rsaKey = (GnuRSAPrivateKey) key;
188: byte[] encoded = rsaKey.getEncoded(Registry.PKCS8_ENCODING_ID);
189: return new PKCS8EncodedKeySpec(encoded);
190: }
191: if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
192: {
193: byte[] encoded = key.getEncoded();
194: return new PKCS8EncodedKeySpec(encoded);
195: }
196: throw new InvalidKeySpecException(
197: "Wrong key type or unsupported (private) key specification");
198: }
199: throw new InvalidKeySpecException(
200: "Wrong key type or unsupported key specification");
201: }
202:
203: protected Key engineTranslateKey(Key key) throws InvalidKeyException
204: {
205: if ((key instanceof GnuRSAPublicKey) || (key instanceof GnuRSAPrivateKey))
206: return key;
207:
208: if (key instanceof RSAPublicKey)
209: {
210: RSAPublicKey rsaKey = (RSAPublicKey) key;
211: BigInteger n = rsaKey.getModulus();
212: BigInteger e = rsaKey.getPublicExponent();
213: return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
214: }
215: if (key instanceof RSAPrivateCrtKey)
216: {
217: RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
218: BigInteger n = rsaKey.getModulus();
219: BigInteger e = rsaKey.getPublicExponent();
220: BigInteger d = rsaKey.getPrivateExponent();
221: BigInteger p = rsaKey.getPrimeP();
222: BigInteger q = rsaKey.getPrimeQ();
223: BigInteger dP = rsaKey.getPrimeExponentP();
224: BigInteger dQ = rsaKey.getPrimeExponentQ();
225: BigInteger qInv = rsaKey.getCrtCoefficient();
226: return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
227: n, e, d, p, q, dP, dQ, qInv);
228: }
229: throw new InvalidKeyException("Unsupported key type");
230: }
231: }