; t @c@sdZdkZdkZdkZdkZdkZdkZdkZydkZei Z Wn%e j odk Z e i Z nXde fdYZ dZ dZdZdZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dfdYZ'ddZ(dfdYZ)d e)eifd!YZ*d"e)eifd#YZ+d$e)eifd%YZ,d&e,fd'YZ-d(e,fd)YZ.d*e,fd+YZ/d,e,fd-YZ0d.e,fd/YZ1d0e)fd1YZ2d2e)fd3YZ3d4e)fd5YZ4d6e)fd7YZ5d8e)fd9YZ6d:e)fd;YZ7d<fd=YZ8d>Z9d?Z:dd@Z;ddAZ<dBZ=ddCZ>dDZ?dEZ@dFZAdGZBdHZCdIe,fdJYZDdKe,fdLYZEdMe,fdNYZFdOe,fdPYZGdS(Qs A parser for ASN1 object encoded using BER The doc string just sketches the names of objects in the module. Consult the documentation for more details. Burton S. Kaliski Jr. wrote a helpful introduction to ASN.1 and the BER encoding titled 'A Layman's Guide to a Subset of ASN.1, BER, and DER.' It is available from http://www.rsasecurity.com/rsalabs/pkcs/. The text version is available at ftp://ftp.rsasecurity.com/pub/pkcs/ascii/layman.asc. functions: parse(buf: string) -> ASN1Object display(obj: ASN1Object) parseCfg(path) -> {oid:name} classes: ASN1Object plus subclasses for each asn.1 type, e.q. Sequence, Set, etc. constants: INTEGER, BIT_STRING, OCTET_STRING, NULL, OBJECT_IDENTIFIER, SEQUENCE, SET, PrintableString, T61String, IA5String, UTCTIME, BOOLEAN The following objects are not part of the user-visible API: Displayer ASN1Parser unparseLengthXXX functions NsEOFErrorcBstZRS(N(s__name__s __module__(((s./pylib/pisces/asn1.pysEOFError/siiiiiiiiiiiiiii iiiii@ii is DisplayercBs tZedZddZRS(NcCs|o ||_ndS(N(soidssself(sselfsoids((s./pylib/pisces/asn1.pys__init__VsicCsVy.|io|io t|to|ii|oW|i|}|idod|GdG|dGdGHnd|GdG|dGHdSnd|Gt |GHnt|t o,d|Gd|i GHt |i |dnJd|G|iiGdGHx"|i D]}t ||dqWd|GdGHWn!tj od|G| GHnXdS( NsWarnings sOIDs Descriptions[%d]is{s}(sobjsatomicsselfsoidss isinstancesOIDshas_keysinfosindentsstrs Contextualstagsdisplaysvals __class__s__name__seltsAttributeError(sselfsobjsindentsinfoselt((s./pylib/pisces/asn1.pys__call__Ys& -  (s__name__s __module__sNones__init__s__call__(((s./pylib/pisces/asn1.pys DisplayerUs cCsy|iod|t|GHnt|to,d|Gd|iGHt|i|dnJd|G|i i GdGHx"|iD]}t||dqWd|GdGHWn!t j od|G| GHnXdS(Ns s[%d]is{s}( sobjsatomicsindentsstrs isinstances Contextualstagsdisplaysvals __class__s__name__seltsAttributeError(sobjsindentselt((s./pylib/pisces/asn1.pysdisplayrs  s ASN1ObjectcBs5tZdZdZdZdZedZRS(NicCs ||_dS(N(svalsself(sselfsval((s./pylib/pisces/asn1.pys__init__scCs/t|tot|i|iSndSdS(Ni(s isinstancesothers ASN1Objectscmpsselfsval(sselfsother((s./pylib/pisces/asn1.pys__cmp__scCs!d|iit|ifSdS(Ns%s:%s(sselfs __class__s__name__sreprsval(sself((s./pylib/pisces/asn1.pys__repr__scCs|tjot}d}nd}y |i}Wn&tj otd|ii n X|||o|i SndS(Niis&%s instance does not implement _encode( siosNonesStringIOsioFlagsselfs_encodesencodesAttributeErrors __class__s__name__sgetvalue(sselfsiosioFlagsencode((s./pylib/pisces/asn1.pysencodes     (s__name__s __module__satomics__init__s__cmp__s__repr__sNonesencode(((s./pylib/pisces/asn1.pys ASN1Objects    sSequencecBs,tZdZedZdZdZRS(NicCs&| o g}n||_|_dS(N(svalsselfsdata(sselfsval((s./pylib/pisces/asn1.pys__init__s cCs dt|idd!dSdS(Ns SEQUENCE {iis}(sreprsselfsval(sself((s./pylib/pisces/asn1.pys__repr__scCsDg}x$|iD]}|it|qW|it|dS(N( sencObjssselfsdataseltsappendsencodesioswritesunparseSequence(sselfsiosencObjsselt((s./pylib/pisces/asn1.pys_encodes  (s__name__s __module__satomicsNones__init__s__repr__s_encode(((s./pylib/pisces/asn1.pysSequences  sSetcBs)tZdZdZdZdZRS(NicCs|g|_|_dS(N(svalsselfsdata(sselfsval((s./pylib/pisces/asn1.pys__init__scCsdt|idSdS(NsSET {s}(sreprsselfsval(sself((s./pylib/pisces/asn1.pys__repr__scCsDg}x$|iD]}|it|qW|it|dS(N( sencObjssselfsdataseltsappendsencodesioswrites unparseSet(sselfsiosencObjsselt((s./pylib/pisces/asn1.pys_encodes  (s__name__s __module__satomics__init__s__repr__s_encode(((s./pylib/pisces/asn1.pysSets  sStringcBs tZeZdZdZRS(NcCs||_|_dS(N(svalsselfsdata(sselfsval((s./pylib/pisces/asn1.pys__init__scCs4|it|itt|i|idS(N(sioswriteschrsselfstags unparseLengthslensval(sselfsio((s./pylib/pisces/asn1.pys_encodes(s__name__s __module__sNonestags__init__s_encode(((s./pylib/pisces/asn1.pysStrings s BitStringcBs#tZeZddZdZRS(NicCs||_|_||_dS(N(svalsselfsdatasunused(sselfsvalsunused((s./pylib/pisces/asn1.pys__init__scCsE|it|itt|idt|i|idS(Ni( sioswriteschrsselfstags unparseLengthslensvalsunused(sselfsio((s./pylib/pisces/asn1.pys_encodes(s__name__s __module__s BIT_STRINGstags__init__s_encode(((s./pylib/pisces/asn1.pys BitStrings sPrintableStringcBstZeZRS(N(s__name__s __module__sPRINTABLE_STRINGstag(((s./pylib/pisces/asn1.pysPrintableStringss OctetStringcBstZeZRS(N(s__name__s __module__s OCTET_STRINGstag(((s./pylib/pisces/asn1.pys OctetStringss T61StringcBstZeZRS(N(s__name__s __module__s T61STRINGstag(((s./pylib/pisces/asn1.pys T61Stringss IA5StringcBstZeZRS(N(s__name__s __module__s IA5STRINGstag(((s./pylib/pisces/asn1.pys IA5StringssGeneralizedTimecBs>tZdZedZdZdZdZdZRS(sThe generalized time type, GeneralizedTime, is a standard ASN.1 type for variable precision representation of time. Optionally, the GeneralizedTime field can include a representation of the time differential between local and Greenwich Mean Time. cCs=t|tijo||_t|_n|i|dS(N(stypesvalstypess StringTypesselfsNones_valsunparse(sselfsval((s./pylib/pisces/asn1.pys__init__s  cCst|i|iSdS(N(scmpsselfsvalsother(sselfsother((s./pylib/pisces/asn1.pys__cmp__scCs1|itttt|i|idS(N(sioswriteschrsGENERALIZEDTIMEs unparseLengthslensselfsval(sselfsio((s./pylib/pisces/asn1.pys_encodescCs ti||_t|tijo6yti|}Wq^t j ot dq^Xn|d}|djo(|d}|djo t dqn6|djo(|d}|djo t dqnd||d |d |d |d |d f|_dS(s\Convert a Python time representation to UTC time. Argument must be in UTC. s"time must be seconds or time-tupleiii2s$can't handle data that far in futureils"can't handle data that far in pasts%02d%02d%02d%02d%02d%02dZiiiiiN( stimesmktimesvalsselfs_valstypestypess TupleTypesgmtimes TypeErrorsyys ValueError(sselfsvalsyy((s./pylib/pisces/asn1.pysunparses"       c Cs|io |iSnt|id }t|idd!}t|idd!}t|idd!}t|idd!}t|idd!}|iddjpt |d jod |}n d |}t i ||||||dddf t i |_|iSdS( Niiiii i isZi2ili(sselfs_valsintsvalsyysmm1sddshhsmm2ssssAssertionErrorstimesmktimestimezone(sselfsmm1smm2syysssshhsdd((s./pylib/pisces/asn1.pys_parses    4( s__name__s __module__s__doc__sNones__init__s__cmp__s_encodesunparses_parse(((s./pylib/pisces/asn1.pysGeneralizedTimes     sUTCTimecBs>tZdZedZdZdZdZdZRS(sStandard ASN.1 type for time expressed in GMT draft-ietf-pkix-ipki-part1-08.txt notes: For the purposes of this profile, UTCTime values shall be expressed Greenwich Mean Time (Zulu) and shall include seconds (i.e., times are YYMMDDHHMMSSZ), even where the number of seconds is zero. Conforming systems shall interpret the year field (YY) as follows: Where YY is greater than or equal to 50, the year shall be inter- preted as 19YY; and Where YY is less than 50, the year shall be interpreted as 20YY. cCs=t|tijo||_t|_n|i|dS(N(stypesvalstypess StringTypesselfsNones_valsunparse(sselfsval((s./pylib/pisces/asn1.pys__init__4s  cCst|i|iSdS(N(scmpsselfsvalsother(sselfsother((s./pylib/pisces/asn1.pys__cmp__;scCs1|itttt|i|idS(N(sioswriteschrsUTCTIMEs unparseLengthslensselfsval(sselfsio((s./pylib/pisces/asn1.pys_encode>scCs ti||_t|tijo6yti|}Wq^t j ot dq^Xn|d}|djo(|d}|djo t dqn6|djo(|d}|djo t dqnd||d |d |d |d |d f|_dS(s\Convert a Python time representation to UTC time. Argument must be in UTC. s"time must be seconds or time-tupleiii2s$can't handle data that far in futureils"can't handle data that far in pasts%02d%02d%02d%02d%02d%02dZiiiiiN( stimesmktimesvalsselfs_valstypestypess TupleTypesgmtimes TypeErrorsyys ValueError(sselfsvalsyy((s./pylib/pisces/asn1.pysunparseAs"       c Cs|io |iSnt|id }t|idd!}t|idd!}t|idd!}t|idd!}t|idd!}|iddjpt |d jod |}n d |}t i ||||||dddf t i |_|iSdS( Niiiii i isZi2ili(sselfs_valsintsvalsyysmm1sddshhsmm2ssssAssertionErrorstimesmktimestimezone(sselfsmm1smm2syysssshhsdd((s./pylib/pisces/asn1.pys_parse^s    4( s__name__s __module__s__doc__sNones__init__s__cmp__s_encodesunparses_parse(((s./pylib/pisces/asn1.pysUTCTime%s     s ContextualcBs2tZdZdZdZdZdZRS(sWrapper for optional and choice encoded items (primarily) For contextual encoding, we can't necessarily tell what the type of the value is without looking at the ASN.1 type decl. Of course, the whole purpose of this module is to avoid looking at the type decl -- so we can't win (directly). The solution is this thunk object. When the decoded structure is actually used, it should be clear whether this is, say, an OPTIONAL integer type, some other tagged, known type, or an encoded CHOICE. Call the decode method when the encoding includes the full DER encoding. Call choose when the value doesn't have the appropriate tag/len info. cCsH||_||_||_d|_|io d|_n d|_dS(Nii(stagsselfslengthsvalsunknownsatomic(sselfstagslengthsval((s./pylib/pisces/asn1.pys__init__s      cCs[|iod|i|ifSn6|io d|it|idSn d|iSdS(Nss[%d] {s}s[%d](sselfsunknownstagslengthsvalsrepr(sself((s./pylib/pisces/asn1.pys__repr__s    cCs4|iot|i|_d|_n|iSdS(Ni(sselfsunknownsparsesval(sself((s./pylib/pisces/asn1.pysdecodes  cCs^|ioIt|i}d|_|i|_|i||i|_d|_n|iSdS(Ni( sselfsunknownsparsesvalspsidslengths_parsestag(sselfstagsp((s./pylib/pisces/asn1.pyschooses    (s__name__s __module__s__doc__s__init__s__repr__sdecodeschoose(((s./pylib/pisces/asn1.pys Contextualqs   s ConstructedcBs5tZdZdZdZdZdZRS(NcCs,||_||_||_|i|dS(N(sencsselfstagslengths_parsesval(sselfsencstagslengthsval((s./pylib/pisces/asn1.pys__init__s   cCs]t|tijp tdtt|}|i|_|i |_ |i |_ dS(Ns***bah!(stypesvalstypess StringTypesAssertionErrors ASN1ParsersStringIOspsparsesselfstagsval_tagslengths val_length(sselfsvalsp((s./pylib/pisces/asn1.pys_parses   cCsAt|to)t|i|if|i|ifSndSdS(Ni(s isinstancesothers Constructedscmpsselfstagsval(sselfsother((s./pylib/pisces/asn1.pys__cmp__s)cCsd|it|ifSdS(Ns [%d] {%s}(sselfstagsreprsval(sself((s./pylib/pisces/asn1.pys__repr__scCs<|itttB|iBt|it|i dS(N( sioswriteschrs CONTEXTUALs CONSTRUCTEDsselfstagslengthsencodesval(sselfsio((s./pylib/pisces/asn1.pys_encodes(s__name__s __module__s__init__s_parses__cmp__s__repr__s_encode(((s./pylib/pisces/asn1.pys Constructeds     sBooleancBs#tZdZdZdZRS(NcCs|iodSndSdS(Nii(sselfsval(sself((s./pylib/pisces/asn1.pys __nonzero__s cCs|iodSndSdS(NsTRUEsFALSE(sselfsval(sself((s./pylib/pisces/asn1.pys__repr__s cCs.|itttdt|idS(Ni(sioswriteschrsBOOLEANsselfsval(sselfsio((s./pylib/pisces/asn1.pys_encodes(s__name__s __module__s __nonzero__s__repr__s_encode(((s./pylib/pisces/asn1.pysBooleans  sOIDcBs5tZdZdZdZdZdZRS(NcCst||_dS(N(stuplesvalsself(sselfsval((s./pylib/pisces/asn1.pys__init__scCs8t|d otti|i|_n|iSdS(Ns_hash(shasattrsselfsreducesoperatorsxorsvals_hash(sself((s./pylib/pisces/asn1.pys__hash__scCst|i|iSdS(N(scmpsselfsvalsother(sselfsother((s./pylib/pisces/asn1.pys__cmp__scCsditt|iSdS(Ns.(sjoinsmapsstrsselfsval(sself((s./pylib/pisces/asn1.pys__repr__sc Cs|i}g}|id|d|dx|dD]}|djo|i|q7g}|}}x.|o&t |d\}}|i|qnW|i t d|d }|||d}q7W|itttt|d it t|dS( Ni(iiiiicCs|dBS(Ni(sx(sx((s./pylib/pisces/asn1.pyssis(sselfsvalseltssbytessappendseltstempsdivsremsdivmodsreversesmapsheadsioswriteschrsOBJECT_IDENTIFIERs unparseLengthslensjoin( sselfsioseltssheadstempsbytessremsdivselt((s./pylib/pisces/asn1.pys_encodes"     (s__name__s __module__s__init__s__hash__s__cmp__s__repr__s_encode(((s./pylib/pisces/asn1.pysOIDs     s ASN1ParsercBstZdZdZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZdZhee<ee<e e <e!e<e"e<e#e<e$e<e%e<e&e<e'e<e(e<e)e<e*e<e+e<e e <e,e<e-e<e.e <detd@B}|d@oPqqW||_n|iSdS(Nisstag %s iiii(sselfsiosreadscsEOFErrorsordstagsVERBOSEssyssstderrswriteshexsTAG_MASKsids ENCODING_MASKs CONSTRUCTEDs constructedsvalue(sselfscstag((s./pylib/pisces/asn1.pysgetTags(         cCst|iid}|djod|_d|_n|d@o)t|ii|d@}||_n|d@|_|i ot i i d|in|iSdS(Niiiis length %d ( sordsselfsiosreadsoct1slengths indefinitesconvertOctetsToIntslsVERBOSEssyssstderrswrite(sselfslsoct1((s./pylib/pisces/asn1.pys getLengths       cCs|ii|i}|SdS(N(sselfsiosreadslengthsbuf(sselfsbuf((s./pylib/pisces/asn1.pysgetBody!scCs+t|djo tnt|SdS(Ni(slenscharsEOFErrorsord(sselfschar((s./pylib/pisces/asn1.pysord's cCs]y*|i|_}|i|_}Wntj otdnX|i||SdS(Nsunexpected end of encoded data(sselfsgetTagstags getLengthslengthsEOFErrors ValueErrors_parse(sselfstagslength((s./pylib/pisces/asn1.pysparse,s cCs|it@}|tjoIy|i|}Wn"tj o|i |_ qX|||_ n7|t jo|i |_ nt d|i|f|i SdS(Ns&invalid or unsupported tag: %s (id %s)(sselfsids CLASS_MASKs_classs UNIVERSALs_ASN1Parser__dispatchstagsmethodsKeyErrors parseUnknownsvals CONTEXTUALsparseContextSpecifics ValueError(sselfstagslengthsmethods_class((s./pylib/pisces/asn1.pys_parse4s   cCs#|i|i}t|SdS(N(sselfsordsgetBodysbsBoolean(sselfsb((s./pylib/pisces/asn1.pys parseBooleanFscCs|idjo|i otSn|i}|it@}|t jot ||i |i|Snt |i |i|SdS(Ni( sselfslengths indefinitesNonesgetBodysbufsids ENCODING_MASKsencs CONSTRUCTEDs Constructedstags Contextual(sselfsencsbuf((s./pylib/pisces/asn1.pysparseContextSpecificJs   cCstt|iSdS(N(sSetsparsesselfsgetBody(sself((s./pylib/pisces/asn1.pysparseSetXscCs|iSdS(N(sselfsgetBody(sself((s./pylib/pisces/asn1.pys parseUnknown[scCs7|i}t|djo tnt|SdS(Ni(sselfsgetBodysbufslensEOFErrors getInteger(sselfsbuf((s./pylib/pisces/asn1.pys parseInteger^s  cCs|id@o |iSndS(Ni(sselfsidslength(sself((s./pylib/pisces/asn1.pys parseZerodscCst}|ii}|i}t|t i jp t dt |}y2x+no#t|i}|i|q]WWnttfj onX|SdS(Ns***bah!i(sSequencesseqsselfsiostellsbasesgetBodysbodystypestypess StringTypesAssertionErrorsStringIOsnewIos ASN1ParsersparsesobjsappendsEOFErrors ValueError(sselfsbodysobjsseqsbasesnewIo((s./pylib/pisces/asn1.pys parseSequencejs    cCst|iSdS(N(sGeneralizedTimesselfsgetBody(sself((s./pylib/pisces/asn1.pysparseGeneralizedTimexscCst|iSdS(N(sUTCTimesselfsgetBody(sself((s./pylib/pisces/asn1.pys parseUTCTime{scCs?|i|iid}t|ii|id|SdS(Ni(sselfsordsiosreadsunuseds BitStringslength(sselfsunused((s./pylib/pisces/asn1.pysparseBitString~scCst|iSdS(N(sPrintableStringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparsePrintableStringscCst|iSdS(N(s OctetStringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseOctetStringscCst|iSdS(N(s T61StringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseT61StringscCst|iSdS(N(s VisibleStringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseVisibleStringscCst|iSdS(N(sUniversalStringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseUniversalStringscCst|iSdS(N(s UTF8StringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseUTF8StringscCst|iSdS(N(s BMPStringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseBMPStringscCst|iSdS(N(s IA5StringsselfsgetBody(sself((s./pylib/pisces/asn1.pysparseIA5StringscCs t|i}t|SdS(N(sparsesselfsgetBodyscontainssSet(sselfscontains((s./pylib/pisces/asn1.pysparseSetsc Cs_|i}y|i|d}Wntj o tnX|d}|d}|djo||dd}d}n||g}t }xt |i|dD]}|d@o1|o|d>|d@B}qMt|d@}q|oR|d>|B}y|it|Wn tj o|i|nXt }q|i|qWt|SdS(Nii(iiili(sselfsgetBodysbufsordso1s IndexErrorsEOFErrorsxsysoidsNonesnumsmapsoctetslongsfinalsappendsints OverflowErrorsOID( sselfsoidsoctetsnumsxsysbufsfinalso1((s./pylib/pisces/asn1.pysparseObjectIdentifiers6         cCs|itSdS(N(sselfsgetBodysNone(sself((s./pylib/pisces/asn1.pys parseNulls (0s__name__s __module__sVERBOSEs__init__sgetTags getLengthsgetBodysordsparses_parses parseBooleansparseContextSpecificsparseSets parseUnknowns parseIntegers parseZeros parseSequencesparseGeneralizedTimes parseUTCTimesparseBitStringsparsePrintableStringsparseOctetStringsparseT61StringsparseVisibleStringsparseUniversalStringsparseUTF8StringsparseBMPStringsparseIA5StringsparseObjectIdentifiers parseNullsSEQUENCEsINTEGERsSETsGENERALIZEDTIMEsUTCTIMEs BIT_STRINGs OCTET_STRINGsPRINTABLE_STRINGs VISIBLESTRINGsUNIVERSALSTRINGs BMPSTRINGs UTF8STRINGs T61STRINGs IA5STRINGsOBJECT_IDENTIFIERsNULLsBOOLEANs_ASN1Parser__dispatch(((s./pylib/pisces/asn1.pys ASN1Parsers<                          " cCstt|}|dd@o d}nd}t|dd@}x |dD]}|d>|B}qMW|djo|Sn| SdS(Niiiiii(smapsordsbufsbytesssignslongsvaluesbyte(sbufsbytessvaluessignsbyte((s./pylib/pisces/asn1.pys getIntegers   cCst|}|tijotdt|nt|doft}y|i |Wn+t j ot|Gt|GHnX|i dd|i Sn|titifjot|Sn|tjo tSntdt|dS(Nscan't encode string: %ssencodeisdon't know how to encode: %s(stypesobjststypess StringTypes ValueErrorsreprshasattrsStringIOsfsencodes TypeErrorsseeksreadsIntTypesLongTypesunparseIntegersNones unparseNull(sobjsfst((s./pylib/pisces/asn1.pysencodes"    cCs1t|oddB|Btt||SdS(Ni@i(schrs constructedstags unparseLengthslensenc(stagsencs constructed((s./pylib/pisces/asn1.pysunparseContextualscCsCdi|}t|odtBpttt||SdS(Nsi (sjoinsencObjssbufschrs constructedsSEQUENCEs unparseLengthslen(sencObjss constructedsbuf((s./pylib/pisces/asn1.pysunparseSequencescCsdSdS(Ns((((s./pylib/pisces/asn1.pys unparseNullscCsCdi|}t|odtBpttt||SdS(Nsi (sjoinsencObjssbufschrs constructedsSETs unparseLengthslen(sencObjss constructedsbuf((s./pylib/pisces/asn1.pys unparseSetscCs+|djod}| }nd}|djo dg}ng}|}d}xQ|oIyt|d\}}Wntj o nX|it|qSW|d}|d@o|idn|djo|ddB|d|B}qYW|SdS(Nis>lili( slensbufslsstructsunpackschrsvalsmapsordsbyte(sbufsvalslsbyte((s./pylib/pisces/asn1.pysconvertOctetsToInt2s  *c Cshh}t}x<no4|i} | djoPn| i} | p| ddjoqny:| idd\}}|i|if\}}Wnt j o| }t}nX|djos|o|||