i @~dZddlZddlZddlZddlmZmZddlmZddl m Z m Z m Z m Z ddlTddlmZmZmZmZmZddlmZmZmZmZdd lmZejeZGd d ZGd d eZ Gddej!Z"Gddej!Z#Gdde#Z$dej%fdZ&de#fdZ'dS)z( pySim: PCSC reader transport link base N)OptionalTuple) Construct)b2hh2bi2hHexstr)*)SwHexstr SwMatchstrResTuplesw_matchparse_command_apdu)ProactiveCommandCommandDetailsDeviceIdentitiesResult) PySimLoggerc eZdZdZdZdZdS) ApduTracercdSN)selfcmds F/home/jenkins/workspace/simtester-sanitize/pySim/transport/__init__.py trace_commandzApduTracer.trace_command% cdSrrrrswresps rtrace_responsezApduTracer.trace_response(rrcdSrrrs r trace_resetzApduTracer.trace_reset+rrN)__name__ __module__ __qualname__rr$r'rrrrr$sA           rrceZdZdZdZdZdS)StdoutApduTracerz6Minimalistic APDU tracer, printing commands to stdout.ctd|dd|ddtd||dS)Nz-> %s %s z <- %s: %sloginfor!s rr$zStdoutApduTracer.trace_response0sD S"Xs233x000 b$'''''rc:tddS)Nz-- RESETr/r&s rr'zStdoutApduTracer.trace_reset4s rN)r(r)r*__doc__r$r'rrrr,r,.s8@@(((rr,c@eZdZdZdedefdZdefdZd dedefdZ d S) ProactiveHandlerzAbstract base class representing the interface of some code that handles the proactive commands, as returned by the card in responses to the FETCH command.pcmdparsedcdt|jz}t||r t||}||jS||S)Nz handle_%s)typer(hasattrgetattrdecoded receive_fetch)rr6r7 handle_namehandlers rreceive_fetch_rawz"ProactiveHandler.receive_fetch_raw;s\!DLL$99 4 % % )dK00G74<(( (!!$'''rc0td|jz)z=Default handler for not otherwise handled proactive commands.zNo handler method for %s)NotImplementedErrorr<)rr6s rr=zProactiveHandler.receive_fetchDs!"z5ProactiveHandler.prepare_response..Js'XXXA*Q:W:WXaXXXrc<g|]}t|t|Sr)rGrrHs rrKz5ProactiveHandler.prepare_response..Ls(ZZZA*QHX:Y:YZaZZZrdevice_identities source_dev_id dest_dev_id)rOrNresult)rDadditional_information)childrenr from_dictr<r)rr6rDcommand_detailscommand_dev_ids rsp_dev_idsrPs rprepare_responsez!ProactiveHandler.prepare_responseHsXXXXXZZZZZ&(( 23B3J?3[5D5L]5[5]5]^ _ _ _(~ac$d$deffff55rN)rC) r(r)r*r3rr r@r=strrXrrrr5r57s(&6(((((M"2MMMM 6 6%5 6s 6 6 6 6 6 6rr5ceZdZdZ ddeedeefdZej de fdZ ej de de fd Zd Zej dd eed efdZej dZej de fdZej dZej dZdZde de fdZdde dede fdZdS)LinkBasez&Base class for link/transport to card.N apdu_tracerproactive_handlerc>||_||_||_d|_dSNF)sw_interpreterr\r] apdu_strict)rr`r\r]s r__init__zLinkBase.__init__Xs(,&!2 rreturncdS)zRImplementation specific method for printing an information to identify the device.Nrr&s r__str__zLinkBase.__str___rapducdS)zImplementation specific method for sending the APDU. This method must accept APDUs as defined in ISO/IEC 7816-3, section 12.1 Nrrrgs r _send_apduzLinkBase._send_apducrfrc||_dS)z*Set an (optional) status word interpreter.N)r`)rinterps rset_sw_interpreterzLinkBase.set_sw_interpreterhs$rFtimeout newcardonlycdS)zWait for a card and connect to it Args: timeout : Maximum wait time in seconds (None=no timeout) newcardonly : Should we wait for a new card, or an already inserted one ? Nr)rrnros r wait_for_cardzLinkBase.wait_for_cardlrfrcdS)z&Connect to a card immediately Nrr&s rconnectzLinkBase.connecturfrcdS)zRetrieve card ATR Nrr&s rget_atrzLinkBase.get_atrzrfrcdS)zDisconnect from card Nrr&s r disconnectzLinkBase.disconnectrfrcdS)(Resets the card (power down/up) Nrr&s r _reset_cardzLinkBase._reset_cardrfrcj|jr|j|S)ry)r\r'rzr&s r reset_cardzLinkBase.reset_cards6   +   ( ( * * *!!!rctt|\}}}}|jr|j|||\}}|jr|j|||t |dkrI|dks|dkr=dd||dzfzz}|jrt|t |||fS)aSends an APDU with minimal processing Args: apdu : string of hexadecimal characters (ex. "A0A40000023F00", must comply to ISO/IEC 7816-3, section 12.1) Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") rz7received unexpected response data, incorrect APDU-case z&(%d, should be %d, missing Le field?)!) rrr\rrjr$lenra ValueErrorr0warning) rrgcase_lc_le_datadatar" exeption_strs r send_apduzLinkBase.send_apdus#5SYY"?"?sC   1   * *4 0 0 0__T** r   <   + +D"d ; ; ; t99q==daii4199T 4dQh7G GHL * ... L)))bzr9000r"c||}|d}|dkrt|dr|ddf}|d|ddz|}|d}t}|t |d}t d|dt|j |j r9|j ||}|s|j |d }n|j |d }d |D} d | } d tt| dd| zz} || } | d}|dkrt|dt|d|s.t#|d||j|S)a3Sends an APDU and check returned SW Args: apdu : string of hexadecimal characters (ex. "A0A40000023F00", must comply to ISO/IEC 7816-3, section 12.1) sw : string of 4 hexadecimal characters (ex. "9000"). The user may mask out certain digits using a '?' to add some ambiguity if needed. Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") rr91xxr80120000NzFETCH: %s (%s)FIXME"command_beyond_terminal_capabilityc6g|]}|Sr)to_tlv)rIxs rrKz.LinkBase.send_apdu_checksw..s 777!188::777rr80140000big)rrsend_apdu_checkswrfrom_tlvrr0r1r9r(r]r@rXjoinrrto_bytes SwMatchErrorlowerr`) rrgr"rvlast_swfetch_rvr6r7ti_list ti_list_bintailterminal_responseterminal_response_rvs rrzLinkBase.send_apdu_checksws^^D ! !Q%Fllx88lQ%B--j7122;.FKKH qkG#%%D]]3x{#3#344F HH%x{DLL4I J J J% n0BB4PPU"4EEdGTTG0AA$Hlmm87w777K88K((D!+ST1C1CAu1M1MPT1T-U-U U #'>>2C#D#D *1-GUFllx88lX1r"" Gr!ubhhjj$2EFF F r)NNNr_)r)r(r)r*r3rrr5rbabcabstractmethodrYrer r rjrmintboolrqrsrurwrzr|rr rrrrr[r[Us00NR?C!!*9M!$,-=$>!!!! aaaaa )v)())))%%%   Xc]                       """&f&&&&&P==f=*=(======rr[cxeZdZdZdefdZejdede fdZ dede fdZ dede fd Z dede fd Z d S) LinkBaseTpdurprotocolc||_dS)aSet TPDU format. Each transport protocol has its specific TPDU format. This method allows the concrete transport layer implementation to set the TPDU format it expects. (This method must not be called by higher layers. Switching the TPDU format does not switch the transport protocol that the reader uses on the wire) Args: protocol : number of the transport protocol used. (0 => T=0, 1 => T=1) N)r)rrs rset_tpdu_formatzLinkBaseTpdu.set_tpdu_formats! rtpdurccdS)zImplementation specific method for sending the resulting TPDU. This method must accept TPDUs as defined in ETSI TS 102 221, section 7.3.1 and 7.3.2, depending on the protocol selected. Nr)rrs r send_tpduzLinkBaseTpdu.send_tpdurfrrgc|jdkr||S|jdkr||Std|jz)aTransforms APDU into a TPDU and sends it. The response TPDU is returned as APDU back to the caller. Args: apdu : string of hexadecimal characters (eg. "A0A40000023F00", must comply to ISO/IEC 7816-3, section 12) Returns: tuple(data, sw), where data : string (in hex) of returned data (ex. "074F4EFFFF") sw : string (in hex) of status word (ex. "9000") rrz#unspported protocol selected (T=%d))r_LinkBaseTpdu__send_apdu_T0$_LinkBaseTpdu__send_apdu_transparentrris rrjzLinkBaseTpdu._send_apdu s\ =A  &&t,, , ]a  //55 5>NOOOrctt|\}}}}|dkr|dz}n|dkr |dd}n|}|}||\}} | | dddvrC|dddz| ddz} | }|| \} } || z }| dddvC| ddd kr-|dd | ddz} || \}} || fS) Nr00rr)9f616263c000006c)rrr) rrgrrrrr prev_tpdurr"tpdu_grds r__send_apdu_T0zLinkBaseTpdu.__send_apdu_T0s+ #5SYY"?"?sC 199$;DD QYY9DDD >>$''b >ac7666qs)h.AaC8# w//2 ac7666!A#w$#AaC.2ac72>>'22bRxrc,||Sr)rris r__send_apdu_transparentz$LinkBaseTpdu.__send_apdu_transparentEs~~d###rN)r(r)r*rrrrrr r rrjrrrrrrrsH ! ! ! ! ! ZfZZZZZPvP(PPPP"%6%h%%%%N$F$x$$$$$$rr arg_parsercddlm}ddlm}ddlm}ddlm}||||||||| ddd |S) zOAdd all reader related arguments to the given argparse.Argumentparser instance.r SerialSimLink PcscSimLinkModemATCommandLinkCalypsoSimLinkz --apdu-trace store_truez8Trace the command/response APDUs exchanged with the card)actionhelp) pySim.transport.serialrpySim.transport.pcscrpySim.transport.modem_atcmdrpySim.transport.calypsorargparse_add_reader_args add_argument)rrrrrs rrrJs444444000000>>>>>>666666**:666((444// ;;;++J777Nrs & """"""""////////////TTTTTTTTTTTTTTPPPPPPPPPPPP!!!!!!koh        z66666sw666<`````sw```FP$P$P$P$P$8P$P$P$d)@"8r