001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.transport.publickey;
027:
028: import com.sshtools.j2ssh.io.ByteArrayReader;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.io.File;
034: import java.io.FileInputStream;
035: import java.io.IOException;
036:
037: import java.util.Iterator;
038:
039: /**
040: *
041: *
042: * @author $author$
043: * @version $Revision: 1.20 $
044: */
045: public class SshPublicKeyFile {
046: private static Log log = LogFactory.getLog(SshPublicKeyFile.class);
047: private SshPublicKeyFormat format;
048: private byte[] keyblob;
049: private String comment;
050:
051: /**
052: * Creates a new SshPublicKeyFile object.
053: *
054: * @param keyblob
055: * @param format
056: */
057: protected SshPublicKeyFile(byte[] keyblob, SshPublicKeyFormat format) {
058: this .keyblob = keyblob;
059: this .format = format;
060: }
061:
062: /**
063: *
064: *
065: * @return
066: */
067: public byte[] getBytes() {
068: return format.formatKey(keyblob);
069: }
070:
071: /**
072: *
073: *
074: * @return
075: */
076: public String getComment() {
077: return comment;
078: }
079:
080: /**
081: *
082: *
083: * @param comment
084: */
085: public void setComment(String comment) {
086: this .comment = comment;
087: }
088:
089: /**
090: *
091: *
092: * @return
093: */
094: public byte[] getKeyBlob() {
095: return keyblob;
096: }
097:
098: /**
099: *
100: *
101: * @param key
102: * @param format
103: *
104: * @return
105: */
106: public static SshPublicKeyFile create(SshPublicKey key,
107: SshPublicKeyFormat format) {
108: SshPublicKeyFile file = new SshPublicKeyFile(key.getEncoded(),
109: format);
110: file.setComment(format.getComment());
111:
112: return file;
113: }
114:
115: /**
116: *
117: *
118: * @param keyfile
119: *
120: * @return
121: *
122: * @throws InvalidSshKeyException
123: * @throws IOException
124: */
125: public static SshPublicKeyFile parse(File keyfile)
126: throws InvalidSshKeyException, IOException {
127: FileInputStream in = new FileInputStream(keyfile);
128: byte[] data = new byte[in.available()];
129: in.read(data);
130: in.close();
131:
132: return parse(data);
133: }
134:
135: /**
136: *
137: *
138: * @param formattedKey
139: *
140: * @return
141: *
142: * @throws InvalidSshKeyException
143: */
144: public static SshPublicKeyFile parse(byte[] formattedKey)
145: throws InvalidSshKeyException {
146: log.info("Parsing public key file");
147:
148: // Try the default private key format
149: SshPublicKeyFormat format;
150: format = SshPublicKeyFormatFactory
151: .newInstance(SshPublicKeyFormatFactory
152: .getDefaultFormatType());
153:
154: boolean valid = format.isFormatted(formattedKey);
155:
156: if (!valid) {
157: log
158: .info("Public key is not in the default format, attempting parse with other supported formats");
159:
160: Iterator it = SshPublicKeyFormatFactory
161: .getSupportedFormats().iterator();
162: String ft;
163:
164: while (it.hasNext() && !valid) {
165: ft = (String) it.next();
166: log.debug("Attempting " + ft);
167: format = SshPublicKeyFormatFactory.newInstance(ft);
168: valid = format.isFormatted(formattedKey);
169: }
170: }
171:
172: if (valid) {
173: SshPublicKeyFile file = new SshPublicKeyFile(format
174: .getKeyBlob(formattedKey), format);
175: file.setComment(format.getComment());
176:
177: return file;
178: } else {
179: throw new InvalidSshKeyException(
180: "The key format is not a supported format");
181: }
182: }
183:
184: /**
185: *
186: *
187: * @return
188: */
189: public String getAlgorithm() {
190: return ByteArrayReader.readString(keyblob, 0);
191: }
192:
193: /**
194: *
195: *
196: * @param newFormat
197: *
198: * @throws InvalidSshKeyException
199: */
200: public void setFormat(SshPublicKeyFormat newFormat)
201: throws InvalidSshKeyException {
202: if (newFormat.supportsAlgorithm(getAlgorithm())) {
203: newFormat.setComment(format.getComment());
204: this .format = newFormat;
205: } else {
206: throw new InvalidSshKeyException(
207: "The format does not support the public key algorithm");
208: }
209: }
210:
211: /**
212: *
213: *
214: * @return
215: */
216: public SshPublicKeyFormat getFormat() {
217: return format;
218: }
219:
220: /**
221: *
222: *
223: * @return
224: *
225: * @throws IOException
226: */
227: public SshPublicKey toPublicKey() throws IOException {
228: ByteArrayReader bar = new ByteArrayReader(keyblob);
229: String type = bar.readString();
230: SshKeyPair pair = SshKeyPairFactory.newInstance(type);
231:
232: return pair.decodePublicKey(keyblob);
233: }
234:
235: /**
236: *
237: *
238: * @return
239: */
240: public String toString() {
241: return new String(format.formatKey(keyblob));
242: }
243: }
|