# Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com> # # This file is part of paramiko. # # Paramiko is free software; you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free # Software Foundation; either version 2.1 of the License, or (at your option) # any later version. # # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. """ RSA keys. """ from cryptography.exceptions import InvalidSignature from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import rsa, padding from paramiko.message import Message from paramiko.pkey import PKey from paramiko.py3compat import PY2 from paramiko.ssh_exception import SSHException class RSAKey(PKey): """ Representation of an RSA key which can be used to sign and verify SSH2 data. """ def __init__( self, msg=None, data=None, filename=None, password=None, key=None, file_obj=None, ): self.key = None self.public_blob = None if file_obj is not None: self._from_private_key(file_obj, password) return if filename is not None: self._from_private_key_file(filename, password) return if (msg is None) and (data is not None): msg = Message(data) if key is not None: self.key = key else: self._check_type_and_load_cert( msg=msg, key_type="ssh-rsa", cert_type="ssh-rsa-cert-v01@openssh.com", ) self.key = rsa.RSAPublicNumbers( e=msg.get_mpint(), n=msg.get_mpint() ).public_key(default_backend()) @property def size(self): return self.key.key_size @property def public_numbers(self): if isinstance(self.key, rsa.RSAPrivateKey): return self.key.private_numbers().public_numbers else: return self.key.public_numbers() def asbytes(self): m = Message() m.add_string("ssh-rsa") m.add_mpint(self.public_numbers.e) m.add_mpint(self.public_numbers.n) return m.asbytes() def __str__(self): # NOTE: as per inane commentary in #853, this appears to be the least # crummy way to get a representation that prints identical to Python # 2's previous behavior, on both interpreters. # TODO: replace with a nice clean fingerprint display or something if PY2: # Can't just return the .decode below for Py2 because stuff still # tries stuffing it into ASCII for whatever godforsaken reason return self.asbytes() else: return self.asbytes().decode("utf8", errors="ignore") def __hash__(self): return hash( (self.get_name(), self.public_numbers.e, self.public_numbers.n) ) def get_name(self): return "ssh-rsa" def get_bits(self): return self.size def can_sign(self): return isinstance(self.key, rsa.RSAPrivateKey) def sign_ssh_data(self, data): sig = self.key.sign( data, padding=padding.PKCS1v15(), algorithm=hashes.SHA1() ) m = Message() m.add_string("ssh-rsa") m.add_string(sig) return m def verify_ssh_sig(self, data, msg): if msg.get_text() != "ssh-rsa": return False key = self.key if isinstance(key, rsa.RSAPrivateKey): key = key.public_key() try: key.verify( msg.get_binary(), data, padding.PKCS1v15(), hashes.SHA1() ) except InvalidSignature: return False else: return True def write_private_key_file(self, filename, password=None): self._write_private_key_file( filename, self.key, serialization.PrivateFormat.TraditionalOpenSSL, password=password, ) def write_private_key(self, file_obj, password=None): self._write_private_key( file_obj, self.key, serialization.PrivateFormat.TraditionalOpenSSL, password=password, ) @staticmethod def generate(bits, progress_func=None): """ Generate a new private RSA key. This factory function can be used to generate a new host key or authentication key. :param int bits: number of bits the generated key should be. :param progress_func: Unused :return: new `.RSAKey` private key """ key = rsa.generate_private_key( public_exponent=65537, key_size=bits, backend=default_backend() ) return RSAKey(key=key) # ...internals... def _from_private_key_file(self, filename, password): data = self._read_private_key_file("RSA", filename, password) self._decode_key(data) def _from_private_key(self, file_obj, password): data = self._read_private_key("RSA", file_obj, password) self._decode_key(data) def _decode_key(self, data): try: key = serialization.load_der_private_key( data, password=None, backend=default_backend() ) except ValueError as e: raise SSHException(str(e)) assert isinstance(key, rsa.RSAPrivateKey) self.key = key
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
__init__.py | File | 3.76 KB | 0644 |
|
_version.py | File | 80 B | 0644 |
|
_winapi.py | File | 11.09 KB | 0644 |
|
agent.py | File | 12.46 KB | 0644 |
|
auth_handler.py | File | 31.19 KB | 0644 |
|
ber.py | File | 4.25 KB | 0644 |
|
buffered_pipe.py | File | 7.29 KB | 0644 |
|
channel.py | File | 48.18 KB | 0644 |
|
client.py | File | 31.32 KB | 0644 |
|
common.py | File | 8.04 KB | 0644 |
|
compress.py | File | 1.26 KB | 0644 |
|
config.py | File | 13.17 KB | 0644 |
|
dsskey.py | File | 7.65 KB | 0644 |
|
ecdsakey.py | File | 10.1 KB | 0644 |
|
ed25519key.py | File | 7.84 KB | 0644 |
|
file.py | File | 19.13 KB | 0644 |
|
hostkeys.py | File | 12.95 KB | 0644 |
|
kex_curve25519.py | File | 4.3 KB | 0644 |
|
kex_ecdh_nist.py | File | 4.86 KB | 0644 |
|
kex_gex.py | File | 10.06 KB | 0644 |
|
kex_group1.py | File | 5.6 KB | 0644 |
|
kex_group14.py | File | 1.79 KB | 0644 |
|
kex_group16.py | File | 2.23 KB | 0644 |
|
kex_gss.py | File | 24 KB | 0644 |
|
message.py | File | 8.8 KB | 0644 |
|
packet.py | File | 22.13 KB | 0644 |
|
pipe.py | File | 3.83 KB | 0644 |
|
pkey.py | File | 21.04 KB | 0644 |
|
primes.py | File | 5 KB | 0644 |
|
proxy.py | File | 4.34 KB | 0644 |
|
py3compat.py | File | 3.71 KB | 0644 |
|
rsakey.py | File | 5.84 KB | 0644 |
|
server.py | File | 29.7 KB | 0644 |
|
sftp.py | File | 5.89 KB | 0644 |
|
sftp_attr.py | File | 8.15 KB | 0644 |
|
sftp_client.py | File | 33.4 KB | 0644 |
|
sftp_file.py | File | 20.1 KB | 0644 |
|
sftp_handle.py | File | 7.26 KB | 0644 |
|
sftp_server.py | File | 19.14 KB | 0644 |
|
sftp_si.py | File | 12.28 KB | 0644 |
|
ssh_exception.py | File | 6.24 KB | 0644 |
|
ssh_gss.py | File | 28.22 KB | 0644 |
|
transport.py | File | 117.68 KB | 0644 |
|
util.py | File | 8.37 KB | 0644 |
|
win_pageant.py | File | 4.17 KB | 0644 |
|