KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.maverick.util.ByteArrayReader;
31
32
33 /**
34  *
35  *
36  * @author $author$
37  */

38 public class SshPrivateKeyFile {
39     private static Log log = LogFactory.getLog(SshPrivateKeyFile.class);
40     private SshPrivateKeyFormat format;
41     private byte[] keyblob;
42
43     /**
44      * Creates a new SshPrivateKeyFile object.
45      *
46      * @param keyblob
47      * @param format
48      */

49     protected SshPrivateKeyFile(byte[] keyblob, SshPrivateKeyFormat format) {
50         this.keyblob = keyblob;
51         this.format = format;
52     }
53
54     /**
55      *
56      *
57      * @return
58      */

59     public byte[] getBytes() {
60         return keyblob;
61     }
62
63     /**
64      *
65      *
66      * @param passphrase
67      *
68      * @return byte[]
69      *
70      * @throws InvalidKeyException
71      */

72     public byte[] getKeyBlob(String JavaDoc passphrase) throws InvalidKeyException {
73         return format.decryptKeyblob(keyblob, passphrase);
74     }
75
76     /**
77      *
78      *
79      * @param oldPassphrase
80      * @param newPassphrase
81      *
82      * @throws InvalidKeyException
83      */

84     public void changePassphrase(String JavaDoc oldPassphrase, String JavaDoc newPassphrase)
85         throws InvalidKeyException {
86         byte[] raw = format.decryptKeyblob(keyblob, oldPassphrase);
87         keyblob = format.encryptKeyblob(raw, newPassphrase);
88     }
89
90     /**
91      *
92      *
93      * @param formattedKey
94      *
95      * @return
96      *
97      * @throws InvalidKeyException
98      */

99     public static SshPrivateKeyFile parse(byte[] formattedKey)
100         throws InvalidKeyException {
101         if (formattedKey == null) {
102             throw new InvalidKeyException("Key data is null");
103         }
104
105         if (log.isInfoEnabled())
106             log.info("Parsing private key file");
107
108         // Try the default private key format
109
SshPrivateKeyFormat format;
110         format = SshPrivateKeyFormatFactory.newInstance(SshPrivateKeyFormatFactory.getDefaultFormatType());
111
112         boolean valid = format.isFormatted(formattedKey);
113
114         if (valid) {
115             return new SshPrivateKeyFile(formattedKey, format);
116         } else {
117             throw new InvalidKeyException(
118                 "The key format is not a supported format");
119         }
120     }
121
122     /**
123      *
124      *
125      * @param keyfile
126      *
127      * @return SshPrivateKeyFile
128      *
129      * @throws InvalidKeyException
130      * @throws IOException
131      */

132     public static SshPrivateKeyFile parse(File JavaDoc keyfile)
133         throws InvalidKeyException, IOException JavaDoc {
134
135         return parse(new FileInputStream JavaDoc(keyfile));
136     }
137     
138     /**
139      * @param in
140      * @return SshPrivateKeyFile
141      * @throws InvalidKeyException
142      * @throws IOException
143      */

144     public static SshPrivateKeyFile parse(InputStream JavaDoc in)
145         throws InvalidKeyException, IOException JavaDoc {
146     
147         byte[] data = null;
148
149         try {
150             data = new byte[in.available()];
151             in.read(data);
152         } finally {
153             try {
154                 if (in != null) {
155                     in.close();
156                 }
157             } catch (IOException JavaDoc ex) {
158             }
159         }
160
161         return parse(data);
162     }
163
164     /**
165      *
166      *
167      * @return
168      */

169     public boolean isPassphraseProtected() {
170         return format.isPassphraseProtected(keyblob);
171     }
172
173     /*public void changePassphrase(String oldPassphrase, String newPassphrase)
174      throws InvalidKeyException {
175      keyblob = format.changePassphrase(keyblob, oldPassphrase, newPassphrase);
176       }*/

177     /**
178      * @param key
179      * @param passphrase
180      * @param format
181      * @return SshPrivateKeyFile
182      * @throws InvalidKeyException
183      */

184     public static SshPrivateKeyFile create(SshPrivateKey key,
185         String JavaDoc passphrase, SshPrivateKeyFormat format)
186         throws InvalidKeyException {
187         byte[] keyblob = format.encryptKeyblob(key.getEncoded(), passphrase);
188
189         return new SshPrivateKeyFile(keyblob, format);
190     }
191
192     /**
193      *
194      *
195      * @param newFormat
196      * @param passphrase
197      *
198      * @throws InvalidKeyException
199      */

200     public void setFormat(SshPrivateKeyFormat newFormat, String JavaDoc passphrase)
201         throws InvalidKeyException {
202         byte[] raw = this.format.decryptKeyblob(keyblob, passphrase);
203         format = newFormat;
204         keyblob = format.encryptKeyblob(raw, passphrase);
205     }
206
207     /**
208      *
209      *
210      * @return SshPrivateKeyFormat
211      */

212     public SshPrivateKeyFormat getFormat() {
213         return format;
214     }
215
216     /**
217      *
218      *
219      * @param passphrase
220      *
221      * @return SshPrivateKey
222      *
223      * @throws InvalidKeyException
224      */

225     public SshPrivateKey toPrivateKey(String JavaDoc passphrase)
226         throws InvalidKeyException {
227             byte[] raw = format.decryptKeyblob(keyblob, passphrase);
228             SshKeyPair pair = SshKeyPairFactory.newInstance(getAlgorithm(raw));
229
230             return pair.decodePrivateKey(raw);
231
232     }
233
234     /**
235      *
236      *
237      * @return String
238      */

239     public String JavaDoc toString() {
240         return new String JavaDoc(keyblob);
241     }
242
243     /**
244      * @param raw
245      * @return String
246      */

247     private String JavaDoc getAlgorithm(byte[] raw) {
248         try {
249             ByteArrayReader r = new ByteArrayReader(raw);
250             return r.readString();
251         } catch (IOException JavaDoc e) {
252             return null;
253         }
254     }
255 }
256
Popular Tags