com.mindbright.ssh2
Class SSH2KeyPairFile

java.lang.Object
  extended bycom.mindbright.ssh2.SSH2KeyPairFile

public class SSH2KeyPairFile
extends java.lang.Object

This class implements the file formats commonly used for storing key pairs for public key authentication. It can handle both OpenSSH's PEM file format as well as SSH Communications proprietary format for DSA keys. It can also read the PuTTY key file format. When importing/exporting use the appropriate constructor and the load/store methods. Note that this class can also be used to convert key pair files between the formats.

See Also:
SSH2PublicKeyFile

Nested Class Summary
static class SSH2KeyPairFile.PEMDSAPrivate
          Handles PEM encoding of a DSA key.
 
Field Summary
static java.lang.String[] BEGIN_PRV_KEY
           
static java.lang.String[] END_PRV_KEY
           
static java.lang.String FILE_COMMENT
           
static java.lang.String FILE_SUBJECT
           
static java.lang.String PRV_DEKINFO
           
static java.lang.String PRV_PROCTYPE
           
static int SSH_PRIVATE_KEY_MAGIC
           
 
Constructor Summary
SSH2KeyPairFile()
          This is the constructor used for loading a key pair.
SSH2KeyPairFile(KeyPair keyPair, java.lang.String subject, java.lang.String comment)
          This is the constructor used for storing a key pair.
 
Method Summary
static byte[] expandPasswordToKey(java.lang.String password, int keyLen, byte[] salt)
           
static byte[] expandPasswordToKeySSHCom(java.lang.String password, int keyLen)
           
 java.lang.String getAlgorithmName()
           
 ASCIIArmour getArmour()
           
 int getBitLength()
           
 java.lang.String getComment()
           
 KeyPair getKeyPair()
           
 java.lang.String getSubject()
           
 boolean isPuttyFormat()
           
 boolean isSSHComFormat()
           
 void load(java.io.InputStream in, java.lang.String password)
          Load key pair from stream.
 void load(java.lang.String fileName, java.lang.String password)
          Load key pair from file.
static KeyPair readKeyPair(ASCIIArmour armour, byte[] keyBlob, java.lang.String password)
           
static KeyPair readKeyPairSSHCom(byte[] keyBlob, java.lang.String password)
           
 void setComment(java.lang.String comment)
           
 void setSubject(java.lang.String subject)
           
 void store(java.io.OutputStream out, SecureRandom random, java.lang.String password, boolean sshComFormat)
          Store the key pair in the given stream with more format control
 void store(java.lang.String fileName, SecureRandom random, java.lang.String password)
          Store the key pair in the given file
 void store(java.lang.String fileName, SecureRandom random, java.lang.String password, boolean sshComFormat)
          Store the key pair in the given file with more format control
static byte[] writeKeyPair(ASCIIArmour armour, java.lang.String password, SecureRandom random, KeyPair keyPair)
           
static byte[] writeKeyPairSSHCom(java.lang.String password, java.lang.String cipher, KeyPair keyPair)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BEGIN_PRV_KEY

public static final java.lang.String[] BEGIN_PRV_KEY

END_PRV_KEY

public static final java.lang.String[] END_PRV_KEY

SSH_PRIVATE_KEY_MAGIC

public static final int SSH_PRIVATE_KEY_MAGIC
See Also:
Constant Field Values

PRV_PROCTYPE

public static final java.lang.String PRV_PROCTYPE
See Also:
Constant Field Values

PRV_DEKINFO

public static final java.lang.String PRV_DEKINFO
See Also:
Constant Field Values

FILE_SUBJECT

public static final java.lang.String FILE_SUBJECT
See Also:
Constant Field Values

FILE_COMMENT

public static final java.lang.String FILE_COMMENT
See Also:
Constant Field Values
Constructor Detail

SSH2KeyPairFile

public SSH2KeyPairFile(KeyPair keyPair,
                       java.lang.String subject,
                       java.lang.String comment)
This is the constructor used for storing a key pair.

Parameters:
keyPair - the key pair to store
subject - the subject name of the key owner
comment - a comment to accompany the key

SSH2KeyPairFile

public SSH2KeyPairFile()
This is the constructor used for loading a key pair.

Method Detail

getKeyPair

public KeyPair getKeyPair()

getSubject

public java.lang.String getSubject()

setSubject

public void setSubject(java.lang.String subject)

getComment

public java.lang.String getComment()

setComment

public void setComment(java.lang.String comment)

getArmour

public ASCIIArmour getArmour()

isSSHComFormat

public boolean isSSHComFormat()

isPuttyFormat

public boolean isPuttyFormat()

getAlgorithmName

public java.lang.String getAlgorithmName()

getBitLength

public int getBitLength()

writeKeyPair

public static byte[] writeKeyPair(ASCIIArmour armour,
                                  java.lang.String password,
                                  SecureRandom random,
                                  KeyPair keyPair)
                           throws SSH2FatalException
Throws:
SSH2FatalException

writeKeyPairSSHCom

public static byte[] writeKeyPairSSHCom(java.lang.String password,
                                        java.lang.String cipher,
                                        KeyPair keyPair)
                                 throws SSH2FatalException
Throws:
SSH2FatalException

readKeyPair

public static KeyPair readKeyPair(ASCIIArmour armour,
                                  byte[] keyBlob,
                                  java.lang.String password)
                           throws SSH2Exception
Throws:
SSH2Exception

readKeyPairSSHCom

public static KeyPair readKeyPairSSHCom(byte[] keyBlob,
                                        java.lang.String password)
                                 throws SSH2Exception
Throws:
SSH2Exception

store

public void store(java.lang.String fileName,
                  SecureRandom random,
                  java.lang.String password)
           throws java.io.IOException,
                  SSH2FatalException
Store the key pair in the given file

Parameters:
fileName - name of file to store keys in
random - random number generator used when encrypting the keys
password - password to use when encrypting the keys
Throws:
java.io.IOException
SSH2FatalException

store

public void store(java.lang.String fileName,
                  SecureRandom random,
                  java.lang.String password,
                  boolean sshComFormat)
           throws java.io.IOException,
                  SSH2FatalException
Store the key pair in the given file with more format control

Parameters:
fileName - name of file to store keys in
random - random number generator used when encrypting the keys
password - password to use when encrypting the keys
sshComFormat - if tru store the key in the ssh.com format
Throws:
java.io.IOException
SSH2FatalException

store

public void store(java.io.OutputStream out,
                  SecureRandom random,
                  java.lang.String password,
                  boolean sshComFormat)
           throws java.io.IOException,
                  SSH2FatalException
Store the key pair in the given stream with more format control

Parameters:
out - output stream to store keys to. Note that this stream will not be closed.
random - random number generator used when encrypting the keys
password - password to use when encrypting the keys
sshComFormat - if tru store the key in the ssh.com format
Throws:
java.io.IOException
SSH2FatalException

load

public void load(java.lang.String fileName,
                 java.lang.String password)
          throws java.io.IOException,
                 SSH2Exception
Load key pair from file.

Parameters:
fileName - name of file to load keys from
password - password used to encrypt the file
Throws:
java.io.IOException
SSH2Exception

load

public void load(java.io.InputStream in,
                 java.lang.String password)
          throws java.io.IOException,
                 SSH2Exception
Load key pair from stream.

Parameters:
in - input stream from which the key pair is read. It will be wrapped in a PushbackInputStream, but not closed.
password - password used to encrypt the file
Throws:
java.io.IOException
SSH2Exception

expandPasswordToKey

public static byte[] expandPasswordToKey(java.lang.String password,
                                         int keyLen,
                                         byte[] salt)

expandPasswordToKeySSHCom

public static byte[] expandPasswordToKeySSHCom(java.lang.String password,
                                               int keyLen)