mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
Refactored KeyAlgorithms slightly
This commit is contained in:
@@ -15,12 +15,9 @@
|
||||
*/
|
||||
package com.hierynomus.sshj
|
||||
|
||||
import com.hierynomus.sshj.key.ECDSAKeyAlgorithm
|
||||
import com.hierynomus.sshj.key.EdDSAKeyAlgorithm
|
||||
import com.hierynomus.sshj.signature.SignatureEdDSA
|
||||
import com.hierynomus.sshj.key.KeyAlgorithms
|
||||
import net.schmizz.sshj.DefaultConfig
|
||||
import net.schmizz.sshj.SSHClient
|
||||
import net.schmizz.sshj.signature.SignatureECDSA
|
||||
import net.schmizz.sshj.transport.TransportException
|
||||
import net.schmizz.sshj.userauth.UserAuthException
|
||||
import spock.lang.Unroll
|
||||
@@ -42,7 +39,7 @@ class IntegrationSpec extends IntegrationBaseSpec {
|
||||
sshClient.isConnected()
|
||||
|
||||
where:
|
||||
signatureFactory << [new ECDSAKeyAlgorithm.Factory256(), new EdDSAKeyAlgorithm.Factory()]
|
||||
signatureFactory << [KeyAlgorithms.ECDSASHANistp256(), KeyAlgorithms.EdDSA25519()]
|
||||
fingerprint << ["d3:6a:a9:52:05:ab:b5:48:dd:73:60:18:0c:3a:f0:a3", "dc:68:38:ce:fc:6f:2c:d6:6d:6b:34:eb:5c:f0:41:6a"]
|
||||
signatureName = signatureFactory.getName()
|
||||
}
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
package com.hierynomus.sshj.signature
|
||||
|
||||
import com.hierynomus.sshj.IntegrationBaseSpec
|
||||
import com.hierynomus.sshj.key.RSAKeyAlgorithm
|
||||
import com.hierynomus.sshj.key.KeyAlgorithms
|
||||
import net.schmizz.sshj.DefaultConfig
|
||||
import net.schmizz.sshj.signature.SignatureRSA
|
||||
import spock.lang.Unroll
|
||||
|
||||
class SignatureSpec extends IntegrationBaseSpec {
|
||||
@@ -37,7 +36,7 @@ class SignatureSpec extends IntegrationBaseSpec {
|
||||
client.authenticated
|
||||
|
||||
where:
|
||||
sigFactory << [new RSAKeyAlgorithm.FactorySSHRSA(), new RSAKeyAlgorithm.FactoryRSASHA256(), new RSAKeyAlgorithm.FactoryRSASHA512()]
|
||||
sigFactory << [KeyAlgorithms.SSHRSA(), KeyAlgorithms.RSASHA256(), KeyAlgorithms.RSASHA512()]
|
||||
sig = sigFactory.name
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,12 +23,12 @@ import net.schmizz.sshj.signature.Signature;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.PublicKey;
|
||||
|
||||
public abstract class AbstractKeyAlgorithm implements KeyAlgorithm {
|
||||
public class BaseKeyAlgorithm implements KeyAlgorithm {
|
||||
private final String keyAlgorithm;
|
||||
private final Factory.Named<Signature> signature;
|
||||
private final KeyType keyFormat;
|
||||
|
||||
public AbstractKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
public BaseKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
this.keyAlgorithm = keyAlgorithm;
|
||||
this.signature = signature;
|
||||
this.keyFormat = keyFormat;
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (C)2009 - SSHJ Contributors
|
||||
*
|
||||
* Licensed 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 com.hierynomus.sshj.key;
|
||||
|
||||
import net.schmizz.sshj.common.Factory;
|
||||
import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.signature.Signature;
|
||||
import net.schmizz.sshj.signature.SignatureDSA;
|
||||
|
||||
public class DSAKeyAlgorithm extends AbstractKeyAlgorithm {
|
||||
|
||||
/**
|
||||
* A named factory for the SSH-DSA key algorithm.
|
||||
*/
|
||||
public static class FactorySSHDSA
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new DSAKeyAlgorithm(KeyType.DSA.toString(), new SignatureDSA.Factory(), KeyType.DSA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.DSA.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A named factory for the SSH-DSS-CERT key algorithm
|
||||
*/
|
||||
public static class FactorySSHDSSCert
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new DSAKeyAlgorithm(KeyType.DSA_CERT.toString(), new SignatureDSA.Factory(), KeyType.DSA_CERT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.DSA_CERT.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public DSAKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
super(keyAlgorithm, signature, KeyType.DSA);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C)2009 - SSHJ Contributors
|
||||
*
|
||||
* Licensed 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 com.hierynomus.sshj.key;
|
||||
|
||||
import net.schmizz.sshj.common.Factory;
|
||||
import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.signature.Signature;
|
||||
import net.schmizz.sshj.signature.SignatureECDSA;
|
||||
|
||||
public class ECDSAKeyAlgorithm extends AbstractKeyAlgorithm {
|
||||
/** A named factory for ECDSA-256 signature */
|
||||
public static class Factory256 implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new ECDSAKeyAlgorithm(KeyType.ECDSA256.toString(), new SignatureECDSA.Factory256(), KeyType.ECDSA256);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.ECDSA256.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** A named factory for ECDSA-384 signature */
|
||||
public static class Factory384 implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new ECDSAKeyAlgorithm(KeyType.ECDSA384.toString(), new SignatureECDSA.Factory384(), KeyType.ECDSA384);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.ECDSA384.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** A named factory for ECDSA-521 signature */
|
||||
public static class Factory521 implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new ECDSAKeyAlgorithm(KeyType.ECDSA521.toString(), new SignatureECDSA.Factory384(), KeyType.ECDSA521);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.ECDSA521.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ECDSAKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
super(keyAlgorithm, signature, keyFormat);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (C)2009 - SSHJ Contributors
|
||||
*
|
||||
* Licensed 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 com.hierynomus.sshj.key;
|
||||
|
||||
import com.hierynomus.sshj.signature.SignatureEdDSA;
|
||||
import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.signature.Signature;
|
||||
|
||||
public class EdDSAKeyAlgorithm extends AbstractKeyAlgorithm {
|
||||
public static class Factory implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return KeyType.ED25519.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new EdDSAKeyAlgorithm(KeyType.ED25519.toString(), new SignatureEdDSA.Factory(), KeyType.ED25519);
|
||||
}
|
||||
}
|
||||
|
||||
public EdDSAKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
super(keyAlgorithm, signature, keyFormat);
|
||||
}
|
||||
}
|
||||
60
src/main/java/com/hierynomus/sshj/key/KeyAlgorithms.java
Normal file
60
src/main/java/com/hierynomus/sshj/key/KeyAlgorithms.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C)2009 - SSHJ Contributors
|
||||
*
|
||||
* Licensed 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 com.hierynomus.sshj.key;
|
||||
|
||||
import com.hierynomus.sshj.signature.SignatureEdDSA;
|
||||
import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.signature.Signature;
|
||||
import net.schmizz.sshj.signature.SignatureDSA;
|
||||
import net.schmizz.sshj.signature.SignatureECDSA;
|
||||
import net.schmizz.sshj.signature.SignatureRSA;
|
||||
|
||||
public class KeyAlgorithms {
|
||||
|
||||
public static Factory SSHRSA() { return new Factory("ssh-rsa", new SignatureRSA.FactorySSHRSA(), KeyType.RSA); }
|
||||
public static Factory SSHRSACertV01() { return new Factory("ssh-rsa-cert-v01@openssh.com", new SignatureRSA.FactoryCERT(), KeyType.RSA_CERT); }
|
||||
public static Factory RSASHA256() { return new Factory("rsa-sha2-256", new SignatureRSA.FactoryRSASHA256(), KeyType.RSA); }
|
||||
public static Factory RSASHA512() { return new Factory("rsa-sha2-512", new SignatureRSA.FactoryRSASHA512(), KeyType.RSA); }
|
||||
public static Factory SSHDSA() { return new Factory(KeyType.DSA.toString(), new SignatureDSA.Factory(), KeyType.DSA); }
|
||||
public static Factory SSHDSSCertV01() { return new Factory(KeyType.DSA_CERT.toString(), new SignatureDSA.Factory(), KeyType.DSA_CERT); }
|
||||
public static Factory ECDSASHANistp256() { return new Factory(KeyType.ECDSA256.toString(), new SignatureECDSA.Factory256(), KeyType.ECDSA256); }
|
||||
public static Factory ECDSASHANistp384() { return new Factory(KeyType.ECDSA384.toString(), new SignatureECDSA.Factory384(), KeyType.ECDSA384); }
|
||||
public static Factory ECDSASHANistp521() { return new Factory(KeyType.ECDSA521.toString(), new SignatureECDSA.Factory521(), KeyType.ECDSA521); }
|
||||
public static Factory EdDSA25519() { return new Factory(KeyType.ED25519.toString(), new SignatureEdDSA.Factory(), KeyType.ED25519); }
|
||||
|
||||
public static class Factory implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
private final String algorithmName;
|
||||
private final Named<Signature> signatureFactory;
|
||||
private final KeyType keyType;
|
||||
|
||||
public Factory(String algorithmName, Named<Signature> signatureFactory, KeyType keyType) {
|
||||
this.algorithmName = algorithmName;
|
||||
this.signatureFactory = signatureFactory;
|
||||
this.keyType = keyType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return algorithmName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new BaseKeyAlgorithm(algorithmName, signatureFactory, keyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright (C)2009 - SSHJ Contributors
|
||||
*
|
||||
* Licensed 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 com.hierynomus.sshj.key;
|
||||
|
||||
import net.schmizz.sshj.common.Factory;
|
||||
import net.schmizz.sshj.common.KeyType;
|
||||
import net.schmizz.sshj.signature.Signature;
|
||||
import net.schmizz.sshj.signature.SignatureRSA;
|
||||
|
||||
public class RSAKeyAlgorithm extends AbstractKeyAlgorithm {
|
||||
|
||||
/**
|
||||
* A named factory for the SSH-RSA (SHA1) public key algorithm
|
||||
*/
|
||||
public static class FactorySSHRSA
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new RSAKeyAlgorithm("ssh-rsa", new SignatureRSA.FactorySSHRSA(), KeyType.RSA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ssh-rsa";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A named factory for the ssh-rsa-cert-v01@openssh.com (SHA1) public key algorithm
|
||||
*/
|
||||
public static class FactorySSHRSACert
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new RSAKeyAlgorithm("ssh-rsa-cert-v01@openssh.com", new SignatureRSA.FactoryCERT(), KeyType.RSA_CERT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ssh-rsa-cert-v01@openssh.com";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A named factory for the RSA-SHA2-256 public key algorithm
|
||||
*/
|
||||
public static class FactoryRSASHA256
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new RSAKeyAlgorithm("rsa-sha2-256", new SignatureRSA.FactoryRSASHA256(), KeyType.RSA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "rsa-sha2-256";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A named factory for the RSA-SHA2-512 public key algorithm
|
||||
*/
|
||||
public static class FactoryRSASHA512
|
||||
implements net.schmizz.sshj.common.Factory.Named<KeyAlgorithm> {
|
||||
|
||||
@Override
|
||||
public KeyAlgorithm create() {
|
||||
return new RSAKeyAlgorithm("rsa-sha2-512", new SignatureRSA.FactoryRSASHA512(), KeyType.RSA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "rsa-sha2-512";
|
||||
}
|
||||
}
|
||||
|
||||
public RSAKeyAlgorithm(String keyAlgorithm, Factory.Named<Signature> signature, KeyType keyFormat) {
|
||||
super(keyAlgorithm, signature, keyFormat);
|
||||
}
|
||||
}
|
||||
@@ -15,14 +15,10 @@
|
||||
*/
|
||||
package net.schmizz.sshj;
|
||||
|
||||
import com.hierynomus.sshj.key.DSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.EdDSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.RSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.signature.SignatureEdDSA;
|
||||
|
||||
import com.hierynomus.sshj.key.KeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.KeyAlgorithms;
|
||||
import net.schmizz.sshj.common.Factory;
|
||||
import net.schmizz.sshj.common.SecurityUtils;
|
||||
import net.schmizz.sshj.signature.SignatureDSA;
|
||||
import net.schmizz.sshj.signature.SignatureRSA;
|
||||
import net.schmizz.sshj.transport.random.JCERandom;
|
||||
import net.schmizz.sshj.transport.random.SingletonRandomFactory;
|
||||
|
||||
@@ -41,10 +37,10 @@ public class AndroidConfig
|
||||
|
||||
@Override
|
||||
protected void initKeyAlgorithms() {
|
||||
setKeyAlgorithms(Arrays.asList(
|
||||
new EdDSAKeyAlgorithm.Factory(),
|
||||
new RSAKeyAlgorithm.FactorySSHRSA(),
|
||||
new DSAKeyAlgorithm.FactorySSHDSA()
|
||||
setKeyAlgorithms(Arrays.<Factory.Named<KeyAlgorithm>>asList(
|
||||
KeyAlgorithms.EdDSA25519(),
|
||||
KeyAlgorithms.SSHRSA(),
|
||||
KeyAlgorithms.SSHDSA()
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package net.schmizz.sshj;
|
||||
|
||||
import com.hierynomus.sshj.key.DSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.ECDSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.EdDSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.RSAKeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.KeyAlgorithm;
|
||||
import com.hierynomus.sshj.key.KeyAlgorithms;
|
||||
import com.hierynomus.sshj.transport.cipher.BlockCiphers;
|
||||
import com.hierynomus.sshj.transport.cipher.StreamCiphers;
|
||||
import com.hierynomus.sshj.transport.kex.DHGroups;
|
||||
@@ -134,17 +132,17 @@ public class DefaultConfig
|
||||
}
|
||||
|
||||
protected void initKeyAlgorithms() {
|
||||
setKeyAlgorithms(Arrays.asList(
|
||||
new EdDSAKeyAlgorithm.Factory(),
|
||||
new ECDSAKeyAlgorithm.Factory521(),
|
||||
new ECDSAKeyAlgorithm.Factory384(),
|
||||
new ECDSAKeyAlgorithm.Factory256(),
|
||||
new RSAKeyAlgorithm.FactoryRSASHA512(),
|
||||
new RSAKeyAlgorithm.FactoryRSASHA256(),
|
||||
new RSAKeyAlgorithm.FactorySSHRSACert(),
|
||||
new DSAKeyAlgorithm.FactorySSHDSSCert(),
|
||||
new RSAKeyAlgorithm.FactorySSHRSA(),
|
||||
new DSAKeyAlgorithm.FactorySSHDSA()));
|
||||
setKeyAlgorithms(Arrays.<Factory.Named<KeyAlgorithm>>asList(
|
||||
KeyAlgorithms.EdDSA25519(),
|
||||
KeyAlgorithms.ECDSASHANistp521(),
|
||||
KeyAlgorithms.ECDSASHANistp384(),
|
||||
KeyAlgorithms.ECDSASHANistp256(),
|
||||
KeyAlgorithms.RSASHA512(),
|
||||
KeyAlgorithms.RSASHA256(),
|
||||
KeyAlgorithms.SSHRSACertV01(),
|
||||
KeyAlgorithms.SSHDSSCertV01(),
|
||||
KeyAlgorithms.SSHRSA(),
|
||||
KeyAlgorithms.SSHDSA()));
|
||||
}
|
||||
|
||||
protected void initRandomFactory(boolean bouncyCastleRegistered) {
|
||||
|
||||
Reference in New Issue
Block a user