Skip to content

Commit

Permalink
#89 fix rare ECDSA signature validation issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
norrisjeremy committed Oct 30, 2021
1 parent 51187fc commit c10fc00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* [0.1.70](https://github.com/mwiede/jsch/releases/tag/jsch-0.1.70)
* Address [#89](https://github.com/mwiede/jsch/issues/89) by fixing rare ECDSA signature validation issue
* [0.1.69](https://github.com/mwiede/jsch/releases/tag/jsch-0.1.69)
* Address [#83](https://github.com/mwiede/jsch/issues/83) by sending CR LF at the end of the identification string
* Fix earlier change for [#76](https://github.com/mwiede/jsch/issues/76) that failed to correctly make the "Host" keyword case-insensitive
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/com/jcraft/jsch/jce/SignatureECDSAN.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public boolean verify(byte[] sig) throws Exception{
byte[] r = b.getMPInt();
byte[] s = b.getMPInt();

r=insert0(r);
s=insert0(s);
r=trimLeadingZeros(insert0(r));
s=trimLeadingZeros(insert0(s));

byte[] asn1 = null;
if(r.length<64){
Expand Down Expand Up @@ -175,22 +175,40 @@ public boolean verify(byte[] sig) throws Exception{
return signature.verify(sig);
}

private byte[] insert0(byte[] buf){
private static byte[] insert0(byte[] buf){
if ((buf[0] & 0x80) == 0) return buf;
byte[] tmp = new byte[buf.length+1];
System.arraycopy(buf, 0, tmp, 1, buf.length);
bzero(buf);
return tmp;
}
private byte[] chop0(byte[] buf){

private static byte[] chop0(byte[] buf){
if(buf[0]!=0) return buf;
byte[] tmp = new byte[buf.length-1];
System.arraycopy(buf, 1, tmp, 0, tmp.length);
bzero(buf);
return tmp;
}

private void bzero(byte[] buf){
private static void bzero(byte[] buf){
for(int i = 0; i<buf.length; i++) buf[i]=0;
}

private static byte[] trimLeadingZeros(byte[] buf){
if(buf.length<2) return buf;

int i=0;
while(i<buf.length-1){
if(buf[i] == 0 && (buf[i+1] & 0x80) == 0) i++;
else break;
}

if(i == 0) return buf;

byte[] tmp = new byte[buf.length-i];
System.arraycopy(buf, i, tmp, 0, tmp.length);
bzero(buf);
return tmp;
}
}

0 comments on commit c10fc00

Please sign in to comment.