1 19 20 package com.sslexplorer.security.pki; 21 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import com.maverick.util.ByteArrayReader; 36 import com.sslexplorer.security.pki.dsa.SshDssKeyPair; 37 import com.sslexplorer.security.pki.rsa.SshRsaKeyPair; 38 39 40 45 public class SshKeyPairFactory { 46 private static Map pks; 47 private static String defaultAlgorithm; 48 private static Log log = LogFactory.getLog(SshKeyPairFactory.class); 49 50 static { 51 pks = new HashMap (); 52 if (log.isInfoEnabled()) 53 log.info("Loading public key algorithms"); 54 55 pks.put("ssh-rsa", SshRsaKeyPair.class); 56 pks.put("ssh-dss", SshDssKeyPair.class); 57 58 if ((defaultAlgorithm == null) || !pks.containsKey(defaultAlgorithm)) { 59 Iterator it = pks.keySet().iterator(); 60 defaultAlgorithm = (String ) it.next(); 61 } 62 } 63 64 67 protected SshKeyPairFactory() { 68 } 69 70 73 public static void initialize() { 74 } 75 76 81 public static String getDefaultPublicKey() { 82 return defaultAlgorithm; 83 } 84 85 90 public static List getSupportedKeys() { 91 return new ArrayList (pks.keySet()); 93 } 94 95 103 public static SshKeyPair newInstance(String methodName) { 104 try { 105 return (SshKeyPair) ((Class ) pks.get(methodName)).newInstance(); 106 } catch (Exception e) { 107 return null; 108 } 109 } 110 111 118 public static boolean supportsKey(String algorithm) { 119 return pks.containsKey(algorithm); 120 } 121 122 131 public static SshPrivateKey decodePrivateKey(byte[] encoded) 132 throws InvalidKeyException { 133 try { 134 ByteArrayReader bar = new ByteArrayReader(encoded); 135 String algorithm = bar.readString(); 136 137 if (supportsKey(algorithm)) { 138 SshKeyPair pair = newInstance(algorithm); 139 140 return pair.decodePrivateKey(encoded); 141 } else { 142 return null; 143 } 144 } catch (IOException ioe) { 145 return null; 146 } 147 } 148 149 public static SshPrivateKey decodePrivateKey(InputStream in) 150 throws InvalidKeyException, IOException { 151 152 153 ByteArrayOutputStream out = new ByteArrayOutputStream (); 154 byte[] buf = new byte[4096]; 155 int read; 156 157 while((read = in.read(buf)) > -1) { 158 out.write(buf, 0, read); 159 } 160 161 return decodePrivateKey(out.toByteArray()); 162 163 } 164 173 public static SshPublicKey decodePublicKey(byte[] encoded) 174 throws InvalidKeyException { 175 try { 176 ByteArrayReader bar = new ByteArrayReader(encoded); 177 String algorithm = bar.readString(); 178 179 if (supportsKey(algorithm)) { 180 SshKeyPair pair = newInstance(algorithm); 181 182 return pair.decodePublicKey(encoded); 183 } else { 184 return null; 185 } 186 } catch (IOException ioe) { 187 return null; 188 } 189 } 190 191 public static SshPublicKey decodePublicKey(InputStream in) 192 throws InvalidKeyException, IOException { 193 194 195 ByteArrayOutputStream out = new ByteArrayOutputStream (); 196 byte[] buf = new byte[4096]; 197 int read; 198 199 while((read = in.read(buf)) > -1) { 200 out.write(buf, 0, read); 201 } 202 203 return decodePublicKey(out.toByteArray()); 204 205 } 206 } 207 208 | Popular Tags |