Merge pull request #9 from tehfink/master

support for openssl keys
This commit is contained in:
Robey Pointer 2011-05-21 19:12:35 -07:00
commit 22608540c3
1 changed files with 11 additions and 10 deletions

View File

@ -25,7 +25,7 @@ from binascii import hexlify, unhexlify
import os import os
from Crypto.Hash import MD5 from Crypto.Hash import MD5
from Crypto.Cipher import DES3 from Crypto.Cipher import DES3, AES
from paramiko.common import * from paramiko.common import *
from paramiko import util from paramiko import util
@ -40,7 +40,8 @@ class PKey (object):
# known encryption types for private key files: # known encryption types for private key files:
_CIPHER_TABLE = { _CIPHER_TABLE = {
'DES-EDE3-CBC': { 'cipher': DES3, 'keysize': 24, 'blocksize': 8, 'mode': DES3.MODE_CBC } 'AES-128-CBC': { 'cipher': AES, 'keysize': 16, 'blocksize': 16, 'mode': AES.MODE_CBC },
'DES-EDE3-CBC': { 'cipher': DES3, 'keysize': 24, 'blocksize': 8, 'mode': DES3.MODE_CBC },
} }
@ -171,7 +172,7 @@ class PKey (object):
@rtype: boolean @rtype: boolean
""" """
return False return False
def from_private_key_file(cls, filename, password=None): def from_private_key_file(cls, filename, password=None):
""" """
Create a key object by reading a private key file. If the private Create a key object by reading a private key file. If the private
@ -204,7 +205,7 @@ class PKey (object):
object. If the private key is encrypted and C{password} is not C{None}, object. If the private key is encrypted and C{password} is not C{None},
the given password will be used to decrypt the key (otherwise the given password will be used to decrypt the key (otherwise
L{PasswordRequiredException} is thrown). L{PasswordRequiredException} is thrown).
@param file_obj: the file to read from @param file_obj: the file to read from
@type file_obj: file @type file_obj: file
@param password: an optional password to use to decrypt the key, if it's @param password: an optional password to use to decrypt the key, if it's
@ -212,7 +213,7 @@ class PKey (object):
@type password: str @type password: str
@return: a new key object based on the given private key @return: a new key object based on the given private key
@rtype: L{PKey} @rtype: L{PKey}
@raise IOError: if there was an error reading the key @raise IOError: if there was an error reading the key
@raise PasswordRequiredException: if the private key file is encrypted, @raise PasswordRequiredException: if the private key file is encrypted,
and C{password} is C{None} and C{password} is C{None}
@ -236,17 +237,17 @@ class PKey (object):
@raise SSHException: if the key is invalid @raise SSHException: if the key is invalid
""" """
raise Exception('Not implemented in PKey') raise Exception('Not implemented in PKey')
def write_private_key(self, file_obj, password=None): def write_private_key(self, file_obj, password=None):
""" """
Write private key contents into a file (or file-like) object. If the Write private key contents into a file (or file-like) object. If the
password is not C{None}, the key is encrypted before writing. password is not C{None}, the key is encrypted before writing.
@param file_obj: the file object to write into @param file_obj: the file object to write into
@type file_obj: file @type file_obj: file
@param password: an optional password to use to encrypt the key @param password: an optional password to use to encrypt the key
@type password: str @type password: str
@raise IOError: if there was an error writing to the file @raise IOError: if there was an error writing to the file
@raise SSHException: if the key is invalid @raise SSHException: if the key is invalid
""" """
@ -279,7 +280,7 @@ class PKey (object):
data = self._read_private_key(tag, f, password) data = self._read_private_key(tag, f, password)
f.close() f.close()
return data return data
def _read_private_key(self, tag, f, password=None): def _read_private_key(self, tag, f, password=None):
lines = f.readlines() lines = f.readlines()
start = 0 start = 0
@ -350,7 +351,7 @@ class PKey (object):
os.chmod(filename, 0600) os.chmod(filename, 0600)
self._write_private_key(tag, f, data, password) self._write_private_key(tag, f, data, password)
f.close() f.close()
def _write_private_key(self, tag, f, data, password=None): def _write_private_key(self, tag, f, data, password=None):
f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag) f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
if password is not None: if password is not None: