def get_pkey_obj(self): logging.info('Parsing private key {!r}'.format(self.filename)) name, length = self.parse_name(self.iostr, self.tag_to_name) if not name: raise InvalidValueError('Invalid key {}.'.format(self.filename)) offset = self.iostr.tell() - length password = to_bytes(self.password) if self.password else None pkey = self.get_specific_pkey(name, offset, password) if pkey is None and name == 'Ed25519': for name in ['RSA', 'ECDSA', 'DSS']: pkey = self.get_specific_pkey(name, offset, password) if pkey: break if pkey: return pkey logging.error(str(self.last_exception)) msg = 'Invalid key' if self.password: msg += ' or wrong passphrase "{}" for decrypting it.'.format( self.password) raise InvalidValueError(msg)
def initialize(self): if self.is_open_to_public is None: MixinHandler.is_open_to_public = settings.is_open_to_public if self.forbid_public_http is None: MixinHandler.forbid_public_http = options.fbidhttp if self.is_forbidden(): result = '{} 403 Forbidden\r\n\r\n'.format(self.request.version) self.request.connection.stream.write(to_bytes(result)) self.request.connection.close() raise ValueError('Accesss denied')
def get_pkey_obj(cls, privatekey, password): password = to_bytes(password) pkey = cls.get_specific_pkey(paramiko.RSAKey, privatekey, password)\ or cls.get_specific_pkey(paramiko.DSSKey, privatekey, password)\ or cls.get_specific_pkey(paramiko.ECDSAKey, privatekey, password)\ or cls.get_specific_pkey(paramiko.Ed25519Key, privatekey, password) if not pkey: raise ValueError('Not a valid private key file or ' 'wrong password for decrypting the private key.') return pkey
def get_pkey_obj(cls, privatekey, password, filename): bpass = to_bytes(password) pkey = cls.get_specific_pkey(paramiko.RSAKey, privatekey, bpass)\ or cls.get_specific_pkey(paramiko.DSSKey, privatekey, bpass)\ or cls.get_specific_pkey(paramiko.ECDSAKey, privatekey, bpass)\ or cls.get_specific_pkey(paramiko.Ed25519Key, privatekey, bpass) if not pkey: if not password: error = 'Invalid private key: {}'.format(filename) else: error = ('Wrong password {!r} for decrypting the private key.' ).format(password) raise InvalidException(error) return pkey
def get_pkey_obj(self): name, length = self.parse_name(self.iostr, self.tag_to_name) if not name: raise InvalidValueError('Invalid key {}.'.format(self.filename)) offset = self.iostr.tell() - length self.iostr.seek(offset) logging.debug('Reset offset to {}.'.format(offset)) logging.info('Parsing {} key'.format(name)) pkeycls = getattr(paramiko, name+'Key') password = to_bytes(self.password) if self.password else None try: return pkeycls.from_private_key(self.iostr, password=password) except paramiko.PasswordRequiredException: raise InvalidValueError('Need a password to decrypt the key.') except paramiko.SSHException as exc: logging.error(str(exc)) msg = 'Invalid key' if self.password: msg += ' or wrong password "{}" for decrypting it.'.format( self.password) raise InvalidValueError(msg)
def test_to_bytes(self): b = b'hello' u = u'hello' self.assertEqual(to_bytes(b), b) self.assertEqual(to_bytes(u), b)