Disambiguated signature initialization

This commit is contained in:
Charles Gould
2017-09-05 12:51:27 -04:00
committed by Jeroen van Erp
parent 9ac55de26c
commit bdbd9d7eb5
7 changed files with 67 additions and 7 deletions

View File

@@ -62,6 +62,24 @@ public class SignatureEdDSA implements Signature {
}
}
@Override
public void initVerify(PublicKey pubkey) {
try {
engine.initVerify(pubkey);
} catch (InvalidKeyException e) {
throw new SSHRuntimeException(e);
}
}
@Override
public void initSign(PrivateKey prvkey) {
try {
engine.initSign(prvkey);
} catch (InvalidKeyException e) {
throw new SSHRuntimeException(e);
}
}
@Override
public void update(byte[] H) {
update(H, 0, H.length);

View File

@@ -47,6 +47,26 @@ public abstract class AbstractSignature
}
}
@Override
public void initVerify(PublicKey publicKey) {
try {
signature = SecurityUtils.getSignature(algorithm);
signature.initVerify(publicKey);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException(e);
}
}
@Override
public void initSign(PrivateKey privateKey) {
try {
signature = SecurityUtils.getSignature(algorithm);
signature.initSign(privateKey);
} catch (GeneralSecurityException e) {
throw new SSHRuntimeException(e);
}
}
@Override
public void update(byte[] foo) {
update(foo, 0, foo.length);

View File

@@ -27,9 +27,31 @@ public interface Signature {
*
* @param pubkey (null-ok) specify in case verification is needed
* @param prvkey (null-ok) specify in case signing is needed
* @deprecated Use {@link #initVerify(PublicKey)} or {@link #initSign(PrivateKey)} instead.
*/
@Deprecated
void init(PublicKey pubkey, PrivateKey prvkey);
/**
* Initialize this signature with the given public key for signature verification.
*
* Note that subsequent calls to either {@link #initVerify(PublicKey)} or {@link #initSign(PrivateKey)} will
* overwrite prior initialization.
*
* @param pubkey the public key to use for signature verification
*/
void initVerify(PublicKey pubkey);
/**
* Initialize this signature with the given private key for signing.
*
* Note that subsequent calls to either {@link #initVerify(PublicKey)} or {@link #initSign(PrivateKey)} will
* overwrite prior initialization.
*
* @param prvkey the private key to use for signing
*/
void initSign(PrivateKey prvkey);
/**
* Convenience method, same as calling {@link #update(byte[], int, int)} with offset as {@code 0} and {@code
* H.length}.

View File

@@ -80,7 +80,7 @@ public abstract class AbstractDHG extends AbstractDH
Signature signature = Factory.Named.Util.create(trans.getConfig().getSignatureFactories(),
KeyType.fromKey(hostKey).toString());
signature.init(hostKey, null);
signature.initVerify(hostKey);
signature.update(H, 0, H.length);
if (!signature.verify(sig))
throw new TransportException(DisconnectReason.KEY_EXCHANGE_FAILED,

View File

@@ -86,7 +86,7 @@ public abstract class AbstractDHGex extends AbstractDH {
H = digest.digest();
Signature signature = Factory.Named.Util.create(trans.getConfig().getSignatureFactories(),
KeyType.fromKey(hostKey).toString());
signature.init(hostKey, null);
signature.initVerify(hostKey);
signature.update(H, 0, H.length);
if (!signature.verify(sig))
throw new TransportException(DisconnectReason.KEY_EXCHANGE_FAILED,

View File

@@ -66,7 +66,7 @@ public abstract class KeyedAuthMethod
if (signature == null)
throw new UserAuthException("Could not create signature instance for " + kt + " key");
signature.init(null, key);
signature.initSign(key);
signature.update(new Buffer.PlainBuffer()
.putString(params.getTransport().getSessionID())
.putBuffer(reqBuf) // & rest of the data for sig

View File

@@ -34,7 +34,7 @@ public class VerificationTest {
PublicKey hostKey = new Buffer.PlainBuffer(K_S).readPublicKey();
Signature signature = new SignatureECDSA.Factory256().create();
signature.init(hostKey, null);
signature.initVerify(hostKey);
signature.update(H, 0, H.length);
Assert.assertTrue("ECDSA256 signature verifies", signature.verify(sig));
@@ -49,7 +49,7 @@ public class VerificationTest {
PublicKey hostKey = new Buffer.PlainBuffer(K_S).readPublicKey();
Signature signature = new SignatureECDSA.Factory384().create();
signature.init(hostKey, null);
signature.initVerify(hostKey);
signature.update(H, 0, H.length);
Assert.assertTrue("ECDSA384 signature verifies", signature.verify(sig));
@@ -64,7 +64,7 @@ public class VerificationTest {
PublicKey hostKey = new Buffer.PlainBuffer(K_S).readPublicKey();
Signature signature = new SignatureECDSA.Factory521().create();
signature.init(hostKey, null);
signature.initVerify(hostKey);
signature.update(H, 0, H.length);
Assert.assertTrue("ECDSA521 signature verifies", signature.verify(sig));