def key(self): """ :return: a subclass of PKey of the appropriate key type :rtype: PKey :raises ValidationError: """ # Check if the key pair exists: at least a public or private part of # the key is required, as well as the key type. if not self.key_type: return None if not self.public_key and not self.private_key: return None public_key = None private_key = None if self.public_key: public_key = base64.b64decode(self.public_key) if self.private_key: private_key = StringIO(self.private_key) if self.key_type == 'ssh-dss': pkey = DSSKey(data=public_key, file_obj=private_key) elif self.key_type == 'ssh-rsa': pkey = RSAKey(data=public_key, file_obj=private_key) elif self.key_type.startswith('ecdsa'): pkey = ECDSAKey(data=public_key, file_obj=private_key) elif self.key_type == '*****@*****.**': pkey = RSAKey(data=public_key, file_obj=private_key) pkey.load_certificate(Message(public_key)) else: raise ValidationError('Unsupported key type: ' + self.key_type) return pkey