Exemple #1
0
def init_card(sl):
    """
	Detect card in reader and setup card profile and runtime state. This
	function must be called at least once on startup. The card and runtime
	state object (rs) is required for all pySim-shell commands.
	"""

    # Wait up to three seconds for a card in reader and try to detect
    # the card type.
    print("Waiting for card...")
    try:
        sl.wait_for_card(3)
    except NoCardError:
        print("No card detected!")
        return None, None
    except:
        print("Card not readable!")
        return None, None

    card = card_detect("auto", scc)
    if card is None:
        print("Could not detect card type!")
        return None, None

    # Create runtime state with card profile
    profile = CardProfileUICC()
    profile.add_application(CardApplicationUSIM())
    profile.add_application(CardApplicationISIM())
    rs = RuntimeState(card, profile)

    # FIXME: do this dynamically
    rs.mf.add_file(DF_TELECOM())
    rs.mf.add_file(DF_CUSTOM())
    rs.mf.add_file(DF_EIRENE())

    CardModel.apply_matching_models(scc, rs)

    # inform the transport that we can do context-specific SW interpretation
    sl.set_sw_interpreter(rs)

    return rs, card
Exemple #2
0
def process_card(opts, first, card_handler):

    if opts.dry_run is False:
        # Connect transport
        card_handler.get(first)

    if opts.dry_run is False:
        # Get card
        card = card_detect(opts.type, scc)
        if card is None:
            print("No card detected!")
            return -1

        # Probe only
        if opts.probe:
            return 0

        # Erase if requested
        if opts.erase:
            print("Formatting ...")
            card.erase()
            card.reset()

    # Generate parameters
    if opts.source == 'cmdline':
        cp = gen_parameters(opts)
    elif opts.source == 'csv':
        imsi = None
        iccid = None
        if opts.read_iccid:
            if opts.dry_run:
                # Connect transport
                card_handler.get(False)
            (res, _) = scc.read_binary(['3f00', '2fe2'], length=10)
            iccid = dec_iccid(res)
        elif opts.read_imsi:
            if opts.dry_run:
                # Connect transport
                card_handler.get(False)
            (res, _) = scc.read_binary(EF['IMSI'])
            imsi = swap_nibbles(res)[3:]
        else:
            imsi = opts.imsi
        cp = read_params_csv(opts, imsi=imsi, iccid=iccid)
    if cp is None:
        print("Error reading parameters from CSV file!\n")
        return 2
    print_parameters(cp)

    if opts.dry_run is False:
        # Program the card
        print("Programming ...")
        card.program(cp)
    else:
        print("Dry Run: NOT PROGRAMMING!")

    # Write parameters permanently
    write_parameters(opts, cp)

    # Batch mode state update and save
    if opts.num is not None:
        opts.num += 1
    save_batch(opts)

    card_handler.done()
    return 0
Exemple #3
0
    scc.cla_byte = "00"
    scc.sel_ctrl = "0004"

    # Testing for Classic SIM or UICC
    (res,
     sw) = sl.send_apdu(scc.cla_byte + "a4" + scc.sel_ctrl + "02" + "3f00")
    if sw == '6e00':
        # Just a Classic SIM
        scc.cla_byte = "a0"
        scc.sel_ctrl = "0000"

    # Program the card
    print("Reading ...")

    # Initialize Card object by auto detecting the card
    card = card_detect("auto", scc) or Card(scc)

    # Read all AIDs on the UICC
    card.read_aids()

    # EF.ICCID
    (res, sw) = card.read_iccid()
    if sw == '9000':
        print("ICCID: %s" % (res, ))
    else:
        print("ICCID: Can't read, response code = %s" % (sw, ))

    # EF.IMSI
    (res, sw) = card.read_imsi()
    if sw == '9000':
        print("IMSI: %s" % (res, ))
Exemple #4
0
    # Parse options
    opts = parse_options()

    # Init card reader driver
    sl = init_reader(opts)
    if (sl == None):
        exit(1)

    # Create command layer
    scc = SimCardCommands(transport=sl)

    sl.wait_for_card()

    card_handler = card_handler(sl)

    card = card_detect("auto", scc)
    if card is None:
        print("No card detected!")
        sys.exit(2)

    profile = CardProfileUICC()
    profile.add_application(ADF_USIM())
    profile.add_application(ADF_ISIM())

    rs = RuntimeState(card, profile)

    # FIXME: do this dynamically
    rs.mf.add_file(DF_TELECOM())
    rs.mf.add_file(DF_GSM())

    app = PysimApp(card, rs, opts.script)