*g# dZddlmZmZmZddlmZddlmZm Z ddl Z ddl Z gZ gdgdgdgd gd d Z Gd d e jZGddeZe fdefdZe fdededeeeffdZe fdedededeefdZdS)aObtaining card parameters (mostly key data) from external source. This module contains a base class and a concrete implementation of obtaining card key material (or other card-individual parameters) from an external data source. This is used e.g. to keep PIN/PUK data in some file on disk, avoiding the need of manually entering the related card-individual data on every operation with pySim-shell. )ListDictOptional)AES)h2bb2hN)UICC_SCP02_KIC1UICC_SCP02_KID1UICC_SCP02_KIK1)UICC_SCP03_KIC1UICC_SCP03_KID1UICC_SCP03_KIK1)SCP03_ENC_ISDRSCP03_MAC_ISDRSCP03_DEK_ISDR)rSCP03_MAC_ISDASCP03_DEK_ISDA)SCP03_ENC_ECASDSCP03_MAC_ECASDSCP03_DEK_ECASD) UICC_SCP02 UICC_SCP03 SCP03_ISDR SCP03_ISDA SCP03_ECASDc eZdZdZgdZgddfdeedededeeeffd Zdd ededede efd Z e j deedededeeeffd Z d S)CardKeyProviderz7Base class, not containing any concrete implementation.)ICCIDEIDIMSIrfieldskeyvaluereturncf||jvr'td|dt|jiS)aVerify multiple fields for identified card. Args: fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained key : look-up key to identify card data, such as 'ICCID' value : value for look-up key to identify card data Returns: dictionary of {field, value} strings for each requested field from 'fields' zKey field name 'z4' is not a valid field name, valid field names are: )VALID_KEY_FIELD_NAMES ValueErrorstrselfr"r#r$s E/home/jenkins/workspace/simtester-sanitize/pySim/card_key_provider.py_verify_get_dataz CardKeyProvider._verify_get_data9sM d0 0 0*!cc3t'A#B#B#BDEE E fieldc`|g}||||}||S)zAget a single field from CSV file using a specified key/value pair)get)r+r/r#r$r"results r, get_fieldzCardKeyProvider.get_fieldJs0&#u--zz%   r.cdS)aGet multiple card-individual fields for identified card. Args: fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained key : look-up key to identify card data, such as 'ICCID' value : value for look-up key to identify card data Returns: dictionary of {field, value} strings for each requested field from 'fields' Nr*s r,r1zCardKeyProvider.getPsr.N)rr!)__name__ __module__ __qualname____doc__r'rr)rr-rr3abcabstractmethodr1r5r.r,rr3sAA55546'XZtCyCRU_cdgildl_m"!!s!!s!HUXM!!!!    $s)  #  c  d38n        r.rc eZdZdZdZdZdZdedefdZ e defdZ ded ed efd Z d e ed eded eeefffd ZxZS)CardKeyProviderCsvaWCard key provider implementation that allows to query against a specified CSV file. Supports column-based encryption as it is generally a bad idea to store cryptographic key material in plaintext. Instead, the key material should be encrypted by a "key-encryption key", occasionally also known as "transport key" (see GSMA FS.28).s################Nfilenametransport_keysct|d|_|jstd|z||_|||_dS)a Args: filename : file name (path) of CSV file containing card-individual key/data transport_keys : a dict indexed by field name, whose values are hex-encoded AES keys for the respective field (column) of the CSV. This is done so that different fields (columns) can use different transport keys, which is strongly recommended by GSMA FS.28 rzCould not open CSV file '%s'N)opencsv_file RuntimeErrorr>process_transport_keysr?)r+r>r?s r,__init__zCardKeyProviderCsv.__init__fsWXs++ } J=HII I  "99.IIr.ci}|D])\}}|tvrt|D]}|||<$|||<*|S)zPApply a single transport key to multiple fields/columns, if the name is a group.)items CRYPT_GROUPS)r?new_dictnamer#r/s r,rEz)CardKeyProviderCsv.process_transport_keysusi'--// % %ID#|##)$/**E&)HUOO*"%r. field_name encrypted_valr%c||jvr|Stjt|j|tj|j}t |t|S)zNdecrypt a single field, if we have a transport key for the field of that name.)r?rnewrMODE_CBCIVrdecrypt)r+rLrMciphers r,_decrypt_fieldz!CardKeyProviderCsv._decrypt_fields_T000 T0<==s|TWUU6>>#m"4"455666r.r"r#r$c t||||jdt j|j}|st d|jzd|jD|_i}|D]c}|||krU|D]R}||vr2| || |||i8t d|jd|dd|S)Nrz+Could not open DictReader for CSV-File '%s'c6g|]}|Sr5)upper).0r/s r, z*CardKeyProviderCsv.get..s BBB5BBBr.z CSV-File 'z' lacks column '') superr-rCseekcsv DictReaderrDr> fieldnamesupdaterT) r+r"r#r$crrcrowf __class__s r,r1zCardKeyProviderCsv.gets   e444 1 ^DM * * O= MOO OBBBMBBB  ? ?C3x5  ??ACxx 1d&9&9!SV&D&D"EFFFF*l,0MMM111,>??? r.)r6r7r8r9rQrCr>r)dictrF staticmethodrErTrrr1 __classcell__)res@r,r=r=]s22 BHH J Jd J J J J t   \ 77S7S7777$s)#cd38nr.r=providercxt|tstd||dS)zRegister a new card key provider. Args: provider : the to-be-registered provider provider_list : override the list of providers from the global default z#provider is not a card data provierN) isinstancerr(append)ri provider_lists r,card_key_provider_registerrns@ h 0 0@>???"""""r.r#r$r%c|D]C}t|tstd||||}|r|cSDiS)aQuery all registered card data providers for card-individual [key] data. Args: fields : list of valid field names such as 'ADM1', 'PIN1', ... which are to be obtained key : look-up key to identify card data, such as 'ICCID' value : value for look-up key to identify card data provider_list : override the list of providers from the global default Returns: dictionary of {field, value} strings for each requested field from 'fields' ?provider list contains element which is not a card data provier)rkrr(r1)r"r#r$rmpr2s r,card_key_provider_getrrsn!_-- SQSS SvsE**  MMM  Ir.r/c|D]C}t|tstd||||}|r|cSDdS)aQuery all registered card data providers for a single field. Args: field : name valid field such as 'ADM1', 'PIN1', ... which is to be obtained key : look-up key to identify card data, such as 'ICCID' value : value for look-up key to identify card data provider_list : override the list of providers from the global default Returns: dictionary of {field, value} strings for the requested field rpN)rkrr(r3)r/r#r$rmrqr2s r,card_key_provider_get_fieldrtsn!_-- SQSS SUC//  MMM  4r.)r9typingrrrCryptodome.Cipherr osmocom.utilsrrr:r]card_key_providersrIABCrr=rnr)rrrtr5r.r,rzs  :('''''''''!!!!!!""""""""  LKKKKKHHHHHHLLL  ' ' ' ' ' cg' ' ' T>>>>>>>>BI[ # # # # # #GYs3]abegjbj]k*QcsSgopsgtr.