h?RdZddlZddlZddlZddlmZmZddlmZddl m Z m Z m Z m Z ddlTddlmZmZmZmZmZddlmZmZmZmZGd d ZGd d eZGd dejZGddejZGddeZdej fdZ!defdZ"dS)z( pySim: PCSC reader transport link base N)OptionalTuple) Construct)b2hh2bi2hHexstr)*)SwHexstr SwMatchstrResTuplesw_matchparse_command_apdu)ProactiveCommandCommandDetailsDeviceIdentitiesResultc 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.cztd|ddd|ddtd|d|dS)Nz->  z<- z: printr s rr#zStdoutApduTracer.trace_response0sN CHHHc"##hh/000 RRR&'''''rc$tddS)Nz-- RESETr/r%s rr&zStdoutApduTracer.trace_reset4s jrN)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)rr5r6 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;)rr5s rr<zProactiveHandler.receive_fetchDs!"z5ProactiveHandler.prepare_response..Js'XXXA*Q:W:WXaXXXrc<g|]}t|t|Sr)rFrrGs rrJz5ProactiveHandler.prepare_response..Ls(ZZZA*QHX:Y:YZaZZZrdevice_identities source_dev_id dest_dev_id)rNrMresult)rCadditional_information)childrenr from_dictr;r)rr5rCcommand_detailscommand_dev_ids rsp_dev_idsrOs rprepare_responsez!ProactiveHandler.prepare_responseHsXXXXXZZZZZ&(( 23B3J?3[5D5L]5[5]5]^ _ _ _(~ac$d$deffff55rN)rB) r'r(r)r2rr r?r<strrWrrrr4r47s(&6(((((M"2MMMM 6 6%5 6s 6 6 6 6 6 6rr4ceZdZdZ 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 Nrrrfs r _send_apduzLinkBase._send_apducrerc||_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)rrmrns r wait_for_cardzLinkBase.wait_for_cardlrercdS)z&Connect to a card immediately Nrr%s rconnectzLinkBase.connecturercdS)zRetrieve card ATR Nrr%s rget_atrzLinkBase.get_atrzrercdS)zDisconnect from card Nrr%s r disconnectzLinkBase.disconnectrercdS)(Resets the card (power down/up) Nrr%s r _reset_cardzLinkBase._reset_cardrercj|jr|j|S)rx)r[r&ryr%s r reset_cardzLinkBase.reset_cards6   +   ( ( * * *!!!rctt|\}}}}|jr|j|||\}}|jr|j|||t |dkrA|dks|dkr5dd||dzfzz}|jrt|td|z||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?)!z Warning: %s) rrr[rrir#lenr` ValueErrorr0) rrfcase_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 4 ...ml2333bzr9000r!c||}|d}|dkrt|dr}|ddf}|d|ddz|}|d}t}|t |d}t d|dd t|jd |j r9|j ||}|s|j |d }n|j |d }d |D} d | } dtt| dd| zz} || } | d}|dkrt|d}t|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") r~r91xxr80120000NzFETCH: z ()FIXME"command_beyond_terminal_capabilityc6g|]}|Sr)to_tlv)rHxs rrJz.LinkBase.send_apdu_checksw..s 777!188::777rr80140000big)rrsend_apdu_checkswrfrom_tlvrr0r8r'r\r?rWjoinrrto_bytes SwMatchErrorlowerr_) rrfr!rvlast_swfetch_rvr5r6ti_list ti_list_bintailterminal_responseterminal_response_rvs rrzLinkBase.send_apdu_checksws^^D ! !Q%Fllx88lQ%B--j7122;.FKKH qkG#%%D]]3x{#3#344F Ehqkkk4<<3H3H3HI 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)r2rrr4raabcabstractmethodrXrdr r rirlintboolrprrrtrvryr{rr rrrrrZrZUs00NR?C!!*9M!$,-=$>!!!! aaaaa )v)())))%%%   Xc]                       """&f&&&&&P==f=*=(======rrZcxeZdZdZdefdZejdede 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! rtpdurbcdS)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_tpdurerrfc|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") rr~z#unspported protocol selected (T=%d))r_LinkBaseTpdu__send_apdu_T0$_LinkBaseTpdu__send_apdu_transparentrrhs rrizLinkBaseTpdu._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) Nr~00rr)9f616263c000006c)rrr) rrfrrrrr 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)rrhs r__send_apdu_transparentz$LinkBaseTpdu.__send_apdu_transparentEs~~d###rN)r'r(r)rrrrrr r rrirrrrrrrsH ! ! ! ! ! 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*        z66666sw666<`````sw```FP$P$P$P$P$8P$P$P$d)@"8r