import java.io.Console;
import java.io.IOException;
import java.net.URI;
-import java.security.GeneralSecurityException;
-import java.security.KeyPair;
import java.util.Arrays;
import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.sshd.client.SshClient;
-import org.apache.sshd.client.config.keys.ClientIdentityLoader;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
-import org.apache.sshd.common.config.keys.FilePasswordProvider;
abstract class AbstractSsh {
private final static Log log = LogFactory.getLog(AbstractSsh.class);
return sftpFileSystemProvider;
}
+ @SuppressWarnings("restriction")
void authenticate() {
try {
if (sshKeyPair != null) {
Console console = System.console();
if (console == null) {// IDE
System.out.print("Password: ");
- Scanner s = new Scanner(System.in);
- password = s.next();
+ try (Scanner s = new Scanner(System.in)) {
+ password = s.next();
+ }
} else {
console.printf("Password: ");
char[] pwd = console.readPassword();
openSession(uri.getUserInfo(), uri.getHost(), uri.getPort() > 0 ? uri.getPort() : null);
}
+ @SuppressWarnings("restriction")
void openSession(String login, String host, Integer port) {
if (session != null)
throw new IllegalStateException("Session is already open");
}
}
+ @SuppressWarnings("restriction")
void closeSession() {
if (session == null)
throw new IllegalStateException("No session is open");
package org.argeo.ssh;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.KeyPair;
@SuppressWarnings("restriction")
public class SshKeyPair {
+ public final static String RSA_KEY_TYPE = "ssh-rsa";
+
private PublicKey publicKey;
private PrivateKey privateKey;
private KeyPair keyPair;
return PublicKeyEntry.toString(publicKey);
}
- public String getPrivateKeyAsString(char[] password) {
+ public String getPrivateKeyAsPemString(char[] password) {
try {
Object obj;
}
}
+ public static SshKeyPair loadOrGenerate(Path privateKeyPath, int size, char[] password) {
+ try {
+ SshKeyPair sshKeyPair;
+ if (Files.exists(privateKeyPath)) {
+// String privateKeyStr = new String(Files.readAllBytes(privateKeyPath), StandardCharsets.US_ASCII);
+ sshKeyPair = load(
+ new InputStreamReader(Files.newInputStream(privateKeyPath), StandardCharsets.US_ASCII),
+ password);
+ // TOD make sure public key is consistemt
+ } else {
+ sshKeyPair = generate(size);
+ Files.write(privateKeyPath,
+ sshKeyPair.getPrivateKeyAsPemString(password).getBytes(StandardCharsets.US_ASCII));
+ Path publicKeyPath = privateKeyPath.resolveSibling(privateKeyPath.getFileName() + ".pub");
+ Files.write(publicKeyPath,
+ sshKeyPair.getPublicKeyAsOpenSshString().getBytes(StandardCharsets.US_ASCII));
+ }
+ return sshKeyPair;
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot read or write private key " + privateKeyPath, e);
+ }
+ }
+
public static SshKeyPair generate(int size) {
+ return generate(RSA_KEY_TYPE, size);
+ }
+
+ public static SshKeyPair generate(String keyType, int size) {
try {
- KeyPair keyPair = KeyUtils.generateKeyPair("ssh-rsa", size);
+ KeyPair keyPair = KeyUtils.generateKeyPair(keyType, size);
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
return new SshKeyPair(publicKey, privateKey);
public static SshKeyPair load(Reader reader, char[] password) {
try (PEMParser pemParser = new PEMParser(reader)) {
Object object = pemParser.readObject();
- JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter();// .setProvider("BC");
KeyPair kp;
if (object instanceof PKCS8EncryptedPrivateKeyInfo) {
// Encrypted key - we will use provided password
}
public static void main(String args[]) {
- SshKeyPair okp = SshKeyPair.generate(1024);
- System.out.println("Public:\n" + okp.getPublicKeyAsOpenSshString());
- System.out.println("Private (plain):\n" + okp.getPrivateKeyAsString(null));
- System.out.println("Private (encrypted):\n" + okp.getPrivateKeyAsString("demo".toCharArray()));
-
- StringReader reader = new StringReader(okp.getPrivateKeyAsString(null));
- okp = SshKeyPair.load(reader, null);
- System.out.println("Public:\n" + okp.getPublicKeyAsOpenSshString());
- System.out.println("Private (plain):\n" + okp.getPrivateKeyAsString(null));
- System.out.println("Private (encrypted):\n" + okp.getPrivateKeyAsString("demo".toCharArray()));
-
- reader = new StringReader(okp.getPrivateKeyAsString("demo".toCharArray()));
- okp = SshKeyPair.load(reader, "demo".toCharArray());
- System.out.println("Public:\n" + okp.getPublicKeyAsOpenSshString());
- System.out.println("Private (plain):\n" + okp.getPrivateKeyAsString(null));
- System.out.println("Private (encrypted):\n" + okp.getPrivateKeyAsString("demo".toCharArray()));
+ Path privateKeyPath = Paths.get(System.getProperty("user.dir") + "/id_rsa");
+ SshKeyPair skp = SshKeyPair.loadOrGenerate(privateKeyPath, 1024, null);
+ System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString());
+ System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null));
+ System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray()));
+
+ StringReader reader = new StringReader(skp.getPrivateKeyAsPemString(null));
+ skp = SshKeyPair.load(reader, null);
+ System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString());
+ System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null));
+ System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray()));
+
+ reader = new StringReader(skp.getPrivateKeyAsPemString("demo".toCharArray()));
+ skp = SshKeyPair.load(reader, "demo".toCharArray());
+ System.out.println("Public:\n" + skp.getPublicKeyAsOpenSshString());
+ System.out.println("Private (plain):\n" + skp.getPrivateKeyAsPemString(null));
+ System.out.println("Private (encrypted):\n" + skp.getPrivateKeyAsPemString("demo".toCharArray()));
}
}
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.security.KeyPair;
-import java.security.PublicKey;
-import java.util.HashSet;
-import java.util.List;
import java.util.Map;
import java.util.Scanner;
-import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.sshd.agent.local.LocalAgentFactory;
import org.apache.sshd.agent.unix.UnixAgentFactory;
import org.apache.sshd.client.SshClient;
-import org.apache.sshd.client.channel.ClientChannel;
-import org.apache.sshd.client.channel.ClientChannelEvent;
-import org.apache.sshd.client.config.keys.ClientIdentityLoader;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem;
import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystemProvider;
-import org.apache.sshd.common.config.keys.FilePasswordProvider;
-import org.apache.sshd.common.util.io.NoCloseInputStream;
-import org.apache.sshd.common.util.io.NoCloseOutputStream;
public class SshSync {
private final static Log log = LogFactory.getLog(SshSync.class);