def init_reader(opts): """ Init card reader driver """ try: if opts.pcsc_dev is not None: print("Using PC/SC reader interface") from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(opts.pcsc_dev) elif opts.osmocon_sock is not None: print("Using Calypso-based (OsmocomBB) reader interface") from pySim.transport.calypso import CalypsoSimLink sl = CalypsoSimLink(sock_path=opts.osmocon_sock) elif opts.modem_dev is not None: print("Using modem for Generic SIM Access (3GPP TS 27.007)") from pySim.transport.modem_atcmd import ModemATCommandLink sl = ModemATCommandLink(device=opts.modem_dev, baudrate=opts.modem_baud) else: # Serial reader is default print("Using serial reader interface") from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) return sl except Exception as e: print("Card reader initialization failed with exception:\n" + str(e)) return None
def auth(request, slot): """REST API endpoint for performing authentication against a USIM. Expects a JSON body containing RAND and AUTN. Returns a JSON body containing RES, CK, IK and Kc.""" try: # there are two hex-string JSON parameters in the body: rand and autn content = json.loads(request.content.read()) rand = content['rand'] autn = content['autn'] except: request.setResponseCode(400) return "Malformed Request" try: tp = PcscSimLink(slot, apdu_tracer=ApduPrintTracer()) tp.connect() except ReaderError: request.setResponseCode(404) return "Specified SIM Slot doesn't exist" except ProtocolError: request.setResponseCode(500) return "Error" except NoCardError: request.setResponseCode(410) return "No SIM card inserted in slot" scc = SimCardCommands(tp) card = UsimCard(scc) # this should be part of UsimCard, but FairewavesSIM breaks with that :/ scc.cla_byte = "00" scc.sel_ctrl = "0004" try: card.read_aids() card.select_adf_by_aid(adf='usim') res, sw = scc.authenticate(rand, autn) except SwMatchError as e: request.setResponseCode(500) return "Communication Error %s" % e tp.disconnect() return json.dumps(res, indent=4)
parser.add_argument('--tar') parser.add_argument('--dump-phonebook', action='store_true') parser.add_argument('--set-phonebook-entry', nargs=4) parser.add_argument('--kic', default='') parser.add_argument('--kid', default='') parser.add_argument('--ram-kic-id', default=1) parser.add_argument('--ram-kid-id', default=1) parser.add_argument('--smpp', action='store_true') parser.add_argument('--test', action='store_true') parser.add_argument('--aram-apdu', default='') args = parser.parse_args() if args.pcsc is not None: from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(args.pcsc) elif args.serialport is not None: from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=args.serialport, baudrate=9600) else: raise RuntimeError("Need to specify either --serialport or --pcsc") sc = SimCardCommands(sl) ac = AppLoaderCommands(sl) if not args.smpp: sl.wait_for_card(newcardonly=args.new_card_required) time.sleep(args.sleep_after_insertion) if not args.smpp: # Get the ICCID
return options if __name__ == '__main__': # Parse options opts = parse_options() # Connect to the card if opts.pcsc_dev is None: from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) else: from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(opts.pcsc_dev) # Create command layer scc = SimCardCommands(transport=sl) # Wait for SIM card sl.wait_for_card() # Program the card print("Reading ...") # EF.ICCID (res, sw) = scc.read_binary(['3f00', '2fe2']) if sw == '9000': print("ICCID: %s" % (dec_iccid(res), )) else:
if args: parser.error("Extraneous arguments") return options if __name__ == '__main__': # Parse options opts = parse_options() # Init card reader driver if opts.pcsc_dev is not None: print("Using PC/SC reader (dev=%d) interface" % opts.pcsc_dev) from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(opts.pcsc_dev) elif opts.osmocon_sock is not None: print("Using Calypso-based (OsmocomBB, sock=%s) reader interface" % opts.osmocon_sock) from pySim.transport.calypso import CalypsoSimLink sl = CalypsoSimLink(sock_path=opts.osmocon_sock) else: # Serial reader is default print("Using serial reader (port=%s, baudrate=%d) interface" % (opts.device, opts.baudrate)) from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) # Create command layer scc = SimCardCommands(transport=sl) # Wait for SIM card
raise ValueError("Unknown card type: %s" % opts.type) return card if __name__ == '__main__': # Parse options opts = parse_options() # Init card reader driver if opts.pcsc_dev is not None: print("Using PC/SC reader (dev=%d) interface" % opts.pcsc_dev) from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(opts.pcsc_dev) elif opts.osmocon_sock is not None: print("Using Calypso-based (OsmocomBB, sock=%s) reader interface" % opts.osmocon_sock) from pySim.transport.calypso import CalypsoSimLink sl = CalypsoSimLink(sock_path=opts.osmocon_sock) else: # Serial reader is default print("Using serial reader (port=%s, baudrate=%d) interface" % (opts.device, opts.baudrate)) from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate) # Create command layer scc = SimCardCommands(transport=sl) # Batch mode init
sl = DummySL() pass ac = AppLoaderCommands(transport=sl) loadfile = ac.generate_load_file(args.show_cap_aid) aid = ac.get_aid_from_load_file(loadfile) print 'CAP file %s has AID %s' % (args.show_cap_aid, aid) exit(0) if args.pcsc is not None: from pySim.transport.pcsc import PcscSimLink # print (args.pcsc) # print (args.print_apdus) sl = PcscSimLink(args.pcsc) elif args.serialport is not None: from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=args.serialport, baudrate=9600) elif args.smpp is not None: class DummySL: pass sl = DummySL() pass else: raise RuntimeError("Need to specify either --serialport, --pcsc or --smpp") sc = SimCardCommands(transport=sl) ac = AppLoaderCommands(transport=sl,cla=args.cla,msl1=args.msl1,msl2=args.msl2,keyset=args.keyset,apdu_counter=args.counter) if not args.smpp:
# Send complete APDU response = self._tp.send_apdu_checksw(envelopeData) return response parser = argparse.ArgumentParser(description='Tries to get a DES MAC from OTA response') parser.add_argument('-s', '--serialport') parser.add_argument('-p', '--pcsc', nargs='?', const=0, type=int) args = parser.parse_args() if args.pcsc is not None: from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(args.pcsc) elif args.serialport is not None: from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=args.serialport, baudrate=9600) else: raise RuntimeError("Need to specify either --serialport or --pcsc") sc = SimCardCommands(sl) ac = MessageCommands(sl) sl.wait_for_card(0) print "ICCID: " + swap_nibbles(sc.read_binary(['3f00', '2fe2'])[0]) ac.send_terminal_profile() # Send OTA SMS-PP Download messages (with incrementing CNTR)
'A000000476416E64726F69644354534C', 'A000000476416E64726F69644354534D', 'A000000476416E64726F69644354534E', 'A000000476416E64726F69644354534F', ] for aid in selectable_aids: self.testTransmitApdu(aid) self.testLongSelectResponse(aid) self.testSegmentedResponseTransmit(aid) self.testStatusWordTransmit(aid) self.testP2Value(aid) parser = argparse.ArgumentParser(description='Android Secure Element CTS') parser.add_argument('-p', '--pcsc', nargs='?', const=0, type=int) args = parser.parse_args() transport = None if args.pcsc is not None: transport = PcscSimLink(args.pcsc) else: transport = PcscSimLink() commandif = CommandInterface(transport) transport.wait_for_card() commandif.send_terminal_profile() omapi = OmapiTest(commandif) omapi.execute_all()
parser.add_argument('-z', '--sleep_after_insertion', type=float, default=0.0) parser.add_argument('--disable-pin') parser.add_argument('--pin') parser.add_argument('--tar') parser.add_argument('--dump-phonebook', action='store_true') parser.add_argument('--set-phonebook-entry', nargs=4) parser.add_argument('--record', action="store_true") parser.add_argument('--print') parser.add_argument('--sqlite-db', nargs=1) args = parser.parse_args() if args.pcsc is not None: from pySim.transport.pcsc import PcscSimLink sl = PcscSimLink(args.pcsc) elif args.serialport is not None: from pySim.transport.serial import SerialSimLink sl = SerialSimLink(device=args.serialport, baudrate=9600) else: raise RuntimeError("Need to specify either --serialport or --pcsc") sc = SimCardCommands(sl) sl.wait_for_card(newcardonly=args.new_card_required) time.sleep(args.sleep_after_insertion) # Get the ICCID print("ICCID: %s" % swap_nibbles(sc.read_binary(['3f00', '2fe2'])[0])) if args.pin: