Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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")
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")
def test_valid_key():
pub_key = RSAPublicKey(EXAMPLE_RSA_PUBLIC_KEY)
assert 'Test RSA User Key' == pub_key.key_comment
assert EXAMPLE_RSA_PUBLIC_KEY_N == pub_key.n
assert EXAMPLE_RSA_PUBLIC_KEY_E == pub_key.e
assert 'RSA 57:3d:48:4c:65:90:30:8e:39:ba:d8:fa:d0:20:2e:6c' == pub_key.fingerprint
def test_invalid_keys():
with pytest.raises(TypeError):
RSAPublicKey(EXAMPLE_ECDSA_PUBLIC_KEY)
with pytest.raises(ValueError):
RSAPublicKey('bogus')
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")
def test_invalid_keys():
with pytest.raises(TypeError):
RSAPublicKey(EXAMPLE_ECDSA_PUBLIC_KEY)
with pytest.raises(ValueError):
RSAPublicKey('bogus')
def get_basic_public_key(public_key):
return RSAPublicKey(public_key)
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")
def __init__(self, ssh_public_key):
"""
Extracts the useful RSA Public Key information from an SSH Public Key file.
:param ssh_public_key: SSH Public Key file contents. (i.e. 'ssh-rsa AAAAB3NzaC1yc2E..').
"""
super(RSAPublicKey, self).__init__()
self.type = SSHPublicKeyType.RSA
split_ssh_public_key = ssh_public_key.split(' ')
split_key_len = len(split_ssh_public_key)
# is there a key comment at the end?
if split_key_len > 2:
self.key_comment = ' '.join(split_ssh_public_key[2:])
else:
self.key_comment = ''
public_key = serialization.load_ssh_public_key(ssh_public_key.encode('ascii'), default_backend())
ca_pub_numbers = public_key.public_numbers()
if not isinstance(ca_pub_numbers, RSAPublicNumbers):
raise TypeError("Public Key is not the correct type or format")
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
elif ssh_public_key.startswith(SSHPublicKeyType.ED25519):
ed25519_public_key = ED25519PublicKey(ssh_public_key)
return ed25519_public_key
else:
raise TypeError("Unsupported Public Key Type")