Example #1
0
/* get the uid from the device and return it as an atr */
void readUID(PDWORD Length, PUCHAR Value)
{
	unsigned int size;
	unsigned int size_len = sizeof(size);
	char *data;
	unsigned int data_len;

	if (present == 0)
	{
		*Length = 0;
		return;
	}
	if (rfid_protocol_getopt(ph, RFID_OPT_PROTO_SIZE, &size, &size_len) == 0)
			printf("Size: %u bytes\n", size);
	size_len = sizeof(size);
	size = 0;
	if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS_LEN, &size, &size_len) == 0)
	{
		data_len = size + 1;
		data = malloc(data_len);
		if (data)
		{
			if (rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS, data, &data_len) == 0)
			{
				Log2(PCSC_LOG_DEBUG, "got %d bytes of ATS", data_len);

				int i ,j;

				// construct standart contactless ATR
				*(Value + 0) = 0x3B; //TS direct convention
				*(Value + 1) = 0x88; //T0 TD1 available, 8 historical bytes
				*(Value + 2) = 0x80; //TD1 TD2 follows, protocol T0
				*(Value + 3) = 0x01; //TD2 no Tx3, protocol T1
				j = 4;
				char crc = 0x88 ^ 0x80 ^ 0x01;
				for (i = 5; i < data_len; ++i)
				{
					*(Value + i - 5 + j) = data[i];
					crc ^= data[i];
				}
				*(Value + i + j) = crc;
				*Length = data_len - 5 + j + 1;

				return;
			}
		}
	}	

	*Length = 0;	
}
Example #2
0
static int init_proto(void)
{
	struct req_ctx *detect_rctx;
	struct openpcd_hdr *opcdh;
	struct openpcd_l2_connectinfo *l2c;
	struct openpcd_proto_connectinfo *pc;
	unsigned int size;

	l2h = rfid_layer2_scan(rh);
	if (!l2h)
		return 0;

	DEBUGP("l2='%s' ", rfid_layer2_name(l2h));

	detect_rctx = req_ctx_find_get(0, RCTX_STATE_FREE,
					RCTX_STATE_LIBRFID_BUSY);
	if (detect_rctx) {
		unsigned int uid_len;
		opcdh = (struct openpcd_hdr *) detect_rctx->data;
		l2c = (struct openpcd_l2_connectinfo *) 
				(char *) opcdh + sizeof(opcdh);
		l2c->uid_len = sizeof(l2c->uid);
		opcdh->cmd = OPENPCD_CMD_LRFID_DETECT_IRQ;
		opcdh->flags = 0x00;
		opcdh->reg = 0x03;
		opcdh->val = l2h->l2->id;

		detect_rctx->tot_len = sizeof(*opcdh) + sizeof(*l2c);
#if 0
		/* copy UID / PUPI into data section */
		rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_UID, (void *)l2c->uid, 
					&uid_len);
		l2c->uid_len = uid_len & 0xff;
		
		size = sizeof(l2c->proto_supported);
		rfid_layer2_getopt(l2h, RFID_OPT_LAYER2_PROTO_SUPP,
					&l2c->proto_supported, &size);

		switch (l2h->l2->id) {
		case RFID_LAYER2_ISO14443A:
			break;
		case RFID_LAYER2_ISO14443B:
			break;
		case RFID_LAYER2_ISO15693:
			break;
		}
#endif
		req_ctx_set_state(detect_rctx, RCTX_STATE_UDP_EP3_PENDING);
	} else
		DEBUGPCRF("=>>>>>>>>>>>>>>no req_ctx for L2!");
	ph = rfid_protocol_scan(l2h);
	if (!ph)
		return 3;

	DEBUGP("p='%s' ", rfid_protocol_name(ph));
	detect_rctx = req_ctx_find_get(0, RCTX_STATE_FREE,
					RCTX_STATE_LIBRFID_BUSY);
	if (detect_rctx) {
		opcdh = (struct openpcd_hdr *) detect_rctx->data;
		pc = (struct openpcd_proto_connectinfo *)
				((char *) opcdh + sizeof(*opcdh));
		detect_rctx->tot_len = sizeof(*opcdh) + sizeof(*pc);
		opcdh->cmd = OPENPCD_CMD_LRFID_DETECT_IRQ;
		opcdh->flags = 0x00;
		opcdh->reg = 0x04;
		opcdh->val = ph->proto->id;
		/* copy L4 info into data section */

#if 0
		switch (ph->proto->id) {
		case RFID_PROTOCOL_TCL: {
			struct openpcd_proto_tcl_connectinfo *ptc
				= (struct openpcd_proto_tcl_connectinfo *)
						((char *) ph + sizeof(*ph));
			unsigned int space;
			detect_rctx->tot_len += sizeof(*ptc);
			space = detect_rctx->size - sizeof(*opcdh)-sizeof(*pc);
			size = space;
			rfid_protocol_getopt(ph, RFID_OPT_P_TCL_ATS,
					     &ptc->ats_snippet, &size);
			if (size == space) {
				/* we've only copied part of the ATS */
				size = sizeof(ptc->ats_tot_len);
				rfid_protocol_getopt(ph, 
						     RFID_OPT_P_TCL_ATS_LEN,
						     &ptc->ats_tot_len, &size);
			} else {
				ptc->ats_tot_len = size;
			}

			} break;
		}
#endif
		req_ctx_set_state(detect_rctx, RCTX_STATE_UDP_EP3_PENDING);
	} else
		DEBUGPCRF("=>>>>>>>>>>>>>>no req_ctx for L2!");
	led_switch(1, 1);

	if (ph->proto->id == RFID_PROTOCOL_MIFARE_CLASSIC) {
		int rc;

		DEBUGPCR("Authenticating sector %u: ", sector);
		rc = mfcl_set_key(ph, MIFARE_CL_KEYA_DEFAULT_INFINEON);
		if (rc < 0) {
			DEBUGPCR("key format error");
			return 4;
		}
		rc = mfcl_auth(ph, RFID_CMD_MIFARE_AUTH1A, sector*4);
		if (rc < 0) {
			DEBUGPCR("mifare auth error");
			return 4;
		} else
			DEBUGPCR("mifare auth succeeded!\n");

		mifare_classic_read_sector(ph, sector);

		return 5;
	}

	return 4;
}