Extract formats. Add PuTTY to enum.

This commit is contained in:
David Kocher
2014-05-06 15:41:35 +02:00
parent 466ff99e1c
commit 06e421e752
3 changed files with 42 additions and 32 deletions

View File

@@ -23,12 +23,6 @@ import java.io.File;
public interface FileKeyProvider public interface FileKeyProvider
extends KeyProvider { extends KeyProvider {
enum Format {
PKCS8,
OpenSSH,
Unknown
}
void init(File location); void init(File location);
void init(File location, PasswordFinder pwdf); void init(File location, PasswordFinder pwdf);

View File

@@ -0,0 +1,11 @@
package net.schmizz.sshj.userauth.keyprovider;
/**
* @version $Id:$
*/
public enum KeyFormat {
PKCS8,
OpenSSH,
PuTTY,
Unknown
}

View File

@@ -15,8 +15,6 @@
*/ */
package net.schmizz.sshj.userauth.keyprovider; package net.schmizz.sshj.userauth.keyprovider;
import net.schmizz.sshj.common.IOUtils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@@ -24,6 +22,8 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import net.schmizz.sshj.common.IOUtils;
public class KeyProviderUtil { public class KeyProviderUtil {
/** /**
@@ -32,12 +32,10 @@ public class KeyProviderUtil {
* Return values are consistent with the {@code NamedFactory} implementations in the {@code keyprovider} package. * Return values are consistent with the {@code NamedFactory} implementations in the {@code keyprovider} package.
* *
* @param location * @param location
*
* @return name of the key file format * @return name of the key file format
*
* @throws java.io.IOException * @throws java.io.IOException
*/ */
public static FileKeyProvider.Format detectKeyFileFormat(File location) public static KeyFormat detectKeyFileFormat(File location)
throws IOException { throws IOException {
return detectKeyFileFormat(new FileReader(location), 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 privateKey Private key stored in a string
* @param separatePubKey Is the public key stored separately from the private key * @param separatePubKey Is the public key stored separately from the private key
*
* @return name of the key file format * @return name of the key file format
*
* @throws java.io.IOException * @throws java.io.IOException
*/ */
public static FileKeyProvider.Format detectKeyFileFormat(String privateKey, public static KeyFormat detectKeyFileFormat(String privateKey,
boolean separatePubKey) boolean separatePubKey)
throws IOException { throws IOException {
return detectKeyFileFormat(new StringReader(privateKey), separatePubKey); return detectKeyFileFormat(new StringReader(privateKey), separatePubKey);
@@ -68,29 +64,38 @@ public class KeyProviderUtil {
* *
* @param privateKey Private key accessible through a {@code Reader} * @param privateKey Private key accessible through a {@code Reader}
* @param separatePubKey Is the public key stored separately from the private key * @param separatePubKey Is the public key stored separately from the private key
*
* @return name of the key file format * @return name of the key file format
*
* @throws java.io.IOException * @throws java.io.IOException
*/ */
private static FileKeyProvider.Format detectKeyFileFormat(Reader privateKey, public static KeyFormat detectKeyFileFormat(Reader privateKey,
boolean separatePubKey) boolean separatePubKey)
throws IOException { throws IOException {
BufferedReader br = new BufferedReader(privateKey); BufferedReader br = new BufferedReader(privateKey);
String firstLine = br.readLine(); final String firstLine;
try {
firstLine = br.readLine();
}
finally {
IOUtils.closeQuietly(br); IOUtils.closeQuietly(br);
if (firstLine == null) }
if(firstLine == null) {
throw new IOException("Empty file"); throw new IOException("Empty file");
if (firstLine.startsWith("-----BEGIN") && firstLine.endsWith("PRIVATE KEY-----")) }
if (separatePubKey) if(firstLine.startsWith("-----BEGIN") && firstLine.endsWith("PRIVATE KEY-----")) {
if(separatePubKey)
// Can delay asking for password since have unencrypted pubkey // Can delay asking for password since have unencrypted pubkey
return FileKeyProvider.Format.OpenSSH; {
return KeyFormat.OpenSSH;
}
else else
// More general // More general
return FileKeyProvider.Format.PKCS8; {
/* return KeyFormat.PKCS8;
* TODO: Tectia, PuTTY (.ppk) ... }
*/ }
return FileKeyProvider.Format.Unknown; if(firstLine.startsWith("PuTTY-User-Key-File-")) {
return KeyFormat.PuTTY;
}
return KeyFormat.Unknown;
} }
} }