KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > SshKeyPairFactory


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki;
21
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.maverick.util.ByteArrayReader;
36 import com.sslexplorer.security.pki.dsa.SshDssKeyPair;
37 import com.sslexplorer.security.pki.rsa.SshRsaKeyPair;
38
39
40 /**
41  *
42  *
43  * @author $author$
44  */

45 public class SshKeyPairFactory {
46     private static Map JavaDoc pks;
47     private static String JavaDoc defaultAlgorithm;
48     private static Log log = LogFactory.getLog(SshKeyPairFactory.class);
49
50     static {
51         pks = new HashMap JavaDoc();
52         if (log.isInfoEnabled())
53             log.info("Loading public key algorithms");
54
55         pks.put("ssh-rsa", SshRsaKeyPair.class);
56         pks.put("ssh-dss", SshDssKeyPair.class);
57         
58         if ((defaultAlgorithm == null) || !pks.containsKey(defaultAlgorithm)) {
59             Iterator JavaDoc it = pks.keySet().iterator();
60             defaultAlgorithm = (String JavaDoc) it.next();
61         }
62     }
63
64     /**
65      * Creates a new SshKeyPairFactory object.
66      */

67     protected SshKeyPairFactory() {
68     }
69
70     /**
71      *
72      */

73     public static void initialize() {
74     }
75
76     /**
77      *
78      *
79      * @return
80      */

81     public static String JavaDoc getDefaultPublicKey() {
82         return defaultAlgorithm;
83     }
84
85     /**
86      *
87      *
88      * @return
89      */

90     public static List JavaDoc getSupportedKeys() {
91         // Get the list of pks
92
return new ArrayList JavaDoc(pks.keySet());
93     }
94
95     /**
96      *
97      *
98      * @param methodName
99      *
100      * @return SshKeyPair
101      *
102      */

103     public static SshKeyPair newInstance(String JavaDoc methodName) {
104         try {
105             return (SshKeyPair) ((Class JavaDoc) pks.get(methodName)).newInstance();
106         } catch (Exception JavaDoc e) {
107             return null;
108         }
109     }
110
111     /**
112      *
113      *
114      * @param algorithm
115      *
116      * @return
117      */

118     public static boolean supportsKey(String JavaDoc algorithm) {
119         return pks.containsKey(algorithm);
120     }
121
122     /**
123      *
124      *
125      * @param encoded
126      *
127      * @return SshPrivateKey
128      *
129      * @throws InvalidKeyException
130      */

131     public static SshPrivateKey decodePrivateKey(byte[] encoded)
132         throws InvalidKeyException {
133         try {
134             ByteArrayReader bar = new ByteArrayReader(encoded);
135             String JavaDoc algorithm = bar.readString();
136
137             if (supportsKey(algorithm)) {
138                 SshKeyPair pair = newInstance(algorithm);
139
140                 return pair.decodePrivateKey(encoded);
141             } else {
142                 return null;
143             }
144         } catch (IOException JavaDoc ioe) {
145             return null;
146         }
147     }
148     
149     public static SshPrivateKey decodePrivateKey(InputStream JavaDoc in)
150     throws InvalidKeyException, IOException JavaDoc {
151     
152     
153     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
154     byte[] buf = new byte[4096];
155     int read;
156     
157     while((read = in.read(buf)) > -1) {
158         out.write(buf, 0, read);
159     }
160     
161     return decodePrivateKey(out.toByteArray());
162     
163  }
164     /**
165      *
166      *
167      * @param encoded
168      *
169      * @return SshPublicKey
170      *
171      * @throws InvalidKeyException
172      */

173     public static SshPublicKey decodePublicKey(byte[] encoded)
174         throws InvalidKeyException {
175         try {
176             ByteArrayReader bar = new ByteArrayReader(encoded);
177             String JavaDoc algorithm = bar.readString();
178
179             if (supportsKey(algorithm)) {
180                 SshKeyPair pair = newInstance(algorithm);
181
182                 return pair.decodePublicKey(encoded);
183             } else {
184                 return null;
185             }
186         } catch (IOException JavaDoc ioe) {
187             return null;
188         }
189     }
190     
191     public static SshPublicKey decodePublicKey(InputStream JavaDoc in)
192        throws InvalidKeyException, IOException JavaDoc {
193         
194         
195         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
196         byte[] buf = new byte[4096];
197         int read;
198         
199         while((read = in.read(buf)) > -1) {
200             out.write(buf, 0, read);
201         }
202         
203         return decodePublicKey(out.toByteArray());
204         
205     }
206 }
207
208
Popular Tags