How to use the asn1crypto._errors.unwrap function in asn1crypto

To help you get started, we’ve selected a few asn1crypto 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 wbond / asn1crypto / asn1crypto / core.py View on Github external
end_token = encoded_data.find(b'\x00\x00', pointer)
        if end_token > encoded_length:
            raise ValueError(unwrap(
                '''
                Insufficient data - %s bytes requested but only %s available
                ''',
                end_token,
                encoded_length
            ))
        contents = encoded_data[pointer:end_token]
        pointer = end_token + 2
        trailer = b'\x00\x00'

    else:
        if pointer + length > encoded_length:
            raise ValueError(unwrap(
                '''
                Insufficient data - %s bytes requested but only %s available
                ''',
                pointer + length,
                encoded_length
            ))
        contents = encoded_data[pointer:pointer + length]
        pointer += length
        trailer = b''

    return ((class_, method, tag, header, contents, trailer), pointer - start)
github wbond / asn1crypto / asn1crypto / core.py View on Github external
Insufficient data - %s bytes requested but only %s available
            ''',
            pointer + 1,
            encoded_length
        ))
    length_octet = ord(encoded_data[pointer]) if py2 else encoded_data[pointer]

    pointer += 1
    length_type = length_octet >> 7
    if length_type == 1:
        length = 0
        remaining_length_octets = length_octet & 127
        while remaining_length_octets > 0:
            length *= 256
            if pointer + 1 > encoded_length:
                raise ValueError(unwrap(
                    '''
                    Insufficient data - %s bytes requested but only %s available
                    ''',
                    pointer + 1,
                    encoded_length
                ))
            length += ord(encoded_data[pointer]) if py2 else encoded_data[pointer]
            pointer += 1
            remaining_length_octets -= 1
    else:
        length = length_octet & 127

    if pointer > encoded_length:
        raise ValueError(unwrap(
            '''
            Insufficient data - %s bytes requested but only %s available
github wbond / asn1crypto / asn1crypto / util.py View on Github external
def _comparison_error(self, other):
        """
        Raises a TypeError about the other object not being suitable for
        comparison

        :param other:
            The object being compared to
        """

        raise TypeError(unwrap(
            '''
            An asn1crypto.util.extended_datetime object can only be compared to
            an asn1crypto.util.extended_datetime or datetime.datetime object,
            not %s
            ''',
            type_name(other)
        ))
github wbond / asn1crypto / asn1crypto / algos.py View on Github external
def kdf_salt(self):
        """
        Returns the byte string to use as the salt for the KDF.

        :return:
            A byte string
        """

        encryption_algo = self['algorithm'].native

        if encryption_algo == 'pbes2':
            salt = self['parameters']['key_derivation_func']['parameters']['salt']

            if salt.name == 'other_source':
                raise ValueError(unwrap(
                    '''
                    Can not determine key derivation salt - the
                    reserved-for-future-use other source salt choice was
                    specified in the PBKDF2 params structure
                    '''
                ))

            return salt.native

        if encryption_algo.find('.') == -1:
            if encryption_algo.find('_') != -1:
                return self['parameters']['salt'].native

            raise ValueError(unwrap(
                '''
                Encryption algorithm "%s" does not have a registered key
github wbond / asn1crypto / asn1crypto / pem.py View on Github external
and footer of the block. E.g. "CERTIFICATE", "PRIVATE KEY", etc. This
        will appear as "-----BEGIN CERTIFICATE-----" and
        "-----END CERTIFICATE-----".

    :param der_bytes:
        A byte string to be armored

    :param headers:
        An OrderedDict of the header lines to write after the BEGIN line

    :return:
        A byte string of the PEM block
    """

    if not isinstance(der_bytes, byte_cls):
        raise TypeError(unwrap(
            '''
            der_bytes must be a byte string, not %s
            ''' % _type_name(der_bytes)
        ))

    if not isinstance(type_name, str_cls):
        raise TypeError(unwrap(
            '''
            type_name must be a unicode string, not %s
            ''',
            _type_name(type_name)
        ))

    type_name = type_name.upper().encode('ascii')

    output = BytesIO()
github wbond / asn1crypto / asn1crypto / core.py View on Github external
if pointer + 1 > encoded_length:
                raise ValueError(unwrap(
                    '''
                    Insufficient data - %s bytes requested but only %s available
                    ''',
                    pointer + 1,
                    encoded_length
                ))
            length += ord(encoded_data[pointer]) if py2 else encoded_data[pointer]
            pointer += 1
            remaining_length_octets -= 1
    else:
        length = length_octet & 127

    if pointer > encoded_length:
        raise ValueError(unwrap(
            '''
            Insufficient data - %s bytes requested but only %s available
            ''',
            pointer,
            encoded_length
        ))
    header = encoded_data[start:pointer]

    # Indefinite length
    if length_type == 1 and length == 0:
        end_token = encoded_data.find(b'\x00\x00', pointer)
        if end_token > encoded_length:
            raise ValueError(unwrap(
                '''
                Insufficient data - %s bytes requested but only %s available
                ''',
github wbond / asn1crypto / asn1crypto / core.py View on Github external
# If an explicit specification was passed in, make sure it matches
    if spec is not None:
        if spec_params:
            value = spec(contents=contents, **spec_params)
        else:
            value = spec(contents=contents)

        is_choice = isinstance(value, Choice)

        if spec == Any:
            pass

        elif value.tag_type == 'explicit':
            if class_ != value.explicit_class:
                raise ValueError(unwrap(
                    '''
                    Error parsing %s - explicitly-tagged class should have been
                    %s, but %s was found
                    ''',
                    type_name(value),
                    CLASS_NUM_TO_NAME_MAP.get(value.explicit_class),
                    CLASS_NUM_TO_NAME_MAP.get(class_, class_)
                ))
            if method != 1:
                raise ValueError(unwrap(
                    '''
                    Error parsing %s - explicitly-tagged method should have
                    been %s, but %s was found
                    ''',
                    type_name(value),
                    METHOD_NUM_TO_NAME_MAP.get(1),
github wbond / asn1crypto / asn1crypto / core.py View on Github external
if pointer > encoded_length:
        raise ValueError(unwrap(
            '''
            Insufficient data - %s bytes requested but only %s available
            ''',
            pointer,
            encoded_length
        ))
    header = encoded_data[start:pointer]

    # Indefinite length
    if length_type == 1 and length == 0:
        end_token = encoded_data.find(b'\x00\x00', pointer)
        if end_token > encoded_length:
            raise ValueError(unwrap(
                '''
                Insufficient data - %s bytes requested but only %s available
                ''',
                end_token,
                encoded_length
            ))
        contents = encoded_data[pointer:end_token]
        pointer = end_token + 2
        trailer = b'\x00\x00'

    else:
        if pointer + length > encoded_length:
            raise ValueError(unwrap(
                '''
                Insufficient data - %s bytes requested but only %s available
                ''',
github wbond / asn1crypto / asn1crypto / core.py View on Github external
value = ''.join(map(str_cls, value))

        else:
            raise TypeError(unwrap(
                '''
                %s value must be a tuple of ones and zeros or a set of unicode
                strings, not %s
                ''',
                type_name(self),
                type_name(value)
            ))

        if self._map is not None:
            size = self._size
            if len(value) > size:
                raise ValueError(unwrap(
                    '''
                    %s value must be at most %s bits long, specified was %s long
                    ''',
                    type_name(self),
                    size,
                    len(value)
                ))
            value += '0' * (size - len(value))
        else:
            size = len(value)

        size_mod = size % 8
        extra_bits = 0
        if size_mod != 0:
            extra_bits = 8 - size_mod
            value += '0' * extra_bits
github wbond / asn1crypto / asn1crypto / _inet.py View on Github external
def inet_ntop(address_family, packed_ip):
    """
    Windows compatibility shim for socket.inet_ntop().

    :param address_family:
        socket.AF_INET for IPv4 or socket.AF_INET6 for IPv6

    :param packed_ip:
        A byte string of the network form of an IP address

    :return:
        A unicode string of the IP address
    """

    if address_family not in set([socket.AF_INET, socket.AF_INET6]):
        raise ValueError(unwrap(
            '''
            address_family must be socket.AF_INET (%s) or socket.AF_INET6 (%s),
            not %s
            ''',
            repr(socket.AF_INET),
            repr(socket.AF_INET6),
            repr(address_family)
        ))

    if not isinstance(packed_ip, byte_cls):
        raise TypeError(unwrap(
            '''
            packed_ip must be a byte string, not %s
            ''',
            type_name(packed_ip)
        ))