%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core18/current/usr/lib/python3/dist-packages/asn1crypto/__pycache__/
Upload File :
Create Path :
Current File : //snap/core18/current/usr/lib/python3/dist-packages/asn1crypto/__pycache__/core.cpython-36.pyc

3

&\ZLf�@s�dZddlmZmZmZmZddlmZmZddlZddl	Z	ddl
Z
ddlZddlZddl
mZddlmZddlmZdd	lmZmZmZmZmZdd
lmZmZddlmZmZmZm Z ej!d�kr�dd
l"m#Z$e%Z&dZ'nddl(m$Z$dZ'ej)�ddddd�Z*ddddddddd�Z+ddd�Z,ej-d�Z.iZ/d�dd�Z0Gdd�de1�Z2Gd d!�d!�Z3Gd"d#�d#e1�Z4Gd$d%�d%e1�Z5Gd&d'�d'e2�Z6e6�Z7Gd(d)�d)e2�Z8Gd*d+�d+e2�Z9Gd,d-�d-e1�Z:Gd.d/�d/e2�Z;Gd0d1�d1e5e;�Z<Gd2d3�d3e;�Z=Gd4d5�d5e;e3�Z>Gd6d7�d7e5e4e;e3e1�Z?Gd8d9�d9e5e4e;�Z@Gd:d;�d;e5e4e;�ZAGd<d=�d=e5e4e;�ZBGd>d?�d?e5e4e;�ZCGd@dA�dAe5e4e;�ZDGdBdC�dCeD�ZEGdDdE�dEe;�ZFGdFdG�dGe;e3�ZGGdHdI�dIe;�ZHGdJdK�dKe;�ZIGdLdM�dMe;�ZJGdNdO�dOe>�ZKGdPdQ�dQe<�ZLGdRdS�dSeG�ZMGdTdU�dUe2�ZNGdVdW�dWe2�ZOGdXdY�dYeN�ZPGdZd[�d[eO�ZQGd\d]�d]eN�ZRGd^d_�d_e<�ZSGd`da�dae<�ZTGdbdc�dce<�ZUGddde�deeB�ZVGdfdg�dge<�ZWGdhdi�die<�ZXGdjdk�dkeX�ZYGdldm�dmeX�ZZGdndo�doe<�Z[Gdpdq�dqe<�Z\Gdrds�dse<�Z]Gdtdu�due<�Z^Gdvdw�dwe<�Z_Gdxdy�dye<�Z`dzd{�Zad|d}�Zbd~d�Zcd�d��Zde=e>e?eBeFeGeHeIeJeKeReLeMeNePeSeTeUeVeWeYeZe[e\e]e^e_e`d��Zed�d�d��Zfd�d�d��ZgdS)�a
ASN.1 type classes for universal types. Exports the following items:

 - load()
 - Any()
 - Asn1Value()
 - BitString()
 - BMPString()
 - Boolean()
 - CharacterString()
 - Choice()
 - EmbeddedPdv()
 - Enumerated()
 - GeneralizedTime()
 - GeneralString()
 - GraphicString()
 - IA5String()
 - InstanceOf()
 - Integer()
 - IntegerBitString()
 - IntegerOctetString()
 - Null()
 - NumericString()
 - ObjectDescriptor()
 - ObjectIdentifier()
 - OctetBitString()
 - OctetString()
 - PrintableString()
 - Real()
 - RelativeOid()
 - Sequence()
 - SequenceOf()
 - Set()
 - SetOf()
 - TeletexString()
 - UniversalString()
 - UTCTime()
 - UTF8String()
 - VideotexString()
 - VisibleString()
 - VOID
 - Void()

Other type classes are defined that help compose the types listed above.
�)�unicode_literals�division�absolute_import�print_function)�datetime�	timedeltaN�)�_teletex_codec)�unwrap)�OrderedDict)�	type_name�str_cls�byte_cls�	int_types�chr_cls)�_parse�_dump_header)�int_to_bytes�int_from_bytes�timezone�extended_datetime�)�StringIOT)�BytesIOF�	universal�application�context�private)rr�rr)rrrrrrrrZ	primitiveZconstructed)rrz
^\d+(\.\d+)*$cCstj||d�S)a5
    Loads a BER/DER-encoded byte string and construct a universal object based
    on the tag value:

     - 1: Boolean
     - 2: Integer
     - 3: BitString
     - 4: OctetString
     - 5: Null
     - 6: ObjectIdentifier
     - 7: ObjectDescriptor
     - 8: InstanceOf
     - 9: Real
     - 10: Enumerated
     - 11: EmbeddedPdv
     - 12: UTF8String
     - 13: RelativeOid
     - 16: Sequence,
     - 17: Set
     - 18: NumericString
     - 19: PrintableString
     - 20: TeletexString
     - 21: VideotexString
     - 22: IA5String
     - 23: UTCTime
     - 24: GeneralizedTime
     - 25: GraphicString
     - 26: VisibleString
     - 27: GeneralString
     - 28: UniversalString
     - 29: CharacterString
     - 30: BMPString

    :param encoded_data:
        A byte string of BER or DER-encoded data

    :param strict:
        A boolean indicating if trailing data should be forbidden - if so, a
        ValueError will be raised when trailing data exists

    :raises:
        ValueError - when strict is True and trailing data is present
        ValueError - when the encoded value tag a tag other than listed above
        ValueError - when the ASN.1 header length is longer than the data
        TypeError - when encoded_data is not a byte string

    :return:
        An instance of the one of the universal classes
    )�strict)�	Asn1Value�load)�encoded_datar�r#�1/usr/lib/python3/dist-packages/asn1crypto/core.pyr!rs3r!c	@s�eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
ed$dd��Zd%dd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zd&dd�Zdd�Zdd�Zd'd d!�Zd(d"d#�ZdS))r z'
    The basis of all ASN.1 values
    NF�cKsDt|t�stdt|���d}|jdk	r,|}t||||d�\}}|S)a�
        Loads a BER/DER-encoded byte string using the current class as the spec

        :param encoded_data:
            A byte string of BER or DER-encoded data

        :param strict:
            A boolean indicating if trailing data should be forbidden - if so, a
            ValueError will be raised when trailing data exists

        :return:
            An instance of the current class
        z*encoded_data must be a byte string, not %sN)�spec�spec_paramsr)�
isinstancer�	TypeErrorr�tag�_parse_build)�clsr"r�kwargsr&�value�_r#r#r$r!�s

zAsn1Value.loadc
Cs4�y�|jtkrR|j}
|
jdk	r8t|
jdt�r8|
jf|
_t|
d�rJ|j�dt|
<|dk	r�t|t�rx|dkrpd}||f}|dkr�d}d}|dk	r�t|t�r�|dkr�d}||f}|dkr�d}d}|dk	�r
|dkr�d}|dkr�||f}n$|dkr�||f}nttdt	|����|dk	�r�t
|�d	k�r8t|d
t��r8|f}x�|D]�\}}d}t|t��rf|tk�r||}n|tk�rt|}t|}|dk	�r�ttdt	|����|dk	�r�t|t��s�t
tdt|����|jdk�r�||ff|_n|j||ff|_�q>Wn�|dk	�rb|\}}|tk�r"ttd
t	|����|dk	�rJt|t��sJt
tdt|����t||_||_d|_n@|dk	�r�|tk�r�ttdt	|����t||_|dk	�r�||_|�r�d|_|	dk	�r�|	|_n|dk	�r�|j|�WnXtt
fk
�r.}z6|jd
d�}
|jddt|�f|
|_|�WYdd}~XnXdS)a�
        The optional parameter is not used, but rather included so we don't
        have to delete it from the parameter dictionary when passing as keyword
        args

        :param explicit:
            An int tag number for explicit tagging, or a 2-element tuple of
            class and tag.

        :param implicit:
            An int tag number for implicit tagging, or a 2-element tuple of
            class and tag.

        :param no_explicit:
            If explicit tagging info should be removed from this instance.
            Used internally to allow contructing the underlying value that
            has been wrapped in an explicit tag.

        :param tag_type:
            None for normal values, or one of "implicit", "explicit" for tagged
            values. Deprecated in favor of explicit and implicit params.

        :param class_:
            The class for the value - defaults to "universal" if tag_type is
            None, otherwise defaults to "context". Valid values include:
             - "universal"
             - "application"
             - "context"
             - "private"
            Deprecated in favor of explicit and implicit params.

        :param tag:
            The integer tag to override - usually this is used with tag_type or
            class_. Deprecated in favor of explicit and implicit params.

        :param optional:
            Dummy parameter that allows "optional" key in spec param dicts

        :param default:
            The default value to use if the value is currently None

        :param contents:
            A byte string of the encoded contents of the value

        :raises:
            ValueError - when implicit, explicit, tag_type, class_ or tag are invalid values
        Nr�_setupTr�explicit�implicitzh
                        tag_type must be one of "implicit", "explicit", not %s
                        rrz�
                            explicit class must be one of "universal", "application",
                            "context", "private", not %s
                            zi
                                explicit tag must be an integer, not %s
                                z�
                        implicit class must be one of "universal", "application",
                        "context", "private", not %s
                        za
                            implicit tag must be an integer, not %s
                            z�
                            class_ must be one of "universal", "application",
                            "context", "private", not %s
                            z
    while constructing %s)�	__class__�_SETUP_CLASSESr1r(r�hasattrr0�
ValueErrorr
�repr�len�CLASS_NUM_TO_NAME_MAP�CLASS_NAME_TO_NUM_MAPr)r�class_r*r2�contents�set�args)�selfr1r2�no_explicit�tag_typer;r*�optional�defaultr<r,Z
invalid_class�er>r#r#r$�__init__�s�2






















zAsn1Value.__init__cCstr|j�S|j�SdS)z�
        Since str is different in Python 2 and 3, this calls the appropriate
        method, __unicode__() or __bytes__()

        :return:
            A unicode string
        N)�_PY2�	__bytes__�__unicode__)r?r#r#r$�__str__�s	zAsn1Value.__str__cCsDtr"dt|�t|�t|j��fSdt|�t|�t|j��fSdS)z7
        :return:
            A unicode string
        z<%s %s b%s>z
<%s %s %s>N)rFr�idr7�dump)r?r#r#r$�__repr__�szAsn1Value.__repr__cCs|j�jd�S)z�
        A fall-back method for print() in Python 2

        :return:
            A byte string of the output of repr()
        zutf-8)rL�encode)r?r#r#r$rG�szAsn1Value.__bytes__cCs|j�S)z�
        A fall-back method for print() in Python 3

        :return:
            A unicode string of the output of repr()
        )rL)r?r#r#r$rH�szAsn1Value.__unicode__cCs,|j�}|j|_|j|_|j|_|j|_|S)z�
        Constructs a new copy of the current object, preserving any tagging

        :return:
            An Asn1Value object
        )r3r;r*r2r1)r?�new_objr#r#r$�
_new_instance�szAsn1Value._new_instancecCs|j�}|j|tj�|S)z�
        Implements the copy.copy() interface

        :return:
            A new shallow copy of the current Asn1Value object
        )rO�_copy�copy)r?rNr#r#r$�__copy__�szAsn1Value.__copy__cCs&|j�}||t|�<|j|tj�|S)z�
        Implements the copy.deepcopy() interface

        :param memo:
            A dict for memoization

        :return:
            A new deep copy of the current Asn1Value object
        )rOrJrPrQ�deepcopy)r?�memorNr#r#r$�__deepcopy__�szAsn1Value.__deepcopy__cCs
tj|�S)z}
        Copies the object, preserving any special tagging from it

        :return:
            An Asn1Value object
        )rQrS)r?r#r#r$rQ�szAsn1Value.copycCs>t|t�s||i}|j|jd�|jd�d�}|j|tj�|S)a}
        Copies the object, applying a new tagging to it

        :param tagging:
            A dict containing the keys "explicit" and "implicit". Legacy
            API allows a unicode string of "implicit" or "explicit".

        :param tag:
            A integer tag number. Only used when tagging is a unicode string.

        :return:
            An Asn1Value object
        r1r2)r1r2)r(�dictr3�getrPrQrS)r?Ztaggingr*rNr#r#r$�retags

zAsn1Value.retagcCs|j�}|j|tj�|S)z{
        Copies the object, removing any special tagging from it

        :return:
            An Asn1Value object
        )r3rPrQrS)r?rNr#r#r$�untagszAsn1Value.untagcCs<|j|jkr$ttdt|�t|����|j|_||j�|_dS)a!
        Copies the contents of another Asn1Value object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        zQ
                Can not copy values from %s object to %s object
                N)r3r)r
rr<�_native)r?�other�	copy_funcr#r#r$rP"szAsn1Value._copyrcCs�d|}t|d�}t||�|r2|jj|d�nVt|d�rN|jj|d�n:trvt|jt�rvt	d|t
|j�f�nt	d||jf�dS)zJ
        Show the binary data and parsed data in a tree structure
        z  �parsedr�chosenz%s    Native: b%sz%s    Native: %sN)r5�_basic_debugr]�debugr^rFr(�nativer�printr7)r?�
nest_level�prefixZ
has_parsedr#r#r$r`:s


zAsn1Value.debugcCs�|j}|jdks|r�t|t�r*|jr*d|_t|j|j|j|j�}|j	dk	rvx*|j	D] \}}t|d|||j�|}qRW||_d|_
|j|S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        Nrrr%)r<�_headerr(�
Constructable�_indefinite�methodrr;r*r1�_trailer)r?�forcer<�headerr;r*r#r#r$rKPs
zAsn1Value.dump)F)	NNFNNNNNN)N)r)F)�__name__�
__module__�__qualname__�__doc__rhr;r*�_bad_tagr2r1rer<rirZ�classmethodr!rErIrLrGrHrOrRrUrQrXrYrPr`rKr#r#r#r$r �s:
6




r c@s eZdZdZdZdZdd�ZdS)�ValueMapzl
    Basic functionality that allows for mapping values from ints or OIDs to
    python unicode strings
    NcCsJ|j}|jdks|jdk	rdSi|_x |jj�D]\}}||j|<q0WdS)z2
        Generates _reverse_map from _map
        N)r3�_map�_reverse_map�items)r?r,�keyr.r#r#r$r0|szValueMap._setup)rlrmrnrorsrtr0r#r#r#r$rrnsrrc@seZdZdZdd�ZdS)�Castablez�
    A mixin to handle converting an object between different classes that
    represent the same encoded value, but with different rules for converting
    to and from native Python values
    cCs�|j|jjkr0ttdt|�t|�|j|jj���|�}|j|_|j|_|j|_|j|_|j	|_	|j
|_
t|t�r�|j
|_
|j|_|S)a+
        Converts the current object into an object of a different class. The
        new class must use the ASN.1 encoding for the value.

        :param other_class:
            The class to instantiate the new object from

        :return:
            An instance of the type other_class
        z�
                Can not covert a value from %s object to %s object since they
                use different tags: %d versus %d
                )r*r3r)r
rr;r2r1rer<rir(rfrhrg)r?Zother_classrNr#r#r$�cast�s$
z
Castable.castN)rlrmrnrorxr#r#r#r$rw�srwcs8eZdZdZdZdZdd�Zdd�Z�fdd	�Z�Z	S)
rfz�
    A mixin to handle string types that may be constructed from chunks
    contained within an indefinite length BER-encoded container
    FrcCsx|js|j�S|j}t|j�}d}x@||krbt|j||jd�\}}|dkrT|j�}q$||j�7}q$W|dkrt|j�S|S)zc
        :return:
            A concatenation of the native values of the contained chunks
        N)r&)rg�	_as_chunk�_chunks_offsetr8r<r+r3�
_merge_chunks)r?�pointer�contents_len�outputZ	sub_valuer#r#r$r{�s


zConstructable._merge_chunkscCs |jdkr|jS|j|jd�S)a
        A method to return a chunk of data that can be combined for
        constructed method values

        :return:
            A native Python value that can be added together. Examples include
            byte strings, unicode strings or tuples.
        rN)rzr<)r?r#r#r$ry�s

zConstructable._as_chunkcs&tt|�j||�|j|_|j|_dS)a%
        Copies the contents of another Constructable object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)�superrfrPrhrg)r?r[r\)r3r#r$rP�szConstructable._copy)
rlrmrnrorgrzr{ryrP�
__classcell__r#r#)r3r$rf�srfc@sJeZdZdZdZdd�Zdd�Zdd�Zd	d
�Ze	dd��Z
ddd�ZdS)�Voidz�
    A representation of an optional value that is not present. Has .native
    property and .dump() method to be compatible with other value classes.
    r%cCs|j|jkS)zu
        :param other:
            The other Primitive to compare to

        :return:
            A boolean
        )r3)r?r[r#r#r$�__eq__s	zVoid.__eq__cCsdS)NFr#)r?r#r#r$�__nonzero__szVoid.__nonzero__cCsdS)Nrr#)r?r#r#r$�__len__szVoid.__len__cCstf�S)N)�iter)r?r#r#r$�__iter__sz
Void.__iter__cCsdS)zn
        The a native Python datatype representation of this value

        :return:
            None
        Nr#)r?r#r#r$ras	zVoid.nativeFcCsdS)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        r%r#)r?rjr#r#r$rK#sz	Void.dumpN)F)rlrmrnror<r�r�r�r��propertyrarKr#r#r#r$r��sr�csZeZdZdZdZddd�Zedd��Zedd��Zdd	d
�Z	�fdd�Z
ddd�Z�ZS)�Anyz�
    A value class that can contain any value, and allows for easy parsing of
    the underlying encoded value using a spec. This is normally contained in
    a Structure that has an ObjectIdentifier field and _oid_pair and _oid_specs
    defined.
    NcKs�tj|f|�y@|dk	rLt|t�s4ttdt|����||jdf|_|j�|_	WnVt
tfk
r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnXdS)z�
        Sets the value of the object before passing to Asn1Value.__init__()

        :param value:
            An Asn1Value object that will be set as the parsed value
        Nz`
                        value must be an instance of Asn1Value, not %s
                        rrz
    while constructing %s)r rEr(r)r
rr3�_parsedrKr<r6r>)r?r.r-rDr>r#r#r$rE@s
zAny.__init__cCs|jdkr|j�|jdjS)z�
        The a native Python datatype representation of this value

        :return:
            The .native value from the parsed value object
        Nr)r��parsera)r?r#r#r$ra\s	
z
Any.nativecCs|jdkr|j�|jdS)zw
        Returns the parsed object from .parse()

        :return:
            The object returned by .parse()
        Nr)r�r�)r?r#r#r$r]js	
z
Any.parsedc	Cs|jdks"|jdd�||fk�ry�|p*i}t|�|jdk	rdd|krZ|j|d|d<n
|j|d<|j|j|j}t|||d�\}}|||f|_d|_d|_d|_d|_||_d|_WnXt	t
fk
�r}z6|jdd�}|jdd	t|�f||_|�WYdd}~XnX|jdS)
av
        Parses the contents generically, or using a spec with optional params

        :param spec:
            A class derived from Asn1Value that defines what class_ and tag the
            value should have, and the semantics of the encoded value. The
            return value will be of this type. If omitted, the encoded value
            will be decoded using the standard universal tag based on the
            encoded tag number.

        :param spec_params:
            A dict of params to pass to the spec object

        :return:
            An object of the type spec, or if not present, a child of Asn1Value
        Nrrr1)r&r'Fr%rz
    while parsing %s)
r��_tag_type_to_explicit_implicitr1rer<rir+r*r2r6r)r>r)	r?r&r'Z
passed_paramsr<�parsed_valuer/rDr>r#r#r$r�xs2"


z	Any.parsecs"tt|�j||�||j�|_dS)a
        Copies the contents of another Any object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�)r?r[r\)r3r#r$rP�sz	Any._copyFcCs$|jdkr|j�|jdj|d�S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        Nr)rj)r�r�rK)r?rjr#r#r$rK�s
zAny.dump)N)NN)F)
rlrmrnror�rEr�rar]r�rPrKr�r#r#)r3r$r�5s

2r�cs�eZdZdZdZdZdZdZdZdZ	e
ddd��Zdd�Zddd	�Z
ed
d��Zdd
�Zedd��Zedd��Zdd�Zdd�Z�fdd�Zddd�Z�ZS)�ChoicezF
    A class to handle when a value may be one of several options
    NFcKs2t|t�stdt|���t||||d�\}}|S)a�
        Loads a BER/DER-encoded byte string using the current class as the spec

        :param encoded_data:
            A byte string of BER or DER encoded data

        :param strict:
            A boolean indicating if trailing data should be forbidden - if so, a
            ValueError will be raised when trailing data exists

        :return:
            A instance of the current class
        z*encoded_data must be a byte string, not %s)r&r'r)r(rr)rr+)r,r"rr-r.r/r#r#r$r!�s
zChoice.loadcCsx|j}i|_i|_x`t|j�D]R\}}t|�dkrF|if}||j|<t|d|d�}||j|<||j|d<qWdS)zS
        Generates _id_map from _alternatives to allow validating contents
        rrrrN)r3�_id_map�	_name_map�	enumerate�
_alternativesr8�_build_id_tuple)r?r,�index�info�id_r#r#r$r0s


z
Choice._setupc	Ks�t|�tj|f|��y|jd�dk	r4ttd���|dk	�r$t|t�r�t|�dkrlttdt	|�t|����t
|j��d\}}t|t�r�t|�dkr�ttdt	|�t|����|d}|d}||j
kr�ttd	|t	|����|j
||_|j|j\}}}t||��s||f|�}n
t||�}||_WnXttfk
�r~}z6|jdd�}|jdd
t	|�f||_|�WYdd}~XnXdS)au
        Checks to ensure implicit tagging is not being used since it is
        incompatible with Choice, then forwards on to Asn1Value.__init__()

        :param name:
            The name of the alternative to be set - used with value.
            Alternatively this may be a dict with a single key being the name
            and the value being the value, or a two-element tuple of the the
            name and the value.

        :param value:
            The alternative value to set - used with name

        :raises:
            ValueError - when implicit param is passed (or legacy tag_type param is "implicit")
        r2Nz�
                    The Choice type can not be implicitly tagged even if in an
                    implicit module - due to its nature any tagging must be
                    explicit
                    rz�
                            When passing a dict as the "name" argument to %s,
                            it must have a single key/value - however %d were
                            present
                            rrz�
                            When passing a tuple as the "name" argument to %s,
                            it must have two elements, the name and value -
                            however %d were present
                            z�
                        The name specified, "%s", is not a valid alternative
                        for %s
                        z
    while constructing %s)r�r rErWr6r
r(rVr8r�listru�tupler��_choicer��_fix_taggingr�r)r>)	r?�namer.r-r/r&�paramsrDr>r#r#r$rEsJ





zChoice.__init__cCs|js|j|jd|_|jS)zc
        :return:
            A unicode string of the field name of the chosen alternative
        r)�_namer�r�)r?r#r#r$r�dszChoice.namecCs�|jdk	r|jSy,|j|j\}}}t|j||d�\|_}WnVttfk
r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnXdS)z}
        Parses the detected alternative

        :return:
            An Asn1Value object of the chosen alternative
        N)r&r'rrz
    while parsing %s)	r�r�r�r+r<r6r)r>r)r?r/r&r�rDr>r#r#r$r�ns
zChoice.parsecCs|j�S)zT
        :return:
            An Asn1Value object of the chosen alternative
        )r�)r?r#r#r$r^�sz
Choice.chosencCs|jjS)z�
        The a native Python datatype representation of this value

        :return:
            The .native value from the contained value object
        )r^ra)r?r#r#r$ra�s	z
Choice.nativecs||f}�jdk	rX�jd
|kr2ttdt�����t|t|��\\}}}}}}}||f}|�jkrr�j|�_dS�jdk	r��j	dk	r�t�j
�dkr�ttdt�����|�j�j	fkr�d�_dS�j||�}�fdd��jD�}ttd|t��d	j|����dS)a�
        Ensures that the class and tag specified exist as an alternative

        :param class_:
            The integer class_ from the encoded value header

        :param tag:
            The integer tag from the encoded value header

        :param contents:
            A byte string of the contents of the value - used when the object
            is explicitly tagged

        :raises:
            ValueError - when value is not a valid alternative
        Nrz�
                    %s was explicitly tagged, but the value provided does not
                    match the class and tag
                    z|
                    %s was implicitly tagged, but more than one alternative
                    exists
                    rcs g|]}�j|d|d��qS)rr)�_format_class_tag)�.0Zpair)r?r#r$�
<listcomp>�sz#Choice.validate.<locals>.<listcomp>zs
            Value %s did not match the class and tag of any of the alternatives
            in %s: %s
            z, ���)
r1r6r
rrr8r�r�r;r*r�r��join)r?r;r*r<r�r/Zasn1Zasn1sr#)r?r$�validate�s4

zChoice.validatecCsdt|j�|fS)zo
        :return:
            A unicode string of a human-friendly representation of the class and tag
        z[%s %s])r9�upper)r?r;r*r#r#r$r��szChoice._format_class_tagcs2tt|�j||�|j|_|j|_||j�|_dS)a
        Copies the contents of another Choice object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�r�r�)r?r[r\)r3r#r$rP�szChoice._copycCsl|jj|d�|_|jdks|r`d|_|jdk	r`x0|jD]&\}}t|d||j|j�|j|_q6W|j|jS)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        )rjNr%r)r^rKr<rer1r)r?rjr;r*r#r#r$rK�s
"zChoice.dump)F)NN)F)rlrmrnror�r�r�r�r�r�rqr!r0rEr�r�r�r^rar�r�rPrKr�r#r#)r3r$r��s&
T
	@r�c@s�eZdZdZdZdZed'dd��Zd(dd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd)dd�Zd*dd�Zedd��Zdd �Zd!d"�Zd#d$�Zd%d&�ZdS)+�Concata
    A class that contains two or more encoded child values concatentated
    together. THIS IS NOT PART OF THE ASN.1 SPECIFICATION! This exists to handle
    the x509.TrustedCertificate() class for OpenSSL certificates containing
    extra information.
    NFcCs|||d�S)a�
        Loads a BER/DER-encoded byte string using the current class as the spec

        :param encoded_data:
            A byte string of BER or DER encoded data

        :param strict:
            A boolean indicating if trailing data should be forbidden - if so, a
            ValueError will be raised when trailing data exists

        :return:
            A Concat object
        )r<rr#)r,r"rr#r#r$r!szConcat.loadc
Cs"|dk	r�ytt|�}g|_d}x<|jD]2}||krDt|||d�\}}n|�}|jj|�q$W|rz||krz||}td|��WnVttfk
r�}	z6|	jdd�}
|	jddt|�f|
|	_|	�WYdd}	~	XnX|dk	�r|jdkr�dgt|j�|_x"t	|�D]\}}|j
||��qWdS)a
        :param value:
            A native Python datatype to initialize the object value with

        :param contents:
            A byte string of the encoded contents of the value

        :param strict:
            A boolean indicating if trailing data should be forbidden - if so, a
            ValueError will be raised when trailing data exists in contents

        :raises:
            ValueError - when an error occurs with one of the children
            TypeError - when an error occurs with one of the children
        Nr)r|r&z4Extra data - %d bytes of trailing data were providedrz
    while constructing %s)r8�	_children�_child_specsr+�appendr6r)r>rr��__setitem__)
r?r.r<rr}�offsetr&Zchild_value�extra_bytesrDr>r��datar#r#r$rE"s,

zConcat.__init__cCstr|j�S|j�SdS)z�
        Since str is different in Python 2 and 3, this calls the appropriate
        method, __unicode__() or __bytes__()

        :return:
            A unicode string
        N)rFrGrH)r?r#r#r$rIOs	zConcat.__str__cCs|j�S)z;
        A byte string of the DER-encoded contents
        )rK)r?r#r#r$rG]szConcat.__bytes__cCst|�S)z7
        :return:
            A unicode string
        )r7)r?r#r#r$rHdszConcat.__unicode__cCsdt|�t|�t|j��fS)z7
        :return:
            A unicode string
        z
<%s %s %s>)rrJr7rK)r?r#r#r$rLlszConcat.__repr__cCs|j�}|j|tj�|S)z|
        Implements the copy.copy() interface

        :return:
            A new shallow copy of the Concat object
        )r3rPrQ)r?rNr#r#r$rRtszConcat.__copy__cCs&|j�}||t|�<|j|tj�|S)z�
        Implements the copy.deepcopy() interface

        :param memo:
            A dict for memoization

        :return:
            A new deep copy of the Concat object and all child objects
        )r3rJrPrQrS)r?rTrNr#r#r$rU�szConcat.__deepcopy__cCs
tj|�S)zQ
        Copies the object

        :return:
            A Concat object
        )rQrS)r?r#r#r$rQ�szConcat.copycCs4|j|jkr$ttdt|�t|����||j�|_dS)a
        Copies the contents of another Concat object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        zQ
                Can not copy values from %s object to %s object
                N)r3r)r
rr�)r?r[r\r#r#r$rP�szConcat._copyrcCsRd|}td|t|�t|�f�td|f�x|jD]}|j|d�q8WdS)zJ
        Show the binary data and parsed data in a tree structure
        z  z%s%s Object #%sz
%s  Children:rN)rbrrJr�r`)r?rcrd�childr#r#r$r`�s
zConcat.debugcCs(d}x|jD]}||j|d�7}qW|S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        r%)rj)r�rK)r?rjr<r�r#r#r$rK�szConcat.dumpcCs|j�S)z`
        :return:
            A byte string of the DER-encoded contents of the children
        )rK)r?r#r#r$r<�szConcat.contentscCs
t|j�S)z.
        :return:
            Integer
        )r8r�)r?r#r#r$r��szConcat.__len__cCs8|t|j�dks|dkr.ttd|t|����|j|S)z�
        Allows accessing children by index

        :param key:
            An integer of the child index

        :raises:
            KeyError - when an index is invalid

        :return:
            The Asn1Value object of the child specified
        rrzN
                No child is definition for position %d of %s
                )r8r��KeyErrorr
rr�)r?rvr#r#r$�__getitem__�szConcat.__getitem__cCsZ|t|j�dks|dkr.ttd|t|����t|t�sLttd|t|����||j|<dS)aG
        Allows settings children by index

        :param key:
            An integer of the child index

        :param value:
            An Asn1Value object to set the child to

        :raises:
            KeyError - when an index is invalid
            ValueError - when the value is not an instance of Asn1Value
        rrzK
                No child is defined for position %d of %s
                zz
                Value for child %s of %s is not an instance of
                asn1crypto.core.Asn1Value
                N)	r8r�r�r
rr(r r6r�)r?rvr.r#r#r$r��s
zConcat.__setitem__cCs
t|j�S)zB
        :return:
            An iterator of child values
        )r�r�)r?r#r#r$r�szConcat.__iter__)F)NNF)r)F)rlrmrnror�r�rqr!rErIrGrHrLrRrUrQrPr`rKr�r<r�r�r�r�r#r#r#r$r�s*
-


	$r�c@sDeZdZdZdZdZddd�Zdd�Zdd	d
�Zdd�Z	d
d�Z
dS)�	PrimitivezO
    Sets the class_ and method attributes for primitive, universal values
    rNcKs�tj|f|�y:|dk	r ||_n&|dk	r4|j|�n|dk	rF|j|�WnVttfk
r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnXdS)a^
        Sets the value of the object before passing to Asn1Value.__init__()

        :param value:
            A native Python datatype to initialize the object value with

        :param default:
            The default value if no value is specified

        :param contents:
            A byte string of the encoded contents of the value
        Nrrz
    while constructing %s)r rEr<r=r6r)r>r)r?r.rCr<r-rDr>r#r#r$rE-szPrimitive.__init__cCsHt|t�s"ttdt|�t|����||_||_d|_|jdkrDd|_dS)z_
        Sets the value of the object

        :param value:
            A byte string
        zH
                %s value must be a byte string, not %s
                Nr%)	r(rr)r
rrZr<reri)r?r.r#r#r$r=Ls

z
Primitive.setFcCs$|r|j}d|_|j|�tj|�S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        N)rar<r=r rK)r?rjrar#r#r$rKcs

zPrimitive.dumpcCs
||kS)Nr#)r?r[r#r#r$�__ne__vszPrimitive.__ne__cCs�t|t�sdS|j|jkrdS|jj|jjkr2dS|j|jkrN|j|jkrNdSt|jj�t|jg�Bttttg�}t|jj�t|jg�Bttttg�}||Br�|j|jkS|j	s�|j
s�|j	s�|j
r�|j�j�|j�j�kS|j�|j�kS)zu
        :param other:
            The other Primitive to compare to

        :return:
            A boolean
        FT)
r(r�r<r3r*r=�	__bases__r rrr2r1rYrK)r?r[Z
self_bases�other_basesr#r#r$r�ys	
&&zPrimitive.__eq__)NNN)F)rlrmrnror;rhrEr=rKr�r�r#r#r#r$r�$s

r�csDeZdZdZdZdZdd�Zdd�Z�fdd	�Ze	d
d��Z
�ZS)�AbstractStringz�
    A base class for all strings that have a known encoding. In general, we do
    not worry ourselves with confirming that the decoded values match a specific
    set of characters, only that they are decoded into a Python unicode string
    �latin1NcCsbt|t�s"ttdt|�t|����||_|j|j�|_d|_	|j
rNd|_
d|_|jdkr^d|_dS)zb
        Sets the value of the string

        :param value:
            A unicode string
        zK
                %s value must be a unicode string, not %s
                NFrr%)
r(r
r)r
r�_unicoderM�	_encodingr<rergrhri)r?r.r#r#r$r=�s

zAbstractString.setcCs0|jdkrdS|jdkr*|j�j|j�|_|jS)z7
        :return:
            A unicode string
        N�)r<r�r{�decoder�)r?r#r#r$rH�s


zAbstractString.__unicode__cstt|�j||�|j|_dS)a&
        Copies the contents of another AbstractString object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�)r?r[r\)r3r#r$rP�szAbstractString._copycCs|jdkrdS|j�S)z�
        The a native Python datatype representation of this value

        :return:
            A unicode string or None
        N)r<rH)r?r#r#r$ra�s	
zAbstractString.native)rlrmrnror�r�r=rHrPr�rar�r#r#)r3r$r��sr�c@s8eZdZdZdZdd�Zdd�Zdd�Zed	d
��Z	dS)�Booleanz7
    Represents a boolean in both ASN.1 and Python
    rcCs2t|�|_|sdnd|_d|_|jdkr.d|_dS)z�
        Sets the value of the object

        :param value:
            True, False or another value that works with bool()
        ���Nr%)�boolrZr<reri)r?r.r#r#r$r=�s


zBoolean.setcCs|j�S)z4
        :return:
            True or False
        )�__bool__)r?r#r#r$r�szBoolean.__nonzero__cCs
|jdkS)z4
        :return:
            True or False
        r�)r<)r?r#r#r$r�
szBoolean.__bool__cCs(|jdkrdS|jdkr"|j�|_|jS)z}
        The a native Python datatype representation of this value

        :return:
            True, False or None
        N)r<rZr�)r?r#r#r$ras
	


zBoolean.nativeN)
rlrmrnror*r=r�r�r�rar#r#r#r$r��sr�c@s0eZdZdZdZdd�Zdd�Zedd��Zd	S)
�Integerz8
    Represents an integer in both ASN.1 and Python
    rcCs�t|t�rP|jdkr&ttdt|����||jkrDttdt|�|���|j|}n"t|t�srttdt|�t|����|jr�||jkr�|j|n||_	t
|dd�|_d|_|j
dkr�d|_
dS)z�
        Sets the value of the object

        :param value:
            An integer, or a unicode string if _map is set

        :raises:
            ValueError - when an invalid value is passed
        Nz\
                    %s value is a unicode string, but no _map provided
                    zR
                    %s value, %s, is not present in the _map
                    z�
                %s value must be an integer or unicode string when a name_map
                is provided, not %s
                T)�signedr%)r(r
rsr6r
rrtrr)rZrr<reri)r?r.r#r#r$r=,s*



 
zInteger.setcCst|jdd�S)z1
        :return:
            An integer
        T)r�)rr<)r?r#r#r$�__int__\szInteger.__int__cCsL|jdkrdS|jdkrF|j�|_|jdk	rF|j|jkrF|j|j|_|jS)z|
        The a native Python datatype representation of this value

        :return:
            An integer or None
        N)r<rZr�rs)r?r#r#r$racs	


zInteger.nativeN)	rlrmrnror*r=r�r�rar#r#r#r$r�%s
0r�c@sTeZdZdZdZdZdZdZdd�Zdd�Z	d	d
�Z
dd�Zd
d�Ze
dd��ZdS)�	BitStringzK
    Represents a bit string from ASN.1 as a Python tuple of 1s and 0s
    rNrcCs2tj|�|j}|jdk	r.t|jj��d|_dS)z2
        Generates _reverse_map from _map
        Nr)rrr0r3rs�max�keys�_size)r?r,r#r#r$r0�s

zBitString._setupc
Cs"t|t�r�|jdkr&ttdt|����dg|j}||_x:td|j�D]*}|jj	|�}|dkr`qF||krFd||<qFWdj
tt|��}n�|j
tkr�|jdkr�||_n<t�|_x2t|�D]&\}}|r�|jj	||�}|jj|�q�Wdj
tt|��}nttdt|�t|����d|_|jdk	�rPt|�|jk�rFttdt|�|jt|����|jd�}t|�}|d	}d}	|dk�r�d	|}	|d|	7}ttj|d	��}
|	�r�t|	�}nd
}|dk�r�d}ntt|d��}t|�|
k�r�d
|
t|�|}|||_d|_|j�rd
|_d|_|jdk�rd|_dS)z�
        Sets the value of the object

        :param value:
            An integer or a tuple of integers 0 and 1

        :raises:
            ValueError - when an invalid value is passed
        NzF
                    %s._map has not been defined
                    rrr�z�
                %s value must be a tuple of ones and zeros or a set of unicode
                strings, not %s
                zf
                    %s value must be at most %s bits long, specified was %s long
                    �0�r�r%rF)r(r=rsr6r
rr�rZ�rangerWr��mapr
r3r�r��addr)�_chunkr8�rstrip�int�mathZceilrr<rergrhri)
r?r.�bitsr�rv�bitr��sizeZsize_mod�
extra_bitsZ
size_in_bytesZextra_bits_byteZvalue_bytesr#r#r$r=�st








z
BitString.setcCs�t|t�}|sJt|jt�s,ttdt|����||jkrJttdt|�|���|jdkrZ|j	|jdkr�t
|j�|dkr�t|j|�SdS|r�|jj||�}||jkS)aG
        Retrieves a boolean version of one of the bits based on a name from the
        _map

        :param key:
            The unicode string of one of the bit names

        :raises:
            ValueError - when _map is not set or the key name is invalid

        :return:
            A boolean if the bit is set
        zF
                    %s._map has not been defined
                    zT
                    %s._map does not contain an entry for "%s"
                    NrF)
r(rrsrVr6r
rrtrZrar8r�rW)r?rv�is_intr#r#r$r��s(



zBitString.__getitem__cCst|t�}|sH|jdkr*ttdt|����||jkrHttdt|�|���|jdkrX|j|jdkr�t	|j�}t
|�d}||kr�|jdg||�|r�dnd||<t|�|_nD|r�|jj
||�}|r�||jkr�|jj|�n||jkr�|jj|�|j|j�dS)a
        Sets one of the bits based on a name from the _map

        :param key:
            The unicode string of one of the bit names

        :param value:
            A boolean value

        :raises:
            ValueError - when _map is not set or the key name is invalid
        NzF
                    %s._map has not been defined
                    zT
                    %s._map does not contain an entry for "%s"
                    rr)r(rrsr6r
rrtrZrar�r8�extendr�rWr��remover=)r?rvr.r�Z
new_nativeZmax_keyr#r#r$r�!s8







zBitString.__setitem__cCs�t|jdd��}djt|jdd���}t|jdd��}t|�}|d}|dkrldd||}t|�}|d|kr�||d}dd||}|dkr�|dd|�}tttt|���S)zr
        Allows reconstructing indefinite length values

        :return:
            A tuple of integers
        rrz{0:b}Nr�r�)rr<�formatr8r�r�r�)r?r��
bit_stringZbyte_lenZbit_len�mod_bit_lenZ
missing_bytesr#r#r$ryZszBitString._as_chunkcCs�|jdkr,|jdkr |jf�n|jt��|jdkr�|j�}|jr�t�|_x:t|�D]&\}}|rV|jj||�}|jj|�qVWn||_|jS)z�
        The a native Python datatype representation of this value

        :return:
            If a _map is set, a set of names, or if no _map is set, a tuple of
            integers 1 and 0. None if no value.
        N)r<rsr=rZr{r�rWr�)r?r�r�r�r�r#r#r$raxs


zBitString.native)rlrmrnror*r�r�rzr0r=r�r�ryr�rar#r#r#r$r�vsd/9r�csHeZdZdZdZdZdZdd�Zdd�Z�fd	d
�Z	e
dd��Z�ZS)
�OctetBitStringzB
    Represents a bit string in ASN.1 as a Python byte string
    rrNcCs^t|t�s"ttdt|�t|����||_d||_d|_|jrJd|_d|_	|j
dkrZd|_
dS)z�
        Sets the value of the object

        :param value:
            A byte string

        :raises:
            ValueError - when an invalid value is passed
        zH
                %s value must be a byte string, not %s
                r�NFrr%)r(rr)r
r�_bytesr<rergrhri)r?r.r#r#r$r=�s


zOctetBitString.setcCs(|jdkrdS|jdkr"|j�|_|jS)z4
        :return:
            A byte string
        Nr%)r<r�r{)r?r#r#r$rG�s



zOctetBitString.__bytes__cstt|�j||�|j|_dS)a&
        Copies the contents of another OctetBitString object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�)r?r[r\)r3r#r$rP�szOctetBitString._copycCs|jdkrdS|j�S)z
        The a native Python datatype representation of this value

        :return:
            A byte string or None
        N)r<rG)r?r#r#r$ra�s	
zOctetBitString.native)
rlrmrnror*rzr�r=rGrPr�rar�r#r#)r3r$r��sr�c@s4eZdZdZdZdZdd�Zdd�Zedd	��Z	d
S)�IntegerBitStringz>
    Represents a bit string in ASN.1 as a Python integer
    rrcCsft|t�s"ttdt|�t|����||_dt|dd�|_d|_|j	rRd|_	d|_
|jdkrbd|_dS)	z�
        Sets the value of the object

        :param value:
            An integer

        :raises:
            ValueError - when an invalid value is passed
        zE
                %s value must be an integer, not %s
                r�T)r�NFrr%)r(rr)r
rrZrr<rergrhri)r?r.r#r#r$r=�s

zIntegerBitString.setcCsjt|jdd��}djt|jdd���}t|�d}|dkrNdd||}|dkrf|dd|�S|S)z�
        Allows reconstructing indefinite length values

        :return:
            A unicode string of bits - 1s and 0s
        rrz{0:b}Nr�r�)rr<r�r8)r?r�r�r�r#r#r$ry	szIntegerBitString._as_chunkcCs||jdkrdS|jdkrvt|jdd��}|jrP|dkrPt|jdd��|_n&|jrf|dkrftd��t|j�d�|_|jS)z|
        The a native Python datatype representation of this value

        :return:
            An integer or None
        Nrrz=Constructed bit string has extra bits on indefinite containerr)r<rZrrgr6r�r{)r?r�r#r#r$ra*	s	

zIntegerBitString.nativeN)
rlrmrnror*rzr=ryr�rar#r#r#r$r��sr�csDeZdZdZdZdZdd�Zdd�Z�fdd	�Ze	d
d��Z
�ZS)�OctetStringz;
    Represents a byte string in both ASN.1 and Python
    �NcCsZt|t�s"ttdt|�t|����||_||_d|_|jrFd|_d|_	|j
dkrVd|_
dS)z_
        Sets the value of the object

        :param value:
            A byte string
        zH
                %s value must be a byte string, not %s
                NFrr%)r(rr)r
rr�r<rergrhri)r?r.r#r#r$r=L	s

zOctetString.setcCs(|jdkrdS|jdkr"|j�|_|jS)z4
        :return:
            A byte string
        Nr%)r<r�r{)r?r#r#r$rGf	s



zOctetString.__bytes__cstt|�j||�|j|_dS)a#
        Copies the contents of another OctetString object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�)r?r[r\)r3r#r$rPr	szOctetString._copycCs|jdkrdS|j�S)z
        The a native Python datatype representation of this value

        :return:
            A byte string or None
        N)r<rG)r?r#r#r$ra�	s	
zOctetString.native)rlrmrnror*r�r=rGrPr�rar�r#r#)r3r$r�B	sr�c@s(eZdZdZdZdd�Zedd��ZdS)�IntegerOctetStringz?
    Represents a byte string in ASN.1 as a Python integer
    r�cCsbt|t�s"ttdt|�t|����||_t|dd�|_d|_|j	rNd|_	d|_
|jdkr^d|_dS)z�
        Sets the value of the object

        :param value:
            An integer

        :raises:
            ValueError - when an invalid value is passed
        zE
                %s value must be an integer, not %s
                F)r�Nrr%)r(rr)r
rrZrr<rergrhri)r?r.r#r#r$r=�	s

zIntegerOctetString.setcCs,|jdkrdS|jdkr&t|j��|_|jS)z|
        The a native Python datatype representation of this value

        :return:
            An integer or None
        N)r<rZrr{)r?r#r#r$ra�	s
	

zIntegerOctetString.nativeN)rlrmrnror*r=r�rar#r#r#r$r��	sr�csneZdZdZdZdZddd�Zdd�Zddd�Zd	d
�Z	�fdd�Z
ed
d��Zedd��Z
ddd�Z�ZS)�ParsableOctetStringr�NcKsVd}|dkr*|dk	r*t|t�r*|j�}d}tj|fd|i|��|rR||jdf|_dS)a�
        Allows providing a parsed object that will be serialized to get the
        byte string value

        :param value:
            A native Python datatype to initialize the object value with

        :param parsed:
            If value is None and this is an Asn1Value object, this will be
            set as the parsed value, and the value will be obtained by calling
            .dump() on this object.
        FNTr.)r(r rKr�rEr3r�)r?r.r]r-Z
set_parsedr#r#r$rE�	szParsableOctetString.__init__cCsZt|t�s"ttdt|�t|����||_||_d|_|jrFd|_d|_	|j
dkrVd|_
dS)z_
        Sets the value of the object

        :param value:
            A byte string
        zH
                %s value must be a byte string, not %s
                NFrr%)r(rr)r
rr�r<rergrhri)r?r.r#r#r$r=�	s

zParsableOctetString.setcCsL|jdks |jdd�||fkrBt|j�||d�\}}|||f|_|jdS)av
        Parses the contents generically, or using a spec with optional params

        :param spec:
            A class derived from Asn1Value that defines what class_ and tag the
            value should have, and the semantics of the encoded value. The
            return value will be of this type. If omitted, the encoded value
            will be decoded using the standard universal tag based on the
            encoded tag number.

        :param spec_params:
            A dict of params to pass to the spec object

        :return:
            An object of the type spec, or if not present, a child of Asn1Value
        Nrr)r&r'r)r�r+rG)r?r&r'r�r/r#r#r$r�
s zParsableOctetString.parsecCs(|jdkrdS|jdkr"|j�|_|jS)z4
        :return:
            A byte string
        Nr%)r<r�r{)r?r#r#r$rG
s



zParsableOctetString.__bytes__cs*tt|�j||�|j|_||j�|_dS)a+
        Copies the contents of another ParsableOctetString object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�r�)r?r[r\)r3r#r$rP#
szParsableOctetString._copycCs0|jdkrdS|jdk	r$|jdjS|j�SdS)z
        The a native Python datatype representation of this value

        :return:
            A byte string or None
        Nr)r<r�rarG)r?r#r#r$ra3
s
	

zParsableOctetString.nativecCs|jdkr|j�|jdS)zw
        Returns the parsed object from .parse()

        :return:
            The object returned by .parse()
        Nr)r�r�)r?r#r#r$r]D
s	
zParsableOctetString.parsedFcCs>|r4|jdk	r|jj|d�}n|j}d|_|j|�tj|�S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        N)rj)r�r]rKrar<r=r )r?rjrar#r#r$rKR
s

zParsableOctetString.dump)NN)NN)F)rlrmrnr*r�r�rEr=r�rGrPr�rar]rKr�r#r#)r3r$r��	s

r�c@seZdZdZdZdd�ZdS)�ParsableOctetBitStringrrcCs^t|t�s"ttdt|�t|����||_d||_d|_|jrJd|_d|_	|j
dkrZd|_
dS)z�
        Sets the value of the object

        :param value:
            A byte string

        :raises:
            ValueError - when an invalid value is passed
        zH
                %s value must be a byte string, not %s
                r�NFrr%)r(rr)r
rr�r<rergrhri)r?r.r#r#r$r=s
s


zParsableOctetBitString.setN)rlrmrnr*rzr=r#r#r#r$r�i
sr�c@s,eZdZdZdZdZdd�Zedd��ZdS)	�Nullz<
    Represents a null value in ASN.1 as None in Python
    �r%cCs
d|_dS)zV
        Sets the value of the object

        :param value:
            None
        r%N)r<)r?r.r#r#r$r=�
szNull.setcCsdS)zn
        The a native Python datatype representation of this value

        :return:
            None
        Nr#)r?r#r#r$ra�
s	zNull.nativeN)	rlrmrnror*r<r=r�rar#r#r#r$r��
s

r�c@sXeZdZdZdZdZedd��Zedd��Zdd	�Z	d
d�Z
edd
��Zedd��Z
dS)�ObjectIdentifierz`
    Represents an object identifier in ASN.1 as a Python unicode dotted
    integer string
    �NcCsF|jdkrttdt|����t|t�s8ttdt|����|jj||�S)a^
        Converts a dotted unicode string OID into a mapped unicode string

        :param value:
            A dotted unicode string OID

        :raises:
            ValueError - when no _map dict has been defined on the class
            TypeError - when value is not a unicode string

        :return:
            A mapped unicode string
        Nz>
                %s._map has not been defined
                zH
                value must be a unicode string, not %s
                )rsr6r
rr(r
r)rW)r,r.r#r#r$r��
s

zObjectIdentifier.mapcCs�|tkr|�j�dt|<|jdkr6ttdt|����t|t�sRttdt|����||j	krf|j	|St
j|�s�ttdt|�|���|S)a�
        Converts a mapped unicode string value into a dotted unicode string OID

        :param value:
            A mapped unicode string OR dotted unicode string OID

        :raises:
            ValueError - when no _map dict has been defined on the class or the value can't be unmapped
            TypeError - when value is not a unicode string

        :return:
            A dotted unicode string OID
        TNz>
                %s._map has not been defined
                zH
                value must be a unicode string, not %s
                zL
                %s._map does not contain an entry for "%s"
                )r4r0rsr6r
rr(r
r)rt�_OID_RE�match)r,r.r#r#r$�unmap�
s&





zObjectIdentifier.unmapcCs�t|t�s"ttdt|�t|����||_|jdk	rF||jkrF|j|}d|_d}x�t	|j
d��D]�\}}t|�}|dkr�|}q`n|dkr�|d|}td|@�}|d	?}x(|dkr�td
d|@B�|}|d	?}q�W|j|7_q`Wd|_
|jdkr�d|_dS)a
        Sets the value of the object

        :param value:
            A unicode string. May be a dotted integer string, or if _map is
            provided, one of the mapped values.

        :raises:
            ValueError - when an invalid value is passed
        zK
                %s value must be a unicode string, not %s
                Nr%�.rr�(���)r(r
r)r
rrZrsrtr<r��splitr�rreri)r?r.�firstr��partZencoded_partr#r#r$r=s6





zObjectIdentifier.setcCs|jS)z7
        :return:
            A unicode string
        )�dotted)r?r#r#r$rHBszObjectIdentifier.__unicode__cCs�|jdkr�g}d}x~|jD]t}tr*t|�}|d}||d@7}|d@dkrt|�dkr||jt|d��|jt|d��n|jt|��d}qWdj|�|_|jS)z�
        :return:
            A unicode string of the object identifier in dotted notation, thus
            ignoring any mapped value
        Nrr�r�r�r�)�_dottedr<rF�ordr8r�r
r�)r?r~r�Zbyter#r#r$r�Js 
zObjectIdentifier.dottedcCsJ|jdkrdS|jdkrD|j|_|jdk	rD|j|jkrD|j|j|_|jS)aE
        The a native Python datatype representation of this value

        :return:
            A unicode string or None. If _map is not defined, the unicode string
            is a string of dotted integers. If _map is defined and the dotted
            string is present in the _map, the mapped value is returned.
        N)r<rZr�rs)r?r#r#r$rags

zObjectIdentifier.native)rlrmrnror*r�rqr�r�r=rHr�r�rar#r#r#r$r��
s"22r�c@seZdZdZdZdS)�ObjectDescriptorzO
    Represents an object descriptor from ASN.1 - no Python implementation
    r�N)rlrmrnror*r#r#r#r$r�|sr�c@seZdZdZdZdS)�
InstanceOfzF
    Represents an instance from ASN.1 - no Python implementation
    r�N)rlrmrnror*r#r#r#r$r��sr�c@seZdZdZdZdS)�RealzH
    Represents a real number from ASN.1 - no Python implementation
    �	N)rlrmrnror*r#r#r#r$r��sr�c@s(eZdZdZdZdd�Zedd��ZdS)�
Enumeratedz\
    Represents a enumerated list of integers from ASN.1 as a Python
    unicode string
    �
cCs�t|t�r0t|t�r0ttdt|�t|����t|t�rd||jkrXttdt|�|���|j|}n||jkr�ttdt|�|���t	j
||�dS)z�
        Sets the value of the object

        :param value:
            An integer or a unicode string from _map

        :raises:
            ValueError - when an invalid value is passed
        zY
                %s value must be an integer or a unicode string, not %s
                zL
                    %s value "%s" is not a valid value
                    zB
                %s value %s is not a valid value
                N)r(rr
r)r
rrtr6rsr�r=)r?r.r#r#r$r=�s$


zEnumerated.setcCs.|jdkrdS|jdkr(|j|j�|_|jS)z�
        The a native Python datatype representation of this value

        :return:
            A unicode string or None
        N)r<rZrsr�)r?r#r#r$ra�s
	

zEnumerated.nativeN)rlrmrnror*r=r�rar#r#r#r$r��s+r�c@seZdZdZdZdZdS)�
UTF8StringzI
    Represents a UTF-8 string from ASN.1 as a Python unicode string
    �zutf-8N)rlrmrnror*r�r#r#r#r$r��sr�c@seZdZdZdZdS)�RelativeOidz`
    Represents an object identifier in ASN.1 as a Python unicode dotted
    integer string
    �
N)rlrmrnror*r#r#r#r$r��sr�cseZdZdZdZdZdZdZdZdZ	gZ
dZdZdZ
dZdZdZdZd.dd�Zed	d
��Zejdd
��Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd/dd�Zdd�Zdd�Zd d!�Z d0d"d#�Z!d$d%�Z"ed&d'��Z#�fd(d)�Z$d1d*d+�Z%d2d,d-�Z&�Z'S)3�Sequencezf
    Represents a sequence of fields from ASN.1 as a Python object with a
    dict-like interface
    �rrNFcKs�tj|f|�d}|dkrL|dk	rLd}|jdkrH|jdkr@d}n|j�|}|dk	�r�y�|jr|dd�|jD�}t|j��}n|j�}t|�}xl|D]d}|r�|j|}|t	|j�kr�|j|t
k	r�||kr�|j|�q�||kr�|j|||�|j|�q�Wt	|��r&t
tdt|�djtt|������WnXt
tfk
�r�}	z6|	jdd�}
|	jd	d
t|�f|
|	_|	�WYdd}	~	XnXdS)a"
        Allows setting field values before passing everything else along to
        Asn1Value.__init__()

        :param value:
            A native Python datatype to initialize the object value with

        :param default:
            The default value if no value is specified
        FNTcSsg|]}|d�qS)rr#)r�r�r#r#r$r�Asz%Sequence.__init__.<locals>.<listcomp>z�
                        One or more unknown fields was passed to the constructor
                        of %s: %s
                        z, rrz
    while constructing %s)r rE�childrenr<�_parse_children�_fieldsr=r��
_field_mapr8�VOIDr�r�r6r
rr��sortedr�r)r>)r?r.rCr-Zcheck_existingr�Zunused_keysrvr�rDr>r#r#r$rE#sF






zSequence.__init__cCs&|jdkr|jS|j�r |j�|jS)z`
        :return:
            A byte string of the DER-encoded contents of the sequence
        N)r��	_contents�_is_mutated�
_set_contents)r?r#r#r$r<ds

zSequence.contentscCs
||_dS)ze
        :param value:
            A byte string of the DER-encoded contents of the sequence
        N)r)r?r.r#r#r$r<sscCsD|j}|jdk	r@x.|jD]$}t|t�s0t|t�r|p:|j�}qW|S)z~
        :return:
            A boolean - if the sequence or any children (recursively) have been
            mutated
        N)�_mutatedr�r(r��
SequenceOfr)r?�mutatedr�r#r#r$r|s
zSequence._is_mutatedcCs*|j|}|jtkr&t|�}|j|<|S)z]
        Builds a child object if the child has only been parsed into a tuple so far
        )r�r3r��_build)r?r�r�r#r#r$�_lazy_child�s

zSequence._lazy_childcCs|jdkr|j�t|j�S)z.
        :return:
            Integer
        N)r�r�r8)r?r#r#r$r��s
zSequence.__len__cCs�|jdkr|j�t|t�sD||jkr:ttd|t|����|j|}|t|j�krfttd|t|����y
|j	|�St
tfk
r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnXdS)a9
        Allows accessing fields by name or index

        :param key:
            A unicode string of the field name, or an integer of the field index

        :raises:
            KeyError - when a field name or index is invalid

        :return:
            The Asn1Value object of the field specified
        NzL
                    No field named "%s" defined for %s
                    zL
                No field numbered %s is present in this %s
                rrz
    while parsing %s)
r�r�r(rrr�r
rr8rr6r)r>)r?rvrDr>r#r#r$r��s(




zSequence.__getitem__c
Cs�|jdkr|j�t|t�sD||jkr:ttd|t|����|j|}|j|�\}}}}}|j	|||||�}d}	t|t
�r�|jdk}	n"t|t�r�|j
jdk}	n
|jdk}	|	r�ttd|t|����||j|<|jdk	r�|j|j|j|j|d<d|_dS)a�
        Allows settings fields by name or index

        :param key:
            A unicode string of the field name, or an integer of the field index

        :param value:
            A native Python datatype to set the field value to. This method will
            construct the appropriate Asn1Value object from _fields.

        :raises:
            ValueError - when a field name or index is invalid
        NzL
                    No field named "%s" defined for %s
                    FzG
                Value for field "%s" of %s is not set
                rT)r�r�r(rrr�r
r�_determine_spec�_make_valuer�r]r�r^r<r6rZrarr)
r?rvr.�
field_name�
field_spec�
value_spec�field_paramsr/�	new_valueZ
invalid_valuer#r#r$r��s4








zSequence.__setitem__cCs�|jdkr|j�t|t�sD||jkr:ttd|t|����|j|}|j|\}}}|sjd|kr~d|kr~t	td|t|����d|kr�t
|j|<|jdk	r�d|j|<n|j|d�d|_
dS)a/
        Allows deleting optional or default fields by name or index

        :param key:
            A unicode string of the field name, or an integer of the field index

        :raises:
            ValueError - when a field name or index is invalid, or the field is not optional or defaulted
        NzL
                    No field named "%s" defined for %s
                    rCrBz�
                Can not delete the value for the field "%s" of %s since it is
                not optional or defaulted
                T)r�r�r(rrr�r
rrr6rrZr�r)r?rvr�r/r�r#r#r$�__delitem__
s*





zSequence.__delitem__ccsx|jD]}|dVqWdS)zE
        :return:
            An iterator of field key names
        rN)r)r?r�r#r#r$r�4
szSequence.__iter__cCs�|jdkr|j�t�}x�t|j�D]�\}}|j|}|dkrDd}nH|jtkr�|rf|j|�j|d�}q�|d|d|d}n|j|d�}|dr�d|dkr�|d	f|d�}|j�|kr�q$|j	|�q$W|j
�|_d|_|j
dkr�d|_
dS)
a
        Updates the .contents attribute of the value with the encoded value of
        all of the child objects

        :param force:
            Ensure all contents are in DER format instead of possibly using
            cached BER-encoded data
        Nr%)rjrr�r�rrCr)r�r�rr�rr3r�rrK�write�getvaluerreri)r?rjr<r�r�r�Z
child_dumpZ
default_valuer#r#r$r=
s*





zSequence._set_contentscCs,|j}i|_g|_g|_x^t|j�D]P\}}t|�dkrL|if}||j|<||j|d<|jjt|d|d��q$W|j	dk	r�|j|j	d|j|j	df|_
x�t|j�D]t\}}|jdk	o�|d|jk}|j
dk	o�|j
d|k}|s�|r�|jjd�q�|jj|d|d|d|ddf�q�WdS)zS
        Generates _field_map, _field_ids and _oid_nums for use in parsing
        rrrrN)r3r�
_field_ids�_precomputed_specsr�rr8r�r��	_oid_pair�	_oid_nums�_spec_callbacks)r?r,r��field�has_callback�
is_mapped_oidr#r#r$r0b
s$


"zSequence._setupc	Cs�|j|\}}}|}d}|jdk	r�||jkr�|j|}||�}|r�|jtkrrt|�dkrr|\}}|dkr�|}d}q�|dkr�|}|}d}q�|}nB|jdk	r�|jd|kr�|j|jd�j}||jkr�|j|}|}|||||fS)a
        Determine how a value for a field should be constructed

        :param index:
            The field number

        :return:
            A tuple containing the following elements:
             - unicode string of the field name
             - Asn1Value class of the field spec
             - Asn1Value class of the value spec
             - None or dict of params to pass to the field spec
             - None or Asn1Value class indicating the value spec was derived from an OID or a spec callback
        Nrrr)	rrr3r�r8rrra�
_oid_specs)	r?r�r�rrr�
spec_override�callbackZoidr#r#r$r}
s.


zSequence._determine_specc
Cs |dkrd|krtS||k}t|t�}t|t�r�t|t�sNttd|t|����t||�s~|�}|j	|j
|j|j�||_
|}	n|}	n�t||�r�|}	|r�|	j|�np|s�|r�t||�r�||f|�}	nLt||�r�|}	n||�}	|o�|�r|fd|	j�i|��}|	|	jdf|_
|}	t|	|�}	|	S)a&
        Contructs an appropriate Asn1Value object for a field

        :param field_name:
            A unicode string of the field name

        :param field_spec:
            An Asn1Value class that is the field spec

        :param value_spec:
            An Asn1Value class that is the vaue spec

        :param field_params:
            None or a dict of params for the field spec

        :param value:
            The value to construct an Asn1Value object from

        :return:
            An instance of a child class of Asn1Value
        NrBz�
                    Can not set a native python value to %s, which has the
                    choice type of %s - value must be an instance of Asn1Value
                    r.)r�
issubclassr�r�r(r r6r
rr�r;r*r<r�r�rKr3r�)
r?rrrrr.Zspecs_differentZis_any�wrapperrr#r#r$r
�
s>






zSequence._make_valuecCs<|j}|jdkr�|jr�tgt|j�|_xpt|j�D]b\}\}}}d|kr4|j|rj|j|\}}}}	}n|j|�\}}}}	}|j	||||	d�|j|<q4WdS�y>g|_t|j�}
d}d}t|j�}
d}||
k}�x�|�rb|dkr�t
|j|
|d�\}}||
k}||
k�rB|j|�p|j|�\}}}}	}|	�r�d|	k�sBd|	k�r�|j||d|dfk�r�|tk�r�d}t
|t��r�y,|f|	�}|j|d|d|d�d	}Wntk
�r�YnX|�s�d|	k�r�|jjt�n|jj|f|	��|d
7}d	}q�|dk�s|�rt
|t��r|}d}|�r4|||	|f}n|||	f}n�|
dk�r|d
|
k�rg}|d
}x\|dk�r�|j|}t|�dk�r�Pd|dk�s�d|dk�r�|j|d�|d
8}�qhWt|�d
k�r�dnd
}dj|�}ttd|d
tj|d�tj|d
�|d||���n|}|�rHt|�}t|ttf��rH|jd	d�|jj|�|d
7}d}q�Wt|j�}xl||
k�r�|j|\}}}	d|	k�r�|jj|f|	��n&d|	k�r�|jjt�nttd|���|d
7}�qpWWnXttfk
�r6}z6|jd
d�}|jddt|�f||_|�WYdd}~XnXdS)aQ
        Parses the contents and generates Asn1Value objects based on the
        definitions from _fields.

        :param recurse:
            If child objects that are Sequence or SequenceOf objects should
            be recursively parsed

        :raises:
            ValueError - when an error occurs parsing child objects
        NrCr)r|rBrFr�Trr�sr�z, z�
                        Data for field %s (%s class, %s method, tag %s) does
                        not match the field definition%s of %s
                        )�recursezV
                        Field "%s" is missing from structure
                        z
    while parsing %s)r3rrrr8r�r�rrr
rrr�r!r�r�r6r�r�r
r9rW�METHOD_NUM_TO_NAME_MAPr
r(r�rr�r)r>r)r?r$r,r�r/r�rrrr�contents_length�
child_pointerrZ	field_len�partsZagainrZchoice_matchZtesterr�Z
missed_fieldsZ
prev_fieldZprev_field_infoZpluralZmissed_field_namesr�rDr>r#r#r$r��
s�






 &







zSequence._parse_childrencCsZt|t�sttdt|����|jdkr>ttdt|�t|����|j|}|j	|�}|dS)a
        Determines the spec to use for the field specified. Depending on how
        the spec is determined (_oid_pair or _spec_callbacks), it may be
        necessary to set preceding field values before calling this. Usually
        specs, if dynamic, are controlled by a preceding ObjectIdentifier
        field.

        :param field_name:
            A unicode string of the field name to get the spec for

        :return:
            A child class of asn1crypto.core.Asn1Value that the field must be
            encoded using
        zM
                field_name must be a unicode string, not %s
                Nz�
                Unable to retrieve spec for field %s in the class %s because
                _fields has not been set
                r)
r(r
r)r
rrr6r7rr)r?rr�r�r#r#r$r&|s



z
Sequence.speccCs|jdkrdS|jdk�r|jdkr0|jdd�y|t�|_xnt|j�D]`\}}|jtkrjt|�}||j|<y|j	|d}Wnt
k
r�t|�}YnX|j|j|<qFWWnXt
tfk
�r}z6|jdd�}|jddt|�f||_|�WYdd}~XnX|jS)z�
        The a native Python datatype representation of this value

        :return:
            An OrderedDict or None. If an OrderedDict, all child values are
            recursively converted to native representation also.
        NT)r$rrz
    while parsing %s)r<rZr�r�rr�r3r�r
r�
IndexErrorr
rar6r)r>r)r?r�r�r�rDr>r#r#r$ra�s*




zSequence.nativecs^tt|�j||�|jdk	rZg|_x6|jD],}|jtkrF|jj|�q*|jj|j��q*WdS)a 
        Copies the contents of another Sequence object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rr�rPr�r3r�r�rQ)r?r[r\r�)r3r#r$rP�s

zSequence._copycCsl|jdkr|j�d|}t||�xB|D]:}|j|j|�}|tk	r*td||f�|j|d�q*WdS)zJ
        Show the binary data and parsed data in a tree structure
        Nz  z%s    Field "%s"r)r�r�r_rrrrbr`)r?rcrdrr�r#r#r$r`�s


zSequence.debugcCsx|r|j|d�|jrn|jdk	rnxLt|j�D]>\}\}}}|j|tk	rJq,d|ks,d|kr\q,ttd|���q,Wtj|�S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        )rjNrCrBzN
                    Field "%s" is missing from structure
                    )	rrr�r�rr6r
r rK)r?rjr�rr/r�r#r#r$rK�sz
Sequence.dump)NN)F)F)r)F)(rlrmrnror*r;rhr�rrrrrrrrrrrEr�r<�setterrrr�r�r�rr�rr0rr
r�r&rarPr`rKr�r#r#)r3r$r��sH
A	
.9-	
%/I
'!
r�cs�eZdZdZdZdZdZdZdZdZ	dZ
d.dd�Zed	d
��Z
e
jdd
��Z
dd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd/d d!�Zd0d"d#�Zd$d%�Zed&d'��Z�fd(d)�Zd1d*d+�Zd2d,d-�Z�Z S)3rz�
    Represents a sequence (ordered) of a single type of values from ASN.1 as a
    Python object with a list-like interface
    r�rrNFc
Ks�|r
||_tj|f|�yd|dk	r*||_nP|dkr>|dk	r>|}|dk	rzx t|�D]\}}|j||�qPW|jdkrz|j�WnVttfk
r�}z6|j	dd�}	|j	ddt
|�f|	|_	|�WYdd}~XnXdS)a
        Allows setting child objects and the _child_spec via the spec parameter
        before passing everything else along to Asn1Value.__init__()

        :param value:
            A native Python datatype to initialize the object value with

        :param default:
            The default value if no value is specified

        :param contents:
            A byte string of the encoded contents of the value

        :param spec:
            A class derived from Asn1Value to use to parse children
        Nrrz
    while constructing %s)�_child_specr rEr<r�r�rr6r)r>r)
r?r.rCr<r&r-r�r�rDr>r#r#r$rE s"
zSequenceOf.__init__cCs&|jdkr|jS|j�r |j�|jS)z`
        :return:
            A byte string of the DER-encoded contents of the sequence
        N)r�rrr)r?r#r#r$r<Ks

zSequenceOf.contentscCs
||_dS)ze
        :param value:
            A byte string of the DER-encoded contents of the sequence
        N)r)r?r.r#r#r$r<ZscCsD|j}|jdk	r@x.|jD]$}t|t�s0t|t�r|p:|j�}qW|S)z~
        :return:
            A boolean - if the sequence or any children (recursively) have been
            mutated
        N)rr�r(r�rr)r?r	r�r#r#r$rcs
zSequenceOf._is_mutatedcCs*|j|}|jtkr&t|�}||j|<|S)z]
        Builds a child object if the child has only been parsed into a tuple so far
        )r�r3r�r
)r?r�r�r#r#r$rrs



zSequenceOf._lazy_childcCs�t||j�r|}n�t|jt�rBt|t�r.|}q�ttdt|����nrt|jt�r�t|t�spttdt|�|jj	���t||j�s�|j�}|j
|j|j|j
�||_|}|}n|j|d�Si}|jjr�|jj|d<|jjr�|jj|jjf|d<t||�S)a
        Constructs a _child_spec value from a native Python data type, or
        an appropriate Asn1Value object

        :param value:
            A native Python value, or some child of Asn1Value

        :return:
            An object of type _child_spec
        z�
                    Can not set a native python value to %s where the
                    _child_spec is Any - value must be an instance of Asn1Value
                    z�
                    Can not set a native python value to %s where the
                    _child_spec is the choice type %s - value must be an
                    instance of Asn1Value
                    )r.r1r2)r(r+r!r�r r6r
rr�rlr�r;r*r<r�r1r2r�)r?r.rr"r�r#r#r$r
}s6

zSequenceOf._make_valuecCs|jdkr|j�t|j�S)z1
        :return:
            An integer
        N)r�r�r8)r?r#r#r$r��s
zSequenceOf.__len__cCs|jdkr|j�|j|�S)zm
        Allows accessing children via index

        :param key:
            Integer index of child
        N)r�r�r)r?rvr#r#r$r��s	
zSequenceOf.__getitem__cCs||jdkr|j�|j|�}|t|j�krL|jjd�|jdk	rL|jjd�||j|<|jdk	rr|j|j|j|<d|_dS)z�
        Allows overriding a child via index

        :param key:
            Integer index of child

        :param value:
            Native python datatype that will be passed to _child_spec to create
            new child object
        NT)r�r�r
r8r�rZrar)r?rvr.rr#r#r$r��s




zSequenceOf.__setitem__cCs>|jdkr|j�|jj|�|jdk	r4|jj|�d|_dS)zk
        Allows removing a child via index

        :param key:
            Integer index of child
        NT)r�r��poprZr)r?rvr#r#r$r�s	

zSequenceOf.__delitem__ccs<|jdkr|j�x$tdt|j��D]}|j|�Vq$WdS)zA
        :return:
            An iter() of child objects
        Nr)r�r�r�r8r)r?r�r#r#r$r��s
zSequenceOf.__iter__cCs^|dks|tkrdSt||j�s@ttdt|�t|j�t|����x|D]}||krFdSqFWdS)z�
        :param item:
            An object of the type cls._child_spec

        :return:
            A boolean if the item is contained in this SequenceOf
        NFzy
                Checking membership in %s is only available for instances of
                %s, not %s
                T)rr(r+r)r
r)r?�itemr�r#r#r$�__contains__s	
zSequenceOf.__contains__cCsL|jdkr|j�|jj|j|��|jdk	rB|jj|jdj�d|_dS)z�
        Allows adding a child to the end of the sequence

        :param value:
            Native python datatype that will be passed to _child_spec to create
            new child object
        NrTr�)r�r�r�r
rZrar)r?r.r#r#r$r�(s


zSequenceOf.appendcCs\|jdkr|j�t�}x|D]}|j|j|d��qW|j�|_d|_|jdkrXd|_dS)z�
        Encodes all child objects into the contents for this object

        :param force:
            Ensure all contents are in DER format instead of possibly using
            cached BER-encoded data
        N)rjr%)	r�r�rrrKrrreri)r?rjr<r�r#r#r$r<s	



zSequenceOf._set_contentscCs�y�g|_|jdkrdSt|j�}d}xj||kr�t|j||d�\}}|jrV||jf}n|}|r�t|�}t|ttf�r�|j	dd�|jj
|�q&WWnVttfk
r�}z6|j
dd�}|j
ddt|�f||_
|�WYdd}~XnXdS)aU
        Parses the contents and generates Asn1Value objects based on the
        definitions from _child_spec.

        :param recurse:
            If child objects that are Sequence or SequenceOf objects should
            be recursively parsed

        :raises:
            ValueError - when an error occurs parsing child objects
        Nr)r|T)r$rz
    while parsing %s)r�rr8rr+r
r(r�rr�r�r6r)r>r)r?r$r&r'r(r�rDr>r#r#r$r�Ps(



zSequenceOf._parse_childrencCs|jS)z�
        Determines the spec to use for child values.

        :return:
            A child class of asn1crypto.core.Asn1Value that child values must be
            encoded using
        )r+)r?r#r#r$r&ss	zSequenceOf.speccCs�|jdkrdS|jdkr�|jdkr.|jdd�ydd�|D�|_WnVttfk
r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnX|jS)	z�
        The a native Python datatype representation of this value

        :return:
            A list or None. If a list, all child values are recursively
            converted to native representation also.
        NT)r$cSsg|]
}|j�qSr#)ra)r�r�r#r#r$r��sz%SequenceOf.native.<locals>.<listcomp>rrz
    while parsing %s)r<rZr�r�r6r)r>r)r?rDr>r#r#r$ra~s



zSequenceOf.nativecs^tt|�j||�|jdk	rZg|_x6|jD],}|jtkrF|jj|�q*|jj|j��q*WdS)a"
        Copies the contents of another SequenceOf object to itself

        :param object:
            Another instance of the same class

        :param copy_func:
            An reference of copy.copy() or copy.deepcopy() to use when copying
            lists, dicts and objects
        N)rrrPr�r3r�r�rQ)r?r[r\r�)r3r#r$rP�s

zSequenceOf._copycCsD|jdkr|j�d|}t||�x|D]}|j|d�q*WdS)zJ
        Show the binary data and parsed data in a tree structure
        Nz  r)r�r�r_r`)r?rcrdr�r#r#r$r`�s


zSequenceOf.debugcCs|r|j|d�tj|�S)a
        Encodes the value using DER

        :param force:
            If the encoded contents already exist, clear them and regenerate
            to ensure they are in DER format instead of BER format

        :return:
            A byte string of the DER-encoded value
        )rj)rr rK)r?rjr#r#r$rK�szSequenceOf.dump)NNNN)F)F)r)F)!rlrmrnror*r;rhr�rrr+rEr�r<r*rrr
r�r�r�rr�r.r�rr�r&rarPr`rKr�r#r#)r3r$rs8
+	7


#

rc@s<eZdZdZdZdZdZdZdd�Zd
d	d
�Z	ddd�Z
dS)�Setzm
    Represents a set of fields (unordered) from ASN.1 as a Python object with a
    dict-like interface
    rr�NcCs*|j}i|_i|_g|_x\t|j�D]N\}}t|�dkrL|if}||j|<||j|d<||jt|d|d�<q$W|jdk	r�|j|jd|j|jdf|_	x�t|j�D]t\}}|j
dk	o�|d|j
k}|j	dk	o�|j	d|k}|s�|r�|jjd�q�|jj|d|d|d|ddf�q�WdS)zS
        Generates _field_map, _field_ids and _oid_nums for use in parsing
        rrrrN)r3rrrr�rr8r�rrrr�)r?r,r�rrrr#r#r$r0�s$


"z
Set._setupFcCs>|j}|jdkr�|jr�tgt|j�|_xpt|j�D]b\}\}}}d|kr4|j|rj|j|\}}}}	}n|j|�\}}}}	}|j	||||	d�|j|<q4WdS�y@i}
t|j
�}d}d}
�x||k�r�t|j
||d�\}}|d|df}|jj
|�}|dk�r,ttd|
tj
|d�tj
|d�|d���|j|�p@|j|�\}}}}	}|dk�sh|�rpt|t��rp|}d}|�r�|||	|f}n|||	f}|�r�t|�}t|ttf��r�|jdd	�||
|<|
d7}
q�Wt|j�}x�td|�D]�}||
k�r��q�|j|�p|j|�\}}}}	}|dk�s4|�r<t|t��r<|}d}d
}|	�sLd}nFd|	k�rfd|	k�rfd}n,d|	k�rzt|
|<nd|	k�r�|f|	�|
|<|�r�ttd|t|�����q�Wg|_x$td|�D]}|jj|
|��q�WWnXttfk
�r8}z6|jdd�}|jdd
t|�f||_|�WYdd}~XnXdS)aQ
        Parses the contents and generates Asn1Value objects based on the
        definitions from _fields.

        :param recurse:
            If child objects that are Sequence or SequenceOf objects should
            be recursively parsed

        :raises:
            ValueError - when an error occurs parsing child objects
        NrCr)r|rz�
                        Data for field %s (%s class, %s method, tag %s) does
                        not match any of the field definitions
                        rT)r$FrBzU
                        Missing required field "%s" from %s
                        z
    while parsing %s)r3rrrr8r�r�rrr
r<rrrWr6r
r9r%r!r�r
r(r�rr�r�rr�r)r>)r?r$r,r�r/r�rrrrZ	child_mapr&r'Z
seen_fieldr(r�rrr�Ztotal_fieldsr�ZmissingrDr>r#r#r$r��s�




 

 


zSet._parse_childrenc	Cs�|jdkr|j�g}x^t|j�D]P\}}|j|d�}|j|\}}}d|krb|f|�j�|krbq"|j|j|f�q"W|jdd�d�djdd	�|D��|_	d|_
|jdkr�d|_dS)
ac
        Encodes all child objects into the contents for this object.

        This method is overridden because a Set needs to be encoded by
        removing defaulted fields and then sorting the fields by tag.

        :param force:
            Ensure all contents are in DER format instead of possibly using
            cached BER-encoded data
        N)rjrCcSs|dS)Nrr#)�ctr#r#r$�<lambda>|sz#Set._set_contents.<locals>.<lambda>)rvr%cSsg|]}|d�qS)rr#)r�r1r#r#r$r�~sz%Set._set_contents.<locals>.<listcomp>)r�r�r�rKrr�r*�sortr�rreri)	r?rjZchild_tag_encodingsr�r�Zchild_encodingr�r&rr#r#r$rbs

zSet._set_contents)F)F)rlrmrnrorhr;r*rr0r�rr#r#r#r$r/�s
or/c@seZdZdZdZddd�ZdS)�SetOfz~
    Represents a set (unordered) of a single type of values from ASN.1 as a
    Python object with a list-like interface
    r0FcCs`|jdkr|j�g}x|D]}|j|j|d��qWdjt|��|_d|_|jdkr\d|_dS)aD
        Encodes all child objects into the contents for this object.

        This method is overridden because a SetOf needs to be encoded by
        sorting the child encodings.

        :param force:
            Ensure all contents are in DER format instead of possibly using
            cached BER-encoded data
        N)rjr%)	r�r�r�rKr�rrreri)r?rjZchild_encodingsr�r#r#r$r�s


zSetOf._set_contentsN)F)rlrmrnror*rr#r#r#r$r4�sr4c@seZdZdZdZdS)�EmbeddedPdvz
    A sequence structure
    �N)rlrmrnror*r#r#r#r$r5�sr5c@seZdZdZdZdZdS)�
NumericStringzK
    Represents a numeric string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$r7�sr7c@seZdZdZdZdZdS)�PrintableStringzM
    Represents a printable string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$r9�sr9c@seZdZdZdZdZdS)�
TeletexStringzK
    Represents a teletex string from ASN.1 as a Python unicode string
    �ZteletexN)rlrmrnror*r�r#r#r#r$r;�sr;c@seZdZdZdZdS)�VideotexStringzI
    Represents a videotex string from ASN.1 as a Python byte string
    �N)rlrmrnror*r#r#r#r$r=�sr=c@seZdZdZdZdZdS)�	IA5StringzH
    Represents an IA5 string from ASN.1 as a Python unicode string
    ��asciiN)rlrmrnror*r�r#r#r#r$r?�sr?c@seZdZdZedd��ZdS)�AbstractTimezK
    Represents a time from ASN.1 as a Python datetime.datetime object
    cCs�|jdkrdS|jdkr�t|�}tjd|�}|sV|jd�}|j|�}|jtj	d�|_nl|j|dd
��}t
|dd��}t
|d
d��}tt|�|d	�}|dkr�||8}n||7}|jtj	d�|_|jS)z�
        The a native Python datatype representation of this value

        :return:
            A datetime.datetime object in the UTC timezone or None
        Nz[-\+]�Z)�tzinforr�r�r)�hours�minutes���������rI)
r<rZr
�re�searchr��_date_by_len�replacerZutcr�r�abs)r?�stringZhas_timezoneZdaterErFZdeltar#r#r$ra�s$	




zAbstractTime.nativeN)rlrmrnror�rar#r#r#r$rB�srBc@s$eZdZdZdZdd�Zdd�ZdS)�UTCTimezV
    Represents a UTC time from ASN.1 as a Python datetime.datetime object in UTC
    �cCs8t|t�r"|jd�}tr"|jd�}tj||�d|_dS)z�
        Sets the value of the object

        :param value:
            A unicode string or a datetime.datetime object

        :raises:
            ValueError - when an invalid value is passed
        z
%y%m%d%H%M%SZrAN)r(r�strftimerFr�r�r=rZ)r?r.r#r#r$r=s


zUTCTime.setcCs^t|�}t|dd��}|dkr&d}nd}|dkrBtj||d�S|dkrZtj||d	�S|S)
z�
        Parses a date from a string based on its length

        :param string:
            A unicode string to parse

        :return:
            A datetime.datetime object or a unicode string
        rr�2Z20Z19r�z
%Y%m%d%H%Mr�z%Y%m%d%H%M%S)r8r�r�strptime)r?rO�strlenZyear_numrdr#r#r$rL#szUTCTime._date_by_lenN)rlrmrnror*r=rLr#r#r#r$rPsrPc@s$eZdZdZdZdd�Zdd�ZdS)�GeneralizedTimez�
    Represents a generalized time from ASN.1 as a Python datetime.datetime
    object or asn1crypto.util.extended_datetime object in UTC
    �cCs<t|ttf�r&|jd�}tr&|jd�}tj||�d|_dS)a
        Sets the value of the object

        :param value:
            A unicode string, a datetime.datetime object or an
            asn1crypto.util.extended_datetime object

        :raises:
            ValueError - when an invalid value is passed
        z
%Y%m%d%H%M%SZrAN)	r(rrrRrFr�r�r=rZ)r?r.r#r#r$r=Gs

zGeneralizedTime.setc	Cs�t|�}d}|dkrd}n(|dkr(d}n|dkr6d}n|dkrBd	}|r�t|�d
kr�|dd
�dkr�tjd
|d
d�|�}td|j|j|j|j|j|j	|j
�Stj||�S|S)a
        Parses a date from a string based on its length

        :param string:
            A unicode string to parse

        :return:
            A datetime.datetime object, asn1crypto.util.extended_datetime object or
            a unicode string
        Nr�z%Y%m%d%Hr�z
%Y%m%d%H%M�z%Y%m%d%H%M%Sr8z%Y%m%d%H%M%S.%fr�rZ0000Z2000)r8rrTrZmonthZdayZhourZminute�secondZmicrosecondrD)r?rOrUZdate_format�tr#r#r$rL]s0zGeneralizedTime._date_by_lenN)rlrmrnror*r=rLr#r#r#r$rV?srVc@seZdZdZdZdZdS)�
GraphicStringzK
    Represents a graphic string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$r[�sr[c@seZdZdZdZdZdS)�
VisibleStringzK
    Represents a visible string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$r]�sr]c@seZdZdZdZdZdS)�
GeneralStringzK
    Represents a general string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$r_�sr_c@seZdZdZdZdZdS)�UniversalStringzM
    Represents a universal string from ASN.1 as a Python unicode string
    �z	utf-32-beN)rlrmrnror*r�r#r#r#r$ra�srac@seZdZdZdZdZdS)�CharacterStringzM
    Represents a character string from ASN.1 as a Python unicode string
    �r�N)rlrmrnror*r�r#r#r#r$rc�srcc@seZdZdZdZdZdS)�	BMPStringzG
    Represents a BMP string from ASN.1 as a Python unicode string
    �z	utf-16-beN)rlrmrnror*r�r#r#r#r$re�srecCs4td|t|�t|�f�|jrBtd|tj|jp2d�jd�f�|jdk	o^|jdk	o^|j	dk	}|r|t
j|j�}tj|j�}|j
dk	r�x*|j
D] \}}td|tj|�|f�q�W|r�td||||j	f�n>|jr�|r�td||||j	f�n|�rtd	||||j	f�td
|tj|j�p d�jd�f�dS)a7
    Prints out basic information about an Asn1Value object. Extracted for reuse
    among different classes that customize the debug information.

    :param prefix:
        A unicode string of spaces to prefix output line with

    :param self:
        The object to print the debugging information about
    z%s%s Object #%sz%s  Header: 0x%sr%zutf-8Nz#%s    %s tag %s (explicitly tagged)z%s      %s %s %sz&%s    %s %s tag %s (implicitly tagged)z%s    %s %s tag %sz%s  Data: 0x%s)rbrrJre�binasciiZhexlifyr�rhr;r*r%rWr9r1r2r<)rdr?Z
has_headerZmethod_name�
class_namer;r*r#r#r$r_�s,"
r_cCspd|krl|ddkr.|jdd�|df|d<n$|ddkrR|jdd�|df|d<|d=|d=d|krl|d=dS)z�
    Converts old-style "tag_type" and "tag" params to "explicit" and "implicit"

    :param params:
        A dict of parameters to convert from tag_type/tag to explicit/implicit
    rAr1�classrr*r2N)rW)r�r#r#r$r��sr�cCs�t|�d}d|kr$|jdk	rxd}nTt|dt�r@|d\}}n|d}d}|jdkr\d}n|jt|kst|j|krxd}|jd�|jkr�d}|r�|j	|�S|S)z�
    Checks if a value is properly tagged based on the spec, and re/untags as
    necessary

    :param value:
        An Asn1Value object

    :param params:
        A dict of spec params

    :return:
        An Asn1Value that is properly tagged
    Fr2Trr1)
r�r2r(r�r;r:r*rWr1rX)r.r�rXr;r*r#r#r$r�s$


r�cCs�|dkrdS|j}|j}t|�d|krRt|dt�rD|d\}}q�d}|d}n0d|kr�t|dt�rv|d\}}nd}|d}|dk	r�t|t�r�t|}|jd|�}|jd|�}||fS)aP
    Builds a 2-element tuple used to identify fields by grabbing the class_
    and tag from an Asn1Value class and the params dict being passed to it

    :param params:
        A dict of params to pass to spec

    :param spec:
        An Asn1Value class

    :return:
        A 2-element integer tuple in the form (class_, tag)
    Nr1rr2r;r*)NN)r;r*r�r(r�rr:rW)r�r&Zrequired_classZrequired_tagr#r#r$r�(s(
r�)rrrr�r�r�r�r�r�r�r6r�r�r�r0r8r:r<r>r@rQrWr\r^r`rbrdrfc	Cs�|dk	rt|�|dkrtSd}	|dk	�r|o4d|k}
|
oL|jsP|oLd|k�rz|r`|f|�}n|�}|j}t|�}
|}|}|}|}|}|p�d}x�|
D]�\}}||kr�ttdt|�tj|�tj||����|dkr�ttdt|�t	jd�t	j||����||k�rttd	t|�||���t
|t|��\}}|\}}}}}}||7}||}q�Wt||dd
id��}||_
||_||_d
}	�qT|�r�|fd|i|��}n
||d
�}|tk�r��qTt|t��r>|j|||�y||j|_d}|j�WnXttfk
�r:}z6|jdd�}|jddt|�f||_|�WYdd}~XnXn�||jk�rpttdt|�tj|j�tj||����||jk�r�|dk�o�|jdk�o�|dk}|�s�t|t��r�ttdt|�t	j|j�t	j||����n||_d
|_||jk�rT||jk�rTttdt|�|j|����n<|�r�d|k�r�tfd|i|��}|j}|}|}|�pPd}xHt|�D]<\}}t
|t|��\}}|\}}}}}}||7}||}�q\Wt||dd
id��}||j
|_
|j|�p�d7_||_d
}	nx|tk�rttdtj|�t	j|�|���t|}|||d�}|dk�o4|jdk�o4|dk}|�rNt|t��rNd
|_||_|	�sl||_
|�phd|_d|_|�r�y|j|�WnXttfk
�r�}z6|jdd�}|jddt|�f||_|�WYdd}~XnX|S)a
    Builds an Asn1Value object generically, or using a spec with optional params

    :param class_:
        An integer representing the ASN.1 class

    :param method:
        An integer representing the ASN.1 method

    :param tag:
        An integer representing the ASN.1 tag

    :param header:
        A byte string of the ASN.1 header (class, method, tag, length)

    :param contents:
        A byte string of the ASN.1 value

    :param trailer:
        A byte string of any ASN.1 trailer (only used by indefinite length encodings)

    :param spec:
        A class derived from Asn1Value that defines what class_ and tag the
        value should have, and the semantics of the encoded value. The
        return value will be of this type. If omitted, the encoded value
        will be decoded using the standard universal tag based on the
        encoded tag number.

    :param spec_params:
        A dict of params to pass to the spec object

    :param nested_spec:
        For certain Asn1Value classes (such as OctetString and BitString), the
        contents can be further parsed and interpreted as another Asn1Value.
        This parameter controls the spec for that sub-parsing.

    :return:
        An object of the type spec, or if not specified, a child of Asn1Value
    NFr@r1r%z�
                        Error parsing %s - explicitly-tagged class should have been
                        %s, but %s was found
                        rz�
                        Error parsing %s - explicitly-tagged method should have
                        been %s, but %s was found
                        z�
                        Error parsing %s - explicitly-tagged tag should have been
                        %s, but %s was found
                        T)r&r'r<)r<rz
    while parsing %sz�
                        Error parsing %s - class should have been %s, but %s was
                        found
                        szy
                            Error parsing %s - method should have been %s, but %s was found
                            zn
                        Error parsing %s - tag should have been %s, but %s was found
                        zO
                Unknown element - %s class, %s method, tag %s
                )r<r;) r�rr1�reversedr6r
rr9rWr%rr8r
rerir�r(r�r�r<r�r)r>r;rhrfrgr*rpr �_UNIVERSAL_SPECSrZ)r;rhr*rkr<Ztrailerr&r'Znested_specZ
header_setr@r.Zoriginal_explicitZ
explicit_infoZparsed_classZ
parsed_methodZ
parsed_tagZto_parseZexplicit_headerZexplicit_trailerZexpected_classZexpected_tagr�r/Z
parsed_headerZparsed_trailerrDr>Z	ber_indef�original_valuer#r#r$r
us�)







r
c	CsTt|�}t|||�\}}|r@|||kr@|||}td|��t|||d��|fS)a�
    Parses a byte string generically, or using a spec with optional params

    :param encoded_data:
        A byte string that contains BER-encoded data

    :param pointer:
        The index in the byte string to parse from

    :param spec:
        A class derived from Asn1Value that defines what class_ and tag the
        value should have, and the semantics of the encoded value. The
        return value will be of this type. If omitted, the encoded value
        will be decoded using the standard universal tag based on the
        encoded tag number.

    :param spec_params:
        A dict of params to pass to the spec object

    :param strict:
        A boolean indicating if trailing data should be forbidden - if so, a
        ValueError will be raised when trailing data exists

    :return:
        A 2-element tuple:
         - 0: An object of the type spec, or if not specified, a child of Asn1Value
         - 1: An integer indicating how many bytes were consumed
    z4Extra data - %d bytes of trailing data were provided)r&r')r8rr6r
)	r"r|r&r'rZencoded_lenr�Znew_pointerr�r#r#r$r+Wsr+)r)F)NNN)rNNF)hroZ
__future__rrrrrrrgrQr�rJ�sysr�r	Z_errorsr
Z_ordereddictrZ_typesrr
rrr�parserrr�utilrrrr�version_infoZ	cStringIOrrZxranger�rF�io�registerr9r:r%�compiler�r4r!�objectr rrrwrfr�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr/r4r5r7r9r;r=r?rBrPrVr[r]r_rarcrer_r�r�r�rkr
r+r#r#r#r$�<module>/s


6I,G6:#{Q5Q"XTN5%)LD		$E;!				.8I
	
	
	,(.
c

Zerion Mini Shell 1.0