00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00035
00036
00037
00038
00039 #ifdef HAVE_CONFIG_H
00040 # include "config.h"
00041 #endif // HAVE_CONFIG_H
00042
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <stddef.h>
00046 #include <stdint.h>
00047 #include <string.h>
00048 #include <signal.h>
00049
00050 #include <nfc/nfc.h>
00051
00052 #include <nfc/nfc-messages.h>
00053 #include "nfc-utils.h"
00054
00055 #define MAX_FRAME_LEN (264)
00056 #define SAK_ISO14443_4_COMPLIANT 0x20
00057
00058 static byte_t abtRx[MAX_FRAME_LEN];
00059 static size_t szRx;
00060 static nfc_device_t *pnd;
00061 static bool quiet_output = false;
00062 static bool init_mfc_auth = false;
00063
00064 void
00065 intr_hdlr (void)
00066 {
00067 printf ("\nQuitting...\n");
00068 if (pnd != NULL) {
00069 nfc_disconnect(pnd);
00070 }
00071 exit (EXIT_FAILURE);
00072 }
00073
00074 bool
00075 target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput )
00076 {
00077 bool loop = true;
00078 *pszOutput = 0;
00079
00080
00081 if (!quiet_output) {
00082 printf (" In: ");
00083 print_hex (pbtInput, szInput);
00084 }
00085 if(szInput) {
00086 switch(pbtInput[0]) {
00087 case 0x30:
00088
00089 *pszOutput = 15;
00090 strcpy((char*)pbtOutput, "You read block ");
00091 pbtOutput[15] = pbtInput[1];
00092 break;
00093 case 0x50:
00094 if (!quiet_output) {
00095 printf("Target halted me. Bye!\n");
00096 }
00097 loop = false;
00098 break;
00099 case 0x60:
00100 case 0x61:
00101
00102 *pszOutput = 2;
00103 pbtOutput[0] = 0x12;
00104 pbtOutput[1] = 0x34;
00105
00106 init_mfc_auth = true;
00107 break;
00108 case 0xe0:
00109
00110 *pszOutput = pnt->nti.nai.szAtsLen + 1;
00111 pbtOutput[0] = pnt->nti.nai.szAtsLen + 1;
00112 if(pnt->nti.nai.szAtsLen) {
00113 memcpy(pbtOutput+1, pnt->nti.nai.abtAts, pnt->nti.nai.szAtsLen);
00114 }
00115 break;
00116 case 0xc2:
00117 if (!quiet_output) {
00118 printf("Target released me. Bye!\n");
00119 }
00120 loop = false;
00121 break;
00122 default:
00123 if (!quiet_output) {
00124 printf("Unknown frame, emulated target abort.\n");
00125 }
00126 loop = false;
00127 }
00128 }
00129
00130 if ((!quiet_output) && *pszOutput) {
00131 printf (" Out: ");
00132 print_hex (pbtOutput, *pszOutput);
00133 }
00134 return loop;
00135 }
00136
00137 bool
00138 nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt)
00139 {
00140 size_t szTx;
00141 byte_t abtTx[MAX_FRAME_LEN];
00142 bool loop = true;
00143
00144 if (!nfc_target_init (pnd, pnt, abtRx, &szRx)) {
00145 nfc_perror (pnd, "nfc_target_init");
00146 return false;
00147 }
00148
00149 while ( loop ) {
00150 loop = target_io( pnt, abtRx, szRx, abtTx, &szTx );
00151 if (szTx) {
00152 if (!nfc_target_send_bytes(pnd, abtTx, szTx)) {
00153 nfc_perror (pnd, "nfc_target_send_bytes");
00154 return false;
00155 }
00156 }
00157 if ( loop ) {
00158 if ( init_mfc_auth ) {
00159 nfc_configure (pnd, NDO_HANDLE_CRC, false);
00160 init_mfc_auth = false;
00161 }
00162 if (!nfc_target_receive_bytes(pnd, abtRx, &szRx)) {
00163 nfc_perror (pnd, "nfc_target_receive_bytes");
00164 return false;
00165 }
00166 }
00167 }
00168 return true;
00169 }
00170
00171 int
00172 main (int argc, char *argv[])
00173 {
00174 const char *acLibnfcVersion;
00175
00176 #ifdef WIN32
00177 signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr);
00178 #else
00179 signal (SIGINT, (void (*)()) intr_hdlr);
00180 #endif
00181
00182
00183 pnd = nfc_connect (NULL);
00184
00185
00186 acLibnfcVersion = nfc_version ();
00187 printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00188
00189 if (pnd == NULL) {
00190 ERR("Unable to connect to NFC device");
00191 return EXIT_FAILURE;
00192 }
00193
00194 printf ("Connected to NFC device: %s\n", pnd->acName);
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 nfc_target_t nt = {
00207 .nm.nmt = NMT_ISO14443A,
00208 .nm.nbr = NBR_UNDEFINED,
00209 .nti.nai.abtAtqa = { 0x00, 0x04 },
00210 .nti.nai.abtUid = { 0x08, 0xab, 0xcd, 0xef },
00211 .nti.nai.btSak = 0x09,
00212 .nti.nai.szUidLen = 4,
00213 .nti.nai.szAtsLen = 0,
00214 };
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 printf ("%s will emulate this ISO14443-A tag:\n", argv[0]);
00240 print_nfc_iso14443a_info (nt.nti.nai, true);
00241
00242
00243 nfc_configure (pnd, NDO_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT));
00244 printf ("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n");
00245 if (!nfc_target_emulate_tag (pnd, &nt)) {
00246 nfc_perror (pnd, "nfc_target_emulate_tag");
00247 return EXIT_FAILURE;
00248 }
00249
00250 nfc_disconnect(pnd);
00251 exit (EXIT_SUCCESS);
00252 }
00253