-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce KeyAlgorithm to separate KeyType from Algorithm (Fixes #588) (
#589)
- Loading branch information
1 parent
91105e6
commit 9671352
Showing
27 changed files
with
625 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/itest/groovy/com/hierynomus/sshj/signature/SignatureSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.signature | ||
|
||
import com.hierynomus.sshj.IntegrationBaseSpec | ||
import com.hierynomus.sshj.key.RSAKeyAlgorithm | ||
import net.schmizz.sshj.DefaultConfig | ||
import net.schmizz.sshj.signature.SignatureRSA | ||
import spock.lang.Unroll | ||
|
||
class SignatureSpec extends IntegrationBaseSpec { | ||
|
||
@Unroll | ||
def "should correctly connect with #sig Signature"() { | ||
given: | ||
def cfg = new DefaultConfig() | ||
cfg.setKeyAlgorithms(Collections.singletonList(sigFactory)) | ||
def client = getConnectedClient(cfg) | ||
|
||
when: | ||
client.authPublickey(USERNAME, KEYFILE) | ||
|
||
then: | ||
client.authenticated | ||
|
||
where: | ||
sigFactory << [new RSAKeyAlgorithm.FactorySSHRSA(), new RSAKeyAlgorithm.FactoryRSASHA256(), new RSAKeyAlgorithm.FactoryRSASHA512()] | ||
sig = sigFactory.name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/hierynomus/sshj/key/AbstractKeyAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 net.schmizz.sshj.common.Buffer; | ||
import net.schmizz.sshj.common.Factory; | ||
import net.schmizz.sshj.common.KeyType; | ||
import net.schmizz.sshj.signature.Signature; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.PublicKey; | ||
|
||
public abstract class AbstractKeyAlgorithm 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) { | ||
this.keyAlgorithm = keyAlgorithm; | ||
this.signature = signature; | ||
this.keyFormat = keyFormat; | ||
} | ||
|
||
public void putPubKeyIntoBuffer(PublicKey pk, Buffer<?> buf) { | ||
keyFormat.putPubKeyIntoBuffer(pk, buf); | ||
} | ||
|
||
@Override | ||
public PublicKey readPubKeyFromBuffer(Buffer<?> buf) throws GeneralSecurityException { | ||
return keyFormat.readPubKeyFromBuffer(buf); | ||
} | ||
|
||
@Override | ||
public String getKeyAlgorithm() { | ||
return keyAlgorithm; | ||
} | ||
|
||
@Override | ||
public KeyType getKeyFormat() { | ||
return keyFormat; | ||
} | ||
|
||
@Override | ||
public Signature newSignature() { | ||
return this.signature.create(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/hierynomus/sshj/key/DSAKeyAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/hierynomus/sshj/key/ECDSAKeyAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/hierynomus/sshj/key/EdDSAKeyAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.Buffer; | ||
import net.schmizz.sshj.common.KeyType; | ||
import net.schmizz.sshj.signature.Signature; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.PublicKey; | ||
|
||
/** | ||
* In [RFC4252], the concept "public key algorithm" is used to establish | ||
* a relationship between one algorithm name, and: | ||
* <p> | ||
* A. procedures used to generate and validate a private/public | ||
* keypair; | ||
* B. a format used to encode a public key; and | ||
* C. procedures used to calculate, encode, and verify a signature. | ||
*/ | ||
public interface KeyAlgorithm { | ||
|
||
PublicKey readPubKeyFromBuffer(Buffer<?> buf) throws GeneralSecurityException; | ||
|
||
void putPubKeyIntoBuffer(PublicKey pk, Buffer<?> buf); | ||
|
||
String getKeyAlgorithm(); | ||
|
||
KeyType getKeyFormat(); | ||
|
||
Signature newSignature(); | ||
} |
Oops, something went wrong.