From 065d92593af8bd43b47f016ebf6b4c1dd24fb769 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Sat, 26 Dec 2015 00:13:30 -0600 Subject: Add tests for ECDSASHA2Verify Since SimpleDERReader is now more tested and this class duplicates the functionality of that one, we will switch to using SimpleDERReader enstead. --- .../trilead/ssh2/signature/ECDSASHA2Verify.java | 80 ++-------------------- 1 file changed, 5 insertions(+), 75 deletions(-) (limited to 'sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java') diff --git a/sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java b/sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java index f628ae7..89cacfb 100644 --- a/sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java +++ b/sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java @@ -24,6 +24,7 @@ import java.security.spec.KeySpec; import java.util.Map; import java.util.TreeMap; +import com.trilead.ssh2.crypto.SimpleDERReader; import com.trilead.ssh2.log.Logger; import com.trilead.ssh2.packets.TypesReader; import com.trilead.ssh2.packets.TypesWriter; @@ -267,24 +268,6 @@ public class ECDSASHA2Verify { } } - private static final int readLength(byte[] sig, int offset, int numOctets) throws IOException { - if (numOctets > 4 || numOctets <= 0) { - throw new IOException("Cannot decode DER length"); - } - - long length = 0L; - for (int i = 0; i < numOctets; i++) { - length <<= 8; - length |= sig[offset++]; - } - - if (length > 0xFFFFFFL || length < 0L) { - throw new IOException("Invalid DER length"); - } - - return (int) length; - } - public static byte[] encodeSSHECDSASignature(byte[] sig, ECParameterSpec params) throws IOException { TypesWriter tw = new TypesWriter(); @@ -299,64 +282,11 @@ public class ECDSASHA2Verify { * 0x02 */ - if (sig[0] != 0x30) { - throw new IOException("Invalid signature format"); - } - - final int seqHeaderLength; - final int seqLength; - if ((sig[1] & 0x80) != 0) { - int seqHeaderOctets = sig[1] & 0x7F; - seqHeaderLength = seqHeaderOctets + 1; - seqLength = readLength(sig, 2, seqHeaderOctets); - } else { - seqHeaderLength = 1; - seqLength = sig[1]; - } - - if ((seqLength == 0) || (1 + seqHeaderLength + seqLength != sig.length) || (sig[1 + seqHeaderLength] != 0x02)) { - throw new IOException("Invalid signature format"); - } - - final int rHeaderLength; - final int rLength; - if ((sig[1 + seqHeaderLength + 1] & 0x80) != 0) { - int rHeaderOctets = sig[seqHeaderLength + 2] & 0x7F; - rHeaderLength = rHeaderOctets + 1; - rLength = readLength(sig, seqHeaderLength + 3, rHeaderOctets); - } else { - rHeaderLength = 1; - rLength = sig[seqHeaderLength + 2]; - } - - if ((rLength == 0) || (rLength > seqLength - (rHeaderLength + 1 + 1 + 1)) || - sig[1 + seqHeaderLength + 1 + rHeaderLength + rLength] != 0x02) { - throw new IOException("Invalid signature format"); - } - - final int sHeaderLength; - final int sLength; - if ((sig[1 + seqHeaderLength + 1 + rHeaderLength + rLength + 1] & 0x80) != 0) { - int sHeaderOctets = sig[1 + seqHeaderLength + 1 + rHeaderLength + rLength + 1] & 0x7F; - sHeaderLength = sHeaderOctets + 1; - sLength = readLength(sig, 4 + rHeaderLength + rLength, sHeaderOctets); - } else { - sHeaderLength = 1; - sLength = sig[1 + seqHeaderLength + 1 + rHeaderLength + rLength + 1]; - } - - if ((sLength == 0) || 2 + rHeaderLength + rLength + sHeaderLength + sLength > seqLength) { - throw new IOException("Invalid signature format"); - } - - byte[] rArray = new byte[rLength]; - byte[] sArray = new byte[sLength]; - - System.arraycopy(sig, 1 + seqHeaderLength + 1, rArray, 0, rLength); - System.arraycopy(sig, 1 + seqHeaderLength + 1 + rLength + 1 + sHeaderLength, sArray, 0, sLength); + SimpleDERReader reader = new SimpleDERReader(sig); + SimpleDERReader seqReader = new SimpleDERReader(reader.readSequenceAsByteArray()); - BigInteger r = new BigInteger(1, rArray); - BigInteger s = new BigInteger(1, sArray); + BigInteger r = seqReader.readInt(); + BigInteger s = seqReader.readInt(); // Write the to its own types writer. TypesWriter rsWriter = new TypesWriter(); -- cgit v1.2.3