mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-07 15:50:57 +03:00
Extracted common key file methods into an abstract base class
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
package net.schmizz.sshj.userauth.keyprovider;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
import net.schmizz.sshj.common.KeyType;
|
||||||
|
import net.schmizz.sshj.userauth.password.*;
|
||||||
|
|
||||||
|
abstract class BaseFileKeyProvider implements FileKeyProvider {
|
||||||
|
protected Resource<?> resource;
|
||||||
|
protected PasswordFinder pwdf;
|
||||||
|
protected KeyPair kp;
|
||||||
|
|
||||||
|
protected KeyType type;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Reader location) {
|
||||||
|
assert location != null;
|
||||||
|
resource = new PrivateKeyReaderResource(location);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Reader location, PasswordFinder pwdf) {
|
||||||
|
init(location);
|
||||||
|
this.pwdf = pwdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(File location) {
|
||||||
|
assert location != null;
|
||||||
|
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(File location, PasswordFinder pwdf) {
|
||||||
|
init(location);
|
||||||
|
this.pwdf = pwdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(String privateKey, String publicKey) {
|
||||||
|
assert privateKey != null;
|
||||||
|
assert publicKey == null;
|
||||||
|
resource = new PrivateKeyStringResource(privateKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(String privateKey, String publicKey, PasswordFinder pwdf) {
|
||||||
|
init(privateKey, publicKey);
|
||||||
|
this.pwdf = pwdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PrivateKey getPrivate()
|
||||||
|
throws IOException {
|
||||||
|
return kp != null ? kp.getPrivate() : (kp = readKeyPair()).getPrivate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PublicKey getPublic()
|
||||||
|
throws IOException {
|
||||||
|
return kp != null ? kp.getPublic() : (kp = readKeyPair()).getPublic();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KeyType getType()
|
||||||
|
throws IOException {
|
||||||
|
return type != null ? type : (type = KeyType.fromKey(getPublic()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected abstract KeyPair readKeyPair() throws IOException;
|
||||||
|
}
|
||||||
@@ -15,28 +15,28 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.userauth.keyprovider;
|
package net.schmizz.sshj.userauth.keyprovider;
|
||||||
|
|
||||||
import net.schmizz.sshj.common.Base64;
|
import java.io.BufferedReader;
|
||||||
import net.schmizz.sshj.common.IOUtils;
|
import java.io.EOFException;
|
||||||
import net.schmizz.sshj.common.KeyType;
|
import java.io.IOException;
|
||||||
import net.schmizz.sshj.transport.cipher.*;
|
|
||||||
import net.schmizz.sshj.transport.digest.Digest;
|
|
||||||
import net.schmizz.sshj.transport.digest.MD5;
|
|
||||||
import net.schmizz.sshj.userauth.password.*;
|
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
|
||||||
import java.io.*;
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.security.spec.*;
|
import java.security.spec.*;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import net.schmizz.sshj.common.Base64;
|
||||||
|
import net.schmizz.sshj.common.IOUtils;
|
||||||
|
import net.schmizz.sshj.common.KeyType;
|
||||||
|
import net.schmizz.sshj.transport.cipher.*;
|
||||||
|
import net.schmizz.sshj.transport.digest.Digest;
|
||||||
|
import net.schmizz.sshj.transport.digest.MD5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a PKCS5-encoded key file. This is the format typically used by OpenSSH, OpenSSL, Amazon, etc.
|
* Represents a PKCS5-encoded key file. This is the format typically used by OpenSSH, OpenSSL, Amazon, etc.
|
||||||
*/
|
*/
|
||||||
public class PKCS5KeyFile
|
public class PKCS5KeyFile extends BaseFileKeyProvider {
|
||||||
implements FileKeyProvider {
|
|
||||||
|
|
||||||
public static class Factory
|
public static class Factory
|
||||||
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
||||||
@@ -74,67 +74,8 @@ public class PKCS5KeyFile
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected PasswordFinder pwdf;
|
|
||||||
protected Resource<?> resource;
|
|
||||||
protected KeyPair kp;
|
|
||||||
protected KeyType type;
|
|
||||||
protected byte[] data;
|
protected byte[] data;
|
||||||
|
|
||||||
@Override
|
|
||||||
public PrivateKey getPrivate()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPrivate() : (kp = readKeyPair()).getPrivate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PublicKey getPublic()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPublic() : (kp = readKeyPair()).getPublic();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public KeyType getType()
|
|
||||||
throws IOException {
|
|
||||||
return type != null ? type : (type = KeyType.fromKey(getPublic()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Reader location) {
|
|
||||||
assert location != null;
|
|
||||||
resource = new PrivateKeyReaderResource(location);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Reader location, PasswordFinder pwdf) {
|
|
||||||
init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location) {
|
|
||||||
assert location != null;
|
|
||||||
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location, PasswordFinder pwdf) {
|
|
||||||
init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey) {
|
|
||||||
assert privateKey != null;
|
|
||||||
assert publicKey == null;
|
|
||||||
resource = new PrivateKeyStringResource(privateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey, PasswordFinder pwdf) {
|
|
||||||
init(privateKey, publicKey);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected KeyPair readKeyPair()
|
protected KeyPair readKeyPair()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.userauth.keyprovider;
|
package net.schmizz.sshj.userauth.keyprovider;
|
||||||
|
|
||||||
import net.schmizz.sshj.common.IOUtils;
|
import java.io.IOException;
|
||||||
import net.schmizz.sshj.common.KeyType;
|
import java.security.KeyPair;
|
||||||
import net.schmizz.sshj.userauth.password.*;
|
|
||||||
import org.bouncycastle.openssl.EncryptionException;
|
import org.bouncycastle.openssl.EncryptionException;
|
||||||
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
|
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
|
||||||
import org.bouncycastle.openssl.PEMKeyPair;
|
import org.bouncycastle.openssl.PEMKeyPair;
|
||||||
@@ -27,16 +26,11 @@ import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import net.schmizz.sshj.common.IOUtils;
|
||||||
import java.io.IOException;
|
import net.schmizz.sshj.userauth.password.PasswordUtils;
|
||||||
import java.io.Reader;
|
|
||||||
import java.security.KeyPair;
|
|
||||||
import java.security.PrivateKey;
|
|
||||||
import java.security.PublicKey;
|
|
||||||
|
|
||||||
/** Represents a PKCS8-encoded key file. This is the format used by OpenSSH and OpenSSL. */
|
/** Represents a PKCS8-encoded key file. This is the format used by (old-style) OpenSSH and OpenSSL. */
|
||||||
public class PKCS8KeyFile
|
public class PKCS8KeyFile extends BaseFileKeyProvider {
|
||||||
implements FileKeyProvider {
|
|
||||||
|
|
||||||
public static class Factory
|
public static class Factory
|
||||||
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
||||||
@@ -53,68 +47,9 @@ public class PKCS8KeyFile
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
protected final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
protected PasswordFinder pwdf;
|
|
||||||
protected Resource<?> resource;
|
|
||||||
protected KeyPair kp;
|
|
||||||
|
|
||||||
protected KeyType type;
|
|
||||||
|
|
||||||
protected char[] passphrase; // for blanking out
|
protected char[] passphrase; // for blanking out
|
||||||
|
|
||||||
@Override
|
|
||||||
public PrivateKey getPrivate()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPrivate() : (kp = readKeyPair()).getPrivate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PublicKey getPublic()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPublic() : (kp = readKeyPair()).getPublic();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public KeyType getType()
|
|
||||||
throws IOException {
|
|
||||||
return type != null ? type : (type = KeyType.fromKey(getPublic()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Reader location) {
|
|
||||||
assert location != null;
|
|
||||||
resource = new PrivateKeyReaderResource(location);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Reader location, PasswordFinder pwdf) {
|
|
||||||
init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location) {
|
|
||||||
assert location != null;
|
|
||||||
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location, PasswordFinder pwdf) {
|
|
||||||
init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey) {
|
|
||||||
assert privateKey != null;
|
|
||||||
assert publicKey == null;
|
|
||||||
resource = new PrivateKeyStringResource(privateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey, PasswordFinder pwdf) {
|
|
||||||
init(privateKey, publicKey);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected KeyPair readKeyPair()
|
protected KeyPair readKeyPair()
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @version $Id:$
|
* @version $Id:$
|
||||||
*/
|
*/
|
||||||
public class PuTTYKeyFile implements FileKeyProvider {
|
public class PuTTYKeyFile extends BaseFileKeyProvider {
|
||||||
|
|
||||||
public static class Factory
|
public static class Factory
|
||||||
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
implements net.schmizz.sshj.common.Factory.Named<FileKeyProvider> {
|
||||||
@@ -75,56 +75,6 @@ public class PuTTYKeyFile implements FileKeyProvider {
|
|||||||
private byte[] privateKey;
|
private byte[] privateKey;
|
||||||
private byte[] publicKey;
|
private byte[] publicKey;
|
||||||
|
|
||||||
private KeyPair kp;
|
|
||||||
|
|
||||||
protected PasswordFinder pwdf;
|
|
||||||
|
|
||||||
protected Resource<?> resource;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(Reader location) {
|
|
||||||
this.resource = new PrivateKeyReaderResource(location);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init(Reader location, PasswordFinder pwdf) {
|
|
||||||
this.init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location) {
|
|
||||||
resource = new PrivateKeyFileResource(location.getAbsoluteFile());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(File location, PasswordFinder pwdf) {
|
|
||||||
this.init(location);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey) {
|
|
||||||
resource = new PrivateKeyStringResource(privateKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(String privateKey, String publicKey, PasswordFinder pwdf) {
|
|
||||||
init(privateKey, publicKey);
|
|
||||||
this.pwdf = pwdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PrivateKey getPrivate()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPrivate() : (kp = this.readKeyPair()).getPrivate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PublicKey getPublic()
|
|
||||||
throws IOException {
|
|
||||||
return kp != null ? kp.getPublic() : (kp = this.readKeyPair()).getPublic();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key type. Either "ssh-rsa" for RSA key, or "ssh-dss" for DSA key.
|
* Key type. Either "ssh-rsa" for RSA key, or "ssh-dss" for DSA key.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user