mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-08 05:56:11 +02:00
Added a major refactoring to include Emulated interface.
It works in combination with virtualsmarcard module (vpcd). It properly installed, it creates a virtual reader that can be interfaced via PCSC+vcpd. At user app level, it has no difference of having a physical smart card. At this moment, it only works emulating a CCID interface. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -44,8 +44,6 @@
|
||||
#include "apdu.h"
|
||||
#include "usb.h"
|
||||
|
||||
const uint8_t *ccid_atr = NULL;
|
||||
|
||||
#if MAX_RES_APDU_DATA_SIZE > MAX_CMD_APDU_DATA_SIZE
|
||||
#define USB_BUF_SIZE (MAX_RES_APDU_DATA_SIZE+20+9)
|
||||
#else
|
||||
|
||||
169
src/usb/emulation/emulation.c
Normal file
169
src/usb/emulation/emulation.c
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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 "emulation.h"
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "hsm.h"
|
||||
#include "apdu.h"
|
||||
#include "usb.h"
|
||||
#include "ccid/ccid.h"
|
||||
|
||||
int sock = 0;
|
||||
|
||||
int msleep(long msec)
|
||||
{
|
||||
struct timespec ts;
|
||||
int res;
|
||||
|
||||
if (msec < 0)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ts.tv_sec = msec / 1000;
|
||||
ts.tv_nsec = (msec % 1000) * 1000000;
|
||||
|
||||
do {
|
||||
res = nanosleep(&ts, &ts);
|
||||
} while (res && errno == EINTR);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int emul_init(char *host, uint16_t port) {
|
||||
struct sockaddr_in serv_addr;
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
printf("\n Socket creation error \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(port);
|
||||
|
||||
// Convert IPv4 and IPv6 addresses from text to binary
|
||||
// form
|
||||
if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) {
|
||||
printf("\nInvalid address/ Address not supported \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
printf("\nConnection Failed \n");
|
||||
return -1;
|
||||
}
|
||||
int x = fcntl(sock ,F_GETFL, 0);
|
||||
fcntl(sock, F_SETFL, x | O_NONBLOCK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
uint16_t size = htons(buffer_size);
|
||||
DEBUG_PAYLOAD(buffer,buffer_size);
|
||||
int ret = 0;
|
||||
do {
|
||||
ret = send(sock, &size, sizeof(size), 0);
|
||||
if (ret < 0)
|
||||
msleep(10);
|
||||
} while(ret <= 0);
|
||||
do {
|
||||
ret = send(sock, buffer, (uint16_t)buffer_size, 0);
|
||||
if (ret < 0)
|
||||
msleep(10);
|
||||
} while(ret <= 0);
|
||||
return buffer_size;
|
||||
}
|
||||
|
||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
||||
//DEBUG_PAYLOAD(usb_get_tx(ITF_EMUL)+offset, size);
|
||||
return usb_write_offset(ITF_EMUL, size, offset);
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
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]);
|
||||
emul_write(ccid_atr ? ccid_atr[0] : 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_PAYLOAD(data, len);
|
||||
apdu_process(ITF_EMUL, data, len);
|
||||
process_apdu();
|
||||
apdu_finish();
|
||||
size_t ret = apdu_next();
|
||||
emul_write(ret);
|
||||
}
|
||||
}
|
||||
usb_clear_rx(ITF_EMUL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t emul_read() {
|
||||
uint16_t len = 0;
|
||||
fd_set input;
|
||||
FD_ZERO(&input);
|
||||
FD_SET(sock, &input);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 2;
|
||||
timeout.tv_usec = 0 * 1000;
|
||||
int n = select(sock + 1, &input, NULL, NULL, &timeout);
|
||||
if (n == -1) {
|
||||
printf("read wrong\n");
|
||||
//something wrong
|
||||
} else if (n == 0) {
|
||||
printf("read timeout\n");
|
||||
}
|
||||
if (FD_ISSET(sock, &input)) {
|
||||
int valread = recv(sock, &len, sizeof(len), 0);
|
||||
len = ntohs(len);
|
||||
if (len > 0) {
|
||||
while (true) {
|
||||
valread = recv(sock, usb_get_rx(ITF_EMUL), len, 0);
|
||||
if (valread > 0)
|
||||
return valread;
|
||||
msleep(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
24
src/usb/emulation/emulation.h
Normal file
24
src/usb/emulation/emulation.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _EMULATION_H_
|
||||
#define _EMULATION_H_
|
||||
|
||||
#include <stdint.h>
|
||||
extern int emul_init(char *host, uint16_t port);
|
||||
|
||||
#endif // _EMULATION_H_
|
||||
@@ -25,6 +25,7 @@ typedef unsigned int uint32_t;
|
||||
typedef unsigned long int uint64_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -58,7 +59,9 @@ typedef struct {
|
||||
uint8_t data[HID_RPT_SIZE - 5]; // Data payload
|
||||
} cont;
|
||||
};
|
||||
}__packed CTAPHID_FRAME;
|
||||
} __attribute__ ((__packed__)) CTAPHID_FRAME;
|
||||
|
||||
extern CTAPHID_FRAME *ctap_req, *ctap_resp;
|
||||
|
||||
#define FRAME_TYPE(f) ((f)->type & TYPE_MASK)
|
||||
#define FRAME_CMD(f) ((f)->init.cmd & ~TYPE_MASK)
|
||||
@@ -106,7 +109,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
}__packed CTAPHID_INIT_REQ;
|
||||
} __attribute__ ((__packed__)) CTAPHID_INIT_REQ;
|
||||
|
||||
typedef struct {
|
||||
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
|
||||
@@ -116,7 +119,7 @@ typedef struct {
|
||||
uint8_t versionMinor; // Minor version number
|
||||
uint8_t versionBuild; // Build version number
|
||||
uint8_t capFlags; // Capabilities flags
|
||||
}__packed CTAPHID_INIT_RESP;
|
||||
} __attribute__ ((__packed__)) CTAPHID_INIT_RESP;
|
||||
|
||||
// CTAPHID_SYNC command defines
|
||||
|
||||
|
||||
@@ -74,10 +74,12 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) reqlen;
|
||||
printf("get_report\n");
|
||||
printf("get_report %d %d %d\n",itf,report_id,report_type);
|
||||
DEBUG_PAYLOAD(buffer, reqlen);
|
||||
buffer[1] = HSM_SDK_VERSION_MAJOR;
|
||||
buffer[2] = HSM_SDK_VERSION_MINOR;
|
||||
|
||||
return 0;
|
||||
return reqlen;
|
||||
}
|
||||
|
||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset) {
|
||||
@@ -189,7 +191,9 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
|
||||
(void) itf;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
|
||||
printf("set_report %d %d %d\n",itf,report_id,report_type);
|
||||
if (itf == ITF_KEYBOARD)
|
||||
DEBUG_PAYLOAD(buffer,bufsize);
|
||||
usb_rx(itf, buffer, bufsize);
|
||||
}
|
||||
|
||||
@@ -229,7 +233,6 @@ extern const uint8_t fido_aid[];
|
||||
|
||||
int driver_process_usb_packet_hid(uint16_t read) {
|
||||
int apdu_sent = 0;
|
||||
apdu.sw = 0;
|
||||
if (read >= 5) {
|
||||
driver_init_hid();
|
||||
last_packet_time = board_millis();
|
||||
@@ -296,7 +299,7 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
||||
ctap_resp->init.cmd = CTAPHID_INIT;
|
||||
ctap_resp->init.bcntl = 17;
|
||||
ctap_resp->init.bcnth = 0;
|
||||
driver_exec_finished_hid(17);
|
||||
hid_write(64);
|
||||
msg_packet.len = msg_packet.current_len = 0;
|
||||
last_packet_time = 0;
|
||||
}
|
||||
|
||||
@@ -16,20 +16,19 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pico/unique_id.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// Pico
|
||||
#ifndef ENABLE_EMULATION
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/multicore.h"
|
||||
#include "tusb.h"
|
||||
#include "bsp/board.h"
|
||||
#endif
|
||||
#include "hsm.h"
|
||||
#include "usb.h"
|
||||
#include "apdu.h"
|
||||
|
||||
#include "bsp/board.h"
|
||||
|
||||
// For memcpy
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -46,7 +45,9 @@ void usb_set_timeout_counter(uint8_t itf, uint32_t v) {
|
||||
}
|
||||
|
||||
uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint8_t pkt_max = 64;
|
||||
#endif
|
||||
int w = 0;
|
||||
if (len > sizeof(tx_buffer[itf]))
|
||||
len = sizeof(tx_buffer[itf]);
|
||||
@@ -59,6 +60,10 @@ uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
w = driver_write_ccid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
w = driver_write_emul(tx_buffer[itf]+offset, len);
|
||||
#endif
|
||||
w_len[itf] -= w;
|
||||
tx_r_offset[itf] += w;
|
||||
@@ -95,6 +100,10 @@ uint32_t usb_write_flush(uint8_t itf) {
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
w = driver_write_ccid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
w = driver_write_emul(tx_buffer[itf]+tx_r_offset[itf], w_len[itf]);
|
||||
#endif
|
||||
tx_r_offset[itf] += w;
|
||||
w_len[itf] -= w;
|
||||
@@ -135,20 +144,27 @@ void usb_clear_rx(uint8_t itf) {
|
||||
|
||||
#define USB_BCD 0x0200
|
||||
|
||||
uint32_t timeout = 0;
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
queue_t usb_to_card_q;
|
||||
queue_t card_to_usb_q;
|
||||
#endif
|
||||
|
||||
void usb_init() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
||||
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
||||
#endif
|
||||
}
|
||||
|
||||
extern int driver_process_usb_nopacket();
|
||||
extern uint32_t timeout;
|
||||
|
||||
static int usb_event_handle(uint8_t itf) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint16_t rx_read = usb_read_available(itf);
|
||||
#else
|
||||
uint16_t rx_read = emul_read();
|
||||
#endif
|
||||
int proc_packet = 0;
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
@@ -157,11 +173,17 @@ static int usb_event_handle(uint8_t itf) {
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
proc_packet = driver_process_usb_packet_ccid(rx_read);
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
proc_packet = driver_process_usb_packet_emul(rx_read);
|
||||
#endif
|
||||
if (proc_packet > 0) {
|
||||
card_locked_itf = itf;
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint32_t flag = EV_CMD_AVAILABLE;
|
||||
queue_add_blocking(&usb_to_card_q, &flag);
|
||||
#endif
|
||||
timeout_start();
|
||||
}
|
||||
else {
|
||||
@@ -179,12 +201,15 @@ static int usb_event_handle(uint8_t itf) {
|
||||
|
||||
extern void low_flash_init();
|
||||
void card_init_core1() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
low_flash_init_core1();
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t finished_data_size = 0;
|
||||
|
||||
void card_start(void (*func)(void)) {
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint32_t m = 0;
|
||||
while (queue_is_empty(&usb_to_card_q) == false) {
|
||||
if (queue_try_remove(&usb_to_card_q, &m) == false)
|
||||
@@ -197,17 +222,24 @@ void card_start(void (*func)(void)) {
|
||||
multicore_reset_core1();
|
||||
multicore_launch_core1(func);
|
||||
led_set_blink(BLINK_MOUNTED);
|
||||
#endif
|
||||
}
|
||||
|
||||
void card_exit() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
uint32_t flag = EV_EXIT;
|
||||
queue_try_add(&usb_to_card_q, &flag);
|
||||
led_set_blink(BLINK_SUSPENDED);
|
||||
#endif
|
||||
card_locked_itf = ITF_TOTAL;
|
||||
}
|
||||
extern void hid_task();
|
||||
void usb_task() {
|
||||
#ifndef ENABLE_EMULATION
|
||||
bool mounted = false;
|
||||
#else
|
||||
bool mounted = true;
|
||||
#endif
|
||||
for (uint8_t itf = 0; itf < ITF_TOTAL; itf++) {
|
||||
#ifdef USB_ITF_HID
|
||||
if (itf == ITF_HID)
|
||||
@@ -223,6 +255,7 @@ void usb_task() {
|
||||
|
||||
}
|
||||
usb_write_flush(itf);
|
||||
#ifndef ENABLE_EMULATION
|
||||
if (card_locked_itf == itf) {
|
||||
uint32_t m = 0x0;
|
||||
bool has_m = queue_try_remove(&card_to_usb_q, &m);
|
||||
@@ -263,18 +296,14 @@ void usb_task() {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#ifdef USB_ITF_HID
|
||||
hid_task();
|
||||
#endif
|
||||
}
|
||||
|
||||
void timeout_stop() {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
void timeout_start() {
|
||||
timeout = board_millis();
|
||||
}
|
||||
|
||||
uint8_t *usb_prepare_response(uint8_t itf) {
|
||||
#ifdef USB_ITF_HID
|
||||
@@ -284,6 +313,10 @@ uint8_t *usb_prepare_response(uint8_t itf) {
|
||||
#ifdef USB_ITF_CCID
|
||||
if (itf == ITF_CCID)
|
||||
return driver_prepare_response_ccid();
|
||||
#endif
|
||||
#ifdef ENABLE_EMULATION
|
||||
if (itf == ITF_EMUL)
|
||||
return driver_prepare_response_emul();
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@
|
||||
#ifndef _USB_H_
|
||||
#define _USB_H_
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
#include "pico/util/queue.h"
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
/* USB thread */
|
||||
#define EV_CARD_CHANGE 1
|
||||
@@ -38,12 +42,16 @@
|
||||
|
||||
|
||||
enum {
|
||||
#ifndef ENABLE_EMULATION
|
||||
#ifdef USB_ITF_HID
|
||||
ITF_HID = 0,
|
||||
ITF_KEYBOARD,
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
ITF_CCID,
|
||||
#endif
|
||||
#else
|
||||
ITF_EMUL = 0,
|
||||
#endif
|
||||
ITF_TOTAL
|
||||
};
|
||||
@@ -55,8 +63,10 @@ enum
|
||||
};
|
||||
|
||||
extern void usb_task();
|
||||
#ifndef ENABLE_EMULATION
|
||||
extern queue_t usb_to_card_q;
|
||||
extern queue_t card_to_usb_q;
|
||||
#endif
|
||||
extern uint8_t card_locked_itf;
|
||||
|
||||
#ifdef USB_ITF_HID
|
||||
@@ -83,14 +93,25 @@ extern size_t driver_read_ccid(uint8_t *, size_t);
|
||||
extern int driver_process_usb_nopacket_ccid();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_EMULATION
|
||||
extern int driver_process_usb_packet_emul(uint16_t rx_read);
|
||||
extern void driver_exec_finished_emul(size_t size_next);
|
||||
extern void driver_exec_finished_cont_emul(size_t size_next, size_t offset);
|
||||
extern void driver_exec_timeout_emul();
|
||||
extern bool driver_mounted_emul();
|
||||
extern uint8_t *driver_prepare_response_emul();
|
||||
extern int driver_write_emul(const uint8_t *, size_t);
|
||||
extern size_t driver_read_emul(uint8_t *, size_t);
|
||||
extern int driver_process_usb_nopacket_emul();
|
||||
extern uint16_t emul_read();
|
||||
#endif
|
||||
|
||||
extern size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len);
|
||||
|
||||
extern void card_start(void (*func)(void));
|
||||
extern void card_exit();
|
||||
extern void usb_init();
|
||||
extern uint8_t *usb_prepare_response(uint8_t itf);
|
||||
extern void timeout_stop();
|
||||
extern void timeout_start();
|
||||
extern uint8_t *usb_get_rx(uint8_t itf);
|
||||
extern uint8_t *usb_get_tx(uint8_t itf);
|
||||
extern uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset);
|
||||
|
||||
Reference in New Issue
Block a user