diff options
author | Kenny Root <kenny@the-b.org> | 2015-12-26 00:22:28 -0600 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2015-12-26 00:22:28 -0600 |
commit | 3bc88faec1312f81e172f927151bd23408257d2c (patch) | |
tree | 20cb84b576ee22d1ce7cfd0a27efe098a0f8ce6d /sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java | |
parent | 39aef25501455b50fff7e0cb3ddf5399ab4bfc29 (diff) | |
parent | 065d92593af8bd43b47f016ebf6b4c1dd24fb769 (diff) | |
download | sshlib-3bc88faec1312f81e172f927151bd23408257d2c.tar.gz sshlib-3bc88faec1312f81e172f927151bd23408257d2c.tar.bz2 sshlib-3bc88faec1312f81e172f927151bd23408257d2c.zip |
Merge pull request #6 from kruton/unitTests
Unit tests
Diffstat (limited to 'sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java')
-rw-r--r-- | sshlib/src/main/java/com/trilead/ssh2/signature/ECDSASHA2Verify.java | 80 |
1 files changed, 5 insertions, 75 deletions
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 <len> <data[len]> */ - 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 <r,s> to its own types writer. TypesWriter rsWriter = new TypesWriter(); |