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: import com.sshtools.j2ssh.util.Base64;
030:
031: /**
032: *
033: *
034: * @author $author$
035: * @version $Revision: 1.19 $
036: */
037: public class OpenSSHPublicKeyFormat implements SshPublicKeyFormat {
038: String comment = null;
039:
040: /**
041: * Creates a new OpenSSHPublicKeyFormat object.
042: *
043: * @param comment
044: */
045: public OpenSSHPublicKeyFormat(String comment) {
046: setComment(comment);
047: }
048:
049: /**
050: * Creates a new OpenSSHPublicKeyFormat object.
051: */
052: public OpenSSHPublicKeyFormat() {
053: }
054:
055: /**
056: *
057: *
058: * @param comment
059: */
060: public void setComment(String comment) {
061: this .comment = comment;
062: }
063:
064: /**
065: *
066: *
067: * @return
068: */
069: public String getComment() {
070: return comment;
071: }
072:
073: /**
074: *
075: *
076: * @return
077: */
078: public String getFormatType() {
079: return "OpenSSH-PublicKey";
080: }
081:
082: /**
083: *
084: *
085: * @param formattedKey
086: *
087: * @return
088: *
089: * @throws InvalidSshKeyException
090: */
091: public byte[] getKeyBlob(byte[] formattedKey)
092: throws InvalidSshKeyException {
093: String temp = new String(formattedKey);
094:
095: // Get the algorithm name end index
096: int i = temp.indexOf(" ");
097:
098: if (i > 0) {
099: String algorithm = temp.substring(0, i);
100:
101: if (supportsAlgorithm(algorithm)) {
102: // Get the keyblob end index
103: int i2 = temp.indexOf(" ", i + 1);
104:
105: if (i2 > i) {
106: String encoded = temp.substring(i + 1, i2);
107:
108: if (temp.length() > i2) {
109: comment = temp.substring(i2 + 1).trim();
110: }
111:
112: return Base64.decode(encoded);
113: }
114: }
115: }
116:
117: throw new InvalidSshKeyException(
118: "Failed to read OpenSSH key format");
119: }
120:
121: /**
122: *
123: *
124: * @param keyblob
125: *
126: * @return
127: */
128: public byte[] formatKey(byte[] keyblob) {
129: String algorithm = ByteArrayReader.readString(keyblob, 0);
130: String formatted = algorithm + " "
131: + Base64.encodeBytes(keyblob, true);
132:
133: if (comment != null) {
134: formatted += (" " + comment);
135: }
136:
137: return formatted.getBytes();
138: }
139:
140: /**
141: *
142: *
143: * @param formattedKey
144: *
145: * @return
146: */
147: public boolean isFormatted(byte[] formattedKey) {
148: String test = new String(formattedKey).trim();
149:
150: if (test.startsWith("ssh-dss") || test.startsWith("ssh-rsa")) {
151: return true;
152: } else {
153: return false;
154: }
155: }
156:
157: /**
158: *
159: *
160: * @param algorithm
161: *
162: * @return
163: */
164: public boolean supportsAlgorithm(String algorithm) {
165: if (algorithm.equals("ssh-dss") || algorithm.equals("ssh-rsa")) {
166: return true;
167: } else {
168: return false;
169: }
170: }
171: }
|