Beispiel #1
0
/*
 * call-seq:
 *  change led
 *
 * Deselect the current tag
 */
static VALUE dev_led(VALUE self, VALUE led_state, VALUE t1_dur, VALUE t2_dur, VALUE b2_rep)
{
  nfc_device * dev;
  uint8_t   pbtTx[9] = { 0xFF,0x00,0x40,0x00,0x04,0x00,0x00,0x00,0x00};
  uint8_t    byte_conv = 0;
  size_t      szTx = sizeof(pbtTx);
  size_t      timeout = -1;
  uint8_t *    pbtRx[2];
  size_t      pszRx = sizeof(pbtRx);
  byte_conv = (int) NUM2INT(led_state) & 0xFF; 
  printf("%i %i\n", szTx, byte_conv);
  pbtTx[3] = byte_conv;
  pbtTx[5] = (uint8_t) NUM2INT(t1_dur) & 0xFF;
  pbtTx[6] = (uint8_t) NUM2INT(t2_dur) & 0xFF;
  pbtTx[7] = (uint8_t) NUM2INT(b2_rep) & 0xFF;
  Data_Get_Struct(self, nfc_device, dev);
  

  if(nfc_initiator_transceive_bytes(dev, pbtTx, szTx, &pbtRx, &pszRx,NULL) < 0) {
    printf("ERROR while transceiving...\n");
    printf("%d\n", pszRx);
    return T_FALSE;
  }else {

  }
  return INT2NUM(pbtRx[1]);
}
Beispiel #2
0
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
bool transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
{
  // Transmit the command bytes
  if (nfc_initiator_transceive_bytes(device, pbtTx, szTx, abtRx, sizeof(abtRx), 0) < 0)
    return false;

  return true;
}
Beispiel #5
0
static  bool
transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
{
  if ((szRx = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
    return false;

  return true;
}
Beispiel #6
0
bool
nfc_initiator_jewel_cmd(nfc_device *pnd, const jewel_req req, jewel_res *pres)
{
    size_t nLenReq;
    size_t nLenRes;

    switch (req.rid.btCmd) {
    case TC_RID:
        nLenReq = sizeof(jewel_req_rid);
        nLenRes = sizeof(jewel_res_rid);
        break;
    case TC_RALL:
        nLenReq = sizeof(jewel_req_rall);
        nLenRes = sizeof(jewel_res_rall);
        break;
    case TC_READ:
        nLenReq = sizeof(jewel_req_read);
        nLenRes = sizeof(jewel_res_read);
        break;
    case TC_WRITEE:
        nLenReq = sizeof(jewel_req_writee);
        nLenRes = sizeof(jewel_res_writee);
        break;
    case TC_WRITENE:
        nLenReq = sizeof(jewel_req_writene);
        nLenRes = sizeof(jewel_res_writene);
        break;
    case TC_RSEG:
        nLenReq = sizeof(jewel_req_rseg);
        nLenRes = sizeof(jewel_res_rseg);
        break;
    case TC_READ8:
        nLenReq = sizeof(jewel_req_read8);
        nLenRes = sizeof(jewel_res_read8);
        break;
    case TC_WRITEE8:
        nLenReq = sizeof(jewel_req_writee8);
        nLenRes = sizeof(jewel_res_writee8);
        break;
    case TC_WRITENE8:
        nLenReq = sizeof(jewel_req_writene8);
        nLenRes = sizeof(jewel_res_writene8);
        break;
    default:
        return false;
    }

    if (nfc_initiator_transceive_bytes(pnd, (uint8_t *)&req, nLenReq, (uint8_t *)pres, nLenRes, -1) < 0) {
        nfc_perror(pnd, "nfc_initiator_transceive_bytes");
        return false;
    }

    return true;
}
Beispiel #7
0
static  bool
transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
{
  // Show transmitted command
  printf("Sent bits:     ");
  print_hex(pbtTx, szTx);
  // Transmit the command bytes
  int res;
  if ((res = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
    return false;

  // Show received answer
  printf("Received bits: ");
  print_hex(abtRx, res);
  // Succesful transfer
  return true;
}
static  bool
transmit_bytes (const byte_t * pbtTx, const size_t szTx)
{
  // Show transmitted command
  if (!quiet_output) {
    printf ("Sent bits:     ");
    print_hex (pbtTx, szTx);
  }
  // Transmit the command bytes
  if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx))
    return false;

  // Show received answer
  if (!quiet_output) {
    printf ("Received bits: ");
    print_hex (abtRx, szRx);
  }
  // Succesful transfer
  return true;
}
/*
 * Callback for freefare_tag_new to test presence of a MIFARE UltralightC on the reader.
 */
bool
is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai)
{
    int ret;
    uint8_t cmd_step1[2];
    uint8_t res_step1[9];
    cmd_step1[0] = 0x1A;
    cmd_step1[1] = 0x00;

    nfc_target pnti;
    nfc_modulation modulation = {
	.nmt = NMT_ISO14443A,
	.nbr = NBR_106
    };
    nfc_initiator_select_passive_target (device, modulation, nai.abtUid, nai.szUidLen, &pnti);
    nfc_device_set_property_bool (device, NP_EASY_FRAMING, false);
    ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, sizeof(res_step1), 0);
    nfc_device_set_property_bool (device, NP_EASY_FRAMING, true);
    nfc_initiator_deselect_target (device);
    return ret >= 0;
}
Beispiel #10
0
bool transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
{
    //! Show transmitted command
    #ifdef DEBUG_PRINTF
    fprintf(stderr,"Sent bits:     ");
    #endif
    print_hex(pbtTx, szTx);
    //! Transmit the command bytes
    int res;
    if ((res = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
    {
        return false;
    }
    //! Show received answer
    #ifdef DEBUG_PRINTF
    fprintf(stderr,"Received bits: ");
    #endif
    print_hex(abtRx, res);
    //! Succesful transfer
    return true;
}
int
CardTransmit(nfc_device *pnd, uint8_t * capdu, size_t capdulen, uint8_t * rapdu, size_t * rapdulen)
{
  int res;
  size_t  szPos;
  printf("=> ");
  for (szPos = 0; szPos < capdulen; szPos++) {
    printf("%02x ", capdu[szPos]);
  }
  printf("\n");
  if ((res = nfc_initiator_transceive_bytes(pnd, capdu, capdulen, rapdu, *rapdulen, 500)) < 0) {
    return -1;
  } else {
    *rapdulen = (size_t) res;
    printf("<= ");
    for (szPos = 0; szPos < *rapdulen; szPos++) {
      printf("%02x ", rapdu[szPos]);
    }
    printf("\n");
    return 0;
  }
}
Beispiel #12
0
bool silvia_nfc_card::transmit(bytestring APDU, bytestring& data_sw)
{
	if (!connected) return false;
	
	data_sw.resize(65536);
	
	int out_len = nfc_initiator_transceive_bytes(device, APDU.byte_str(), APDU.size(), data_sw.byte_str(), data_sw.size(), 0);

	if (out_len < 2)
	{
		return false;
	}
	
	data_sw.resize(out_len);
	
	if (data_sw.size() < 2)
	{
		return false;
	}
	
	return true;
}
Beispiel #13
0
int main(int argc, char **argv) {
    nfc_device *pnd;
    nfc_target nt;

    // Allocate only a pointer to nfc_context
    nfc_context *context;

    byte_t abtRx[MAX_FRAME_LEN];
    byte_t abtTx[MAX_FRAME_LEN];
    size_t szRx = sizeof(abtRx);
	size_t szTx;

	byte_t START_14443A[] = {0x4A, 0x01, 0x00};
	byte_t SELECT_APP[] = {0x40,0x01,0x00,0xA4,0x04,0x00,0x07,0xA0,0x00,0x00,0x00,0x42,0x10,0x10,0x00};
	byte_t READ_RECORD_VISA[] = {0x40, 0x01, 0x00, 0xB2, 0x02, 0x0C, 0x00, 0x00};
	byte_t READ_RECORD_MC[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x14, 0x00, 0x00};
	byte_t READ_PAYLOG_VISA[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x8C, 0x00, 0x00};
	byte_t READ_PAYLOG_MC[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x5C, 0x00, 0x00};

	unsigned char *res, output[50], c, amount[10],msg[100];
	unsigned int i, j, expiry;

    // Initialize the libnfc and set the nfc_context
    nfc_init(&context);
    if (context == NULL) {
        printf("Unable to init libnfc(malloc)\n");
        return 1;
    }typedef unsigned char byte;

    // Open first available device
    pnd = nfc_open(context, NULL);
	if (pnd == NULL) {
		printf("Unable to connect to NFC device.\n");
		return(1);
	}
	//printf("Connected to NFC reader: %s\n", pnd->acName);
	nfc_initiator_init(pnd);

	while(1) {

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, START_14443A, sizeof(START_14443A), abtRx, szRx, 0)) {
				nfc_perror(pnd, "START_14443A");
				return(1);
		}
		//show(szRx, abtRx);

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, SELECT_APP, sizeof(SELECT_APP), abtRx, szRx, 0)) {
				nfc_perror(pnd, "SELECT_APP");
				return(1);
		}
		//show(szRx, abtRx);

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, READ_RECORD_VISA, sizeof(READ_RECORD_VISA), abtRx, szRx, 0)) {
				nfc_perror(pnd, "READ_RECORD");
				return(1);
		}
		//show(szRx, abtRx);

		/* Look for cardholder name */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x5f&&*(res+1)==0x20) {
					strncpy(output, res+3, (int) *(res+2));
					output[(int) *(res+2)]=0;
					printf("Cardholder name: %s\n",output);
					break;			
				}
				res++;
		}

		/* Look for PAN & Expiry date */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x4d&&*(res+1)==0x57) {
					strncpy(output, res+3, 13);
					output[11]=0;
					printf("PAN:");
					
					for(j=0;j<8;j++) {
						if(j%2==0) printf(" ");
						c=output[j];
						if(MASKED&j>=2&j<=5) {
							printf("**");
						}
						else {
							printf("%02x",c&0xff);
						}
					}
					printf("\n");
					expiry = (output[10]+(output[9]<<8)+(output[8]<<16))>>4;
					printf("Expiration date: %02x/20%02x\n\n",(expiry&0xff),((expiry>>8)&0xff));
					break;			
				}
				res++;
		}

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, READ_RECORD_MC, sizeof(READ_RECORD_MC), abtRx, szRx, 0)) {
				nfc_perror(pnd, "READ_RECORD");
				return(1);
		}
		//show(szRx, abtRx);

		/* Look for cardholder name */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x5f&&*(res+1)==0x20) {
					strncpy(output, res+3, (int) *(res+2));
					output[(int) *(res+2)]=0;
					printf("Cardholder name: %s\n",output);
					break;			
				}
				res++;
		}

		/* Look for PAN & Expiry date */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x9c&&*(res+1)==0x57) {
					strncpy(output, res+3, 13);
					output[11]=0;
					printf("PAN:");
					
					for(j=0;j<8;j++) {
						if(j%2==0) printf(" ");
						c=output[j];
						if(MASKED&j>=2&j<=5) {
							printf("**");
						}
						else {
							printf("%02x",c&0xff);
						}
					}
					printf("\n");
					expiry = (output[10]+(output[9]<<8)+(output[8]<<16))>>4;
					printf("Expiration date: %02x/20%02x\n\n",(expiry&0xff),((expiry>>8)&0xff));
					break;			
				}
				res++;
		}
Beispiel #14
0
/**
 * @brief Execute a MIFARE Classic Command
 * @return Returns true if action was successfully performed; otherwise returns false.
 * @param pmp Some commands need additional information. This information should be supplied in the mifare_param union.
 *
 * The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
 * @note There are three different types of information (Authenticate, Data and Value).
 *
 * First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID. 
 * They are both used to initialize the internal cipher-state of the PN53X chip (http://libnfc.org/hardware/pn53x-chip).
 * After a successful authentication it will be possible to execute other commands (e.g. Read/Write). 
 * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
 */
bool
nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
{
  byte_t  abtRx[265];
  size_t  szRxLen;
  size_t  szParamLen;
  byte_t  abtCmd[265];

  // Make sure we are dealing with a active device
  if (!pnd->bActive)
    return false;

  abtCmd[0] = mc;               // The MIFARE Classic command
  abtCmd[1] = ui8Block;         // The block address (1K=0x00..0x39, 4K=0x00..0xff)

  switch (mc) {
    // Read and store command have no parameter
  case MC_READ:
  case MC_STORE:
    szParamLen = 0;
    break;

    // Authenticate command
  case MC_AUTH_A:
  case MC_AUTH_B:
    szParamLen = sizeof (mifare_param_auth);
    break;

    // Data command
  case MC_WRITE:
    szParamLen = sizeof (mifare_param_data);
    break;

    // Value command
  case MC_DECREMENT:
  case MC_INCREMENT:
  case MC_TRANSFER:
    szParamLen = sizeof (mifare_param_value);
    break;

    // Please fix your code, you never should reach this statement
  default:
    return false;
    break;
  }

  // When available, copy the parameter bytes
  if (szParamLen)
    memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);

  // Fire the mifare command
  if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) {
    /*if (pnd->iLastError != 0x14)
      nfc_perror (pnd, "nfc_initiator_transceive_bytes");*/
    return false;
  }
  // When we have executed a read command, copy the received bytes into the param
  if (mc == MC_READ) {
    if (szRxLen == 16) {
      memcpy (pmp->mpd.abtData, abtRx, 16);
    } else {
		//printf("\n-----------------------------------------\n");
      return false;
    }
  }
  // Command succesfully executed
  return true;
}
/**
 * @brief Execute a MIFARE Classic Command
 * @return Returns true if action was successfully performed; otherwise returns false.
 * @param pmp Some commands need additional information. This information should be supplied in the mifare_param union.
 *
 * The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
 * @note There are three different types of information (Authenticate, Data and Value).
 *
 * First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID. 
 * They are both used to initialize the internal cipher-state of the PN53X chip (http://libnfc.org/hardware/pn53x-chip).
 * After a successful authentication it will be possible to execute other commands (e.g. Read/Write). 
 * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
 */
bool
nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
{
  byte_t  abtRx[265];
  size_t  szRx;
  size_t  szParamLen;
  byte_t  abtCmd[265];
  bool    bEasyFraming;

  // Make sure we are dealing with a active device
  if (!pnd->bActive)
    return false;

  abtCmd[0] = mc;               // The MIFARE Classic command
  abtCmd[1] = ui8Block;         // The block address (1K=0x00..0x39, 4K=0x00..0xff)

  switch (mc) {
    // Read and store command have no parameter
  case MC_READ:
  case MC_STORE:
    szParamLen = 0;
    break;

    // Authenticate command
  case MC_AUTH_A:
  case MC_AUTH_B:
    szParamLen = sizeof (mifare_param_auth);
    break;

    // Data command
  case MC_WRITE:
    szParamLen = sizeof (mifare_param_data);
    break;

    // Value command
  case MC_DECREMENT:
  case MC_INCREMENT:
  case MC_TRANSFER:
    szParamLen = sizeof (mifare_param_value);
    break;

    // Please fix your code, you never should reach this statement
  default:
    return false;
    break;
  }

  // When available, copy the parameter bytes
  if (szParamLen)
    memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);

  bEasyFraming = pnd->bEasyFraming;
  if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) {
    nfc_perror (pnd, "nfc_configure");
    return false;
  }
  // Fire the mifare command
  if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx)) {
    if (pnd->iLastError == EINVRXFRAM) {
      // "Invalid received frame" AKA EINVRXFRAM,  usual means we are
      // authenticated on a sector but the requested MIFARE cmd (read, write)
      // is not permitted by current acces bytes;
      // So there is nothing to do here.
    } else {
      nfc_perror (pnd, "nfc_initiator_transceive_bytes");
    }
    nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming);
    return false;
  }
  if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) {
    nfc_perror (pnd, "nfc_configure");
    return false;
  }

  // When we have executed a read command, copy the received bytes into the param
  if (mc == MC_READ) {
    if (szRx == 16) {
      memcpy (pmp->mpd.abtData, abtRx, 16);
    } else {
      return false;
    }
  }
  // Command succesfully executed
  return true;
}
Beispiel #16
0
bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp, FILE *output)
{
    uint8_t  abtRx[265];
    size_t  szParamLen;
    uint8_t  abtCmd[265];
    //bool    bEasyFraming;
    
    output_file = output;
    
    abtCmd[0] = mc;               // The MIFARE Classic command
    abtCmd[1] = ui8Block;         // The block address (1K=0x00..0x39, 4K=0x00..0xff)

    switch (mc) {
    // Read and store command have no parameter
    case MC_READ:
    case MC_STORE:
        szParamLen = 0;
        break;

      // Authenticate command
    case MC_AUTH_A:
    case MC_AUTH_B:
        szParamLen = sizeof(struct mifare_param_auth);
        break;

      // Data command
    case MC_WRITE:
        szParamLen = sizeof(struct mifare_param_data);
        break;

      // Value command
    case MC_DECREMENT:
    case MC_INCREMENT:
    case MC_TRANSFER:
        szParamLen = sizeof(struct mifare_param_value);
        break;

      // Please fix your code, you never should reach this statement
    default:
        return false;
    }

  // When available, copy the parameter bytes
    if (szParamLen)
        memcpy(abtCmd + 2, (uint8_t *) pmp, szParamLen);

    // FIXME: Save and restore bEasyFraming
    // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
    if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
        fprintf(output_file, "nfc_device_set_property_bool");  //This should never happen
        return false;
    }
    // Fire the mifare command
    int res;
    if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1))  < 0) {
        if (res == NFC_ERFTRANS) {
        // "Invalid received frame",  usual means we are
        // authenticated on a sector but the requested MIFARE cmd (read, write)
        // is not permitted by current access bytes;
        // So there is nothing to do here.
        } else {
            fprintf(output_file, "Mifare authentication failed: nfc_initiator_transceive_bytes");
            }
    // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
    return false;
    }
    
  // When we have executed a read command, copy the received bytes into the param
    if (mc == MC_READ) {
        if (res == 16) {
            memcpy(pmp->mpd.abtData, abtRx, 16);
        } else {
            return false;
        }
    }
    return true;
}