In order to add FIDO2 support, we need to reorganize some USB/CCID calls to specific area (named driver). Thus, pico-hsm-sdk has two drivers: - CCID driver implements APDU over USB/CCID ISO-7816 standard procedures. - HID driver implements APDU over HID. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
284 lines
No EOL
7.3 KiB
C
284 lines
No EOL
7.3 KiB
C
|
|
/*
|
|
* This file is part of the Pico HSM SDK distribution (https://github.com/polhenarejos/pico-hsm-sdk).
|
|
* Copyright (c) 2022 Pol Henarejos.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 3.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "pico/unique_id.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
// Pico
|
|
#include "pico/stdlib.h"
|
|
#include "pico/multicore.h"
|
|
#include "tusb.h"
|
|
#include "hsm.h"
|
|
#include "usb.h"
|
|
#include "apdu.h"
|
|
|
|
#include "bsp/board.h"
|
|
|
|
// For memcpy
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
// Device specific functions
|
|
static uint8_t rx_buffer[4096], tx_buffer[4096];
|
|
static uint16_t w_offset = 0, r_offset = 0;
|
|
static uint16_t w_len = 0, tx_r_offset = 0;
|
|
|
|
uint32_t usb_write_offset(uint16_t len, uint16_t offset) {
|
|
uint8_t pkt_max = 64;
|
|
if (len > sizeof(tx_buffer))
|
|
len = sizeof(tx_buffer);
|
|
w_len = len;
|
|
tx_r_offset = offset;
|
|
driver_write(tx_buffer+offset, MIN(len, pkt_max));
|
|
w_len -= MIN(len, pkt_max);
|
|
tx_r_offset += MIN(len, pkt_max);
|
|
return MIN(w_len, pkt_max);
|
|
}
|
|
|
|
size_t usb_rx(const uint8_t *buffer, size_t len) {
|
|
uint16_t size = MIN(sizeof(rx_buffer) - w_offset, len);
|
|
if (size > 0) {
|
|
if (buffer == NULL)
|
|
size = driver_read(rx_buffer + w_offset, size);
|
|
else
|
|
memcpy(rx_buffer + w_offset, buffer, size);
|
|
w_offset += size;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
uint32_t usb_write_flush() {
|
|
if (w_len > 0) {
|
|
driver_write(tx_buffer+tx_r_offset, MIN(w_len, 64));
|
|
tx_r_offset += MIN(w_len, 64);
|
|
w_len -= MIN(w_len, 64);
|
|
}
|
|
return w_len;
|
|
}
|
|
|
|
uint32_t usb_write(uint16_t len) {
|
|
return usb_write_offset(len, 0);
|
|
}
|
|
|
|
uint16_t usb_read_available() {
|
|
return w_offset - r_offset;
|
|
}
|
|
|
|
uint16_t usb_write_available() {
|
|
return w_len > 0;
|
|
}
|
|
|
|
uint8_t *usb_get_rx() {
|
|
return rx_buffer;
|
|
}
|
|
|
|
uint8_t *usb_get_tx() {
|
|
return tx_buffer;
|
|
}
|
|
|
|
void usb_clear_rx() {
|
|
w_offset = r_offset = 0;
|
|
}
|
|
|
|
uint16_t usb_read(uint8_t *buffer, size_t buffer_size) {
|
|
uint16_t size = MIN(buffer_size, w_offset-r_offset);
|
|
if (size > 0) {
|
|
memcpy(buffer, rx_buffer+r_offset, size);
|
|
r_offset += size;
|
|
if (r_offset == w_offset) {
|
|
r_offset = w_offset = 0;
|
|
}
|
|
return size;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#ifndef USB_VID
|
|
#define USB_VID 0xFEFF
|
|
#endif
|
|
#ifndef USB_PID
|
|
#define USB_PID 0xFCFD
|
|
#endif
|
|
|
|
#define USB_BCD 0x0200
|
|
|
|
uint32_t timeout = 0;
|
|
|
|
queue_t usb_to_card_q;
|
|
queue_t card_to_usb_q;
|
|
|
|
void usb_init() {
|
|
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
|
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
|
driver_init();
|
|
}
|
|
|
|
static int usb_event_handle() {
|
|
uint16_t rx_read = usb_read_available();
|
|
if (driver_process_usb_packet(rx_read) > 0) {
|
|
uint32_t flag = EV_CMD_AVAILABLE;
|
|
queue_add_blocking(&usb_to_card_q, &flag);
|
|
timeout_start();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void card_init_core1(void) {
|
|
//gpg_data_scan (flash_do_start, flash_do_end);
|
|
low_flash_init_core1();
|
|
}
|
|
|
|
void card_thread() {
|
|
card_init_core1();
|
|
|
|
while (1) {
|
|
uint32_t m;
|
|
queue_remove_blocking(&usb_to_card_q, &m);
|
|
|
|
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE)
|
|
{
|
|
set_res_sw (0x6f, 0x00);
|
|
goto done;
|
|
}
|
|
else if (m == EV_EXIT) {
|
|
if (current_app && current_app->unload) {
|
|
current_app->unload();
|
|
}
|
|
break;
|
|
}
|
|
|
|
process_apdu();
|
|
|
|
done:;
|
|
uint32_t flag = EV_EXEC_FINISHED;
|
|
queue_add_blocking(&card_to_usb_q, &flag);
|
|
}
|
|
//printf("EXIT !!!!!!\r\n");
|
|
if (current_app && current_app->unload)
|
|
current_app->unload();
|
|
}
|
|
|
|
void card_thread();
|
|
void card_start()
|
|
{
|
|
multicore_reset_core1();
|
|
multicore_launch_core1(card_thread);
|
|
led_set_blink(BLINK_MOUNTED);
|
|
}
|
|
|
|
void card_exit() {
|
|
uint32_t flag = EV_EXIT;
|
|
queue_try_add(&usb_to_card_q, &flag);
|
|
led_set_blink(BLINK_SUSPENDED);
|
|
}
|
|
|
|
void usb_task() {
|
|
if (driver_mounted()) {
|
|
if (usb_event_handle() != 0) {
|
|
|
|
}
|
|
usb_write_flush();
|
|
uint32_t m = 0x0;
|
|
bool has_m = queue_try_remove(&card_to_usb_q, &m);
|
|
//if (m != 0)
|
|
// printf("\r\n ------ M = %lu\r\n",m);
|
|
if (has_m) {
|
|
if (m == EV_EXEC_FINISHED) {
|
|
apdu_finish();
|
|
size_t size_next = apdu_next();
|
|
driver_exec_finished(size_next);
|
|
led_set_blink(BLINK_MOUNTED);
|
|
}
|
|
else if (m == EV_PRESS_BUTTON) {
|
|
uint32_t flag = wait_button() ? EV_BUTTON_TIMEOUT : EV_BUTTON_PRESSED;
|
|
queue_try_add(&usb_to_card_q, &flag);
|
|
}
|
|
/*
|
|
if (m == EV_RX_DATA_READY) {
|
|
c->ccid_state = ccid_handle_data(c);
|
|
timeout = 0;
|
|
c->timeout_cnt = 0;
|
|
}
|
|
else if (m == EV_EXEC_FINISHED) {
|
|
if (c->ccid_state == CCID_STATE_EXECUTE) {
|
|
exec_done:
|
|
if (c->a->sw == CCID_THREAD_TERMINATED) {
|
|
c->sw1sw2[0] = 0x90;
|
|
c->sw1sw2[1] = 0x00;
|
|
c->state = APDU_STATE_RESULT;
|
|
ccid_send_data_block(c);
|
|
c->ccid_state = CCID_STATE_EXITED;
|
|
c->application = 0;
|
|
return;
|
|
}
|
|
|
|
c->a->cmd_apdu_data_len = 0;
|
|
c->sw1sw2[0] = c->a->sw >> 8;
|
|
c->sw1sw2[1] = c->a->sw & 0xff;
|
|
if (c->a->res_apdu_data_len <= c->a->expected_res_size) {
|
|
c->state = APDU_STATE_RESULT;
|
|
ccid_send_data_block(c);
|
|
c->ccid_state = CCID_STATE_WAIT;
|
|
}
|
|
else {
|
|
c->state = APDU_STATE_RESULT_GET_RESPONSE;
|
|
c->p = c->a->res_apdu_data;
|
|
c->len = c->a->res_apdu_data_len;
|
|
ccid_send_data_block_gr(c, c->a->expected_res_size);
|
|
c->ccid_state = CCID_STATE_WAIT;
|
|
}
|
|
}
|
|
else {
|
|
DEBUG_INFO ("ERR05\r\n");
|
|
}
|
|
led_set_blink(BLINK_MOUNTED);
|
|
}
|
|
else if (m == EV_TX_FINISHED){
|
|
if (c->state == APDU_STATE_RESULT)
|
|
ccid_reset(c);
|
|
else
|
|
c->tx_busy = 0;
|
|
if (c->state == APDU_STATE_WAIT_COMMAND || c->state == APDU_STATE_COMMAND_CHAINING || c->state == APDU_STATE_RESULT_GET_RESPONSE)
|
|
ccid_prepare_receive(c);
|
|
}
|
|
*/
|
|
}
|
|
else {
|
|
if (timeout > 0) {
|
|
if (timeout + 1500 < board_millis()) {
|
|
driver_exec_timeout();
|
|
timeout = board_millis();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void timeout_stop() {
|
|
timeout = 0;
|
|
}
|
|
|
|
void timeout_start() {
|
|
timeout = board_millis();
|
|
}
|
|
|
|
uint8_t *usb_prepare_response() {
|
|
return driver_prepare_response();
|
|
} |