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.configuration.ConfigurationException;
029: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
030: import com.sshtools.j2ssh.configuration.ExtensionAlgorithm;
031: import com.sshtools.j2ssh.configuration.SshAPIConfiguration;
032: import com.sshtools.j2ssh.io.ByteArrayReader;
033: import com.sshtools.j2ssh.transport.AlgorithmNotSupportedException;
034: import com.sshtools.j2ssh.transport.publickey.dsa.SshDssKeyPair;
035: import com.sshtools.j2ssh.transport.publickey.rsa.SshRsaKeyPair;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import java.io.IOException;
041:
042: import java.util.ArrayList;
043: import java.util.HashMap;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Map;
047:
048: /**
049: *
050: *
051: * @author $author$
052: * @version $Revision: 1.26 $
053: */
054: public class SshKeyPairFactory {
055: private static Map pks;
056: private static String defaultAlgorithm;
057: private static Log log = LogFactory.getLog(SshKeyPairFactory.class);
058:
059: static {
060: pks = new HashMap();
061: log.info("Loading public key algorithms");
062: pks.put("ssh-dss", SshDssKeyPair.class);
063: pks.put("ssh-rsa", SshRsaKeyPair.class);
064:
065: try {
066: // Load external pks from configuration file
067: if (ConfigurationLoader
068: .isConfigurationAvailable(SshAPIConfiguration.class)) {
069: SshAPIConfiguration config = (SshAPIConfiguration) ConfigurationLoader
070: .getConfiguration(SshAPIConfiguration.class);
071:
072: if (config != null) {
073: List list = config.getPublicKeyExtensions();
074:
075: if (list != null) {
076: Iterator it = list.iterator();
077:
078: while (it.hasNext()) {
079: ExtensionAlgorithm algorithm = (ExtensionAlgorithm) it
080: .next();
081: String name = algorithm.getAlgorithmName();
082:
083: if (pks.containsKey(name)) {
084: log
085: .debug("Standard public key "
086: + name
087: + " is being overidden by "
088: + algorithm
089: .getImplementationClass());
090: } else {
091: log
092: .debug(algorithm
093: .getAlgorithmName()
094: + " public key is implemented by "
095: + algorithm
096: .getImplementationClass());
097: }
098:
099: try {
100: pks
101: .put(
102: algorithm
103: .getAlgorithmName(),
104: ConfigurationLoader
105: .getExtensionClass(algorithm
106: .getImplementationClass()));
107: } catch (ClassNotFoundException cnfe) {
108: log
109: .error("Could not locate "
110: + algorithm
111: .getImplementationClass());
112: }
113: }
114: }
115:
116: defaultAlgorithm = config.getDefaultPublicKey();
117: }
118: }
119: } catch (ConfigurationException ex) {
120: }
121:
122: if ((defaultAlgorithm == null)
123: || !pks.containsKey(defaultAlgorithm)) {
124: log
125: .debug("The default public key is not set! using first in list");
126:
127: Iterator it = pks.keySet().iterator();
128: defaultAlgorithm = (String) it.next();
129: }
130: }
131:
132: /**
133: * Creates a new SshKeyPairFactory object.
134: */
135: protected SshKeyPairFactory() {
136: }
137:
138: /**
139: *
140: */
141: public static void initialize() {
142: }
143:
144: /**
145: *
146: *
147: * @return
148: */
149: public static String getDefaultPublicKey() {
150: return defaultAlgorithm;
151: }
152:
153: /**
154: *
155: *
156: * @return
157: */
158: public static List getSupportedKeys() {
159: // Get the list of pks
160: return new ArrayList(pks.keySet());
161: }
162:
163: /**
164: *
165: *
166: * @param methodName
167: *
168: * @return
169: *
170: * @throws AlgorithmNotSupportedException
171: */
172: public static SshKeyPair newInstance(String methodName)
173: throws AlgorithmNotSupportedException {
174: try {
175: return (SshKeyPair) ((Class) pks.get(methodName))
176: .newInstance();
177: } catch (Exception e) {
178: throw new AlgorithmNotSupportedException(methodName
179: + " is not supported!");
180: }
181: }
182:
183: /**
184: *
185: *
186: * @param algorithm
187: *
188: * @return
189: */
190: public static boolean supportsKey(String algorithm) {
191: return pks.containsKey(algorithm);
192: }
193:
194: /**
195: *
196: *
197: * @param encoded
198: *
199: * @return
200: *
201: * @throws InvalidSshKeyException
202: * @throws AlgorithmNotSupportedException
203: */
204: public static SshPrivateKey decodePrivateKey(byte[] encoded)
205: throws InvalidSshKeyException,
206: AlgorithmNotSupportedException {
207: try {
208: ByteArrayReader bar = new ByteArrayReader(encoded);
209: String algorithm = bar.readString();
210:
211: if (supportsKey(algorithm)) {
212: SshKeyPair pair = newInstance(algorithm);
213:
214: return pair.decodePrivateKey(encoded);
215: } else {
216: throw new AlgorithmNotSupportedException(algorithm
217: + " is not supported");
218: }
219: } catch (IOException ioe) {
220: throw new InvalidSshKeyException(ioe.getMessage());
221: }
222: }
223:
224: /**
225: *
226: *
227: * @param encoded
228: *
229: * @return
230: *
231: * @throws InvalidSshKeyException
232: * @throws AlgorithmNotSupportedException
233: */
234: public static SshPublicKey decodePublicKey(byte[] encoded)
235: throws InvalidSshKeyException,
236: AlgorithmNotSupportedException {
237: try {
238: ByteArrayReader bar = new ByteArrayReader(encoded);
239: String algorithm = bar.readString();
240:
241: if (supportsKey(algorithm)) {
242: SshKeyPair pair = newInstance(algorithm);
243:
244: return pair.decodePublicKey(encoded);
245: } else {
246: throw new AlgorithmNotSupportedException(algorithm
247: + " is not supported");
248: }
249: } catch (IOException ioe) {
250: throw new InvalidSshKeyException(ioe.getMessage());
251: }
252: }
253: }
|