1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59: public class RSAKeyFactory extends KeyFactorySpi
60: {
61:
62:
63:
64:
65:
66:
67:
68: protected PrivateKey engineGeneratePrivate(KeySpec spec)
69: throws InvalidKeySpecException
70: {
71: if (spec instanceof RSAPrivateCrtKeySpec)
72: {
73: return new GnuRSAPrivateKey((RSAPrivateCrtKeySpec) spec);
74: }
75: if (spec instanceof RSAPrivateKeySpec)
76: {
77: return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
78: ((RSAPrivateKeySpec) spec).getModulus(), null,
79: ((RSAPrivateKeySpec) spec).getPrivateExponent(), null,
80: null, null, null, null));
81: }
82: if (spec instanceof PKCS8EncodedKeySpec)
83: {
84: EncodedKeyFactory ekf = new EncodedKeyFactory();
85: PrivateKey pk = ekf.engineGeneratePrivate(spec);
86: if (pk instanceof RSAPrivateKey)
87: return pk;
88: }
89: throw new InvalidKeySpecException();
90: }
91:
92: protected PublicKey engineGeneratePublic(KeySpec spec)
93: throws InvalidKeySpecException
94: {
95: if (spec instanceof RSAPublicKeySpec)
96: {
97: return new GnuRSAPublicKey((RSAPublicKeySpec) spec);
98: }
99: if (spec instanceof X509EncodedKeySpec)
100: {
101: EncodedKeyFactory ekf = new EncodedKeyFactory();
102: PublicKey pk = ekf.engineGeneratePublic(spec);
103: if (pk instanceof RSAPublicKey)
104: return pk;
105: }
106: throw new InvalidKeySpecException();
107: }
108:
109: protected KeySpec engineGetKeySpec(Key key, Class keySpec)
110: throws InvalidKeySpecException
111: {
112: if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class)
113: && (key instanceof RSAPrivateCrtKey))
114: {
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());
124: }
125: if (keySpec.isAssignableFrom(RSAPrivateKeySpec.class)
126: && (key instanceof RSAPrivateKey))
127: {
128: return new RSAPrivateKeySpec(
129: ((RSAPrivateCrtKey) key).getModulus(),
130: ((RSAPrivateCrtKey) key).getPrivateExponent());
131: }
132: if (keySpec.isAssignableFrom(RSAPublicKeySpec.class)
133: && (key instanceof RSAPublicKey))
134: {
135: return new RSAPublicKeySpec(
136: ((RSAPrivateCrtKey) key).getModulus(),
137: ((RSAPrivateCrtKey) key).getPublicExponent());
138: }
139: if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)
140: && key.getFormat().equalsIgnoreCase("PKCS#8"))
141: {
142: return new PKCS8EncodedKeySpec(key.getEncoded());
143: }
144: if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)
145: && key.getFormat().equalsIgnoreCase("X.509"))
146: {
147: return new X509EncodedKeySpec(key.getEncoded());
148: }
149: throw new InvalidKeySpecException();
150: }
151:
152: protected Key engineTranslateKey(Key key) throws InvalidKeyException
153: {
154: if (key instanceof RSAPrivateCrtKey)
155: {
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()));
165: }
166: if (key instanceof RSAPrivateKey)
167: {
168: return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
169: ((RSAPrivateKey) key).getModulus(), null,
170: ((RSAPrivateKey) key).getPrivateExponent(), null,
171: null, null, null, null));
172: }
173: if (key instanceof RSAPublicKey)
174: {
175: return new GnuRSAPublicKey(new RSAPublicKeySpec(
176: ((RSAPrivateCrtKey) key).getModulus(),
177: ((RSAPrivateCrtKey) key).getPublicExponent()));
178: }
179: throw new InvalidKeyException();
180: }
181: }