mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-10 17:28:27 +02:00
Switching to new style.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -45,9 +45,9 @@
|
||||
#include "usb.h"
|
||||
|
||||
#if MAX_RES_APDU_DATA_SIZE > MAX_CMD_APDU_DATA_SIZE
|
||||
#define USB_BUF_SIZE (MAX_RES_APDU_DATA_SIZE+20+9)
|
||||
#define USB_BUF_SIZE (MAX_RES_APDU_DATA_SIZE + 20 + 9)
|
||||
#else
|
||||
#define USB_BUF_SIZE (MAX_CMD_APDU_DATA_SIZE+20+9)
|
||||
#define USB_BUF_SIZE (MAX_CMD_APDU_DATA_SIZE + 20 + 9)
|
||||
#endif
|
||||
|
||||
#define CCID_SET_PARAMS 0x61 /* non-ICCD command */
|
||||
@@ -102,24 +102,21 @@ struct ccid_header {
|
||||
uint8_t ccid_status = 1;
|
||||
static uint8_t itf_num;
|
||||
|
||||
void ccid_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
if (*usb_get_tx(ITF_CCID)+offset != 0x81) {
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID)+offset, size+10);
|
||||
void ccid_write_offset(uint16_t size, uint16_t offset) {
|
||||
if (*usb_get_tx(ITF_CCID) + offset != 0x81) {
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID) + offset, size + 10);
|
||||
}
|
||||
usb_write_offset(ITF_CCID, size+10, offset);
|
||||
usb_write_offset(ITF_CCID, size + 10, offset);
|
||||
}
|
||||
|
||||
void ccid_write(uint16_t size)
|
||||
{
|
||||
void ccid_write(uint16_t size) {
|
||||
ccid_write_offset(size, 0);
|
||||
}
|
||||
|
||||
struct ccid_header *ccid_response;
|
||||
struct ccid_header *ccid_header;
|
||||
|
||||
int driver_init_ccid()
|
||||
{
|
||||
int driver_init_ccid() {
|
||||
ccid_header = (struct ccid_header *) usb_get_rx(ITF_CCID);
|
||||
// apdu.header = &ccid_header->apdu;
|
||||
|
||||
@@ -130,41 +127,35 @@ int driver_init_ccid()
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
void tud_vendor_rx_cb(uint8_t itf)
|
||||
{
|
||||
void tud_vendor_rx_cb(uint8_t itf) {
|
||||
(void) itf;
|
||||
|
||||
uint32_t len = tud_vendor_available();
|
||||
usb_rx(ITF_CCID, NULL, len);
|
||||
}
|
||||
|
||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes)
|
||||
{
|
||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) {
|
||||
printf("written %ld\n", sent_bytes);
|
||||
usb_write_flush(ITF_CCID);
|
||||
}
|
||||
|
||||
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size) {
|
||||
return tud_vendor_write(buffer, buffer_size);
|
||||
}
|
||||
|
||||
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size) {
|
||||
return tud_vendor_read(buffer, buffer_size);
|
||||
}
|
||||
|
||||
int driver_process_usb_nopacket_ccid()
|
||||
{
|
||||
int driver_process_usb_nopacket_ccid() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
{
|
||||
int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
||||
if (rx_read >= 10) {
|
||||
driver_init_ccid();
|
||||
//printf("%d %d %x\r\n",tccid->dwLength,rx_read-10,tccid->bMessageType);
|
||||
if (ccid_header->dwLength <= rx_read-10) {
|
||||
if (ccid_header->dwLength <= rx_read - 10) {
|
||||
size_t apdu_sent = 0;
|
||||
if (ccid_header->bMessageType != 0x65) {
|
||||
DEBUG_PAYLOAD(usb_get_rx(ITF_CCID), usb_read_available(ITF_CCID));
|
||||
@@ -177,7 +168,8 @@ int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
ccid_response->abRFU0 = ccid_status;
|
||||
ccid_response->abRFU1 = 0;
|
||||
ccid_write(0);
|
||||
} else if (ccid_header->bMessageType == 0x62) {
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x62) {
|
||||
size_t size_atr = (ccid_atr ? ccid_atr[0] : 0);
|
||||
ccid_response->bMessageType = 0x80;
|
||||
ccid_response->dwLength = size_atr;
|
||||
@@ -186,11 +178,12 @@ int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
ccid_response->abRFU0 = 0;
|
||||
ccid_response->abRFU1 = 0;
|
||||
//printf("1 %x %x %x || %x %x %x\r\n",ccid_response->apdu,apdu.rdata,ccid_response,ccid_header,ccid_header->apdu,apdu.data);
|
||||
memcpy(&ccid_response->apdu, ccid_atr+1, size_atr);
|
||||
memcpy(&ccid_response->apdu, ccid_atr + 1, size_atr);
|
||||
card_start(apdu_thread);
|
||||
ccid_status = 0;
|
||||
ccid_write(size_atr);
|
||||
} else if (ccid_header->bMessageType == 0x63) {
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x63) {
|
||||
if (ccid_status == 0) {
|
||||
card_exit(0);
|
||||
}
|
||||
@@ -202,7 +195,8 @@ int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
ccid_response->abRFU0 = ccid_status;
|
||||
ccid_response->abRFU1 = 0;
|
||||
ccid_write(0);
|
||||
} else if (ccid_header->bMessageType == 0x6F) {
|
||||
}
|
||||
else if (ccid_header->bMessageType == 0x6F) {
|
||||
apdu_sent = apdu_process(ITF_CCID, &ccid_header->apdu, ccid_header->dwLength);
|
||||
}
|
||||
usb_clear_rx(ITF_CCID);
|
||||
@@ -222,13 +216,11 @@ int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool driver_mounted_ccid()
|
||||
{
|
||||
bool driver_mounted_ccid() {
|
||||
return tud_vendor_mounted();
|
||||
}
|
||||
|
||||
void driver_exec_timeout_ccid()
|
||||
{
|
||||
void driver_exec_timeout_ccid() {
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = 0;
|
||||
ccid_response->bSlot = 0;
|
||||
@@ -238,8 +230,7 @@ void driver_exec_timeout_ccid()
|
||||
ccid_write(0);
|
||||
}
|
||||
|
||||
void driver_exec_finished_ccid(size_t size_next)
|
||||
{
|
||||
void driver_exec_finished_ccid(size_t size_next) {
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = size_next;
|
||||
ccid_response->bSlot = 0;
|
||||
@@ -249,21 +240,19 @@ void driver_exec_finished_ccid(size_t size_next)
|
||||
ccid_write(size_next);
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset)
|
||||
{
|
||||
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset) {
|
||||
|
||||
ccid_response = (struct ccid_header *) (usb_get_tx(ITF_CCID)+offset-10);
|
||||
ccid_response = (struct ccid_header *) (usb_get_tx(ITF_CCID) + offset - 10);
|
||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||
ccid_response->dwLength = size_next;
|
||||
ccid_response->bSlot = 0;
|
||||
ccid_response->bSeq = ccid_header->bSeq;
|
||||
ccid_response->abRFU0 = ccid_status;
|
||||
ccid_response->abRFU1 = 0;
|
||||
ccid_write_offset(size_next, offset-10);
|
||||
ccid_write_offset(size_next, offset - 10);
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_ccid()
|
||||
{
|
||||
uint8_t *driver_prepare_response_ccid() {
|
||||
ccid_response = (struct ccid_header *) usb_get_tx(ITF_CCID);
|
||||
apdu.rdata = &ccid_response->apdu;
|
||||
return &ccid_response->apdu;
|
||||
@@ -272,37 +261,34 @@ uint8_t *driver_prepare_response_ccid()
|
||||
|
||||
#define MAX_USB_POWER 1
|
||||
|
||||
static void ccid_init_cb(void)
|
||||
{
|
||||
static void ccid_init_cb(void) {
|
||||
TU_LOG1("-------- CCID INIT\r\n");
|
||||
vendord_init();
|
||||
|
||||
//ccid_notify_slot_change(c);
|
||||
}
|
||||
|
||||
static void ccid_reset_cb(uint8_t rhport)
|
||||
{
|
||||
static void ccid_reset_cb(uint8_t rhport) {
|
||||
TU_LOG1("-------- CCID RESET\r\n");
|
||||
itf_num = 0;
|
||||
vendord_reset(rhport);
|
||||
}
|
||||
|
||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
|
||||
{
|
||||
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t)*max_len);
|
||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
|
||||
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t) * max_len);
|
||||
//TU_LOG1("-------- CCID OPEN\r\n");
|
||||
TU_VERIFY(
|
||||
itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0,
|
||||
0);
|
||||
|
||||
//vendord_open expects a CLASS_VENDOR interface class
|
||||
memcpy(itf_vendor, itf_desc, sizeof(uint8_t)*max_len);
|
||||
memcpy(itf_vendor, itf_desc, sizeof(uint8_t) * max_len);
|
||||
((tusb_desc_interface_t *) itf_vendor)->bInterfaceClass = TUSB_CLASS_VENDOR_SPECIFIC;
|
||||
vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len);
|
||||
free(itf_vendor);
|
||||
|
||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
2*sizeof(tusb_desc_endpoint_t);
|
||||
2 * sizeof(tusb_desc_endpoint_t);
|
||||
TU_VERIFY(max_len >= drv_len, 0);
|
||||
|
||||
itf_num = itf_desc->bInterfaceNumber;
|
||||
@@ -312,8 +298,7 @@ static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc,
|
||||
// Support for parameterized reset via vendor interface control request
|
||||
static bool ccid_control_xfer_cb(uint8_t __unused rhport,
|
||||
uint8_t stage,
|
||||
tusb_control_request_t const *request)
|
||||
{
|
||||
tusb_control_request_t const *request) {
|
||||
// nothing to do with DATA & ACK stage
|
||||
TU_LOG2("-------- CCID CTRL XFER\r\n");
|
||||
if (stage != CONTROL_STAGE_SETUP) {
|
||||
@@ -358,8 +343,7 @@ static bool ccid_control_xfer_cb(uint8_t __unused rhport,
|
||||
static bool ccid_xfer_cb(uint8_t rhport,
|
||||
uint8_t ep_addr,
|
||||
xfer_result_t result,
|
||||
uint32_t xferred_bytes)
|
||||
{
|
||||
uint32_t xferred_bytes) {
|
||||
//printf("------ CALLED XFER_CB\r\n");
|
||||
return vendord_xfer_cb(rhport, ep_addr, result, xferred_bytes);
|
||||
//return true;
|
||||
@@ -378,8 +362,7 @@ static const usbd_class_driver_t ccid_driver = {
|
||||
};
|
||||
|
||||
// Implement callback to add our custom driver
|
||||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count)
|
||||
{
|
||||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) {
|
||||
*driver_count = 1;
|
||||
return &ccid_driver;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
|
||||
extern const uint8_t historical_bytes[];
|
||||
|
||||
#define MAX_CMD_APDU_DATA_SIZE (24+4+512*4)
|
||||
#define MAX_RES_APDU_DATA_SIZE (5+9+512*4)
|
||||
#define MAX_CMD_APDU_DATA_SIZE (24 + 4 + 512 * 4)
|
||||
#define MAX_RES_APDU_DATA_SIZE (5 + 9 + 512 * 4)
|
||||
#define CCID_MSG_HEADER_SIZE 10
|
||||
#define USB_LL_BUF_SIZE 64
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ static const struct ccid_class_descriptor ccid_desc = {
|
||||
.bMaxSlotIndex = 0,
|
||||
.bVoltageSupport = 0x01, // 5.0V
|
||||
.dwProtocols = (
|
||||
0x01| // T=0
|
||||
0x01 | // T=0
|
||||
0x02), // T=1
|
||||
.dwDefaultClock = (0xDFC),
|
||||
.dwMaximumClock = (0xDFC),
|
||||
@@ -176,7 +176,7 @@ static const struct ccid_class_descriptor ccid_desc = {
|
||||
.dwSynchProtocols = (0),
|
||||
.dwMechanical = (0),
|
||||
.dwFeatures = 0x40840, //USB-ICC, short & extended APDU
|
||||
.dwMaxCCIDMessageLength = 65544+10,
|
||||
.dwMaxCCIDMessageLength = 65544 + 10,
|
||||
.bClassGetResponse = 0xFF,
|
||||
.bclassEnvelope = 0xFF,
|
||||
.wLcdLayout = 0x0,
|
||||
|
||||
@@ -33,8 +33,7 @@
|
||||
|
||||
int sock = 0;
|
||||
|
||||
int msleep(long msec)
|
||||
{
|
||||
int msleep(long msec) {
|
||||
struct timespec ts;
|
||||
int res;
|
||||
|
||||
@@ -53,8 +52,7 @@ int msleep(long msec)
|
||||
return res;
|
||||
}
|
||||
|
||||
int emul_init(char *host, uint16_t port)
|
||||
{
|
||||
int emul_init(char *host, uint16_t port) {
|
||||
struct sockaddr_in serv_addr;
|
||||
fprintf(stderr, "\n Starting emulation envionrment\n");
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
@@ -81,14 +79,12 @@ int emul_init(char *host, uint16_t port)
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_emul()
|
||||
{
|
||||
uint8_t *driver_prepare_response_emul() {
|
||||
apdu.rdata = usb_get_tx(ITF_EMUL);
|
||||
return apdu.rdata;
|
||||
}
|
||||
|
||||
int driver_write_emul(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
int driver_write_emul(const uint8_t *buffer, size_t buffer_size) {
|
||||
uint16_t size = htons(buffer_size);
|
||||
//DEBUG_PAYLOAD(buffer,buffer_size);
|
||||
int ret = 0;
|
||||
@@ -107,8 +103,7 @@ int driver_write_emul(const uint8_t *buffer, size_t buffer_size)
|
||||
return buffer_size;
|
||||
}
|
||||
|
||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
||||
if (size > 0) {
|
||||
//DEBUG_PAYLOAD(usb_get_tx(ITF_EMUL)+offset, size);
|
||||
return usb_write_offset(ITF_EMUL, size, offset);
|
||||
@@ -116,29 +111,27 @@ uint32_t emul_write_offset(uint16_t size, uint16_t offset)
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t emul_write(uint16_t size)
|
||||
{
|
||||
uint32_t emul_write(uint16_t size) {
|
||||
return emul_write_offset(size, 0);
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_emul(size_t size_next, size_t offset)
|
||||
{
|
||||
void driver_exec_finished_cont_emul(size_t size_next, size_t offset) {
|
||||
emul_write_offset(size_next, offset);
|
||||
}
|
||||
|
||||
int driver_process_usb_packet_emul(uint16_t len)
|
||||
{
|
||||
int driver_process_usb_packet_emul(uint16_t len) {
|
||||
if (len > 0) {
|
||||
uint8_t *data = usb_get_rx(ITF_EMUL), *rdata = usb_get_tx(ITF_EMUL);
|
||||
if (len == 1) {
|
||||
uint8_t c = data[0];
|
||||
if (c == 4) {
|
||||
if (ccid_atr) {
|
||||
memcpy(rdata, ccid_atr+1, ccid_atr[0]);
|
||||
memcpy(rdata, ccid_atr + 1, ccid_atr[0]);
|
||||
}
|
||||
emul_write(ccid_atr ? ccid_atr[0] : 0);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
DEBUG_PAYLOAD(data, len);
|
||||
if (apdu_process(ITF_EMUL, data, len) > 0) {
|
||||
process_apdu();
|
||||
@@ -153,8 +146,7 @@ int driver_process_usb_packet_emul(uint16_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t emul_read()
|
||||
{
|
||||
uint16_t emul_read() {
|
||||
uint16_t len = 0;
|
||||
fd_set input;
|
||||
FD_ZERO(&input);
|
||||
@@ -166,7 +158,8 @@ uint16_t emul_read()
|
||||
if (n == -1) {
|
||||
printf("read wrong\n");
|
||||
//something wrong
|
||||
} else if (n == 0) {
|
||||
}
|
||||
else if (n == 0) {
|
||||
printf("read timeout\n");
|
||||
}
|
||||
if (FD_ISSET(sock, &input)) {
|
||||
|
||||
@@ -65,7 +65,7 @@ extern CTAPHID_FRAME *ctap_req, *ctap_resp;
|
||||
|
||||
#define FRAME_TYPE(f) ((f)->type & TYPE_MASK)
|
||||
#define FRAME_CMD(f) ((f)->init.cmd & ~TYPE_MASK)
|
||||
#define MSG_LEN(f) ((f)->init.bcnth*256 + (f)->init.bcntl)
|
||||
#define MSG_LEN(f) ((f)->init.bcnth * 256 + (f)->init.bcntl)
|
||||
#define FRAME_SEQ(f) ((f)->cont.seq & ~TYPE_MASK)
|
||||
|
||||
// HID usage- and usage-page definitions
|
||||
|
||||
@@ -35,20 +35,17 @@ typedef struct msg_packet {
|
||||
|
||||
msg_packet_t msg_packet = { 0 };
|
||||
|
||||
void tud_mount_cb()
|
||||
{
|
||||
void tud_mount_cb() {
|
||||
mounted = true;
|
||||
}
|
||||
|
||||
bool driver_mounted_hid()
|
||||
{
|
||||
bool driver_mounted_hid() {
|
||||
return mounted;
|
||||
}
|
||||
|
||||
CTAPHID_FRAME *ctap_req = NULL, *ctap_resp = NULL;
|
||||
|
||||
int driver_init_hid()
|
||||
{
|
||||
int driver_init_hid() {
|
||||
tud_init(BOARD_TUD_RHPORT);
|
||||
ctap_req = (CTAPHID_FRAME *) usb_get_rx(ITF_HID);
|
||||
apdu.header = ctap_req->init.data;
|
||||
@@ -72,8 +69,7 @@ uint16_t tud_hid_get_report_cb(uint8_t itf,
|
||||
uint8_t report_id,
|
||||
hid_report_type_t report_type,
|
||||
uint8_t *buffer,
|
||||
uint16_t reqlen)
|
||||
{
|
||||
uint16_t reqlen) {
|
||||
// TODO not Implemented
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
@@ -88,16 +84,14 @@ uint16_t tud_hid_get_report_cb(uint8_t itf,
|
||||
return reqlen;
|
||||
}
|
||||
|
||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset)
|
||||
{
|
||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset) {
|
||||
if (*usb_get_tx(ITF_HID) != 0x81) {
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_HID)+offset, size);
|
||||
DEBUG_PAYLOAD(usb_get_tx(ITF_HID) + offset, size);
|
||||
}
|
||||
return usb_write_offset(ITF_HID, size, offset);
|
||||
}
|
||||
|
||||
uint32_t hid_write(uint16_t size)
|
||||
{
|
||||
uint32_t hid_write(uint16_t size) {
|
||||
return hid_write_offset(size, 0);
|
||||
}
|
||||
|
||||
@@ -110,14 +104,12 @@ static const uint8_t conv_table[128][2] = { HID_ASCII_TO_KEYCODE };
|
||||
static uint8_t keyboard_w = 0;
|
||||
static bool sent_key = false;
|
||||
|
||||
void add_keyboard_buffer(const uint8_t *data, size_t data_len)
|
||||
{
|
||||
void add_keyboard_buffer(const uint8_t *data, size_t data_len) {
|
||||
keyboard_buffer_len = MIN(sizeof(keyboard_buffer), data_len);
|
||||
memcpy(keyboard_buffer, data, keyboard_buffer_len);
|
||||
}
|
||||
|
||||
static void send_hid_report(uint8_t report_id)
|
||||
{
|
||||
static void send_hid_report(uint8_t report_id) {
|
||||
if (!tud_hid_ready()) {
|
||||
return;
|
||||
}
|
||||
@@ -137,14 +129,16 @@ static void send_hid_report(uint8_t report_id)
|
||||
keycode) == true) {
|
||||
sent_key = true;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, 0,
|
||||
NULL) == true) {
|
||||
keyboard_w++;
|
||||
sent_key = false;
|
||||
}
|
||||
}
|
||||
} else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
||||
}
|
||||
else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
||||
keyboard_w = keyboard_buffer_len = 0;
|
||||
}
|
||||
}
|
||||
@@ -154,8 +148,7 @@ static void send_hid_report(uint8_t report_id)
|
||||
}
|
||||
}
|
||||
|
||||
void hid_task(void)
|
||||
{
|
||||
void hid_task(void) {
|
||||
// Poll every 10ms
|
||||
const uint32_t interval_ms = 10;
|
||||
static uint32_t start_ms = 0;
|
||||
@@ -168,13 +161,13 @@ void hid_task(void)
|
||||
// Remote wakeup
|
||||
if (tud_suspended() && keyboard_buffer_len > 0) {
|
||||
tud_remote_wakeup();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
send_hid_report(REPORT_ID_KEYBOARD);
|
||||
}
|
||||
}
|
||||
|
||||
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, /*uint16_t*/ uint8_t len)
|
||||
{
|
||||
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, /*uint16_t*/ uint8_t len) {
|
||||
if (send_buffer_size > 0 && instance == ITF_HID) {
|
||||
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1;
|
||||
if (last_write_result == true) {
|
||||
@@ -188,8 +181,7 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, /*uint1
|
||||
}
|
||||
}
|
||||
|
||||
int driver_write_hid(const uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
int driver_write_hid(const uint8_t *buffer, size_t buffer_size) {
|
||||
last_write_result = tud_hid_n_report(ITF_HID, 0, buffer, buffer_size);
|
||||
printf("result %d\n", last_write_result);
|
||||
if (last_write_result == false) {
|
||||
@@ -198,8 +190,7 @@ int driver_write_hid(const uint8_t *buffer, size_t buffer_size)
|
||||
return MIN(64, buffer_size);
|
||||
}
|
||||
|
||||
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size)
|
||||
{
|
||||
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -209,8 +200,7 @@ void tud_hid_set_report_cb(uint8_t itf,
|
||||
uint8_t report_id,
|
||||
hid_report_type_t report_type,
|
||||
uint8_t const *buffer,
|
||||
uint16_t bufsize)
|
||||
{
|
||||
uint16_t bufsize) {
|
||||
// This example doesn't use multiple report and report ID
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
@@ -223,8 +213,7 @@ void tud_hid_set_report_cb(uint8_t itf,
|
||||
}
|
||||
|
||||
uint32_t last_cmd_time = 0, last_packet_time = 0;
|
||||
int ctap_error(uint8_t error)
|
||||
{
|
||||
int ctap_error(uint8_t error) {
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
@@ -246,9 +235,8 @@ uint8_t thread_type = 0; //1 is APDU, 2 is CBOR
|
||||
extern void cbor_thread();
|
||||
extern bool cancel_button;
|
||||
|
||||
int driver_process_usb_nopacket_hid()
|
||||
{
|
||||
if (last_packet_time > 0 && last_packet_time+500 < board_millis()) {
|
||||
int driver_process_usb_nopacket_hid() {
|
||||
if (last_packet_time > 0 && last_packet_time + 500 < board_millis()) {
|
||||
ctap_error(CTAP1_ERR_MSG_TIMEOUT);
|
||||
last_packet_time = 0;
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
@@ -258,8 +246,7 @@ int driver_process_usb_nopacket_hid()
|
||||
|
||||
extern const uint8_t fido_aid[];
|
||||
|
||||
int driver_process_usb_packet_hid(uint16_t read)
|
||||
{
|
||||
int driver_process_usb_packet_hid(uint16_t read) {
|
||||
int apdu_sent = 0;
|
||||
if (read >= 5) {
|
||||
driver_init_hid();
|
||||
@@ -271,18 +258,19 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
return ctap_error(CTAP1_ERR_INVALID_CHANNEL);
|
||||
}
|
||||
if (board_millis() < lock && ctap_req->cid != last_req.cid &&
|
||||
last_cmd_time+100 > board_millis()) {
|
||||
last_cmd_time + 100 > board_millis()) {
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
}
|
||||
if (FRAME_TYPE(ctap_req) == TYPE_INIT) {
|
||||
if (MSG_LEN(ctap_req) > CTAP_MAX_PACKET_SIZE) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
}
|
||||
if (msg_packet.len > 0 && last_cmd_time+100 > board_millis() &&
|
||||
if (msg_packet.len > 0 && last_cmd_time + 100 > board_millis() &&
|
||||
ctap_req->init.cmd != CTAPHID_INIT) {
|
||||
if (last_req.cid != ctap_req->cid) { //We are in a transaction
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
||||
}
|
||||
}
|
||||
@@ -291,14 +279,15 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
if (MSG_LEN(ctap_req) > 64 - 7) {
|
||||
msg_packet.len = MSG_LEN(ctap_req);
|
||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->init.data, 64-7);
|
||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->init.data, 64 - 7);
|
||||
msg_packet.current_len += 64 - 7;
|
||||
}
|
||||
memcpy(&last_req, ctap_req, sizeof(CTAPHID_FRAME));
|
||||
last_cmd = ctap_req->init.cmd;
|
||||
last_seq = 0;
|
||||
last_cmd_time = board_millis();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (msg_packet.len == 0) { //Received a cont with a prior init pkt
|
||||
return 0;
|
||||
}
|
||||
@@ -311,7 +300,8 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
||||
memcpy(&last_req, ctap_req, sizeof(CTAPHID_FRAME));
|
||||
last_seq++;
|
||||
} else if (last_cmd_time+100 > board_millis()) {
|
||||
}
|
||||
else if (last_cmd_time + 100 > board_millis()) {
|
||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||
}
|
||||
|
||||
@@ -337,7 +327,8 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
} else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||
if (MSG_LEN(ctap_req) != 0) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
}
|
||||
@@ -347,14 +338,16 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
} else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
}
|
||||
else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
memcpy(ctap_resp->init.data, msg_packet.data, msg_packet.len);
|
||||
driver_exec_finished_hid(msg_packet.len);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
memcpy(ctap_resp->init.data, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
ctap_resp->init.cmd = last_cmd;
|
||||
@@ -364,7 +357,8 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
}
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
} else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
||||
if (MSG_LEN(ctap_req) != 1) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||
}
|
||||
@@ -379,14 +373,16 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
} else if (last_cmd == CTAPHID_MSG &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
}
|
||||
else if (last_cmd == CTAPHID_MSG &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
if (current_app == NULL ||
|
||||
memcmp(current_app->aid, fido_aid+1, MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
||||
memcmp(current_app->aid, fido_aid + 1,
|
||||
MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
||||
for (int a = 0; a < num_apps; a++) {
|
||||
if ((current_app = apps[a].select_aid(&apps[a], fido_aid+1, fido_aid[0]))) {
|
||||
if ((current_app = apps[a].select_aid(&apps[a], fido_aid + 1, fido_aid[0]))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -397,23 +393,26 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
apdu_sent = apdu_process(ITF_HID, msg_packet.data, msg_packet.len);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
apdu_sent = apdu_process(ITF_HID, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
}
|
||||
DEBUG_PAYLOAD(apdu.data, (int) apdu.nc);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
} else if ((last_cmd == CTAPHID_CBOR ||
|
||||
(last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
}
|
||||
else if ((last_cmd == CTAPHID_CBOR ||
|
||||
(last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) &&
|
||||
(msg_packet.len == 0 ||
|
||||
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
//if (thread_type != 2)
|
||||
card_start(cbor_thread);
|
||||
thread_type = 2;
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||
apdu_sent = cbor_process(last_cmd, msg_packet.data, msg_packet.len);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
apdu_sent = cbor_process(last_cmd, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||
}
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
@@ -421,12 +420,14 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
if (apdu_sent < 0) {
|
||||
return ctap_error(-apdu_sent);
|
||||
}
|
||||
} else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
||||
ctap_error(0x2D);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
cancel_button = true;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (msg_packet.len == 0) {
|
||||
return ctap_error(CTAP1_ERR_INVALID_CMD);
|
||||
}
|
||||
@@ -439,8 +440,7 @@ int driver_process_usb_packet_hid(uint16_t read)
|
||||
return apdu_sent;
|
||||
}
|
||||
|
||||
void send_keepalive()
|
||||
{
|
||||
void send_keepalive() {
|
||||
CTAPHID_FRAME *resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + 4096);
|
||||
//memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||
resp->cid = ctap_req->cid;
|
||||
@@ -451,15 +451,13 @@ void send_keepalive()
|
||||
hid_write_offset(64, 4096);
|
||||
}
|
||||
|
||||
void driver_exec_timeout_hid()
|
||||
{
|
||||
void driver_exec_timeout_hid() {
|
||||
if (thread_type == 2) {
|
||||
send_keepalive();
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *driver_prepare_response_hid()
|
||||
{
|
||||
uint8_t *driver_prepare_response_hid() {
|
||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||
apdu.rdata = ctap_resp->init.data;
|
||||
send_buffer_size = 0;
|
||||
@@ -467,20 +465,19 @@ uint8_t *driver_prepare_response_hid()
|
||||
return ctap_resp->init.data;
|
||||
}
|
||||
|
||||
void driver_exec_finished_hid(size_t size_next)
|
||||
{
|
||||
void driver_exec_finished_hid(size_t size_next) {
|
||||
if (size_next > 0) {
|
||||
if (thread_type == 2 && apdu.sw != 0) {
|
||||
ctap_error(apdu.sw & 0xff);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
driver_exec_finished_cont_hid(size_next, 7);
|
||||
}
|
||||
}
|
||||
apdu.sw = 0;
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont_hid(size_t size_next, size_t offset)
|
||||
{
|
||||
void driver_exec_finished_cont_hid(size_t size_next, size_t offset) {
|
||||
offset -= 7;
|
||||
ctap_resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + offset);
|
||||
ctap_resp->cid = ctap_req->cid;
|
||||
@@ -491,6 +488,6 @@ void driver_exec_finished_cont_hid(size_t size_next, size_t offset)
|
||||
if (hid_write_offset(64, offset) > 0) {
|
||||
ctap_resp = (CTAPHID_FRAME *) ((uint8_t *) ctap_resp + 64 - 5);
|
||||
|
||||
send_buffer_size -= MIN(64-7, send_buffer_size);
|
||||
send_buffer_size -= MIN(64 - 7, send_buffer_size);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user