o
    q¨ÊhLf ã                   @   sþ  d Z ddlmZmZmZmZ ddlmZmZ ddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZmZ ddlmZmZmZm Z  ej!dkrnddl"m#Z$ e%Z&dZ'nddl(m$Z$ dZ'e )¡  dddddœZ*dddddddddœZ+dddœZ,e -d¡Z.i Z/d–dd„Z0G dd „ d e1ƒZ2G d!d"„ d"ƒZ3G d#d$„ d$e1ƒZ4G d%d&„ d&e1ƒZ5G d'd(„ d(e2ƒZ6e6ƒ Z7G d)d*„ d*e2ƒZ8G d+d,„ d,e2ƒZ9G d-d.„ d.e1ƒZ:G d/d0„ d0e2ƒZ;G d1d2„ d2e5e;ƒZ<G d3d4„ d4e;ƒZ=G d5d6„ d6e;e3ƒZ>G d7d8„ d8e5e4e;e3e1ƒZ?G d9d:„ d:e5e4e;ƒZ@G d;d<„ d<e5e4e;ƒZAG d=d>„ d>e5e4e;ƒZBG d?d@„ d@e5e4e;ƒZCG dAdB„ dBe5e4e;ƒZDG dCdD„ dDeDƒZEG dEdF„ dFe;ƒZFG dGdH„ dHe;e3ƒZGG dIdJ„ dJe;ƒZHG dKdL„ dLe;ƒZIG dMdN„ dNe;ƒZJG dOdP„ dPe>ƒZKG dQdR„ dRe<ƒZLG dSdT„ dTeGƒZMG dUdV„ dVe2ƒZNG dWdX„ dXe2ƒZOG dYdZ„ dZeNƒZPG d[d\„ d\eOƒZQG d]d^„ d^eNƒZRG d_d`„ d`e<ƒZSG dadb„ dbe<ƒZTG dcdd„ dde<ƒZUG dedf„ dfeBƒZVG dgdh„ dhe<ƒZWG didj„ dje<ƒZXG dkdl„ dleXƒZYG dmdn„ dneXƒZZG dodp„ dpe<ƒZ[G dqdr„ dre<ƒZ\G dsdt„ dte<ƒZ]G dudv„ dve<ƒZ^G dwdx„ dxe<ƒZ_G dydz„ dze<ƒZ`d{d|„ Zad}d~„ Zbdd€„ Zcd�d‚„ Zdi de=“de>“de?“dƒeB“d„eF“d…eG“d†eH“d‡eI“dˆeJ“d‰eK“dŠeR“d‹eL“dŒeM“d�eN“dŽeP“d�eS“d�eT“eUeVeWeYeZe[e\e]e^e_e`d‘œ¥Zed—d’d“„Zfd˜d”d•„ZgdS )™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)r   r   é   r   r   r   )r   r   r   r   r   r   r   r   Ú	primitiveÚconstructed©r   r   z^\d+(\.\d+)*$c                 C   s   t j| |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&   úA/var/www/html/env/lib/python3.10/site-packages/asn1crypto/core.pyr$   r   s   3r$   c                   @   sÂ   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZed$dd„ƒZ		d%dd„Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd&dd„Zdd„ Zdd„ Zd'd d!„Zd$d"d#„ZdS )(r#   z'
    The basis of all ASN.1 values
    NFó    c                 K   sD   t |tƒstdt|ƒ ƒ‚d}| jdu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
        ú*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   

zAsn1Value.loadc
              
   C   s  �zb| j tvr)| j }
|
jdurt|
jd tƒr|
jf|
_t|
dƒr%|  ¡  dt|
< |durDt|tƒr<|du r8d}||f}|dkrDd}d}|dur_t|tƒrW|du rSd}||f}|dkr_d}d}|dur„|du rid}|dkrr||f}n|dkr{||f}n	ttdt	|ƒƒƒ‚|durìt
|ƒd	kr˜t|d
 tƒr˜|f}|D ]P\}}d}t|tƒr¬|tvr«|}n
|tvr²|}t| }|durÃttdt	|ƒƒƒ‚|durÕt|tƒsÕttdt|ƒƒƒ‚| jdu rá||ff| _qš| j||ff | _qšnW|du�r#|\}}|tv�rttdt	|ƒƒƒ‚|du�rt|tƒ�sttdt|ƒƒƒ‚t| | _|| _d| _n |du�r;|tv�r6ttdt	|ƒƒƒ‚t| | _|du�rC|| _|�rId| _|	du�rT|	| _W dS |du�ra|  |¡ W dS W dS  ttf�yŠ } z|jd
d… }|jd dt| ƒ  f| |_|‚d}~ww )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
                        r   r   z¬
                            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
                            ú
    while constructing %s)Ú	__class__Ú_SETUP_CLASSESr6   r-   r   Úhasattrr5   Ú
ValueErrorr
   ÚreprÚlenÚCLASS_NUM_TO_NAME_MAPÚCLASS_NAME_TO_NUM_MAPr.   r   Úclass_r/   r7   ÚcontentsÚsetÚargs)Úselfr6   r7   Úno_explicitÚtag_typerA   r/   ÚoptionalÚdefaultrB   r1   Úinvalid_classÚerD   r&   r&   r'   Ú__init__è   sÈ   2






ü
€û
ü
ä

û
ü


û



ÿ€ýzAsn1Value.__init__c                 C   ó   t r|  ¡ S |  ¡ S ©z²
        Since str is different in Python 2 and 3, this calls the appropriate
        method, __unicode__() or __bytes__()

        :return:
            A unicode string
        ©Ú_PY2Ú	__bytes__Ú__unicode__©rE   r&   r&   r'   Ú__str__ž  ó   	zAsn1Value.__str__c                 C   s@   t rdt| ƒt| ƒt|  ¡ ƒf S dt| ƒt| ƒt|  ¡ ƒf S )ú7
        :return:
            A unicode string
        z<%s %s b%s>ú
<%s %s %s>)rP   r   Úidr=   ÚdumprS   r&   r&   r'   Ú__repr__¬  s   zAsn1Value.__repr__c                 C   s   |   ¡  d¡S )z€
        A fall-back method for print() in Python 2

        :return:
            A byte string of the output of repr()
        úutf-8)rZ   ÚencoderS   r&   r&   r'   rQ   ·  s   zAsn1Value.__bytes__c                 C   ó   |   ¡ S )zƒ
        A fall-back method for print() in Python 3

        :return:
            A unicode string of the output of repr()
        )rZ   rS   r&   r&   r'   rR   Á  s   zAsn1Value.__unicode__c                 C   s,   |   ¡ }| j|_| j|_| j|_| j|_|S )z‡
        Constructs a new copy of the current object, preserving any tagging

        :return:
            An Asn1Value object
        )r9   rA   r/   r7   r6   ©rE   Únew_objr&   r&   r'   Ú_new_instanceË  s   zAsn1Value._new_instancec                 C   ó   |   ¡ }| | tj¡ |S )z‡
        Implements the copy.copy() interface

        :return:
            A new shallow copy of the current Asn1Value object
        )r`   Ú_copyÚcopyr^   r&   r&   r'   Ú__copy__Ú  ó   zAsn1Value.__copy__c                 C   ó&   |   ¡ }||t| ƒ< | | tj¡ |S )zÁ
        Implements the copy.deepcopy() interface

        :param memo:
            A dict for memoization

        :return:
            A new deep copy of the current Asn1Value object
        )r`   rX   rb   rc   Údeepcopy©rE   Úmemor_   r&   r&   r'   Ú__deepcopy__æ  ó   zAsn1Value.__deepcopy__c                 C   ó
   t  | ¡S )z}
        Copies the object, preserving any special tagging from it

        :return:
            An Asn1Value object
        ©rc   rg   rS   r&   r&   r'   rc   ö  ó   
zAsn1Value.copyc                 C   s>   t |tƒs	||i}| j| d¡| d¡d�}| | tj¡ |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
        r6   r7   )r6   r7   )r-   Údictr9   Úgetrb   rc   rg   )rE   Útaggingr/   r_   r&   r&   r'   Úretag   s
   
zAsn1Value.retagc                 C   s   |   ¡ }| | tj¡ |S )z{
        Copies the object, removing any special tagging from it

        :return:
            An Asn1Value object
        )r9   rb   rc   rg   r^   r&   r&   r'   Úuntag  re   zAsn1Value.untagc                 C   s<   | j |j krttdt|ƒt| ƒƒƒ‚|j| _||jƒ| _dS )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
        úQ
                Can not copy values from %s object to %s object
                N)r9   r.   r
   r   rB   Ú_native©rE   ÚotherÚ	copy_funcr&   r&   r'   rb   "  s   ûzAsn1Value._copyr   c                 C   s’   d| }t | dƒ}t|| ƒ |r| j |d ¡ dS t | dƒr)| j |d ¡ dS tr>t| jtƒr>t	d|t
| jƒf ƒ dS t	d|| jf ƒ dS )úJ
        Show the binary data and parsed data in a tree structure
        ú  Úparsedr   Úchosenz%s    Native: b%sz%s    Native: %sN)r;   Ú_basic_debugr{   Údebugr|   rP   r-   Únativer   Úprintr=   )rE   Ú
nest_levelÚprefixÚ
has_parsedr&   r&   r'   r~   :  s   


zAsn1Value.debugc                 C   sˆ   | j }| jdu s
|r?t| tƒr| jrd| _t| j| j| j| j ƒ}| j	dur9| j	D ]\}}t|d||| j  ƒ| }q(|| _d| _
| j| S )á  
        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   r   r(   )rB   Ú_headerr-   ÚConstructableÚ_indefiniteÚmethodr   rA   r/   r6   Ú_trailer)rE   ÚforcerB   ÚheaderrA   r/   r&   r&   r'   rY   P  s   

zAsn1Value.dump©F)	NNFNNNNNN©N©r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rˆ   rA   r/   Ú_bad_tagr7   r6   r…   rB   r‰   ru   Úclassmethodr$   rL   rT   rZ   rQ   rR   r`   rd   rj   rc   rr   rs   rb   r~   rY   r&   r&   r&   r'   r#   ¨   s>    
ÿ 7




r#   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚValueMapzl
    Basic functionality that allows for mapping values from ints or OIDs to
    python unicode strings
    Nc                 C   sF   | j }|jdu s|jdurdS i |_|j ¡ D ]	\}}||j|< qdS )ú2
        Generates _reverse_map from _map
        N)r9   Ú_mapÚ_reverse_mapÚitems)rE   r1   Úkeyr3   r&   r&   r'   r5   |  s   ÿzValueMap._setup)r�   r�   r‘   r’   r—   r˜   r5   r&   r&   r&   r'   r•   n  s
    r•   c                   @   s   e Zd ZdZdd„ ZdS )Ú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
    c                 C   s„   |j | jj krttdt|ƒt| ƒ|j | jj ƒƒ‚|ƒ }| 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/   r9   r.   r
   r   rA   r7   r6   r…   rB   r‰   r-   r†   rˆ   r‡   )rE   Úother_classr_   r&   r&   r'   Úcast�  s&   ø
zCastable.castN)r�   r�   r‘   r’   r�   r&   r&   r&   r'   r›   ‰  s    r›   c                       s8   e Zd ZdZdZdZdd„ Zdd„ Z‡ fdd	„Z‡  Z	S )
r†   zŒ
    A mixin to handle string types that may be constructed from chunks
    contained within an indefinite length BER-encoded container
    Fr   c                 C   sz   | j s|  ¡ S | j}t| jƒ}d}||k r3t| j|| jd�\}}|du r)| ¡ }n|| ¡ 7 }||k s|du r;|  ¡ S |S )zc
        :return:
            A concatenation of the native values of the contained chunks
        N)r+   )r‡   Ú	_as_chunkÚ_chunks_offsetr>   rB   r0   r9   Ú_merge_chunks)rE   ÚpointerÚcontents_lenÚoutputÚ	sub_valuer&   r&   r'   r    Ã  s   

úzConstructable._merge_chunksc                 C   s    | j dkr| jS | j| j d… 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.
        r   N)rŸ   rB   rS   r&   r&   r'   rž   Ý  s   

zConstructable._as_chunkc                    s&   t t| ƒ ||¡ |j| _|j| _dS )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)Úsuperr†   rb   rˆ   r‡   rv   ©r9   r&   r'   rb   ë  s   zConstructable._copy)
r�   r�   r‘   r’   r‡   rŸ   r    rž   rb   Ú__classcell__r&   r&   r¦   r'   r†   µ  s    r†   c                   @   sJ   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Ze	dd„ ƒZ
ddd„ZdS )Ú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(   c                 C   s   |j | j kS )úu
        :param other:
            The other Primitive to compare to

        :return:
            A boolean
        r¦   ©rE   rw   r&   r&   r'   Ú__eq__  s   	zVoid.__eq__c                 C   ó   dS )NFr&   rS   r&   r&   r'   Ú__nonzero__  ó   zVoid.__nonzero__c                 C   r¬   ©Nr   r&   rS   r&   r&   r'   Ú__len__  r®   zVoid.__len__c                 C   s   t dƒS )Nr&   )ÚiterrS   r&   r&   r'   Ú__iter__  s   zVoid.__iter__c                 C   r¬   ©zn
        The a native Python datatype representation of this value

        :return:
            None
        Nr&   rS   r&   r&   r'   r     ó   	zVoid.nativeFc                 C   r¬   )r„   r(   r&   ©rE   rŠ   r&   r&   r'   rY   #  s   z	Void.dumpNrŒ   )r�   r�   r‘   r’   rB   r«   r­   r°   r²   Úpropertyr   rY   r&   r&   r&   r'   r¨   ü  s    

r¨   c                       sZ   e Zd ZdZdZddd„Zedd„ ƒZedd„ ƒZdd	d
„Z	‡ fdd„Z
ddd„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.
    Nc              
   K   s¨   t j| fi |¤Ž z$|dur+t|t ƒsttdt|ƒƒƒ‚||jdf| _| ¡ | _	W dS W dS  t
tfyS } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )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
                        r   r   r8   )r#   rL   r-   r.   r
   r   r9   Ú_parsedrY   rB   r<   rD   )rE   r3   r2   rK   rD   r&   r&   r'   rL   @  s"   
üö€ýzAny.__init__c                 C   s   | j du r	|  ¡  | j d jS )z˜
        The a native Python datatype representation of this value

        :return:
            The .native value from the parsed value object
        Nr   )r¸   Úparser   rS   r&   r&   r'   r   \  s   
	z
Any.nativec                 C   ó   | j du r	|  ¡  | j d S ©zw
        Returns the parsed object from .parse()

        :return:
            The object returned by .parse()
        Nr   ©r¸   r¹   rS   r&   r&   r'   r{   j  ó   
	
z
Any.parsedc           	   
   C   s  | j du s| j dd… ||fkrƒzL|pi }t|ƒ | jdur1d|v r,| j|d  |d< n| j|d< | j| j | j }t|||d�\}}|||f| _ d| _d| _d| _d| _|| _d| _W n& t	t
fy‚ } z|jdd… }|jd d	t| ƒ  f| |_|‚d}~ww | j d S )
áv  
        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
        Nr   r   r6   ©r+   r,   Fr(   r   ú
    while parsing %s)r¸   Ú_tag_type_to_explicit_implicitr6   r…   rB   r‰   r0   r/   r7   r<   r.   rD   r   )	rE   r+   r,   Úpassed_paramsrB   Úparsed_valuer4   rK   rD   r&   r&   r'   r¹   x  s8    


ý
€ý
z	Any.parsec                    s"   t t| ƒ ||¡ ||jƒ| _dS )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)r¥   r·   rb   r¸   rv   r¦   r&   r'   rb   ª  s   z	Any._copyFc                 C   s$   | j du r	|  ¡  | j d j|d�S )r„   Nr   ©rŠ   )r¸   r¹   rY   rµ   r&   r&   r'   rY   ¹  s   
zAny.dumpr�   ©NNrŒ   )r�   r�   r‘   r’   r¸   rL   r¶   r   r{   r¹   rb   rY   r§   r&   r&   r¦   r'   r·   5  s    



2r·   c                       sž   e Zd ZdZdZdZdZdZdZdZ	e
ddd„ƒZdd„ Zddd	„Zed
d„ ƒZdd„ Zedd„ ƒZedd„ ƒZdd„ Zdd„ Z‡ fdd„Zddd„Z‡  ZS )ÚChoicezF
    A class to handle when a value may be one of several options
    NFc                 K   s2   t |tƒstdt|ƒ ƒ‚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
        r)   r*   )r-   r   r.   r   r0   )r1   r%   r"   r2   r3   r4   r&   r&   r'   r$   ê  s   
zChoice.loadc                 C   st   | j }i |_i |_t|jƒD ])\}}t|ƒdk r"|i f }||j|< t|d |d ƒ}||j|< ||j|d < qdS )zS
        Generates _id_map from _alternatives to allow validating contents
        r   r   r   r   N)r9   Ú_id_mapÚ	_name_mapÚ	enumerateÚ_alternativesr>   Ú_build_id_tuple)rE   r1   ÚindexÚinfoÚid_r&   r&   r'   r5      s   


úzChoice._setupc           	   
   K   s~  t |ƒ tj| fi |¤Ž z‹| d¡durttdƒƒ‚|dur–t|tƒr@t|ƒdkr6ttdt	| ƒt|ƒƒƒ‚t
| ¡ ƒd \}}t|tƒr_t|ƒdkrWttdt	| ƒt|ƒƒƒ‚|d }|d }|| jvrnttd	|t	| ƒƒƒ‚| j| | _| j| j \}}}t||ƒs‹||fi |¤Ž}nt||ƒ}|| _W dS W dS  ttfy¾ } z|jdd… }|jd d
t	| ƒ  f| |_|‚d}~ww )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")
        r7   NzÍ
                    The Choice type can not be implicitly tagged even if in an
                    implicit module - due to its nature any tagging must be
                    explicit
                    r   zÝ
                            When passing a dict as the "name" argument to %s,
                            it must have a single key/value - however %d were
                            present
                            r   r   zì
                            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
                        r8   )rÁ   r#   rL   rp   r<   r
   r-   ro   r>   r   Úlistr™   ÚtuplerÈ   Ú_choicerÊ   Ú_fix_taggingr¸   r.   rD   )	rE   Únamer3   r2   r4   r+   ÚparamsrK   rD   r&   r&   r'   rL     sX   ÿ
ù	
ù	
ú	

Ó/€ýzChoice.__init__c                 C   s   | j s| j| j d | _ | j S )zc
        :return:
            A unicode string of the field name of the chosen alternative
        r   )Ú_namerÊ   rÑ   rS   r&   r&   r'   rÓ   d  s   zChoice.namec              
   C   sŒ   | j dur| j S z| j| j \}}}t| j||d�\| _ }W dS  ttfyE } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )z}
        Parses the detected alternative

        :return:
            An Asn1Value object of the chosen alternative
        Nr¿   r   r   rÀ   )	r¸   rÊ   rÑ   r0   rB   r<   r.   rD   r   )rE   r4   r+   rÔ   rK   rD   r&   r&   r'   r¹   n  s   
€ýzChoice.parsec                 C   r]   )zT
        :return:
            An Asn1Value object of the chosen alternative
        )r¹   rS   r&   r&   r'   r|   �  ó   zChoice.chosenc                 C   s   | j jS )z›
        The a native Python datatype representation of this value

        :return:
            The .native value from the contained value object
        )r|   r   rS   r&   r&   r'   r   Š  s   	zChoice.nativec                    sü   ||f}ˆ j dur,ˆ j d |krttdtˆ ƒƒƒ‚t|t|ƒƒ\\}}}}}}}||f}|ˆ jv r9ˆ j| ˆ _dS ˆ jdur`ˆ j	dur`tˆ j
ƒdkrSttdtˆ ƒƒƒ‚|ˆ jˆ j	fkr`dˆ _dS ˆ  ||¡}‡ fdd„ˆ jD ƒ}ttd	|tˆ ƒd
 |¡ƒƒ‚)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
        Néÿÿÿÿz�
                    %s was explicitly tagged, but the value provided does not
                    match the class and tag
                    r   z|
                    %s was implicitly tagged, but more than one alternative
                    exists
                    r   c                    s    g | ]}ˆ   |d  |d ¡‘qS r!   )Ú_format_class_tag)Ú.0ÚpairrS   r&   r'   Ú
<listcomp>É  s     z#Choice.validate.<locals>.<listcomp>zs
            Value %s did not match the class and tag of any of the alternatives
            in %s: %s
            ú, )r6   r<   r
   r   r   r>   rÇ   rÑ   rA   r/   rÊ   rØ   Újoin)rE   rA   r/   rB   rÎ   r4   Úasn1Úasn1sr&   rS   r'   Úvalidate•  s:   
û
ûùzChoice.validatec                 C   s   dt |  ¡ |f S )zo
        :return:
            A unicode string of a human-friendly representation of the class and tag
        z[%s %s])r?   Úupper)rE   rA   r/   r&   r&   r'   rØ   Õ  s   zChoice._format_class_tagc                    s2   t t| ƒ ||¡ |j| _|j| _||jƒ| _dS )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)r¥   rÆ   rb   rÑ   rÕ   r¸   rv   r¦   r&   r'   rb   Ý  s   zChoice._copyc                 C   sh   | j j|d�| _| jdu s|r.d| _| jdur.| jD ]\}}t|d|| j| j ƒ| j | _q| j| j S )r„   rÄ   Nr(   r   )r|   rY   rB   r…   r6   r   )rE   rŠ   rA   r/   r&   r&   r'   rY   î  s   
 zChoice.dumprŒ   rÅ   )r�   r�   r‘   r’   rÑ   rÕ   r¸   rÊ   rÇ   rÈ   r”   r$   r5   rL   r¶   rÓ   r¹   r|   r   rà   rØ   rb   rY   r§   r&   r&   r¦   r'   rÆ   Ë  s.    
T
	


@rÆ   c                   @   s°   e Zd ZdZdZdZed'dd„ƒZd(dd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd)dd„Zd'dd„Zedd„ ƒZdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdS )*Ú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.
    NFc                 C   s   | ||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
        )rB   r"   r&   )r1   r%   r"   r&   r&   r'   r$     s   zConcat.loadc              
   C   s  |durcz8t |ƒ}g | _d}| jD ]}||k r!t|||d�\}}n|ƒ }| j |¡ q|r;||kr;|| }td| ƒ‚W n& ttfyb }	 z|	jdd… }
|	jd dt| ƒ  f|
 |	_|	‚d}	~	ww |dur„| jdu rudgt | jƒ | _t	|ƒD ]\}}|  
||¡ qydS dS )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+   ú4Extra data - %d bytes of trailing data were providedr   r8   )r>   Ú	_childrenÚ_child_specsr0   Úappendr<   r.   rD   r   rÉ   Ú__setitem__)rE   r3   rB   r"   r¢   Úoffsetr+   Úchild_valueÚextra_bytesrK   rD   rÌ   Údatar&   r&   r'   rL   "  s6   
€€ý
üzConcat.__init__c                 C   rM   rN   rO   rS   r&   r&   r'   rT   O  rU   zConcat.__str__c                 C   r]   )z;
        A byte string of the DER-encoded contents
        ©rY   rS   r&   r&   r'   rQ   ]  ó   zConcat.__bytes__c                 C   s   t | ƒS ©rV   )r=   rS   r&   r&   r'   rR   d  s   zConcat.__unicode__c                 C   s   dt | ƒt| ƒt|  ¡ ƒf S )rV   rW   )r   rX   r=   rY   rS   r&   r&   r'   rZ   l  s   zConcat.__repr__c                 C   ra   )z|
        Implements the copy.copy() interface

        :return:
            A new shallow copy of the Concat object
        )r9   rb   rc   r^   r&   r&   r'   rd   t  re   zConcat.__copy__c                 C   rf   )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
        )r9   rX   rb   rc   rg   rh   r&   r&   r'   rj   €  rk   zConcat.__deepcopy__c                 C   rl   )zQ
        Copies the object

        :return:
            A Concat object
        rm   rS   r&   r&   r'   rc   �  rn   zConcat.copyc                 C   s4   | j |j krttdt|ƒt| ƒƒƒ‚||jƒ| _dS )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
        rt   N)r9   r.   r
   r   rä   rv   r&   r&   r'   rb   š  s   ûzConcat._copyr   c                 C   sN   d| }t d|t| ƒt| ƒf ƒ t d|f ƒ | jD ]	}| |d ¡ qdS )ry   rz   ú%s%s Object #%sz%s  Children:r   N)r€   r   rX   rä   r~   ©rE   r�   r‚   Úchildr&   r&   r'   r~   ±  s   
ÿzConcat.debugc                 C   s$   d}| j D ]
}||j|d�7 }q|S )r„   r(   rÄ   )rä   rY   ©rE   rŠ   rB   rñ   r&   r&   r'   rY   ¼  s   
zConcat.dumpc                 C   r]   )z`
        :return:
            A byte string of the DER-encoded contents of the children
        rì   rS   r&   r&   r'   rB   Í  rÖ   zConcat.contentsc                 C   ó
   t | jƒS )ú.
        :return:
            Integer
        )r>   rä   rS   r&   r&   r'   r°   Ö  ó   
zConcat.__len__c                 C   s8   |t | jƒd ks|dk rttd|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
        r   r   zN
                No child is definition for position %d of %s
                )r>   rå   ÚKeyErrorr
   r   rä   ©rE   rš   r&   r&   r'   Ú__getitem__Þ  s   û
zConcat.__getitem__c                 C   sZ   |t | jƒd ks|dk rttd|t| ƒƒƒ‚t|tƒs&ttd|t| ƒƒƒ‚|| j|< dS )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
        r   r   zK
                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)	r>   rå   rö   r
   r   r-   r#   r<   rä   )rE   rš   r3   r&   r&   r'   rç   ÷  s   û
ú	zConcat.__setitem__c                 C   ró   )zB
        :return:
            An iterator of child values
        )r±   rä   rS   r&   r&   r'   r²     rõ   zConcat.__iter__rŒ   )NNFrŽ   )r�   r�   r‘   r’   rå   rä   r”   r$   rL   rT   rQ   rR   rZ   rd   rj   rc   rb   r~   rY   r¶   rB   r°   rø   rç   r²   r&   r&   r&   r'   râ     s.    
-



$râ   c                   @   sD   e Zd ZdZdZdZddd„Zdd„ Zdd	d
„Zdd„ Z	dd„ Z
dS )Ú	PrimitivezO
    Sets the class_ and method attributes for primitive, universal values
    r   Nc              
   K   sª   t j| fi |¤Ž z%|dur|| _W dS |dur |  |¡ W dS |dur,|  |¡ W dS W dS  ttfyT } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )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
        Nr   r   r8   )r#   rL   rB   rC   r<   r.   rD   r   )rE   r3   rI   rB   r2   rK   rD   r&   r&   r'   rL   -  s   ÿ€ýzPrimitive.__init__c                 C   sL   t |tƒsttdt| ƒt|ƒƒƒ‚|| _|| _d| _| jdkr$d| _dS dS )ú_
        Sets the value of the object

        :param value:
            A byte string
        úH
                %s value must be a byte string, not %s
                Nr(   )	r-   r   r.   r
   r   ru   rB   r…   r‰   ©rE   r3   r&   r&   r'   rC   L  s   
û

ÿzPrimitive.setFc                 C   s$   |r| j }d| _|  |¡ t | ¡S )r„   N)r   rB   rC   r#   rY   ©rE   rŠ   r   r&   r&   r'   rY   c  s
   

zPrimitive.dumpc                 C   s
   | |k S r�   r&   rª   r&   r&   r'   Ú__ne__v  s   
zPrimitive.__ne__c                 C   sî   t |tƒsdS | j|jkrdS | jj|jjkrdS | j|jkr'| j|jkr'dS t| jjƒt| jgƒB ttttgƒ }t|jjƒt|jgƒB ttttgƒ }||B rW| j|jkS | j	sc| j
sc|j	sc|j
ro|  ¡  ¡ | ¡  ¡ kS |  ¡ | ¡ kS )r©   FT)r-   rù   rB   r9   r/   rC   Ú	__bases__r#   r•   r7   r6   rs   rY   )rE   rw   Ú
self_basesÚother_basesr&   r&   r'   r«   y  s   
	&&zPrimitive.__eq__©NNNrŒ   )r�   r�   r‘   r’   rA   rˆ   rL   rC   rY   rþ   r«   r&   r&   r&   r'   rù   $  s    

rù   c                       óD   e Zd ZdZdZdZdd„ Zdd„ Z‡ fdd	„Ze	d
d„ ƒZ
‡  ZS )Ú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
    Úlatin1Nc                 C   sf   t |tƒsttdt| ƒt|ƒƒƒ‚|| _| | j¡| _d| _	| j
r'd| _
d| _| jdkr1d| _dS dS )zb
        Sets the value of the string

        :param value:
            A unicode string
        úK
                %s value must be a unicode string, not %s
                NFr   r(   )r-   r   r.   r
   r   Ú_unicoder\   Ú	_encodingrB   r…   r‡   rˆ   r‰   rü   r&   r&   r'   rC   ¬  s   
û

ÿzAbstractString.setc                 C   s0   | j du rdS | jdu r|  ¡  | j¡| _| jS )rV   NÚ )rB   r  r    Údecoder  rS   r&   r&   r'   rR   Æ  s
   

zAbstractString.__unicode__c                    ó   t t| ƒ ||¡ |j| _dS )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)r¥   r  rb   r  rv   r¦   r&   r'   rb   Ò  ó   zAbstractString._copyc                 C   ó   | j du rdS |  ¡ S ©z‚
        The a native Python datatype representation of this value

        :return:
            A unicode string or None
        N)rB   rR   rS   r&   r&   r'   r   á  ó   
	zAbstractString.native)r�   r�   r‘   r’   r  r  rC   rR   rb   r¶   r   r§   r&   r&   r¦   r'   r  Ÿ  s    r  c                   @   s8   e Zd ZdZdZdd„ Zdd„ Zdd„ Zed	d
„ ƒZ	dS )ÚBooleanz7
    Represents a boolean in both ASN.1 and Python
    r   c                 C   s6   t |ƒ| _|s	dnd| _d| _| jdkrd| _dS dS )z…
        Sets the value of the object

        :param value:
            True, False or another value that works with bool()
        ó    ó   ÿNr(   )Úboolru   rB   r…   r‰   rü   r&   r&   r'   rC   ÷  s   


ÿzBoolean.setc                 C   r]   )ú4
        :return:
            True or False
        )Ú__bool__rS   r&   r&   r'   r­     rí   zBoolean.__nonzero__c                 C   s
   | j dkS )r  r  ©rB   rS   r&   r&   r'   r    s   
zBoolean.__bool__c                 C   s(   | j du rdS | jdu r|  ¡ | _| jS )z}
        The a native Python datatype representation of this value

        :return:
            True, False or None
        N)rB   ru   r  rS   r&   r&   r'   r     s
   
	

zBoolean.nativeN)
r�   r�   r‘   r’   r/   rC   r­   r  r¶   r   r&   r&   r&   r'   r  ð  s    r  c                   @   s0   e Zd ZdZdZdd„ Zdd„ Zedd„ ƒZd	S )
ÚIntegerz8
    Represents an integer in both ASN.1 and Python
    r   c                 C   s¾   t |tƒr(| jdu rttdt| ƒƒƒ‚|| jvr"ttdt| ƒ|ƒƒ‚| j| }nt |tƒs9ttdt| ƒt|ƒƒƒ‚| jrF|| jv rF| j| n|| _	t
|dd�| _d| _| jdkr]d| _dS dS )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   r—   r<   r
   r   r˜   r   r.   ru   r   rB   r…   r‰   rü   r&   r&   r'   rC   ,  s2   

ü
û
ú 	

ÿzInteger.setc                 C   s   t | jdd�S )ú1
        :return:
            An integer
        Tr  )r   rB   rS   r&   r&   r'   Ú__int__\  s   zInteger.__int__c                 C   sL   | j du rdS | jdu r#|  ¡ | _| jdur#| j| jv r#| j| j | _| jS ©ú|
        The a native Python datatype representation of this value

        :return:
            An integer or None
        N)rB   ru   r  r—   rS   r&   r&   r'   r   c  s   
	

zInteger.nativeN)	r�   r�   r‘   r’   r/   rC   r  r¶   r   r&   r&   r&   r'   r  %  s    0r  c                   @   sT   e Zd ZdZdZdZdZdZdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zedd„ ƒZdS )Ú	BitStringzK
    Represents a bit string from ASN.1 as a Python tuple of 1s and 0s
    r   Nr   c                 C   s6   t  | ¡ | j}|jdurt| j ¡ ƒd |_dS dS )r–   Nr   )r•   r5   r9   r—   ÚmaxÚkeysÚ_size)rE   r1   r&   r&   r'   r5   ƒ  s
   

ÿzBitString._setupc                 C   s  t |tƒrA| jdu rttdt| ƒƒƒ‚dg| j }|| _td| jƒD ]}| j 	|¡}|du r/q"||v r7d||< q"d 
tt|ƒ¡}n?|jtkrt| jdu rO|| _ntƒ | _t|ƒD ]\}}|rj| j 	||¡}| j |¡ qWd 
tt|ƒ¡}nttdt| ƒt|ƒƒƒ‚d| _| jdur¢t|ƒ| jkr�ttdt| ƒ| jt|ƒƒƒ‚| d¡}t|ƒ}|d	 }d}	|dkrºd	| }	|d|	 7 }tt |d	 ¡ƒ}
|	rÊt|	ƒ}nd
}|dkrÓd}ntt|dƒƒ}t|ƒ|
krêd
|
t|ƒ  | }|| | _d| _| jrûd| _d| _| jdk�rd| _dS dS )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
        NúF
                    %s._map has not been defined
                    r   r   r	  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(   r   F)r-   rC   r—   r<   r
   r   r!  ru   Úrangerp   rÝ   Úmapr   r9   rÐ   rÉ   Úaddr.   Ú_chunkr>   ÚrstripÚintÚmathÚceilr   rB   r…   r‡   rˆ   r‰   )rE   r3   ÚbitsrÌ   rš   ÚbitrÓ   ÚsizeÚsize_modÚ
extra_bitsÚsize_in_bytesÚextra_bits_byteÚvalue_bytesr&   r&   r'   rC   Ž  s€   

ü€

€ú	
ú



ÿzBitString.setc                 C   s¤   t |tƒ}|s%t | jtƒsttdt| ƒƒƒ‚|| jvr%ttdt| ƒ|ƒƒ‚| jdu r-| j	 | jdu rDt
| jƒ|d krBt| j| ƒS dS |rM| j ||¡}|| jv S )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
        r"  úT
                    %s._map does not contain an entry for "%s"
                    Nr   F)r-   r   r—   ro   r<   r
   r   r˜   ru   r   r>   r  rp   )rE   rš   Úis_intr&   r&   r'   rø   ò  s,   
ü
û


zBitString.__getitem__c                 C   s  t |tƒ}|s$| jdu rttdt| ƒƒƒ‚|| jvr$ttdt| ƒ|ƒƒ‚| jdu r,| j | jdu rXt	| jƒ}t
|ƒd }||krJ| dg||  ¡ |rNdnd||< t|ƒ| _n"|ra| j ||¡}|ro|| jvrn| j |¡ n|| jv rz| j |¡ |  | j¡ dS )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
        Nr"  r5  r   r   )r-   r   r—   r<   r
   r   r˜   ru   r   rÏ   r>   ÚextendrÐ   rp   r'  ÚremoverC   )rE   rš   r3   r6  Ú
new_nativeÚmax_keyr&   r&   r'   rç   !  s>   

ü
û



€
zBitString.__setitem__c                 C   s¾   t | jdd… ƒ}d t | jdd… ƒ¡}t| jdd… ƒ}t|ƒ}|d }|dkr6dd|  | }t|ƒ}|d |k rJ||d  }dd|  | }|dkrV|dd| … }tttt|ƒƒƒS )zr
        Allows reconstructing indefinite length values

        :return:
            A tuple of integers
        r   r   ú{0:b}Nr$  r#  )r   rB   Úformatr>   rÐ   r&  r*  )rE   r1  Ú
bit_stringÚbyte_lenÚbit_lenÚmod_bit_lenÚmissing_bytesr&   r&   r'   rž   Z  s   zBitString._as_chunkc                 C   sŽ   | j du r| jdu r|  d¡ n|  tƒ ¡ | jdu rD|  ¡ }| jrAtƒ | _t|ƒD ]\}}|r=| j ||¡}| j |¡ q*| jS || _| jS )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.
        Nr&   )rB   r—   rC   ru   r    rÉ   rp   r'  )rE   r-  rÌ   r.  rÓ   r&   r&   r'   r   x  s    


€ÿzBitString.native)r�   r�   r‘   r’   r/   r!  r(  rŸ   r5   rC   rø   rç   rž   r¶   r   r&   r&   r&   r'   r  v  s    d/9r  c                       sH   e Zd ZdZdZdZdZdd„ Zdd„ Z‡ fd	d
„Z	e
dd„ ƒZ‡  ZS )ÚOctetBitStringzB
    Represents a bit string in ASN.1 as a Python byte string
    r   r   Nc                 C   ób   t |tƒsttdt| ƒt|ƒƒƒ‚|| _d| | _d| _| jr%d| _d| _	| j
dkr/d| _
dS dS ©zª
        Sets the value of the object

        :param value:
            A byte string

        :raises:
            ValueError - when an invalid value is passed
        rû   r  NFr   r(   ©r-   r   r.   r
   r   Ú_bytesrB   r…   r‡   rˆ   r‰   rü   r&   r&   r'   rC   ¦  ó   
û


ÿzOctetBitString.setc                 C   ó(   | j du rdS | jdu r|  ¡ | _| jS ©z4
        :return:
            A byte string
        Nr(   ©rB   rF  r    rS   r&   r&   r'   rQ   Ä  ó
   


zOctetBitString.__bytes__c                    r  )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)r¥   rB  rb   rF  rv   r¦   r&   r'   rb   Ð  r  zOctetBitString._copyc                 C   r  ©ú
        The a native Python datatype representation of this value

        :return:
            A byte string or None
        N©rB   rQ   rS   r&   r&   r'   r   ß  r  zOctetBitString.native)r�   r�   r‘   r’   r/   rŸ   rF  rC   rQ   rb   r¶   r   r§   r&   r&   r¦   r'   rB  –  s    rB  c                   @   s4   e Zd ZdZdZdZdd„ Zdd„ Zedd	„ ƒZ	d
S )ÚIntegerBitStringz>
    Represents a bit string in ASN.1 as a Python integer
    r   r   c                 C   sj   t |tƒsttdt| ƒt|ƒƒƒ‚|| _dt|dd� | _d| _| j	r)d| _	d| _
| jdkr3d| _dS dS )	ú§
        Sets the value of the object

        :param value:
            An integer

        :raises:
            ValueError - when an invalid value is passed
        úE
                %s value must be an integer, not %s
                r  Tr  NFr   r(   ©r-   r   r.   r
   r   ru   r   rB   r…   r‡   rˆ   r‰   rü   r&   r&   r'   rC   ÷  s   
û

ÿzIntegerBitString.setc                 C   sj   t | jdd… ƒ}d t | jdd… ƒ¡}t|ƒd }|dkr'dd|  | }|dkr3|dd| … S |S )zƒ
        Allows reconstructing indefinite length values

        :return:
            A unicode string of bits - 1s and 0s
        r   r   r;  Nr$  r#  )r   rB   r<  r>   )rE   r1  r=  r@  r&   r&   r'   rž   	  s   zIntegerBitString._as_chunkc                 C   s~   | j du rdS | jdu r<t| j dd… ƒ}| js)|dkr)t| j dd… ƒ| _| jS | jr4|dkr4tdƒ‚t|  ¡ dƒ| _| jS )r  Nr   r   z=Constructed bit string has extra bits on indefinite containerr   )rB   ru   r   r‡   r<   r*  r    )rE   r1  r&   r&   r'   r   *	  s   
	
ýzIntegerBitString.nativeN)
r�   r�   r‘   r’   r/   rŸ   rC   rž   r¶   r   r&   r&   r&   r'   rO  î  s    rO  c                       r  )ÚOctetStringz;
    Represents a byte string in both ASN.1 and Python
    é   Nc                 C   ó^   t |tƒsttdt| ƒt|ƒƒƒ‚|| _|| _d| _| jr#d| _d| _	| j
dkr-d| _
dS dS ©rú   rû   NFr   r(   rE  rü   r&   r&   r'   rC   L	  ó   
û

ÿzOctetString.setc                 C   rH  rI  rJ  rS   r&   r&   r'   rQ   f	  rK  zOctetString.__bytes__c                    r  )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)r¥   rS  rb   rF  rv   r¦   r&   r'   rb   r	  r  zOctetString._copyc                 C   r  rL  rN  rS   r&   r&   r'   r   �	  r  zOctetString.native)r�   r�   r‘   r’   r/   rF  rC   rQ   rb   r¶   r   r§   r&   r&   r¦   r'   rS  B	  s    rS  c                   @   ó(   e Zd ZdZdZdd„ Zedd„ ƒZdS )ÚIntegerOctetStringz?
    Represents a byte string in ASN.1 as a Python integer
    rT  c                 C   sf   t |tƒsttdt| ƒt|ƒƒƒ‚|| _t|dd�| _d| _| j	r'd| _	d| _
| jdkr1d| _dS dS )rP  rQ  Fr  Nr   r(   rR  rü   r&   r&   r'   rC   —	  s   
û

ÿzIntegerOctetString.setc                 C   s,   | j du rdS | jdu rt|  ¡ ƒ| _| jS r  )rB   ru   r   r    rS   r&   r&   r'   r   ´	  s
   
	
zIntegerOctetString.nativeN©r�   r�   r‘   r’   r/   rC   r¶   r   r&   r&   r&   r'   rY  �	  s    rY  c                       sn   e Zd ZdZdZdZddd„Zdd„ Zddd„Zd	d
„ Z	‡ fdd„Z
edd„ ƒZedd„ ƒZddd„Z‡  ZS )ÚParsableOctetStringrT  Nc                 K   sZ   d}|du r|durt |tƒr| ¡ }d}tj| fd|i|¤Ž |r+||jdf| _dS dS )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.
        FNTr3   )r-   r#   rY   rù   rL   r9   r¸   )rE   r3   r{   r2   Ú
set_parsedr&   r&   r'   rL   Î	  s   ÿzParsableOctetString.__init__c                 C   rU  rV  rE  rü   r&   r&   r'   rC   æ	  rW  zParsableOctetString.setc                 C   sL   | j du s| j dd… ||fkr!t|  ¡ ||d�\}}|||f| _ | j d S )r¾   Nr   r   r¿   r   )r¸   r0   rQ   )rE   r+   r,   rÃ   r4   r&   r&   r'   r¹    
  s    
zParsableOctetString.parsec                 C   rH  rI  rJ  rS   r&   r&   r'   rQ   
  rK  zParsableOctetString.__bytes__c                    s*   t t| ƒ ||¡ |j| _||jƒ| _dS )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)r¥   r[  rb   rF  r¸   rv   r¦   r&   r'   rb   #
  s   zParsableOctetString._copyc                 C   s,   | j du rdS | jdur| jd jS |  ¡ S )rM  Nr   )rB   r¸   r   rQ   rS   r&   r&   r'   r   3
  s
   
	
zParsableOctetString.nativec                 C   rº   r»   r¼   rS   r&   r&   r'   r{   D
  r½   zParsableOctetString.parsedFc                 C   s>   |r| j dur| jj|d�}n| j}d| _|  |¡ t | ¡S )r„   NrÄ   )r¸   r{   rY   r   rB   rC   r#   rý   r&   r&   r'   rY   R
  s   


zParsableOctetString.dumprÅ   rŒ   )r�   r�   r‘   r/   r¸   rF  rL   rC   r¹   rQ   rb   r¶   r   r{   rY   r§   r&   r&   r¦   r'   r[  Å	  s    



r[  c                   @   s   e Zd ZdZdZdd„ ZdS )ÚParsableOctetBitStringr   r   c                 C   rC  rD  rE  rü   r&   r&   r'   rC   s
  rG  zParsableOctetBitString.setN)r�   r�   r‘   r/   rŸ   rC   r&   r&   r&   r'   r]  i
  s    r]  c                   @   s,   e Zd ZdZdZdZdd„ Zedd„ ƒZdS )	ÚNullz<
    Represents a null value in ASN.1 as None in Python
    é   r(   c                 C   s
   d| _ dS )zV
        Sets the value of the object

        :param value:
            None
        r(   Nr  rü   r&   r&   r'   rC   ›
  rn   zNull.setc                 C   r¬   r³   r&   rS   r&   r&   r'   r   ¥
  r´   zNull.nativeN)	r�   r�   r‘   r’   r/   rB   rC   r¶   r   r&   r&   r&   r'   r^  ’
  s    
r^  c                   @   sX   e Zd ZdZdZdZedd„ ƒZedd„ ƒZdd	„ Z	d
d„ Z
edd„ ƒZedd„ ƒZdS )ÚObjectIdentifierú`
    Represents an object identifier in ASN.1 as a Python unicode dotted
    integer string
    é   Nc                 C   sF   | j du rttdt| ƒƒƒ‚t|tƒsttdt|ƒƒƒ‚| j  ||¡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
        Nú>
                %s._map has not been defined
                úH
                value must be a unicode string, not %s
                )r—   r<   r
   r   r-   r   r.   rp   ©r1   r3   r&   r&   r'   r&  ¼
  s   
ü
üzObjectIdentifier.mapc                 C   sˆ   | t vr| ƒ  ¡  dt | < | jdu rttdt| ƒƒƒ‚t|tƒs)ttdt|ƒƒƒ‚|| j	v r3| j	| S t
 |¡sBttdt| ƒ|ƒƒ‚|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
        TNrc  rd  zL
                %s._map does not contain an entry for "%s"
                )r:   r5   r—   r<   r
   r   r-   r   r.   r˜   Ú_OID_REÚmatchre  r&   r&   r'   ÚunmapÞ
  s,   

ü
ü


ûzObjectIdentifier.unmapc                 C   sþ   t |tƒsttdt| ƒt|ƒƒƒ‚|| _| jdur#|| jv r#| j| }d| _d}t	| 
d¡ƒD ]@\}}t|ƒ}|dkr>|}q/|dkrH|d | }td|@ ƒ}|d	? }|dkrhtd
d|@ B ƒ| }|d	? }|dksV|  j|7  _q/d| _| jdkr}d| _dS dS )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
        r  Nr(   Ú.r   r   é(   é   é   é€   )r-   r   r.   r
   r   ru   r—   r˜   rB   rÉ   Úsplitr*  r   r…   r‰   )rE   r3   ÚfirstrÌ   ÚpartÚencoded_partr&   r&   r'   rC     s<   
û


þ

ÿzObjectIdentifier.setc                 C   ó   | j S rî   )ÚdottedrS   r&   r&   r'   rR   B  s   zObjectIdentifier.__unicode__c                 C   s    | j du rMg }d}| jD ]:}trt|ƒ}|d }||d@ 7 }|d@ dkrFt|ƒdkr=| t|d ƒ¡ | t|d ƒ¡ n| t|ƒ¡ d}qd |¡| _ | j S )z�
        :return:
            A unicode string of the object identifier in dotted notation, thus
            ignoring any mapped value
        Nr   rm  rk  rj  ri  )Ú_dottedrB   rP   Úordr>   ræ   r   rÝ   )rE   r£   rp  Úbyter&   r&   r'   rs  J  s"   

€zObjectIdentifier.dottedc                 C   sJ   | j du rdS | jdu r"| j| _| jdur"| j| jv r"| j| j | _| jS )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)rB   ru   rs  r—   rS   r&   r&   r'   r   g  s   

zObjectIdentifier.native)r�   r�   r‘   r’   r/   rt  r”   r&  rh  rC   rR   r¶   rs  r   r&   r&   r&   r'   r`  ±
  s    
!
12
r`  c                   @   ó   e Zd ZdZdZdS )ÚObjectDescriptorzO
    Represents an object descriptor from ASN.1 - no Python implementation
    rl  N©r�   r�   r‘   r’   r/   r&   r&   r&   r'   rx  |  ó    rx  c                   @   rw  )Ú
InstanceOfzF
    Represents an instance from ASN.1 - no Python implementation
    r$  Nry  r&   r&   r&   r'   r{  „  rz  r{  c                   @   rw  )ÚRealzH
    Represents a real number from ASN.1 - no Python implementation
    é	   Nry  r&   r&   r&   r'   r|  Œ  rz  r|  c                   @   rX  )Ú
Enumeratedz\
    Represents a enumerated list of integers from ASN.1 as a Python
    unicode string
    é
   c                 C   sŽ   t |tƒst |tƒsttdt| ƒt|ƒƒƒ‚t |tƒr0|| jvr*ttdt| ƒ|ƒƒ‚| j| }n|| jvr?ttdt| ƒ|ƒƒ‚t	 
| |¡ dS )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-   r   r   r.   r
   r   r˜   r<   r—   r  rC   rü   r&   r&   r'   rC   œ  s*   û

û
ûzEnumerated.setc                 C   s.   | j du rdS | jdu r| j|  ¡  | _| jS r  )rB   ru   r—   r  rS   r&   r&   r'   r   Ç  s
   
	
zEnumerated.nativeNrZ  r&   r&   r&   r'   r~  ”  s    +r~  c                   @   ó   e Zd ZdZdZdZdS )Ú
UTF8StringzI
    Represents a UTF-8 string from ASN.1 as a Python unicode string
    é   r[   N©r�   r�   r‘   r’   r/   r  r&   r&   r&   r'   r�  Ø  ó    r�  c                   @   rw  )ÚRelativeOidra  é   Nry  r&   r&   r&   r'   r…  á  s    r…  c                       s  e Zd ZdZdZdZdZdZdZdZ	g Z
dZdZdZdZdZdZdZd.dd„Zed	d
„ ƒZejdd
„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd/dd„Zdd„ Zdd„ Zd d!„ Z d/d"d#„Z!d$d%„ Z"ed&d'„ ƒZ#‡ fd(d)„Z$d0d*d+„Z%d/d,d-„Z&‡  Z'S )1ÚSequencezf
    Represents a sequence of fields from ASN.1 as a Python object with a
    dict-like interface
    é   r   r   NFc              
   K   sx  t j| fi |¤Ž d}|du r(|dur(d}| jdu r&| jdu r"d}n|  ¡  |}|durºzg| jr?dd„ | jD ƒ}t| ¡ ƒ}n| ¡ }t|ƒ}|D ]2}|rj| j| }|t	| jƒk rj| j| t
urj||v ri| |¡ qI||v r{|  ||| ¡ | |¡ qIt	|ƒr‘ttdt| ƒd tt|ƒƒ¡ƒƒ‚W dS  ttfy¹ }	 z|	jdd… }
|	jd	 d
t| ƒ  f|
 |	_|	‚d}	~	ww dS )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
        FNTc                 S   ó   g | ]}|d  ‘qS )r   r&   )rÙ   rÍ   r&   r&   r'   rÛ   A  ó    z%Sequence.__init__.<locals>.<listcomp>zŒ
                        One or more unknown fields was passed to the constructor
                        of %s: %s
                        rÜ   r   r   r8   )r#   rL   ÚchildrenrB   Ú_parse_childrenÚ_fieldsrC   r   Ú
_field_mapr>   ÚVOIDr8  rç   r<   r
   r   rÝ   ÚsortedrÏ   r.   rD   )rE   r3   rI   r2   Úcheck_existingr   Úunused_keysrš   rÌ   rK   rD   r&   r&   r'   rL   #  sR   




€úÿ
€ýÜzSequence.__init__c                 C   ó&   | j du r| jS |  ¡ r|  ¡  | jS ©z`
        :return:
            A byte string of the DER-encoded contents of the sequence
        N©r‹  Ú	_contentsÚ_is_mutatedÚ_set_contentsrS   r&   r&   r'   rB   d  ó
   
zSequence.contentsc                 C   ó
   || _ dS ©ze
        :param value:
            A byte string of the DER-encoded contents of the sequence
        N©r–  rü   r&   r&   r'   rB   s  ó   
c                 C   ó@   | j }| jdur| jD ]}t|tƒst|tƒr|p| ¡ }q|S ©z~
        :return:
            A boolean - if the sequence or any children (recursively) have been
            mutated
        N©Ú_mutatedr‹  r-   r‡  Ú
SequenceOfr—  ©rE   Úmutatedrñ   r&   r&   r'   r—  |  ó   

€zSequence._is_mutatedc                 C   s*   | j | }|jtkrt|Ž  }| j |< |S ©z]
        Builds a child object if the child has only been parsed into a tuple so far
        ©r‹  r9   rÐ   Ú_build©rE   rÌ   rñ   r&   r&   r'   Ú_lazy_child‹  s   

zSequence._lazy_childc                 C   ó   | j du r	|  ¡  t| j ƒS )rô   N©r‹  rŒ  r>   rS   r&   r&   r'   r°   •  ó   

zSequence.__len__c              
   C   sÀ   | j du r	|  ¡  t|tƒs"|| jvrttd|t| ƒƒƒ‚| j| }|t| j ƒkr3ttd|t| ƒƒƒ‚z|  	|¡W S  t
tfy_ } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )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
        NúL
                    No field named "%s" defined for %s
                    zL
                No field numbered %s is present in this %s
                r   r   rÀ   )r‹  rŒ  r-   r   rŽ  rö   r
   r   r>   rª  r<   r.   rD   )rE   rš   rK   rD   r&   r&   r'   rø      s0   


û
û€ýzSequence.__getitem__c           
      C   sø   | j du r	|  ¡  t|tƒs"|| jvrttd|t| ƒƒƒ‚| j| }|  |¡\}}}}}|  	|||||¡}d}	t|t
ƒrB|jdu }	nt|tƒrN|jjdu }	n|jdu }	|	r_ttd|t| ƒƒƒ‚|| j |< | jdurw| j | j| j| j| d < d| _dS )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
        Nr®  FzG
                Value for field "%s" of %s is not set
                r   T)r‹  rŒ  r-   r   rŽ  rö   r
   r   Ú_determine_specÚ_make_valuer·   r{   rÆ   r|   rB   r<   ru   r   r�  r¡  )
rE   rš   r3   Ú
field_nameÚ
field_specÚ
value_specÚfield_paramsr4   Ú	new_valueÚinvalid_valuer&   r&   r'   rç   Î  s8   


û



û


zSequence.__setitem__c                 C   sº   | j du r	|  ¡  t|tƒs"|| jvrttd|t| ƒƒƒ‚| j| }| j| \}}}|r4d|vr>d|vr>t	td|t| ƒƒƒ‚d|v rRt
| j |< | jdurQd| j|< n|  |d¡ d| _dS )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
        Nr®  rI   rH   z‰
                Can not delete the value for the field "%s" of %s since it is
                not optional or defaulted
                T)r‹  rŒ  r-   r   rŽ  rö   r
   r   r�  r<   r�  ru   rç   r¡  )rE   rš   rÓ   r4   rÔ   r&   r&   r'   Ú__delitem__  s0   


û
ú	


€
zSequence.__delitem__c                 c   s   � | j D ]}|d V  qdS )zE
        :return:
            An iterator of field key names
        r   N)r�  )rE   rÍ   r&   r&   r'   r²   4  s   €
ÿzSequence.__iter__c                 C   sö   | j du r	|  ¡  tƒ }t| jƒD ]U\}}| j | }|du r!d}n$|jtkr?|r2|  |¡j|d�}n|d |d  |d  }n|j|d�}|d rad|d v ra|d	 d
i |d ¤Ž}| ¡ |kraq| 	|¡ q| 
¡ | _d| _| jdkryd| _dS dS )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(   rÄ   r   rT  r_  r   rI   r   r&   )r‹  rŒ  r   rÉ   r�  r9   rÐ   rª  rY   ÚwriteÚgetvaluer–  r…   r‰   )rE   rŠ   rB   rÌ   rÍ   rñ   Ú
child_dumpÚdefault_valuer&   r&   r'   r˜  =  s,   






ÿzSequence._set_contentsc                 C   s$  | j }i |_g |_g |_t|jƒD ](\}}t|ƒdk r%|i f }||j|< ||j|d < |j t|d |d ƒ¡ q|j	durP|j|j	d  |j|j	d  f|_
t|jƒD ]:\}}|jduod|d |jv }|j
duop|j
d |k}|su|r||j d¡ qU|j |d |d |d |d df¡ qUdS ©zS
        Generates _field_map, _field_ids and _oid_nums for use in parsing
        r   r   r   r   N)r9   rŽ  Ú
_field_idsÚ_precomputed_specsrÉ   r�  r>   ræ   rË   Ú	_oid_pairÚ	_oid_numsÚ_spec_callbacks©rE   r1   rÌ   ÚfieldÚhas_callbackÚis_mapped_oidr&   r&   r'   r5   b  s&   


"(úzSequence._setupc           	      C   sÞ   | j | \}}}|}d}| jdurG|| jv rG| j| }|| ƒ}|rF|jtkr9t|ƒdkr9|\}}|du r8|}d}n/|du rD|}|}d}n$|}n!| jdurh| jd |krh|  | jd ¡j}|| jv rh| j| }|}|||||fS )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
        Nr   r   r   )	r�  rÁ  r9   rÐ   r>   rÀ  rª  r   Ú
_oid_specs)	rE   rÌ   rÓ   r²  r´  r³  Úspec_overrideÚcallbackÚoidr&   r&   r'   r¯  }  s2   
€€

zSequence._determine_specc           
      C   s  |du r
d|v r
t S ||k}t|tƒ}t|tƒrBt|tƒs'ttd|t|ƒƒƒ‚t||ƒs?|ƒ }| 	|j
|j|j¡ ||_|}	nH|}	nEt||ƒrQ|}	|rP|	 |¡ n6|rU|rct||ƒsc||fi |¤Ž}	n$t||ƒrk|}	n||ƒ}	|r‡|s‡|dd|	 ¡ i|¤Ž}|	|	jdf|_|}	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
        NrH   z¯
                    Can not set a native python value to %s, which has the
                    choice type of %s - value must be an instance of Asn1Value
                    r3   r&   )r�  Ú
issubclassr·   rÆ   r-   r#   r<   r
   r   rà   rA   r/   rB   r¸   r¹   rY   r9   rÒ   )
rE   r±  r²  r³  r´  r3   Úspecs_differentÚis_anyÚwrapperrµ  r&   r&   r'   r°  ¬  sB   


ú


€

zSequence._make_valuec              
   C   s$  | j }| jdu rM| jrKtgt| jƒ | _t| jƒD ]1\}\}}}d|v rJ|j| r4|j| \}}}}	}n
|  |¡\}}}}	}|  	||||	d¡| j|< qdS �zœg | _t| jƒ}
d}d}t| jƒ}d}||
k }|�r¦|du rwt
| j|
|d�\}}||
k }||k �r|j| p‰|  |¡\}}}}	}|	ròd|	v s™d|	v rò| j| |d |d fkrò|tkròd}t|tƒrÓz|di |	¤Ž}| |d |d |d ¡ d	}W n	 tyÒ   Y nw |sòd|	v rà| j t¡ n| j |di |	¤Ž¡ |d
7 }d	}qf|du sÿ|�rt|tƒ�r|}d}|�r|||	|f }nu|||	f }nn|dk�r�|d
 |k�r�g }|d
 }|dk�rW| j| }t|ƒdk �r9nd|d v �sGd|d v �rN| |d ¡ |d
8 }|dk�s,t|ƒd
k�r`dnd}d |¡}ttd|d
 t |d ¡t |d
 ¡|d ||ƒƒ‚|}|�r˜t|Ž }t|ttfƒ�r˜|jd	d� | j |¡ |d
7 }d}|sit| jƒ}||k �rè| j| \}}}	d|	v �rÉ| j |di |	¤Ž¡ nd|	v �rÕ| j t¡ nttd|ƒƒ‚|d
7 }||k �s°W dS W dS  ttf�y } z|jd
d… }|jd dt| ƒ  f| |_|‚d}~ww )áQ  
        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
        NrI   r   ©r¡   rH   r   FrT  Tr   r   Úsr	  rÜ   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
                        rÀ   r&   )r9   r–  r�  r�  r>   r‹  rÉ   r¾  r¯  r°  r   r½  r·   rÊ  rÆ   rà   r<   ræ   rÝ   r
   r?   rp   ÚMETHOD_NUM_TO_NAME_MAPr¨  r-   r‡  r¢  rŒ  r.   rD   r   )rE   rÒ  r1   rÌ   r4   rÔ   r±  r²  r³  r´  Úcontents_lengthÚchild_pointerrÃ  Ú	field_lenÚpartsÚagainrÇ  Úchoice_matchÚtesterrñ   Úmissed_fieldsÚ
prev_fieldÚprev_field_infoÚpluralÚmissed_field_namesrÓ   rK   rD   r&   r&   r'   rŒ  õ  sÊ   

€


ÿ"
ÿ


ú
ö²
P


üó€ýzSequence._parse_childrenc                 C   sZ   t |tƒsttdt|ƒƒƒ‚| jdu rttdt|ƒt| ƒƒƒ‚| j| }|  	|¡}|d S )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   r�  r<   r=   rŽ  r¯  )rE   r±  rÌ   rÍ   r&   r&   r'   r+   |  s   
ü
ú
	
zSequence.specc              
   C   sü   | j du rdS | jdu r{| jdu r| jdd� z=tƒ | _t| jƒD ]/\}}|jtkr3t|Ž }|| j|< z	| j	| d }W n t
yI   t|ƒ}Y nw |j| j|< q!W | jS  ttfyz } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww | jS )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.
        NTrÑ  r   r   rÀ   )rB   ru   r‹  rŒ  r   rÉ   r9   rÐ   r¨  r�  Ú
IndexErrorr   r   r<   r.   rD   r   )rE   rÌ   rñ   rÓ   rK   rD   r&   r&   r'   r   £  s4   





ÿøü€ýzSequence.nativec                    ó^   t t| ƒ ||¡ | jdur+g | _|jD ]}|jtkr"| j |¡ q| j | ¡ ¡ qdS dS )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)r¥   r‡  rb   r‹  r9   rÐ   ræ   rc   ©rE   rw   rx   rñ   r¦   r&   r'   rb   Ä  ó   


úzSequence._copyc                 C   sh   | j du r	|  ¡  d| }t|| ƒ | D ]}|  | j| ¡}|tur1td||f ƒ | |d ¡ qdS )ry   Nrz   z%s    Field "%s"r   )r‹  rŒ  r}   rª  rŽ  r�  r€   r~   )rE   r�   r‚   r±  rñ   r&   r&   r'   r~   Ù  s   

€üzSequence.debugc                 C   sr   |r| j |d� | jr4| jdur4t| jƒD ]\}\}}}| j| tur$qd|v s,d|v r-qttd|ƒƒ‚t | ¡S )r„   rÄ   NrI   rH   zN
                    Field "%s" is missing from structure
                    )	r˜  r�  r‹  rÉ   r�  r<   r
   r#   rY   )rE   rŠ   rÌ   r±  r4   rÔ   r&   r&   r'   rY   é  s   ü
zSequence.dumprÅ   rŒ   rŽ   )(r�   r�   r‘   r’   r/   rA   rˆ   r‹  r–  r¡  r�  rÁ  rŽ  r½  r¿  rÆ  rÀ  r¾  rL   r¶   rB   Úsetterr—  rª  r°   rø   rç   r·  r²   r˜  r5   r¯  r°  rŒ  r+   r   rb   r~   rY   r§   r&   r&   r¦   r'   r‡  ê  sP    
A


.9-
	%/
I '
 
r‡  c                       sì   e Zd ZdZdZdZdZdZdZdZ	dZ
d.dd„Zed	d
„ ƒZejdd
„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd/d d!„Zd/d"d#„Zd$d%„ Zed&d'„ ƒZ‡ fd(d)„Zd0d*d+„Zd/d,d-„Z‡  Z S )1r¢  z�
    Represents a sequence (ordered) of a single type of values from ASN.1 as a
    Python object with a list-like interface
    rˆ  r   r   NFc           
   
   K   sÜ   |r|| _ tj| fi |¤Ž z9|dur|| _W dS |du r#|dur#|}|durBt|ƒD ]
\}}|  ||¡ q+| jdu rE|  ¡  W dS W dS W dS  ttfym } z|j	dd… }	|j	d dt
| ƒ  f|	 |_	|‚d}~ww )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
        Nr   r   r8   )Ú_child_specr#   rL   rB   rÉ   rç   r˜  r<   r.   rD   r   )
rE   r3   rI   rB   r+   r2   rÌ   rñ   rK   rD   r&   r&   r'   rL      s*   
ú€ýzSequenceOf.__init__c                 C   r“  r”  r•  rS   r&   r&   r'   rB   K  r™  zSequenceOf.contentsc                 C   rš  r›  rœ  rü   r&   r&   r'   rB   Z  r�  c                 C   rž  rŸ  r   r£  r&   r&   r'   r—  c  r¥  zSequenceOf._is_mutatedc                 C   s*   | j | }|jtkrt|Ž }|| j |< |S r¦  r§  r©  r&   r&   r'   rª  r  s
   


zSequenceOf._lazy_childc                 C   sð   t || jƒr	|}nPt| jtƒr t |tƒr|}nBttdt| ƒƒƒ‚t| jtƒrSt |tƒs7ttdt| ƒ| jj	ƒƒ‚t || jƒsP|  ¡ }| 
|j|j|j¡ ||_|}|}n| j|d�S i }| jjre| jj|d< | jjrs| jj| jj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
                    )r3   r6   r7   )r-   rå  rÊ  r·   r#   r<   r
   r   rÆ   r�   rà   rA   r/   rB   r¸   r6   r7   rÒ   )rE   r3   rµ  rÍ  rÔ   r&   r&   r'   r°  }  s:   
û
ù	
zSequenceOf._make_valuec                 C   r«  )r  Nr¬  rS   r&   r&   r'   r°   ´  r­  zSequenceOf.__len__c                 C   s   | j du r	|  ¡  |  |¡S )zm
        Allows accessing children via index

        :param key:
            Integer index of child
        N)r‹  rŒ  rª  r÷   r&   r&   r'   rø   ¿  r½   zSequenceOf.__getitem__c                 C   s|   | j du r	|  ¡  |  |¡}|t| j ƒkr&| j  d¡ | jdur&| j d¡ || j |< | jdur9| j | j| j|< d| _dS )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°  r>   ræ   ru   r   r¡  )rE   rš   r3   rµ  r&   r&   r'   rç   Í  s   





zSequenceOf.__setitem__c                 C   s>   | j du r	|  ¡  | j  |¡ | jdur| j |¡ d| _dS )zk
        Allows removing a child via index

        :param key:
            Integer index of child
        NT)r‹  rŒ  Úpopru   r¡  r÷   r&   r&   r'   r·  ì  s   
	

zSequenceOf.__delitem__c                 c   s:   � | j du r
|  ¡  tdt| j ƒƒD ]}|  |¡V  qdS )zA
        :return:
            An iter() of child objects
        Nr   )r‹  rŒ  r%  r>   rª  )rE   rÌ   r&   r&   r'   r²   þ  s   €
ÿzSequenceOf.__iter__c                 C   s\   |du s|t u r
dS t|| jƒs ttdt| ƒt| jƒt|ƒƒƒ‚| D ]	}||kr+ dS q"dS )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)r�  r-   rå  r.   r
   r   )rE   Úitemrñ   r&   r&   r'   Ú__contains__  s   	ù
ÿzSequenceOf.__contains__c                 C   sL   | j du r	|  ¡  | j  |  |¡¡ | jdur!| j | j d j¡ d| _dS )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
        Nr×   T)r‹  rŒ  ræ   r°  ru   r   r¡  rü   r&   r&   r'   ræ   (  s   



zSequenceOf.appendc                 C   s\   | j du r	|  ¡  tƒ }| D ]}| |j|d�¡ q| ¡ | _d| _| jdkr,d| _dS dS )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
        NrÄ   r(   )	r‹  rŒ  r   r¸  rY   r¹  r–  r…   r‰   rò   r&   r&   r'   r˜  <  s   
	


ÿzSequenceOf._set_contentsc              
   C   sì   zOg | _ | jdu rW dS t| jƒ}d}||k rMt| j||d�\}}| jr+|| jf }n|}|r@t|Ž }t|ttfƒr@|j	dd� | j  
|¡ ||k sW dS W dS  ttfyu } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )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Ï  TrÑ  r   rÀ   )r‹  r–  r>   r   rå  r¨  r-   r‡  r¢  rŒ  ræ   r<   r.   rD   r   )rE   rÒ  rÔ  rÕ  r×  rñ   rK   rD   r&   r&   r'   rŒ  P  s.   

ö€ýzSequenceOf._parse_childrenc                 C   rr  )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å  rS   r&   r&   r'   r+   s  s   	zSequenceOf.specc              
   C   sš   | j du rdS | jdu rJ| jdu r| jdd� zdd„ | D ƒ| _W | jS  ttfyI } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww | jS )	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.
        NTrÑ  c                 S   s   g | ]}|j ‘qS r&   )r   )rÙ   rñ   r&   r&   r'   rÛ   �  s    z%SequenceOf.native.<locals>.<listcomp>r   r   rÀ   )rB   ru   r‹  rŒ  r<   r.   rD   r   )rE   rK   rD   r&   r&   r'   r   ~  s   



ü€ýzSequenceOf.nativec                    rá  )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)r¥   r¢  rb   r‹  r9   rÐ   ræ   rc   râ  r¦   r&   r'   rb   –  rã  zSequenceOf._copyc                 C   s@   | j du r	|  ¡  d| }t|| ƒ | D ]	}| |d ¡ qdS )ry   Nrz   r   )r‹  rŒ  r}   r~   rð   r&   r&   r'   r~   «  s   

ÿzSequenceOf.debugc                 C   s   |r| j |d� t | ¡S )r„   rÄ   )r˜  r#   rY   rµ   r&   r&   r'   rY   ¸  s   
zSequenceOf.dump)NNNNrŒ   rŽ   )!r�   r�   r‘   r’   r/   rA   rˆ   r‹  r–  r¡  rå  rL   r¶   rB   rä  r—  rª  r°  r°   rø   rç   r·  r²   rè  ræ   r˜  rŒ  r+   r   rb   r~   rY   r§   r&   r&   r¦   r'   r¢    s@    
+

7

#

r¢  c                   @   s<   e Zd ZdZdZdZdZdZdd„ Zdd	d
„Z	ddd„Z
dS )ÚSetzm
    Represents a set of fields (unordered) from ASN.1 as a Python object with a
    dict-like interface
    r   r   é   Nc                 C   s"  | j }i |_i |_g |_t|jƒD ]'\}}t|ƒdk r%|i f }||j|< ||j|d < ||jt|d |d ƒ< q|jdurO|j|jd  |j|jd  f|_	t|jƒD ]:\}}|j
duoc|d |j
v }|j	duoo|j	d |k}|st|r{|j d¡ qT|j |d |d |d |d df¡ qTdS r¼  )r9   rŽ  r½  r¾  rÉ   r�  r>   rË   r¿  rÀ  rÁ  ræ   rÂ  r&   r&   r'   r5   Ø  s&   


"(úz
Set._setupFc              
   C   s  | j }| jdu rM| jrKtgt| jƒ | _t| jƒD ]1\}\}}}d|v rJ|j| r4|j| \}}}}	}n
|  |¡\}}}}	}|  	||||	d¡| j|< qdS �zi }
t| j
ƒ}d}d}||k rÝt| j
||d�\}}|d |d f}| j |¡}|du r�ttd|t |d ¡t |d ¡|d ƒƒ‚|j| p™|  |¡\}}}}	}|du sª|r®t|tƒr®|}d}|r¸|||	|f }n|||	f }|rÑt|Ž }t|ttfƒrÑ|jdd	� ||
|< |d7 }||k s^t| jƒ}td|ƒD ]b}||
v rîqç|j| p÷|  |¡\}}}}	}|du �s|�rt|tƒ�r|}d}d
}|	�sd}n%d|	v�r$d|	v�r$d}nd|	v �r.t|
|< nd|	v �r<|di |	¤Ž|
|< |�rIttd|t| ƒƒƒ‚qçg | _td|ƒD ]}| j |
| ¡ �qRW dS  ttf�y‡ } z|jdd… }|jd dt| ƒ  f| |_|‚d}~ww )rÎ  NrI   r   rÏ  r   z¥
                        Data for field %s (%s class, %s method, tag %s) does
                        not match any of the field definitions
                        r   TrÑ  FrH   zU
                        Missing required field "%s" from %s
                        rÀ   r&   )r9   r–  r�  r�  r>   r‹  rÉ   r¾  r¯  r°  rB   r   r½  rp   r<   r
   r?   rÓ  rÊ  r·   r¨  r-   r‡  r¢  rŒ  r%  r   ræ   r.   rD   )rE   rÒ  r1   rÌ   r4   rÔ   r±  r²  r³  r´  Ú	child_maprÔ  rÕ  Ú
seen_fieldr×  rÎ   rÃ  rÇ  rñ   Útotal_fieldsrÓ   ÚmissingrK   rD   r&   r&   r'   rŒ  ó  sž   

€
øÿÜ
&ÿ


ûÿ	ÿ€ýzSet._parse_childrenc           	      C   sº   | j du r	|  ¡  g }t| j ƒD ]*\}}|j|d�}| j| \}}}d|v r2|d
i |¤Ž ¡ |kr2q| |j|f¡ q|jdd„ d� d dd	„ |D ƒ¡| _	d| _
| jdkr[d| _dS dS )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
        NrÄ   rI   c                 S   s   | d S r¯   r&   )Úctr&   r&   r'   Ú<lambda>|  s    z#Set._set_contents.<locals>.<lambda>)rš   r(   c                 S   r‰  rŽ   r&   )rÙ   rï  r&   r&   r'   rÛ   ~  rŠ  z%Set._set_contents.<locals>.<listcomp>r&   )r‹  rŒ  rÉ   rY   r�  ræ   r/   ÚsortrÝ   r–  r…   r‰   )	rE   rŠ   Úchild_tag_encodingsrÌ   rñ   Úchild_encodingrÓ   r+   r´  r&   r&   r'   r˜  b  s    


ÿzSet._set_contentsrŒ   )r�   r�   r‘   r’   rˆ   rA   r/   r½  r5   rŒ  r˜  r&   r&   r&   r'   ré  Ê  s    
oré  c                   @   s   e Zd ZdZdZddd„ZdS )ÚSetOfz~
    Represents a set (unordered) of a single type of values from ASN.1 as a
    Python object with a list-like interface
    rê  Fc                 C   s`   | j du r	|  ¡  g }| D ]}| |j|d�¡ qd t|ƒ¡| _d| _| jdkr.d| _dS dS )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
        NrÄ   r(   )	r‹  rŒ  ræ   rY   rÝ   r�  r–  r…   r‰   )rE   rŠ   Úchild_encodingsrñ   r&   r&   r'   r˜  Œ  s   


ÿzSetOf._set_contentsNrŒ   )r�   r�   r‘   r’   r/   r˜  r&   r&   r&   r'   rô  „  s    rô  c                   @   rw  )ÚEmbeddedPdvz
    A sequence structure
    é   Nry  r&   r&   r&   r'   rö  ¥  rz  rö  c                   @   r€  )ÚNumericStringzK
    Represents a numeric string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   rø  ­  r„  rø  c                   @   r€  )ÚPrintableStringzM
    Represents a printable string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   rú  ¶  r„  rú  c                   @   r€  )ÚTeletexStringzK
    Represents a teletex string from ASN.1 as a Python unicode string
    é   ÚteletexNrƒ  r&   r&   r&   r'   rü  ¿  r„  rü  c                   @   rw  )ÚVideotexStringzI
    Represents a videotex string from ASN.1 as a Python byte string
    é   Nry  r&   r&   r&   r'   rÿ  È  rz  rÿ  c                   @   r€  )Ú	IA5StringzH
    Represents an IA5 string from ASN.1 as a Python unicode string
    é   ÚasciiNrƒ  r&   r&   r&   r'   r  Ð  r„  r  c                   @   s   e Zd ZdZedd„ ƒZdS )ÚAbstractTimezK
    Represents a time from ASN.1 as a Python datetime.datetime object
    c                 C   sÌ   | j du rdS | jdu rct| ƒ}t d|¡}|s-| d¡}|  |¡}|jtj	d�| _| jS |  |dd… ¡}t
|dd… ƒ}t
|dd… ƒ}tt|ƒ|d	�}|dk rW||8 }n||7 }|jtj	d�| _| jS )
z 
        The a native Python datatype representation of this value

        :return:
            A datetime.datetime object in the UTC timezone or None
        Nz[-\+]ÚZ)Útzinfor   éûÿÿÿéüÿÿÿéþÿÿÿ)ÚhoursÚminutes)rB   ru   r   ÚreÚsearchr)  Ú_date_by_lenÚreplacer   Úutcr*  r   Úabs)rE   ÚstringÚhas_timezoneÚdater
  r  Údeltar&   r&   r'   r   Þ  s&   
	


ô
zAbstractTime.nativeN)r�   r�   r‘   r’   r¶   r   r&   r&   r&   r'   r  Ù  s    r  c                   @   ó$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚUTCTimezV
    Represents a UTC time from ASN.1 as a Python datetime.datetime object in UTC
    é   c                 C   s8   t |tƒr| d¡}tr| d¡}t | |¡ d| _dS )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%SZr  N)r-   r   ÚstrftimerP   r
  r  rC   ru   rü   r&   r&   r'   rC     s   



zUTCTime.setc                 C   s^   t |ƒ}t|dd… ƒ}|dk rd}nd}|dkr!t || d¡S |dkr-t || 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
        r   r   é2   Ú20Ú19r  ú
%Y%m%d%H%Mr‚  ú%Y%m%d%H%M%S)r>   r*  r   Ústrptime)rE   r  ÚstrlenÚyear_numr‚   r&   r&   r'   r  #  s   zUTCTime._date_by_lenN©r�   r�   r‘   r’   r/   rC   r  r&   r&   r&   r'   r    s
    r  c                   @   r  )ÚGeneralizedTimezŽ
    Represents a generalized time from ASN.1 as a Python datetime.datetime
    object or asn1crypto.util.extended_datetime object in UTC
    é   c                 C   s<   t |ttfƒr| d¡}tr| d¡}t | |¡ d| _dS )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%SZr  N)	r-   r   r   r  rP   r
  r  rC   ru   rü   r&   r&   r'   rC   G  s   


zGeneralizedTime.setc              	   C   s®   t |ƒ}d}|dkrd}n|dkrd}n|dkrd}n|dkr!d	}|rUt |ƒd
krO|dd
… dkrOt d|d
d…  |¡}td|j|j|j|j|j|j	|j
ƒS t ||¡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‚  r  é   r  rù  z%Y%m%d%H%M%S.%frT  r   Ú0000Ú2000)r>   r   r  r   ÚmonthÚdayÚhourÚminuteÚsecondÚmicrosecondr  )rE   r  r   Údate_formatÚtr&   r&   r'   r  ]  s2   ø
zGeneralizedTime._date_by_lenNr"  r&   r&   r&   r'   r#  ?  s
    r#  c                   @   r€  )ÚGraphicStringzK
    Represents a graphic string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   r0  ˆ  ó    r0  c                   @   r€  )ÚVisibleStringzK
    Represents a visible string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   r3  ’  r„  r3  c                   @   r€  )ÚGeneralStringzK
    Represents a general string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   r5  ›  r2  r5  c                   @   r€  )ÚUniversalStringzM
    Represents a universal string from ASN.1 as a Python unicode string
    é   z	utf-32-beNrƒ  r&   r&   r&   r'   r7  ¥  r„  r7  c                   @   r€  )ÚCharacterStringzM
    Represents a character string from ASN.1 as a Python unicode string
    é   r  Nrƒ  r&   r&   r&   r'   r9  ®  r2  r9  c                   @   r€  )Ú	BMPStringzG
    Represents a BMP string from ASN.1 as a Python unicode string
    é   z	utf-16-beNrƒ  r&   r&   r&   r'   r;  ¸  r„  r;  c                 C   s,  t d| t|ƒt|ƒf ƒ |jr!t d| t |jpd¡ d¡f ƒ |jduo/|jduo/|j	du}|r>t
 |j¡}t |j¡}|jdure|jD ]\}}t d| t |¡|f ƒ qF|rdt d| |||j	f ƒ n|jrv|rut d| |||j	f ƒ n|rƒt d	| |||j	f ƒ t d
| t |jpŒd¡ d¡f ƒ dS )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
    rï   z%s  Header: 0x%sr(   r[   Nz#%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)r€   r   rX   r…   ÚbinasciiÚhexlifyr
  rˆ   rA   r/   rÓ  rp   r?   r6   r7   rB   )r‚   rE   Ú
has_headerÚmethod_nameÚ
class_namerA   r/   r&   r&   r'   r}   Á  s6   "
ýÿÿ€€&r}   c                 C   sx   d| v r8| d dkr|   dd¡| d f| d< n| d dkr)|   dd¡| d f| d< | d= | d= d| v r:| d= dS dS dS )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
    rG   r6   Úclassr   r/   r7   N)rp   )rÔ   r&   r&   r'   rÁ   í  s   
ørÁ   c                 C   sž   t |ƒ d}d|vr| jdurd}n*t|d tƒr |d \}}n|d }d}| jdu r.d}n| jt| ks:| j|kr<d}| d¡| jkrFd}|rM|  	|¡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
    Fr7   Tr   r6   )
rÁ   r7   r-   rÐ   rA   r@   r/   rp   r6   rr   )r3   rÔ   rr   rA   r/   r&   r&   r'   rÒ      s&   
€

rÒ   c                 C   s¼   |du rdS |j }|j}t| ƒ d| v r)t| d tƒr"| d \}}nd}| d }nd| v rAt| d tƒr;| d \}}nd}| d }|durNt|tƒsNt| }|  d|¡}|  d|¡}||fS )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)
    NrÅ   r6   r   r7   rA   r/   )rA   r/   rÁ   r-   rÐ   r   r@   rp   )rÔ   r+   Úrequired_classÚrequired_tagr&   r&   r'   rË   (  s(   
rË   rT  r_  rb  rl  r$  r}  r  r÷  r‚  r†  rˆ  rê  rù  rû  )rý  r   r  r  r$  r1  r4  r6  r8  r:  r<  c	              
   C   s¼  |durt |ƒ |du rtS d}	|du�r~|od|v }
|
sº|js&|rºd|v rº|r0|di |¤Ž}n|ƒ }|j}t|ƒ}| }|}|}|}|}|pGd}|D ]X\}}||krdttdt|ƒt |¡t ||¡ƒƒ‚|dkrzttdt|ƒt	 d¡t	 ||¡ƒƒ‚||kr‰ttd	t|ƒ||ƒƒ‚t
|t|ƒƒ\}}|\}}}}}}||7 }|| }qJt||dd
idœŽ}||_||_||_d
}	�n`|rÆ|dd|i|¤Ž}n||d�}|tu rÑ�nIt|tƒ�r| | ||¡ z||j |_d}| ¡  W �n, ttf�y } z|jdd… }|jd dt|ƒ  f| |_|‚d}~ww | |jk�r.ttdt|ƒt |j¡t | | ¡ƒƒ‚||jk�re|dk�oB|jdk�oB|dk}|�rLt|tƒ�s_ttdt|ƒt	 |j¡t	 ||¡ƒƒ‚||_d
|_||jk�r}||jk�r}ttdt|ƒ|j|ƒƒ‚nœ|�rÞd|v �rÞtdd|i|¤Ž}|j}|}|}|�pšd}t|ƒD ]\}}t
|t|ƒƒ\}}|\}}}}}}||7 }|| }�qŸt||dd
idœŽ}||j |_| j|�pÕd7  _||_d
}	n<|tv�ròttdt | ¡t	 |¡|ƒƒ‚t| }||| d�}|dk�o
|jdk�o
|dk}|�rt|tƒ�rd
|_||_|	�s&||_|�p$d|_d|_|�r\z| |¡ W |S  ttf�y[ } z|jdd… }|jd dt|ƒ  f| |_|‚d}~ww |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
    NFrF   r6   r(   zš
                        Error parsing %s - explicitly-tagged class should have been
                        %s, but %s was found
                        r   z›
                        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
                        Tr¿   rB   r  r   rÀ   zˆ
                        Error parsing %s - class should have been %s, but %s was
                        found
                        s     zy
                            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
                )rB   rA   r&   ) rÁ   r�  r6   Úreversedr<   r
   r   r?   rp   rÓ  r   r>   r¨  r…   r‰   r·   r-   rÆ   rà   rB   r¹   r.   rD   rA   rˆ   r†   r‡   r/   r“   r#   Ú_UNIVERSAL_SPECSru   )rA   rˆ   r/   r‹   rB   Útrailerr+   r,   Únested_specÚ
header_setrF   r3   Úoriginal_explicitÚexplicit_infoÚparsed_classÚparsed_methodÚ
parsed_tagÚto_parseÚexplicit_headerÚexplicit_trailerÚexpected_classÚexpected_tagrÍ   r4   Úparsed_headerÚparsed_trailerrK   rD   Ú	ber_indefÚoriginal_valuer&   r&   r'   r¨  u  s  )

ù	
ù	ù	

€ý

ù	

ú	ú€

ú	û€ýr¨  c           	      C   sT   t | ƒ}t| ||ƒ\}}|r ||| kr || | }td| ƒ‚t|||dœŽ|fS )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
    rã   r¿   )r>   r   r<   r¨  )	r%   r¡   r+   r,   r"   Úencoded_lenrÍ   Únew_pointerrê   r&   r&   r'   r0   W  s   r0   rŒ   r  )r   NNF)hr’   Ú
__future__r   r   r   r   r   r   r=  rc   r+  r  Úsysr	  r	   Ú_errorsr
   Ú_ordereddictr   Ú_typesr   r   r   r   r   Úparserr   r   Úutilr   r   r   r   Úversion_infoÚ	cStringIOr   r   Úxranger%  rP   ÚioÚregisterr?   r@   rÓ  Úcompilerf  r:   r$   Úobjectr#   r•   r›   r†   r¨   r�  r·   rÆ   râ   rù   r  r  r  r  rB  rO  rS  rY  r[  r]  r^  r`  rx  r{  r|  r~  r�  r…  r‡  r¢  ré  rô  rö  rø  rú  rü  rÿ  r  r  r  r#  r0  r3  r5  r7  r9  r;  r}   rÁ   rÒ   rË   rF  r¨  r0   r&   r&   r&   r'   Ú<module>   sH  .
üøþ

6   I,G6   :  #{Q5Q  "XTN5 %) LD		      $   E ;!				.8I
	
	
	,(-ÿþýüûúùø	÷
öõôóòñðïä
  c