HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/Crypto/Util/__pycache__/asn1.cpython-310.pyc
o

we�{�@s�ddlZddlmZmZmZmZddlmZmZgd�Z	ddd�Z
Gdd	�d	e�ZGd
d�de�Z
Gdd
�d
e
�ZGdd�de
�ZGdd�de
�ZGdd�de
�ZGdd�de
�ZGdd�de
�ZGdd�de
�ZdS)�N)�byte_string�b�bchr�bord)�
long_to_bytes�
bytes_to_long)�	DerObject�
DerInteger�DerOctetString�DerNull�DerSequence�DerObjectId�DerBitString�DerSetOfFcCs4d}z||}Wn
tyYdSw|p|dkS)NrF)�	TypeError)�x�only_non_negative�test�r�I/home/arjun/projects/env/lib/python3.10/site-packages/Crypto/Util/asn1.py�
_is_number!s�rc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�BytesIO_EOFzeThis class differs from BytesIO in that a ValueError exception is
    raised whenever EOF is reached.cCs||_d|_d|_dS�Nr)�_buffer�_index�	_bookmark)�self�
initial_bytesrrr�__init__.s
zBytesIO_EOF.__init__cCs|j|_dS�N)rr�rrrr�set_bookmark3�zBytesIO_EOF.set_bookmarkcCs |jdusJ�|j|j|j�Sr)rrrr rrr�data_since_bookmark6szBytesIO_EOF.data_since_bookmarkcCst|j�|jSr)�lenrrr rrr�remaining_data:�zBytesIO_EOF.remaining_datacCsH|j|}|t|j�krtd|t|j�f��|j|j|�}||_|S)Nz@Not enough data for DER decoding: expected %d bytes and found %d)rr$r�
ValueError)r�length�	new_index�resultrrr�read=s
zBytesIO_EOF.readcCst|�d�d�S)N�r)rr+r rrr�	read_byteF�zBytesIO_EOF.read_byteN)
�__name__�
__module__�__qualname__�__doc__rr!r#r%r+r-rrrrr*s	rc@sTeZdZdZ		ddd�Zdd�Zed	d
��Zdd�Zd
d�Z	ddd�Z
dd�ZdS)rzpBase class for defining a single DER object.

        This class should never be directly instantiated.
        N�FcCs�|dur	d|_dS|�|�}||_d||fvrtd��|dur-dd|B|�|�B|_dS|durBd|�|�B|_d||B|_dSd||B|_dS)a�Initialize the DER object according to a specific ASN.1 type.

                :Parameters:
                  asn1Id : integer
                    The universal DER tag number for this object
                    (e.g. 0x10 for a SEQUENCE).
                    If None, the tag is not known yet.

                  payload : byte string
                    The initial payload of the object (that it,
                    the content octets).
                    If not specified, the payload is empty.

                  implicit : integer
                    The IMPLICIT tag number to use for the encoded object.
                    It overrides the universal tag *asn1Id*.

                  constructed : bool
                    True when the ASN.1 type is *constructed*.
                    False when it is *primitive*.

                  explicit : integer
                    The EXPLICIT tag number to use for the encoded object.
                Nz1Explicit and implicit tags are mutually exclusive�� �)�
_tag_octet�_convertTag�payloadr'�_inner_tag_octet)r�asn1Idr9�implicit�constructed�explicitrrrrPs
zDerObject.__init__cCsPt|�st|�dkrt|d�}t|�r"d|kr!dks&td��td��|S)zvCheck if *tag* is a real DER tag.
                Convert it from a character to number if necessary.
                r,r�z
Wrong DER tag)rr$rr')r�tagrrrr8�s�zDerObject._convertTagcCs,|dkrt|�}tt|�d�|St|�S)zXBuild length octets according to BER/DER
                definite form.
                �r4)rrr$)r(�encodingrrr�_definite_form�szDerObject._definite_formcCsL|j}t|d�rt|j�|�t|j��|j}t|j�|�t|��|S)z?Return this DER element, fully encoded as a binary byte string.r:)r9�hasattrrr:rCr$r7)r�output_payloadrrr�encode�s
����zDerObject.encodecCsR|��}|dkr'|�|d@�}t|d�dkrtd��t|�}|dkr'td��|S)z%Decode DER length octets from a file.rArz$Invalid DER: length has leading zeroz5Invalid DER: length in long form but smaller than 128)r-r+rr'r)r�sr(�encoded_lengthrrr�
_decodeLen�szDerObject._decodeLencCs<t|�std��t|�}|�||�|��dkrtd��|S)aDecode a complete DER element, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string): A complete DER element.

                Raises:
                  ValueError: in case of parsing errors.
                zInput is not a byte stringr�-Unexpected extra data after the DER structure)rr'r�_decodeFromStreamr%)r�der_encoded�strictrGrrr�decode�szDerObject.decodecCs�|��}|jdur||jkrtd��n||_|�|�}|�|�|_t|d�rMt|j�}|��}||jkr8td��|�|�}|�|�|_|�	�dkrOtd��dSdS)z*Decode a complete DER element from a file.NzUnexpected DER tagr:zUnexpected internal DER tagrrJ)
r-r7r'rIr+r9rDrr:r%)rrGrM�idOctetr(�p�inner_octetrrrrK�s&

�




�	zDerObject._decodeFromStream)Nr3NFN�F)r/r0r1r2rr8�staticmethodrCrFrIrNrKrrrrrJs
�?
	
rc@�4eZdZdZd
dd�Zdd�Zdd	d
�Zdd�ZdS)r	a�Class to model a DER INTEGER.

        An example of encoding is::

          >>> from Crypto.Util.asn1 import DerInteger
          >>> from binascii import hexlify, unhexlify
          >>> int_der = DerInteger(9)
          >>> print hexlify(int_der.encode())

        which will show ``020109``, the DER encoding of 9.

        And for decoding::

          >>> s = unhexlify(b'020109')
          >>> try:
          >>>   int_der = DerInteger()
          >>>   int_der.decode(s)
          >>>   print int_der.value
          >>> except ValueError:
          >>>   print "Not a valid DER INTEGER"

        the output will be ``9``.

        :ivar value: The integer value
        :vartype value: integer
        rNcC�t�|dd|d|�||_dS)aUInitialize the DER object as an INTEGER.

                :Parameters:
                  value : integer
                    The value of the integer.

                  implicit : integer
                    The IMPLICIT tag to use for the encoded object.
                    It overrides the universal tag for INTEGER (2).
                �r3FN�rr�value�rrXr<r>rrrrs�
zDerInteger.__init__cCs||j}d|_	tt|d@��|j|_d|krdkr'nntd�|j|_d|kr1dkr4nnn|dL}qt�|�S)zIReturn the DER INTEGER, fully encoded as a
                binary string.r3T�r4ri�����)rXr9r�intrrF)r�numberrrrrF%s�
zDerInteger.encodeFcCstj|||d�S)a!Decode a complete DER INTEGER DER, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string): A complete INTEGER DER element.

                Raises:
                  ValueError: in case of parsing errors.
                �rM�rrN�rrLrMrrrrN4szDerInteger.decodecCs�t�|||�|r.t|j�dkrtd��t|j�dkr.t�d|jdd��ddkr.td��d|_d}|jD]}|jd	9_|jt|�7_|d
K}q6|jrbt|jd�d@rd|j|8_dSdSdS)z*Decode a complete DER INTEGER from a file.rz/Invalid encoding for DER INTEGER: empty payloadrVz>HNr4z.Invalid encoding for DER INTEGER: leading zeror,�r[)	rrKr$r9r'�struct�unpackrXr)rrGrM�bits�irrrrKAs,

�zDerInteger._decodeFromStream)rNNrR�r/r0r1r2rrFrNrKrrrrr	�s


r	c@s�eZdZdZd#dd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
d$dd�Zd$dd�Zdd�Zd%dd �Zd!d"�ZdS)&ra�Class to model a DER SEQUENCE.

        This object behaves like a dynamic Python sequence.

        Sub-elements that are INTEGERs behave like Python integers.

        Any other sub-element is a binary string encoded as a complete DER
        sub-element (TLV).

        An example of encoding is:

          >>> from Crypto.Util.asn1 import DerSequence, DerInteger
          >>> from binascii import hexlify, unhexlify
          >>> obj_der = unhexlify('070102')
          >>> seq_der = DerSequence([4])
          >>> seq_der.append(9)
          >>> seq_der.append(obj_der.encode())
          >>> print hexlify(seq_der.encode())

        which will show ``3009020104020109070102``, the DER encoding of the
        sequence containing ``4``, ``9``, and the object with payload ``02``.

        For decoding:

          >>> s = unhexlify(b'3009020104020109070102')
          >>> try:
          >>>   seq_der = DerSequence()
          >>>   seq_der.decode(s)
          >>>   print len(seq_der)
          >>>   print seq_der[0]
          >>>   print seq_der[:]
          >>> except ValueError:
          >>>   print "Not a valid DER SEQUENCE"

        the output will be::

          3
          4
          [4, 9, b'']

        NcCs.t�|dd|d�|durg|_dS||_dS)a�Initialize the DER object as a SEQUENCE.

                :Parameters:
                  startSeq : Python sequence
                    A sequence whose element are either integers or
                    other DER objects.

                  implicit : integer
                    The IMPLICIT tag to use for the encoded object.
                    It overrides the universal tag for SEQUENCE (16).
                �r3TN)rr�_seq)r�startSeqr<rrrr�s


zDerSequence.__init__cCs|j|=dSr�rh�r�nrrr�__delitem__�r"zDerSequence.__delitem__cC�
|j|Srrjrkrrr�__getitem__��
zDerSequence.__getitem__cCs||j|<dSrrj)r�keyrXrrr�__setitem__�szDerSequence.__setitem__cCs||j||�<dSrrj)rre�j�sequencerrr�__setslice__�r.zDerSequence.__setslice__cCs|j||�=dSrrj�rrersrrr�__delslice__�r&zDerSequence.__delslice__cCs|jtd|�td|��Sr)rh�maxrvrrr�__getslice__�szDerSequence.__getslice__cC�
t|j�Sr�r$rhr rrr�__len__�rpzDerSequence.__len__cC�|j�|�|Sr�rh�append�r�itemrrr�__iadd__��zDerSequence.__iadd__cCr}rr~r�rrrr�r�zDerSequence.appendTcs�fdd�|jD�}t|�S)z�Return the number of items in this sequence that are
                integers.

                Args:
                  only_non_negative (boolean):
                    If ``True``, negative integers are not counted in.
                csg|]	}t|��r|�qSr)r��.0r�rrr�
<listcomp>�sz'DerSequence.hasInts.<locals>.<listcomp>)rhr$)rr�itemsrr�r�hasInts�s	zDerSequence.hasIntscCs|jo|�|�t|j�kS)a�Return ``True`` if all items in this sequence are integers
                or non-negative integers.

                This function returns False is the sequence is empty,
                or at least one member is not an integer.

                Args:
                  only_non_negative (boolean):
                    If ``True``, the presence of negative integers
                    causes the method to return ``False``.)rhr�r$)rrrrr�hasOnlyInts�szDerSequence.hasOnlyIntscCsfd|_|jD]'}t|�r|j|7_qt|�r$|jt|���7_q|j|��7_qt�|�S)z�Return this DER SEQUENCE, fully encoded as a
                binary string.

                Raises:
                  ValueError: if some elements in the sequence are neither integers
                              nor byte strings.
                r3)r9rhrrr	rFrr�rrrrF�s

zDerSequence.encodeFcCs.||_tj|||d�}|r|��std��|S)a9Decode a complete DER SEQUENCE, and re-initializes this
                object with it.

                Args:
                  der_encoded (byte string):
                    A complete SEQUENCE DER element.
                  nr_elements (None or integer or list of integers):
                    The number of members the SEQUENCE can have
                  only_ints_expected (boolean):
                    Whether the SEQUENCE is expected to contain only integers.
                  strict (boolean):
                    Whether decoding must check for strict DER compliancy.

                Raises:
                  ValueError: in case of parsing errors.

                DER INTEGERs are decoded into Python integers. Any other DER
                element is not decoded. Its validity is not checked.
                r^zSome members are not INTEGERs)�_nr_elementsrrNr�r')rrLrM�nr_elements�only_ints_expectedr*rrrrN�s
zDerSequence.decodecCs�g|_t�|||�t|j�}|��dkrK|��t�}|�||�|jdkr0|j�|�	��nt
�}|�	�}|j||d�|j�|j�|��dksd}|j
durnz
t|j�|j
v}Wntymt|j�|j
k}Ynw|sytdt|j���dS)z+Decode a complete DER SEQUENCE from a file.rrVr^TNz1Unexpected number of members (%d) in the sequence)rhrrKrr9r%r!r7rr#r	rNrXr�r$rr')rrGrMrP�der�derInt�data�okrrrrK�s4

�
���zDerSequence._decodeFromStream�NN)T)FNF)r/r0r1r2rrmrorrrurwryr|r�rr�r�rFrNrKrrrrrXs"
*


rc@seZdZdZddd�ZdS)r
a�Class to model a DER OCTET STRING.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerOctetString
    >>> from binascii import hexlify, unhexlify
    >>> os_der = DerOctetString(b'\xaa')
    >>> os_der.payload += b'\xbb'
    >>> print hexlify(os_der.encode())

    which will show ``0402aabb``, the DER encoding for the byte string
    ``b'\xAA\xBB'``.

    For decoding:

    >>> s = unhexlify(b'0402aabb')
    >>> try:
    >>>   os_der = DerOctetString()
    >>>   os_der.decode(s)
    >>>   print hexlify(os_der.payload)
    >>> except ValueError:
    >>>   print "Not a valid DER OCTET STRING"

    the output will be ``aabb``.

    :ivar payload: The content of the string
    :vartype payload: byte string
    r3NcCst�|d||d�dS)ahInitialize the DER object as an OCTET STRING.

        :Parameters:
          value : byte string
            The initial payload of the object.
            If not specified, the payload is empty.

          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for OCTET STRING (4).
        �FN�rr)rrXr<rrrrAszDerOctetString.__init__)r3N�r/r0r1r2rrrrrr
#sr
c@seZdZdZdd�ZdS)rz"Class to model a DER NULL element.cCst�|dddd�dS)z$Initialize the DER object as a NULL.�r3NFr�r rrrrSszDerNull.__init__Nr�rrrrrPsrc@rT)r
a(Class to model a DER OBJECT ID.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerObjectId
    >>> from binascii import hexlify, unhexlify
    >>> oid_der = DerObjectId("1.2")
    >>> oid_der.value += ".840.113549.1.1.1"
    >>> print hexlify(oid_der.encode())

    which will show ``06092a864886f70d010101``, the DER encoding for the
    RSA Object Identifier ``1.2.840.113549.1.1.1``.

    For decoding:

    >>> s = unhexlify(b'06092a864886f70d010101')
    >>> try:
    >>>   oid_der = DerObjectId()
    >>>   oid_der.decode(s)
    >>>   print oid_der.value
    >>> except ValueError:
    >>>   print "Not a valid DER OBJECT ID"

    the output will be ``1.2.840.113549.1.1.1``.

    :ivar value: The Object ID (OID), a dot separated list of integers
    :vartype value: string
    �NcCrU)a�Initialize the DER object as an OBJECT ID.

        :Parameters:
          value : string
            The initial Object Identifier (e.g. "1.2.0.0.6.2").
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for OBJECT ID (6).
          explicit : integer
            The EXPLICIT tag to use for the encoded object.
        �r3FNrWrYrrrrws
zDerObjectId.__init__cCs�dd�|j�d�D�}t|�dkrtd��td|d|d�|_|dd	�D]5}|dkr2dg}ng}|rF|�d|d
@dB�|dL}|s6|d
d
M<|jd�dd�|D��7_q(t�	|�S)zCReturn the DER OBJECT ID, fully encoded as a
        binary string.cS�g|]}t|��qSr)r\r�rrrr���z&DerObjectId.encode.<locals>.<listcomp>�.rVz$Not a valid Object Identifier string�(rr,NrAr4����r3cSr�r)rr�rrrr��r�)
rX�splitr$r'rr9�insert�joinrrF)r�comps�v�encrrrrF�s� 
zDerObjectId.encodeFcC�t�|||�S)abDecode a complete DER OBJECT ID, and re-initializes this
        object with it.

        Args:
            der_encoded (byte string):
                A complete DER OBJECT ID.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        r_r`rrrrN�szDerObjectId.decodecCs�t�|||�t|j�}dd�t|��d�D�}d}|��r;|��}|d|d@}|d@s7|�t|��d}|��sd�	|�|_
dS)	z,Decode a complete DER OBJECT ID from a file.cSr�r)�strr�rrrr��r�z1DerObjectId._decodeFromStream.<locals>.<listcomp>r�rr4rAr�N)rrKrr9�divmodr-r%rr�r�rX)rrGrMrPr�r��crrrrK�s
�zDerObjectId._decodeFromStream)r�NNrRrfrrrrr
Ys

r
c@rT)ra�Class to model a DER BIT STRING.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerBitString
    >>> bs_der = DerBitString(b'\xAA')
    >>> bs_der.value += b'\xBB'
    >>> print(bs_der.encode().hex())

    which will show ``030300aabb``, the DER encoding for the bit string
    ``b'\xAA\xBB'``.

    For decoding:

    >>> s = bytes.fromhex('030300aabb')
    >>> try:
    >>>   bs_der = DerBitString()
    >>>   bs_der.decode(s)
    >>>   print(bs_der.value.hex())
    >>> except ValueError:
    >>>   print "Not a valid DER BIT STRING"

    the output will be ``aabb``.

    :ivar value: The content of the string
    :vartype value: byte string
    r3NcCs6t�|dd|d|�t|t�r|��|_dS||_dS)a�Initialize the DER object as a BIT STRING.

        :Parameters:
          value : byte string or DER object
            The initial, packed bit string.
            If not specified, the bit string is empty.
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for OCTET STRING (3).
          explicit : integer
            The EXPLICIT tag to use for the encoded object.
        �r3FN)rr�
isinstancerFrXrYrrrr�s

zDerBitString.__init__cCsd|j|_t�|�S)zBReturn the DER BIT STRING, fully encoded as a
        byte string.�)rXr9rrFr rrrrF�s
zDerBitString.encodeFcCr�)aTDecode a complete DER BIT STRING, and re-initializes this
        object with it.

        Args:
            der_encoded (byte string): a complete DER BIT STRING.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        r_r`rrrrN�s
zDerBitString.decodecCsRt�|||�|jrt|jd�dkrtd��d|_|jr'|jdd�|_dSdS)z1Decode a complete DER BIT STRING DER from a file.rzNot a valid BIT STRINGr3r,N)rrKr9rr'rX)rrGrMrrrrKs�zDerBitString._decodeFromStream)r3NNrRrfrrrrr�s

rc@sTeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zddd�Z	dd�Z
dd�ZdS)ra�Class to model a DER SET OF.

    An example of encoding is:

    >>> from Crypto.Util.asn1 import DerBitString
    >>> from binascii import hexlify, unhexlify
    >>> so_der = DerSetOf([4,5])
    >>> so_der.add(6)
    >>> print hexlify(so_der.encode())

    which will show ``3109020104020105020106``, the DER encoding
    of a SET OF with items 4,5, and 6.

    For decoding:

    >>> s = unhexlify(b'3109020104020105020106')
    >>> try:
    >>>   so_der = DerSetOf()
    >>>   so_der.decode(s)
    >>>   print [x for x in so_der]
    >>> except ValueError:
    >>>   print "Not a valid DER SET OF"

    the output will be ``[4, 5, 6]``.
    NcCs>t�|dd|d�g|_d|_|r|D]	}|�|�qdSdS)a9Initialize the DER object as a SET OF.

        :Parameters:
          startSet : container
            The initial set of integers or DER encoded objects.
          implicit : integer
            The IMPLICIT tag to use for the encoded object.
            It overrides the universal tag for SET OF (17).
        �r3TN)rrrh�
_elemOctet�add)r�startSetr<�errrr1s
�zDerSetOf.__init__cCrnrrjrkrrrroFrpzDerSetOf.__getitem__cCrzr)�iterrhr rrr�__iter__IrpzDerSetOf.__iter__cCrzrr{r rrrr|LrpzDerSetOf.__len__cCslt|�rd}nt|t�r|j}nt|d�}|j|kr'|jdur$td��||_||jvr4|j�|�dSdS)z�Add an element to the set.

        Args:
            elem (byte string or integer):
              An element of the same type of objects already in the set.
              It can be an integer or a DER encoded object.
        rVrNz&New element does not belong to the set)	rr�rr7rr�r'rhr)r�elem�eorrrr�Os	



�zDerSetOf.addFcCr�)a�Decode a complete SET OF DER element, and re-initializes this
        object with it.

        DER INTEGERs are decoded into Python integers. Any other DER
        element is left undecoded; its validity is not checked.

        Args:
            der_encoded (byte string): a complete DER BIT SET OF.
            strict (boolean):
                Whether decoding must check for strict DER compliancy.

        Raises:
            ValueError: in case of parsing errors.
        r_r`rrrrNgszDerSetOf.decodecCs�g|_t�|||�t|j�}d}|��dkr\|��t�}|�||�|dkr,|j}n	||jkr5td��|dkrB|j�	|�
��nt�}|�|�
�|�|j�	|j
�|��dksdSdS)z)Decode a complete DER SET OF from a file.r�rz)Not all elements are of the same DER typerVN)rhrrKrr9r%r!r7r'rr#r	rNrX)rrGrMrP�
setIdOctetr�r�rrrrKys$

�zDerSetOf._decodeFromStreamcCsfg}|jD]}t|�rt|���}nt|t�r|��}n|}|�|�q|��d�|�|_	t�|�S)zRReturn this SET OF DER element, fully encoded as a
        binary string.
        r3)
rhrr	rFr�rr�sortr�r9)r�orderedr��bysrrrrF�s



zDerSetOf.encoder�rR)r/r0r1r2rror�r|r�rNrKrFrrrrrs

!rrR)rb�Crypto.Util.py3compatrrrr�Crypto.Util.numberrr�__all__r�objectrrr	rr
rr
rrrrrr�<module>s 
	 0_L-	dY