def test_listen_for_dep_target(self, clf): target = nfc.clf.LocalTarget('106A') target.atr_req = HEX('D400 30313233343536373839 00000000') target.atr_res = HEX('D501 66f6e98d1c13dfe56de4 0000000700') clf.device.listen_dep.return_value = target assert clf.listen(target, 1.0) is target target.atr_req = HEX('D400 30313233343536373839 000000') assert clf.listen(target, 1.0) is None target.atr_req = None assert clf.listen(target, 1.0) is None
def listen_dep(timeout, clf, args): try: id3 = bytearray.fromhex(args['--id3'][0:20]) except ValueError: assert 0, "the '--id3' argument must be hexadecimal" id3 += os.urandom(10 - len(id3)) try: gbt = bytearray.fromhex(args['--gbt']) except ValueError: assert 0, "the '--gbt' argument must be hexadecimal" target = nfc.clf.LocalTarget() target.sensf_res = bytearray.fromhex("01") + id3[0:8] + bytearray(10) target.sens_res = bytearray.fromhex("0101") target.sdd_res = bytearray.fromhex("08") + id3[-3:] target.sel_res = bytearray.fromhex("60" if args['--hce'] else "40") target.atr_res = b"\xD5\x01" + id3 + b"\0\0\0\x08" + ( b"\x32" if gbt else b"\0") + gbt target = clf.listen(target, timeout) if target and target.dep_req: logging.debug("rcvd DEP_REQ %s", hexlify(target.dep_req).decode()) # Verify that we can indeed send a response. Note that we do # not handle a DID, but nobody is sending them anyway. Further # note that target.dep_req is without the frame length byte # but exchange() works on frames and so it has to be added. if target.dep_req.startswith(b"\xD4\x06\x80"): # older phones start with attention dep_res = bytearray.fromhex("04 D5 07 80") elif target.dep_req.startswith(b"\xD4\x06\x00"): # newer phones send information packet dep_res = bytearray.fromhex("06 D5 07 00 00 00") else: logging.warning("communication not verified") return target logging.debug("send DEP_RES %s", hexlify(memoryview(dep_res)[1:]).decode()) try: data = clf.exchange(dep_res, timeout=1) assert data and data[0] == len(data) except (nfc.clf.CommunicationError, AssertionError): logging.error("communication failure after activation") return None logging.debug("rcvd DEP_REQ %s", hexlify(memoryview(data)[1:]).decode()) mode = "passive" if target.sens_res or target.sensf_res else "active" logging.debug("activated in %s communication mode", mode) return target
def listen_ttf(timeout, clf, args): try: bitrate = (int(args['--bitrate']) if args['--bitrate'] else 212) except ValueError: assert 0, "the '--bitrate' argument must be an integer" assert bitrate >= 0, "the '--bitrate' argument must be a positive integer" try: idm = bytearray.fromhex(args['--idm'][0:16]) except ValueError: assert 0, "the '--idm' argument must be hexadecimal" idm += os.urandom(8 - len(idm)) try: pmm = bytearray.fromhex(args['--pmm'][0:16]) except ValueError: assert 0, "the '--pmm' argument must be hexadecimal" pmm += (8 - len(pmm)) * b"\xFF" try: _sys = bytearray.fromhex(args['--sys'][0:4]) except ValueError: assert 0, "the '--sys' argument must be hexadecimal" _sys += (2 - len(_sys)) * b"\xFF" target = nfc.clf.LocalTarget(str(bitrate) + 'F') target.sensf_res = b"\x01" + idm + pmm + _sys target = clf.listen(target, timeout) if target and target.tt3_cmd: if target.tt3_cmd[0] == 0x06: response = struct.pack("B", 29) + b"\7" + idm + b"\0\0\1" + \ bytearray(16) clf.exchange(response, timeout=0) elif target.tt3_cmd[0] == 0x0C: response = struct.pack("B", 13) + b"\x0D" + idm + b"\x01" + _sys else: logging.warning("communication not verified") return target try: clf.exchange(response, timeout=1) return target except nfc.clf.CommunicationError: logging.error("communication failure after activation")
def listen_dep(timeout, clf, args): try: id3 = bytearray.fromhex(args['--id3'][0:20]) except ValueError: assert 0, "the '--id3' argument must be hexadecimal" id3 += os.urandom(10-len(id3)) try: gbt = bytearray.fromhex(args['--gbt']) except ValueError: assert 0, "the '--gbt' argument must be hexadecimal" target = nfc.clf.LocalTarget() target.sensf_res = bytearray.fromhex("01") + id3[0:8] + bytearray(10) target.sens_res = bytearray.fromhex("0101") target.sdd_res = bytearray.fromhex("08") + id3[-3:] target.sel_res = bytearray.fromhex("60" if args['--hce'] else "40") target.atr_res = "\xD5\x01"+id3+"\0\0\0\x08"+("\x32" if gbt else "\0")+gbt target = clf.listen(target, timeout) if target and target.dep_req: logging.debug("rcvd DEP_REQ %s", hexlify(target.dep_req)) # Verify that we can indeed send a response. Note that we do # not handle a DID, but nobody is sending them anyway. Further # note that target.dep_req is without the frame length byte # but exchange() works on frames and so it has to be added. if target.dep_req.startswith("\xD4\x06\x80"): # older phones start with attention dep_res = bytearray.fromhex("04 D5 07 80") elif target.dep_req.startswith("\xD4\x06\x00"): # newer phones send information packet dep_res = bytearray.fromhex("06 D5 07 00 00 00") else: logging.warning("communication not verified") return target logging.debug("send DEP_RES %s", hexlify(buffer(dep_res, 1))) try: data = clf.exchange(dep_res, timeout=1) assert data and data[0]==len(data) except (nfc.clf.CommunicationError, AssertionError): logging.error("communication failure after activation") return None logging.debug("rcvd DEP_REQ %s", hexlify(buffer(data, 1))) mode = "passive" if target.sens_res or target.sensf_res else "active" logging.debug("activated in %s communication mode", mode) return target
def listen_tta(timeout, clf, args): try: bitrate = (int(args['--bitrate']) if args['--bitrate'] else 106) except ValueError: assert 0, "the '--bitrate' argument must be an integer" assert bitrate >= 0, "the '--bitrate' argument must be a positive integer" try: uid = bytearray.fromhex(args['--uid']) except ValueError: assert 0, "the '--uid' argument must be hexadecimal" assert len(uid) in (4, 7, 10), "the '--uid' must be 4, 7, or 10 bytes" target = nfc.clf.LocalTarget(str(bitrate) + 'A') target.sens_res = bytearray(b"\x01\x01") target.sdd_res = uid target.sel_res = bytearray(b"\x00" if args['tt2'] else b"\x20") target = clf.listen(target, timeout) if target and target.tt2_cmd: logging.debug("rcvd TT2_CMD %s", hexlify(target.tt2_cmd).decode()) # Verify that we can send a response. if target.tt2_cmd == b"\x30\x00": data = bytearray.fromhex("046FD536 11127A00 79C80000 E110060F") elif target.tt2_cmd[0] == 0x30: data = bytearray(16) else: logging.warning("communication not verified") return target try: clf.exchange(data, timeout=1) return target except nfc.clf.CommunicationError: logging.error("communication failure after activation") if target and target.tt4_cmd: logging.debug("rcvd TT4_CMD %s", hexlify(target.tt4_cmd).decode()) logging.warning("communication not verified") return target
def listen_tta(timeout, clf, args): try: bitrate = (int(args['--bitrate']) if args['--bitrate'] else 106) except ValueError: assert 0, "the '--bitrate' argument must be an integer" assert bitrate >= 0, "the '--bitrate' argument must be a positive integer" try: uid = bytearray.fromhex(args['--uid']) except ValueError: assert 0, "the '--uid' argument must be hexadecimal" assert len(uid) in (4,7,10), "the '--uid' must be 4, 7, or 10 bytes" target = nfc.clf.LocalTarget(str(bitrate) + 'A') target.sens_res = bytearray("\x01\x01") target.sdd_res = uid target.sel_res = bytearray("\x00" if args['tt2'] else "\x20") target = clf.listen(target, timeout) if target and target.tt2_cmd: logging.debug("rcvd TT2_CMD %s", hexlify(target.tt2_cmd)) # Verify that we can send a response. if target.tt2_cmd == "\x30\x00": data = bytearray.fromhex("046FD536 11127A00 79C80000 E110060F") elif target.tt2_cmd[0] == 0x30: data = bytearray(16) else: logging.warning("communication not verified") return target try: clf.exchange(data, timeout=1) return target except nfc.clf.CommunicationError: logging.error("communication failure after activation") if target and target.tt4_cmd: logging.debug("rcvd TT4_CMD %s", hexlify(target.tt4_cmd)) logging.warning("communication not verified") return target
def listen_ttf(timeout, clf, args): try: bitrate = (int(args['--bitrate']) if args['--bitrate'] else 212) except ValueError: assert 0, "the '--bitrate' argument must be an integer" assert bitrate >= 0, "the '--bitrate' argument must be a positive integer" try: idm = bytearray.fromhex(args['--idm'][0:16]) except ValueError: assert 0, "the '--idm' argument must be hexadecimal" idm += os.urandom(8-len(idm)) try: pmm = bytearray.fromhex(args['--pmm'][0:16]) except ValueError: assert 0, "the '--pmm' argument must be hexadecimal" pmm += (8-len(pmm)) * "\xFF" try: sys = bytearray.fromhex(args['--sys'][0:4]) except ValueError: assert 0, "the '--sys' argument must be hexadecimal" sys += (2-len(sys)) * "\xFF" target = nfc.clf.LocalTarget(str(bitrate) + 'F') target.sensf_res = "\x01" + idm + pmm + sys target = clf.listen(target, timeout) if target and target.tt3_cmd: if target.tt3_cmd[0] == 0x06: response = chr(29) + "\7" + idm + "\0\0\1" + bytearray(16) clf.exchange(response, timeout=0) elif target.tt3_cmd[0] == 0x0C: response = chr(13) + "\x0D" + idm + "\x01" + sys else: logging.warning("communication not verified") return target try: clf.exchange(response, timeout=1) return target except nfc.clf.CommunicationError: logging.error("communication failure after activation")
def test_listen_for_xxx_target(self, clf): target = nfc.clf.LocalTarget('xxx') with pytest.raises(ValueError) as excinfo: clf.listen(target, 1.0) assert str(excinfo.value) == "unsupported bitrate technology type xxx"
def test_listen_for_ttf_target(self, clf): target = nfc.clf.LocalTarget('212F') clf.device.listen_ttf.return_value = target assert clf.listen(target, 1.0) is target
def test_listen_without_device(self, clf): clf.device = None with pytest.raises(IOError) as excinfo: clf.listen(nfc.clf.LocalTarget('106A'), 1.0) assert excinfo.value.errno == errno.ENODEV