Skip to content

Commit

Permalink
Replace BC ASN.1 dependency with asn-one library
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Jun 2, 2020
1 parent 3194fd9 commit 2baf51b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation "org.bouncycastle:bcprov-jdk15on:$bouncycastleVersion"
implementation "org.bouncycastle:bcpkix-jdk15on:$bouncycastleVersion"
implementation "com.jcraft:jzlib:1.1.3"
implementation "com.hierynomus:asn-one:0.4.0"

implementation "net.i2p.crypto:eddsa:0.3.0"

Expand Down
22 changes: 14 additions & 8 deletions src/main/java/net/schmizz/sshj/signature/SignatureDSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
*/
package net.schmizz.sshj.signature;

import com.hierynomus.asn1.encodingrules.der.DEREncoder;
import com.hierynomus.asn1.types.ASN1Object;
import com.hierynomus.asn1.types.constructed.ASN1Sequence;
import com.hierynomus.asn1.types.primitive.ASN1Integer;
import net.schmizz.sshj.common.KeyType;
import net.schmizz.sshj.common.SSHRuntimeException;
import org.bouncycastle.asn1.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* DSA {@link Signature}
Expand Down Expand Up @@ -97,18 +102,19 @@ public boolean verify(byte[] sig) {
* Encodes the signature as a DER sequence (ASN.1 format).
*/
private byte[] asnEncode(byte[] sigBlob) throws IOException {
byte[] r = new BigInteger(1, Arrays.copyOfRange(sigBlob, 0, 20)).toByteArray();
byte[] s = new BigInteger(1, Arrays.copyOfRange(sigBlob, 20, 40)).toByteArray();
BigInteger r = new BigInteger(1, Arrays.copyOfRange(sigBlob, 0, 20));
BigInteger s = new BigInteger(1, Arrays.copyOfRange(sigBlob, 20, 40));

ASN1EncodableVector vector = new ASN1EncodableVector();
vector.add(new ASN1Integer(r));
List<ASN1Object> vector = new ArrayList<ASN1Object>();
vector.add(new com.hierynomus.asn1.types.primitive.ASN1Integer(r));
vector.add(new ASN1Integer(s));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ASN1OutputStream asnOS = new ASN1OutputStream(baos);
com.hierynomus.asn1.ASN1OutputStream asn1OutputStream = new com.hierynomus.asn1.ASN1OutputStream(new DEREncoder(), baos);

asn1OutputStream.writeObject(new ASN1Sequence(vector));
asn1OutputStream.flush();

asnOS.writeObject(new DERSequence(vector));
asnOS.flush();

return baos.toByteArray();
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/net/schmizz/sshj/signature/SignatureECDSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package net.schmizz.sshj.signature;

import com.hierynomus.asn1.encodingrules.der.DEREncoder;
import com.hierynomus.asn1.types.ASN1Object;
import com.hierynomus.asn1.types.constructed.ASN1Sequence;
import com.hierynomus.asn1.types.primitive.ASN1Integer;
import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.KeyType;
import net.schmizz.sshj.common.SSHRuntimeException;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1OutputStream;
import org.bouncycastle.asn1.DERSequence;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;

/** ECDSA {@link Signature} */
public class SignatureECDSA extends AbstractSignature {
Expand Down Expand Up @@ -122,18 +124,18 @@ public boolean verify(byte[] sig) {
*/
private byte[] asnEncode(byte[] sigBlob) throws IOException {
Buffer.PlainBuffer sigbuf = new Buffer.PlainBuffer(sigBlob);
byte[] r = sigbuf.readBytes();
byte[] s = sigbuf.readBytes();
BigInteger r = sigbuf.readMPInt();
BigInteger s = sigbuf.readMPInt();

ASN1EncodableVector vector = new ASN1EncodableVector();
List<ASN1Object> vector = new ArrayList<ASN1Object>();
vector.add(new ASN1Integer(r));
vector.add(new ASN1Integer(s));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ASN1OutputStream asnOS = new ASN1OutputStream(baos);
com.hierynomus.asn1.ASN1OutputStream asn1OutputStream = new com.hierynomus.asn1.ASN1OutputStream(new DEREncoder(), baos);

asnOS.writeObject(new DERSequence(vector));
asnOS.flush();
asn1OutputStream.writeObject(new ASN1Sequence(vector));
asn1OutputStream.flush();

return baos.toByteArray();
}
Expand Down

0 comments on commit 2baf51b

Please sign in to comment.