-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ECDSA & EdDSA type PuTTY keys.
- Loading branch information
1 parent
d77c858
commit 2fbfa19
Showing
27 changed files
with
493 additions
and
22 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
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
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
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,131 @@ | ||
package com.jcraft.jsch; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.images.builder.ImageFromDockerfile; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.net.URISyntaxException; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Testcontainers | ||
public class KeyPair2IT { | ||
|
||
// Python can be slow for DH group 18 | ||
private static final int timeout = 10000; | ||
|
||
@Container | ||
public GenericContainer<?> sshd = new GenericContainer<>( | ||
new ImageFromDockerfile().withFileFromClasspath("asyncsshd.py", "docker/asyncsshd.py") | ||
.withFileFromClasspath("ssh_host_ed448_key", "docker/ssh_host_ed448_key") | ||
.withFileFromClasspath("ssh_host_ed448_key.pub", "docker/ssh_host_ed448_key.pub") | ||
.withFileFromClasspath("ssh_host_rsa_key", "docker/ssh_host_rsa_key") | ||
.withFileFromClasspath("ssh_host_rsa_key.pub", "docker/ssh_host_rsa_key.pub") | ||
.withFileFromClasspath("ssh_host_ecdsa256_key", "docker/ssh_host_ecdsa256_key") | ||
.withFileFromClasspath("ssh_host_ecdsa256_key.pub", "docker/ssh_host_ecdsa256_key.pub") | ||
.withFileFromClasspath("ssh_host_ecdsa384_key", "docker/ssh_host_ecdsa384_key") | ||
.withFileFromClasspath("ssh_host_ecdsa384_key.pub", "docker/ssh_host_ecdsa384_key.pub") | ||
.withFileFromClasspath("ssh_host_ecdsa521_key", "docker/ssh_host_ecdsa521_key") | ||
.withFileFromClasspath("ssh_host_ecdsa521_key.pub", "docker/ssh_host_ecdsa521_key.pub") | ||
.withFileFromClasspath("ssh_host_ed25519_key", "docker/ssh_host_ed25519_key") | ||
.withFileFromClasspath("ssh_host_ed25519_key.pub", "docker/ssh_host_ed25519_key.pub") | ||
.withFileFromClasspath("ssh_host_dsa_key", "docker/ssh_host_dsa_key") | ||
.withFileFromClasspath("ssh_host_dsa_key.pub", "docker/ssh_host_dsa_key.pub") | ||
.withFileFromClasspath("authorized_keys", "docker/authorized_keys.KeyPairIT") | ||
.withFileFromClasspath("Dockerfile", "docker/Dockerfile.asyncssh")).withExposedPorts(22); | ||
|
||
@ParameterizedTest | ||
@MethodSource("com.jcraft.jsch.KeyPair2Test#keyArgs") | ||
void connectWithPublicKey(String path, String password, String keyType) throws Exception { | ||
|
||
final JSch jSch = createIdentity(path, password); | ||
|
||
Session session = createSession(jSch); | ||
|
||
if (keyType != null) { | ||
session.setConfig("PubkeyAcceptedAlgorithms", keyType); | ||
} | ||
try { | ||
session.connect(timeout); | ||
assertTrue(session.isConnected()); | ||
} finally { | ||
session.disconnect(); | ||
} | ||
|
||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("com.jcraft.jsch.KeyPair2Test#keyArgs") | ||
void connectWithPublicKeyAndUserInfo(String path, String password, String keyType) throws Exception { | ||
|
||
final JSch jSch = new JSch(); | ||
|
||
jSch.addIdentity(Paths.get(ClassLoader.getSystemResource(path).toURI()).toFile().getAbsolutePath()); | ||
|
||
Session session = createSession(jSch); | ||
session.setUserInfo(new UserInfo() { | ||
@Override | ||
public String getPassphrase() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean promptPassword(String message) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean promptPassphrase(String message) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean promptYesNo(String message) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void showMessage(String message) { | ||
|
||
} | ||
}); | ||
|
||
if (keyType != null) { | ||
session.setConfig("PubkeyAcceptedAlgorithms", keyType); | ||
} | ||
try { | ||
session.connect(timeout); | ||
assertTrue(session.isConnected()); | ||
} finally { | ||
session.disconnect(); | ||
} | ||
|
||
} | ||
|
||
private JSch createIdentity(String path, String password) throws JSchException, URISyntaxException { | ||
JSch ssh = new JSch(); | ||
if (password != null) { | ||
ssh.addIdentity(Paths.get(ClassLoader.getSystemResource(path).toURI()).toFile().getAbsolutePath(), | ||
password); | ||
} else { | ||
ssh.addIdentity(Paths.get(ClassLoader.getSystemResource(path).toURI()).toFile().getAbsolutePath()); | ||
} | ||
return ssh; | ||
} | ||
|
||
private Session createSession(JSch ssh) throws Exception { | ||
Session session = ssh.getSession("root", sshd.getHost(), sshd.getFirstMappedPort()); | ||
session.setConfig("StrictHostKeyChecking", "no"); | ||
session.setConfig("PreferredAuthentications", "publickey"); | ||
return session; | ||
} | ||
} |
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 @@ | ||
package com.jcraft.jsch; | ||
|
||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.File; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class KeyPair2Test { | ||
|
||
@TempDir | ||
public Path tmpDir; | ||
|
||
static Stream<Arguments> keyArgs() { | ||
return Stream.of( | ||
Arguments.of("ppkv2_ed448_unix.ppk", null, "ssh-ed448"), | ||
Arguments.of("ppkv2_ed448_unix_encrypted.ppk", "secret123", "ssh-ed448"), | ||
Arguments.of("ppkv2_ed448_windows.ppk", null, "ssh-ed448"), | ||
Arguments.of("ppkv2_ed448_windows_encrypted.ppk", "secret123", "ssh-ed448") | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("keyArgs") | ||
void loadKey(String path, String password, String keyType) throws URISyntaxException, JSchException { | ||
final JSch jSch = new JSch(); | ||
final String prvkey = Paths.get(ClassLoader.getSystemResource(path).toURI()).toFile().getAbsolutePath(); | ||
assertTrue(new File(prvkey).exists()); | ||
assertDoesNotThrow(() -> { | ||
if (null != password) { | ||
jSch.addIdentity(prvkey, password); | ||
} else { | ||
jSch.addIdentity(prvkey); | ||
} | ||
}); | ||
assertEquals(keyType, jSch.getIdentityRepository().getIdentities().get(0).getAlgName()); | ||
} | ||
} |
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
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
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,10 @@ | ||
PuTTY-User-Key-File-2: ecdsa-sha2-nistp256 | ||
Encryption: none | ||
Comment: test | ||
Public-Lines: 3 | ||
AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCo/5qxk4V6Z | ||
ZGDFHDeVBr7ayBkc85+7SPuuY2OnBSrObqo06lw3nlH8dcloRzCt7qYvbmWTutlT | ||
ujgSOJuvd3A= | ||
Private-Lines: 1 | ||
AAAAIEfIpXqum0vHuX3aJmumOiRTGEcPs8cB6B5cmKV7j8Pq | ||
Private-MAC: c2c907c13abcd1e429a8a53c162fb27fa32bf88d |
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,10 @@ | ||
PuTTY-User-Key-File-2: ecdsa-sha2-nistp256 | ||
Encryption: aes256-cbc | ||
Comment: test | ||
Public-Lines: 3 | ||
AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAJ3Mot6VLEk | ||
kG5HscBLY3geI20FnwrgQi2wzAKiktPDhF39ZavVlOMzvkIGgQDxmo8WoQllY52e | ||
d4e8+7KQ76M= | ||
Private-Lines: 1 | ||
Giq4fU1ffVMQzpRS7vPtl4eViHxNmagS8TmYqnvb4KsMip0XwtRkRd1tqV9UGS2F | ||
Private-MAC: 9a55718d397e4bbbc0c00003bdcf5b8648da7f2c |
Oops, something went wrong.