
    iI                        S SK r S SKJrJrJr  S SKJrJr  / SQrSS jr	 " S S\
5      r " S S	\
5      r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)    N)byte_stringbchrbord)long_to_bytesbytes_to_long)		DerObject
DerInteger
DerBooleanDerOctetStringDerNullDerSequenceDerObjectIdDerBitStringDerSetOfc                 V    Sn X-   nU(       + =(       d    U S:  $ ! [          a     gf = f)Nr   F)	TypeError)xonly_non_negativetests      K/var/www/html/trading/venv/lib/python3.13/site-packages/Crypto/Util/asn1.py
_is_numberr   (   s;    Dx ! *AF*  s    
((c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)BytesIO_EOF1   zaThis class differs from BytesIO in that a ValueError exception is
raised whenever EOF is reached.c                 ,    Xl         SU l        S U l        g Nr   )_buffer_index	_bookmark)selfinitial_bytess     r   __init__BytesIO_EOF.__init__5   s    $    c                 &    U R                   U l        g N)r   r   r    s    r   set_bookmarkBytesIO_EOF.set_bookmark:   s    r$   c                 f    U R                   c   eU R                  U R                   U R                   $ r&   r   r   r   r'   s    r   data_since_bookmarkBytesIO_EOF.data_since_bookmark=   s+    ~~)))||DNN4;;77r$   c                 F    [        U R                  5      U R                  -
  $ r&   )lenr   r   r'   s    r   remaining_dataBytesIO_EOF.remaining_dataA   s    4<< 4;;..r$   c                     U R                   U-   nU[        U R                  5      :  a#  [        SU[        U R                  5      4-  5      eU R                  U R                   U nX l         U$ )Nz@Not enough data for DER decoding: expected %d bytes and found %d)r   r/   r   
ValueError)r    length	new_indexresults       r   readBytesIO_EOF.readD   sp    KK&(	s4<<((_clnqrvr~r~n  cA  A  B  Bdkk)4r$   c                 <    [        U R                  S5      S   5      $ )N   r   )r   r7   r'   s    r   	read_byteBytesIO_EOF.read_byteM   s    DIIaLO$$r$   r+   N)__name__
__module____qualname____firstlineno____doc__r"   r(   r,   r0   r7   r;   __static_attributes__ r$   r   r   r   1   s%    '
%8/%r$   r   c                   X    \ rS rSrSr  SS jrS r\S 5       rS r	S r
SS	 jrS
 rSrg)r   Q   z`Base class for defining a single DER object.

This class should never be directly instantiated.
Nc                     Uc  SU l         gU R                  U5      nX l        U(       a  SOSnSXS4;  a  [        S5      eUb  SU-  U R                  U5      -  U l         gUb#  SU R                  U5      -  U l         Xa-  U l        gXa-  U l         g)a  Initialize the DER object according to a specific ASN.1 type.

:Parameters:
  asn1Id : integer or byte
    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 or byte
    The IMPLICIT tag number (< 0x1F) to use for the encoded object.
    It overrides the universal tag *asn1Id*.
    It cannot be combined with the ``explicit`` parameter.
    By default, there is no IMPLICIT tag.

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

  explicit : integer or byte
    The EXPLICIT tag number (< 0x1F) to use for the encoded object.
    It cannot be combined with the ``implicit`` parameter.
    By default, there is no EXPLICIT tag.
N    r   z1Explicit and implicit tags are mutually exclusive      )
_tag_octet_convertTagpayloadr3   _inner_tag_octet)r    asn1IdrL   implicitconstructedexplicitconstructed_bits          r   r"   DerObject.__init__W   s    > >&*DO))&1&  +6$433$ &; < < '&*_&<t?O?OPX?Y&YDO)&1D4D4DX4N&NDO,;,DD) '6&>DOr$   c                     [        U5      (       d  [        U5      S:X  a  [        US   5      n[        U5      (       a  SUs=::  a  S:  d  O  [        S5      eU$ )z_Check if *tag* is a real DER tag (5 bits).
Convert it from a character to number if necessary.
r:   r      zWrong DER tag)r   r/   r   r3   )r    tags     r   rK   DerObject._convertTag   sH     "#3x1}"3q6l"3AOtO$_55
r$   c                 n    U S:  a%  [        U 5      n[        [        U5      S-   5      U-   $ [        U 5      $ )z8Build length octets according to BER/DER
definite form.
   rH   )r   r   r/   )r4   encodings     r   _definite_formDerObject._definite_form   s8    
 C<#0#8#CMC$788CCF|#r$   c                 0   U R                   n[        U S5      (       aG  [        U R                  5      U R	                  [        U R                   5      5      -   U R                   -   n[        U R                  5      U R	                  [        U5      5      -   U-   $ )z?Return this DER element, fully encoded as a binary byte string.rM   )rL   hasattrr   rM   r[   r/   rJ   )r    output_payloads     r   encodeDerObject.encode   s     "& 4!344&*4+@+@&A&*&9&9#dll:K&L'M&*ll'3N T__-++C,?@A&' (r$   c                     UR                  5       nUS:  aM  UR                  US-  5      n[        US   5      S:X  a  [        S5      e[	        U5      nUS::  a  [        S5      eU$ )z%Decode DER length octets from a file.rY   r   z$Invalid DER: length has leading zeroz5Invalid DER: length in long form but smaller than 128)r;   r7   r   r3   r   )r    sr4   encoded_lengths       r   
_decodeLenDerObject._decodeLen   sh     C<%&VVFTM%:NN1-.!3()OPP*>:F}()`aar$   c                     [        U5      (       d  [        S5      e[        U5      nU R                  X25        UR	                  5       S:  a  [        S5      eU $ )zDecode 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.
zInput is not a byte stringr   -Unexpected extra data after the DER structure)r   r3   r   _decodeFromStreamr0   )r    der_encodedstrictrc   s       r   decodeDerObject.decode   sW     #;//$%ABB,&&q1 ##%)$%TUUr$   c                    UR                  5       nU R                  b  X0R                  :w  a  [        S5      eOX0l        U R                  U5      nUR	                  U5      U l        [        U S5      (       a  [        U R
                  5      nUR                  5       nX`R                  :w  a  [        S5      eU R                  U5      nUR	                  U5      U l        UR                  5       S:  a  [        S5      egg)z*Decode a complete DER element from a file.NzUnexpected DER tagrM   zUnexpected internal DER tagr   rh   )
r;   rJ   r3   re   r7   rL   r^   r   rM   r0   )r    rc   rk   idOctetr4   pinner_octets          r   ri   DerObject._decodeFromStream   s     ++-??.//1()=>> 2 '.O+ vvf~ 4!344#DLL1A"#++-K"&;&;;()FGG!__Q/F#$66&>DL '')A-()XYY . 5r$   )rM   rJ   rL   )Nr$   NFNF)r=   r>   r?   r@   rA   r"   rK   staticmethodr[   r`   re   rl   ri   rB   rC   r$   r   r   r   Q   sE    	
 ?C15D	?L
	 
	$ 
	$	(&		.	Zr$   r   c                   8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)r	   i  a8  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
Nc                 D    [         R                  U SSUSU5        Xl        g)zInitialize 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).
   r$   FNr   r"   valuer    ry   rO   rQ   s       r   r"   DerInteger.__init__#  s$     ""4sH#((4"
r$   c                 0   U R                   nSU l         [        [        US-  5      5      U R                  -   U l        SUs=::  a  S::  a   O  O[        S5      U R                  -   U l        SUs=::  a  S::  a  O  OOUS-  nMn  [        R                  U 5      $ )z9Return the DER INTEGER, fully encoded as a
binary string.r$      rH   r   i   )ry   rL   r   intr   r`   )r    numbers     r   r`   DerInteger.encode3  s     "#'FSL(9#:T\\#IDLf++'+DzDLL'@v,,qLF  !''--r$   c                 *    [         R                  XUS9$ )zDecode a DER-encoded INTEGER, and re-initializes this
object with it.

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

Raises:
  ValueError: in case of parsing errors.
rk   r   rl   r    rj   rk   s      r   rl   DerInteger.decodeB  s     !''&'IIr$   c                 \   [         R                  XU5        U(       as  [        U R                  5      S:X  a  [	        S5      e[        U R                  5      S:  a6  [
        R                  " SU R                  SS 5      S   S:  a  [	        S5      eSU l        SnU R                   H;  nU =R                  S	-  sl        U =R                  [        U5      -  sl        US
-  nM=     U R                  (       a7  [        U R                  S   5      S-  (       a  U =R                  U-  sl        ggg)z*Decode a complete DER INTEGER from a file.r   z/Invalid encoding for DER INTEGER: empty payloadrw   z>HNrH   z.Invalid encoding for DER INTEGER: leading zeror:      r~   )	r   ri   r/   rL   r3   structunpackry   r   )r    rc   rk   bitsis        r   ri   DerInteger._decodeFromStreamO  s     ++DV<4<<(A-()Z[[4<<(A-&--dllSUTUFV2WXY2Z]a2a()YZZ 
AJJ#%JJJ$q')JQJD & <<Da$9D$@JJ$&J %A<r$   rL   ry   )r   NNrs   
r=   r>   r?   r@   rA   r"   r`   rl   ri   rB   rC   r$   r   r	   r	     s    	6	# 	.	J	'r$   r	   c                   8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)r
   if  a  Class to model a DER-encoded BOOLEAN.

An example of encoding is::

>>> from Crypto.Util.asn1 import DerBoolean
>>> bool_der = DerBoolean(True)
>>> print(bool_der.encode().hex())

which will show ``0101ff``, the DER encoding of True.

And for decoding::

>>> s = bytes.fromhex('0101ff')
>>> try:
>>>   bool_der = DerBoolean()
>>>   bool_der.decode(s)
>>>   print(bool_der.value)
>>> except ValueError:
>>>   print "Not a valid DER BOOLEAN"

the output will be ``True``.

:ivar value: The boolean value
:vartype value: boolean
Nc                 D    [         R                  U SSUSU5        Xl        g)a6  Initialize the DER object as a BOOLEAN.

Args:
  value (boolean):
    The value of the boolean. Default is False.

  implicit (integer or byte):
    The IMPLICIT tag number (< 0x1F) to use for the encoded object.
    It overrides the universal tag for BOOLEAN (1).
    It cannot be combined with the ``explicit`` parameter.
    By default, there is no IMPLICIT tag.

  explicit (integer or byte):
    The EXPLICIT tag number (< 0x1F) to use for the encoded object.
    It cannot be combined with the ``implicit`` parameter.
    By default, there is no EXPLICIT tag.
r:   r$   FNrx   rz   s       r   r"   DerBoolean.__init__  s!    & 	4sHeXF
r$   c                 `    U R                   (       a  SOSU l        [        R                  U 5      $ )z9Return the DER BOOLEAN, fully encoded as a binary string.       ry   rL   r   r`   r'   s    r   r`   DerBoolean.encode  s$     #'**w'%%r$   c                 .    [         R                  XU5      $ )zDecode a DER-encoded BOOLEAN, and re-initializes this object with it.

Args:
    der_encoded (byte string): A DER-encoded BOOLEAN.

Raises:
    ValueError: in case of parsing errors.
r   r   s      r   rl   DerBoolean.decode  s     6::r$   c                    [         R                  XU5        [        U R                  5      S:w  a  [	        S5      e[        U R                  S   5      S:X  a  SU l        g[        U R                  S   5      S:X  a  SU l        g[	        S5      e)	z)Decode a DER-encoded BOOLEAN from a file.r:   z7Invalid encoding for DER BOOLEAN: payload is not 1 byter   Fr}   TzInvalid payload for DER BOOLEANN)r   ri   r/   rL   r3   r   ry   r    rc   rk   s      r   ri   DerBoolean._decodeFromStream  su     	##DV4t||!VWWQ A%DJ$,,q/"d*DJ>??r$   r   )FNNrs   r   rC   r$   r   r
   r
   f  s    2,&
;@r$   r
   c                       \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rS rSS jrSS jrS rSS jrS rSrg)r   i  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'']

Nc                 Z    [         R                  U SSUSU5        Uc  / U l        gXl        g)ac  Initialize the DER object as a SEQUENCE.

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

  implicit : integer or byte
    The IMPLICIT tag number (< 0x1F) to use for the encoded object.
    It overrides the universal tag for SEQUENCE (16).
    It cannot be combined with the ``explicit`` parameter.
    By default, there is no IMPLICIT tag.

  explicit : integer or byte
    The EXPLICIT tag number (< 0x1F) to use for the encoded object.
    It cannot be combined with the ``implicit`` parameter.
    By default, there is no EXPLICIT tag.
   r$   TN)r   r"   _seq)r    startSeqrO   rQ   s       r   r"   DerSequence.__init__  s.    ( ""4sHdHM# "DI (Ir$   c                     U R                   U	 g r&   r   r    ns     r   __delitem__DerSequence.__delitem__   s    IIaLr$   c                      U R                   U   $ r&   r   r   s     r   __getitem__DerSequence.__getitem__  s    yy|#r$   c                      X R                   U'   g r&   r   )r    keyry   s      r   __setitem__DerSequence.__setitem__  s    !&		#r$   c                     X0R                   X& g r&   r   )r    r   jsequences       r   __setslice__DerSequence.__setslice__	  s    !)		!r$   c                      U R                   X2	 g r&   r   r    r   r   s      r   __delslice__DerSequence.__delslice__  s    IIacNr$   c                 H    U R                   [        SU5      [        SU5       $ r   )r   maxr   s      r   __getslice__DerSequence.__getslice__  s    yyQ3q!955r$   c                 ,    [        U R                  5      $ r&   r/   r   r'   s    r   __len__DerSequence.__len__  s    499~%r$   c                 <    U R                   R                  U5        U $ r&   r   appendr    items     r   __iadd__DerSequence.__iadd__      		  &r$   c                 <    U R                   R                  U5        U $ r&   r   r   s     r   r   DerSequence.append  r   r$   c                 <    U R                   R                  X5        U $ r&   )r   insert)r    indexr   s      r   r   DerSequence.insert  s    		  -r$   c                 ~    U R                    Vs/ s H  n[        X!5      (       d  M  UPM     nn[        U5      $ s  snf )zReturn the number of items in this sequence that are
integers.

Args:
  only_non_negative (boolean):
    If ``True``, negative integers are not counted in.
)r   r   r/   )r    r   r   itemss       r   hasIntsDerSequence.hasInts!  s3     %)IIRIqA1QIR5z! Ss   ::c                 v    U R                   =(       a'    U R                  U5      [        U R                   5      :H  $ )a9  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``.)r   r   r/   )r    r   s     r   hasOnlyIntsDerSequence.hasOnlyInts-  s*     yyVT\\2C%DDII%VVr$   c                 p   SU l         U R                   H  n[        U5      (       a  U =R                   U-  sl         M*  [        U5      (       a.  U =R                   [	        U5      R                  5       -  sl         Mh  U =R                   UR                  5       -  sl         M     [        R                  U 5      $ )zReturn this DER SEQUENCE, fully encoded as a
binary string.

Raises:
  ValueError: if some elements in the sequence are neither integers
              nor byte strings.
r$   )rL   r   r   r   r	   r`   r   r   s     r   r`   DerSequence.encode:  s      # IID"4((,#D))
4(8(?(?(AA5 & !''--r$   c                     X0l         [        R                  XUS9nU(       a   U R                  5       (       d  [	        S5      eU$ )aI  Decode 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   zSome members are not INTEGERs)_nr_elementsr   rl   r   r3   )r    rj   rk   nr_elementsonly_ints_expectedr6   s         r   rl   DerSequence.decodeL  sA    * %0!"))$F)K%d.>.>.@.@$%DEEr$   c                 :   / U l         [        R                  XU5        [        U R                  5      nUR                  5       S:  a  UR                  5         [        5       nUR                  X25        UR                  S:w  a*  U R                   R                  UR                  5       5        ON[        5       nUR                  5       nUR                  XbS9  U R                   R                  UR                  5        UR                  5       S:  a  M  SnU R                  b#   [        U R                   5      U R                  ;   nU(       d!  [!        S[        U R                   5      -  5      eg! [         a%    [        U R                   5      U R                  :H  n NWf = f)z+Decode a complete DER SEQUENCE from a file.r   rw   r   TNz1Unexpected number of members (%d) in the sequence)r   r   ri   r   rL   r0   r(   rJ   r   r,   r	   rl   ry   r   r/   r   r3   )r    rc   rk   rp   derderIntdataoks           r   ri   DerSequence._decodeFromStreami  s]    	 ++DV<  -&&(1,NN$#+C))!4 ~~-		(()>)>)@A!+ 446d:		((6 &&(1, $$0A ^t/@/@@ $ &8:=dii.&I J J  % A ^t/@/@@As    "E+ +,FF)r   r   rL   )NNN)T)FNF)r=   r>   r?   r@   rA   r"   r   r   r   r   r   r   r   r   r   r   r   r   r`   rl   ri   rB   rC   r$   r   r   r     sZ    (	T	)8	!	$	'	*	#	6	&			
	"	W	.$	:"	Jr$   r   c                   "    \ rS rSrSrSS jrSrg)r   i  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
Nc                 4    [         R                  U SXS5        g)a(  Initialize 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r   r"   )r    ry   rO   s      r   r"   DerOctetString.__init__  s     	4u>r$   rC   )r$   Nr=   r>   r?   r@   rA   r"   rB   rC   r$   r   r   r     s    :?r$   r   c                       \ rS rSrSrS rSrg)r   i  z"Class to model a DER NULL element.c                 6    [         R                  U SSSS5        g)z$Initialize the DER object as a NULL.   r$   NFr   r'   s    r   r"   DerNull.__init__  s     	4sD%8r$   rC   Nr   rC   r$   r   r   r     s
    ,9r$   r   c                   8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)r   i  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
Nc                 D    [         R                  U SSUSU5        Xl        g)aJ  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.
   r$   FNrx   rz   s       r   r"   DerObjectId.__init__  s!     	4sHeXF
r$   c                    U R                   R                  S5       Vs/ s H  n[        U5      PM     nn[        U5      S:  a  [	        S5      eUS   S:  a  [	        S5      eUS   S:  a  US   S:  a  [	        S5      eS	US   -  US   -   /USS
 -   n/ n[        U5       HJ  nUR                  US-  5        US-  nU(       d  M%  UR                  US-  S-  5        US-  nU(       a  M%  ML     SR                  [        U5       Vs/ s H  n[        U5      PM     sn5      U l	        [        R                  U 5      $ s  snf s  snf )z;Return the DER OBJECT ID, fully encoded as a
binary string..rw   z$Not a valid Object Identifier stringr   z!First component must be 0, 1 or 2r:   '   z#Second component must be 39 at most(   NrY      rH   r$   )ry   splitr   r/   r3   reversedr   joinr   rL   r   r`   )r    r   compssubcompsrZ   vs         r   r`   DerObjectId.encode  s?    "&!1!1#!67!6AQ!67u:>CDD8a<@AA8a<E!HrMBCCqME!H,-ab	9(#AOOAH%!GA!TT 12a ! $ xx(82D E2DQa2D EF%%) 8& !Fs   D>Ec                 .    [         R                  XU5      $ )a  Decode 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   s      r   rl   DerObjectId.decode  s     6::r$   c                 8   [         R                  XU5        [        U R                  5      n/ nSnUR	                  5       (       aO  UR                  5       nUS-  US-  -   nUS-  (       d  UR                  U5        SnUR	                  5       (       a  MO  [        U5      S:X  a  [        S5      eUS   S:  a  SUS   /USS& O$US   S	:  a  SUS   S-
  /USS& OS
US   S	-
  /USS& SR                  U Vs/ s H  n[        U5      PM     sn5      U l        gs  snf )z,Decode a complete DER OBJECT ID from a file.r   r   rY   rH   zEmpty payloadr   Nr:   P   rw   r   )r   ri   r   rL   r0   r;   r   r/   r3   r   strry   )r    rc   rk   rp   r   r   cr   s           r   ri   DerObjectId._decodeFromStream  s     	##DV4 %  AaAH%AH"    x=A_--A;x{+HRaLa[2x{R/0HRaLx{R/0HRaLXXx8x!s1vx89
8s   5Dr   ) NNrs   r   rC   r$   r   r   r     s    :&4; :r$   r   c                   8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)r   i:  aV  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
Nc                     [         R                  U SSUSU5        [        U[         5      (       a  UR                  5       U l        gXl        g)ay  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 BIT STRING (3).
  explicit : integer
    The EXPLICIT tag to use for the encoded object.
   r$   FN)r   r"   
isinstancer`   ry   rz   s       r   r"   DerBitString.__init__W  s=     	4sHeXF eY''DJJr$   c                 T    SU R                   -   U l        [        R                  U 5      $ )z:Return the DER BIT STRING, fully encoded as a
byte string.r   r   r'   s    r   r`   DerBitString.encodel  s%    
 +%%r$   c                 .    [         R                  XU5      $ )a  Decode 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   s      r   rl   DerBitString.decodet  s     6::r$   c                     [         R                  XU5        U R                  (       a'  [        U R                  S   5      S:w  a  [	        S5      eSU l        U R                  (       a  U R                  SS U l        gg)z1Decode a complete DER BIT STRING DER from a file.r   zNot a valid BIT STRINGr$   r:   N)r   ri   rL   r   r3   ry   r   s      r   ri   DerBitString._decodeFromStream  sb     	##DV4<<Da1Q6566 
<<ab)DJ r$   r   )r$   NNrs   r   rC   r$   r   r   r   :  s    8*&;*r$   r   c                   P    \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rS rSrg)r   i  a=  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]``.
Nc                     [         R                  U SSUS5        / U l        SU l        U(       a  U H  nU R	                  U5        M     gg)a  Initialize 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).
   r$   TN)r   r"   r   
_elemOctetadd)r    startSetrO   es       r   r"   DerSetOf.__init__  sG     	4sHd;	   r$   c                      U R                   U   $ r&   r   r   s     r   r   DerSetOf.__getitem__  s    yy|r$   c                 ,    [        U R                  5      $ r&   )iterr   r'   s    r   __iter__DerSetOf.__iter__  s    DIIr$   c                 ,    [        U R                  5      $ r&   r   r'   s    r   r   DerSetOf.__len__  s    499~r$   c                 <   [        U5      (       a  SnO0[        U[        5      (       a  U R                  nO[	        US   5      nU R
                  U:w  a  U R
                  b  [        S5      eX l        XR                  ;  a  U R                  R                  U5        gg)zAdd 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.
rw   r   Nz&New element does not belong to the set)	r   r  r   rJ   r   r  r3   r   r   )r    elemeos      r   r  DerSetOf.add  s     dBi((Bd1gB??b * !IJJ Oyy IIT" !r$   c                 .    [         R                  XU5      $ )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   s      r   rl   DerSetOf.decode  s      6::r$   c                 p   / U l         [        R                  XU5        [        U R                  5      nSnUR                  5       S:  a  UR                  5         [        5       nUR                  X25        US:  a  UR                  nOXER                  :w  a  [        S5      eUS:w  a*  U R                   R                  UR                  5       5        OO[        5       nUR                  UR                  5       U5        U R                   R                  UR                  5        UR                  5       S:  a  M  gg)z)Decode a complete DER SET OF from a file.r   z)Not all elements are of the same DER typerw   N)r   r   ri   r   rL   r0   r(   rJ   r3   r   r,   r	   rl   ry   )r    rc   rk   rp   
setIdOctetr   r   s          r   ri   DerSetOf._decodeFromStream  s     	 	##DV4 %
 1$NN+C!!!, A~ ^^
/$%PQQ T!		  !6!6!89#a335v>		  .'  1$r$   c                 h   / nU R                    Hf  n[        U5      (       a  [        U5      R                  5       nO([	        U[
        5      (       a  UR                  5       nOUnUR                  U5        Mh     UR                  5         SR                  U5      U l	        [
        R                  U 5      $ )zBReturn this SET OF DER element, fully encoded as a
binary string.
r$   )
r   r   r	   r`   r  r   r   sortr   rL   )r    orderedr   byss       r   r`   DerSetOf.encode  s     IID$ &--/D),,kkmNN3  	xx(%%r$   )r  r   rL   )NNrs   )r=   r>   r?   r@   rA   r"   r   r  r   r  rl   ri   r`   rB   rC   r$   r   r   r     s0    4*#0;$/B&r$   r   rs   )r   Crypto.Util.py3compatr   r   r   Crypto.Util.numberr   r   __all__r   objectr   r   r	   r
   r   r   r   r   r   r   rC   r$   r   <module>r3     s   .  9 9 ;P+%& %@sZ sZl\' \'~P@ P@fRJ) RJj*?Y *?Z9i 9s:) s:lV*9 V*rU&y U&r$   