1 19 20 package com.sslexplorer.security.pki.dsa; 21 22 import java.io.IOException ; 23 import java.math.BigInteger ; 24 import java.security.KeyFactory ; 25 import java.security.PrivateKey ; 26 import java.security.Signature ; 27 import java.security.interfaces.DSAPrivateKey ; 28 import java.security.interfaces.DSAPublicKey ; 29 import java.security.spec.DSAPrivateKeySpec ; 30 import java.security.spec.DSAPublicKeySpec ; 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.maverick.util.ByteArrayWriter; 37 import com.sslexplorer.security.pki.InvalidKeyException; 38 import com.sslexplorer.security.pki.InvalidSignatureException; 39 import com.sslexplorer.security.pki.SimpleASNReader; 40 import com.sslexplorer.security.pki.SshPrivateKey; 41 import com.sslexplorer.security.pki.SshPublicKey; 42 43 44 class SshDssPrivateKey extends SshPrivateKey { 45 private static Log log = LogFactory.getLog(SshDssPrivateKey.class); 46 DSAPrivateKey prvkey; 47 48 53 public SshDssPrivateKey(DSAPrivateKey prvkey) { 54 this.prvkey = prvkey; 55 } 56 57 public PrivateKey getPrivateKey() { 58 return prvkey; 59 } 60 61 68 public SshDssPrivateKey(byte[] key) throws InvalidKeyException { 69 try { 70 DSAPrivateKeySpec dsaKey; 71 72 ByteArrayReader bar = new ByteArrayReader(key); 74 String header = bar.readString(); 75 76 if (!header.equals(getAlgorithmName())) { 77 throw new InvalidKeyException(); 78 } 79 80 BigInteger p = bar.readBigInteger(); 81 BigInteger q = bar.readBigInteger(); 82 BigInteger g = bar.readBigInteger(); 83 BigInteger x = bar.readBigInteger(); 84 dsaKey = new DSAPrivateKeySpec (x, p, q, g); 85 86 KeyFactory kf = KeyFactory.getInstance("DSA"); 87 prvkey = (DSAPrivateKey ) kf.generatePrivate(dsaKey); 88 } catch (Exception e) { 89 throw new InvalidKeyException(); 90 } 91 } 92 93 100 public boolean equals(Object obj) { 101 if (obj instanceof SshDssPrivateKey) { 102 return prvkey.equals(((SshDssPrivateKey) obj).prvkey); 103 } 104 105 return false; 106 } 107 108 113 public int hashCode() { 114 return prvkey.hashCode(); 115 } 116 117 122 public String getAlgorithmName() { 123 return "ssh-dss"; 124 } 125 126 131 public int getBitLength() { 132 return prvkey.getX().bitLength(); 133 } 134 135 140 public byte[] getEncoded() { 141 try { 142 ByteArrayWriter baw = new ByteArrayWriter(); 143 baw.writeString("ssh-dss"); 144 baw.writeBigInteger(prvkey.getParams().getP()); 145 baw.writeBigInteger(prvkey.getParams().getQ()); 146 baw.writeBigInteger(prvkey.getParams().getG()); 147 baw.writeBigInteger(prvkey.getX()); 148 149 return baw.toByteArray(); 150 } catch (IOException ioe) { 151 return null; 152 } 153 } 154 155 160 public SshPublicKey getPublicKey() { 161 try { 162 DSAPublicKeySpec spec = new DSAPublicKeySpec (getY(), 163 prvkey.getParams().getP(), prvkey.getParams().getQ(), 164 prvkey.getParams().getG()); 165 KeyFactory kf = KeyFactory.getInstance("DSA"); 166 167 return new SshDssPublicKey((DSAPublicKey ) kf.generatePublic(spec)); 168 } catch (Exception e) { 169 return null; 170 } 171 } 172 173 182 public byte[] generateSignature(byte[] data) 183 throws InvalidSignatureException { 184 try { 185 Signature sig = Signature.getInstance("SHA1withDSA"); 186 sig.initSign(prvkey); 187 188 sig.update(data); 189 190 byte[] signature = sig.sign(); 191 byte[] decoded = new byte[40]; 192 SimpleASNReader asn = new SimpleASNReader(signature); 193 asn.getByte(); 194 asn.getLength(); 195 asn.getByte(); 196 197 byte[] r = asn.getData(); 198 asn.getByte(); 199 200 byte[] s = asn.getData(); 201 202 if (r.length >= 20) { 203 System.arraycopy(r, r.length - 20, decoded, 0, 20); 204 } else { 205 System.arraycopy(r, 0, decoded, 20 - r.length, r.length); 206 } 207 208 if (s.length >= 20) { 209 System.arraycopy(s, s.length - 20, decoded, 20, 20); 210 } else { 211 System.arraycopy(s, 0, decoded, 20 + (20 - s.length), s.length); 212 } 213 214 if (log.isDebugEnabled()) { 215 log.debug("s length is " + String.valueOf(s.length)); 216 log.debug("r length is " + String.valueOf(r.length)); 217 218 String str = ""; 219 220 for (int i = 0; i < signature.length; i++) { 221 str += (Integer.toHexString(signature[i] & 0xFF) + " "); 222 } 223 224 log.debug("Java signature is " + str); 225 str = ""; 226 227 for (int i = 0; i < decoded.length; i++) { 228 str += (Integer.toHexString(decoded[i] & 0xFF) + " "); 229 } 230 231 log.debug("SSH signature is " + str); 232 } 233 234 ByteArrayWriter baw = new ByteArrayWriter(); 235 baw.writeString(getAlgorithmName()); 236 baw.writeBinaryString(decoded); 237 238 return baw.toByteArray(); 239 } catch (Exception e) { 240 throw new InvalidSignatureException(e); 241 } 242 } 243 244 private BigInteger getY() { 245 return prvkey.getParams().getG().modPow(prvkey.getX(), 246 prvkey.getParams().getP()); 247 } 248 } 249 | Popular Tags |