How to use the sshpubkeys.exceptions.InvalidKeyError function in sshpubkeys

To help you get started, we’ve selected a few sshpubkeys examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ojarva / python-sshpubkeys / tests / invalid_authorized_keys.py View on Github external
from .invalid_keys import keys as invalid_keys
from .valid_keys import keys as valid_keys
from sshpubkeys.exceptions import InvalidKeyError, MalformedDataError

items = [
    ["lines_with_spaces", " # Comments\n  \n" + valid_keys[0][0] + "\nasdf", InvalidKeyError],
    ["invalid_key", "# Comments\n" + invalid_keys[0][0], MalformedDataError],
]
github ojarva / python-sshpubkeys / tests / invalid_keys.py View on Github external
"too_short_ed25519", ["loose", "strict"]
    ],
    [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIUGODBKRjsFB/1v3pDRGpA6xR+QpOJg9vat0brlbUNDDpA==", InvalidKeyLengthError,
        "too_long_ed25519", ["loose", "strict"]
    ],
    [
        "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE2gqbAChP2h3fTPx3Jy2KdOJUiBGEiqBUwoosfzllw+KrqmGiDEWlufSxdiSOFuLd4a8PSwhoWbdQgRVFrZAvFE=",
        InvalidKeyError, "invalid_ecdsa_key", ["loose", "strict"]
    ],
    [
        "ecdsa-sha2-nistp255 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTUAAAAIbmlzdHAyNTUAAABBBE2gqbAChP2h3fTPx3Jy2KdOJUiBGEiqBUwoosfzllw+KrqmGiDEWlufSxdiSOFuLd4a8PSwhoWbdQRVFrZAvFE=",
        NotImplementedError, "invalid_nist_curve", ["loose", "strict"]
    ],
    ["", InvalidKeyError, "empty_key", ["loose", "strict"]],
    ["- -", InvalidKeyError, "no_content", ["loose", "strict"]],
    [
        "ssh-invalid-key-type AAAAFHNzaC1pbnZhbGlkLWtleS10eXBlAAAAgMEgAGDgAKEBAMAgoECggSCAQIEAoIAhASEhAKDAAOAAwGCBIKAAYGCgYKDhAEBAwICAoIDgIADAYKAAYGAAYAEgwGBBIGDhIOBg4CAAoQDAIEAgIEEggEDgQGCAIGAhAQDgoKAhIAAggSBA4CBggOEBIEDgoMDAwKBg4MDAYIDgAAAAIAAAAAAAAAAAAAAAAIAggEEggSBAIIBgAMDAQADAIQEgAAAAgJAQABBgICBQUABQcFAAcJBgUIAAIHAAkJBQEIAQMEAgIHBQEFCAUBBQUHAQMGBgQBCAEEAgICBQYDBwIJBAYFCQYDBwIAAQIHCQMEBwYCBAICAAEFBQAHAgAIBQAJBwgEBwYGCAEHBgkBAwEBAQcHAQMGCQMCBQIABAUJAgEIAgAAAAgIGAAYIDgoSBA4ABAgAEAIEEAoEAg4IDAgQCAwCDgQMAgQABgoGAAgMCgoOCA4QABAGBAQQDgoKABASBhAQDgoIBgIQEgAQEAQSBg4IEBICEggKBAAABgYKDAgGDgYAAgoGCAoODgYGEAwIBAoACgQADBAGDgASCgQMDgAABAISA",
        NotImplementedError, "not_implemented_key_type", ["loose", "strict"]
    ],

    # 512 bit ed25519 keys are not yet supported by OpenSSH
    [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAQIMcGCUjHYKD/rfvSGiNSB1ij8hScTB7e1bo3XK2oaGGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
        InvalidKeyLengthError, "512bit_ed25519", ["loose", "strict"]
    ],
    [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAQIBBAIDAICBAgEABACAggGCgAABAwOEAwGBAgEEg4SBAAKBBIGEAgSEgAIEgoMCgICAgYMCgYQDgoKBgIIDgwCA=",
        InvalidKeyLengthError, "512bit_ed25519", ["loose", "strict"]
    ],
    [
        'command="dump /home,no-pty,no-port-forwarding ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABADR9kolU4uiD26LMrbakQlNf4QWB2xrdY3nASf6CJdQYzTMjNmbt6sJ4A4pGnCupFrzL04EYDvbVmT4GEZm6CU4BsY61yosnpGSqqcVCdw5xW1k4bCSDPW75WHLCVmYyROhZ+yyo8uAcIy5UIyBZXF/PO7taJrrIi5RwdqIPwtCrJ3dJkcFWa3qZWJykLAFQD5A/lta/egS/u/nyCap2e16WGnvSluz9CyYtGFNS9axzOwHxLFEv2ocOsJjYgzV+Jfpiao94A4VzLKbUDHlfV57KS0tJaT8FKKsg34vN3bsD0zUftLUPpUFgJfMwje0C2rCJkCzwgya2vxLqj2fg0Q0= ojarva@ojar-laptop.local',
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
quote_open = False
            for i, character in enumerate(data):
                if character == '"':  # only double quotes are allowed, no need to care about single quotes
                    quote_open = not quote_open
                if quote_open:
                    continue
                if character == " ":
                    # Data begins after the first space
                    options_raw = data[:i]
                    data = data[i + 1:]
                    break
            else:
                raise MalformedDataError("Couldn't find beginning of the key data")
        key_parts = data.strip().split(None, 2)
        if len(key_parts) < 2:  # Key type and content are mandatory fields.
            raise InvalidKeyError("Unexpected key format: at least type and base64 encoded value is required")
        if len(key_parts) == 3:
            self.comment = key_parts[2]
            key_parts = key_parts[0:2]
        if options_raw:
            # Populate and parse options field.
            self.options_raw = options_raw
            if not self.skip_option_parsing:
                self.options = self.parse_options(self.options_raw)
        else:
            # Set empty defaults for fields
            self.options_raw = None
            self.options = {}
        return key_parts
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
def _process_ecdsa_sha(self, data):
        """Parses ecdsa-sha public keys."""
        current_position, curve_information = self._unpack_by_int(data, 0)
        if curve_information not in self.ECDSA_CURVE_DATA:
            raise NotImplementedError("Invalid curve type: %s" % curve_information)
        curve, hash_algorithm = self.ECDSA_CURVE_DATA[curve_information]

        current_position, key_data = self._unpack_by_int(data, current_position)
        try:
            # data starts with \x04, which should be discarded.
            ecdsa_key = ecdsa.VerifyingKey.from_string(key_data[1:], curve, hash_algorithm)
        except AssertionError:
            raise InvalidKeyError("Invalid ecdsa key")
        self.bits = int(curve_information.replace(b"nistp", b""))
        self.ecdsa = ecdsa_key
        return current_position
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
def _process_ed25516(self, data):
        """Parses ed25516 keys.

        There is no (apparent) way to validate ed25519 keys. This only
        checks data length (256 bits), but does not try to validate
        the key in any way."""

        current_position, verifying_key = self._unpack_by_int(data, 0)
        verifying_key_length = len(verifying_key) * 8
        verifying_key = self._parse_long(verifying_key)

        if verifying_key < 0:
            raise InvalidKeyError("ed25519 verifying key must be >0.")

        self.bits = verifying_key_length
        if self.bits != 256:
            raise InvalidKeyLengthError("ed25519 keys must be 256 bits (was %s bits)" % self.bits)
        return current_position
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
self._decoded_key = None
        self.rsa = None
        self.dsa = None
        self.ecdsa = None
        self.bits = None
        self.comment = None
        self.options = None
        self.options_raw = None
        self.key_type = None
        self.strict_mode = bool(kwargs.get("strict", True))
        self.skip_option_parsing = bool(kwargs.get("skip_option_parsing", False))
        self.disallow_options = bool(kwargs.get("disallow_options", False))
        if keydata:
            try:
                self.parse(keydata)
            except (InvalidKeyError, NotImplementedError):
                pass
github ojarva / python-sshpubkeys / sshpubkeys / exceptions.py View on Github external
"""Key is shorter than what the specification allow."""


class TooShortKeyError(TooShortKeyException):
    """Key is shorter than what the specification allows."""


class TooLongKeyException(InvalidKeyLengthError):
    """Key is longer than what the specification allows."""


class TooLongKeyError(TooLongKeyException):
    """Key is longer than what the specification allows."""


class InvalidTypeException(InvalidKeyError):
    """Key type is invalid or unrecognized."""


class InvalidTypeError(InvalidTypeException):
    """Key type is invalid or unrecognized."""


class MalformedDataException(InvalidKeyError):
    """The key is invalid - unable to parse the data. The data may be corrupted, truncated, or includes extra content that is not allowed."""


class MalformedDataError(MalformedDataException):
    """The key is invalid - unable to parse the data. The data may be corrupted, truncated, or includes extra content that is not allowed."""


class InvalidOptionsException(MalformedDataError):
github ojarva / python-sshpubkeys / sshpubkeys / exceptions.py View on Github external
class InvalidKeyException(Exception):
    """Invalid key - something is wrong with the key, and it should not be accepted, as OpenSSH will not work with it."""


class InvalidKeyError(InvalidKeyException):
    """Invalid key - something is wrong with the key, and it should not be accepted, as OpenSSH will not work with it."""


class InvalidKeyLengthException(InvalidKeyError):
    """Invalid key length - either too short or too long.

    See also TooShortKeyException and TooLongKeyException."""


class InvalidKeyLengthError(InvalidKeyError):
    """Invalid key length - either too short or too long.

    See also TooShortKeyException and TooLongKeyException."""


class TooShortKeyException(InvalidKeyLengthError):
    """Key is shorter than what the specification allow."""


class TooShortKeyError(TooShortKeyException):
    """Key is shorter than what the specification allows."""


class TooLongKeyException(InvalidKeyLengthError):
    """Key is longer than what the specification allows."""