U +¥Mb©{ã@sâddlZddlmZmZmZmZddlmZmZdddddd d d gZ dd d„Z Gdd„de ƒZ Gdd„de ƒZ Gdd„de ƒZGdd„de ƒZGdd„de ƒZGdd„de ƒZGdd „d e ƒZGdd „d e ƒZGdd „d e ƒZdS)éN)Ú byte_stringÚbÚbchrÚbord)Ú long_to_bytesÚ bytes_to_longÚ DerObjectÚ DerIntegerÚDerOctetStringÚDerNullÚ DerSequenceÚ DerObjectIdÚ DerBitStringÚDerSetOfFcCs6d}z ||}Wntk r&YdSX| p4|dkS)NrF)Ú TypeError)ÚxÚonly_non_negativeÚtest©rú7/tmp/pip-target-t616c12r/lib/python/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)ÚselfZ initial_bytesrrrÚ__init__.szBytesIO_EOF.__init__cCs |j|_dS©N)rr©rrrrÚ set_bookmark3szBytesIO_EOF.set_bookmarkcCs |jdk st‚|j|j|j…Sr)rÚAssertionErrorrrrrrrÚdata_since_bookmark6szBytesIO_EOF.data_since_bookmarkcCst|jƒ|jSr)ÚlenrrrrrrÚremaining_data:szBytesIO_EOF.remaining_datacCsH|j|}|t|jƒkr.td|t|jƒfƒ‚|j|j|…}||_|S)Nz@Not enough data for DER decoding: expected %d bytes and found %d)rr#rÚ ValueError)rÚlengthZ new_indexÚresultrrrÚread=s  zBytesIO_EOF.readcCst| d¡dƒS)Nér)rr(rrrrÚ read_byteFszBytesIO_EOF.read_byteN) Ú__name__Ú __module__Ú __qualname__Ú__doc__rr r"r$r(r*rrrrr*s rc@sPeZdZdZddd„Zdd„Zed d „ƒZd d „Zd d„Z ddd„Z dd„Z dS)rzpBase class for defining a single DER object. This class should never be directly instantiated. NóFcCs–|dkrd|_dS| |¡}||_d||fkr6tdƒ‚|dk rZdd|B| |¡B|_dS|dk r„d| |¡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)rZasn1Idr5ÚimplicitZ constructedÚexplicitrrrrPs  zDerObject.__init__cCsJt|ƒs t|ƒdkr t|dƒ}t|ƒr>d|krrr6r$)rr@rEZidOctetr&ÚpZ inner_octetrrrrCÞs            zDerObject._decodeFromStream)Nr/NFN)F) r+r,r-r.rr4Ú staticmethodr=r?rArFrCrrrrrJsÿ ?   c@s4eZdZdZd dd„Zdd„Zdd d „Zd d „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 rNcCst |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). ér/FN©rrÚvalue©rrKr7r8rrrrs ÿzDerInteger.__init__cCsz|j}d|_tt|d@ƒƒ|j|_d|kr8dkrLnntdƒ|j|_d|kr`dkrfqpnqp|dL}q t |¡S)zIReturn the DER INTEGER, fully encoded as a binary string.r/éÿr0ri€ÿÿÿé)rKr5rÚintrr?)rÚnumberrrrr?%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. ©rE©rrF©rrDrErrrrF4s zDerInteger.decodecCsÄt |||¡|r\t|jƒdkr(tdƒ‚t|jƒdkr\t d|jdd…¡ddkr\tdƒ‚d|_d}|jD],}|jd 9_|jt|ƒ7_|d K}ql|jrÀt|jdƒd@rÀ|j|8_dS) z*Decode a complete DER INTEGER from a file.rz/Invalid encoding for DER INTEGER: empty payloadrIz>HNr0z.Invalid encoding for DER INTEGER: leading zeror)érN) rrCr#r5r%ÚstructÚunpackrKr)rr@rEÚbitsÚirrrrCAs,  zDerInteger._decodeFromStream)rNN)F©r+r,r-r.rr?rFrCrrrrr ùs   c@s�eZdZdZd#dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„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¡|dkr"g|_n||_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). ér/TN)rrÚ_seq)rZstartSeqr7rrrrƒs zDerSequence.__init__cCs |j|=dSr©r[©rÚnrrrÚ __delitem__˜szDerSequence.__delitem__cCs |j|Srr\r]rrrÚ __getitem__›szDerSequence.__getitem__cCs||j|<dSrr\)rÚkeyrKrrrÚ __setitem__žszDerSequence.__setitem__cCs||j||…<dSrr\)rrXÚjÚsequencerrrÚ __setslice__¡szDerSequence.__setslice__cCs|j||…=dSrr\©rrXrcrrrÚ __delslice__¤szDerSequence.__delslice__cCs|jtd|ƒtd|ƒ…Sr)r[ÚmaxrfrrrÚ __getslice__§szDerSequence.__getslice__cCs t|jƒSr©r#r[rrrrÚ__len__ªszDerSequence.__len__cCs|j |¡|Sr©r[Úappend©rÚitemrrrÚ__iadd__­s zDerSequence.__iadd__cCs|j |¡|Srrlrnrrrrm±s 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Ú ¾s z'DerSequence.hasInts..)r[r#)rrÚitemsrrsrÚ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``.)r[rvr#)rrrrrÚ hasOnlyIntsÁs zDerSequence.hasOnlyIntscCsfd|_|jD]N}t|ƒr(|j|7_q t|ƒrH|jt|ƒ ¡7_q |j| ¡7_q t |¡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. r/)r5r[rrr r?rrnrrrr?Îs zDerSequence.encodeFcCs.||_tj|||d�}|r*| ¡s*tdƒ‚|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. rQzSome members are not INTEGERs)Ú _nr_elementsrrFrwr%)rrDrEZ nr_elementsZonly_ints_expectedr'rrrrFàs  zDerSequence.decodecCsîg|_t |||¡t|jƒ}| ¡dkrŒ| ¡tƒ}| ||¡|jdkr`|j |  ¡¡qt ƒ}|  ¡}|j ||d�|j |j ¡qd}|j dk rÔzt|jƒ|j k}Wn$tk rÒt|jƒ|j k}YnX|sêtdt|jƒƒ‚dS)z+Decode a complete DER SEQUENCE from a file.rrIrQTNz1Unexpected number of members (%d) in the sequence)r[rrCrr5r$r r3rmr"r rFrKrxr#rr%)rr@rErGÚderÚderIntÚdataÚokrrrrCýs.     ÿzDerSequence._decodeFromStream)NN)T)T)FNF)r+r,r-r.rr_r`rbrergrirkrprmrvrwr?rFrCrrrrr Xs *   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 r/NcCst |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)rrKr7rrrrAs zDerOctetString.__init__)r/N©r+r,r-r.rrrrrr #sc@seZdZdZdd„ZdS)r z"Class to model a DER NULL element.cCst |dddd¡dS)z$Initialize the DER object as a NULL.ér/NFr~rrrrrSszDerNull.__init__Nrrrrrr Psc@s4eZdZdZd dd„Zdd„Zdd d „Zd d „ZdS)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 ÚNcCst |dd|d|¡||_dS)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. ér/FNrJrLrrrrws zDerObjectId.__init__cCsÄdd„|j d¡Dƒ}t|ƒdkr*tdƒ‚td|d|dƒ|_|dd …D]h}|dkrddg}n6g}|rŠ| d|d @d B¡|d L}qh|d d M<|jd dd„|Dƒ¡7_qPt  |¡S)zCReturn the DER OBJECT ID, fully encoded as a binary string.cSsg|] }t|ƒ‘qSr)rOrqrrrrtŠsz&DerObjectId.encode..Ú.rIz$Not a valid Object Identifier stringé(rr)Nr;r0ééÿÿÿÿr/cSsg|] }t|ƒ‘qSr)rrqrrrrt—s) rKÚsplitr#r%rr5ÚinsertÚjoinrr?)rÚcompsÚvÚencrrrr?†s   zDerObjectId.encodeFcCst |||¡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. rRrSrrrrFšszDerObjectId.decodecCs€t |||¡t|jƒ}dd„t| ¡dƒDƒ}d}| ¡rp| ¡}|d|d@}|d@s4| t|ƒ¡d}q4d  |¡|_ dS) z,Decode a complete DER OBJECT ID from a file.cSsg|] }t|ƒ‘qSr)Ústrrqrrrrt²sz1DerObjectId._decodeFromStream..r„rr0r;rƒN) rrCrr5Údivmodr*r$rmr�r‰rK)rr@rErGrŠr‹ÚcrrrrCªs zDerObjectId._decodeFromStream)r�NN)FrYrrrrr Ys   c@s4eZdZdZd dd„Zdd„Zdd d „Zd d „ZdS)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 r/NcCs4t |dd|d|¡t|tƒr*| ¡|_n||_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. ér/FN)rrÚ isinstancer?rKrLrrrrÚs   zDerBitString.__init__cCsd|j|_t |¡S)zBReturn the DER BIT STRING, fully encoded as a byte string.ó)rKr5rr?rrrrr?ïs zDerBitString.encodeFcCst |||¡S)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. rRrSrrrrF÷s zDerBitString.decodecCsNt |||¡|jr.t|jdƒdkr.tdƒ‚d|_|jrJ|jdd…|_dS)z1Decode a complete DER BIT STRING DER from a file.rzNot a valid BIT STRINGr/r)N)rrCr5rr%rK)rr@rErrrrCs zDerBitString._decodeFromStream)r/NN)FrYrrrrr½s   c@sTeZdZdZddd„Zdd„Zdd„Zd d „Zd d „Zddd„Z dd„Z dd„Z dS)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|_|r6|D]}| |¡q&dS)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). ér/TN)rrr[Ú _elemOctetÚadd)rZstartSetr7Úerrrr1s zDerSetOf.__init__cCs |j|Srr\r]rrrr`FszDerSetOf.__getitem__cCs t|jƒSr)Úiterr[rrrrÚ__iter__IszDerSetOf.__iter__cCs t|jƒSrrjrrrrrkLszDerSetOf.__len__cCsht|ƒrd}nt|tƒr |j}n t|dƒ}|j|krN|jdk rHtdƒ‚||_||jkrd|j |¡dS)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. rIrNz&New element does not belong to the set) rr‘rr3rr”r%r[rm)rÚelemÚeorrrr•Os      z DerSetOf.addFcCst |||¡S)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. rRrSrrrrFgszDerSetOf.decodecCs®g|_t |||¡t|jƒ}d}| ¡dkrª| ¡tƒ}| ||¡|dkrX|j}n||jkrjtdƒ‚|dkr„|j  |  ¡¡q"t ƒ}|  |  ¡|¡|j  |j ¡q"dS)z)Decode a complete DER SET OF from a file.r†rz)Not all elements are of the same DER typerIN)r[rrCrr5r$r r3r%rmr"r rFrK)rr@rErGZ setIdOctetryrzrrrrCys"    zDerSetOf._decodeFromStreamcCsfg}|jD]<}t|ƒr$t|ƒ ¡}nt|tƒr8| ¡}n|}| |¡q | ¡d |¡|_ t |¡S)zRReturn this SET OF DER element, fully encoded as a binary string. r/) r[rr r?r‘rrmÚsortr‰r5)rZorderedroZbysrrrr?šs     zDerSetOf.encode)NN)F) r+r,r-r.rr`r˜rkr•rFrCr?rrrrrs  !)F)rUZCrypto.Util.py3compatrrrrZCrypto.Util.numberrrÚ__all__rÚobjectrrr r r r r rrrrrrÚs(ÿ  0_L- dY