String header = new String(sig);
if (log.isDebugEnabled())
log.debug("Header is " + header);
if (!header.equals("ssh-dss")) {
throw new InvalidSignatureException();
}
signature = bar.readBinaryString();
//if (log.isDebugEnabled()) {log.debug("Read signature from blob: " + new String(signature));}
}
// Using a SimpleASNWriter
ByteArrayOutputStream r = new ByteArrayOutputStream();
ByteArrayOutputStream s = new ByteArrayOutputStream();
SimpleASNWriter asn = new SimpleASNWriter();
asn.writeByte(0x02);
if (((signature[0] & 0x80) == 0x80) && (signature[0] != 0x00)) {
r.write(0);
r.write(signature, 0, 20);
} else {
r.write(signature, 0, 20);
}
asn.writeData(r.toByteArray());
asn.writeByte(0x02);
if (((signature[20] & 0x80) == 0x80) && (signature[20] != 0x00)) {
s.write(0);
s.write(signature, 20, 20);
} else {
s.write(signature, 20, 20);
}
asn.writeData(s.toByteArray());
SimpleASNWriter asnEncoded = new SimpleASNWriter();
asnEncoded.writeByte(0x30);
asnEncoded.writeData(asn.toByteArray());
byte[] encoded = asnEncoded.toByteArray();
if (log.isDebugEnabled()) {
log.debug("Verifying host key signature");
log.debug("Signature length is " +
String.valueOf(signature.length));
String hex = "";
for (int i = 0; i < signature.length; i++) {
hex += (Integer.toHexString(signature[i] & 0xFF) + " ");
}
log.debug("SSH: " + hex);
hex = "";
for (int i = 0; i < encoded.length; i++) {
hex += (Integer.toHexString(encoded[i] & 0xFF) + " ");
}
log.debug("Encoded: " + hex);
}
// The previous way
/*byte[] encoded;
// Determine the encoded length of the big integers
int rlen = (((signature[0] & 0x80) == 0x80) ? 0x15 : 0x14);
log.debug("rlen=" + String.valueOf(rlen));
int slen = (((signature[20] & 0x80) == 0x80) ? 0x15 : 0x14);
log.debug("slen=" + String.valueOf(slen));
byte[] asn1r = { 0x30, (byte) (rlen + slen + 4), 0x02, (byte) rlen };
byte[] asn1s = { 0x02, (byte) slen };
// Create the encoded byte array
encoded = new byte[asn1r.length + rlen + asn1s.length + slen];
// Copy the data and encode it into the array
System.arraycopy(asn1r, 0, encoded, 0, asn1r.length);
// Copy the integer inserting a zero byte if signed
int roffset = (((signature[0] & 0x80) == 0x80) ? 1 : 0);
System.arraycopy(signature, 0, encoded, asn1r.length + roffset, 20);
System.arraycopy(asn1s, 0, encoded, asn1r.length + roffset + 20,
asn1s.length);
int soffset = (((signature[20] & 0x80) == 0x80) ? 1 : 0);
System.arraycopy(signature, 20, encoded,
asn1r.length + roffset + 20 + asn1s.length + soffset, 20);
*/
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initVerify(pubkey);
sig.update(data);
return sig.verify(encoded);
} catch (NoSuchAlgorithmException nsae) {
throw new InvalidSignatureException();
} catch (java.security.InvalidKeyException ike) {
throw new InvalidSignatureException();
} catch (IOException ioe) {
throw new InvalidSignatureException();
} catch (SignatureException se) {
throw new InvalidSignatureException();
}
}