libnfc  1.7.0
nfc-emulate-tag.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 // Note that depending on the device (initiator) you'll use against, this
43 // emulator it might work or not. Some readers are very strict on responses
44 // timings, e.g. a Nokia NFC and will drop communication too soon for us.
45 
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif // HAVE_CONFIG_H
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <stddef.h>
53 #include <stdint.h>
54 #include <string.h>
55 #include <signal.h>
56 
57 #include <nfc/nfc.h>
58 
59 #include "utils/nfc-utils.h"
60 
61 #define MAX_FRAME_LEN (264)
62 #define SAK_ISO14443_4_COMPLIANT 0x20
63 
64 static uint8_t abtRx[MAX_FRAME_LEN];
65 static int szRx;
66 static nfc_context *context;
67 static nfc_device *pnd;
68 static bool quiet_output = false;
69 static bool init_mfc_auth = false;
70 
71 static void
72 intr_hdlr(int sig)
73 {
74  (void) sig;
75  printf("\nQuitting...\n");
76  if (pnd != NULL) {
77  nfc_abort_command(pnd);
78  }
79  nfc_close(pnd);
80  nfc_exit(context);
81  exit(EXIT_FAILURE);
82 }
83 
84 static bool
85 target_io(nfc_target *pnt, const uint8_t *pbtInput, const size_t szInput, uint8_t *pbtOutput, size_t *pszOutput)
86 {
87  bool loop = true;
88  *pszOutput = 0;
89 
90  // Show transmitted command
91  if (!quiet_output) {
92  printf(" In: ");
93  print_hex(pbtInput, szInput);
94  }
95  if (szInput) {
96  switch (pbtInput[0]) {
97  case 0x30: // Mifare read
98  // block address is in pbtInput[1]
99  *pszOutput = 15;
100  strcpy((char *)pbtOutput, "You read block ");
101  pbtOutput[15] = pbtInput[1];
102  break;
103  case 0x50: // HLTA (ISO14443-3)
104  if (!quiet_output) {
105  printf("Initiator HLTA me. Bye!\n");
106  }
107  loop = false;
108  break;
109  case 0x60: // Mifare authA
110  case 0x61: // Mifare authB
111  // Let's give back a very random nonce...
112  *pszOutput = 2;
113  pbtOutput[0] = 0x12;
114  pbtOutput[1] = 0x34;
115  // Next commands will be without CRC
116  init_mfc_auth = true;
117  break;
118  case 0xe0: // RATS (ISO14443-4)
119  // Send ATS
120  *pszOutput = pnt->nti.nai.szAtsLen + 1;
121  pbtOutput[0] = pnt->nti.nai.szAtsLen + 1; // ISO14443-4 says that ATS contains ATS_Length as first byte
122  if (pnt->nti.nai.szAtsLen) {
123  memcpy(pbtOutput + 1, pnt->nti.nai.abtAts, pnt->nti.nai.szAtsLen);
124  }
125  break;
126  case 0xc2: // S-block DESELECT
127  if (!quiet_output) {
128  printf("Initiator DESELECT me. Bye!\n");
129  }
130  loop = false;
131  break;
132  default: // Unknown
133  if (!quiet_output) {
134  printf("Unknown frame, emulated target abort.\n");
135  }
136  loop = false;
137  }
138  }
139  // Show transmitted command
140  if ((!quiet_output) && *pszOutput) {
141  printf(" Out: ");
142  print_hex(pbtOutput, *pszOutput);
143  }
144  return loop;
145 }
146 
147 static bool
148 nfc_target_emulate_tag(nfc_device *dev, nfc_target *pnt)
149 {
150  size_t szTx;
151  uint8_t abtTx[MAX_FRAME_LEN];
152  bool loop = true;
153 
154  if ((szRx = nfc_target_init(dev, pnt, abtRx, sizeof(abtRx), 0)) < 0) {
155  nfc_perror(dev, "nfc_target_init");
156  return false;
157  }
158 
159  while (loop) {
160  loop = target_io(pnt, abtRx, (size_t) szRx, abtTx, &szTx);
161  if (szTx) {
162  if (nfc_target_send_bytes(dev, abtTx, szTx, 0) < 0) {
163  nfc_perror(dev, "nfc_target_send_bytes");
164  return false;
165  }
166  }
167  if (loop) {
168  if (init_mfc_auth) {
170  init_mfc_auth = false;
171  }
172  if ((szRx = nfc_target_receive_bytes(dev, abtRx, sizeof(abtRx), 0)) < 0) {
173  nfc_perror(dev, "nfc_target_receive_bytes");
174  return false;
175  }
176  }
177  }
178  return true;
179 }
180 
181 int
182 main(int argc, char *argv[])
183 {
184  (void) argc;
185  const char *acLibnfcVersion;
186 
187 #ifdef WIN32
188  signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr);
189 #else
190  signal(SIGINT, intr_hdlr);
191 #endif
192 
193  nfc_init(&context);
194  if (context == NULL) {
195  ERR("Unable to init libnfc (malloc)");
196  exit(EXIT_FAILURE);
197  }
198 
199  // Display libnfc version
200  acLibnfcVersion = nfc_version();
201  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
202 
203  // Try to open the NFC reader
204  pnd = nfc_open(context, NULL);
205 
206  if (pnd == NULL) {
207  ERR("Unable to open NFC device");
208  nfc_exit(context);
209  exit(EXIT_FAILURE);
210  }
211 
212  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
213 
214  // Notes for ISO14443-A emulated tags:
215  // * Only short UIDs are supported
216  // If your UID is longer it will be truncated
217  // Therefore e.g. an UltraLight can only have short UID, which is
218  // typically badly handled by readers who still try to send their "0x95"
219  // * First byte of UID will be masked by 0x08 by the PN53x firmware
220  // as security countermeasure against real UID emulation
221 
222  // Example of a Mifare Classic Mini
223  // Note that crypto1 is not implemented in this example
224  nfc_target nt = {
225  .nm = {
226  .nmt = NMT_ISO14443A,
227  .nbr = NBR_UNDEFINED,
228  },
229  .nti = {
230  .nai = {
231  .abtAtqa = { 0x00, 0x04 },
232  .abtUid = { 0x08, 0xab, 0xcd, 0xef },
233  .btSak = 0x09,
234  .szUidLen = 4,
235  .szAtsLen = 0,
236  },
237  },
238  };
239  /*
240  // Example of a FeliCa
241  nfc_target nt = {
242  .nm = {
243  .nmt = NMT_FELICA,
244  .nbr = NBR_UNDEFINED,
245  },
246  .nti = {
247  .nfi = {
248  .abtId = { 0x01, 0xFE, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
249  .abtPad = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
250  .abtSysCode = { 0xFF, 0xFF },
251  },
252  },
253  };
254  */
255  /*
256  // Example of a ISO14443-4 (DESfire)
257  nfc_target nt = {
258  .nm = {
259  .nmt = NMT_ISO14443A,
260  .nbr = NBR_UNDEFINED,
261  },
262  .nti = {
263  .nai = {
264  abtAtqa = { 0x03, 0x44 },
265  abtUid = { 0x08, 0xab, 0xcd, 0xef },
266  btSak = 0x20,
267  .szUidLen = 4,
268  .abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 },
269  .szAtsLen = 5,
270  },
271  },
272  };
273  */
274 
275  printf("%s will emulate this ISO14443-A tag:\n", argv[0]);
276  print_nfc_target(&nt, true);
277 
278  // Switch off NP_EASY_FRAMING if target is not ISO14443-4
279  nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT));
280  printf("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n");
281  if (!nfc_target_emulate_tag(pnd, &nt)) {
282  nfc_perror(pnd, "nfc_target_emulate_tag");
283  nfc_close(pnd);
284  nfc_exit(context);
285  exit(EXIT_FAILURE);
286  }
287 
288  nfc_close(pnd);
289  nfc_exit(context);
290  exit(EXIT_SUCCESS);
291 }
292 
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device&#39;s boolean-property value.
Definition: nfc.c:426
int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
Initialize NFC device as an emulated tag.
Definition: nfc.c:915
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1200
libnfc interface
int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
Send bytes and APDU frames.
Definition: nfc.c:994
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1120
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
NFC device information.
Definition: nfc-internal.h:190
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1146
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:973
NFC target structure.
Definition: nfc-types.h:328
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
Receive bytes and APDU frames.
Definition: nfc.c:1014
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
NFC library context Struct which contains internal options, references, pointers, etc...
Definition: nfc-internal.h:175
Provide some examples shared functions like print, parity calculation, options parsing.