void TagInit(ReaderTag *rt){ nfc_init(&rt->context); if (rt->context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device rt->device = nfc_open(rt->context, NULL); if (rt->device == NULL) { ERR("Error opening NFC device"); nfc_exit(rt->context); exit(EXIT_FAILURE); } if (nfc_initiator_init(rt->device) < 0) { nfc_perror(rt->device, "nfc_initiator_init"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Scan until the cows come home, or the tag, which ever comes first. if (nfc_device_set_property_bool(rt->device, NP_INFINITE_SELECT, true) < 0) { nfc_perror(rt->device, "nfc_device_set_property_bool"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(rt->device)); printf("\nWaiting for tag\n"); // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(rt->device, nmMifare, NULL, 0, &rt->nt) <= 0) { ERR("no tag was found\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (rt->nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Found MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < rt->nt.nti.nai.szUidLen; szPos++) { printf("%02x", rt->nt.nti.nai.abtUid[szPos]); } printf("\n"); }
void cut_teardown(void) { nfc_close(second_device); nfc_close(first_device); nfc_exit(context); }
static int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { // ISO14443-4 card, turn RF field off/on to access ISO14443-3 again if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } } // Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { printf("Error: tag disappeared\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } return res; }
void cut_teardown(void) { nfc_close(devices[TARGET]); nfc_close(devices[INITIATOR]); nfc_exit(NULL); }
int mf_disconnect(int ret_state) { nfc_close(device); nfc_exit(context); device = NULL; memset(&target, 0, sizeof(target)); return ret_state; }
int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; //! Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { //! ISO14443-4 card, turn RF field off/on to access ISO14443-3 again nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true); } //! Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { sprintf(message_erreur,"Error: tag disappeared !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } return res; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { warnx("ERROR: %s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) ISO14443A tag was found:\n"); printf(" ATQA (SENS_RES): "); print_hex(nt.nti.nai.abtAtqa, 2); printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf(" SAK (SEL_RES): "); print_hex(&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf(" ATS (ATR): "); print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); return EXIT_SUCCESS; }
static void stop_emulation(int sig) { (void) sig; if (pnd != NULL) { nfc_abort_command(pnd); } else { nfc_exit(context); exit(EXIT_FAILURE); } }
static void stop_polling(int sig) { (void) sig; if (pnd != NULL) nfc_abort_command(pnd); else { nfc_exit(context); exit(EXIT_FAILURE); } }
bool verification_libnfc(QLabel* label_conf_libnfc, QLabel* Message_utilisateur){ Message_utilisateur->setText("Detecting libNFC"); nfc_context* context; nfc_init(&context); bool res= verification_libnfc_context(&context,label_conf_libnfc, Message_utilisateur); nfc_exit(context); return res; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_context *context; nfc_init(&context); if (context == NULL) { printf("Unable to init libnfc (malloc)\n"); exit(EXIT_FAILURE); } const char *acLibnfcVersion = nfc_version(); (void)argc; printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC device."); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); for( ; ; ){ const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) ISO14443A tag was found:\n"); printf(" ATQA (SENS_RES): "); print_hex(nt.nti.nai.abtAtqa, 2); printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf(" SAK (SEL_RES): "); print_hex(&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf(" ATS (ATR): "); print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } sleep(3); } } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
static void stop_polling(int sig) { (void) sig; run = false; if (RT.device != NULL) nfc_abort_command(RT.device); else { nfc_exit(RT.context); exit(EXIT_FAILURE); } }
static void intr_hdlr(int sig) { (void) sig; if (pnd != NULL) { printf("\nAborting current command...\n"); nfc_abort_command(pnd); nfc_close(pnd); } nfc_exit(context); exit(EXIT_SUCCESS); }
bool nfosc_check() { nfc_context *test_context; nfc_connstring test_strings[MAX_DEVICE_COUNT]; nfc_init(&test_context); if (test_context == NULL) return false; size_t szFound = nfc_list_devices (test_context, test_strings, MAX_DEVICE_COUNT); nfc_exit(context); if (szFound) return true; else return false; }
int main(int argc, const char *argv[]) { system("clear"); nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device. pnd = nfc_open(context, NULL); if (pnd == NULL) { warnx("ERROR: %s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); printf("...\n", nfc_device_get_name(pnd)); while (true){ // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); } sleep(1); } // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); return EXIT_SUCCESS; }
void nfosc_stop() { int i; if (!running) return; running = false; if( main_thread ) pthread_detach(main_thread); main_thread = NULL; lo_bundle osc_bundle = lo_bundle_new(LO_TT_IMMEDIATE); for (i=0;i<buffer_size;i++) { lo_message del_message = lo_message_new(); lo_message_add_int32(del_message, tag_buffer[i].device_id); lo_message_add_int32(del_message, tag_buffer[i].symbol_id); lo_message_add_int32(del_message, tag_buffer[i].type_id); lo_message_add_string(del_message, tag_buffer[i].uid_str); lo_bundle_add_message(osc_bundle, "/nfosc/del", del_message); if (verbose) printf("del %d %d %d %s\n",tag_buffer[i].session_id,tag_buffer[i].symbol_id,tag_buffer[i].type_id,tag_buffer[i].uid_str); } lo_timetag frame_time; lo_timetag_now (&frame_time); for (int dev=0;dev<no_devices;dev++) { if (pnd[dev]==NULL) continue; lo_message frm_message = lo_message_new(); lo_message_add_int32(frm_message, -1); lo_message_add_timetag(frm_message, frame_time); lo_message_add_int32(frm_message, 0); // sensor dim if (device_count>1) sprintf(source_string, "NFOSC:%d",dev); lo_message_add_string(frm_message, source_string); // source name lo_bundle_add_message(osc_bundle, "/tuio2/frm", frm_message); lo_message sid_message = lo_message_new(); lo_bundle_add_message(osc_bundle, "/tuio2/alv", sid_message); } int ret = lo_send_bundle(target, osc_bundle); if(ret == -1) { fprintf(stderr, "an OSC error occured: %s\n", lo_address_errstr(target)); exit(1); } for (int dev=0;dev<no_devices;dev++) { if (pnd[dev]!=NULL) { printf("closing NFC reader #%d: %s\n",dev,nfc_device_get_name(pnd[dev])); nfc_close(pnd[dev]); } } nfc_exit(context); write_database(); }
int main (int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_init (NULL); // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode if (nfc_initiator_init (pnd) < 0) { nfc_perror (pnd, "nfc_initiator_init"); exit (EXIT_FAILURE); } printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) > 0) { printf ("The following (NFC) ISO14443A tag was found:\n"); printf (" ATQA (SENS_RES): "); print_hex (nt.nti.nai.abtAtqa, 2); printf (" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex (nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf (" SAK (SEL_RES): "); print_hex (&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf (" ATS (ATR): "); print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } // Close NFC device nfc_close (pnd); nfc_exit (NULL); return EXIT_SUCCESS; }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); if (context == NULL) { exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); (void)argc; // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } // Poll for a ISO14443A (MIFARE) tag nfc_modulation nmMifare; nmMifare.nmt = NMT_ISO14443A; nmMifare.nbr = NBR_106; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, char** argv) { memset(&mtDump, 0x00, sizeof(mtDump)); Credentials crd; char cmd[200]; signal(SIGINT, stop_polling); nfc_init(&RT.context); if (RT.context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } TagInit(&RT); while(run){ if(ScanForTag(&RT)){ if(N_read_card(RT,&mtDump)){ if(NDDEF_DEGenerate(&crd, &mtDump)){ if(Credentials_Check(&crd)){ printf("\nGood\n"); memset(cmd, 0x00, sizeof(cmd)); //snprintf(cmd,200,"startx /usr/bin/nice -n 18 /usr/bin/rdesktop -a 24 -x 6 -P -r sound:local -u %s -p %s -f %s",crd.user,crd.password, crd.VM); //printf("%s",cmd); //system(cmd); printf("Waiting for card removing..."); sleep(1); while (0 == nfc_initiator_target_is_present(RT.device, NULL)) {} nfc_perror(RT.device, "nfc_initiator_target_is_present"); printf("done.\n"); } } } } //nfc_idle(RT.device); //nfc_exit(RT.context); sleep(1); } if (RT.device != NULL) nfc_abort_command(RT.device); else { nfc_exit(RT.context); exit(EXIT_SUCCESS); } }
int main(int argc, const char *argv[]) { pinMode(LED_R, OUTPUT); // Set PWM LED as PWM output pinMode(LED_G, OUTPUT); // Set regular LED as output pinMode(LED_B, OUTPUT); pinMode(DOOR, OUTPUT); //tür öffner // nfc initiiere nfc_init(&context); if (context == NULL) { printf("ERROR: fehler beim dev init\n"); exit(EXIT_FAILURE); } while (true) { doorCode = 0; // öffnet verbindung zum token pnd = nfc_open(context, NULL); led(1,1,0); //Gelb an if (pnd == NULL) { printf("ERROR: fehler beim öffnen.\n"); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { toTokenKey(nt.nti.nai.abtUid); printf("%s\n",tokenKey); checkToken(); } // TODO checken ob offen ist oder nicht um dann blau oder black zu zeigen nfc_close(pnd); } nfc_exit(context); exit(EXIT_SUCCESS); }
void cut_teardown (void) { if (tag) mifare_classic_disconnect (tag); if (tags) { freefare_free_tags (tags); tags = NULL; } if (device) nfc_close (device); nfc_exit(NULL); }
void cut_teardown (void) { if (tag) mifare_desfire_disconnect (tag); if (tags) { freefare_free_tags (tags); tags = NULL; } if (device) nfc_close (device); nfc_exit (context); }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; nfc_context *context; nfc_init(&context); if(context == NULL) { printf("unable to init (malloc)\n"); exit(EXIT_FAILURE); } pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC Device."); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; for(int i = 0; i < 24; i++) { if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) card was found:\n"); print_card(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); } while (0 == nfc_initiator_target_is_present(pnd, NULL)){} } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
void vNFCTask(void *vParameter){ vDebugString("vNFCTask started"); vTaskDelay(100); nfc_init(&context); pnd = nfc_open(context); nfc_close(pnd); nfc_exit(context); for(;;){ vTaskDelay(1000); } }
int main(int argc, char** argv) { // Handling Linux Signals if (signal(SIGHUP, signal_handler) == SIG_ERR) { perror("\nCan't catch SIGHUP\n"); } if (signal(SIGQUIT, signal_handler) == SIG_ERR) { perror("\nCan't catch SIGQUIT\n"); } if (signal(SIGUSR1, signal_handler) == SIG_ERR) { perror("\nCan't catch SIGUSR1\n"); } // supervisord will send this one if (signal(SIGTERM, signal_handler) == SIG_ERR) { perror("\nCan't catch SIGTERM\n"); } if (signal(SIGINT, signal_handler) == SIG_ERR) { perror("\nCan't catch SIGINT\n"); } // About GPIO & wiringPI Lib setupWiring(); while (1) { nfc_init(&context); if (context == NULL) { printf("Unable to init libnfc (malloc)\n"); exit(EXIT_FAILURE); } nfcInitListen(); nfcProtocol(); nfc_close(pnd); nfc_exit(context); sleep(2); printf("starting again...\n"); } return (EXIT_SUCCESS); }
bool verification_lecteur(QLabel* label_conf_lecteur,QLabel* label_conf_libnfc, QLabel* Message_utilisateur ){ nfc_context* context; nfc_device *pnd; nfc_init(&context); bool res= verification_libnfc_context(&context,label_conf_libnfc, Message_utilisateur); if (res == 0) { Message_utilisateur->setText("LibNFC necessaire pour detecter le lecteur !"); label_conf_lecteur->setText("Non détécté"); label_conf_lecteur->setStyleSheet("background-color: red;"); } else { res=verification_lecteur_context(&context, &pnd,label_conf_lecteur,Message_utilisateur ); if (res==1){ nfc_close(pnd); } } nfc_exit(context); return res; }
int main(int argc, const char *argv[]) { nfc_device *pnd; //nfc_target nt; MifareTag *tags = NULL; int i,j,k; int nbrsect=0; MifareClassicBlock data; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); if (context == NULL) { printf("Unable to init libnfc\n"); exit(EXIT_FAILURE); } // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC device."); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); tags = freefare_get_tags(pnd); if (!tags) { printf("no Mifare classic\n"); } else { for (i = 0; tags[i]; i++) { switch(freefare_get_tag_type(tags[i])) { case CLASSIC_1K: printf("%u : Mifare 1k (S50) : %s\n", i, freefare_get_tag_uid(tags[i])); nbrsect=16; break; case CLASSIC_4K: printf("%u : Mifare 4k (S70) : %s\n", i, freefare_get_tag_uid(tags[i])); nbrsect=40; break; default: printf("%u : other ISO14443A tag : %s\n", i, freefare_get_tag_uid(tags[i])); } } } if (!tags[0]) { printf("no tag found !\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf ("Found %s\n", freefare_get_tag_friendly_name (tags[0])); /* MifareClassicBlockNumber dablock = 1; if(mifare_classic_connect(tags[0]) == OPERATION_OK) { printf("Connected !\n"); if(mifare_classic_authenticate(tags[0], dablock, keys[0], MFC_KEY_A) == OPERATION_OK) { printf("Authenticated !\n"); if(mifare_classic_get_data_block_permission (tags[0], dablock, MCAB_R|MCAB_W, MFC_KEY_A)) printf("i can READ block %d with key A\n", dablock); if(mifare_classic_get_trailer_block_permission (tags[0], ((dablock)/4)*4+3, MCAB_READ_KEYB, MFC_KEY_B)) printf("i can READ KEY A in trailer\n"); } } */ for(i=0; i<nbrsect; i++) { for(j=0; j < sizeof(keys)/sizeof(keys[0]); j++) { if((mifare_classic_connect(tags[0]) == OPERATION_OK) && (mifare_classic_authenticate( tags[0], mifare_classic_sector_last_block(i), keys[j], MFC_KEY_A) == OPERATION_OK)) { printf("sector %02d auth with A[%d]\n", i, j); for(k=mifare_classic_sector_first_block(i); k<=mifare_classic_sector_last_block(i); k++) { if(mifare_classic_read(tags[0], k, &data) == OPERATION_OK) { print_hex(data,16); } else { printf("read error\n"); } } mifare_classic_disconnect(tags[0]); break; } mifare_classic_disconnect(tags[0]); } printf("\n"); } printf("\n"); freefare_free_tags(tags); // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, const char *argv[]) { (void) argc; (void) argv; nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open using the first available NFC device nfc_device *pnd; pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); // Print the example's menu printf("\nSelect the communication mode:\n"); printf("[1] Virtual card mode.\n"); printf("[2] Wired card mode.\n"); printf("[3] Dual card mode.\n"); printf(">> "); // Take user's choice int input = getchar(); printf("\n"); if ((input < '1') || (input > '3')) { ERR("%s", "Invalid selection."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } /* * '1' -> "Virtual mode" (0x02) * '2' -> "Wired card" (0x03) * '3' -> "Dual card" (0x04) */ int iMode = input - '0' + 0x01; pn532_sam_mode mode = iMode; // Connect with the SAM switch (mode) { case PSM_VIRTUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf("Now the SAM is readable for 1 minute from an external reader.\n"); wait_one_minute(); } break; case PSM_WIRED_CARD: { // Set opened NFC device to initiator mode if (nfc_initiator_init_secure_element(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init_secure_element"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Read the SAM's info const nfc_modulation nmSAM = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; nfc_target nt; int res; if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 0) { ERR("No SAM found."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 1) { printf("The following ISO14443A tag (SAM) was found:\n"); print_nfc_target(&nt, true); } else { ERR("%s", "More than one ISO14442 tag found as SAM."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } } break; case PSM_DUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } uint8_t abtRx[MAX_FRAME_LEN]; nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, }, .nti = { .nai = { .abtAtqa = { 0x04, 0x00 }, .abtUid = { 0x08, 0xad, 0xbe, 0xef }, .btSak = 0x20, .szUidLen = 4, .szAtsLen = 0, }, }, };
int main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hyK:")) != -1) { switch (ch) { case 'h': usage(argv[0]); exit (EXIT_SUCCESS); break; case 'y': configure_options.interactive = false; break; case 'K': if (strlen(optarg) != 16) { usage(argv[0]); exit (EXIT_FAILURE); } uint64_t n = strtoull(optarg, NULL, 16); int i; for (i=7; i>=0; i--) { key_data_picc[i] = (uint8_t) n; n >>= 8; } break; default: usage(argv[0]); exit (EXIT_FAILURE); } } // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; nfc_context *context; nfc_init (&context); if (context == NULL) errx(EXIT_FAILURE, "Unable to init libnfc (malloc)"); device_count = nfc_list_devices (context, devices, 8); if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { device = nfc_open (context, devices[d]); if (!device) { warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } for (int i = 0; (!error) && tags[i]; i++) { if (DESFIRE != freefare_get_tag_type (tags[i])) continue; char *tag_uid = freefare_get_tag_uid (tags[i]); char buffer[BUFSIZ]; int res; res = mifare_desfire_connect (tags[i]); if (res < 0) { warnx ("Can't connect to Mifare DESFire target."); error = EXIT_FAILURE; break; } // Make sure we've at least an EV1 version struct mifare_desfire_version_info info; res = mifare_desfire_get_version (tags[i], &info); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_get_version"); error = 1; break; } if (info.software.version_major < 1) { warnx ("Found old DESFire, skipping"); continue; } printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); bool do_it = true; if (configure_options.interactive) { printf ("Change ATS? [yN] "); fgets (buffer, BUFSIZ, stdin); do_it = ((buffer[0] == 'y') || (buffer[0] == 'Y')); } else { printf ("\n"); } if (do_it) { MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_authenticate"); error = EXIT_FAILURE; break; } mifare_desfire_key_free (key_picc); res = mifare_desfire_set_ats (tags[i], new_ats); if (res < 0) { freefare_perror (tags[i], "mifare_desfire_set_ats"); error = EXIT_FAILURE; break; } } mifare_desfire_disconnect (tags[i]); free (tag_uid); } freefare_free_tags (tags); nfc_close (device); } nfc_exit (context); exit (error); } /* main() */
int main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hyK:")) != -1) { switch (ch) { case 'h': usage(argv[0]); exit (EXIT_SUCCESS); break; case 'y': format_options.interactive = false; break; case 'K': if (strlen(optarg) != 16) { usage(argv[0]); exit (EXIT_FAILURE); } uint64_t n = strtoull(optarg, NULL, 16); int i; for (i=7; i>=0; i--) { key_data_picc[i] = (uint8_t) n; n >>= 8; } break; default: usage(argv[0]); exit (EXIT_FAILURE); } } // Remaining args, if any, are in argv[optind .. (argc-1)] nfc_connstring devices[8]; size_t device_count; nfc_context *context; nfc_init (&context); device_count = nfc_list_devices (context, devices, 8); if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { device = nfc_open (context, devices[d]); if (!device) { warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } for (int i = 0; (!error) && tags[i]; i++) { if (DESFIRE != freefare_get_tag_type (tags[i])) continue; char *tag_uid = freefare_get_tag_uid (tags[i]); char buffer[BUFSIZ]; printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); bool format = true; if (format_options.interactive) { printf ("Format [yN] "); fgets (buffer, BUFSIZ, stdin); format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); } else { printf ("\n"); } if (format) { int res; res = mifare_desfire_connect (tags[i]); if (res < 0) { warnx ("Can't connect to Mifare DESFire target."); error = EXIT_FAILURE; break; } MifareDESFireKey key_picc = mifare_desfire_des_key_new_with_version (key_data_picc); res = mifare_desfire_authenticate (tags[i], 0, key_picc); if (res < 0) { warnx ("Can't authenticate on Mifare DESFire target."); error = EXIT_FAILURE; break; } mifare_desfire_key_free (key_picc); // Send Mifare DESFire ChangeKeySetting to change the PICC master key settings into : // bit7-bit4 equal to 0000b // bit3 equal to 1b, the configuration of the PICC master key MAY be changeable or frozen // bit2 equal to 1b, CreateApplication and DeleteApplication commands are allowed without PICC master key authentication // bit1 equal to 1b, GetApplicationIDs, and GetKeySettings are allowed without PICC master key authentication // bit0 equal to 1b, PICC masterkey MAY be frozen or changeable res = mifare_desfire_change_key_settings (tags[i],0x0F); if (res < 0) errx (EXIT_FAILURE, "ChangeKeySettings failed"); res = mifare_desfire_format_picc (tags[i]); if (res < 0) { warn ("Can't format PICC."); error = EXIT_FAILURE; break; } mifare_desfire_disconnect (tags[i]); } free (tag_uid); } freefare_free_tags (tags); nfc_close (device); } nfc_exit (context); exit (error); } /* main() */