1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
63: public class KeyPairCodecFactory
64: {
65: private static Set names;
66:
67:
68: private KeyPairCodecFactory()
69: {
70: super();
71: }
72:
73:
87: public static IKeyPairCodec getInstance(String name)
88: {
89: if (name == null)
90: return null;
91:
92: name = name.trim();
93: if (name.length() == 0)
94: return null;
95:
96: if (name.startsWith("/"))
97: return null;
98:
99: if (name.endsWith("/"))
100: return getInstance(name.substring(0, name.length() - 1),
101: Registry.RAW_ENCODING_ID);
102:
103: int i = name.indexOf("/");
104: if (i == -1)
105: return getInstance(name, Registry.RAW_ENCODING_ID);
106:
107: String kpgName = name.substring(0, i);
108: String formatName = name.substring(i + 1);
109: return getInstance(kpgName, formatName);
110: }
111:
112:
123: public static IKeyPairCodec getInstance(String name, String format)
124: {
125: int formatID = FormatUtil.getFormatID(format);
126: if (formatID == 0)
127: return null;
128:
129: return getInstance(name, formatID);
130: }
131:
132:
143: public static IKeyPairCodec getInstance(String name, int formatID)
144: {
145: if (name == null)
146: return null;
147:
148: name = name.trim();
149: switch (formatID)
150: {
151: case Registry.RAW_ENCODING_ID:
152: return getRawCodec(name);
153: case Registry.X509_ENCODING_ID:
154: return getX509Codec(name);
155: case Registry.PKCS8_ENCODING_ID:
156: return getPKCS8Codec(name);
157: }
158:
159: return null;
160: }
161:
162:
169: public static IKeyPairCodec getInstance(Key key)
170: {
171: if (key == null)
172: return null;
173:
174: String format = key.getFormat();
175: int formatID = FormatUtil.getFormatID(format);
176: if (formatID == 0)
177: return null;
178:
179: switch (formatID)
180: {
181: case Registry.RAW_ENCODING_ID:
182: return getRawCodec(key);
183: case Registry.X509_ENCODING_ID:
184: return getX509Codec(key);
185: case Registry.PKCS8_ENCODING_ID:
186: return getPKCS8Codec(key);
187: }
188:
189: return null;
190: }
191:
192:
197: public static synchronized final Set getNames()
198: {
199: if (names == null)
200: {
201: HashSet hs = new HashSet();
202: hs.add(Registry.DSS_KPG + "/" + Registry.RAW_ENCODING_SHORT_NAME);
203: hs.add(Registry.DSS_KPG + "/" + Registry.X509_ENCODING_SORT_NAME);
204: hs.add(Registry.DSS_KPG + "/" + Registry.PKCS8_ENCODING_SHORT_NAME);
205: hs.add(Registry.RSA_KPG + "/" + Registry.RAW_ENCODING_SHORT_NAME);
206: hs.add(Registry.RSA_KPG + "/" + Registry.X509_ENCODING_SORT_NAME);
207: hs.add(Registry.RSA_KPG + "/" + Registry.PKCS8_ENCODING_SHORT_NAME);
208: hs.add(Registry.DH_KPG + "/" + Registry.RAW_ENCODING_SHORT_NAME);
209: hs.add(Registry.SRP_KPG + "/" + Registry.RAW_ENCODING_SHORT_NAME);
210: names = Collections.unmodifiableSet(hs);
211: }
212: return names;
213: }
214:
215: private static IKeyPairCodec makeInstance (String clazz)
216: {
217: try
218: {
219: Class c = Class.forName (clazz);
220: Constructor ctor = c.getConstructor (new Class[0]);
221: return (IKeyPairCodec) ctor.newInstance (new Object[0]);
222: }
223: catch (Exception x)
224: {
225: IllegalArgumentException iae =
226: new IllegalArgumentException ("strong crypto key codec not available: "
227: + clazz);
228: iae.initCause (x);
229: throw iae;
230: }
231: }
232:
233: private static boolean matches (Object o, String clazz)
234: {
235: try
236: {
237: Class c = Class.forName (clazz);
238: return c.isAssignableFrom (o.getClass ());
239: }
240: catch (Exception x)
241: {
242:
243: return false;
244: }
245: }
246:
247:
252: private static IKeyPairCodec getRawCodec(String name)
253: {
254: IKeyPairCodec result = null;
255: if (name.equalsIgnoreCase(Registry.DSA_KPG)
256: || name.equals(Registry.DSS_KPG))
257: result = new DSSKeyPairRawCodec();
258: else if (name.equalsIgnoreCase(Registry.RSA_KPG))
259: result = new RSAKeyPairRawCodec();
260: else if (name.equalsIgnoreCase(Registry.DH_KPG))
261: result = makeInstance("gnu.javax.crypto.key.dh.DHKeyPairRawCodec");
262: else if (name.equalsIgnoreCase(Registry.SRP_KPG))
263: result = makeInstance("gnu.javax.crypto.key.srp6.SRPKeyPairRawCodec");
264:
265: return result;
266: }
267:
268:
273: private static IKeyPairCodec getX509Codec(String name)
274: {
275: IKeyPairCodec result = null;
276: if (name.equalsIgnoreCase(Registry.DSA_KPG)
277: || name.equals(Registry.DSS_KPG))
278: result = new DSSKeyPairX509Codec();
279: else if (name.equalsIgnoreCase(Registry.RSA_KPG))
280: result = new RSAKeyPairX509Codec();
281: else if (name.equalsIgnoreCase(Registry.DH_KPG))
282: result = makeInstance("gnu.javax.crypto.key.dh.DHKeyPairX509Codec");
283:
284: return result;
285: }
286:
287:
292: private static IKeyPairCodec getPKCS8Codec(String name)
293: {
294: IKeyPairCodec result = null;
295: if (name.equalsIgnoreCase(Registry.DSA_KPG)
296: || name.equals(Registry.DSS_KPG))
297: result = new DSSKeyPairPKCS8Codec();
298: else if (name.equalsIgnoreCase(Registry.RSA_KPG))
299: result = new RSAKeyPairPKCS8Codec();
300: else if (name.equalsIgnoreCase(Registry.DH_KPG))
301: result = makeInstance("gnu.javax.crypto.key.dh.DHKeyPairPKCS8Codec");
302:
303: return result;
304: }
305:
306:
311: private static IKeyPairCodec getRawCodec(Key key)
312: {
313: IKeyPairCodec result = null;
314: if ((key instanceof DSSPublicKey) || (key instanceof DSSPrivateKey))
315: result = new DSSKeyPairRawCodec();
316: else if ((key instanceof GnuRSAPublicKey)
317: || (key instanceof GnuRSAPrivateKey))
318: result = new RSAKeyPairRawCodec();
319: else if (matches(key, "gnu.javax.crypto.key.dh.GnuDHPublicKey")
320: || matches(key, "gnu.javax.crypto.key.dh.GnuDHPrivateKey"))
321: result = makeInstance("gnu.javax.crypto.key.dh.DHKeyPairRawCodec");
322: else if (matches(key, "gnu.javax.crypto.key.srp6.SRPPublicKey")
323: || matches(key, "gnu.javax.crypto.key.srp6.SRPPrivateKey"))
324: result = makeInstance("gnu.javax.crypto.key.srp6.SRPKeyPairRawCodec");
325:
326: return result;
327: }
328:
329:
334: private static IKeyPairCodec getX509Codec(Key key)
335: {
336: IKeyPairCodec result = null;
337: if (key instanceof DSSPublicKey)
338: result = new DSSKeyPairX509Codec();
339: else if (key instanceof GnuRSAPublicKey)
340: result = new RSAKeyPairX509Codec();
341:
342: return result;
343: }
344:
345:
350: private static IKeyPairCodec getPKCS8Codec(Key key)
351: {
352: IKeyPairCodec result = null;
353: if (key instanceof DSSPrivateKey)
354: result = new DSSKeyPairPKCS8Codec();
355: else if (key instanceof GnuRSAPrivateKey)
356: result = new RSAKeyPairPKCS8Codec();
357:
358: return result;
359: }
360: }