This is an automated email from the ASF dual-hosted git repository. twolf pushed a commit to branch dev_3.0 in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit 37764a8d663b9b8ac588e9d1562c4b85ad72b2c9 Author: Thomas Wolf <tw...@apache.org> AuthorDate: Thu Apr 24 21:28:21 2025 +0200 Remove KeyUtils.cloneKeyPair() It was used only in a test, and that test didn't really need it. Besides, if someone wants to duplicate (deep clone) a KeyPair, it can be done by exporting the keys via Key.getEncoded() and then recreating a key via a PKCS8EncodedKeySpec or X509EncodedKeySpec. --- .../sshd/common/config/keys/KeyEntryResolver.java | 61 ------------- .../apache/sshd/common/config/keys/KeyUtils.java | 18 ---- .../config/keys/impl/DSSPublicKeyEntryDecoder.java | 30 ------- .../keys/impl/ECDSAPublicKeyEntryDecoder.java | 38 --------- .../keys/impl/OpenSSHCertificateDecoder.java | 19 ----- .../config/keys/impl/RSAPublicKeyDecoder.java | 35 -------- .../keys/impl/SkECDSAPublicKeyEntryDecoder.java | 16 ---- .../keys/impl/SkED25519PublicKeyEntryDecoder.java | 14 --- .../openssh/OpenSSHDSSPrivateKeyEntryDecoder.java | 30 ------- .../OpenSSHECDSAPrivateKeyEntryDecoder.java | 37 -------- .../openssh/OpenSSHRSAPrivateKeyDecoder.java | 34 -------- .../generic/GenericEd25519PublicKeyDecoder.java | 18 ---- ...enericOpenSSHEd25519PrivateKeyEntryDecoder.java | 18 ---- .../keys/BuiltinClientIdentitiesWatcherTest.java | 9 +- .../sshd/common/config/keys/KeyUtilsCloneTest.java | 99 ---------------------- 15 files changed, 6 insertions(+), 470 deletions(-) diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyEntryResolver.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyEntryResolver.java index 78e268fbd..87bb58431 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyEntryResolver.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyEntryResolver.java @@ -27,7 +27,6 @@ import java.math.BigInteger; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -57,66 +56,6 @@ public interface KeyEntryResolver<PUB extends PublicKey, PRV extends PrivateKey> return gen.generateKeyPair(); } - /** - * @param kp The {@link KeyPair} to be cloned - ignored if {@code null} - * @return A cloned pair (or {@code null} if no original pair) - * @throws GeneralSecurityException If failed to clone - e.g., provided key pair does not contain keys of the - * expected type - * @see #getPublicKeyType() - * @see #getPrivateKeyType() - */ - default KeyPair cloneKeyPair(KeyPair kp) throws GeneralSecurityException { - if (kp == null) { - return null; - } - - PUB pubCloned = null; - PublicKey pubOriginal = kp.getPublic(); - Class<PUB> pubExpected = getPublicKeyType(); - if (pubOriginal != null) { - Class<?> orgType = pubOriginal.getClass(); - if (!pubExpected.isAssignableFrom(orgType)) { - throw new InvalidKeyException("Mismatched public key types: expected=" - + pubExpected.getSimpleName() - + ", actual=" + orgType.getSimpleName()); - } - - PUB castPub = pubExpected.cast(pubOriginal); - pubCloned = clonePublicKey(castPub); - } - - PRV prvCloned = null; - PrivateKey prvOriginal = kp.getPrivate(); - Class<PRV> prvExpected = getPrivateKeyType(); - if (prvOriginal != null) { - Class<?> orgType = prvOriginal.getClass(); - if (!prvExpected.isAssignableFrom(orgType)) { - throw new InvalidKeyException("Mismatched private key types: expected=" - + prvExpected.getSimpleName() - + ", actual=" + orgType.getSimpleName()); - } - - PRV castPrv = prvExpected.cast(prvOriginal); - prvCloned = clonePrivateKey(castPrv); - } - - return new KeyPair(pubCloned, prvCloned); - } - - /** - * @param key The {@link PublicKey} to clone - ignored if {@code null} - * @return The cloned key (or {@code null} if no original key) - * @throws GeneralSecurityException If failed to clone the key - */ - PUB clonePublicKey(PUB key) throws GeneralSecurityException; - - /** - * @param key The {@link PrivateKey} to clone - ignored if {@code null} - * @return The cloned key (or {@code null} if no original key) - * @throws GeneralSecurityException If failed to clone the key - */ - PRV clonePrivateKey(PRV key) throws GeneralSecurityException; - /** * @return A {@link KeyPairGenerator} suitable for this decoder * @throws GeneralSecurityException If failed to create the generator diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java index 357635eeb..5de8d0b3c 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/KeyUtils.java @@ -361,24 +361,6 @@ public final class KeyUtils { return decoder.generateKeyPair(keySize); } - /** - * Performs a deep-clone of the original {@link KeyPair} - i.e., creates <U>new</U> public/private keys that are - * clones of the original one - * - * @param keyType The key type - {@code OpenSSH} name - e.g., {@code ssh-rsa, ssh-dss} - * @param kp The {@link KeyPair} to clone - ignored if {@code null} - * @return The cloned instance - * @throws GeneralSecurityException If failed to clone the pair - */ - public static KeyPair cloneKeyPair(String keyType, KeyPair kp) throws GeneralSecurityException { - PublicKeyEntryDecoder<?, ?> decoder = getPublicKeyEntryDecoder(keyType); - if (decoder == null) { - throw new InvalidKeySpecException("No decoder for key type=" + keyType); - } - - return decoder.cloneKeyPair(kp); - } - /** * Registers the specified decoder for all the types it {@link PublicKeyEntryDecoder<?, ?>#getSupportedKeyTypes() * supports} diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/DSSPublicKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/DSSPublicKeyEntryDecoder.java index cb1fd9ea0..b91b50722 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/DSSPublicKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/DSSPublicKeyEntryDecoder.java @@ -24,13 +24,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPairGenerator; import java.security.interfaces.DSAParams; import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAPublicKey; -import java.security.spec.DSAPrivateKeySpec; import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.util.Collections; @@ -84,34 +82,6 @@ public class DSSPublicKeyEntryDecoder extends AbstractPublicKeyEntryDecoder<DSAP return KeyPairProvider.SSH_DSS; } - @Override - public DSAPublicKey clonePublicKey(DSAPublicKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - DSAParams params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePublicKey(new DSAPublicKeySpec(key.getY(), params.getP(), params.getQ(), params.getG())); - } - - @Override - public DSAPrivateKey clonePrivateKey(DSAPrivateKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - DSAParams params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePrivateKey(new DSAPrivateKeySpec(key.getX(), params.getP(), params.getQ(), params.getG())); - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(KeyUtils.DSS_ALGORITHM); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/ECDSAPublicKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/ECDSAPublicKeyEntryDecoder.java index 621b79bd6..b7b12dd99 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/ECDSAPublicKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/ECDSAPublicKeyEntryDecoder.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -32,7 +31,6 @@ import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; import java.security.spec.ECParameterSpec; import java.security.spec.ECPoint; -import java.security.spec.ECPrivateKeySpec; import java.security.spec.ECPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.util.Map; @@ -107,42 +105,6 @@ public class ECDSAPublicKeyEntryDecoder extends AbstractPublicKeyEntryDecoder<EC return generatePublicKey(new ECPublicKeySpec(w, paramSpec)); } - @Override - public ECPublicKey clonePublicKey(ECPublicKey key) throws GeneralSecurityException { - if (!SecurityUtils.isECCSupported()) { - throw new NoSuchProviderException("ECC not supported"); - } - - if (key == null) { - return null; - } - - ECParameterSpec params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePublicKey(new ECPublicKeySpec(key.getW(), params)); - } - - @Override - public ECPrivateKey clonePrivateKey(ECPrivateKey key) throws GeneralSecurityException { - if (!SecurityUtils.isECCSupported()) { - throw new NoSuchProviderException("ECC not supported"); - } - - if (key == null) { - return null; - } - - ECParameterSpec params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePrivateKey(new ECPrivateKeySpec(key.getS(), params)); - } - @Override public String encodePublicKey(OutputStream s, ECPublicKey key) throws IOException { Objects.requireNonNull(key, "No public key provided"); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/OpenSSHCertificateDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/OpenSSHCertificateDecoder.java index 68822e7ce..8556d1537 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/OpenSSHCertificateDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/OpenSSHCertificateDecoder.java @@ -19,8 +19,6 @@ package org.apache.sshd.common.config.keys.impl; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -88,23 +86,6 @@ public class OpenSSHCertificateDecoder extends AbstractPublicKeyEntryDecoder<Ope return key.getKeyType(); } - @Override - public OpenSshCertificate clonePublicKey(OpenSshCertificate key) throws GeneralSecurityException { - try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) { - String keyType = encodePublicKey(outStream, key); - try (InputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) { - return decodePublicKey(null, keyType, inStream, null); - } - } catch (IOException e) { - throw new GeneralSecurityException("Unable to clone key ID=" + key.getId(), e); - } - } - - @Override - public PrivateKey clonePrivateKey(PrivateKey key) { - throw new UnsupportedOperationException("Private key operations are not supported for certificates."); - } - @Override public KeyFactory getKeyFactoryInstance() { throw new UnsupportedOperationException("Private key operations are not supported for certificates."); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/RSAPublicKeyDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/RSAPublicKeyDecoder.java index 1cbe32b9f..55ea87436 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/RSAPublicKeyDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/RSAPublicKeyDecoder.java @@ -24,14 +24,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPairGenerator; -import java.security.interfaces.RSAPrivateCrtKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; -import java.security.spec.RSAPrivateCrtKeySpec; import java.security.spec.RSAPublicKeySpec; import java.util.Arrays; import java.util.Collections; @@ -85,38 +82,6 @@ public class RSAPublicKeyDecoder extends AbstractPublicKeyEntryDecoder<RSAPublic return KeyPairProvider.SSH_RSA; } - @Override - public RSAPublicKey clonePublicKey(RSAPublicKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePublicKey(new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent())); - } - } - - @Override - public RSAPrivateKey clonePrivateKey(RSAPrivateKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - if (!(key instanceof RSAPrivateCrtKey)) { - throw new InvalidKeyException("Cannot clone a non-RSAPrivateCrtKey: " + key.getClass().getSimpleName()); - } - - RSAPrivateCrtKey rsaPrv = (RSAPrivateCrtKey) key; - return generatePrivateKey( - new RSAPrivateCrtKeySpec( - rsaPrv.getModulus(), - rsaPrv.getPublicExponent(), - rsaPrv.getPrivateExponent(), - rsaPrv.getPrimeP(), - rsaPrv.getPrimeQ(), - rsaPrv.getPrimeExponentP(), - rsaPrv.getPrimeExponentQ(), - rsaPrv.getCrtCoefficient())); - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(KeyUtils.RSA_ALGORITHM); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkECDSAPublicKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkECDSAPublicKeyEntryDecoder.java index 5974b8fbf..0ee5179e1 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkECDSAPublicKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkECDSAPublicKeyEntryDecoder.java @@ -67,17 +67,6 @@ public class SkECDSAPublicKeyEntryDecoder extends AbstractPublicKeyEntryDecoder< return new SkEcdsaPublicKey(appName, noTouchRequired, ecPublicKey); } - @Override - public SkEcdsaPublicKey clonePublicKey(SkEcdsaPublicKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - return new SkEcdsaPublicKey( - key.getAppName(), key.isNoTouchRequired(), - ECDSAPublicKeyEntryDecoder.INSTANCE.clonePublicKey(key.getDelegatePublicKey())); - } - @Override public String encodePublicKey(OutputStream s, SkEcdsaPublicKey key) throws IOException { Objects.requireNonNull(key, "No public key provided"); @@ -86,11 +75,6 @@ public class SkECDSAPublicKeyEntryDecoder extends AbstractPublicKeyEntryDecoder< return KEY_TYPE; } - @Override - public PrivateKey clonePrivateKey(PrivateKey key) { - throw new UnsupportedOperationException("Private key operations are not supported for security keys."); - } - @Override public KeyFactory getKeyFactoryInstance() { throw new UnsupportedOperationException("Private key operations are not supported for security keys."); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkED25519PublicKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkED25519PublicKeyEntryDecoder.java index 550e1b925..d2f882ee3 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkED25519PublicKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/impl/SkED25519PublicKeyEntryDecoder.java @@ -69,15 +69,6 @@ public class SkED25519PublicKeyEntryDecoder extends AbstractPublicKeyEntryDecode return new SkED25519PublicKey(appName, noTouchRequired, edDSAPublicKey); } - @Override - public SkED25519PublicKey clonePublicKey(SkED25519PublicKey key) { - if (key == null) { - return null; - } - - return new SkED25519PublicKey(key.getAppName(), key.isNoTouchRequired(), key.getDelegatePublicKey()); - } - @Override public String encodePublicKey(OutputStream s, SkED25519PublicKey key) throws IOException { Objects.requireNonNull(key, "No public key provided"); @@ -88,11 +79,6 @@ public class SkED25519PublicKeyEntryDecoder extends AbstractPublicKeyEntryDecode return KEY_TYPE; } - @Override - public PrivateKey clonePrivateKey(PrivateKey key) { - throw new UnsupportedOperationException("Private key operations are not supported for security keys."); - } - @Override public KeyFactory getKeyFactoryInstance() { throw new UnsupportedOperationException("Private key operations are not supported for security keys."); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHDSSPrivateKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHDSSPrivateKeyEntryDecoder.java index 32d4c3d58..a821846cb 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHDSSPrivateKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHDSSPrivateKeyEntryDecoder.java @@ -23,14 +23,12 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPairGenerator; import java.security.interfaces.DSAParams; import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAPublicKey; import java.security.spec.DSAPrivateKeySpec; -import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.util.Collections; import java.util.Objects; @@ -102,34 +100,6 @@ public class OpenSSHDSSPrivateKeyEntryDecoder extends AbstractPrivateKeyEntryDec return KeyUtils.recoverDSAPublicKey(privateKey); } - @Override - public DSAPublicKey clonePublicKey(DSAPublicKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - DSAParams params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePublicKey(new DSAPublicKeySpec(key.getY(), params.getP(), params.getQ(), params.getG())); - } - - @Override - public DSAPrivateKey clonePrivateKey(DSAPrivateKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - DSAParams params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePrivateKey(new DSAPrivateKeySpec(key.getX(), params.getP(), params.getQ(), params.getG())); - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(KeyUtils.DSS_ALGORITHM); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHECDSAPrivateKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHECDSAPrivateKeyEntryDecoder.java index 0a5669695..ab126807d 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHECDSAPrivateKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHECDSAPrivateKeyEntryDecoder.java @@ -32,7 +32,6 @@ import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; import java.security.spec.ECParameterSpec; import java.security.spec.ECPrivateKeySpec; -import java.security.spec.ECPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.util.Objects; @@ -110,42 +109,6 @@ public class OpenSSHECDSAPrivateKeyEntryDecoder extends AbstractPrivateKeyEntryD return super.recoverPublicKey(prvKey); } - @Override - public ECPublicKey clonePublicKey(ECPublicKey key) throws GeneralSecurityException { - if (!SecurityUtils.isECCSupported()) { - throw new NoSuchProviderException("ECC not supported"); - } - - if (key == null) { - return null; - } - - ECParameterSpec params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePublicKey(new ECPublicKeySpec(key.getW(), params)); - } - - @Override - public ECPrivateKey clonePrivateKey(ECPrivateKey key) throws GeneralSecurityException { - if (!SecurityUtils.isECCSupported()) { - throw new NoSuchProviderException("ECC not supported"); - } - - if (key == null) { - return null; - } - - ECParameterSpec params = key.getParams(); - if (params == null) { - throw new InvalidKeyException("Missing parameters in key"); - } - - return generatePrivateKey(new ECPrivateKeySpec(key.getS(), params)); - } - @Override public KeyFactory getKeyFactoryInstance() throws GeneralSecurityException { if (SecurityUtils.isECCSupported()) { diff --git a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHRSAPrivateKeyDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHRSAPrivateKeyDecoder.java index 34ff03e71..b149217df 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHRSAPrivateKeyDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/config/keys/loader/openssh/OpenSSHRSAPrivateKeyDecoder.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.security.GeneralSecurityException; -import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateCrtKey; @@ -31,7 +30,6 @@ import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPrivateCrtKeySpec; -import java.security.spec.RSAPublicKeySpec; import java.util.Collections; import java.util.Objects; @@ -110,38 +108,6 @@ public class OpenSSHRSAPrivateKeyDecoder extends AbstractPrivateKeyEntryDecoder< return KeyUtils.recoverRSAPublicKey(privateKey); } - @Override - public RSAPublicKey clonePublicKey(RSAPublicKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePublicKey(new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent())); - } - } - - @Override - public RSAPrivateKey clonePrivateKey(RSAPrivateKey key) throws GeneralSecurityException { - if (key == null) { - return null; - } - - if (!(key instanceof RSAPrivateCrtKey)) { - throw new InvalidKeyException("Cannot clone a non-RSAPrivateCrtKey: " + key.getClass().getSimpleName()); - } - - RSAPrivateCrtKey rsaPrv = (RSAPrivateCrtKey) key; - return generatePrivateKey( - new RSAPrivateCrtKeySpec( - rsaPrv.getModulus(), - rsaPrv.getPublicExponent(), - rsaPrv.getPrivateExponent(), - rsaPrv.getPrimeP(), - rsaPrv.getPrimeQ(), - rsaPrv.getPrimeExponentP(), - rsaPrv.getPrimeExponentQ(), - rsaPrv.getCrtCoefficient())); - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(KeyUtils.RSA_ALGORITHM); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericEd25519PublicKeyDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericEd25519PublicKeyDecoder.java index 99622264b..98455f65c 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericEd25519PublicKeyDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericEd25519PublicKeyDecoder.java @@ -50,24 +50,6 @@ public class GenericEd25519PublicKeyDecoder<PUB extends PublicKey, PRV extends P this.edDSASupport = edDSASupport; } - @Override - public PUB clonePublicKey(PUB key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePublicKey(edDSASupport.createPublicKeySpec(key)); - } - } - - @Override - public PRV clonePrivateKey(PRV key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePrivateKey(edDSASupport.createPrivateKeySpec(key)); - } - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(SecurityUtils.EDDSA); diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericOpenSSHEd25519PrivateKeyEntryDecoder.java b/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericOpenSSHEd25519PrivateKeyEntryDecoder.java index 078a6572b..6d48b67cb 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericOpenSSHEd25519PrivateKeyEntryDecoder.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/generic/GenericOpenSSHEd25519PrivateKeyEntryDecoder.java @@ -147,24 +147,6 @@ public class GenericOpenSSHEd25519PrivateKeyEntryDecoder<PUB extends PublicKey, return edDSASupport.recoverEDDSAPublicKey(prvKey); } - @Override - public PUB clonePublicKey(PUB key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePublicKey(edDSASupport.createPublicKeySpec(key)); - } - } - - @Override - public PRV clonePrivateKey(PRV key) throws GeneralSecurityException { - if (key == null) { - return null; - } else { - return generatePrivateKey(edDSASupport.createPrivateKeySpec(key)); - } - } - @Override public KeyPairGenerator getKeyPairGenerator() throws GeneralSecurityException { return SecurityUtils.getKeyPairGenerator(SecurityUtils.EDDSA); diff --git a/sshd-common/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java b/sshd-common/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java index 1cc0f1071..18b12b544 100644 --- a/sshd-common/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java +++ b/sshd-common/src/test/java/org/apache/sshd/client/config/keys/BuiltinClientIdentitiesWatcherTest.java @@ -64,11 +64,14 @@ class BuiltinClientIdentitiesWatcherTest extends JUnitTestSupport { super(); } + private KeyPair duplicate(KeyPair pair) { + return new KeyPair(pair.getPublic(), pair.getPrivate()); + } + @Test void multipleFilesWatch() throws Exception { KeyPair identity = CommonTestSupportUtils.getFirstKeyPair(createTestHostKeyProvider()); - String keyType - = ValidateUtils.checkNotNullAndNotEmpty(KeyUtils.getKeyType(identity), "Cannot determine identity key type"); + ValidateUtils.checkNotNullAndNotEmpty(KeyUtils.getKeyType(identity), "Cannot determine identity key type"); Path dir = assertHierarchyTargetFolderExists(getTempTargetRelativeFile(getClass().getSimpleName())); Map<BuiltinIdentities, Path> locationsMap = new EnumMap<>(BuiltinIdentities.class); @@ -77,7 +80,7 @@ class BuiltinClientIdentitiesWatcherTest extends JUnitTestSupport { Path idFile = dir.resolve(ClientIdentity.getIdentityFileName(id)); Files.deleteIfExists(idFile); assertNull(locationsMap.put(id, idFile), "Multiple file mappings for " + id); - assertNull(idsMap.put(id, KeyUtils.cloneKeyPair(keyType, identity)), "Multiple identity mappings for " + id); + assertNull(idsMap.put(id, duplicate(identity)), "Multiple identity mappings for " + id); } ClientIdentityLoader loader = new ClientIdentityLoader() { diff --git a/sshd-common/src/test/java/org/apache/sshd/common/config/keys/KeyUtilsCloneTest.java b/sshd-common/src/test/java/org/apache/sshd/common/config/keys/KeyUtilsCloneTest.java deleted file mode 100644 index 2ffc2843c..000000000 --- a/sshd-common/src/test/java/org/apache/sshd/common/config/keys/KeyUtilsCloneTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sshd.common.config.keys; - -import java.security.GeneralSecurityException; -import java.security.KeyPair; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import org.apache.sshd.common.cipher.ECCurves; -import org.apache.sshd.common.keyprovider.KeyPairProvider; -import org.apache.sshd.common.util.security.SecurityUtils; -import org.apache.sshd.util.test.JUnitTestSupport; -import org.junit.jupiter.api.MethodOrderer.MethodName; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -@TestMethodOrder(MethodName.class) // see https://github.com/junit-team/junit/wiki/Parameterized-tests -@Tag("NoIoTestCase") -class KeyUtilsCloneTest extends JUnitTestSupport { - - static List<Object[]> parameters() { - List<Object[]> list = new ArrayList<>(); - addTests(list, KeyPairProvider.SSH_DSS, DSS_SIZES); - addTests(list, KeyPairProvider.SSH_RSA, RSA_SIZES); - if (SecurityUtils.isECCSupported()) { - for (ECCurves curve : ECCurves.VALUES) { - if (!curve.isSupported()) { - continue; - } - addTests(list, curve.getKeyType(), Collections.singletonList(curve.getKeySize())); - } - } - if (SecurityUtils.isEDDSACurveSupported()) { - addTests(list, KeyPairProvider.SSH_ED25519, ED25519_SIZES); - } - return Collections.unmodifiableList(list); - } - - private static void addTests(List<Object[]> list, String keyType, Collection<Integer> sizes) { - for (Integer keySize : sizes) { - list.add(new Object[] { keyType, keySize }); - } - } - - @MethodSource("parameters") - @ParameterizedTest(name = "type={0}, size={1}") - public void keyPairCloning(String keyType, int keySize) throws GeneralSecurityException { - outputDebugMessage("generateKeyPair(%s)[%d]", keyType, keySize); - KeyPair original = KeyUtils.generateKeyPair(keyType, keySize); - - outputDebugMessage("cloneKeyPair(%s)[%d]", keyType, keySize); - KeyPair cloned = KeyUtils.cloneKeyPair(keyType, original); - - String prefix = keyType + "[" + keySize + "]"; - assertNotSame(original, cloned, prefix + ": Key pair not cloned"); - assertTrue(KeyUtils.compareKeyPairs(original, cloned), prefix + ": Cloned pair not equals"); - - PublicKey k1 = original.getPublic(); - PublicKey k2 = cloned.getPublic(); - assertNotSame(k1, k2, prefix + ": Public key not cloned"); - assertTrue(KeyUtils.compareKeys(k1, k2), prefix + ": Cloned public key not equals"); - - String f1 = KeyUtils.getFingerPrint(k1); - String f2 = KeyUtils.getFingerPrint(k2); - assertEquals(f1, f2, prefix + ": Mismatched fingerprints"); - - PrivateKey pk1 = original.getPrivate(); - PrivateKey pk2 = cloned.getPrivate(); - assertNotSame(pk1, pk2, prefix + ": Private key not cloned"); - assertTrue(KeyUtils.compareKeys(pk1, pk2), prefix + ": Cloned private key not equals"); - } -}