KickJava   Java API By Example, From Geeks To Geeks.

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


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 SshPublicKeyFile {
39     private static Log log = LogFactory.getLog(SshPublicKeyFile.class);
40     private SshPublicKeyFormat format;
41     private byte[] keyblob;
42     private String JavaDoc comment;
43
44     /**
45      * Creates a new SshPublicKeyFile object.
46      *
47      * @param keyblob
48      * @param format
49      */

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

60     public byte[] getBytes() {
61         return format.formatKey(keyblob);
62     }
63
64     /**
65      *
66      *
67      * @return
68      */

69     public String JavaDoc getComment() {
70         return comment;
71     }
72
73     /**
74      *
75      *
76      * @param comment
77      */

78     public void setComment(String JavaDoc comment) {
79         this.comment = comment;
80     }
81
82     /**
83      *
84      *
85      * @return
86      */

87     public byte[] getKeyBlob() {
88         return keyblob;
89     }
90
91     /**
92      *
93      *
94      * @param key
95      * @param format
96      *
97      * @return
98      */

99     public static SshPublicKeyFile create(SshPublicKey key,
100         SshPublicKeyFormat format) {
101         SshPublicKeyFile file = new SshPublicKeyFile(key.getEncoded(), format);
102         file.setComment(format.getComment());
103
104         return file;
105     }
106
107     /**
108      *
109      *
110      * @param keyfile
111      *
112      * @return SshPublicKeyFile
113      *
114      * @throws InvalidKeyException
115      * @throws IOException
116      */

117     public static SshPublicKeyFile parse(File JavaDoc keyfile)
118         throws InvalidKeyException, IOException JavaDoc {
119
120         return parse(new FileInputStream JavaDoc(keyfile));
121     }
122     
123     /**
124      * @param in
125      * @return SshPublicKeyFile
126      * @throws InvalidKeyException
127      * @throws IOException
128      */

129     public static SshPublicKeyFile parse(InputStream JavaDoc in)
130         throws InvalidKeyException, IOException JavaDoc {
131
132         byte[] data = new byte[in.available()];
133         in.read(data);
134         in.close();
135
136         return parse(data);
137     }
138
139     /**
140      *
141      *
142      * @param formattedKey
143      *
144      * @return SshPublicKeyFile
145      *
146      * @throws InvalidKeyException
147      */

148     public static SshPublicKeyFile parse(byte[] formattedKey)
149         throws InvalidKeyException {
150         if (log.isInfoEnabled())
151             log.info("Parsing public key file");
152
153         // Try the default private key format
154
SshPublicKeyFormat format;
155         format = SshPublicKeyFormatFactory.newInstance(SshPublicKeyFormatFactory.getDefaultFormatType());
156
157         boolean valid = format.isFormatted(formattedKey);
158
159
160         if (valid) {
161             SshPublicKeyFile file = new SshPublicKeyFile(format.getKeyBlob(
162                         formattedKey), format);
163             file.setComment(format.getComment());
164
165             return file;
166         } else {
167             throw new InvalidKeyException(
168                 "The key format is not a supported format");
169         }
170     }
171
172     /**
173      *
174      *
175      * @return String
176      */

177     public String JavaDoc getAlgorithm() {
178         try {
179             ByteArrayReader r = new ByteArrayReader(keyblob);
180             return r.readString();
181         } catch (IOException JavaDoc e) {
182             return null;
183         }
184     }
185
186     /**
187      *
188      *
189      * @param newFormat
190      *
191      * @throws InvalidKeyException
192      */

193     public void setFormat(SshPublicKeyFormat newFormat)
194         throws InvalidKeyException {
195         if (newFormat.supportsAlgorithm(getAlgorithm())) {
196             newFormat.setComment(format.getComment());
197             this.format = newFormat;
198         } else {
199             throw new InvalidKeyException(
200                 "The format does not support the public key algorithm");
201         }
202     }
203
204     /**
205      *
206      *
207      * @return SshPublicKeyFormat
208      */

209     public SshPublicKeyFormat getFormat() {
210         return format;
211     }
212
213     /**
214      *
215      *
216      * @return SshPublicKey
217      *
218      * @throws IOException
219      */

220     public SshPublicKey toPublicKey() throws IOException JavaDoc, InvalidKeyException {
221         ByteArrayReader bar = new ByteArrayReader(keyblob);
222         String JavaDoc type = bar.readString();
223         SshKeyPair pair = SshKeyPairFactory.newInstance(type);
224
225         return pair.decodePublicKey(keyblob);
226     }
227
228     /**
229      *
230      *
231      * @return
232      */

233     public String JavaDoc toString() {
234         return new String JavaDoc(format.formatKey(keyblob));
235     }
236 }
237
Popular Tags