How to use the sshpubkeys.exceptions.MalformedDataError 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
"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',
        MalformedDataError, "missing_quote", ["loose", "strict"]
    ],
    [
        'ssh-dss AAAAB3NzaC1kc3MAAAEBAM4SvSD+Gpu4L5TvkroBbAcVPeQID1gdZTpr5fEuOCwTVxMyuPwfHH6txh4Nq0K7MRee5zuVKHNkxj84EMF/+g4eJISmE87wzfYNJQhGNOmXO3yYGCmFaDgrlsWH9OhGglWTxz768gJy4IUP2hqvsbotdpjhRvTFCIep12r4L16rFQJeVVti6Xml3UZHXvoMbwJC+BulwU8hnosXvH4mYikgoaSmDUfJkkLB1nD/g48vo172iu2mfEYqAbn9lX46vnetmpz87IHxbCHmIuJxhMfH+vMY5zvF7vUy018XUPrAcqDkmszhJOFuMhB2SQ6IT8nQZnZ+aGGnoQ2kgjCrrusAAAAVAN/iBeqRO1CPAuNIec08Y9VP+MFTAAABAGDVzqw4i64Fd1mLi6BRKYZdbMI56gx+Cu+EfAjH7dx6AqEW7PiSB2yQrXl1dbCbLYhQhEXIaJW+8epTCsm9o22+KZ5yIyyNc5SxPad7W4W/gvSzqMZC+NMBYia8hrq3BHk9OYxFUZfCx47adbs9sKFi1GhgCbXiDsrb8EvzMWMyyKyAl1AaCaMYWVXnDRBtJnASK2GyiR4GWHA+4z9syrEHknM0RonP2B24552cYq3gxCxRvZ6XJBRQaRnS9RjIaziDImXoLa5X/82ZT6cVzwdZa9S9LGJusfBLrE12O8nis1iI1CliukGtOmWvogFqYABtkzGizrnajCpU4doiuoUAAAEAchR3IrCbXnDekPQGt+uM4cSdZ9OIt2wROlxJODV/Z57OOGMm7RFJkuC4olN1h032dhHM6hsKzsMoRkin0zLuZgsInKs2LnmaL27wwB5wcriLnZm1HK9PUEH5A/wqR4Vc2SHUa1Zzvjddqe0fk2rRzYhaACctSii8CH68nmECZDgn0xePz8n4p4CoZalkQvcjEW/O6nzgrudW3VahxEl4YijuWocQ+y0vWIa6OByuYbDAxST971RF84Q0M8I1jnEwteGcYnLV8CVZQaZ5fmI3t4Z8CcRN339rKpvakVgNYYATJcTc402TIhyRHMLew63et11xkIVGocLaaYaI2gzavA==',
        TooLongKeyError, "dsa_2048", ["strict"]
    ],
    [
        'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQbdtLTII+vP98NSDlK2LXxVARELRYO0NODFYQ0imYxsmBMB7BrfljFppLJyjU6cziOT6YFj6rVd8MmCogdCR32u63EV11uT6RCFfJMQJtIi+B1JJipTxLzURsiUOOgAHJc= ojarva@ojar-laptop.local',
        TooShortKeyError, "rsa_771", ["strict"]
    ],
github ojarva / python-sshpubkeys / tests / test_dsa_keys_failing.py View on Github external
from sshpubkeys.exceptions import MalformedDataError

keys = [
    [
        "ssh-dss AAAAB3NzaC1kc3MAAACBAPlHIP5sD+T8/Sx1DGEiCzCXqpl7ww40jBg7wTkxu44OH6pNog5PjJt5M4NBULhKva/i+bhIM3ba+H1Or+aHWWFHACV6W2FCGk/k37ApRF8sIa4hsnN0P9qn6VfhbJKee+DBxa21WjjY/MZiljmJz7IQHx5RTxX9I/hJ7cL+aNmrAAAAFQCKteqc4IkgIrjpcpStsxYAhb3MqQAAAIEA+SfIKuTr7QPcinsZQDdmZOXqcg+u9TLzHA4c47y0Kns3T3BVPr9rWdmuh6eImzLO4wMLxLvcg3ecrqFuiCp1IHvXENkGlpB17S+uOXlVDY+sTdXyvYKRKirg5IZefIAP/m08c0QGkhFDbo4ysr9D5gXgH3LB2rMPIAbvMWm/HZQAAACBAKWtAE3hXRQX5KtI4AoIWVTly/6T4JNBt4u24ZRqV7X//CZEZ0cS5YpR/frlpUDI3WKoMtS+VmT3cBFZINashIxZyfBF8+0UX3s34HwNfp0hDW3ZdgZJU56GC2eclMantYGeVrMxgTQd80pxZFgByEhoXGeZaAwUzN8ULo9jHQo=",
        MalformedDataError, "too_short_data"
    ],
    [
        "ssh-dss AAAAB3NzaC1kc3MAAACBAPlHIP5sD+T8/Sx1DGEiCzCXqpl7ww40jBg7wTkxu44OH6pNog5PjJt5M4NBULhKva/i+bhIM3ba+H1Or+aHWWFHACV6W2FCGk/k37ApRF8sIa4hsnN0P9qn6VfhbJKee+DBxa21WjjY/MZiljmJz7IQHx5RTxX9I/hJ7cL+aNmrAAAAFQCKteqc4IkgIrjpcpStsxYAhb3MqQAAAIEA+SfIKuTr7QPcinsZQDdmZOXqcg+u9TLzHA4c47y0Kns3T3BVPr9rWdmuh6eImzLO4wMLxLvcg3ecrqFuiCp1IHvXENkGlpB17S+uOXlVDY+sTdXyvYKRKirg5IZefIAP/m08c0QGkhFDbo4ysr9D5gXgH3LB2rMPIAbvMWm/HZQAAACBAKWtAE3hXRQX5KtI4AoIWVTly/6T4JNBt4u24ZRqV7X//CZEZ0cS5YpR/frlpUDI3WKoMtS+VmT3cBFZINashIxZyfBF8+0UX3s34HwNfp0hDW3ZdgZJU56GC2eclMantYGeVrMxgTQd80pxZFgByEhoXGeZaAwUzN8ULo9jHQqMjA==",
        MalformedDataError, "too_long_data"
    ],
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
options_raw = None
        # Terribly inefficient way to remove options, but hey, it works.
        if not data.startswith("ssh-") and not data.startswith("ecdsa-"):
            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
pubkey_content = key_parts[1]

        self._decoded_key = self.decode_key(pubkey_content)

        # Check key type
        current_position, unpacked_key_type = self._unpack_by_int(self._decoded_key, 0)
        if key_type is not None and key_type != unpacked_key_type.decode():
            raise InvalidTypeError("Keytype mismatch: %s != %s" % (key_type, unpacked_key_type))

        self.key_type = unpacked_key_type

        key_data_length = self._process_key(self._decoded_key[current_position:])
        current_position = current_position + key_data_length

        if current_position != len(self._decoded_key):
            raise MalformedDataError("Leftover data: %s bytes" % (len(self._decoded_key) - current_position))

        if self.disallow_options and self.options:
            raise InvalidOptionsError("Options are disallowed.")
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
def decode_key(cls, pubkey_content):
        """Decode base64 coded part of the key."""
        try:
            decoded_key = base64.b64decode(pubkey_content.encode("ascii"))
        except (TypeError, binascii.Error):
            raise MalformedDataError("Unable to decode the key")
        return decoded_key
github ojarva / python-sshpubkeys / sshpubkeys / keys.py View on Github external
def _unpack_by_int(self, data, current_position):
        """Returns a tuple with (location of next data field, contents of requested data field)."""
        # Unpack length of data field
        try:
            requested_data_length = struct.unpack('>I', data[current_position:current_position + self.INT_LEN])[0]
        except struct.error:
            raise MalformedDataError("Unable to unpack %s bytes from the data" % self.INT_LEN)

        # Move pointer to the beginning of the data field
        current_position += self.INT_LEN
        remaining_data_length = len(data[current_position:])

        if remaining_data_length < requested_data_length:
            raise MalformedDataError(
                "Requested %s bytes, but only %s bytes available." % (requested_data_length, remaining_data_length)
            )

        next_data = data[current_position:current_position + requested_data_length]
        # Move pointer to the end of the data field
        current_position += requested_data_length
        return current_position, next_data