From 06e421e752a03c7871b44642800227387b168564 Mon Sep 17 00:00:00 2001 From: David Kocher Date: Tue, 6 May 2014 15:41:35 +0200 Subject: [PATCH] Extract formats. Add PuTTY to enum. --- .../userauth/keyprovider/FileKeyProvider.java | 6 -- .../sshj/userauth/keyprovider/KeyFormat.java | 11 ++++ .../userauth/keyprovider/KeyProviderUtil.java | 57 ++++++++++--------- 3 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyFormat.java diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/FileKeyProvider.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/FileKeyProvider.java index 3c120f49..8f4183c2 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/FileKeyProvider.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/FileKeyProvider.java @@ -23,12 +23,6 @@ import java.io.File; public interface FileKeyProvider extends KeyProvider { - enum Format { - PKCS8, - OpenSSH, - Unknown - } - void init(File location); void init(File location, PasswordFinder pwdf); diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyFormat.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyFormat.java new file mode 100644 index 00000000..2860b616 --- /dev/null +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyFormat.java @@ -0,0 +1,11 @@ +package net.schmizz.sshj.userauth.keyprovider; + +/** +* @version $Id:$ +*/ +public enum KeyFormat { + PKCS8, + OpenSSH, + PuTTY, + Unknown +} diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java index 6ec33527..5b0f26a6 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java @@ -15,8 +15,6 @@ */ package net.schmizz.sshj.userauth.keyprovider; -import net.schmizz.sshj.common.IOUtils; - import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -24,6 +22,8 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import net.schmizz.sshj.common.IOUtils; + public class KeyProviderUtil { /** @@ -32,15 +32,13 @@ public class KeyProviderUtil { * Return values are consistent with the {@code NamedFactory} implementations in the {@code keyprovider} package. * * @param location - * * @return name of the key file format - * * @throws java.io.IOException */ - public static FileKeyProvider.Format detectKeyFileFormat(File location) + public static KeyFormat detectKeyFileFormat(File location) throws IOException { return detectKeyFileFormat(new FileReader(location), - new File(location + ".pub").exists()); + new File(location + ".pub").exists()); } /** @@ -50,12 +48,10 @@ public class KeyProviderUtil { * * @param privateKey Private key stored in a string * @param separatePubKey Is the public key stored separately from the private key - * * @return name of the key file format - * * @throws java.io.IOException */ - public static FileKeyProvider.Format detectKeyFileFormat(String privateKey, + public static KeyFormat detectKeyFileFormat(String privateKey, boolean separatePubKey) throws IOException { return detectKeyFileFormat(new StringReader(privateKey), separatePubKey); @@ -68,29 +64,38 @@ public class KeyProviderUtil { * * @param privateKey Private key accessible through a {@code Reader} * @param separatePubKey Is the public key stored separately from the private key - * * @return name of the key file format - * * @throws java.io.IOException */ - private static FileKeyProvider.Format detectKeyFileFormat(Reader privateKey, - boolean separatePubKey) + public static KeyFormat detectKeyFileFormat(Reader privateKey, + boolean separatePubKey) throws IOException { BufferedReader br = new BufferedReader(privateKey); - String firstLine = br.readLine(); - IOUtils.closeQuietly(br); - if (firstLine == null) + final String firstLine; + try { + firstLine = br.readLine(); + } + finally { + IOUtils.closeQuietly(br); + } + if(firstLine == null) { throw new IOException("Empty file"); - if (firstLine.startsWith("-----BEGIN") && firstLine.endsWith("PRIVATE KEY-----")) - if (separatePubKey) - // Can delay asking for password since have unencrypted pubkey - return FileKeyProvider.Format.OpenSSH; + } + if(firstLine.startsWith("-----BEGIN") && firstLine.endsWith("PRIVATE KEY-----")) { + if(separatePubKey) + // Can delay asking for password since have unencrypted pubkey + { + return KeyFormat.OpenSSH; + } else - // More general - return FileKeyProvider.Format.PKCS8; - /* - * TODO: Tectia, PuTTY (.ppk) ... - */ - return FileKeyProvider.Format.Unknown; + // More general + { + return KeyFormat.PKCS8; + } + } + if(firstLine.startsWith("PuTTY-User-Key-File-")) { + return KeyFormat.PuTTY; + } + return KeyFormat.Unknown; } }