
    iN                     T   d Z ddlmZmZmZmZmZ ddlmZ ddl	T ddl
T ddlmZ ddlT ddlZ G d ded	
          Z G d ded
          Z G d ded
          Z G d ded
          Z G d dedeeeeg          Z G d ded
          Z G d ded
          Z G d ded 
          Z G d! d"ed#eeeg          Z G d$ d%ed&eeg          Z G d' d(ed)eg          Z G d* d+ed,eg          Z G d- d.ed/
          Z G d0 d1ed2
          Z G d3 d4ed5eg          Z G d6 d7ed8eg          Z  G d9 d:ed;eg          Z! G d< d=ed>e!g          Z" G d? d@edAeg          Z# G dB dCedDeeeeg          Z$ G dE dFedG
          Z% G dH dIedJeeg          Z& G dK dLedMeeg          Z' G dN dOedP
          Z( G dQ dRedS
          Z) G dT dUedV
          Z* G dW dXedY
          Z+ G dZ d[ed\eeg          Z, G d] d^ed_
          Z- G d` dae.eegb          Z/ G dc dde.eeee"gb          Z0 G de dfe.e-e#e$e%e&e'e(e)e*e+g
b          Z1 G dg dhe.ee,e gb          Z2 G di dje3          Z4dkdldmdndodpdqdrdsdtdudvdwdxdydzd{iZ5 G d| d}e6          Z7dS )~zW
Support for the Secure Element Access Control, specifically the ARA-M inside an UICC.
    )GreedyStringStructEnumInt8ubInt16ub)Optional)*)HexstrNc                   $    e Zd Z ee          ZdS )AidRefDON__name__
__module____qualname__
HexAdapterGreedyBytes
_construct     9/home/jenkins/workspace/simtester-sanitize/pySim/ara_m.pyr   r   (           K((JJJr   r   O   )tagc                       e Zd ZdS )AidRefEmptyDONr   r   r   r   r   r   r   r   -           Dr   r      c                   $    e Zd Z ee          ZdS )DevAppIdRefDONr   r   r   r   r    r    2   r   r   r       c                   <    e Zd Z ed ed          z            ZdS )PkgRefDOpackage_name_stringasciiN)r   r   r   r   r   r   r   r   r   r#   r#   7   s-        -ll7.C.CCDDJJJr   r#      c                       e Zd ZdS )RefDONr   r   r   r   r(   r(   <   r   r   r(      )r   nestedc                        e Zd ZdefdZd ZdS )ApduArDOdoc           
      *   t          |          dk    rG|d         dk    rddi| _        | j        S |d         dk    rddi| _        | j        S t          d          t          |          dz  rt          dt          |          z            d	g i| _        d}|t          |          k     rd| j        d	xx         t          |||d
z                      t          ||d
z   |dz                      dgz  cc<   |dz  }|t          |          k     d| j        S )N   r   generic_access_ruleneveralways'Invalid 1-byte generic APDU access rule   z.Invalid non-modulo-8 length of APDU filter: %dapdu_filter   headermask)lendecoded
ValueErrorb2h)selfr-   offsets      r   _from_byteszApduArDO._from_bytesC   sA   r77a<<!u}} 5w?|#!u}} 5x@|#FGGG2ww{ ] !QTWXZT[T[![\\\)2.DLF3r77""]+++3r&PQ/?R;S;S8;Bvaxq?P<Q8R8R1T 1T 0U U+++! 3r77"" <r   c                    d| j         v r5| j         d         dk    rdS | j         d         dk    rdS t          d          d| j         vrt          d          | j         d         }d	}|D ]}d
|vsd|vrt          d          t          |d
                   }t          |d                   }t          |          dk    st          |          dk    rt          d          |||z   z  }|S )Nr0   r1       r2      r3   r5   zInvalid APDU AR DOr   r8   r9   z(APDU filter must contain header and maskr6   z0APDU filter header and mask must each be 4 bytes)r;   r<   h2br:   )r>   filtersresfheader_bmask_bs         r   	_to_byteszApduArDO._to_bytesW   s    DL00|12g==w|12h>>wFGGG DL00 !5666l=1GC ) )1}}FaKK$%OPPPq{++QvYx==A%%V)9)9$%WXXXx&((Jr   N)r   r   r   bytesr@   rJ   r   r   r   r,   r,   A   s>         e        (    r   r,      c                   B    e Zd Z ed eedd          z            ZdS )NfcArDOnfc_event_access_ruler   r/   )r1   r2   N)r   r   r   r   r   r   r   r   r   r   rN   rN   n   s;        /V1Q7778 9 9JJJr   rN      c                   N    e Zd Z ed e ed                    z            ZdS )PermArDOpermissionsr4   Nr   r   r   r   r   Bytesr   r   r   r   rR   rR   t   s6         jjq&:&::;;JJJr   rR      c                       e Zd ZdS )ArDONr   r   r   r   rX   rX   z   r   r   rX      c                       e Zd ZdS )RefArDONr   r   r   r   r[   r[      r   r   r[      c                       e Zd ZdS )ResponseAllRefArDONr   r   r   r   r^   r^      r   r   r^   i@  c                       e Zd ZdS )ResponseArDONr   r   r   r   r`   r`      r   r   r`   iP  c                   N    e Zd Z ed e ed                    z            ZdS )ResponseRefreshTagDOrefresh_tagr4   NrT   r   r   r   rb   rb      s4        jjq&:&::;;JJJr   rb   i   c                   :    e Zd Z edez  dez  dez            ZdS )DeviceInterfaceVersionDOmajorminorpatchN)r   r   r   r   r   r   r   r   r   re   re      s-        GGJJJr   re      c                       e Zd ZdS )DeviceConfigDONr   r   r   r   rk   rk      r   r   rk      c                       e Zd ZdS )ResponseDeviceConfigDONr   r   r   r   rn   rn      r   r   rn   i  c                       e Zd ZdS )AramConfigDONr   r   r   r   rp   rp      r   r   rp      c                       e Zd ZdS )ResponseAramConfigDONr   r   r   r   rs   rs      r   r   rs   i!  c                       e Zd ZdS )CommandStoreRefArDONr   r   r   r   ru   ru      r   r   ru      c                       e Zd ZdS )CommandDeleteNr   r   r   r   rx   rx      r   r   rx      c                       e Zd ZdS )CommandUpdateRefreshTagDONr   r   r   r   r{   r{      r   r   r{      c                       e Zd ZdS )CommandRegisterClientAidsDONr   r   r   r   r~   r~      r   r   r~      c                       e Zd ZdS )
CommandGetNr   r   r   r   r   r      r   r   r      c                       e Zd ZdS )CommandGetAllNr   r   r   r   r   r      r   r   r      c                       e Zd ZdS )CommandGetClientAidsDONr   r   r   r   r   r      r   r   r      c                       e Zd ZdS )CommandGetNextNr   r   r   r   r   r      r   r   r      c                       e Zd ZdS )CommandGetDeviceConfigDONr   r   r   r   r   r      r   r   r      c                       e Zd ZdS )ResponseAracAidDONr   r   r   r   r   r      r   r   r   ip  c                   2    e Zd Z edez  dez            ZdS )BlockDOr?   lengthN)r   r   r   r   r   r   r   r   r   r   r   r      s(        ((6/::JJJr   r      c                       e Zd ZdS )GetCommandDoCollectionNr   r   r   r   r   r      s        Dr   r   )r*   c                       e Zd ZdS )GetResponseDoCollectionNr   r   r   r   r   r      r   r   r   c                       e Zd ZdS )StoreCommandDoCollectionNr   r   r   r   r   r      s        
 	Dr   r   c                       e Zd ZdS )StoreResponseDoCollectionNr   r   r   r   r   r      r   r   r   c                        e Zd Z	 	 d fd	Zd Zeddefd	            Zed
efd            Z	ed             Z
edd            Z ed           G d de                      Z xZS )ADF_ARAMa00000015141434c00	ADF.ARA-MNARA-M Applicationc                     t                                          |||||           | xj        |                                 gz  c_        g }|                     |           d S )N)aidfidsfidnamedesc)super__init__shell_commandsAddlShellCommands	add_files)r>   r   r   r   r   r   files	__class__s          r   r   zADF_ARAM.__init__   sf    Sc4dKKK 6 6 8 899ur   c                 @    t           j                            |          S )N)pySimglobal_platformdecode_select_response)r>   data_hexs     r   r   zADF_ARAM.decode_select_response  s    $;;HEEEr   9000hdrc                 N   |r9|                                 }t          |          }|dk    rt          d          nd}d}|d|z  z   t          |          z   }|                     ||          \  }}	|r2|r. |            }
|
                    t          |                     |
S |S dS )z~Transceive an APDU with the card, transparently encoding the command data from TLV
        and decoding the response data tlv.   z DO > 255 bytes not supported yetr   r   z%02xN)to_ier:   r<   r=   send_apdu_checkswfrom_tlvrD   )sccr   cmd_doresp_clsexp_sw
cmd_do_enc
cmd_do_lenc_apdudata_swresp_dos              r   xceive_apdu_tlvzADF_ARAM.xceive_apdu_tlv  s      	JZJC !CDDD   JJ+,s:>++FF;;s 	 "(**  T+++K4r   returnc                 F    t                               | d|t                    S )z&Build the Command APDU for STORE DATA.80e29000)r   r   r   )r   r-   s     r   
store_datazADF_ARAM.store_data  s     ''Z=VWWWr   c                 F    t                               | dd t                    S )N80caff40)r   r   r   )r   s    r   get_allzADF_ARAM.get_all$  s    ''Z?VWWWr   r   r/   c                     t                      }|                    d|||dig           t                              | d|t                    S )Ndevice_interface_version_do)rf   rg   rh   80cadf21)rk   from_val_dictr   r   rs   )r   v_majorv_minorv_patchr   s        r   
get_configzADF_ARAM.get_config(  se    !!<(/'G?U ?U V W 	X 	X 	X''ZAUVVVr   zApplication-Specific Commandsc                      e Zd Zd Zd Ze                                Ze                    ddd           e	                                Z
e
                    dd	           e
                    d
dd           e                    dd	           e	                                Ze                    ddd           e                    ddd           e                    dd	           e	                                Ze                    ddd           e                    ddd           e                    dd	           e                    e          d             Zd Zd ZdS ) ADF_ARAM.AddlShellCommandsc                     t                               | j        j        j                  }|r.| j                            |                                           dS dS )z"GET DATA [All] on the ARA-M AppletN)r   r   _cmdlchanr   poutput_jsonto_dictr>   _optsres_dos      r   do_aram_get_allz*ADF_ARAM.AddlShellCommands.do_aram_get_all1  sT    %%dio&9::F 9	&&v~~'7'7888889 9r   c                     t                               | j        j        j                  }|r.| j                            |                                           dS dS )z\Perform GET DATA [Config] on the ARA-M Applet: Tell it our version and retrieve its version.N)r   r   r   r   r   r   r   r   s      r   do_aram_get_configz-ADF_ARAM.AddlShellCommands.do_aram_get_config7  sT    (()<==F 9	&&v~~'7'7888889 9r   z--device-app-idTzIdentifies the specific device application that the rule applies to. Hash of Certificate of Application Provider, or UUID. (20/32 hex bytes))requiredhelpz--aidzIdentifies the specific SE application for which rules are to be stored. Can be a partial AID, containing for example only the RID. (5-16 or 0 hex bytes))r   z--aid-empty
store_truezUNo specific SE application, applies to implicitly selected application (all channels))actionr   z	--pkg-refz6Full Android Java package name (up to 127 chars ASCII)z--apdu-neverzAPDU access is not allowedz--apdu-alwayszAPDU access is allowedz--apdu-filterzZAPDU filter: multiple groups of 8 hex bytes (4 byte CLA/INS/P1/P2 followed by 4 byte mask)z--nfc-alwayszNFC event access is allowedz--nfc-neverzNFC event access is not allowedz--android-permissionsz8Android UICC Carrier Privilege Permissions (8 hex bytes)c                    g }|j         |d|j         igz  }n|j        r|ddigz  }|d|j        igz  }|j        r|dd|j        iigz  }g }|j        r|ddd	iigz  }n|j        r|ddd
iigz  }n|j        rt          |j                  dz  r$t          dt          |j                             d}g }|t          |j                  k     rI||j        ||dz            |j        |dz   |dz            dgz  }|dz  }|t          |j                  k     I|dd|iigz  }|j	        r|ddd
iigz  }n|j
        r
|ddd	iigz  }|j        r|dd|j        iigz  }dd|id|igig}t                      }|                    |           t                              | j        j        j        |          }|r.| j                            |                                           dS dS )zJPerform STORE DATA [Command-Store-REF-AR-DO] to store a (new) access rule.N
aid_ref_doaid_ref_empty_dodev_app_id_ref_do
pkg_ref_dor$   
apdu_ar_dor0   r1   r2      z-Invalid non-modulo-16 length of APDU filter: r   r4   r7   r5   	nfc_ar_dorO   
perm_ar_dorS   	ref_ar_doref_doar_do)r   	aid_emptydevice_app_idpkg_ref
apdu_neverapdu_alwaysr5   r:   r<   
nfc_always	nfc_neverandroid_permissionsru   r   r   r   r   r   r   r   r   )	r>   optsref_do_contentar_do_contentr?   r5   dcsrador   s	            r   do_aram_store_ref_ar_doz2ADF_ARAM.AddlShellCommands.do_aram_store_ref_ar_doX  s     Nx#L$(#;"<< ?$6#=">> 3T5GHIIN| ZL3H$,2W#X"YYM P<2G1Q"R!SS! P<2G1R"S!TT! 	Pt'((2- n$%lUXY]YiUjUj%l%lmmm s4#34444t/?vax/P-1-=fQhvby>P-Q%S %S $T TKbLF s4#34444 <-1M"N!OO U;1H(0S"T!UU U;1H'0R"S!TT' ]<-AY1Z"[!\\> :Wm<TUVWA(**F  ###(()<fEEF 9	&&v~~'7'7888889 9r   c                     t                      }t                              | j        j        j        |          }|r.| j                            |                                           dS dS )zDPerform STORE DATA [Command-Delete[all]] to delete all access rules.N)rx   r   r   r   r   r   r   r   )r>   r   deldor   s       r   do_aram_delete_allz-ADF_ARAM.AddlShellCommands.do_aram_delete_all  s_    !OOE(()<eDDF 9	&&v~~'7'7888889 9r   c                 P    | j         j        j                            dd           dS )u   Lock STORE DATA command to prevent unauthorized changes
            (Proprietary feature that is specific to sysmocom's fork of Bertrand Martel’s ARA-M implementation.)80e2900001A1r   N)r   r   r   r   )r>   r   s     r   do_aram_lockz'ADF_ARAM.AddlShellCommands.do_aram_lock  s'     IO11.&IIIIIr   N)r   r   r   r   r   argparseArgumentParserstore_ref_ar_do_parseadd_argumentadd_mutually_exclusive_groupaid_grpapdu_grpnfc_grpcmd2with_argparserr   r  r  r   r   r   r   r   /  sU       	9 	9 	9	9 	9 	9 !) 7 7 9 9**  4B 	+ 	C 	C 	C'DDFF  v 	 	w 	w 	w]<"y 	 	{ 	{ 	{**V 	+ 	X 	X 	X )EEGG<6R 	 	T 	T 	TL7O 	 	Q 	Q 	Q"~ 	 	@ 	@ 	@'DDFF^L"? 	 	A 	A 	A]<"C 	 	E 	E 	E**#*d 	+ 	f 	f 	f 
		2	3	3&	9 &	9 
4	3&	9P	9 	9 	9	J 	J 	J 	J 	Jr   r   )r   r   NNr   )r   )r   r   r/   )r   r   r   r   r   staticmethodr
   r   rK   r   r   r   with_default_category
CommandSetr   __classcell__r   s   @r   r   r      sJ       RV)     F F F  &    \, Xu X X X \X X X \X W W W \W :;;[J [J [J [J [JJ [J [J <;[J [J [J [J [Jr   r   ARA-Mz:Rule successfully stored but an access rule already existszORule successfully stored but contained at least one unknown (discarded) BER-TLVzMemory ProblemzWrong Length in Lcz&DO is not supported by the ARA-M/ARA-CzSecurity status not satisfiedzHRules have been updated and must be read again / logical channels in usezConditions not satisfiedz$Incorrect values in the command dataz.Rules have been updated and must be read againzIncorrect P1 P2zReferenced data not foundz<Conflicting access rule already exists in the Secure ElementzInvalid instructionzInvalid class)638163826581670069816982698469856a806a846a866a886a896d006e00c                   f     e Zd Z fdZed             Zed             Zedefd            Z xZ	S )CardApplicationARAMc                 r    t                                          dt                      t                     d S )Nr  )adfsw)r   r   r   sw_aram)r>   r   s    r   r   zCardApplicationARAM.__init__  s,    hjjW=====r   c                 \    |d S |D ]$}| |v r|                     |           }| dS |c S %d S )N )get)keydictlistr   objs       r   __export_get_from_dictlistz.CardApplicationARAM.__export_get_from_dictlist  sW    
 4 	 	AaxxeeCjj;22


	 
 tr   c                    d}t                               d|                     d                    }t                               d|                     d                    }|ru|rrt                               d|          }t                               d|          }t                               d|          }t                               d|          }t                               d	|          }t                               d
|          }	t                               d|          }
|dz  }|t          |          dk    r	|d|z  z  }n||dz  }||dz  }|r|d|z  z  }|rd|v r|d|d         z  z  }n,|r*d|v r&|dz  }|d         D ]}||d         z  }||d         z  }|	rd|	v r|d|	d         z  z  }|
r|d|
d         z  z  }|r|d|d         z  z  }|dz  }|S )Nr,  r   r   r   r   r   r   r   r   r   r   aram_store_ref_ar_dor   z	 --aid %sz	 --aid ""z --aid-emptyz --device-app-id %sr0   z
 --apdu-%sr5   z --apdu-filter r8   r9   rO   z	 --nfc-%sz --android-permissions %srS   z --pkg-ref %sr$   
)r&  ._CardApplicationARAM__export_get_from_dictlistr-  r:   )ref_ar_do_list
export_strref_do_list
ar_do_listr   r   r   r   r   r   r   r5   s               r   __export_ref_ar_do_listz+CardApplicationARAM.__export_ref_ar_do_list  s\   
)DDX~OaOabmOnOnoo(CCG^M_M_`kMlMlmm
 #	: #	,GGVabbJ2MMN`bmnn 3 N NObdo p p,GGVabbJ -GGV`aaJ+FF{T^__I,GGV`aaJ 00J%#j//A*=*={Z78

'm+
+n,
  J47HHI
 63zAA|j9N.OOP

 6 ; ;01
#-m#< 6 6K+h"77J+f"55JJ Q4	AA{Y7N-OOP
 X:Z=VVW
 T<Q1RRS
$Jr   as_jsonc                 p   | rt          d          d}|dz  }t                              |j                  }|s|                                S |                                D ]A}|                    dd          s|d         D ]}|t                              |          z  } B|                                S )Nz4res_do encoder not yet implemented. Patches welcome.r,  zaram_delete_all
response_all_ref_ar_doF)	NotImplementedErrorr   r   r   stripr   r-  r&  +_CardApplicationARAM__export_ref_ar_do_list)r;  r   r7  r   res_do_dictr6  s         r   exportzCardApplicationARAM.export  s      	^%&\]]]
))
!!%),, 	&##%%%!>>++ 	Z 	ZK??#;UCC "-.F"G Z Z1II.YYY

Z !!!r   )
r   r   r   r   r  r5  r@  boolrB  r  r  s   @r   r&  r&    s        > > > > >   \ ) ) \)V " " " " \" " " " "r   r&  )8__doc__	constructr   r   r   r   r   r   	COptionalosmocom.constructosmocom.tlvosmocom.utilsr
   pySim.filesystempySim.global_platformr   
BER_TLV_IEr   r   r    r#   r(   r,   rN   rR   rX   r[   r^   r`   rb   re   rk   rn   rp   rs   ru   rx   r{   r~   r   r   r   r   r   r   r   TLV_IE_Collectionr   r   r   r   CardADFr   r*  CardApplicationr&  r   r   r   <module>rP     sO   , B A A A A A A A A A A A A A + + + + + +                            
) ) ) ) )zt ) ) ) )
	 	 	 	 	JD 	 	 	 	
) ) ) ) )JD ) ) ) )
E E E E Ezt E E E E
	 	 	 	 	JD(M=RZ)[ 	 	 	 	
* * * * *zt * * * *Z9 9 9 9 9jd 9 9 9 9< < < < <zt < < < <	 	 	 	 	:47H(E 	 	 	 	
	 	 	 	 	jdE4= 	 	 	 	
	 	 	 	 		 	 	 	 	
	 	 	 	 	:64& 	 	 	 	
< < < < <:6 < < < <
H H H H Hzt H H H H
	 	 	 	 	ZT3K2L 	 	 	 	
	 	 	 	 	ZV^<L 	 	 	 	
	 	 	 	 	:41I0J 	 	 	 	
	 	 	 	 	:6<. 	 	 	 	
	 	 	 	 	*$y 	 	 	 	
	 	 	 	 	JD(M5RY1Z 	 	 	 	
	 	 	 	 	
 	 	 	 	
	 	 	 	 	*$-?X 	 	 	 	
	 	 	 	 	x.G 	 	 	 	
	 	 	 	 	JD 	 	 	 	
	 	 	 	 	ZT 	 	 	 	
	 	 	 	 	ZT 	 	 	 	
	 	 	 	 	zt 	 	 	 	
	 	 	 	 	
-7P 	 	 	 	
; ; ; ; ;jd ; ; ; ;	 	 	 	 	.~7N 	 	 	 	
	 	 	 	 	/9K\9MOc9e 	 	 	 		 	 	 	 	0'.0C]'@B]'1=BX'57O'Q	 	 	 		 	 	 	 	 1(:<MOe'f	 	 	 	
NJ NJ NJ NJ NJw NJ NJ NJf La $8/Z*6@!+N% *S" S" S" S" S"/ S" S" S" S" S"r   