mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
Extract formats. Add PuTTY to enum.
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
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,15 +32,13 @@ 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;
|
||||||
IOUtils.closeQuietly(br);
|
try {
|
||||||
if (firstLine == null)
|
firstLine = br.readLine();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
IOUtils.closeQuietly(br);
|
||||||
|
}
|
||||||
|
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-----")) {
|
||||||
// Can delay asking for password since have unencrypted pubkey
|
if(separatePubKey)
|
||||||
return FileKeyProvider.Format.OpenSSH;
|
// Can delay asking for password since have unencrypted pubkey
|
||||||
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user