1 19 20 package com.sslexplorer.security.pki; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.maverick.util.ByteArrayReader; 31 32 33 38 public class SshPrivateKeyFile { 39 private static Log log = LogFactory.getLog(SshPrivateKeyFile.class); 40 private SshPrivateKeyFormat format; 41 private byte[] keyblob; 42 43 49 protected SshPrivateKeyFile(byte[] keyblob, SshPrivateKeyFormat format) { 50 this.keyblob = keyblob; 51 this.format = format; 52 } 53 54 59 public byte[] getBytes() { 60 return keyblob; 61 } 62 63 72 public byte[] getKeyBlob(String passphrase) throws InvalidKeyException { 73 return format.decryptKeyblob(keyblob, passphrase); 74 } 75 76 84 public void changePassphrase(String oldPassphrase, String newPassphrase) 85 throws InvalidKeyException { 86 byte[] raw = format.decryptKeyblob(keyblob, oldPassphrase); 87 keyblob = format.encryptKeyblob(raw, newPassphrase); 88 } 89 90 99 public static SshPrivateKeyFile parse(byte[] formattedKey) 100 throws InvalidKeyException { 101 if (formattedKey == null) { 102 throw new InvalidKeyException("Key data is null"); 103 } 104 105 if (log.isInfoEnabled()) 106 log.info("Parsing private key file"); 107 108 SshPrivateKeyFormat format; 110 format = SshPrivateKeyFormatFactory.newInstance(SshPrivateKeyFormatFactory.getDefaultFormatType()); 111 112 boolean valid = format.isFormatted(formattedKey); 113 114 if (valid) { 115 return new SshPrivateKeyFile(formattedKey, format); 116 } else { 117 throw new InvalidKeyException( 118 "The key format is not a supported format"); 119 } 120 } 121 122 132 public static SshPrivateKeyFile parse(File keyfile) 133 throws InvalidKeyException, IOException { 134 135 return parse(new FileInputStream (keyfile)); 136 } 137 138 144 public static SshPrivateKeyFile parse(InputStream in) 145 throws InvalidKeyException, IOException { 146 147 byte[] data = null; 148 149 try { 150 data = new byte[in.available()]; 151 in.read(data); 152 } finally { 153 try { 154 if (in != null) { 155 in.close(); 156 } 157 } catch (IOException ex) { 158 } 159 } 160 161 return parse(data); 162 } 163 164 169 public boolean isPassphraseProtected() { 170 return format.isPassphraseProtected(keyblob); 171 } 172 173 177 184 public static SshPrivateKeyFile create(SshPrivateKey key, 185 String passphrase, SshPrivateKeyFormat format) 186 throws InvalidKeyException { 187 byte[] keyblob = format.encryptKeyblob(key.getEncoded(), passphrase); 188 189 return new SshPrivateKeyFile(keyblob, format); 190 } 191 192 200 public void setFormat(SshPrivateKeyFormat newFormat, String passphrase) 201 throws InvalidKeyException { 202 byte[] raw = this.format.decryptKeyblob(keyblob, passphrase); 203 format = newFormat; 204 keyblob = format.encryptKeyblob(raw, passphrase); 205 } 206 207 212 public SshPrivateKeyFormat getFormat() { 213 return format; 214 } 215 216 225 public SshPrivateKey toPrivateKey(String passphrase) 226 throws InvalidKeyException { 227 byte[] raw = format.decryptKeyblob(keyblob, passphrase); 228 SshKeyPair pair = SshKeyPairFactory.newInstance(getAlgorithm(raw)); 229 230 return pair.decodePrivateKey(raw); 231 232 } 233 234 239 public String toString() { 240 return new String (keyblob); 241 } 242 243 247 private String getAlgorithm(byte[] raw) { 248 try { 249 ByteArrayReader r = new ByteArrayReader(raw); 250 return r.readString(); 251 } catch (IOException e) { 252 return null; 253 } 254 } 255 } 256 | Popular Tags |