+
     h^2  c            
       s   R t ^ RIHt ^ RIHtHtHt ^ RI5 R tR t	]
R]R]R/t]R]R]R]R	]],          R
/t ! R R]4      t]R8X  d    . ROt^^.t^ RIHt ^ RIHt ]P6                  ! 4       ^ ,          P9                  4       t]P=                  4        ]! R]PA                  ]],           4      ,          4       ]! ]PC                  ]. 4      4       R# R# )a  PCSCCardConnection class manages connections thru a PCSC reader.

__author__ = "https://www.gemalto.com/"

Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com

This file is part of pyscard.

pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

pyscard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
)CardConnection)CardConnectionExceptionNoCardExceptionSmartcardException)Z*c                sb   ^ pV e   \         P                  V ,          '       d   V\        ,          p\         P                  V ,          '       d   V\        ,          p\         P
                  V ,          '       d   V\        ,          p\         P                  V ,          '       d   V\        ,          pV# )zBTranslate L{CardConnection} protocol mask into PCSC protocol mask.)	r    T0_protocolSCARD_PROTOCOL_T0T1_protocolSCARD_PROTOCOL_T1RAW_protocolSCARD_PROTOCOL_RAWZT15_protocolSCARD_PROTOCOL_T15protocolpcscprotocol   & H/usr/lib64/python3.14/site-packages/smartcard/pcsc/PCSCCardConnection.pytranslateprotocolmaskr   "   s    L%%00--L%%00--L&&11..L&&11..L    c                s    ^ pV eR   \         P                  V 8X  d   \        p\         P                  V 8X  d   \        p\         P
                  V 8X  d   \        pV# )z-Translate protocol into PCSC protocol header.)r    r   SCARD_PCI_T0r   SCARD_PCI_T1r   SCARD_PCI_RAWr   r   r   translateprotocolheaderr   1   sL    L%%1'L%%1'L&&(2(Lr   ZT0ZT1ZRAWZT15zT0 or T1c                   sp   a  ] tR t^Ht o RtR tR tR tRR ltRR lt	R t
R	 tRR
 ltRR ltR tRtV tR# )PCSCCardConnectionzMPCSCCard connection class. Handles connection with a card thru a
PCSC reader.c                s    \         P                  ! W4       RV n        \        \        4      w  q n        V\        8w  d   \        R\        V4      ,           VR7      hR# )zrConstruct a new PCSC card connection.

@param reader: the reader in which the smartcard to connect to is located.
NzFailed to establish context : hresult)	r    __init__hcardZSCardEstablishContextZSCARD_SCOPE_USERhcontextSCARD_S_SUCCESSr   SCardGetErrorMessage)selfreaderr   s   && r   r   ZPCSCCardConnection.__init__L   sT    
 	-
!67G!Ho%)03G3PP  &r   c                s&    V P                  4        R# )z,Destructor. Clean PCSC connection resources.N)release)r   s   &r   __del__ZPCSCCardConnection.__del__Z   s     	r   c                s8   V P                   ev   \        P                  ! V 4       V P                  4        \	        V P                   4      pV\
        8w  d)   V\        8w  d   \        R\        V4      ,           VR7      hRV n         \        P                  ! V 4       R# )zexplicit releaseNzFailed to release context: r   )
r   r    r    
disconnectZSCardReleaseContextr   ZSCARD_E_INVALID_VALUEr   r   r!   r   r   r   r   r    ZPCSCCardConnection.release`   sw    ==$""4(OO)$--8G/)g9N.N-14H4QQ#  !DMt$r   Nc                s   \         P                  ! W4       \        V4      p^ V8X  d   V P                  4       pVf   \        pVf   \
        pW0n        \        V P                  \        V P                  4      W$4      w  qPn        pV\        8w  d\   RV n        V\        \        39   d   \        RVR7      h\!        R\"        V,          ,           R,           \%        V4      ,           VR7      h^ pV\&        \(        ,          8X  d'   \         P*                  \         P,                  ,          pM2\"         F(  pWv8X  g   K  \/        R\"        V,          ,          4      pK*  	  \0        P3                  W4       R# )zConnect to the card.

If protocol is not specified, connect with the default
connection protocol.

If mode is not specified, connect with
C{smartcard.scard.SCARD_SHARE_SHARED}.NzUnable to connectr   z!Unable to connect with protocol: . CardConnection.%s_protocol)r    connectr   getProtocolSCARD_SHARE_SHAREDZSCARD_UNPOWER_CARDdispositionZSCardConnectr   Zstrr   r   r   SCARD_W_REMOVED_CARDSCARD_E_NO_SMARTCARDr   r   dictProtocolr   r   r   r   r   evalr   setProtocolr   r   Zmoder)   r   r   ZdwActiveProtocolZp   &&&&    r   r&   ZPCSCCardConnection.connectn   s?    	t.,X6++-L<%D ,K&0<MM3t{{+T1
-- o%DJ/1EFF%&97KK-7"<01 +734 $  03DDD &11N4N4NNH!(#$@<PQ?$RSH " 	&&t6r   c                s   \         P                  ! W4       V P                  f   \        R4      h\	        V4      p^ V8X  d   V P                  4       pVf   \        pVf   \        pW0n        \        V P                  W$V P                  4      w  rVV\        8w  d\   RV n        V\        \        39   d   \        RVR7      h\        R\        V,          ,           R,           \        V4      ,           VR7      h^ pV\         \"        ,          8X  d'   \         P$                  \         P&                  ,          pM2\         F(  pWv8X  g   K  \)        R\        V,          ,          4      pK*  	  \*        P-                  W4       R# )a
  Reconnect to the card.

If protocol is not specified, connect with the default
connection protocol.

If mode is not specified, connect with
C{smartcard.scard.SCARD_SHARE_SHARED}.

If disposition is not specified, do a warm reset
(C{smartcard.scard.SCARD_RESET_CARD})NCard not connectedzUnable to reconnectr   z#Unable to reconnect with protocol: r$   r%   )r    	reconnectr   r   r   r'   r(   ZSCARD_RESET_CARDr)   ZSCardReconnectr   r*   r+   r   r,   r   r   r   r   r   r-   r   r.   r/   r0   r   r2   ZPCSCCardConnection.reconnect   sM    	  0::)*>??,X6++-L<%D *K&$2JJD,<,<%
! o%DJ/1EFF%&;WMM-9"<01 +734 $  03DDD &11N4N4NNH!(#$@<PQ?$RSH " 	&&t6r   c                s   V P                   ei    \        P                  ! V 4       \	        V P                   V P
                  4      pRV n         V\        8w  d   \        R\        V4      ,           VR7      hR# R#   \         d     L`i ; i)zDisconnect from the card.NzFailed to disconnect: r   )	r   r    r"   Z	TypeErrorZSCardDisconnectr)   r   r   r   r#   r   r   r"   ZPCSCCardConnection.disconnect   s     ::!))$/ &djj$2B2BCGDJ/)-,/CG/LL#  * "  s   A9 9BBc                s    \         P                  ! V 4       V P                  f   \        R4      h\	        V P                  4      w  rr4pV\
        8w  d   \        R\        V4      ,           VR7      hV# )zReturn card ATRr1   zFailed to get status: r   )r    getATRr   r   ZSCardStatusr   r   )r   r   r   Zstater   Zatrs   &     r   r3   ZPCSCCardConnection.getATR   sh    d#::)*>??0;DJJ0G-#o%)(+?+HH  
r   c                sr   Vf   V P                  4       p\        P                  ! WV4       \        V4      p^ V8X  d   \	        R4      hV P
                  f   \	        R4      h\        V P
                  W14      w  rEV\        8w  d7   \	        R\        V,          ,           R,           \        V4      ,           VR7      h\        V4      ^8  d   \	        RVR7      hVR	,          R,           R,          pVR
,          R,           R,          pVRR	  Uu. uF  qR,           R,          NK  	  p	p\        V	4      Wg3# u upi )a  Transmit an apdu to the card and return response apdu.

@param command:  command apdu to transmit (list of bytes)

@param protocol: the transmission protocol, from
    L{CardConnection.T0_protocol}, L{CardConnection.T1_protocol}, or
    L{CardConnection.RAW_protocol}

@return:     a tuple (response, sw1, sw2) where
            - response are the response bytes excluding status words
            - sw1 is status word 1, e.g. 0x90
            - sw2 is status word 2, e.g. 0x1A
Nr1   z!Failed to transmit with protocol r$   r   zCard returned no valid response   z|Invalid protocol in transmit: must be CardConnection.T0_protocol, CardConnection.T1_protocol, or CardConnection.RAW_protocolii)r'   r    
doTransmitr   r   r   ZSCardTransmitr   dictProtocolHeaderr   Zlenlist)
r   commandr   Zpcscprotocolheaderr   responseZsw1Zsw2xdatas
   &&&       r   r5   ZPCSCCardConnection.doTransmit   s:    '')H!!$:4X>"")0  ::)*>??)$**6HRo%)3$%789 'w/0    x=1)17  |c!S(|c!S()1#27ASC7Dz3## 8s   D4c                s   Vf   . p\         P                  ! WV4       \        V P                  W4      w  r4V\        8w  d   \        R\        V4      ,           VR7      hV Uu. uF  qUR,           R,          NK  	  pp\        V4      # u upi )zTransmit a control command to the reader and return response.

@param controlCode: control command

@param command:     command data to transmit (list of bytes)

@return:      response are the response bytes (if any)
zFailed to control r   r4   )r    	doControlZSCardControlr   r   r   r   r7   )r   ZcontrolCoder8   r   r9   r:   r;   s   &&&    r   r<   ZPCSCCardConnection.doControl(  s     ?G  G<([Jo%$$';G'DDg  *22ASC2Dz 3s   "Bc                s    \         P                  ! W4       \        V P                  V4      w  r#V\        8w  d   \        R\        V4      ,           VR7      hV# )zyget an attribute

@param attribId: Identifier for the attribute to get

@return:   response are the attribute byte array
zFailed to getAttrib r   )r    doGetAttribZSCardGetAttribr   r   r   r   )r   ZattribIdr   r9   s   &&  r   r=   ZPCSCCardConnection.doGetAttrib=  sR     	""42*4::x@o%$&)=g)FFPW  r   )r)   r   r   )NNN)N)__name__Z
__module__Z__qualname__Z__firstlineno____doc__r   r!   r    r&   r2   r"   r3   r5   r<   r=   Z__static_attributes__Z__classdictcell__)Z__classdict__s   @r   r   r   H   sD     %/7b57n*.$`* r   r   Z__main__)CM_IOCTL_GET_FEATURE_REQUEST)
PCSCReaderz%r %x %xN)i   i       rB   i   )"r?   Zsmartcard.CardConnectionr    Zsmartcard.Exceptionsr   r   r   Zsmartcard.scardr   r   r   r   r   r6   r   r   r	   r
   r,   r   r>   ZSELECTZ
DF_TELECOMZsmartcard.pcsc.PCSCPart10r@   Zsmartcard.pcsc.PCSCReaderrA   ZreadersZcreateConnectionZccr&   ZprintZtransmitZcontrol) r   r   <module>rC      s   0 4 
 
 #D,mUS tt)):B BJ z>+FJF4				a	 	1	1	3BJJL	*r{{6J#67
78	"**12
67 r   