1 19 20 package com.sslexplorer.security.pki.rsa; 21 22 23 import java.io.IOException ; 24 import java.math.BigInteger ; 25 import java.security.KeyFactory ; 26 import java.security.NoSuchAlgorithmException ; 27 import java.security.PublicKey ; 28 import java.security.Signature ; 29 import java.security.SignatureException ; 30 import java.security.interfaces.RSAPublicKey ; 31 import java.security.spec.InvalidKeySpecException ; 32 import java.security.spec.RSAPublicKeySpec ; 33 34 import com.maverick.util.ByteArrayReader; 35 import com.maverick.util.ByteArrayWriter; 36 import com.sslexplorer.security.pki.InvalidKeyException; 37 import com.sslexplorer.security.pki.InvalidSignatureException; 38 import com.sslexplorer.security.pki.SshPublicKey; 39 40 41 46 public class SshRsaPublicKey extends SshPublicKey { 47 RSAPublicKey pubKey; 48 49 54 public SshRsaPublicKey(RSAPublicKey key) { 55 pubKey = key; 56 } 57 58 59 public PublicKey getPublicKey() { 60 return pubKey; 61 } 62 63 70 public SshRsaPublicKey(byte[] encoded) throws InvalidKeyException { 71 try { 72 RSAPublicKeySpec rsaKey; 74 75 ByteArrayReader bar = new ByteArrayReader(encoded); 77 String header = bar.readString(); 78 79 if (!header.equals(getAlgorithmName())) { 80 throw new InvalidKeyException(); 81 } 82 83 BigInteger e = bar.readBigInteger(); 84 BigInteger n = bar.readBigInteger(); 85 rsaKey = new RSAPublicKeySpec (n, e); 86 87 try { 88 KeyFactory kf = KeyFactory.getInstance("RSA"); 89 pubKey = (RSAPublicKey ) kf.generatePublic(rsaKey); 90 } catch (NoSuchAlgorithmException nsae) { 91 throw new InvalidKeyException(); 92 } catch (InvalidKeySpecException ikpe) { 93 throw new InvalidKeyException(); 94 } 95 } catch (IOException ioe) { 96 throw new InvalidKeyException(); 97 } 98 } 99 100 105 public String getAlgorithmName() { 106 return "ssh-rsa"; 107 } 108 109 114 public int getBitLength() { 115 return pubKey.getModulus().bitLength(); 116 } 117 118 123 public byte[] getEncoded() { 124 try { 125 ByteArrayWriter baw = new ByteArrayWriter(); 126 baw.writeString(getAlgorithmName()); 127 baw.writeBigInteger(pubKey.getPublicExponent()); 128 baw.writeBigInteger(pubKey.getModulus()); 129 130 return baw.toByteArray(); 131 } catch (IOException ioe) { 132 return null; 133 } 134 } 135 136 146 public boolean verifySignature(byte[] signature, byte[] data) 147 throws InvalidSignatureException { 148 try { 149 if (signature.length != 128) { 151 ByteArrayReader bar = new ByteArrayReader(signature); 152 byte[] sig = bar.readBinaryString(); 153 String header = new String (sig); 154 155 if (!header.equals(getAlgorithmName())) { 156 throw new InvalidSignatureException(); 157 } 158 159 signature = bar.readBinaryString(); 160 } 161 162 Signature s = Signature.getInstance("SHA1withRSA"); 163 s.initVerify(pubKey); 164 s.update(data); 165 166 return s.verify(signature); 167 } catch (NoSuchAlgorithmException nsae) { 168 throw new InvalidSignatureException(); 169 } catch (IOException ioe) { 170 throw new InvalidSignatureException(); 171 } catch (java.security.InvalidKeyException ike) { 172 throw new InvalidSignatureException(); 173 } catch (SignatureException se) { 174 throw new InvalidSignatureException(); 175 } 176 } 177 } 178 | Popular Tags |