def get_ssh_public_key(ssh_public_key): """ Returns the proper SSHPublicKey instance based off of the SSH Public Key file. :param ssh_public_key: SSH Public Key file contents. (i.e. 'ssh-XXX AAAA....'). :return: An SSHPublicKey instance. """ if ssh_public_key.startswith(SSHPublicKeyType.RSA): rsa_public_key = RSAPublicKey(ssh_public_key) rsa_public_key.validate_for_signing() return rsa_public_key else: raise TypeError("Unsupported Public Key Type")
def get_ssh_public_key(ssh_public_key): """ Returns the proper SSHPublicKey instance based off of the SSH Public Key file. :param ssh_public_key: SSH Public Key file contents. (i.e. 'ssh-XXX AAAA....'). :return: An SSHPublicKey instance. """ if ssh_public_key.startswith( SSHPublicKeyType.RSA_BYTES) or ssh_public_key.startswith( SSHPublicKeyType.RSA): rsa_public_key = RSAPublicKey(ssh_public_key) rsa_public_key.validate_for_signing() return rsa_public_key else: raise TypeError("Unsupported Public Key Type")
def test_validation_for_signing(): pub_key = RSAPublicKey(EXAMPLE_RSA_PUBLIC_KEY_1024) with pytest.raises(ValueError): pub_key.validate_for_signing() pub_key_sp = RSAPublicKey(EXAMPLE_RSA_PUBLIC_KEY_SMALLPRIME) with pytest.raises(ValueError): pub_key_sp.validate_for_signing() pub_key_e3 = RSAPublicKey(EXAMPLE_RSA_PUBLIC_KEY_E3) with pytest.raises(ValueError): pub_key_e3.validate_for_signing() pub_key_valid = RSAPublicKey(EXAMPLE_RSA_PUBLIC_KEY_2048) try: pub_key_valid.validate_for_signing() except ValueError: pytest.fail("Valid key failed to validate")