예제 #1
0
파일: pkey.py 프로젝트: wj2409/CloudStack
 def _write_private_key(self, tag, f, data, password=None):
     f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
     if password is not None:
         # since we only support one cipher here, use it
         cipher_name = self._CIPHER_TABLE.keys()[0]
         cipher = self._CIPHER_TABLE[cipher_name]['cipher']
         keysize = self._CIPHER_TABLE[cipher_name]['keysize']
         blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
         mode = self._CIPHER_TABLE[cipher_name]['mode']
         salt = randpool.get_bytes(8)
         key = util.generate_key_bytes(MD5, salt, password, keysize)
         if len(data) % blocksize != 0:
             n = blocksize - len(data) % blocksize
             #data += randpool.get_bytes(n)
             # that would make more sense ^, but it confuses openssh.
             data += '\0' * n
         data = cipher.new(key, mode, salt).encrypt(data)
         f.write('Proc-Type: 4,ENCRYPTED\n')
         f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper()))
         f.write('\n')
     s = base64.encodestring(data)
     # re-wrap to 64-char lines
     s = ''.join(s.split('\n'))
     s = '\n'.join([s[i:i + 64] for i in range(0, len(s), 64)])
     f.write(s)
     f.write('\n')
     f.write('-----END %s PRIVATE KEY-----\n' % tag)
예제 #2
0
파일: pkey.py 프로젝트: Defman21/KomodoEdit
 def _write_private_key(self, tag, f, data, password=None):
     f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
     if password is not None:
         cipher_name = list(self._CIPHER_TABLE.keys())[0]
         cipher = self._CIPHER_TABLE[cipher_name]['cipher']
         keysize = self._CIPHER_TABLE[cipher_name]['keysize']
         blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
         mode = self._CIPHER_TABLE[cipher_name]['mode']
         salt = os.urandom(blocksize)
         key = util.generate_key_bytes(md5, salt, password, keysize)
         if len(data) % blocksize != 0:
             n = blocksize - len(data) % blocksize
             #data += os.urandom(n)
             # that would make more sense ^, but it confuses openssh.
             data += zero_byte * n
         data = cipher.new(key, mode, salt).encrypt(data)
         f.write('Proc-Type: 4,ENCRYPTED\n')
         f.write('DEK-Info: %s,%s\n' % (cipher_name, u(hexlify(salt)).upper()))
         f.write('\n')
     s = u(encodebytes(data))
     # re-wrap to 64-char lines
     s = ''.join(s.split('\n'))
     s = '\n'.join([s[i: i + 64] for i in range(0, len(s), 64)])
     f.write(s)
     f.write('\n')
     f.write('-----END %s PRIVATE KEY-----\n' % tag)
예제 #3
0
 def _read_private_key_old_format(cls, headers, data, password):
     if 'proc-type' not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     proc_type = headers['proc-type']
     if proc_type != '4,ENCRYPTED':
         raise SSHException(
             'Unknown private key structure "{}"'.format(proc_type))
     try:
         encryption_type, saltstr = headers['dek-info'].split(',')
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in cls._CIPHER_TABLE:
         raise SSHException(
             'Unknown private key cipher "{}"'.format(encryption_type))
     # if no password was passed in,
     # raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException('Private key file is encrypted')
     cipher = cls._CIPHER_TABLE[encryption_type]['cipher']
     keysize = cls._CIPHER_TABLE[encryption_type]['keysize']
     mode = cls._CIPHER_TABLE[encryption_type]['mode']
     salt = unhexlify(b(saltstr))
     key = util.generate_key_bytes(md5, salt, password, keysize)
     decryptor = Cipher(cipher(key), mode(salt),
                        backend=default_backend()).decryptor()
     return decryptor.update(data) + decryptor.finalize()
예제 #4
0
 def _write_private_key(self, tag, f, data, password=None):
     f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
     if password is not None:
         cipher_name = list(self._CIPHER_TABLE.keys())[0]
         cipher = self._CIPHER_TABLE[cipher_name]['cipher']
         keysize = self._CIPHER_TABLE[cipher_name]['keysize']
         blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
         mode = self._CIPHER_TABLE[cipher_name]['mode']
         salt = os.urandom(blocksize)
         key = util.generate_key_bytes(md5, salt, password, keysize)
         if len(data) % blocksize != 0:
             n = blocksize - len(data) % blocksize
             #data += os.urandom(n)
             # that would make more sense ^, but it confuses openssh.
             data += zero_byte * n
         data = cipher.new(key, mode, salt).encrypt(data)
         f.write('Proc-Type: 4,ENCRYPTED\n')
         f.write('DEK-Info: %s,%s\n' %
                 (cipher_name, u(hexlify(salt)).upper()))
         f.write('\n')
     s = u(encodebytes(data))
     # re-wrap to 64-char lines
     s = ''.join(s.split('\n'))
     s = '\n'.join([s[i:i + 64] for i in range(0, len(s), 64)])
     f.write(s)
     f.write('\n')
     f.write('-----END %s PRIVATE KEY-----\n' % tag)
예제 #5
0
 def test_1_generate_key_bytes(self):
     from Crypto.Hash import MD5
     key = util.generate_key_bytes(MD5, '\x01\x02\x03\x04',
                                   'happy birthday', 30)
     exp = unhexlify(
         '61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64')
     self.assertEquals(exp, key)
예제 #6
0
파일: pkey.py 프로젝트: Arrjaan/Cliff
 def _write_private_key(self, tag, f, data, password=None):
     f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
     if password is not None:
         # since we only support one cipher here, use it
         cipher_name = self._CIPHER_TABLE.keys()[0]
         cipher = self._CIPHER_TABLE[cipher_name]['cipher']
         keysize = self._CIPHER_TABLE[cipher_name]['keysize']
         blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
         mode = self._CIPHER_TABLE[cipher_name]['mode']
         salt = randpool.get_bytes(8)
         key = util.generate_key_bytes(MD5, salt, password, keysize)
         if len(data) % blocksize != 0:
             n = blocksize - len(data) % blocksize
             #data += randpool.get_bytes(n)
             # that would make more sense ^, but it confuses openssh.
             data += '\0' * n
         data = cipher.new(key, mode, salt).encrypt(data)
         f.write('Proc-Type: 4,ENCRYPTED\n')
         f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper()))
         f.write('\n')
     s = base64.encodestring(data)
     # re-wrap to 64-char lines
     s = ''.join(s.split('\n'))
     s = '\n'.join([s[i : i+64] for i in range(0, len(s), 64)])
     f.write(s)
     f.write('\n')
     f.write('-----END %s PRIVATE KEY-----\n' % tag)
예제 #7
0
 def _read_private_key(self, tag, f, password=None):
     lines = f.readlines()
     start = 0
     beginning_of_key = "-----BEGIN " + tag + " PRIVATE KEY-----"
     while start < len(lines) and lines[start].strip() != beginning_of_key:
         start += 1
     if start >= len(lines):
         raise SSHException("not a valid " + tag + " private key file")
     # parse any headers first
     headers = {}
     start += 1
     while start < len(lines):
         l = lines[start].split(": ")
         if len(l) == 1:
             break
         headers[l[0].lower()] = l[1].strip()
         start += 1
     # find end
     end = start
     ending_of_key = "-----END " + tag + " PRIVATE KEY-----"
     while end < len(lines) and lines[end].strip() != ending_of_key:
         end += 1
     # if we trudged to the end of the file, just try to cope.
     try:
         data = decodebytes(b("".join(lines[start:end])))
     except base64.binascii.Error as e:
         raise SSHException("base64 decoding error: " + str(e))
     if "proc-type" not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     proc_type = headers["proc-type"]
     if proc_type != "4,ENCRYPTED":
         raise SSHException(
             'Unknown private key structure "{}"'.format(proc_type)
         )
     try:
         encryption_type, saltstr = headers["dek-info"].split(",")
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in self._CIPHER_TABLE:
         raise SSHException(
             'Unknown private key cipher "{}"'.format(encryption_type)
         )
     # if no password was passed in,
     # raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException("Private key file is encrypted")
     cipher = self._CIPHER_TABLE[encryption_type]["cipher"]
     keysize = self._CIPHER_TABLE[encryption_type]["keysize"]
     mode = self._CIPHER_TABLE[encryption_type]["mode"]
     salt = unhexlify(b(saltstr))
     key = util.generate_key_bytes(md5, salt, password, keysize)
     decryptor = Cipher(
         cipher(key), mode(salt), backend=default_backend()
     ).decryptor()
     return decryptor.update(data) + decryptor.finalize()
예제 #8
0
파일: pkey.py 프로젝트: reaperhulk/paramiko
 def _read_private_key(self, tag, f, password=None):
     lines = f.readlines()
     start = 0
     beginning_of_key = '-----BEGIN ' + tag + ' PRIVATE KEY-----'
     while start < len(lines) and lines[start].strip() != beginning_of_key:
         start += 1
     if start >= len(lines):
         raise SSHException('not a valid ' + tag + ' private key file')
     # parse any headers first
     headers = {}
     start += 1
     while start < len(lines):
         l = lines[start].split(': ')
         if len(l) == 1:
             break
         headers[l[0].lower()] = l[1].strip()
         start += 1
     # find end
     end = start
     ending_of_key = '-----END ' + tag + ' PRIVATE KEY-----'
     while end < len(lines) and lines[end].strip() != ending_of_key:
         end += 1
     # if we trudged to the end of the file, just try to cope.
     try:
         data = decodebytes(b(''.join(lines[start:end])))
     except base64.binascii.Error as e:
         raise SSHException('base64 decoding error: ' + str(e))
     if 'proc-type' not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     if headers['proc-type'] != '4,ENCRYPTED':
         raise SSHException(
             'Unknown private key structure "%s"' % headers['proc-type'])
     try:
         encryption_type, saltstr = headers['dek-info'].split(',')
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in self._CIPHER_TABLE:
         raise SSHException(
             'Unknown private key cipher "%s"' % encryption_type)
     # if no password was passed in,
     # raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException('Private key file is encrypted')
     cipher = self._CIPHER_TABLE[encryption_type]['cipher']
     keysize = self._CIPHER_TABLE[encryption_type]['keysize']
     mode = self._CIPHER_TABLE[encryption_type]['mode']
     salt = unhexlify(b(saltstr))
     key = util.generate_key_bytes(md5, salt, password, keysize)
     decryptor = Cipher(
         cipher(key), mode(salt), backend=default_backend()
     ).decryptor()
     return decryptor.update(data) + decryptor.finalize()
예제 #9
0
파일: pkey.py 프로젝트: reaperhulk/paramiko
 def _read_private_key(self, tag, f, password=None):
     lines = f.readlines()
     start = 0
     beginning_of_key = '-----BEGIN ' + tag + ' PRIVATE KEY-----'
     while start < len(lines) and lines[start].strip() != beginning_of_key:
         start += 1
     if start >= len(lines):
         raise SSHException('not a valid ' + tag + ' private key file')
     # parse any headers first
     headers = {}
     start += 1
     while start < len(lines):
         l = lines[start].split(': ')
         if len(l) == 1:
             break
         headers[l[0].lower()] = l[1].strip()
         start += 1
     # find end
     end = start
     ending_of_key = '-----END ' + tag + ' PRIVATE KEY-----'
     while end < len(lines) and lines[end].strip() != ending_of_key:
         end += 1
     # if we trudged to the end of the file, just try to cope.
     try:
         data = decodebytes(b(''.join(lines[start:end])))
     except base64.binascii.Error as e:
         raise SSHException('base64 decoding error: ' + str(e))
     if 'proc-type' not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     if headers['proc-type'] != '4,ENCRYPTED':
         raise SSHException('Unknown private key structure "%s"' %
                            headers['proc-type'])
     try:
         encryption_type, saltstr = headers['dek-info'].split(',')
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in self._CIPHER_TABLE:
         raise SSHException('Unknown private key cipher "%s"' %
                            encryption_type)
     # if no password was passed in,
     # raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException('Private key file is encrypted')
     cipher = self._CIPHER_TABLE[encryption_type]['cipher']
     keysize = self._CIPHER_TABLE[encryption_type]['keysize']
     mode = self._CIPHER_TABLE[encryption_type]['mode']
     salt = unhexlify(b(saltstr))
     key = util.generate_key_bytes(md5, salt, password, keysize)
     decryptor = Cipher(cipher(key), mode(salt),
                        backend=default_backend()).decryptor()
     return decryptor.update(data) + decryptor.finalize()
예제 #10
0
파일: pkey.py 프로젝트: pjq/bruteforcessh
    def _write_private_key_file(self, tag, filename, data, password=None):
        """
        Write an SSH2-format private key file in a form that can be read by
        paramiko or openssh.  If no password is given, the key is written in
        a trivially-encoded format (base64) which is completely insecure.  If
        a password is given, DES-EDE3-CBC is used.

        @param tag: C{"RSA"} or C{"DSA"}, the tag used to mark the data block.
        @type tag: str
        @param filename: name of the file to write.
        @type filename: str
        @param data: data blob that makes up the private key.
        @type data: str
        @param password: an optional password to use to encrypt the file.
        @type password: str

        @raise IOError: if there was an error writing the file.
        """
        f = open(filename, 'w', 0600)
        # grrr... the mode doesn't always take hold
        os.chmod(filename, 0600)
        f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
        if password is not None:
            # since we only support one cipher here, use it
            cipher_name = self._CIPHER_TABLE.keys()[0]
            cipher = self._CIPHER_TABLE[cipher_name]['cipher']
            keysize = self._CIPHER_TABLE[cipher_name]['keysize']
            blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
            mode = self._CIPHER_TABLE[cipher_name]['mode']
            salt = randpool.get_bytes(8)
            key = util.generate_key_bytes(MD5, salt, password, keysize)
            if len(data) % blocksize != 0:
                n = blocksize - len(data) % blocksize
                #data += randpool.get_bytes(n)
                # that would make more sense ^, but it confuses openssh.
                data += '\0' * n
            data = cipher.new(key, mode, salt).encrypt(data)
            f.write('Proc-Type: 4,ENCRYPTED\n')
            f.write('DEK-Info: %s,%s\n' % (cipher_name, util.hexify(salt)))
            f.write('\n')
        s = base64.encodestring(data)
        # re-wrap to 64-char lines
        s = ''.join(s.split('\n'))
        s = '\n'.join([s[i:i + 64] for i in range(0, len(s), 64)])
        f.write(s)
        f.write('\n')
        f.write('-----END %s PRIVATE KEY-----\n' % tag)
        f.close()
예제 #11
0
파일: pkey.py 프로젝트: doomzzju/BFSSH
    def _write_private_key_file(self, tag, filename, data, password=None):
        """
        Write an SSH2-format private key file in a form that can be read by
        paramiko or openssh.  If no password is given, the key is written in
        a trivially-encoded format (base64) which is completely insecure.  If
        a password is given, DES-EDE3-CBC is used.

        @param tag: C{"RSA"} or C{"DSA"}, the tag used to mark the data block.
        @type tag: str
        @param filename: name of the file to write.
        @type filename: str
        @param data: data blob that makes up the private key.
        @type data: str
        @param password: an optional password to use to encrypt the file.
        @type password: str

        @raise IOError: if there was an error writing the file.
        """
        f = open(filename, 'w', 0600)
        # grrr... the mode doesn't always take hold
        os.chmod(filename, 0600)
        f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
        if password is not None:
            # since we only support one cipher here, use it
            cipher_name = self._CIPHER_TABLE.keys()[0]
            cipher = self._CIPHER_TABLE[cipher_name]['cipher']
            keysize = self._CIPHER_TABLE[cipher_name]['keysize']
            blocksize = self._CIPHER_TABLE[cipher_name]['blocksize']
            mode = self._CIPHER_TABLE[cipher_name]['mode']
            salt = randpool.get_bytes(8)
            key = util.generate_key_bytes(MD5, salt, password, keysize)
            if len(data) % blocksize != 0:
                n = blocksize - len(data) % blocksize
                #data += randpool.get_bytes(n)
                # that would make more sense ^, but it confuses openssh.
                data += '\0' * n
            data = cipher.new(key, mode, salt).encrypt(data)
            f.write('Proc-Type: 4,ENCRYPTED\n')
            f.write('DEK-Info: %s,%s\n' % (cipher_name, util.hexify(salt)))
            f.write('\n')
        s = base64.encodestring(data)
        # re-wrap to 64-char lines
        s = ''.join(s.split('\n'))
        s = '\n'.join([s[i : i+64] for i in range(0, len(s), 64)])
        f.write(s)
        f.write('\n')
        f.write('-----END %s PRIVATE KEY-----\n' % tag)
        f.close()
예제 #12
0
 def _read_private_key(self, tag, f, password=None):
     lines = f.readlines()
     start = 0
     while (start < len(lines)) and (lines[start].strip() != "-----BEGIN " + tag + " PRIVATE KEY-----"):
         start += 1
     if start >= len(lines):
         raise SSHException("not a valid " + tag + " private key file")
     # parse any headers first
     headers = {}
     start += 1
     while start < len(lines):
         l = lines[start].split(": ")
         if len(l) == 1:
             break
         headers[l[0].lower()] = l[1].strip()
         start += 1
     # find end
     end = start
     while (lines[end].strip() != "-----END " + tag + " PRIVATE KEY-----") and (end < len(lines)):
         end += 1
     # if we trudged to the end of the file, just try to cope.
     try:
         data = base64.decodestring("".join(lines[start:end]))
     except base64.binascii.Error as e:
         raise SSHException("base64 decoding error: " + str(e))
     if "proc-type" not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     if headers["proc-type"] != "4,ENCRYPTED":
         raise SSHException('Unknown private key structure "%s"' % headers["proc-type"])
     try:
         encryption_type, saltstr = headers["dek-info"].split(",")
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in self._CIPHER_TABLE:
         raise SSHException('Unknown private key cipher "%s"' % encryption_type)
     # if no password was passed in, raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException("Private key file is encrypted")
     cipher = self._CIPHER_TABLE[encryption_type]["cipher"]
     keysize = self._CIPHER_TABLE[encryption_type]["keysize"]
     mode = self._CIPHER_TABLE[encryption_type]["mode"]
     salt = unhexlify(saltstr)
     key = util.generate_key_bytes(MD5, salt, password, keysize)
     return cipher.new(key, mode, salt).decrypt(data)
예제 #13
0
 def _read_private_key_old_format(self, lines, end, password):
     start = 0
     # parse any headers first
     headers = {}
     start += 1
     while start < len(lines):
         l = lines[start].split(': ')
         if len(l) == 1:
             break
         headers[l[0].lower()] = l[1].strip()
         start += 1
     # if we trudged to the end of the file, just try to cope.
     try:
         data = decodebytes(b(''.join(lines[start:end])))
     except base64.binascii.Error as e:
         raise SSHException('base64 decoding error: ' + str(e))
     if 'proc-type' not in headers:
         # unencryped: done
         return data
     # encrypted keyfile: will need a password
     proc_type = headers['proc-type']
     if proc_type != '4,ENCRYPTED':
         raise SSHException(
             'Unknown private key structure "{}"'.format(proc_type))
     try:
         encryption_type, saltstr = headers['dek-info'].split(',')
     except:
         raise SSHException("Can't parse DEK-info in private key file")
     if encryption_type not in self._CIPHER_TABLE:
         raise SSHException(
             'Unknown private key cipher "{}"'.format(encryption_type))
     # if no password was passed in,
     # raise an exception pointing out that we need one
     if password is None:
         raise PasswordRequiredException('Private key file is encrypted')
     cipher = self._CIPHER_TABLE[encryption_type]['cipher']
     keysize = self._CIPHER_TABLE[encryption_type]['keysize']
     mode = self._CIPHER_TABLE[encryption_type]['mode']
     salt = unhexlify(b(saltstr))
     key = util.generate_key_bytes(md5, salt, password, keysize)
     decryptor = Cipher(cipher(key), mode(salt),
                        backend=default_backend()).decryptor()
     return decryptor.update(data) + decryptor.finalize()
예제 #14
0
class PKey(object):
    """
    Base class for public keys.
    """

    # known encryption types for private key files:
    _CIPHER_TABLE = {
        '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
        },
    }

    def __init__(self, msg=None, data=None):
        """
        Create a new instance of this public key type.  If ``msg`` is given,
        the key's public part(s) will be filled in from the message.  If
        ``data`` is given, the key's public part(s) will be filled in from
        the string.

        :param .Message msg:
            an optional SSH `.Message` containing a public key of this type.
        :param str data: an optional string containing a public key of this type

        :raises SSHException:
            if a key cannot be created from the ``data`` or ``msg`` given, or
            no key was passed in.
        """
        pass

    def __str__(self):
        """
        Return a string of an SSH `.Message` made up of the public part(s) of
        this key.  This string is suitable for passing to `__init__` to
        re-create the key object later.
        """
        return ''

    def __cmp__(self, other):
        """
        Compare this key to another.  Returns 0 if this key is equivalent to
        the given key, or non-0 if they are different.  Only the public parts
        of the key are compared, so a public key will compare equal to its
        corresponding private key.

        :param .Pkey other: key to compare to.
        """
        hs = hash(self)
        ho = hash(other)
        if hs != ho:
            return cmp(hs, ho)
        return cmp(str(self), str(other))

    def get_name(self):
        """
        Return the name of this private key implementation.

        :return:
            name of this private key type, in SSH terminology, as a `str` (for
            example, ``"ssh-rsa"``).
        """
        return ''

    def get_bits(self):
        """
        Return the number of significant bits in this key.  This is useful
        for judging the relative security of a key.

        :return: bits in the key (as an `int`)
        """
        return 0

    def can_sign(self):
        """
        Return ``True`` if this key has the private part necessary for signing
        data.
        """
        return False

    def get_fingerprint(self):
        """
        Return an MD5 fingerprint of the public part of this key.  Nothing
        secret is revealed.

        :return:
            a 16-byte `string <str>` (binary) of the MD5 fingerprint, in SSH
            format.
        """
        return MD5.new(str(self)).digest()

    def get_base64(self):
        """
        Return a base64 string containing the public part of this key.  Nothing
        secret is revealed.  This format is compatible with that used to store
        public key files or recognized host keys.

        :return: a base64 `string <str>` containing the public part of the key.
        """
        return base64.encodestring(str(self)).replace('\n', '')

    def sign_ssh_data(self, rng, data):
        """
        Sign a blob of data with this private key, and return a `.Message`
        representing an SSH signature message.

        :param .Crypto.Util.rng.RandomPool rng: a secure random number generator.
        :param str data: the data to sign.
        :return: an SSH signature `message <.Message>`.
        """
        return ''

    def verify_ssh_sig(self, data, msg):
        """
        Given a blob of data, and an SSH message representing a signature of
        that data, verify that it was signed with this key.

        :param str data: the data that was signed.
        :param .Message msg: an SSH signature message
        :return:
            ``True`` if the signature verifies correctly; ``False`` otherwise.
        """
        return False

    def from_private_key_file(cls, filename, password=None):
        """
        Create a key object by reading a private key file.  If the private
        key is encrypted and ``password`` is not ``None``, the given password
        will be used to decrypt the key (otherwise `.PasswordRequiredException`
        is thrown).  Through the magic of Python, this factory method will
        exist in all subclasses of PKey (such as `.RSAKey` or `.DSSKey`), but
        is useless on the abstract PKey class.

        :param str filename: name of the file to read
        :param str password: an optional password to use to decrypt the key file,
            if it's encrypted
        :return: a new `.PKey` based on the given private key

        :raises IOError: if there was an error reading the file
        :raises PasswordRequiredException: if the private key file is
            encrypted, and ``password`` is ``None``
        :raises SSHException: if the key file is invalid
        """
        key = cls(filename=filename, password=password)
        return key

    from_private_key_file = classmethod(from_private_key_file)

    def from_private_key(cls, file_obj, password=None):
        """
        Create a key object by reading a private key from a file (or file-like)
        object.  If the private key is encrypted and ``password`` is not ``None``,
        the given password will be used to decrypt the key (otherwise
        `.PasswordRequiredException` is thrown).

        :param file file_obj: the file to read from
        :param str password:
            an optional password to use to decrypt the key, if it's encrypted
        :return: a new `.PKey` based on the given private key

        :raises IOError: if there was an error reading the key
        :raises PasswordRequiredException: if the private key file is encrypted,
            and ``password`` is ``None``
        :raises SSHException: if the key file is invalid
        """
        key = cls(file_obj=file_obj, password=password)
        return key

    from_private_key = classmethod(from_private_key)

    def write_private_key_file(self, filename, password=None):
        """
        Write private key contents into a file.  If the password is not
        ``None``, the key is encrypted before writing.

        :param str filename: name of the file to write
        :param str password:
            an optional password to use to encrypt the key file

        :raises IOError: if there was an error writing the file
        :raises SSHException: if the key is invalid
        """
        raise Exception('Not implemented in PKey')

    def write_private_key(self, file_obj, password=None):
        """
        Write private key contents into a file (or file-like) object.  If the
        password is not ``None``, the key is encrypted before writing.

        :param file file_obj: the file object to write into
        :param str password: an optional password to use to encrypt the key

        :raises IOError: if there was an error writing to the file
        :raises SSHException: if the key is invalid
        """
        raise Exception('Not implemented in PKey')

    def _read_private_key_file(self, tag, filename, password=None):
        """
        Read an SSH2-format private key file, looking for a string of the type
        ``"BEGIN xxx PRIVATE KEY"`` for some ``xxx``, base64-decode the text we
        find, and return it as a string.  If the private key is encrypted and
        ``password`` is not ``None``, the given password will be used to decrypt
        the key (otherwise `.PasswordRequiredException` is thrown).

        :param str tag: ``"RSA"`` or ``"DSA"``, the tag used to mark the data block.
        :param str filename: name of the file to read.
        :param str password:
            an optional password to use to decrypt the key file, if it's
            encrypted.
        :return: data blob (`str`) that makes up the private key.

        :raises IOError: if there was an error reading the file.
        :raises PasswordRequiredException: if the private key file is
            encrypted, and ``password`` is ``None``.
        :raises SSHException: if the key file is invalid.
        """
        f = open(filename, 'r')
        data = self._read_private_key(tag, f, password)
        f.close()
        return data

    def _read_private_key(self, tag, f, password=None):
        lines = f.readlines()
        start = 0
        while (start < len(lines)) and (lines[start].strip() != '-----BEGIN ' +
                                        tag + ' PRIVATE KEY-----'):
            start += 1
        if start >= len(lines):
            raise SSHException('not a valid ' + tag + ' private key file')
        # parse any headers first
        headers = {}
        start += 1
        while start < len(lines):
            l = lines[start].split(': ')
            if len(l) == 1:
                break
            headers[l[0].lower()] = l[1].strip()
            start += 1
        # find end
        end = start
        while (lines[end].strip() !=
               '-----END ' + tag + ' PRIVATE KEY-----') and (end < len(lines)):
            end += 1
        # if we trudged to the end of the file, just try to cope.
        try:
            data = base64.decodestring(''.join(lines[start:end]))
        except base64.binascii.Error, e:
            raise SSHException('base64 decoding error: ' + str(e))
        if 'proc-type' not in headers:
            # unencryped: done
            return data
        # encrypted keyfile: will need a password
        if headers['proc-type'] != '4,ENCRYPTED':
            raise SSHException('Unknown private key structure "%s"' %
                               headers['proc-type'])
        try:
            encryption_type, saltstr = headers['dek-info'].split(',')
        except:
            raise SSHException('Can\'t parse DEK-info in private key file')
        if encryption_type not in self._CIPHER_TABLE:
            raise SSHException('Unknown private key cipher "%s"' %
                               encryption_type)
        # if no password was passed in, raise an exception pointing out that we need one
        if password is None:
            raise PasswordRequiredException('Private key file is encrypted')
        cipher = self._CIPHER_TABLE[encryption_type]['cipher']
        keysize = self._CIPHER_TABLE[encryption_type]['keysize']
        mode = self._CIPHER_TABLE[encryption_type]['mode']
        salt = unhexlify(saltstr)
        key = util.generate_key_bytes(MD5, salt, password, keysize)
        return cipher.new(key, mode, salt).decrypt(data)
예제 #15
0
 def test_1_generate_key_bytes(self):
     from Crypto.Hash import MD5
     key = util.generate_key_bytes(MD5, x1234, 'happy birthday', 30)
     exp = b'\x61\xE1\xF2\x72\xF4\xC1\xC4\x56\x15\x86\xBD\x32\x24\x98\xC0\xE9\x24\x67\x27\x80\xF4\x7B\xB3\x7D\xDA\x7D\x54\x01\x9E\x64'
     self.assertEqual(exp, key)
예제 #16
0
 def test_1_generate_key_bytes(self):
     key = util.generate_key_bytes(md5, x1234, "happy birthday", 30)
     exp = (
         b"\x61\xE1\xF2\x72\xF4\xC1\xC4\x56\x15\x86\xBD\x32\x24\x98\xC0\xE9\x24\x67\x27\x80\xF4\x7B\xB3\x7D\xDA\x7D\x54\x01\x9E\x64"
     )
     self.assertEqual(exp, key)
예제 #17
0
 def test_1_generate_key_bytes(self):
     from Crypto.Hash import MD5
     key = util.generate_key_bytes(MD5, '\x01\x02\x03\x04', 'happy birthday', 30)
     exp = unhexlify('61E1F272F4C1C4561586BD322498C0E924672780F47BB37DDA7D54019E64')
     self.assertEquals(exp, key)
예제 #18
0
 def test_generate_key_bytes(self):
     x1234 = b'\x01\x02\x03\x04'
     key = util.generate_key_bytes(md5, x1234, 'happy birthday', 30)
     exp = b'\x61\xE1\xF2\x72\xF4\xC1\xC4\x56\x15\x86\xBD\x32\x24\x98\xC0\xE9\x24\x67\x27\x80\xF4\x7B\xB3\x7D\xDA\x7D\x54\x01\x9E\x64'  # noqa: E501
     self.assertEqual(exp, key)
예제 #19
0
파일: pkey.py 프로젝트: wj2409/CloudStack
class PKey(object):
    """
    Base class for public keys.
    """

    # known encryption types for private key files:
    _CIPHER_TABLE = {
        'DES-EDE3-CBC': {
            'cipher': DES3,
            'keysize': 24,
            'blocksize': 8,
            'mode': DES3.MODE_CBC
        }
    }

    def __init__(self, msg=None, data=None):
        """
        Create a new instance of this public key type.  If C{msg} is given,
        the key's public part(s) will be filled in from the message.  If
        C{data} is given, the key's public part(s) will be filled in from
        the string.

        @param msg: an optional SSH L{Message} containing a public key of this
        type.
        @type msg: L{Message}
        @param data: an optional string containing a public key of this type
        @type data: str

        @raise SSHException: if a key cannot be created from the C{data} or
        C{msg} given, or no key was passed in.
        """
        pass

    def __str__(self):
        """
        Return a string of an SSH L{Message} made up of the public part(s) of
        this key.  This string is suitable for passing to L{__init__} to
        re-create the key object later.

        @return: string representation of an SSH key message.
        @rtype: str
        """
        return ''

    def __cmp__(self, other):
        """
        Compare this key to another.  Returns 0 if this key is equivalent to
        the given key, or non-0 if they are different.  Only the public parts
        of the key are compared, so a public key will compare equal to its
        corresponding private key.

        @param other: key to compare to.
        @type other: L{PKey}
        @return: 0 if the two keys are equivalent, non-0 otherwise.
        @rtype: int
        """
        hs = hash(self)
        ho = hash(other)
        if hs != ho:
            return cmp(hs, ho)
        return cmp(str(self), str(other))

    def get_name(self):
        """
        Return the name of this private key implementation.

        @return: name of this private key type, in SSH terminology (for
        example, C{"ssh-rsa"}).
        @rtype: str
        """
        return ''

    def get_bits(self):
        """
        Return the number of significant bits in this key.  This is useful
        for judging the relative security of a key.

        @return: bits in the key.
        @rtype: int
        """
        return 0

    def can_sign(self):
        """
        Return C{True} if this key has the private part necessary for signing
        data.

        @return: C{True} if this is a private key.
        @rtype: bool
        """
        return False

    def get_fingerprint(self):
        """
        Return an MD5 fingerprint of the public part of this key.  Nothing
        secret is revealed.

        @return: a 16-byte string (binary) of the MD5 fingerprint, in SSH
            format.
        @rtype: str
        """
        return MD5.new(str(self)).digest()

    def get_base64(self):
        """
        Return a base64 string containing the public part of this key.  Nothing
        secret is revealed.  This format is compatible with that used to store
        public key files or recognized host keys.

        @return: a base64 string containing the public part of the key.
        @rtype: str
        """
        return base64.encodestring(str(self)).replace('\n', '')

    def sign_ssh_data(self, randpool, data):
        """
        Sign a blob of data with this private key, and return a L{Message}
        representing an SSH signature message.

        @param randpool: a secure random number generator.
        @type randpool: L{Crypto.Util.randpool.RandomPool}
        @param data: the data to sign.
        @type data: str
        @return: an SSH signature message.
        @rtype: L{Message}
        """
        return ''

    def verify_ssh_sig(self, data, msg):
        """
        Given a blob of data, and an SSH message representing a signature of
        that data, verify that it was signed with this key.

        @param data: the data that was signed.
        @type data: str
        @param msg: an SSH signature message
        @type msg: L{Message}
        @return: C{True} if the signature verifies correctly; C{False}
            otherwise.
        @rtype: boolean
        """
        return False

    def from_private_key_file(cls, filename, password=None):
        """
        Create a key object by reading a private key file.  If the private
        key is encrypted and C{password} is not C{None}, the given password
        will be used to decrypt the key (otherwise L{PasswordRequiredException}
        is thrown).  Through the magic of python, this factory method will
        exist in all subclasses of PKey (such as L{RSAKey} or L{DSSKey}), but
        is useless on the abstract PKey class.

        @param filename: name of the file to read
        @type filename: str
        @param password: an optional password to use to decrypt the key file,
            if it's encrypted
        @type password: str
        @return: a new key object based on the given private key
        @rtype: L{PKey}

        @raise IOError: if there was an error reading the file
        @raise PasswordRequiredException: if the private key file is
            encrypted, and C{password} is C{None}
        @raise SSHException: if the key file is invalid
        """
        key = cls(filename=filename, password=password)
        return key

    from_private_key_file = classmethod(from_private_key_file)

    def from_private_key(cls, file_obj, password=None):
        """
        Create a key object by reading a private key from a file (or file-like)
        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
        L{PasswordRequiredException} is thrown).
        
        @param file_obj: the file to read from
        @type file_obj: file
        @param password: an optional password to use to decrypt the key, if it's
            encrypted
        @type password: str
        @return: a new key object based on the given private key
        @rtype: L{PKey}
        
        @raise IOError: if there was an error reading the key
        @raise PasswordRequiredException: if the private key file is encrypted,
            and C{password} is C{None}
        @raise SSHException: if the key file is invalid
        """
        key = cls(file_obj=file_obj, password=password)
        return key

    from_private_key = classmethod(from_private_key)

    def write_private_key_file(self, filename, password=None):
        """
        Write private key contents into a file.  If the password is not
        C{None}, the key is encrypted before writing.

        @param filename: name of the file to write
        @type filename: str
        @param password: an optional password to use to encrypt the key file
        @type password: str

        @raise IOError: if there was an error writing the file
        @raise SSHException: if the key is invalid
        """
        raise Exception('Not implemented in PKey')

    def write_private_key(self, file_obj, password=None):
        """
        Write private key contents into a file (or file-like) object.  If the
        password is not C{None}, the key is encrypted before writing.
        
        @param file_obj: the file object to write into
        @type file_obj: file
        @param password: an optional password to use to encrypt the key
        @type password: str
        
        @raise IOError: if there was an error writing to the file
        @raise SSHException: if the key is invalid
        """
        raise Exception('Not implemented in PKey')

    def _read_private_key_file(self, tag, filename, password=None):
        """
        Read an SSH2-format private key file, looking for a string of the type
        C{"BEGIN xxx PRIVATE KEY"} for some C{xxx}, base64-decode the text we
        find, and return it as a string.  If the private key is encrypted and
        C{password} is not C{None}, the given password will be used to decrypt
        the key (otherwise L{PasswordRequiredException} is thrown).

        @param tag: C{"RSA"} or C{"DSA"}, the tag used to mark the data block.
        @type tag: str
        @param filename: name of the file to read.
        @type filename: str
        @param password: an optional password to use to decrypt the key file,
            if it's encrypted.
        @type password: str
        @return: data blob that makes up the private key.
        @rtype: str

        @raise IOError: if there was an error reading the file.
        @raise PasswordRequiredException: if the private key file is
            encrypted, and C{password} is C{None}.
        @raise SSHException: if the key file is invalid.
        """
        f = open(filename, 'r')
        data = self._read_private_key(tag, f, password)
        f.close()
        return data

    def _read_private_key(self, tag, f, password=None):
        lines = f.readlines()
        start = 0
        while (start < len(lines)) and (lines[start].strip() != '-----BEGIN ' +
                                        tag + ' PRIVATE KEY-----'):
            start += 1
        if start >= len(lines):
            raise SSHException('not a valid ' + tag + ' private key file')
        # parse any headers first
        headers = {}
        start += 1
        while start < len(lines):
            l = lines[start].split(': ')
            if len(l) == 1:
                break
            headers[l[0].lower()] = l[1].strip()
            start += 1
        # find end
        end = start
        while (lines[end].strip() !=
               '-----END ' + tag + ' PRIVATE KEY-----') and (end < len(lines)):
            end += 1
        # if we trudged to the end of the file, just try to cope.
        try:
            data = base64.decodestring(''.join(lines[start:end]))
        except base64.binascii.Error, e:
            raise SSHException('base64 decoding error: ' + str(e))
        if 'proc-type' not in headers:
            # unencryped: done
            return data
        # encrypted keyfile: will need a password
        if headers['proc-type'] != '4,ENCRYPTED':
            raise SSHException('Unknown private key structure "%s"' %
                               headers['proc-type'])
        try:
            encryption_type, saltstr = headers['dek-info'].split(',')
        except:
            raise SSHException('Can\'t parse DEK-info in private key file')
        if encryption_type not in self._CIPHER_TABLE:
            raise SSHException('Unknown private key cipher "%s"' %
                               encryption_type)
        # if no password was passed in, raise an exception pointing out that we need one
        if password is None:
            raise PasswordRequiredException('Private key file is encrypted')
        cipher = self._CIPHER_TABLE[encryption_type]['cipher']
        keysize = self._CIPHER_TABLE[encryption_type]['keysize']
        mode = self._CIPHER_TABLE[encryption_type]['mode']
        salt = unhexlify(saltstr)
        key = util.generate_key_bytes(MD5, salt, password, keysize)
        return cipher.new(key, mode, salt).decrypt(data)
예제 #20
0
 def test_1_generate_key_bytes(self):
     key = util.generate_key_bytes(md5, x1234, 'happy birthday', 30)
     exp = b'\x61\xE1\xF2\x72\xF4\xC1\xC4\x56\x15\x86\xBD\x32\x24\x98\xC0\xE9\x24\x67\x27\x80\xF4\x7B\xB3\x7D\xDA\x7D\x54\x01\x9E\x64'
     self.assertEqual(exp, key)