mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
Extract formats. Add PuTTY to enum.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.schmizz.sshj.userauth.keyprovider;
|
||||
|
||||
/**
|
||||
* @version $Id:$
|
||||
*/
|
||||
public enum KeyFormat {
|
||||
PKCS8,
|
||||
OpenSSH,
|
||||
PuTTY,
|
||||
Unknown
|
||||
}
|
||||
@@ -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,12 +32,10 @@ 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());
|
||||
@@ -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,
|
||||
public static KeyFormat detectKeyFileFormat(Reader privateKey,
|
||||
boolean separatePubKey)
|
||||
throws IOException {
|
||||
BufferedReader br = new BufferedReader(privateKey);
|
||||
String firstLine = br.readLine();
|
||||
final String firstLine;
|
||||
try {
|
||||
firstLine = br.readLine();
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(br);
|
||||
if (firstLine == null)
|
||||
}
|
||||
if(firstLine == null) {
|
||||
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
|
||||
return FileKeyProvider.Format.OpenSSH;
|
||||
{
|
||||
return KeyFormat.OpenSSH;
|
||||
}
|
||||
else
|
||||
// More general
|
||||
return FileKeyProvider.Format.PKCS8;
|
||||
/*
|
||||
* TODO: Tectia, PuTTY (.ppk) ...
|
||||
*/
|
||||
return FileKeyProvider.Format.Unknown;
|
||||
{
|
||||
return KeyFormat.PKCS8;
|
||||
}
|
||||
}
|
||||
if(firstLine.startsWith("PuTTY-User-Key-File-")) {
|
||||
return KeyFormat.PuTTY;
|
||||
}
|
||||
return KeyFormat.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user