mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
Remove duplicated code
Signed-off-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package net.schmizz.sshj.signature;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.hierynomus.asn1.ASN1OutputStream;
|
||||
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.IOUtils;
|
||||
|
||||
public abstract class AbstractSignatureDSA extends AbstractSignature {
|
||||
protected AbstractSignatureDSA(String algorithm, String signatureName) {
|
||||
super(algorithm, signatureName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ASN.1 Signature encoded using DER Sequence of integers
|
||||
*
|
||||
* @param r DSA Signature R
|
||||
* @param s DSA Signature S
|
||||
* @return ASN.1 Encoded Signature
|
||||
* @throws IOException Thrown when failing to write signature integers
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected byte[] encodeAsnSignature(final BigInteger r, final BigInteger s) throws IOException {
|
||||
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 asn1OutputStream = new ASN1OutputStream(new DEREncoder(), baos);
|
||||
try {
|
||||
asn1OutputStream.writeObject(new ASN1Sequence(vector));
|
||||
asn1OutputStream.flush();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(asn1OutputStream);
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,26 +15,19 @@
|
||||
*/
|
||||
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 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}
|
||||
*/
|
||||
public class SignatureDSA
|
||||
extends AbstractSignature {
|
||||
extends AbstractSignatureDSA {
|
||||
|
||||
/**
|
||||
* A named factory for DSA signature
|
||||
@@ -90,32 +83,14 @@ public class SignatureDSA
|
||||
public boolean verify(byte[] sig) {
|
||||
try {
|
||||
byte[] sigBlob = extractSig(sig, "ssh-dss");
|
||||
return signature.verify(asnEncode(sigBlob));
|
||||
BigInteger r = new BigInteger(1, Arrays.copyOfRange(sigBlob, 0, 20));
|
||||
BigInteger s = new BigInteger(1, Arrays.copyOfRange(sigBlob, 20, 40));
|
||||
|
||||
return signature.verify(encodeAsnSignature(r, s));
|
||||
} catch (SignatureException e) {
|
||||
throw new SSHRuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new SSHRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the signature as a DER sequence (ASN.1 format).
|
||||
*/
|
||||
private byte[] asnEncode(byte[] sigBlob) throws IOException {
|
||||
BigInteger r = new BigInteger(1, Arrays.copyOfRange(sigBlob, 0, 20));
|
||||
BigInteger s = new BigInteger(1, Arrays.copyOfRange(sigBlob, 20, 40));
|
||||
|
||||
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();
|
||||
com.hierynomus.asn1.ASN1OutputStream asn1OutputStream = new com.hierynomus.asn1.ASN1OutputStream(new DEREncoder(), baos);
|
||||
|
||||
asn1OutputStream.writeObject(new ASN1Sequence(vector));
|
||||
asn1OutputStream.flush();
|
||||
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@
|
||||
*/
|
||||
package net.schmizz.sshj.signature;
|
||||
|
||||
import com.hierynomus.asn1.ASN1InputStream;
|
||||
import com.hierynomus.asn1.encodingrules.der.DERDecoder;
|
||||
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;
|
||||
@@ -26,15 +25,12 @@ import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.common.SSHRuntimeException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
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 {
|
||||
public class SignatureECDSA extends AbstractSignatureDSA {
|
||||
|
||||
/** A named factory for ECDSA-256 signature */
|
||||
public static class Factory256 implements net.schmizz.sshj.common.Factory.Named<Signature> {
|
||||
@@ -91,7 +87,7 @@ public class SignatureECDSA extends AbstractSignature {
|
||||
@Override
|
||||
public byte[] encode(byte[] sig) {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(sig);
|
||||
com.hierynomus.asn1.ASN1InputStream asn1InputStream = new com.hierynomus.asn1.ASN1InputStream(new DERDecoder(), bais);
|
||||
ASN1InputStream asn1InputStream = new ASN1InputStream(new DERDecoder(), bais);
|
||||
try {
|
||||
ASN1Sequence sequence = asn1InputStream.readObject();
|
||||
ASN1Integer r = (ASN1Integer) sequence.get(0);
|
||||
@@ -110,35 +106,14 @@ public class SignatureECDSA extends AbstractSignature {
|
||||
public boolean verify(byte[] sig) {
|
||||
try {
|
||||
byte[] sigBlob = extractSig(sig, keyTypeName);
|
||||
return signature.verify(asnEncode(sigBlob));
|
||||
Buffer.PlainBuffer sigbuf = new Buffer.PlainBuffer(sigBlob);
|
||||
BigInteger r = sigbuf.readMPInt();
|
||||
BigInteger s = sigbuf.readMPInt();
|
||||
return signature.verify(encodeAsnSignature(r, s));
|
||||
} catch (SignatureException e) {
|
||||
throw new SSHRuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new SSHRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the signature as a DER sequence (ASN.1 format).
|
||||
*/
|
||||
private byte[] asnEncode(byte[] sigBlob) throws IOException {
|
||||
Buffer.PlainBuffer sigbuf = new Buffer.PlainBuffer(sigBlob);
|
||||
BigInteger r = sigbuf.readMPInt();
|
||||
BigInteger s = sigbuf.readMPInt();
|
||||
|
||||
|
||||
List<ASN1Object> vector = new ArrayList<ASN1Object>();
|
||||
vector.add(new ASN1Integer(r));
|
||||
vector.add(new ASN1Integer(s));
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
com.hierynomus.asn1.ASN1OutputStream asn1OutputStream = new com.hierynomus.asn1.ASN1OutputStream(new DEREncoder(), baos);
|
||||
try {
|
||||
asn1OutputStream.writeObject(new ASN1Sequence(vector));
|
||||
asn1OutputStream.flush();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(asn1OutputStream);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user