Example #1
0
    def test_SPI1531( self ):
        from formutil import findTLValue
        import CAT
        sp_par = { 'SPI': 0x1535,
                   'KIC': ( 0x15, unhexlify( '263B48DBCBC1C21C0AA13E4D4516C446' )),
                   'iKIDCRC': 0x15,
                   'TAR': unhexlify( 'C00000' ),
                   'counter': unhexlify( '0000000076' ) }
        apdu_payload = unhexlify( '80CA00E000' + '00C0000000' )
        sp = SecurePacket( **sp_par )
        data = unhexlify( 'D13A820283818B3444038119F07FF6151124135822042502700000201115351515C000004D5C2C7F43D3E774316C66CE8E1D5045755BACEF5DC83203' )
        tpdu = findTLValue( data, ( 0xD1, 0x8B ))
        sms = CAT.SMS_MT()
        sms.parseTPDU( tpdu )
        udh, secp = sms.mergeUserData()
        payload = sp.checkComPacket( secp )
        self.assertEqual( payload, apdu_payload )

        secp1 = sp.createComPacket( apdu_payload )
        self.assertEqual( secp, secp1 )

        # response
        rdata = unhexlify( 'D04781030113008202818305008B3A4100038119F000F631027100002C0EC00000888B39C361C66D5F3DC4EFCEF508CC228021DD34E577B147E54EFF18B3922C9176644E2E8AA9A695' )
        tpdu = CAT.TLV( 0x8B, findTLValue( rdata, ( 0xD0, 0x8B )))
        SCA = CAT.TLV( 0x05, findTLValue( rdata, ( 0xD0, 0x05 )))
        sms = CAT.SMS_MO()
        sms.addMessage( SCA, tpdu )
        udh, secp = sms.mergeMessages()
        rcs, payload = sp.checkRespPacket( udh + secp )
        self.assertEqual( rcs, 0 )
        self.assertEqual( payload, unhexlify( '019000E012C00401018210C00402018210C00403018210' ))
        scout_log = """
Example #2
0
    def test_SPI1531(self):
        from formutil import findTLValue
        import CAT
        sp_par = {
            'SPI': 0x1535,
            'KIC': (0x15, unhexlify('263B48DBCBC1C21C0AA13E4D4516C446')),
            'iKIDCRC': 0x15,
            'TAR': unhexlify('C00000'),
            'counter': unhexlify('0000000076')
        }
        apdu_payload = unhexlify('80CA00E000' + '00C0000000')
        sp = SecurePacket(**sp_par)
        data = unhexlify(
            'D13A820283818B3444038119F07FF6151124135822042502700000201115351515C000004D5C2C7F43D3E774316C66CE8E1D5045755BACEF5DC83203'
        )
        tpdu = findTLValue(data, (0xD1, 0x8B))
        sms = CAT.SMS_MT()
        sms.parseTPDU(tpdu)
        udh, secp = sms.mergeUserData()
        payload = sp.checkComPacket(secp)
        self.assertEqual(payload, apdu_payload)

        secp1 = sp.createComPacket(apdu_payload)
        self.assertEqual(secp, secp1)

        # response
        rdata = unhexlify(
            'D04781030113008202818305008B3A4100038119F000F631027100002C0EC00000888B39C361C66D5F3DC4EFCEF508CC228021DD34E577B147E54EFF18B3922C9176644E2E8AA9A695'
        )
        tpdu = CAT.TLV(0x8B, findTLValue(rdata, (0xD0, 0x8B)))
        SCA = CAT.TLV(0x05, findTLValue(rdata, (0xD0, 0x05)))
        sms = CAT.SMS_MO()
        sms.addMessage(SCA, tpdu)
        udh, secp = sms.mergeMessages()
        rcs, payload = sp.checkRespPacket(udh + secp)
        self.assertEqual(rcs, 0)
        self.assertEqual(
            payload,
            unhexlify('019000E012C00401018210C00402018210C00403018210'))
Example #3
0
def getExtCardRes( c ):
    """ Issue GET DATA with tag FF21 in order to receive Extended
Card Resources (GP 2.2.1, 11.3 & ETSI TS 102.226, 8.2.1.7).
Returns [ num. of install applets, free NVM, free RAM ]"""
    # CLA = 0x00: return only value
    # CLA = 0x80: return TLV, i.e. 0xFF21 #( value )
    apdu = [ 0x80, INS_GETDATA, 0xFF, 0x21, 0 ] 
    resp, sw1, sw2 = c.transmit( apdu )
    if sw1 == 0x6C:
        apdu[4] = sw2
        resp, sw1, sw2 = c.transmit( apdu )
    sw = ( sw1 << 8 ) + sw2
    if sw != 0x9000: raise ISOException( sw )
    payload = l2s( resp )
    result = [ s2int( findTLValue( payload, ( 0xFF21, tag ))) for
               tag in ( 0x81, 0x82, 0x83 )]
    return result
Example #4
0
def getExtCardRes(c):
    """ Issue GET DATA with tag FF21 in order to receive Extended
Card Resources (GP 2.2.1, 11.3 & ETSI TS 102.226, 8.2.1.7).
Returns [ num. of install applets, free NVM, free RAM ]"""
    # CLA = 0x00: return only value
    # CLA = 0x80: return TLV, i.e. 0xFF21 #( value )
    apdu = [0x80, INS_GETDATA, 0xFF, 0x21, 0]
    resp, sw1, sw2 = c.transmit(apdu)
    if sw1 == 0x6C:
        apdu[4] = sw2
        resp, sw1, sw2 = c.transmit(apdu)
    sw = (sw1 << 8) + sw2
    if sw != 0x9000: raise ISOException(sw)
    payload = l2s(resp)
    result = [
        s2int(findTLValue(payload, (0xFF21, tag)))
        for tag in (0x81, 0x82, 0x83)
    ]
    return result
Example #5
0
def cardInfo( c ):
    """Deselect, read EF_DIR, EF_ICCID"""
    resetCard( c )
    histBytes = l2s( ATR( c.getATR()).getHistoricalBytes())
    infoMF = selectFile( c, '' )
    # read EF_ICCID
    infoICCID = selectFile( c, unhexlify( '2FE2' ))
    fileSize = s2int( findTLValue( infoICCID, ( 0x62, 0x80 )))
    assert fileSize == 10, "Wrong size of EF_ICCID"
    iccid = swapNibbles( readBinary( c, fileSize ))
    # read EF_DIR
    infoDIR = selectFile( c, unhexlify( '2F00' ))
    # see ETSI 102.221 11.1.1.4.3 for coding
    fileDesc = findTLValue( infoDIR, ( 0x62, 0x82 ))
    assert len( fileDesc ) == 5 and \
        fileDesc[:2] == '\x42\x21' # linear EF
    recLen, nRec = unpack( ">HB", fileDesc[2:5] )
    dirDO = []
    for recNum in xrange( 1, nRec+1 ):
        try:
            r = readRecord( c, recNum )
            if r == '\xFF'* len( r ): continue
            aid = findTLValue( r, ( 0x61, 0x4F ))
            label = findTLValue( r, ( 0x61, 0x50 ))
            dirDO.append( { 'AID': aid,
                            'label': label } )
        except ISOException:
            break
    # select USIM and try to read IMSI
    if len( dirDO ) == 1:
        aid_usim = dirDO[0]['AID']
    else:
        aids = [ DO['AID'] for DO in dirDO
                 if re.match( DO['label'], 'USIM' )]
        if len( aids ) == 1:
            aid_usim = aids[0]
        else:
            aid_usim = None
    if aid_usim:
        infoUSIM = selectApplet( c, aid_usim )
        infoIMSI = selectFile( c, unhexlify( '7FFF6F07' ))
        try:
            bimsi = readBinary( c, 9 )
            digits = reduce( lambda d, n: d + [ ord(n) & 0x0F, ord(n) >> 4 ],
                             bimsi[ 1:1+ord(bimsi[0])], [] )
            digits.pop(0)          # remove first nibble 8 or 9
            while digits[-1] == 0x0F: digits.pop() # remove trailing F
            imsi = ''.join( [ chr(ord('0')+i) for i in digits ])
        except ISOException:
            imsi = None
    else: imsi = None
    # select default applet and get tags 45 and 42
    selectApplet( c, '' )
    try:
        cin = findTLValue( getData( c, 0x42 ), ( 0x42, ))
    except ISOException:
        cin = None
    try:
        iin = findTLValue( getData( c, 0x45 ), ( 0x45, ))
    except ISOException:
        iin = None
    return histBytes, iccid, dirDO, imsi, iin, cin
Example #6
0
def cardInfo(c):
    """Deselect, read EF_DIR, EF_ICCID"""
    resetCard(c)
    histBytes = l2s(ATR(c.getATR()).getHistoricalBytes())
    infoMF = selectFile(c, '')
    # read EF_ICCID
    infoICCID = selectFile(c, unhexlify('2FE2'))
    fileSize = s2int(findTLValue(infoICCID, (0x62, 0x80)))
    assert fileSize == 10, "Wrong size of EF_ICCID"
    iccid = swapNibbles(readBinary(c, fileSize))
    # read EF_DIR
    infoDIR = selectFile(c, unhexlify('2F00'))
    # see ETSI 102.221 11.1.1.4.3 for coding
    fileDesc = findTLValue(infoDIR, (0x62, 0x82))
    assert len( fileDesc ) == 5 and \
        fileDesc[:2] == '\x42\x21' # linear EF
    recLen, nRec = unpack(">HB", fileDesc[2:5])
    dirDO = []
    for recNum in xrange(1, nRec + 1):
        try:
            r = readRecord(c, recNum)
            if r == '\xFF' * len(r): continue
            aid = findTLValue(r, (0x61, 0x4F))
            label = findTLValue(r, (0x61, 0x50))
            dirDO.append({'AID': aid, 'label': label})
        except ISOException:
            break
    # select USIM and try to read IMSI
    if len(dirDO) == 1:
        aid_usim = dirDO[0]['AID']
    else:
        aids = [DO['AID'] for DO in dirDO if re.match(DO['label'], 'USIM')]
        if len(aids) == 1:
            aid_usim = aids[0]
        else:
            aid_usim = None
    if aid_usim:
        infoUSIM = selectApplet(c, aid_usim)
        infoIMSI = selectFile(c, unhexlify('7FFF6F07'))
        try:
            bimsi = readBinary(c, 9)
            digits = reduce(
                lambda d, n: d + [ord(n) & 0x0F, ord(n) >> 4],
                bimsi[1:1 + ord(bimsi[0])], [])
            digits.pop(0)  # remove first nibble 8 or 9
            while digits[-1] == 0x0F:
                digits.pop()  # remove trailing F
            imsi = ''.join([chr(ord('0') + i) for i in digits])
        except ISOException:
            imsi = None
    else:
        imsi = None
    # select default applet and get tags 45 and 42
    selectApplet(c, '')
    try:
        cin = findTLValue(getData(c, 0x42), (0x42, ))
    except ISOException:
        cin = None
    try:
        iin = findTLValue(getData(c, 0x45), (0x45, ))
    except ISOException:
        iin = None
    return histBytes, iccid, dirDO, imsi, iin, cin