some refactoring

This commit is contained in:
merlokk 2019-01-24 19:56:18 +02:00
parent d713167ec4
commit 67b0abde4b
2 changed files with 51 additions and 23 deletions

View File

@ -8,6 +8,9 @@
#include "util.h" #include "util.h"
#include "device.h" #include "device.h"
#include "ctap_errors.h"
// Capability container // Capability container
@ -57,6 +60,36 @@ void nfc_write_frame(uint8_t * data, uint8_t len)
ams_write_buffer(data,len); ams_write_buffer(data,len);
ams_write_command(AMS_CMD_TRANSMIT_BUFFER); ams_write_command(AMS_CMD_TRANSMIT_BUFFER);
printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC, data, len);
}
void nfc_write_response_chaining(uint8_t req0, uint8_t * data, uint8_t len)
{
nfc_write_frame(data, len);
}
bool nfc_write_response_ex(uint8_t req0, uint8_t * data, uint8_t len, uint16_t resp)
{
uint8_t res[32];
if (len > 32 - 3)
return false;
res[0] = NFC_CMD_IBLOCK | (req0 & 3);
if (len)
memcpy(&res[1], data, len);
res[len + 1] = resp >> 8;
res[len + 2] = resp & 0xff;
nfc_write_frame(res, 3 + len);
return true;
}
bool nfc_write_response(uint8_t req0, uint16_t resp)
{
return nfc_write_response_ex(req0, NULL, 0, resp);
} }
int answer_rats(uint8_t parameter) int answer_rats(uint8_t parameter)
@ -142,8 +175,8 @@ void nfc_process_iblock(uint8_t * buf, int len)
// NFC_STATE.selected_applet = APP_NDEF_TAG; // NFC_STATE.selected_applet = APP_NDEF_TAG;
// // Select NDEF file! // // Select NDEF file!
// res[0] = NFC_CMD_IBLOCK | (buf[0] & 1); // res[0] = NFC_CMD_IBLOCK | (buf[0] & 1);
// res[1] = APDU_STATUS_SUCCESS>>8; // res[1] = SW_SUCCESS>>8;
// res[2] = APDU_STATUS_SUCCESS & 0xff; // res[2] = SW_SUCCESS & 0xff;
// nfc_write_frame(res, 3); // nfc_write_frame(res, 3);
// printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3); // printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3);
// } // }
@ -156,19 +189,11 @@ void nfc_process_iblock(uint8_t * buf, int len)
// block = NFC_STATE.block_num; // block = NFC_STATE.block_num;
// block = !block; // block = !block;
// NFC_STATE.block_num = block; // NFC_STATE.block_num = block;
res[0] = NFC_CMD_IBLOCK | (buf[0] & 3); nfc_write_response_ex(buf[0], (uint8_t *)"U2F_V2", 6, SW_SUCCESS);
memcpy(&res[1], (uint8_t *)"U2F_V2", 6);
res[7] = APDU_STATUS_SUCCESS >> 8;
res[8] = APDU_STATUS_SUCCESS & 0xff;
nfc_write_frame(res, 3 + 6);
printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3 + 6);
} }
else else
{ {
res[0] = NFC_CMD_IBLOCK | (buf[0] & 3); nfc_write_response(buf[0], SW_FILE_NOT_FOUND);
res[1] = 0x6a;
res[2] = 0x82;
nfc_write_frame(res, 3);
printf1(TAG_NFC, "NOT selected\r\n"); dump_hex1(TAG_NFC,res, 3); printf1(TAG_NFC, "NOT selected\r\n"); dump_hex1(TAG_NFC,res, 3);
} }
} }
@ -177,12 +202,7 @@ void nfc_process_iblock(uint8_t * buf, int len)
case APDU_FIDO_U2F_VERSION: case APDU_FIDO_U2F_VERSION:
printf1(TAG_NFC, "U2F GetVersion command.\r\n"); printf1(TAG_NFC, "U2F GetVersion command.\r\n");
res[0] = NFC_CMD_IBLOCK | (buf[0] & 3); nfc_write_response_ex(buf[0], (uint8_t *)"U2F_V2", 6, SW_SUCCESS);
memcpy(&res[1], (uint8_t *)"U2F_V2", 6);
res[7] = APDU_STATUS_SUCCESS >> 8;
res[8] = APDU_STATUS_SUCCESS & 0xff;
nfc_write_frame(res, 3 + 6);
printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3 + 6);
break; break;
case APDU_FIDO_U2F_REGISTER: case APDU_FIDO_U2F_REGISTER:
@ -201,8 +221,12 @@ void nfc_process_iblock(uint8_t * buf, int len)
status = ctap_request(payload, plen, &ctap_resp); status = ctap_request(payload, plen, &ctap_resp);
printf1(TAG_NFC, "status: %d\r\n", status); printf1(TAG_NFC, "status: %d\r\n", status);
nfc_write_frame(ctap_resp.data, ctap_resp.length); if (status == CTAP1_ERR_SUCCESS)
printf1(TAG_NFC, "<< "); dump_hex1(TAG_NFC, ctap_resp.data, ctap_resp.length); {
nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length);
} else {
nfc_write_response(buf[0], SW_INTERNAL_EXCEPTION | status);
}
break; break;
case APDU_INS_READ_BINARY: case APDU_INS_READ_BINARY:
@ -235,14 +259,15 @@ void nfc_process_iblock(uint8_t * buf, int len)
} }
res[0] = NFC_CMD_IBLOCK | (buf[0] & 1); res[0] = NFC_CMD_IBLOCK | (buf[0] & 1);
res[1+plen] = APDU_STATUS_SUCCESS>>8; res[1+plen] = SW_SUCCESS>>8;
res[2+plen] = APDU_STATUS_SUCCESS & 0xff; res[2+plen] = SW_SUCCESS & 0xff;
nfc_write_frame(res, 3+plen); nfc_write_frame(res, 3+plen);
printf1(TAG_NFC,"APDU_INS_READ_BINARY\r\n"); printf1(TAG_NFC,"APDU_INS_READ_BINARY\r\n");
printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3+plen); printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3+plen);
break; break;
default: default:
printf1(TAG_NFC, "Unknown INS %02x\r\n", apdu->ins); printf1(TAG_NFC, "Unknown INS %02x\r\n", apdu->ins);
nfc_write_response(buf[0], SW_INS_INVALID);
break; break;
} }

View File

@ -66,6 +66,9 @@ typedef enum
APP_FIDO, APP_FIDO,
} APPLETS; } APPLETS;
#define APDU_STATUS_SUCCESS 0x9000 #define SW_SUCCESS 0x9000
#define SW_FILE_NOT_FOUND 0x6a82
#define SW_INS_INVALID 0x6d00
#define SW_INTERNAL_EXCEPTION 0x6f00
#endif #endif