- Ident in spaces

This commit is contained in:
Iger
2017-06-25 11:40:19 +03:00
committed by Jeroen van Erp
parent 3310530d42
commit 6ad6242ed1
3 changed files with 79 additions and 80 deletions

View File

@@ -21,72 +21,72 @@ import com.hierynomus.sshj.secg.SecgUtils;
public class ECDSAVariationsAdapter { public class ECDSAVariationsAdapter {
private final static String BASE_ALGORITHM_NAME = "ecdsa-sha2-nistp"; private final static String BASE_ALGORITHM_NAME = "ecdsa-sha2-nistp";
private final static Logger log = LoggerFactory.getLogger(ECDSAVariationsAdapter.class); private final static Logger log = LoggerFactory.getLogger(ECDSAVariationsAdapter.class);
public final static Map<String, String> SUPPORTED_CURVES = new HashMap<String, String>(); public final static Map<String, String> SUPPORTED_CURVES = new HashMap<String, String>();
public final static Map<String, String> NIST_CURVES_NAMES = new HashMap<String, String>(); public final static Map<String, String> NIST_CURVES_NAMES = new HashMap<String, String>();
static { static {
NIST_CURVES_NAMES.put("256", "p-256"); NIST_CURVES_NAMES.put("256", "p-256");
NIST_CURVES_NAMES.put("384", "p-384"); NIST_CURVES_NAMES.put("384", "p-384");
NIST_CURVES_NAMES.put("521", "p-521"); NIST_CURVES_NAMES.put("521", "p-521");
SUPPORTED_CURVES.put("256", "nistp256"); SUPPORTED_CURVES.put("256", "nistp256");
SUPPORTED_CURVES.put("384", "nistp384"); SUPPORTED_CURVES.put("384", "nistp384");
SUPPORTED_CURVES.put("521", "nistp521"); SUPPORTED_CURVES.put("521", "nistp521");
} }
public static PublicKey readPubKeyFromBuffer(Buffer<?> buf, String variation) throws GeneralSecurityException { public static PublicKey readPubKeyFromBuffer(Buffer<?> buf, String variation) throws GeneralSecurityException {
String algorithm = BASE_ALGORITHM_NAME + variation; String algorithm = BASE_ALGORITHM_NAME + variation;
if (!SecurityUtils.isBouncyCastleRegistered()) { if (!SecurityUtils.isBouncyCastleRegistered()) {
throw new GeneralSecurityException("BouncyCastle is required to read a key of type " + algorithm); throw new GeneralSecurityException("BouncyCastle is required to read a key of type " + algorithm);
} }
try { try {
// final String algo = buf.readString(); it has been already read // final String algo = buf.readString(); it has been already read
final String curveName = buf.readString(); final String curveName = buf.readString();
final int keyLen = buf.readUInt32AsInt(); final int keyLen = buf.readUInt32AsInt();
final byte x04 = buf.readByte(); // it must be 0x04, but don't think final byte x04 = buf.readByte(); // it must be 0x04, but don't think
// we need that check // we need that check
final byte[] x = new byte[(keyLen - 1) / 2]; final byte[] x = new byte[(keyLen - 1) / 2];
final byte[] y = new byte[(keyLen - 1) / 2]; final byte[] y = new byte[(keyLen - 1) / 2];
buf.readRawBytes(x); buf.readRawBytes(x);
buf.readRawBytes(y); buf.readRawBytes(y);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(String.format("Key algo: %s, Key curve: %s, Key Len: %s, 0x04: %s\nx: %s\ny: %s", log.debug(String.format("Key algo: %s, Key curve: %s, Key Len: %s, 0x04: %s\nx: %s\ny: %s",
algorithm, curveName, keyLen, x04, Arrays.toString(x), Arrays.toString(y))); algorithm, curveName, keyLen, x04, Arrays.toString(x), Arrays.toString(y)));
} }
if (!SUPPORTED_CURVES.values().contains(curveName)) { if (!SUPPORTED_CURVES.values().contains(curveName)) {
throw new GeneralSecurityException(String.format("Unknown curve %s", curveName)); throw new GeneralSecurityException(String.format("Unknown curve %s", curveName));
} }
BigInteger bigX = new BigInteger(1, x); BigInteger bigX = new BigInteger(1, x);
BigInteger bigY = new BigInteger(1, y); BigInteger bigY = new BigInteger(1, y);
X9ECParameters ecParams = NISTNamedCurves.getByName(NIST_CURVES_NAMES.get(variation)); X9ECParameters ecParams = NISTNamedCurves.getByName(NIST_CURVES_NAMES.get(variation));
ECPoint pPublicPoint = ecParams.getCurve().createPoint(bigX, bigY); ECPoint pPublicPoint = ecParams.getCurve().createPoint(bigX, bigY);
ECParameterSpec spec = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECParameterSpec spec = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN());
ECPublicKeySpec publicSpec = new ECPublicKeySpec(pPublicPoint, spec); ECPublicKeySpec publicSpec = new ECPublicKeySpec(pPublicPoint, spec);
KeyFactory keyFactory = KeyFactory.getInstance("ECDSA"); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA");
return keyFactory.generatePublic(publicSpec); return keyFactory.generatePublic(publicSpec);
} catch (Exception ex) { } catch (Exception ex) {
throw new GeneralSecurityException(ex); throw new GeneralSecurityException(ex);
} }
} }
public static void writePubKeyContentsIntoBuffer(PublicKey pk, Buffer<?> buf) { public static void writePubKeyContentsIntoBuffer(PublicKey pk, Buffer<?> buf) {
final ECPublicKey ecdsa = (ECPublicKey) pk; final ECPublicKey ecdsa = (ECPublicKey) pk;
byte[] encoded = SecgUtils.getEncoded(ecdsa.getW(), ecdsa.getParams().getCurve()); byte[] encoded = SecgUtils.getEncoded(ecdsa.getW(), ecdsa.getParams().getCurve());
buf.putString(Integer.toString(fieldSizeFromKey(ecdsa))) buf.putString(Integer.toString(fieldSizeFromKey(ecdsa)))
.putBytes(encoded); .putBytes(encoded);
} }
public static int fieldSizeFromKey(ECPublicKey ecPublicKey) { public static int fieldSizeFromKey(ECPublicKey ecPublicKey) {
return ecPublicKey.getParams().getCurve().getField().getFieldSize(); return ecPublicKey.getParams().getCurve().getField().getFieldSize();
} }
} }

View File

@@ -30,12 +30,10 @@ import net.schmizz.sshj.common.KeyType;
import net.schmizz.sshj.common.SSHRuntimeException; import net.schmizz.sshj.common.SSHRuntimeException;
/** ECDSA {@link Signature} */ /** ECDSA {@link Signature} */
public class SignatureECDSA public class SignatureECDSA extends AbstractSignature {
extends AbstractSignature {
/** A named factory for ECDSA-256 signature */ /** A named factory for ECDSA-256 signature */
public static class Factory256 public static class Factory256 implements net.schmizz.sshj.common.Factory.Named<Signature> {
implements net.schmizz.sshj.common.Factory.Named<Signature> {
@Override @Override
public Signature create() { public Signature create() {
@@ -50,8 +48,7 @@ public class SignatureECDSA
} }
/** A named factory for ECDSA-384 signature */ /** A named factory for ECDSA-384 signature */
public static class Factory384 public static class Factory384 implements net.schmizz.sshj.common.Factory.Named<Signature> {
implements net.schmizz.sshj.common.Factory.Named<Signature> {
@Override @Override
public Signature create() { public Signature create() {
@@ -66,8 +63,7 @@ public class SignatureECDSA
} }
/** A named factory for ECDSA-521 signature */ /** A named factory for ECDSA-521 signature */
public static class Factory521 public static class Factory521 implements net.schmizz.sshj.common.Factory.Named<Signature> {
implements net.schmizz.sshj.common.Factory.Named<Signature> {
@Override @Override
public Signature create() { public Signature create() {
@@ -81,7 +77,7 @@ public class SignatureECDSA
} }
private String keyTypeName; private String keyTypeName;
public SignatureECDSA(String algorithm, String keyTypeName) { public SignatureECDSA(String algorithm, String keyTypeName) {
super(algorithm); super(algorithm);
@@ -135,7 +131,7 @@ public class SignatureECDSA
} catch (SignatureException e) { } catch (SignatureException e) {
throw new SSHRuntimeException(e); throw new SSHRuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
throw new SSHRuntimeException(e); throw new SSHRuntimeException(e);
} }
} }
@@ -143,7 +139,10 @@ public class SignatureECDSA
int rLen = r.length; int rLen = r.length;
int sLen = s.length; int sLen = s.length;
/* We can't have the high bit set, so add an extra zero at the beginning if so. */ /*
* We can't have the high bit set, so add an extra zero at the beginning
* if so.
*/
if ((r[0] & 0x80) != 0) { if ((r[0] & 0x80) != 0) {
rLen++; rLen++;
} }
@@ -154,16 +153,16 @@ public class SignatureECDSA
/* Calculate total output length */ /* Calculate total output length */
int length = 6 + rLen + sLen; int length = 6 + rLen + sLen;
ASN1EncodableVector vector = new ASN1EncodableVector(); ASN1EncodableVector vector = new ASN1EncodableVector();
vector.add(new ASN1Integer(r)); vector.add(new ASN1Integer(r));
vector.add(new ASN1Integer(s)); vector.add(new ASN1Integer(s));
ByteArrayOutputStream baos = new ByteArrayOutputStream(length); ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
ASN1OutputStream asnOS = new ASN1OutputStream(baos); ASN1OutputStream asnOS = new ASN1OutputStream(baos);
asnOS.writeObject(new DERSequence(vector)); asnOS.writeObject(new DERSequence(vector));
asnOS.flush(); asnOS.flush();
return baos.toByteArray(); return baos.toByteArray();
} }
} }