From 968d4284a05e2d26951c8540408fad566a8f05eb Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Wed, 19 Oct 2016 12:08:51 +0100 Subject: [PATCH] Extracted common key file methods into an abstract base class --- .../keyprovider/BaseFileKeyProvider.java | 77 +++++++++++++++++ .../userauth/keyprovider/PKCS5KeyFile.java | 83 +++---------------- .../userauth/keyprovider/PKCS8KeyFile.java | 77 ++--------------- .../userauth/keyprovider/PuTTYKeyFile.java | 52 +----------- 4 files changed, 96 insertions(+), 193 deletions(-) create mode 100644 src/main/java/net/schmizz/sshj/userauth/keyprovider/BaseFileKeyProvider.java diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/BaseFileKeyProvider.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/BaseFileKeyProvider.java new file mode 100644 index 00000000..d1244543 --- /dev/null +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/BaseFileKeyProvider.java @@ -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; +} diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java index ff14c3f0..66a28cd8 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java @@ -15,28 +15,28 @@ */ package net.schmizz.sshj.userauth.keyprovider; -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; -import net.schmizz.sshj.userauth.password.*; - -import javax.xml.bind.DatatypeConverter; -import java.io.*; +import java.io.BufferedReader; +import java.io.EOFException; +import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.security.*; import java.security.spec.*; 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. */ -public class PKCS5KeyFile - implements FileKeyProvider { +public class PKCS5KeyFile extends BaseFileKeyProvider { public static class Factory implements net.schmizz.sshj.common.Factory.Named { @@ -74,67 +74,8 @@ public class PKCS5KeyFile } } - protected PasswordFinder pwdf; - protected Resource resource; - protected KeyPair kp; - protected KeyType type; 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() throws IOException { diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java index b72e6ba7..26f50058 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java @@ -15,9 +15,8 @@ */ package net.schmizz.sshj.userauth.keyprovider; -import net.schmizz.sshj.common.IOUtils; -import net.schmizz.sshj.common.KeyType; -import net.schmizz.sshj.userauth.password.*; +import java.io.IOException; +import java.security.KeyPair; import org.bouncycastle.openssl.EncryptionException; import org.bouncycastle.openssl.PEMEncryptedKeyPair; import org.bouncycastle.openssl.PEMKeyPair; @@ -27,16 +26,11 @@ import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -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.IOUtils; +import net.schmizz.sshj.userauth.password.PasswordUtils; -/** Represents a PKCS8-encoded key file. This is the format used by OpenSSH and OpenSSL. */ -public class PKCS8KeyFile - implements FileKeyProvider { +/** Represents a PKCS8-encoded key file. This is the format used by (old-style) OpenSSH and OpenSSL. */ +public class PKCS8KeyFile extends BaseFileKeyProvider { public static class Factory implements net.schmizz.sshj.common.Factory.Named { @@ -53,68 +47,9 @@ public class PKCS8KeyFile } 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 - @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() throws IOException { diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PuTTYKeyFile.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PuTTYKeyFile.java index 80728e83..a991d15f 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PuTTYKeyFile.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PuTTYKeyFile.java @@ -56,7 +56,7 @@ import java.util.Map; * * @version $Id:$ */ -public class PuTTYKeyFile implements FileKeyProvider { +public class PuTTYKeyFile extends BaseFileKeyProvider { public static class Factory implements net.schmizz.sshj.common.Factory.Named { @@ -75,56 +75,6 @@ public class PuTTYKeyFile implements FileKeyProvider { private byte[] privateKey; 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. */