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 SshPublicKeyFile { 39 private static Log log = LogFactory.getLog(SshPublicKeyFile.class); 40 private SshPublicKeyFormat format; 41 private byte[] keyblob; 42 private String comment; 43 44 50 protected SshPublicKeyFile(byte[] keyblob, SshPublicKeyFormat format) { 51 this.keyblob = keyblob; 52 this.format = format; 53 } 54 55 60 public byte[] getBytes() { 61 return format.formatKey(keyblob); 62 } 63 64 69 public String getComment() { 70 return comment; 71 } 72 73 78 public void setComment(String comment) { 79 this.comment = comment; 80 } 81 82 87 public byte[] getKeyBlob() { 88 return keyblob; 89 } 90 91 99 public static SshPublicKeyFile create(SshPublicKey key, 100 SshPublicKeyFormat format) { 101 SshPublicKeyFile file = new SshPublicKeyFile(key.getEncoded(), format); 102 file.setComment(format.getComment()); 103 104 return file; 105 } 106 107 117 public static SshPublicKeyFile parse(File keyfile) 118 throws InvalidKeyException, IOException { 119 120 return parse(new FileInputStream (keyfile)); 121 } 122 123 129 public static SshPublicKeyFile parse(InputStream in) 130 throws InvalidKeyException, IOException { 131 132 byte[] data = new byte[in.available()]; 133 in.read(data); 134 in.close(); 135 136 return parse(data); 137 } 138 139 148 public static SshPublicKeyFile parse(byte[] formattedKey) 149 throws InvalidKeyException { 150 if (log.isInfoEnabled()) 151 log.info("Parsing public key file"); 152 153 SshPublicKeyFormat format; 155 format = SshPublicKeyFormatFactory.newInstance(SshPublicKeyFormatFactory.getDefaultFormatType()); 156 157 boolean valid = format.isFormatted(formattedKey); 158 159 160 if (valid) { 161 SshPublicKeyFile file = new SshPublicKeyFile(format.getKeyBlob( 162 formattedKey), format); 163 file.setComment(format.getComment()); 164 165 return file; 166 } else { 167 throw new InvalidKeyException( 168 "The key format is not a supported format"); 169 } 170 } 171 172 177 public String getAlgorithm() { 178 try { 179 ByteArrayReader r = new ByteArrayReader(keyblob); 180 return r.readString(); 181 } catch (IOException e) { 182 return null; 183 } 184 } 185 186 193 public void setFormat(SshPublicKeyFormat newFormat) 194 throws InvalidKeyException { 195 if (newFormat.supportsAlgorithm(getAlgorithm())) { 196 newFormat.setComment(format.getComment()); 197 this.format = newFormat; 198 } else { 199 throw new InvalidKeyException( 200 "The format does not support the public key algorithm"); 201 } 202 } 203 204 209 public SshPublicKeyFormat getFormat() { 210 return format; 211 } 212 213 220 public SshPublicKey toPublicKey() throws IOException , InvalidKeyException { 221 ByteArrayReader bar = new ByteArrayReader(keyblob); 222 String type = bar.readString(); 223 SshKeyPair pair = SshKeyPairFactory.newInstance(type); 224 225 return pair.decodePublicKey(keyblob); 226 } 227 228 233 public String toString() { 234 return new String (format.formatKey(keyblob)); 235 } 236 } 237 | Popular Tags |