
    Eh1                         d dl mZmZ d dlZd dl g dZ G d d      Z G d de      Z G d	 d
e      Z G d de      Z	 G d de      Z
 G d de      Zd Zy)    )long_to_bytesbytes_to_longN)*)	DerObject
DerIntegerDerOctetStringDerNullDerSequenceDerObjectIdc                   Z    e Zd ZdZdddddddZd	 ed
      fdZd Zd Zd Z	d Z
ddZy	)r   zBase class for defining a single DER object.

        Instantiate this class ONLY when you have to decode a DER element.
        0                  )SEQUENCEz
BIT STRINGINTEGEROCTET STRINGNULLOBJECT IDENTIFIERN c                     t        |      s||| _        || _        yt        |      dk(  rt        |      | _        || _        y| j                  j                  |      | _        || _        y)zInitialize the DER object according to a specific type.

                The ASN.1 type is either specified as the ASN.1 string (e.g.
                'SEQUENCE'), directly with its numerical tag or with no tag
                at all (None).N   )isInttypeTaglenordtypeTagsgetpayload)selfASN1Typer!   s      Q/var/www/html/bid_assistant/venv/lib/python3.12/site-packages/Crypto/Util/asn1.py__init__zDerObject.__init__'   sa     ?h&6#+DL  '	 8}a''*8}  ' (,}}'8'8'B&    c                 :    | j                   |   | j                  k(  S N)r   r   )r"   r#   s     r$   isTypezDerObject.isType6   s    }}X.<<r&   c                 l    |dkD  r%t        |      }t        t        |      dz         |z   S t        |      S )zReturn a byte string that encodes the given payload length (in
                bytes) in a format suitable for a DER length tag (L).
                      )r   bchrr   )r"   
payloadLenencodings      r$   _lengthOctetszDerObject._lengthOctets9   s9     c>#0#<#CM#$56AAJ''r&   c                     t        | j                        | j                  t        | j                              z   | j                  z   S )z6Return a complete DER element, fully encoded as a TLV.)r-   r   r0   r   r!   r"   s    r$   encodezDerObject.encodeB   s6    DLL)D,>,>s4<<?P,QQTXT`T```r&   c                     t        ||         }|dk  r||dz   fS t        ||dz   |dz   |dz  z          }|dk  rt        d      ||dz   |dz  z   fS )a  Given a (part of a) DER element, and an index to the first byte of
                a DER length tag (L), return a tuple with the payload size,
                and the index of the first byte of the such payload (V).

                Raises a ValueError exception if the DER length is invalid.
                Raises an IndexError exception if the DER element is too short.
                r+   r   zNot a DER length tag.)bordr   
ValueError)r"   idxderlengthpayloadLengths        r$   
_decodeLenzDerObject._decodeLenF   st     c#h3; &s1u~- -c#a%Av}8M.N O #%()@AA%s1uftm'<==r&   c                    	 t        |d         | _        | j                  dz  dk(  rt        d      | j                  d|      \  }}|rt	        |      ||z   k7  rt        d      ||||z    | _        ||z   S # t        $ r t        d      w xY w)a  Decode a complete DER element, and re-initializes this
                object with it.

                @param derEle       A complete DER element. It must start with a DER T
                                    tag.
                @param noLeftOvers  Indicate whether it is acceptable to complete the
                                    parsing of the DER element and find that not all
                                    bytes in derEle have been used.
                @return             Index of the first unused byte in the given DER element.

                Raises a ValueError exception in case of parsing errors.
                Raises an IndexError exception if the DER element is too short.
                r      zUnsupported DER tagr   zNot a DER structureNot a valid DER SEQUENCE.)r5   r   r6   r;   r   r!   
IndexError)r"   derElenoLeftOversr9   r7   s        r$   decodezDerObject.decodeV   s    	F'+F1I LL4/$6&01F&G G'+q&'A&3v;3v:+F&01F&G G'-c#f*'= 6z! " F()DEEFs   A0A7 7Br   )__name__
__module____qualname____doc__r   br%   r)   r0   r3   r;   rB    r&   r$   r   r      sI    	 "&Td $dP %)!B% 	'	=	(	a	> 	"r&   r   c                   "    e Zd ZddZd ZddZy)r   c                 >    t         j                  | d       || _        y)z|Class to model an INTEGER DER element.

                Limitation: only non-negative values are supported.
                r   N)r   r%   valuer"   rL   s     r$   r%   zDerInteger.__init__q   s    
 ""43"
r&   c                     t        | j                        | _        t        | j                  d         dkD  rt	        d      | j                  z   | _        t
        j                  |       S )z>Return a complete INTEGER DER element, fully encoded as a TLV.r   r+   )r   rL   r!   r5   r-   r   r3   r2   s    r$   r3   zDerInteger.encodey   sL    ,TZZ8Q(,'+DzDLL'@ ''--r&   c                    t         j                  | ||      }| j                  | j                  d   k7  rt	        d      t        | j                  d         dkD  rt	        d      t        | j                        | _        |S )a  Decode a complete INTEGER DER element, and re-initializes this
                object with it.

                @param derEle       A complete INTEGER DER element. It must start with a DER
                                    INTEGER tag.
                @param noLeftOvers  Indicate whether it is acceptable to complete the
                                    parsing of the DER element and find that not all
                                    bytes in derEle have been used.
                @return             Index of the first unused byte in the given DER element.

                Raises a ValueError exception if the DER element is not a
                valid non-negative INTEGER.
                Raises an IndexError exception if the DER element is too short.
                r   zNot a DER INTEGER.r   r+   zNegative INTEGER.)	r   rB   r   r   r6   r5   r!   r   rL   )r"   r@   rA   	tlvLengths       r$   rB   zDerInteger.decode   sp     &,,T6;G	<<y!99(*>??Q(,(*=>>*4<<8
  r&   NrC   )rD   rE   rF   r%   r3   rB   rI   r&   r$   r   r   p   s    	#	.	!r&   r   c                   b    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd ZddZy)r
   a  Class to model a SEQUENCE DER element.

        This object behave like a dynamic Python sequence.
        Sub-elements that are INTEGERs, look like Python integers.
        Any other sub-element is a binary string encoded as the complete DER
        sub-element (TLV).
        Nc                 X    t         j                  | d       |dk(  rg | _        y|| _        y)zKInitialize the SEQUENCE DER object. Always empty
                initially.r   N)r   r%   _seq)r"   startSeqs     r$   r%   zDerSequence.__init__   s)     ""44T> "DI (DIr&   c                     | j                   |= y r(   rS   r"   ns     r$   __delitem__zDerSequence.__delitem__   s    IIaLr&   c                      | j                   |   S r(   rV   rW   s     r$   __getitem__zDerSequence.__getitem__   s    yy|#r&   c                 "    || j                   |<   y r(   rV   )r"   keyrL   s      r$   __setitem__zDerSequence.__setitem__   s    !&		#r&   c                 "    || j                   || y r(   rV   )r"   ijsequences       r$   __setslice__zDerSequence.__setslice__   s    !)		!Ar&   c                 "    | j                   ||= y r(   rV   r"   r`   ra   s      r$   __delslice__zDerSequence.__delslice__   s    IIacNr&   c                 H    | j                   t        d|      t        d|       S )Nr   )rS   maxre   s      r$   __getslice__zDerSequence.__getslice__   s    yyQ3q!955r&   c                 ,    t        | j                        S r(   )r   rS   r2   s    r$   __len__zDerSequence.__len__   s    499~%r&   c                 8    | j                   j                  |      S r(   )rS   appendr"   items     r$   rm   zDerSequence.append   s    yy''--r&   c                 H    t        t        t        | j                              S )z=Return the number of items in this sequence that are numbers.)r   filterr   rS   r2   s    r$   hasIntszDerSequence.hasInts   s    6%344r&   c                 j    | j                   xr& | j                         t        | j                         k(  S )z6Return True if all items in this sequence are numbers.)rS   rr   r   r2   s    r$   hasOnlyIntszDerSequence.hasOnlyInts   s$    yyCT\\^S^%CCr&   c                 $   t        d      | _        | j                  D ]  }	 | xj                  |z  c_         t        j	                  |       S #  	 | xj                  t        |      j	                         z  c_        n#  t        d      xY wY rxY w)a;  Return the DER encoding for the ASN.1 SEQUENCE, containing
                the non-negative integers and longs added to this object.

                Limitation: Raises a ValueError exception if it some elements
                in the sequence are neither Python integers nor complete DER INTEGERs.
                r   z&Trying to DER encode an unknown object)rH   r!   rS   r   r3   r6   r   rn   s     r$   r3   zDerSequence.encode   s      !u II cDc $ 4c !''--c!c(,
48H8O8O8Q(Q!c.89a.b(b )5s#   AB,A=<B=B

Bc                    g | _         	 t        j                  | ||      }| j                  | j                  d   k7  rt        d      d}|t        | j                        k  rt        | j                  |         }|| j                  d   k(  rQt               }||j                  | j                  |d       z  }| j                   j                  |j                         nR| j                  |dz   | j                        \  }}| j                   j                  | j                  |||z           ||z   }|t        | j                        k  r|S # t        $ r t        d      w xY w)a  Decode a complete SEQUENCE DER element, and re-initializes this
                object with it.

                @param derEle       A complete SEQUENCE DER element. It must start with a DER
                                    SEQUENCE tag.
                @param noLeftOvers  Indicate whether it is acceptable to complete the
                                    parsing of the DER element and find that not all
                                    bytes in derEle have been used.
                @return             Index of the first unused byte in the given DER element.

                DER INTEGERs are decoded into Python integers. Any other DER
                element is not decoded. Its validity is not checked.

                Raises a ValueError exception if the DER element is not a
                valid DER SEQUENCE.
                Raises an IndexError exception if the DER element is too short.
                r   zNot a DER SEQUENCE.r   r   Nr   r>   )rS   r   rB   r   r   r6   r   r!   r5   r   rm   rL   r;   r?   )	r"   r@   rA   rP   r7   r   
newIntegeritemLenitemIdxs	            r$   rB   zDerSequence.decode   sD   & 	F$-$4$4T6;$O	<<z)BB&01F&G G!#dll"33*.t||C/@*A#*DMM),D#D5?\
(+z/@/@cdAS/T(T(,		(8(89I9I(J:>//#a%PTP\P\:](,		(8(8c'RY/9Z([.5.? "#dll"33 !  " F()DEEFs   D=E	 	Er(   rC   )rD   rE   rF   rG   r%   rY   r[   r^   rc   rf   ri   rk   rm   rr   rt   r3   rB   rI   r&   r$   r
   r
      sI    		)	!	$	'	*	#	6	&	.	5	D	.$&	!r&   r
   c                   *    e Zd Z ed      fdZddZy)r   r   c                 >    t         j                  | d       || _        y )Nr   )r   r%   r!   rM   s     r$   r%   zDerOctetString.__init__   s    40r&   c                 j    t         j                  ||      }| j                  d      st        d      |S )Nr   zNot a valid OCTET STRING.r   rB   r)   r6   r"   r@   rA   ps       r$   rB   zDerOctetString.decode  s1    V[1{{>*899r&   NrC   )rD   rE   rF   rH   r%   rB   rI   r&   r$   r   r      s     u r&   r   c                       e Zd Zd Zy)r	   c                 0    t         j                  | d       y )Nr   r   r%   r2   s    r$   r%   zDerNull.__init__
  s    4(r&   N)rD   rE   rF   r%   rI   r&   r$   r	   r	   	  s    )r&   r	   c                       e Zd Zd ZddZy)r   c                 0    t         j                  | d       y )Nr   r   r2   s    r$   r%   zDerObjectId.__init__  s    4!45r&   c                 j    t         j                  ||      }| j                  d      st        d      |S )Nr   zNot a valid OBJECT IDENTIFIER.r}   r~   s       r$   rB   zDerObjectId.decode  s2    V[1{{./=>>r&   NrC   )rD   rE   rF   r%   rB   rI   r&   r$   r   r     s    6r&   r   c                 2    d}	 || z  }y# t         $ r Y yw xY w)Nr   r   )	TypeError)xtests     r$   r   r     s.    D	   s   
 	)Crypto.Util.numberr   r   sysCrypto.Util.py3compat__all__r   r   r
   r   r	   r   r   rI   r&   r$   <module>r      sg   . < 
 #
bQ" Q"f%! %!Ne!) e!N	Y 	)i )) r&   