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:
62:
67: public class DSSKeyFactory
68: extends KeyFactorySpi
69: {
70:
71:
72: protected PublicKey engineGeneratePublic(KeySpec keySpec)
73: throws InvalidKeySpecException
74: {
75: if (keySpec instanceof DSAPublicKeySpec)
76: {
77: DSAPublicKeySpec spec = (DSAPublicKeySpec) keySpec;
78: BigInteger p = spec.getP();
79: BigInteger q = spec.getQ();
80: BigInteger g = spec.getG();
81: BigInteger y = spec.getY();
82: return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
83: }
84: if (keySpec instanceof X509EncodedKeySpec)
85: {
86: X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
87: byte[] encoded = spec.getEncoded();
88: PublicKey result;
89: try
90: {
91: result = new DSSKeyPairX509Codec().decodePublicKey(encoded);
92: return result;
93: }
94: catch (RuntimeException x)
95: {
96: throw new InvalidKeySpecException(x.getMessage(), x);
97: }
98: }
99: throw new InvalidKeySpecException("Unsupported (public) key specification");
100: }
101:
102: protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
103: throws InvalidKeySpecException
104: {
105: if (keySpec instanceof DSAPrivateKeySpec)
106: {
107: DSAPrivateKeySpec spec = (DSAPrivateKeySpec) keySpec;
108: BigInteger p = spec.getP();
109: BigInteger q = spec.getQ();
110: BigInteger g = spec.getG();
111: BigInteger x = spec.getX();
112: return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
113: }
114: if (keySpec instanceof PKCS8EncodedKeySpec)
115: {
116: PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
117: byte[] encoded = spec.getEncoded();
118: PrivateKey result;
119: try
120: {
121: result = new DSSKeyPairPKCS8Codec().decodePrivateKey(encoded);
122: return result;
123: }
124: catch (RuntimeException x)
125: {
126: throw new InvalidKeySpecException(x.getMessage(), x);
127: }
128: }
129: throw new InvalidKeySpecException("Unsupported (private) key specification");
130: }
131:
132: protected KeySpec engineGetKeySpec(Key key, Class keySpec)
133: throws InvalidKeySpecException
134: {
135: if (key instanceof DSAPublicKey)
136: {
137: if (keySpec.isAssignableFrom(DSAPublicKeySpec.class))
138: {
139: DSAPublicKey dsaKey = (DSAPublicKey) key;
140: BigInteger p = dsaKey.getParams().getP();
141: BigInteger q = dsaKey.getParams().getQ();
142: BigInteger g = dsaKey.getParams().getG();
143: BigInteger y = dsaKey.getY();
144: return new DSAPublicKeySpec(y, p, q, g);
145: }
146: if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
147: {
148: if (key instanceof DSSPublicKey)
149: {
150: DSSPublicKey dssKey = (DSSPublicKey) key;
151: byte[] encoded = dssKey.getEncoded(Registry.X509_ENCODING_ID);
152: return new X509EncodedKeySpec(encoded);
153: }
154: if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
155: {
156: byte[] encoded = key.getEncoded();
157: return new X509EncodedKeySpec(encoded);
158: }
159: throw new InvalidKeySpecException(
160: "Wrong key type or unsupported (public) key specification");
161: }
162: throw new InvalidKeySpecException("Unsupported (public) key specification");
163: }
164: if (key instanceof DSAPrivateKey)
165: {
166: if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class))
167: {
168: DSAPrivateKey dsaKey = (DSAPrivateKey) key;
169: BigInteger p = dsaKey.getParams().getP();
170: BigInteger q = dsaKey.getParams().getQ();
171: BigInteger g = dsaKey.getParams().getG();
172: BigInteger x = dsaKey.getX();
173: return new DSAPrivateKeySpec(x, p, q, g);
174: }
175: if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
176: {
177: if (key instanceof DSSPrivateKey)
178: {
179: DSSPrivateKey dssKey = (DSSPrivateKey) key;
180: byte[] encoded = dssKey.getEncoded(Registry.PKCS8_ENCODING_ID);
181: return new PKCS8EncodedKeySpec(encoded);
182: }
183: if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
184: {
185: byte[] encoded = key.getEncoded();
186: return new PKCS8EncodedKeySpec(encoded);
187: }
188: throw new InvalidKeySpecException(
189: "Wrong key type or unsupported (private) key specification");
190: }
191: throw new InvalidKeySpecException("Unsupported (private) key specification");
192: }
193: throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
194: }
195:
196: protected Key engineTranslateKey(Key key) throws InvalidKeyException
197: {
198: if ((key instanceof DSSPublicKey) || (key instanceof DSSPrivateKey))
199: return key;
200:
201: if (key instanceof DSAPublicKey)
202: {
203: DSAPublicKey dsaKey = (DSAPublicKey) key;
204: BigInteger p = dsaKey.getParams().getP();
205: BigInteger q = dsaKey.getParams().getQ();
206: BigInteger g = dsaKey.getParams().getG();
207: BigInteger y = dsaKey.getY();
208: return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
209: }
210: if (key instanceof DSAPrivateKey)
211: {
212: DSAPrivateKey dsaKey = (DSAPrivateKey) key;
213: BigInteger p = dsaKey.getParams().getP();
214: BigInteger q = dsaKey.getParams().getQ();
215: BigInteger g = dsaKey.getParams().getG();
216: BigInteger x = dsaKey.getX();
217: return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
218: }
219: throw new InvalidKeyException("Wrong key type");
220: }
221: }