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.ByteArrayWriter;
029: import com.sshtools.j2ssh.util.Base64;
030:
031: import java.io.BufferedReader;
032: import java.io.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.io.InputStreamReader;
036:
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.Map;
040: import java.util.Set;
041:
042: /**
043: *
044: *
045: * @author $author$
046: * @version $Revision: 1.17 $
047: */
048: public abstract class Base64EncodedFileFormat implements
049: SshKeyFormatConversion {
050: /** */
051: protected String begin;
052:
053: /** */
054: protected String end;
055: private Map headers = new HashMap();
056: private int MAX_LINE_LENGTH = 70;
057:
058: /**
059: * Creates a new Base64EncodedFileFormat object.
060: *
061: * @param begin
062: * @param end
063: */
064: protected Base64EncodedFileFormat(String begin, String end) {
065: this .begin = begin;
066: this .end = end;
067: }
068:
069: /**
070: *
071: *
072: * @return
073: */
074: public String getFormatType() {
075: return "Base64Encoded";
076: }
077:
078: /**
079: *
080: *
081: * @param formattedKey
082: *
083: * @return
084: */
085: public boolean isFormatted(byte[] formattedKey) {
086: String test = new String(formattedKey);
087:
088: if ((test.indexOf(begin) >= 0) && (test.indexOf(end) > 0)) {
089: return true;
090: } else {
091: return false;
092: }
093: }
094:
095: /**
096: *
097: *
098: * @param headerTag
099: * @param headerValue
100: */
101: public void setHeaderValue(String headerTag, String headerValue) {
102: headers.put(headerTag, headerValue);
103: }
104:
105: /**
106: *
107: *
108: * @param headerTag
109: *
110: * @return
111: */
112: public String getHeaderValue(String headerTag) {
113: return (String) headers.get(headerTag);
114: }
115:
116: /**
117: *
118: *
119: * @param formattedKey
120: *
121: * @return
122: *
123: * @throws InvalidSshKeyException
124: */
125: public byte[] getKeyBlob(byte[] formattedKey)
126: throws InvalidSshKeyException {
127: BufferedReader reader = new BufferedReader(
128: new InputStreamReader(new ByteArrayInputStream(
129: formattedKey)));
130: String line;
131: String headerTag;
132: String headerValue;
133: String blob = "";
134: int index;
135:
136: try {
137: // Read in the lines looking for the start
138: while (true) {
139: line = reader.readLine();
140:
141: if (line == null) {
142: throw new InvalidSshKeyException(
143: "Incorrect file format!");
144: }
145:
146: if (line.endsWith(begin)) {
147: break;
148: }
149: }
150:
151: // Read the headers
152: while (true) {
153: line = reader.readLine();
154:
155: if (line == null) {
156: throw new InvalidSshKeyException(
157: "Incorrect file format!");
158: }
159:
160: index = line.indexOf(": ");
161:
162: if (index > 0) {
163: while (line.endsWith("\\")) {
164: line = line.substring(0, line.length() - 1);
165:
166: String tmp = reader.readLine();
167:
168: if (tmp == null) {
169: throw new InvalidSshKeyException(
170: "Incorrect file format!");
171: }
172:
173: line += tmp;
174: }
175:
176: // Record the header
177: headerTag = line.substring(0, index);
178: headerValue = line.substring(index + 2);
179: headers.put(headerTag, headerValue);
180: } else {
181: break;
182: }
183: }
184:
185: // This is now the public key blob Base64 encoded
186: ByteArrayWriter baw = new ByteArrayWriter();
187:
188: while (true) {
189: blob += line;
190: line = reader.readLine();
191:
192: if (line == null) {
193: throw new InvalidSshKeyException(
194: "Invalid file format!");
195: }
196:
197: if (line.endsWith(end)) {
198: break;
199: }
200: }
201:
202: // Convert the blob to some useful data
203: return Base64.decode(blob);
204: } catch (IOException ioe) {
205: throw new InvalidSshKeyException();
206: }
207: }
208:
209: /**
210: *
211: *
212: * @param keyblob
213: *
214: * @return
215: */
216: public byte[] formatKey(byte[] keyblob) {
217: try {
218: ByteArrayOutputStream out = new ByteArrayOutputStream();
219: String headerTag;
220: String headerValue;
221: String line;
222: out.write(begin.getBytes());
223: out.write('\n');
224:
225: int pos;
226: Set tags = headers.keySet();
227: Iterator it = tags.iterator();
228:
229: while (it.hasNext()) {
230: headerTag = (String) it.next();
231: headerValue = (String) headers.get(headerTag);
232:
233: String header = headerTag + ": " + headerValue;
234: pos = 0;
235:
236: while (pos < header.length()) {
237: line = header
238: .substring(
239: pos,
240: (((pos + MAX_LINE_LENGTH) < header
241: .length()) ? (pos + MAX_LINE_LENGTH)
242: : header.length()))
243: + (((pos + MAX_LINE_LENGTH) < header
244: .length()) ? "\\" : "");
245: out.write(line.getBytes());
246: out.write('\n');
247: pos += MAX_LINE_LENGTH;
248: }
249: }
250:
251: String encoded = Base64.encodeBytes(keyblob, false);
252: out.write(encoded.getBytes());
253: out.write('\n');
254: out.write(end.getBytes());
255: out.write('\n');
256:
257: return out.toByteArray();
258: } catch (IOException ioe) {
259: return null;
260: }
261: }
262: }
|