3 L]Lf@sdZddlmZmZmZmZddlmZmZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZddlmZdd lmZmZmZmZmZdd lmZmZdd lmZmZmZm Z e j!dkrdd l"m#Z$e%Z&dZ'nddl(m$Z$dZ'ej)dddddZ*dddd dddd dZ+dddZ,e j-dZ.iZ/dddZ0Gddde1Z2Gd d!d!Z3Gd"d#d#e1Z4Gd$d%d%e1Z5Gd&d'd'e2Z6e6Z7Gd(d)d)e2Z8Gd*d+d+e2Z9Gd,d-d-e1Z:Gd.d/d/e2Z;Gd0d1d1e5e;ZGd6d7d7e5e4e;e3e1Z?Gd8d9d9e5e4e;Z@Gd:d;d;e5e4e;ZAGdd?d?e5e4e;ZCGd@dAdAe5e4e;ZDGdBdCdCeDZEGdDdEdEe;ZFGdFdGdGe;e3ZGGdHdIdIe;ZHGdJdKdKe;ZIGdLdMdMe;ZJGdNdOdOe>ZKGdPdQdQe<ZLGdRdSdSeGZMGdTdUdUe2ZNGdVdWdWe2ZOGdXdYdYeNZPGdZd[d[eOZQGd\d]d]eNZRGd^d_d_e<ZSGd`dadae<ZTGdbdcdce<ZUGdddedeeBZVGdfdgdge<ZWGdhdidie<ZXGdjdkdkeXZYGdldmdmeXZZGdndodoe<Z[Gdpdqdqe<Z\Gdrdsdse<Z]Gdtdudue<Z^Gdvdwdwe<Z_Gdxdydye<Z`dzd{Zad|d}Zbd~dZcddZde=e>e?eBeFeGeHeIeJeKeReLeMeNePeSeTeUeVeWeYeZe[e\e]e^e_e`dZedddZfdddZgdS)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_literalsdivisionabsolute_importprint_function)datetime timedeltaN)_teletex_codec)unwrap) OrderedDict) type_namestr_clsbyte_cls int_typeschr_cls)_parse _dump_header) int_to_bytesint_from_bytestimezoneextended_datetime)StringIOT)BytesIOF universal applicationcontextprivate)rrrr)rrrrrrrrZ primitiveZ constructed)rrz ^\d+(\.\d+)*$cCstj||dS)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) Asn1Valueload) encoded_datarr#7/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/core.pyr!rs3r!c @seZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ ed$ddZd%ddZd d Zd d Zd dZddZddZddZddZddZd&ddZddZddZd'd d!Zd(d"d#ZdS))r z' The basis of all ASN.1 values NFcKsDt|tstdt|d}|jdk 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 z*encoded_data must be a byte string, not %sN)spec spec_paramsr) isinstancer TypeErrorr tag _parse_build)clsr"rkwargsr&value_r#r#r$r!s  zAsn1Value.loadc Cs4y|jtkrR|j} | jdk r8t| jdtr8| jf| _t| drJ|jdt| <|dk rt|trx|dkrpd}||f}|dkrd}d}|dk rt|tr|dkrd}||f}|dkrd}d}|dk r |dkrd}|dkr||f}n$|dkr||f}nttdt ||dk rt |d kr8t|d tr8|f}x|D]\}}d} t|trf|t kr||} n|t krt|} t |}| dk rttd t | |dk rt|tst td t||jdkr||ff|_n|j||ff|_q>Wn|dk rb|\}}|t kr"ttd t ||dk rJt|tsJt tdt|t ||_||_d|_n@|dk r|t krttdt |t ||_|dk r||_|rd|_| dk r| |_n|dk r|j|WnXtt fk r.} z6| jd d} | jddt|f| | _| WYdd} ~ XnXdS)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_setupTrexplicitimplicitzh tag_type must be one of "implicit", "explicit", not %s rrz 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 z while constructing %s) __class___SETUP_CLASSESr1r(rhasattrr0 ValueErrorr reprlenCLASS_NUM_TO_NAME_MAPCLASS_NAME_TO_NUM_MAPr)r class_r*r2contentssetargs)selfr1r2 no_explicittag_typer;r*optionaldefaultr<r,Z invalid_classer>r#r#r$__init__s2                                 zAsn1Value.__init__cCstr |jS|jSdS)z Since str is different in Python 2 and 3, this calls the appropriate method, __unicode__() or __bytes__() :return: A unicode string N)_PY2 __bytes__ __unicode__)r?r#r#r$__str__s zAsn1Value.__str__cCsDtr"dt|t|t|jfSdt|t|t|jfSdS)z7 :return: A unicode string z <%s %s b%s>z <%s %s %s>N)rFr idr7dump)r?r#r#r$__repr__szAsn1Value.__repr__cCs|jjdS)z A fall-back method for print() in Python 2 :return: A byte string of the output of repr() zutf-8)rLencode)r?r#r#r$rGszAsn1Value.__bytes__cCs|jS)z A fall-back method for print() in Python 3 :return: A unicode string of the output of repr() )rL)r?r#r#r$rHszAsn1Value.__unicode__cCs,|j}|j|_|j|_|j|_|j|_|S)z Constructs a new copy of the current object, preserving any tagging :return: An Asn1Value object )r3r;r*r2r1)r?new_objr#r#r$ _new_instances zAsn1Value._new_instancecCs|j}|j|tj|S)z Implements the copy.copy() interface :return: A new shallow copy of the current Asn1Value object )rO_copycopy)r?rNr#r#r$__copy__szAsn1Value.__copy__cCs&|j}||t|<|j|tj|S)z Implements the copy.deepcopy() interface :param memo: A dict for memoization :return: A new deep copy of the current Asn1Value object )rOrJrPrQdeepcopy)r?memorNr#r#r$ __deepcopy__s  zAsn1Value.__deepcopy__cCs tj|S)z} Copies the object, preserving any special tagging from it :return: An Asn1Value object )rQrS)r?r#r#r$rQszAsn1Value.copycCs>t|ts||i}|j|jd|jdd}|j|tj|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 r1r2)r1r2)r(dictr3getrPrQrS)r?Ztaggingr*rNr#r#r$retags  zAsn1Value.retagcCs|j}|j|tj|S)z{ Copies the object, removing any special tagging from it :return: An Asn1Value object )r3rPrQrS)r?rNr#r#r$untagszAsn1Value.untagcCs<|j|jkr$ttdt|t||j|_||j|_dS)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 zQ Can not copy values from %s object to %s object N)r3r)r r r<_native)r?other copy_funcr#r#r$rP"s  zAsn1Value._copyrcCsd|}t|d}t|||r2|jj|dnVt|drN|jj|dn:trvt|jtrvt d|t |jfnt d||jfdS)zJ Show the binary data and parsed data in a tree structure z parsedrchosenz%s Native: b%sz%s Native: %sN) r5 _basic_debugr]debugr^rFr(nativerprintr7)r? nest_levelprefixZ has_parsedr#r#r$r`:s   zAsn1Value.debugcCs|j}|jdks|rt|tr*|jr*d|_t|j|j|j|j}|j dk rvx*|j D] \}}t|d|||j|}qRW||_d|_ |j|S)a 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 Nrrr%) r<_headerr( Constructable _indefinitemethodrr;r*r1_trailer)r?forcer<headerr;r*r#r#r$rKPs  zAsn1Value.dump)F) NNFNNNNNN)N)r)F)__name__ __module__ __qualname____doc__rhr;r*_bad_tagr2r1rer<rirZ classmethodr!rErIrLrGrHrOrRrUrQrXrYrPr`rKr#r#r#r$r s:  6       r c@s eZdZdZdZdZddZdS)ValueMapzl Basic functionality that allows for mapping values from ints or OIDs to python unicode strings NcCsJ|j}|jdks|jdk rdSi|_x |jjD]\}}||j|<q0WdS)z2 Generates _reverse_map from _map N)r3_map _reverse_mapitems)r?r,keyr.r#r#r$r0|s zValueMap._setup)rlrmrnrorsrtr0r#r#r#r$rrnsrrc@seZdZdZddZdS)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 cCs|j|jjkr0ttdt|t||j|jj|}|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*r3r)r r r;r2r1rer<rir(rfrhrg)r?Z other_classrNr#r#r$casts$   z Castable.castN)rlrmrnrorxr#r#r#r$rwsrwcs8eZdZdZdZdZddZddZfdd ZZ S) rfz A mixin to handle string types that may be constructed from chunks contained within an indefinite length BER-encoded container FrcCsx|js|jS|j}t|j}d}x@||krbt|j||jd\}}|dkrT|j}q$||j7}q$W|dkrt|jS|S)zc :return: A concatenation of the native values of the contained chunks N)r&)rg _as_chunk_chunks_offsetr8r<r+r3 _merge_chunks)r?pointer contents_lenoutputZ sub_valuer#r#r$r{s   zConstructable._merge_chunkscCs |jdkr|jS|j|jdS)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. rN)rzr<)r?r#r#r$rys zConstructable._as_chunkcs&tt|j|||j|_|j|_dS)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)superrfrPrhrg)r?r[r\)r3r#r$rPs zConstructable._copy) rlrmrnrorgrzr{ryrP __classcell__r#r#)r3r$rfs rfc@sJeZdZdZdZddZddZddZd d Ze d d Z dddZ dS)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%cCs |j|jkS)zu :param other: The other Primitive to compare to :return: A boolean )r3)r?r[r#r#r$__eq__s z Void.__eq__cCsdS)NFr#)r?r#r#r$ __nonzero__szVoid.__nonzero__cCsdS)Nrr#)r?r#r#r$__len__sz Void.__len__cCstfS)N)iter)r?r#r#r$__iter__sz Void.__iter__cCsdS)zn The a native Python datatype representation of this value :return: None Nr#)r?r#r#r$ras z Void.nativeFcCsdS)a 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 r%r#)r?rjr#r#r$rK#s z Void.dumpN)F) rlrmrnror<rrrrpropertyrarKr#r#r#r$rs  rcsZeZdZdZdZdddZeddZeddZdd d Z fd d Z dddZ 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. NcKstj|f|y@|dk rLt|ts4ttdt|||jdf|_|j|_ WnVt tfk r}z6|j dd}|j ddt|f||_ |WYdd}~XnXdS)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 rrz while constructing %s) r rEr(r)r r r3_parsedrKr<r6r>)r?r.r-rDr>r#r#r$rE@s  z Any.__init__cCs|jdkr|j|jdjS)z The a native Python datatype representation of this value :return: The .native value from the parsed value object Nr)rparsera)r?r#r#r$ra\s z Any.nativecCs|jdkr|j|jdS)zw Returns the parsed object from .parse() :return: The object returned by .parse() Nr)rr)r?r#r#r$r]js z Any.parsedc Cs|jdks"|jdd||fkry|p*i}t||jdk rdd|krZ|j|d|d<n |j|d<|j|j|j}t|||d\}}|||f|_d|_d|_d|_d|_||_d|_WnXt t fk r}z6|j dd}|j dd t |f||_ |WYdd}~XnX|jdS) av 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 Nrrr1)r&r'Fr%rz while parsing %s) r_tag_type_to_explicit_implicitr1rer<rir+r*r2r6r)r>r ) r?r&r'Z passed_paramsr< parsed_valuer/rDr>r#r#r$rxs2"     z Any.parsecs"tt|j||||j|_dS)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)rrrPr)r?r[r\)r3r#r$rPs z Any._copyFcCs$|jdkr|j|jdj|dS)a 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)rj)rrrK)r?rjr#r#r$rKs zAny.dump)N)NN)F) rlrmrnrorrErrar]rrPrKrr#r#)r3r$r5s    2 rcseZdZdZdZdZdZdZdZdZ e dddZ ddZ ddd Z ed d Zd d ZeddZeddZddZddZfddZdddZZS)ChoicezF A class to handle when a value may be one of several options NFcKs2t|tstdt|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 z*encoded_data must be a byte string, not %s)r&r'r)r(rr)r r+)r,r"rr-r.r/r#r#r$r!s z Choice.loadcCsx|j}i|_i|_x`t|jD]R\}}t|dkrF|if}||j|<t|d|d}||j|<||j|d<qWdS)zS Generates _id_map from _alternatives to allow validating contents rrrrN)r3_id_map _name_map enumerate _alternativesr8_build_id_tuple)r?r,indexinfoid_r#r#r$r0s    z Choice._setupc Kst|tj|f|y|jddk r4ttd|dk r$t|trt|dkrlttdt |t|t |j d\}}t|t rt|dkrttdt |t||d}|d}||j krttd |t ||j ||_|j|j\}}}t||s||f|}n t||}||_WnXttfk r~}z6|jdd}|jdd t |f||_|WYdd}~XnXdS) 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") r2Nz The Choice type can not be implicitly tagged even if in an implicit module - due to its nature any tagging must be explicit rz When passing a dict as the "name" argument to %s, it must have a single key/value - however %d were present rrz 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 z while constructing %s)rr rErWr6r r(rVr8r listrutupler_choicer _fix_taggingrr)r>) r?namer.r-r/r&paramsrDr>r#r#r$rEsJ             zChoice.__init__cCs|js|j|jd|_|jS)zc :return: A unicode string of the field name of the chosen alternative r)_namerr)r?r#r#r$rdsz Choice.namecCs|jdk r|jSy,|j|j\}}}t|j||d\|_}WnVttfk r}z6|jdd}|jddt|f||_|WYdd}~XnXdS)z} Parses the detected alternative :return: An Asn1Value object of the chosen alternative N)r&r'rrz while parsing %s) rrrr+r<r6r)r>r )r?r/r&rrDr>r#r#r$rns z Choice.parsecCs|jS)zT :return: An Asn1Value object of the chosen alternative )r)r?r#r#r$r^sz Choice.chosencCs|jjS)z The a native Python datatype representation of this value :return: The .native value from the contained value object )r^ra)r?r#r#r$ras z Choice.nativecs||f}jdk rXjd |kr2ttdtt|t|\\}}}}}}}||f}|jkrrj|_dSjdk rj dk rtj dkrttdt|jj fkrd_dSj ||}fddjD}ttd|td j |dS) 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 Nrz %s was explicitly tagged, but the value provided does not match the class and tag z| %s was implicitly tagged, but more than one alternative exists rcs g|]}j|d|dqS)rr)_format_class_tag).0pair)r?r#r$ sz#Choice.validate..zs Value %s did not match the class and tag of any of the alternatives in %s: %s z, ) r1r6r r rr8rrr;r*rrjoin)r?r;r*r<rr/Zasn1Zasn1sr#)r?r$validates4      zChoice.validatecCsdt|j|fS)zo :return: A unicode string of a human-friendly representation of the class and tag z[%s %s])r9upper)r?r;r*r#r#r$rszChoice._format_class_tagcs2tt|j|||j|_|j|_||j|_dS)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)rrrPrrr)r?r[r\)r3r#r$rPs z Choice._copycCsl|jj|d|_|jdks|r`d|_|jdk r`x0|jD]&\}}t|d||j|j|j|_q6W|j|jS)a 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 )rjNr%r)r^rKr<rer1r)r?rjr;r*r#r#r$rKs  "z Choice.dump)F)NN)F)rlrmrnrorrrrrrrqr!r0rErrrr^rarrrPrKrr#r#)r3r$rs&   T  @ rc@seZdZdZdZdZed'ddZd(ddZdd Z d d Z d d Z ddZ ddZ ddZddZddZd)ddZd*ddZeddZdd Zd!d"Zd#d$Zd%d&ZdS)+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. NFcCs |||dS)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 )r<rr#)r,r"rr#r#r$r!sz Concat.loadc Cs"|dk rytt|}g|_d}x<|jD]2}||krDt|||d\}}n|}|jj|q$W|rz||krz||}td|WnVttfk r} z6| jdd} | jddt|f| | _| WYdd} ~ XnX|dk r|jdkrdgt|j|_x"t |D]\} } |j | | qWdS)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&z4Extra data - %d bytes of trailing data were providedrz while constructing %s) r8 _children _child_specsr+appendr6r)r>r r __setitem__) r?r.r<rr}offsetr&Z child_value extra_bytesrDr>rdatar#r#r$rE"s,    zConcat.__init__cCstr |jS|jSdS)z Since str is different in Python 2 and 3, this calls the appropriate method, __unicode__() or __bytes__() :return: A unicode string N)rFrGrH)r?r#r#r$rIOs zConcat.__str__cCs|jS)z; A byte string of the DER-encoded contents )rK)r?r#r#r$rG]szConcat.__bytes__cCst|S)z7 :return: A unicode string )r7)r?r#r#r$rHdszConcat.__unicode__cCsdt|t|t|jfS)z7 :return: A unicode string z <%s %s %s>)r rJr7rK)r?r#r#r$rLlszConcat.__repr__cCs|j}|j|tj|S)z| Implements the copy.copy() interface :return: A new shallow copy of the Concat object )r3rPrQ)r?rNr#r#r$rRtszConcat.__copy__cCs&|j}||t|<|j|tj|S)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 )r3rJrPrQrS)r?rTrNr#r#r$rUs  zConcat.__deepcopy__cCs tj|S)zQ Copies the object :return: A Concat object )rQrS)r?r#r#r$rQsz Concat.copycCs4|j|jkr$ttdt|t|||j|_dS)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 zQ Can not copy values from %s object to %s object N)r3r)r r r)r?r[r\r#r#r$rPs  z Concat._copyrcCsRd|}td|t|t|ftd|fx|jD]}|j|dq8WdS)zJ Show the binary data and parsed data in a tree structure z z%s%s Object #%sz %s Children:rN)rbr rJrr`)r?rcrdchildr#r#r$r`s  z Concat.debugcCs(d}x|jD]}||j|d7}q W|S)a 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 r%)rj)rrK)r?rjr<rr#r#r$rKs  z Concat.dumpcCs|jS)z` :return: A byte string of the DER-encoded contents of the children )rK)r?r#r#r$r<szConcat.contentscCs t|jS)z. :return: Integer )r8r)r?r#r#r$rszConcat.__len__cCs8|t|jdks|dkr.ttd|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 rrzN No child is definition for position %d of %s )r8rKeyErrorr r r)r?rvr#r#r$ __getitem__s  zConcat.__getitem__cCsZ|t|jdks|dkr.ttd|t|t|tsLttd|t|||j|<dS)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 rrzK 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) r8rrr r r(r r6r)r?rvr.r#r#r$rs   zConcat.__setitem__cCs t|jS)zB :return: An iterator of child values )rr)r?r#r#r$rszConcat.__iter__)F)NNF)r)F)rlrmrnrorrrqr!rErIrGrHrLrRrUrQrPr`rKrr<rrrrr#r#r#r$rs*  -    $rc@sDeZdZdZdZdZdddZddZdd d Zd d Z d dZ dS) PrimitivezO Sets the class_ and method attributes for primitive, universal values rNcKstj|f|y:|dk r ||_n&|dk r4|j|n|dk rF|j|WnVttfk r}z6|jdd}|jddt|f||_|WYdd}~XnXdS)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 Nrrz while constructing %s)r rEr<r=r6r)r>r )r?r.rCr<r-rDr>r#r#r$rE-s zPrimitive.__init__cCsHt|ts"ttdt|t|||_||_d|_|jdkrDd|_dS)z_ Sets the value of the object :param value: A byte string zH %s value must be a byte string, not %s Nr%) r(rr)r r rZr<reri)r?r.r#r#r$r=Ls   z Primitive.setFcCs$|r|j}d|_|j|tj|S)a 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 N)rar<r=r rK)r?rjrar#r#r$rKcs  zPrimitive.dumpcCs ||k S)Nr#)r?r[r#r#r$__ne__vszPrimitive.__ne__cCst|tsdS|j|jkrdS|jj|jjkr2dS|j|jkrN|j|jkrNdSt|jjt|jgBttttg}t|jjt|jgBttttg}||Br|j|jkS|j s|j s|j s|j r|j j |j j kS|j |j kS)zu :param other: The other Primitive to compare to :return: A boolean FT) r(rr<r3r*r= __bases__r rrr2r1rYrK)r?r[Z self_bases other_basesr#r#r$rys  && zPrimitive.__eq__)NNN)F) rlrmrnror;rhrEr=rKrrr#r#r#r$r$s  rcsDeZdZdZdZdZddZddZfdd Ze 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 latin1NcCsbt|ts"ttdt|t|||_|j|j|_d|_ |j rNd|_ d|_ |j dkr^d|_ dS)zb Sets the value of the string :param value: A unicode string zK %s value must be a unicode string, not %s NFrr%) r(r r)r r _unicoderM _encodingr<rergrhri)r?r.r#r#r$r=s   zAbstractString.setcCs0|jdkrdS|jdkr*|jj|j|_|jS)z7 :return: A unicode string N)r<rr{decoder)r?r#r#r$rHs   zAbstractString.__unicode__cstt|j|||j|_dS)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)rrrPr)r?r[r\)r3r#r$rPs zAbstractString._copycCs|jdkrdS|jS)z The a native Python datatype representation of this value :return: A unicode string or None N)r<rH)r?r#r#r$ras zAbstractString.native) rlrmrnrorrr=rHrPrrarr#r#)r3r$rs rc@s8eZdZdZdZddZddZddZed d Z d S) Booleanz7 Represents a boolean in both ASN.1 and Python rcCs2t||_|sdnd|_d|_|jdkr.d|_dS)z Sets the value of the object :param value: True, False or another value that works with bool() Nr%)boolrZr<reri)r?r.r#r#r$r=s   z Boolean.setcCs|jS)z4 :return: True or False )__bool__)r?r#r#r$rszBoolean.__nonzero__cCs |jdkS)z4 :return: True or False r)r<)r?r#r#r$r szBoolean.__bool__cCs(|jdkrdS|jdkr"|j|_|jS)z} The a native Python datatype representation of this value :return: True, False or None N)r<rZr)r?r#r#r$ras   zBoolean.nativeN) rlrmrnror*r=rrrrar#r#r#r$rs rc@s0eZdZdZdZddZddZeddZd S) Integerz8 Represents an integer in both ASN.1 and Python rcCst|trP|jdkr&ttdt|||jkrDttdt|||j|}n"t|tsrttdt|t||jr||jkr|j|n||_ t |dd|_ d|_ |j dkrd|_ dS)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 rsr6r r rtrr)rZrr<reri)r?r.r#r#r$r=,s*         z Integer.setcCst|jddS)z1 :return: An integer T)r)rr<)r?r#r#r$__int__\szInteger.__int__cCsL|jdkrdS|jdkrF|j|_|jdk rF|j|jkrF|j|j|_|jS)z| The a native Python datatype representation of this value :return: An integer or None N)r<rZrrs)r?r#r#r$racs   zInteger.nativeN) rlrmrnror*r=rrrar#r#r#r$r%s 0rc@sTeZdZdZdZdZdZdZddZddZ d d Z d d Z d dZ e ddZdS) BitStringzK Represents a bit string from ASN.1 as a Python tuple of 1s and 0s rNrcCs2tj||j}|jdk r.t|jjd|_dS)z2 Generates _reverse_map from _map Nr)rrr0r3rsmaxkeys_size)r?r,r#r#r$r0s  zBitString._setupc Cs"t|tr|jdkr&ttdt|dg|j}||_x:td|jD]*}|jj |}|dkr`qF||krFd||<qFWdj t t |}n|j tkr|jdkr||_n Represents a bit string in ASN.1 as a Python integer rrcCsft|ts"ttdt|t|||_dt|dd|_d|_|j rRd|_ d|_ |j dkrbd|_ dS) z Sets the value of the object :param value: An integer :raises: ValueError - when an invalid value is passed zE %s value must be an integer, not %s rT)rNFrr%) r(rr)r r rZrr<rergrhri)r?r.r#r#r$r=s   zIntegerBitString.setcCsjt|jdd}djt|jdd}t|d}|dkrNdd||}|dkrf|dd|S|S)z Allows reconstructing indefinite length values :return: A unicode string of bits - 1s and 0s rrz{0:b}Nrr)rr<rr8)r?rrrr#r#r$ry s zIntegerBitString._as_chunkcCs||jdkrdS|jdkrvt|jdd}|j rP|dkrPt|jdd|_n&|jrf|dkrftdt|jd|_|jS)z| The a native Python datatype representation of this value :return: An integer or None Nrrz=Constructed bit string has extra bits on indefinite containerr)r<rZrrgr6rr{)r?rr#r#r$ra* s  zIntegerBitString.nativeN) rlrmrnror*rzr=ryrrar#r#r#r$rs rcsDeZdZdZdZdZddZddZfdd Ze d d Z Z S) OctetStringz; Represents a byte string in both ASN.1 and Python NcCsZt|ts"ttdt|t|||_||_d|_|jrFd|_d|_ |j dkrVd|_ dS)z_ Sets the value of the object :param value: A byte string zH %s value must be a byte string, not %s NFrr%) r(rr)r r rr<rergrhri)r?r.r#r#r$r=L s   zOctetString.setcCs(|jdkrdS|jdkr"|j|_|jS)z4 :return: A byte string Nr%)r<rr{)r?r#r#r$rGf s    zOctetString.__bytes__cstt|j|||j|_dS)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)rrrPr)r?r[r\)r3r#r$rPr s zOctetString._copycCs|jdkrdS|jS)z The a native Python datatype representation of this value :return: A byte string or None N)r<rG)r?r#r#r$ra s zOctetString.native) rlrmrnror*rr=rGrPrrarr#r#)r3r$rB s rc@s(eZdZdZdZddZeddZdS)IntegerOctetStringz? Represents a byte string in ASN.1 as a Python integer rcCsbt|ts"ttdt|t|||_t|dd|_d|_|j rNd|_ d|_ |j dkr^d|_ dS)z Sets the value of the object :param value: An integer :raises: ValueError - when an invalid value is passed zE %s value must be an integer, not %s F)rNrr%) r(rr)r r rZrr<rergrhri)r?r.r#r#r$r= s   zIntegerOctetString.setcCs,|jdkrdS|jdkr&t|j|_|jS)z| The a native Python datatype representation of this value :return: An integer or None N)r<rZrr{)r?r#r#r$ra s  zIntegerOctetString.nativeN)rlrmrnror*r=rrar#r#r#r$r srcsneZdZdZdZdZdddZddZdddZd d Z fd d Z e d dZ e ddZ dddZZS)ParsableOctetStringrNcKsVd}|dkr*|dk r*t|tr*|j}d}tj|fd|i||rR||jdf|_dS)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. FNTr.)r(r rKrrEr3r)r?r.r]r-Z set_parsedr#r#r$rE szParsableOctetString.__init__cCsZt|ts"ttdt|t|||_||_d|_|jrFd|_d|_ |j dkrVd|_ dS)z_ Sets the value of the object :param value: A byte string zH %s value must be a byte string, not %s NFrr%) r(rr)r r rr<rergrhri)r?r.r#r#r$r= s   zParsableOctetString.setcCsL|jdks |jdd||fkrBt|j||d\}}|||f|_|jdS)av 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 Nrr)r&r'r)rr+rG)r?r&r'rr/r#r#r$r s  zParsableOctetString.parsecCs(|jdkrdS|jdkr"|j|_|jS)z4 :return: A byte string Nr%)r<rr{)r?r#r#r$rG s    zParsableOctetString.__bytes__cs*tt|j|||j|_||j|_dS)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)rrrPrr)r?r[r\)r3r#r$rP# s zParsableOctetString._copycCs0|jdkrdS|jdk r$|jdjS|jSdS)z The a native Python datatype representation of this value :return: A byte string or None Nr)r<rrarG)r?r#r#r$ra3 s   zParsableOctetString.nativecCs|jdkr|j|jdS)zw Returns the parsed object from .parse() :return: The object returned by .parse() Nr)rr)r?r#r#r$r]D s zParsableOctetString.parsedFcCs>|r4|jdk r|jj|d}n|j}d|_|j|tj|S)a 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 N)rj)rr]rKrar<r=r )r?rjrar#r#r$rKR s   zParsableOctetString.dump)NN)NN)F)rlrmrnr*rrrEr=rrGrPrrar]rKrr#r#)r3r$r s     rc@seZdZdZdZddZdS)ParsableOctetBitStringrrcCs^t|ts"ttdt|t|||_d||_d|_|jrJd|_d|_ |j dkrZd|_ dS)z Sets the value of the object :param value: A byte string :raises: ValueError - when an invalid value is passed zH %s value must be a byte string, not %s rNFrr%) r(rr)r r rr<rergrhri)r?r.r#r#r$r=s s    zParsableOctetBitString.setN)rlrmrnr*rzr=r#r#r#r$ri src@s,eZdZdZdZdZddZeddZdS) Nullz< Represents a null value in ASN.1 as None in Python r%cCs d|_dS)zV Sets the value of the object :param value: None r%N)r<)r?r.r#r#r$r= szNull.setcCsdS)zn The a native Python datatype representation of this value :return: None Nr#)r?r#r#r$ra s z Null.nativeN) rlrmrnror*r<r=rrar#r#r#r$r s  rc@sXeZdZdZdZdZeddZeddZdd Z d d Z e d d Z e ddZ dS)ObjectIdentifierz` Represents an object identifier in ASN.1 as a Python unicode dotted integer string NcCsF|jdkrttdt|t|ts8ttdt||jj||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 Nz> %s._map has not been defined zH value must be a unicode string, not %s )rsr6r r r(r r)rW)r,r.r#r#r$r s    zObjectIdentifier.mapcCs|tkr|jdt|<|jdkr6ttdt|t|tsRttdt|||j krf|j |St j |sttdt|||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 TNz> %s._map has not been defined zH value must be a unicode string, not %s zL %s._map does not contain an entry for "%s" ) r4r0rsr6r r r(r r)rt_OID_REmatch)r,r.r#r#r$unmap s&        zObjectIdentifier.unmapcCst|ts"ttdt|t|||_|jdk rF||jkrF|j|}d|_d}xt |j dD]\}}t |}|dkr|}q`n|dkr|d|}t d|@}|d ?}x(|dkrt d d|@B|}|d ?}qW|j|7_q`Wd|_ |jdkrd|_dS) 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 zK %s value must be a unicode string, not %s Nr%.rr()r(r r)r r rZrsrtr<rsplitrrreri)r?r.firstrpartZ encoded_partr#r#r$r= s6          zObjectIdentifier.setcCs|jS)z7 :return: A unicode string )dotted)r?r#r#r$rHB szObjectIdentifier.__unicode__cCs|jdkrg}d}x~|jD]t}tr*t|}|d}||d@7}|d@dkrt|dkr||jt|d|jt|dn|jt|d}qWdj||_|jS)z :return: A unicode string of the object identifier in dotted notation, thus ignoring any mapped value Nrrrrr)_dottedr<rFordr8rr r)r?r~rbyter#r#r$rJ s       zObjectIdentifier.dottedcCsJ|jdkrdS|jdkrD|j|_|jdk rD|j|jkrD|j|j|_|jS)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)r<rZrrs)r?r#r#r$rag s  zObjectIdentifier.native)rlrmrnror*rrqrrr=rHrrrar#r#r#r$r s " 22 rc@seZdZdZdZdS)ObjectDescriptorzO Represents an object descriptor from ASN.1 - no Python implementation rN)rlrmrnror*r#r#r#r$r| src@seZdZdZdZdS) InstanceOfzF Represents an instance from ASN.1 - no Python implementation rN)rlrmrnror*r#r#r#r$r src@seZdZdZdZdS)RealzH Represents a real number from ASN.1 - no Python implementation N)rlrmrnror*r#r#r#r$r src@s(eZdZdZdZddZeddZdS) Enumeratedz\ Represents a enumerated list of integers from ASN.1 as a Python unicode string cCst|t r0t|t r0ttdt|t|t|trd||jkrXttdt|||j|}n||jkrttdt||t j ||dS)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(rr r)r r rtr6rsrr=)r?r.r#r#r$r= s$      zEnumerated.setcCs.|jdkrdS|jdkr(|j|j|_|jS)z The a native Python datatype representation of this value :return: A unicode string or None N)r<rZrsr)r?r#r#r$ra s  zEnumerated.nativeN)rlrmrnror*r=rrar#r#r#r$r s+rc@seZdZdZdZdZdS) UTF8StringzI Represents a UTF-8 string from ASN.1 as a Python unicode string zutf-8N)rlrmrnror*rr#r#r#r$r src@seZdZdZdZdS) RelativeOidz` Represents an object identifier in ASN.1 as a Python unicode dotted integer string N)rlrmrnror*r#r#r#r$r srcseZdZdZdZdZdZdZdZdZ gZ dZ dZ dZ dZdZdZdZd.ddZed d Zejd d Zd d ZddZddZddZddZddZddZd/ddZddZddZd d!Z d0d"d#Z!d$d%Z"ed&d'Z#fd(d)Z$d1d*d+Z%d2d,d-Z&Z'S)3Sequencezf Represents a sequence of fields from ASN.1 as a Python object with a dict-like interface rrNFc Kstj|f|d}|dkrL|dk rLd}|jdkrH|jdkr@d}n|j|}|dk ry|jr|dd|jD}t|j}n|j}t|}xl|D]d}|r|j|}|t |jkr|j|t k r||kr|j |q||kr|j ||||j |qWt |r&t tdt|djtt|WnXt tfk r} z6| jdd} | jd d t|f| | _| WYdd} ~ XnXdS) 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 FNTcSsg|] }|dqS)rr#)rrr#r#r$rA sz%Sequence.__init__..z One or more unknown fields was passed to the constructor of %s: %s z, rrz while constructing %s)r rEchildrenr<_parse_children_fieldsr=r _field_mapr8VOIDrrr6r r rsortedrr)r>) r?r.rCr-Zcheck_existingrZ unused_keysrvrrDr>r#r#r$rE# sF        zSequence.__init__cCs&|jdkr|jS|jr |j|jS)z` :return: A byte string of the DER-encoded contents of the sequence N)r _contents _is_mutated _set_contents)r?r#r#r$r<d s  zSequence.contentscCs ||_dS)ze :param value: A byte string of the DER-encoded contents of the sequence N)r)r?r.r#r#r$r<s scCsD|j}|jdk r@x.|jD]$}t|ts0t|tr|p:|j}qW|S)z~ :return: A boolean - if the sequence or any children (recursively) have been mutated N)_mutatedrr(r SequenceOfr)r?mutatedrr#r#r$r| s   zSequence._is_mutatedcCs*|j|}|jtkr&t|}|j|<|S)z] Builds a child object if the child has only been parsed into a tuple so far )rr3r_build)r?rrr#r#r$ _lazy_child s  zSequence._lazy_childcCs|jdkr|jt|jS)z. :return: Integer N)rrr8)r?r#r#r$r s zSequence.__len__cCs|jdkr|jt|tsD||jkr:ttd|t||j|}|t|jkrfttd|t|y |j |St t fk r}z6|j dd}|j ddt|f||_ |WYdd}~XnXdS)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 NzL No field named "%s" defined for %s zL No field numbered %s is present in this %s rrz while parsing %s) rrr(rrrr r r8rr6r)r>)r?rvrDr>r#r#r$r s(       zSequence.__getitem__c Cs|jdkr|jt|tsD||jkr:ttd|t||j|}|j|\}}}}}|j |||||}d} t|t r|j dk} n"t|t r|j jdk} n |jdk} | rttd|t|||j|<|jdk r|j|j|j|j|d<d|_dS)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 NzL No field named "%s" defined for %s FzG Value for field "%s" of %s is not set rT)rrr(rrrr r _determine_spec _make_valuerr]rr^r<r6rZrarr ) r?rvr. field_name field_spec value_spec field_paramsr/ new_valueZ invalid_valuer#r#r$r s4            zSequence.__setitem__cCs|jdkr|jt|tsD||jkr:ttd|t||j|}|j|\}}}| sjd|kr~d|kr~t td|t|d|krt |j|<|j dk rd|j |<n |j |dd|_ dS)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 NzL No field named "%s" defined for %s rCrBz Can not delete the value for the field "%s" of %s since it is not optional or defaulted T)rrr(rrrr r rr6rrZrr )r?rvrr/rr#r#r$ __delitem__ s*          zSequence.__delitem__ccsx|jD]}|dVqWdS)zE :return: An iterator of field key names rN)r)r?rr#r#r$r4 s zSequence.__iter__cCs|jdkr|jt}xt|jD]\}}|j|}|dkrDd}nH|jtkr|rf|j|j|d}q|d|d|d}n |j|d}|drd|dkr|d f|d}|j|krq$|j |q$W|j |_ d|_ |j dkrd|_ dS) 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%)rjrrrrrCr)rrrrrr3rrrKwritegetvaluerreri)r?rjr<rrrZ child_dump default_valuer#r#r$r = s*       zSequence._set_contentscCs,|j}i|_g|_g|_x^t|jD]P\}}t|dkrL|if}||j|<||j|d<|jjt|d|dq$W|j dk r|j|j d|j|j df|_ xt|jD]t\}}|j dk o|d|j k}|j dk o|j d|k}|s|r|jjdq|jj|d|d|d|ddfqWdS)zS Generates _field_map, _field_ids and _oid_nums for use in parsing rrrrN) r3r _field_ids_precomputed_specsrrr8rr _oid_pair _oid_nums_spec_callbacks)r?r,rfield has_callback is_mapped_oidr#r#r$r0b s$    "zSequence._setupc Cs|j|\}}}|}d}|jdk r||jkr|j|}||}|r|jtkrrt|dkrr|\}}|dkr|}d}q|dkr|}|}d}q|}nB|jdk r|jd|kr|j|jdj}||jkr|j|}|}|||||fS)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 Nrrr) rrr3rr8rrra _oid_specs) r?rrrrr spec_overridecallbackoidr#r#r$r} s.   zSequence._determine_specc Cs |dkrd|krtS||k}t|t}t|trt|tsNttd|t|t||s~|}|j |j |j |j ||_ |} n|} nt||r|} |r| j|np| s|rt|| r||f|} nLt||r|} n||} |o| r|fd| ji|}| | jdf|_ |} 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 NrBz Can not set a native python value to %s, which has the choice type of %s - value must be an instance of Asn1Value r.)r issubclassrrr(r r6r r rr;r*r<rrrKr3r) r?rrrrr.Zspecs_differentZis_anywrapperrr#r#r$r s>          zSequence._make_valuecCs<|j}|jdkr|jrtgt|j|_xpt|jD]b\}\}}}d|kr4|j|rj|j|\}}}} }n|j|\}}}} }|j |||| d|j|<q4WdSy>g|_t|j} d} d} t|j} d}| | k}x|rb|dkrt |j| | d\}} | | k}| | krB|j| p|j| \}}}} }| rd| ksBd| kr|j | |d|dfkr|t krd}t |try,|f| }|j|d|d|dd }Wntk rYnX|sd| kr|jjtn|jj|f| | d 7} d }q|dks|rt |t r|}d}|r4||| |f}n ||| f}n| dkr| d | krg}| d }x\|dkr|j|}t|d krPd|dksd|dkr|j|d|d 8}qhWt|d krd nd }dj|}ttd| d tj|dtj|d |d||n|}|rHt|}t|ttfrH|jd d|jj|| d 7} d}qWt|j}xl|| kr|j|\}}} d| kr|jj|f| n&d| kr|jjtnttd||d 7}qpWWnXttfk r6}z6|jd d}|jddt|f||_|WYdd}~XnXdS)aQ 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 NrCr)r|rBrFrTrrsrz, 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 z while parsing %s)r3rrrr8rrrrrrrrr&rrr6rrr r9rWMETHOD_NUM_TO_NAME_MAPr r(rr rr)r>r )r?r)r,rr/rrrrrcontents_length child_pointerrZ field_lenpartsZagainr#Z choice_matchtesterrZ missed_fieldsZ prev_fieldZprev_field_infopluralZmissed_field_namesrrDr>r#r#r$r s        &               zSequence._parse_childrencCsZt|tsttdt||jdkr>ttdt|t||j|}|j |}|dS)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 rr6r7rr)r?rrrr#r#r$r&|s      z Sequence.speccCs |jdkrdS|jdkr|jdkr0|jddy|t|_xnt|jD]`\}}|jtkrjt|}||j|<y|j |d}Wnt k rt |}YnX|j |j|<qFWWnXt tfk r}z6|jdd}|jddt|f||_|WYdd}~XnX|jS)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. NT)r)rrz while parsing %s)r<rZrrr rr3rr r IndexErrorr rar6r)r>r )r?rrrrDr>r#r#r$ras*      zSequence.nativecs^tt|j|||jdk rZg|_x6|jD],}|jtkrF|jj|q*|jj|jq*WdS)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)rrrPrr3rrrQ)r?r[r\r)r3r#r$rPs    zSequence._copycCsl|jdkr|jd|}t||xB|D]:}|j|j|}|tk r*td||f|j|dq*WdS)zJ Show the binary data and parsed data in a tree structure Nz z%s Field "%s"r)rrr_rrrrbr`)r?rcrdrrr#r#r$r`s   zSequence.debugcCsx|r|j|d|jrn|jdk rnxLt|jD]>\}\}}}|j|tk rJq,d|ks,d|kr\q,ttd|q,Wtj|S)a 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 )rjNrCrBzN Field "%s" is missing from structure ) r rrrrr6r r rK)r?rjrrr/rr#r#r$rKs   z Sequence.dump)NN)F)F)r)F)(rlrmrnror*r;rhrrr rrrrrr"rrrErr<setterrrrrrrrr r0rrrr&rarPr`rKrr#r#)r3r$r sH  A    .9- %/I ' !  rcseZdZdZdZdZdZdZdZdZ dZ d.ddZ e d d Z e jd d Z d d ZddZddZddZddZddZddZddZddZddZd/d d!Zd0d"d#Zd$d%Ze d&d'Zfd(d)Zd1d*d+Zd2d,d-ZZ S)3r z Represents a sequence (ordered) of a single type of values from ASN.1 as a Python object with a list-like interface rrrNFc Ks|r ||_tj|f|yd|dk r*||_nP|dkr>|dk r>|}|dk rzx t|D]\}}|j||qPW|jdkrz|jWnVttfk r}z6|j dd} |j ddt |f| |_ |WYdd}~XnXdS)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 Nrrz while constructing %s) _child_specr rEr<rrr r6r)r>r ) r?r.rCr<r&r-rrrDr>r#r#r$rE s"  zSequenceOf.__init__cCs&|jdkr|jS|jr |j|jS)z` :return: A byte string of the DER-encoded contents of the sequence N)rrrr )r?r#r#r$r<Ks  zSequenceOf.contentscCs ||_dS)ze :param value: A byte string of the DER-encoded contents of the sequence N)r)r?r.r#r#r$r<ZscCsD|j}|jdk r@x.|jD]$}t|ts0t|tr|p:|j}qW|S)z~ :return: A boolean - if the sequence or any children (recursively) have been mutated N)r rr(rr r)r?r rr#r#r$rcs   zSequenceOf._is_mutatedcCs*|j|}|jtkr&t|}||j|<|S)z] Builds a child object if the child has only been parsed into a tuple so far )rr3rr )r?rrr#r#r$rrs    zSequenceOf._lazy_childcCst||jr|}nt|jtrBt|tr.|}qttdt|nrt|jtrt|tspttdt||jj t||js|j}|j |j |j |j ||_|}|}n |j|dSi}|jjr|jj|d<|jjr|jj |jj 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 )r.r1r2)r(r2r&rr r6r r rrlrr;r*r<rr1r2r)r?r.rr'rr#r#r$r}s6         zSequenceOf._make_valuecCs|jdkr|jt|jS)z1 :return: An integer N)rrr8)r?r#r#r$rs zSequenceOf.__len__cCs|jdkr|j|j|S)zm Allows accessing children via index :param key: Integer index of child N)rrr)r?rvr#r#r$rs zSequenceOf.__getitem__cCs||jdkr|j|j|}|t|jkrL|jjd|jdk rL|jjd||j|<|jdk rr|j|j|j|<d|_dS)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)rrrr8rrZrar )r?rvr.rr#r#r$rs       zSequenceOf.__setitem__cCs>|jdkr|j|jj||jdk r4|jj|d|_dS)zk Allows removing a child via index :param key: Integer index of child NT)rrpoprZr )r?rvr#r#r$rs    zSequenceOf.__delitem__ccs<|jdkr|jx$tdt|jD]}|j|Vq$WdS)zA :return: An iter() of child objects Nr)rrrr8r)r?rr#r#r$rs zSequenceOf.__iter__cCs^|dks|tkrdSt||js@ttdt|t|jt|x|D]}||krFdSqFWdS)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)rr(r2r)r r )r?itemrr#r#r$ __contains__ s    zSequenceOf.__contains__cCsL|jdkr|j|jj|j||jdk rB|jj|jdjd|_dS)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 NrTr)rrrrrZrar )r?r.r#r#r$r(s  zSequenceOf.appendcCs\|jdkr|jt}x|D]}|j|j|dqW|j|_d|_|jdkrXd|_dS)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 N)rjr%) rrrrrKrrreri)r?rjr<rr#r#r$r <s    zSequenceOf._set_contentscCsyg|_|jdkrdSt|j}d}xj||krt|j||d\}}|jrV||jf}n|}|rt|}t|ttfr|j dd|jj |q&WWnVt t fk r}z6|j dd}|j ddt|f||_ |WYdd}~XnXdS)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|T)r)rz while parsing %s)rrr8rr2r r(rr rrr6r)r>r )r?r)r+r,r-rrDr>r#r#r$rPs(     zSequenceOf._parse_childrencCs|jS)z Determines the spec to use for child values. :return: A child class of asn1crypto.core.Asn1Value that child values must be encoded using )r2)r?r#r#r$r&ss zSequenceOf.speccCs|jdkrdS|jdkr|jdkr.|jddydd|D|_WnVttfk r}z6|jdd}|jddt|f||_|WYdd}~XnX|jS) 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. NT)r)cSsg|] }|jqSr#)ra)rrr#r#r$rsz%SequenceOf.native..rrz while parsing %s)r<rZrrr6r)r>r )r?rDr>r#r#r$ra~s    zSequenceOf.nativecs^tt|j|||jdk rZg|_x6|jD],}|jtkrF|jj|q*|jj|jq*WdS)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)rr rPrr3rrrQ)r?r[r\r)r3r#r$rPs    zSequenceOf._copycCsD|jdkr|jd|}t||x|D]}|j|dq*WdS)zJ Show the binary data and parsed data in a tree structure Nz r)rrr_r`)r?rcrdrr#r#r$r`s    zSequenceOf.debugcCs|r|j|dtj|S)a 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 )rj)r r rK)r?rjr#r#r$rKs  zSequenceOf.dump)NNNN)F)F)r)F)!rlrmrnror*r;rhrrr r2rErr<r1rrrrrrrrr5rr rr&rarPr`rKrr#r#)r3r$r s8 +   7    #   r c@s<eZdZdZdZdZdZdZddZd d d Z dd d Z dS)Setzm Represents a set of fields (unordered) from ASN.1 as a Python object with a dict-like interface rrNcCs*|j}i|_i|_g|_x\t|jD]N\}}t|dkrL|if}||j|<||j|d<||jt|d|d<q$W|jdk r|j|jd|j|jdf|_ xt|jD]t\}}|j dk o|d|j k}|j dk o|j d|k}|s|r|jj dq|jj |d|d|d|ddfqWdS)zS Generates _field_map, _field_ids and _oid_nums for use in parsing rrrrN) r3rrrrrr8rrrrr)r?r,rrr r!r#r#r$r0s$    "z Set._setupFcCs>|j}|jdkr|jrtgt|j|_xpt|jD]b\}\}}}d|kr4|j|rj|j|\}}}} }n|j|\}}}} }|j |||| d|j|<q4WdSy@i} t|j } d} d} x| | krt |j | | d\}} |d|df}|j j |}|dkr,ttd| tj |dtj |d|d|j|p@|j|\}}}} }|dksh|rpt|trp|}d}|r||| |f}n ||| f}|rt|}t|ttfr|jdd || |<| d7} qWt|j}xtd|D]}|| krq|j|p |j|\}}}} }|dks4|r)r?r)r,rr/rrrrrZ child_mapr+r,Z seen_fieldr-rrr#rZ total_fieldsrmissingrDr>r#r#r$rs                   zSet._parse_childrenc Cs|jdkr|jg}x^t|jD]P\}}|j|d}|j|\}}}d|krb|f|j|krbq"|j|j|fq"W|jddddjdd |D|_ d|_ |j dkrd|_ dS) 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 N)rjrCcSs|dS)Nrr#)ctr#r#r$|sz#Set._set_contents..)rvr%cSsg|] }|dqS)rr#)rr9r#r#r$r~sz%Set._set_contents..) rrrrKrrr*sortrrreri) r?rjZchild_tag_encodingsrrZchild_encodingrr&rr#r#r$r bs   zSet._set_contents)F)F) rlrmrnrorhr;r*rr0rr r#r#r#r$r6s or6c@seZdZdZdZdddZdS)SetOfz~ Represents a set (unordered) of a single type of values from ASN.1 as a Python object with a list-like interface r7FcCs`|jdkr|jg}x|D]}|j|j|dqWdjt||_d|_|jdkr\d|_dS)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 N)rjr%) rrrrKrrrreri)r?rjZchild_encodingsrr#r#r$r s   zSetOf._set_contentsN)F)rlrmrnror*r r#r#r#r$r<sr<c@seZdZdZdZdS) EmbeddedPdvz A sequence structure N)rlrmrnror*r#r#r#r$r=sr=c@seZdZdZdZdZdS) NumericStringzK Represents a numeric string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$r?sr?c@seZdZdZdZdZdS)PrintableStringzM Represents a printable string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$rAsrAc@seZdZdZdZdZdS) TeletexStringzK Represents a teletex string from ASN.1 as a Python unicode string ZteletexN)rlrmrnror*rr#r#r#r$rCsrCc@seZdZdZdZdS)VideotexStringzI Represents a videotex string from ASN.1 as a Python byte string N)rlrmrnror*r#r#r#r$rEsrEc@seZdZdZdZdZdS) IA5StringzH Represents an IA5 string from ASN.1 as a Python unicode string asciiN)rlrmrnror*rr#r#r#r$rGsrGc@seZdZdZeddZdS) AbstractTimezK Represents a time from ASN.1 as a Python datetime.datetime object cCs|jdkrdS|jdkrt|}tjd|}|sV|jd}|j|}|jtj d|_nl|j|dd }t |d d }t |d d}t t ||d }|dkr||8}n||7}|jtj d|_|jS)z The a native Python datatype representation of this value :return: A datetime.datetime object in the UTC timezone or None Nz[-\+]Z)tzinforrrr)hoursminutesrQ) r<rZr researchr _date_by_lenreplacerutcrrabs)r?stringZ has_timezonedaterMrNdeltar#r#r$ras$      zAbstractTime.nativeN)rlrmrnrorrar#r#r#r$rJsrJc@s$eZdZdZdZddZddZdS)UTCTimezV Represents a UTC time from ASN.1 as a Python datetime.datetime object in UTC cCs8t|tr"|jd}tr"|jd}tj||d|_dS)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%SZrIN)r(rstrftimerFrrr=rZ)r?r.r#r#r$r=s    z UTCTime.setcCs^t|}t|dd}|dkr&d}nd}|dkrBtj||dS|dkrZtj||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 rr2Z20Z19rz %Y%m%d%H%Mrz %Y%m%d%H%M%S)r8rrstrptime)r?rXstrlenZyear_numrdr#r#r$rT#s zUTCTime._date_by_lenN)rlrmrnror*r=rTr#r#r#r$r[sr[c@s$eZdZdZdZddZddZdS)GeneralizedTimez Represents a generalized time from ASN.1 as a Python datetime.datetime object or asn1crypto.util.extended_datetime object in UTC cCs<t|ttfr&|jd}tr&|jd}tj||d|_dS)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%SZrIN) r(rrr]rFrrr=rZ)r?r.r#r#r$r=Gs    zGeneralizedTime.setc Cst|}d}|dkrd}n(|dkr(d}n|dkr6d}n |dkrBd }|rt|d kr|d d d krtjd |d d|}td |j|j|j|j|j|j |j Stj||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 Nrz%Y%m%d%Hrz %Y%m%d%H%Mz %Y%m%d%H%M%Sr@z%Y%m%d%H%M%S.%frrZ00002000) r8rr_rmonthdayhourminutesecond microsecondrL)r?rXr`Z date_formattr#r#r$rT]s0  zGeneralizedTime._date_by_lenN)rlrmrnror*r=rTr#r#r#r$ra?srac@seZdZdZdZdZdS) GraphicStringzK Represents a graphic string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$rlsrlc@seZdZdZdZdZdS) VisibleStringzK Represents a visible string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$rnsrnc@seZdZdZdZdZdS) GeneralStringzK Represents a general string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$rpsrpc@seZdZdZdZdZdS)UniversalStringzM Represents a universal string from ASN.1 as a Python unicode string z utf-32-beN)rlrmrnror*rr#r#r#r$rrsrrc@seZdZdZdZdZdS)CharacterStringzM Represents a character string from ASN.1 as a Python unicode string rN)rlrmrnror*rr#r#r#r$rtsrtc@seZdZdZdZdZdS) BMPStringzG Represents a BMP string from ASN.1 as a Python unicode string z utf-16-beN)rlrmrnror*rr#r#r#r$rvsrvcCs4td|t|t|f|jrBtd|tj|jp2djdf|jdk o^|jdk o^|j dk }|r|t j |j}t j |j}|j dk rx*|j D] \}}td|t j ||fqW|rtd||||j fn>|jr|rtd||||j fn|r td ||||j ftd |tj|jp djdfdS) 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 z%s%s Object #%sz%s Header: 0x%sr%zutf-8Nz#%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)rbr rJrebinasciihexlifyrrhr;r*r*rWr9r1r2r<)rdr? has_header method_name class_namer;r*r#r#r$r_s, "   r_cCspd|krl|ddkr.|jdd|df|d<n$|ddkrR|jdd|df|d<|d=|d=d|krl|d=dS)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 rAr1classrr*r2N)rW)rr#r#r$rs  rcCst|d}d|kr$|jdk rxd}nTt|dtr@|d\}}n |d}d}|jdkr\d}n|jt|kst|j|krxd}|jd|jkrd}|r|j |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 Fr2Trr1) rr2r(rr;r:r*rWr1rX)r.rrXr;r*r#r#r$rs$   rcCs|dkr dS|j}|j}t|d|krRt|dtrD|d\}}qd}|d}n0d|krt|dtrv|d\}}n d}|d}|dk rt|t rt|}|jd|}|jd|}||fS)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) Nr1rr2r;r*)NN)r;r*rr(rrr:rW)rr&Zrequired_classZ required_tagr#r#r$r(s(   r)rrrrrrrrrrr>rrrr7r@rBrDrFrHr\rbrmrorqrsrurwc Cs|dk rt||dkrtSd} |dk r|o4d|k} | oL|jsP|oLd|krz|r`|f|} n|} | j} t| } |}|}|}|}|}|pd}x| D]\}}||krttdt| tj|tj|||dkrttdt| t jdt j||||krttd t| ||t |t |\}}|\}}}}}}||7}||}qWt ||dd id } || _ || _| | _d } qT|r|fd |i|} n ||d } |tkrqTt| tr>| j|||y|| j| _d}| jWnXttfk r:}z6|jdd}|jddt| f||_|WYdd}~XnXn|| jkrpttdt| tj| jtj|||| jkr|dko| jdko|dk}| st| t rttdt| t j| jt j||n || _d | _|| jkrT|| jkrTttdt| | j|n<|rd|krtfd |i|}|j} |}|}|pPd}xHt| D]<\}}t |t |\}}|\}}}}}}||7}||}q\Wt ||dd id } || j | _ | j|pd7_| | _d } nx|tkrttdtj|t j||t|}|||d} |dko4| jdko4|dk}|rNt| trNd | _|| _| sl|| _ |phd| _d| _|ry| j|WnXttfk r}z6|jdd}|jddt| f||_|WYdd}~XnX| 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 NFr@r1r%z Error parsing %s - explicitly-tagged class should have been %s, but %s was found rz 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 T)r&r'r<)r<rz while parsing %sz Error parsing %s - class should have been %s, but %s was found szy 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 )r<r;) rrr1reversedr6r r r9rWr*rr8r rerirr(rrr<rr)r>r;rhrfrgr*rpr _UNIVERSAL_SPECSrZ)r;rhr*rkr<trailerr&r'Z nested_specZ header_setr@r.Zoriginal_explicitZ explicit_infoZ parsed_classZ parsed_methodZ parsed_tagZto_parseZexplicit_headerZexplicit_trailerZexpected_classZ expected_tagrr/Z parsed_headerZparsed_trailerrDr>Z ber_indeforiginal_valuer#r#r$r us)                    r c CsTt|}t|||\}}|r@|||kr@|||}td|t|||d|fS)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 z4Extra data - %d bytes of trailing data were provided)r&r')r8rr6r ) r"r|r&r'rZ encoded_lenrZ new_pointerrr#r#r$r+Ws   r+)r)F)NNN)rNNF)hro __future__rrrrrrrxrQrrRsysrr _errorsr Z _ordereddictr _typesr r rrrparserrrutilrrrr version_info cStringIOrrxrangerrFioregisterr9r:r*compilerr4r!objectr rrrwrfrrrrrrrrrrrrrrrrrrrrrrrrrr r6r<r=r?rArCrErGrJr[rarlrnrprrrtrvr_rrrrr r+r#r#r#r$/s        6I,G6:#{Q5Q"XTN5%)LD  $E;!    .8I      ,(. c