gnuk/src/usb-icc.c

902 lines
24 KiB
C
Raw Normal View History

2010-08-18 03:57:45 +00:00
/*
2010-08-19 08:09:59 +00:00
* usb-icc.c -- USB CCID/ICCD protocol handling
2010-08-18 03:57:45 +00:00
*
2012-01-16 04:24:31 +00:00
* Copyright (C) 2010, 2011, 2012 Free Software Initiative of Japan
2010-08-18 03:57:45 +00:00
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk 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, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk 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/>.
*
*/
2010-09-04 04:48:26 +00:00
#include "config.h"
2010-08-18 05:21:58 +00:00
#include "ch.h"
#include "hal.h"
2010-08-19 08:09:59 +00:00
#include "gnuk.h"
2010-08-18 05:21:58 +00:00
#include "usb_lib.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_istr.h"
2012-01-16 03:17:45 +00:00
#include "usb_lld.h"
2010-08-18 05:21:58 +00:00
2012-01-18 09:59:16 +00:00
struct apdu apdu;
2010-11-12 14:19:27 +00:00
#define ICC_SET_PARAMS 0x61 /* non-ICCD command */
2010-08-23 05:40:33 +00:00
#define ICC_POWER_ON 0x62
#define ICC_POWER_OFF 0x63
2010-11-12 14:19:27 +00:00
#define ICC_SLOT_STATUS 0x65 /* non-ICCD command */
2011-01-04 12:06:55 +00:00
#define ICC_SECURE 0x69 /* non-ICCD command */
2010-11-12 14:19:27 +00:00
#define ICC_GET_PARAMS 0x6C /* non-ICCD command */
2010-08-23 05:40:33 +00:00
#define ICC_XFR_BLOCK 0x6F
#define ICC_DATA_BLOCK_RET 0x80
2010-11-12 14:19:27 +00:00
#define ICC_SLOT_STATUS_RET 0x81 /* non-ICCD result */
#define ICC_PARAMS_RET 0x82 /* non-ICCD result */
2010-08-23 05:40:33 +00:00
#define ICC_MSG_SEQ_OFFSET 6
#define ICC_MSG_STATUS_OFFSET 7
#define ICC_MSG_ERROR_OFFSET 8
#define ICC_MSG_CHAIN_OFFSET 9
2010-12-07 05:34:25 +00:00
#define ICC_MSG_DATA_OFFSET 10 /* == ICC_MSG_HEADER_SIZE */
2010-08-23 05:40:33 +00:00
#define ICC_MAX_MSG_DATA_SIZE (USB_BUF_SIZE - ICC_MSG_HEADER_SIZE)
#define ICC_STATUS_RUN 0x00
#define ICC_STATUS_PRESENT 0x01
#define ICC_STATUS_NOTPRESENT 0x02
#define ICC_CMD_STATUS_OK 0x00
#define ICC_CMD_STATUS_ERROR 0x40
#define ICC_CMD_STATUS_TIMEEXT 0x80
#define ICC_ERROR_XFR_OVERRUN 0xFC
2010-09-16 02:03:14 +00:00
/*
* Since command-byte is at offset 0,
* error with offset 0 means "command not supported".
*/
#define ICC_OFFSET_CMD_NOT_SUPPORTED 0
#define ICC_OFFSET_PARAM 8
2010-08-23 05:40:33 +00:00
struct icc_header {
uint8_t msg_type;
int32_t data_len;
uint8_t slot;
uint8_t seq;
uint8_t rsvd;
uint16_t param;
} __attribute__((packed));
2012-01-18 09:59:16 +00:00
static int icc_data_size;
2010-08-23 05:40:33 +00:00
2010-12-07 05:34:25 +00:00
/*
* USB-ICC communication could be considered "half duplex".
*
2010-12-07 06:52:50 +00:00
* While the device is sending something, there is no possibility for
* the device to receive anything.
*
* While the device is receiving something, there is no possibility
* for the device to send anything.
*
2010-12-07 05:34:25 +00:00
* Thus, the buffer can be shared for RX and TX.
*/
/*
* Buffer of USB communication: for both of RX and TX
*
* The buffer will be filled by multiple RX transactions (Bulk-OUT)
* or will be used for multiple TX transactions (Bulk-IN)
*/
2012-01-18 09:59:16 +00:00
static uint8_t icc_buffer[USB_BUF_SIZE];
static uint8_t icc_seq;
2010-12-07 05:34:25 +00:00
/*
* Pointer to ICC_BUFFER
*/
static uint8_t *icc_next_p;
/*
* Chain pointer: This implementation support two packets in chain (not more)
*/
static uint8_t *icc_chain_p;
/*
* Whole size of TX transfer (Bulk-IN transactions)
*/
2010-08-23 05:40:33 +00:00
static int icc_tx_size;
2011-05-11 07:46:21 +00:00
static Thread *icc_thread;
2010-08-19 08:09:59 +00:00
#define EV_RX_DATA_READY (eventmask_t)1 /* USB Rx data available */
2010-12-07 05:34:25 +00:00
/* EV_EXEC_FINISHED == 2 */
#define EV_TX_FINISHED (eventmask_t)4 /* USB Tx finished */
2010-08-19 08:09:59 +00:00
/*
2010-12-07 05:34:25 +00:00
* Tx done callback
2010-08-19 08:09:59 +00:00
*/
2010-08-18 05:21:58 +00:00
void
2010-09-04 09:44:01 +00:00
EP1_IN_Callback (void)
2010-08-18 05:21:58 +00:00
{
2010-12-07 05:34:25 +00:00
if (icc_next_p == NULL)
2011-05-11 07:46:21 +00:00
/* The sequence of Bulk-IN transactions finished */
chEvtSignalI (icc_thread, EV_TX_FINISHED);
2010-12-07 05:34:25 +00:00
else if (icc_next_p == &icc_buffer[icc_tx_size])
/* It was multiple of USB_LL_BUF_SIZE */
{
/* Send the last 0-DATA transcation of Bulk-IN in the transactions */
icc_next_p = NULL;
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, icc_buffer, 0);
2010-12-07 05:34:25 +00:00
}
else
2010-08-23 05:40:33 +00:00
{
2010-12-07 05:34:25 +00:00
int tx_size = USB_LL_BUF_SIZE;
uint8_t *p = icc_next_p;
icc_next_p += USB_LL_BUF_SIZE;
if (icc_next_p > &icc_buffer[icc_tx_size])
{
icc_next_p = NULL;
tx_size = &icc_buffer[icc_tx_size] - p;
}
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, p, tx_size);
2010-08-23 05:40:33 +00:00
}
2010-12-07 05:34:25 +00:00
}
static void
icc_prepare_receive (int chain)
{
if (chain)
icc_next_p = icc_chain_p;
2010-08-23 05:40:33 +00:00
else
2010-12-07 05:34:25 +00:00
icc_next_p = icc_buffer;
2012-01-16 03:17:45 +00:00
usb_lld_rx_enable (ENDP2);
2010-08-18 05:21:58 +00:00
}
2010-08-19 08:09:59 +00:00
/*
2010-12-07 05:34:25 +00:00
* Rx ready callback
2010-08-19 08:09:59 +00:00
*/
2010-08-18 05:21:58 +00:00
void
2010-09-04 09:44:01 +00:00
EP2_OUT_Callback (void)
2010-08-18 05:21:58 +00:00
{
2010-08-23 05:40:33 +00:00
int len;
2011-12-28 03:27:16 +00:00
struct icc_header *icc_header;
int data_len_so_far;
int data_len;
2010-08-18 05:21:58 +00:00
2012-01-16 04:24:31 +00:00
len = usb_lld_rx_data_len (ENDP2);
2012-01-16 03:17:45 +00:00
usb_lld_rxcpy (icc_next_p, ENDP2, 0, len);
2011-12-28 03:27:16 +00:00
if (len == 0)
{ /* Just ignore Zero Length Packet (ZLP), if any */
2012-01-16 03:17:45 +00:00
usb_lld_rx_enable (ENDP2);
2011-12-28 03:27:16 +00:00
return;
}
icc_next_p += len;
if (icc_chain_p)
icc_header = (struct icc_header *)icc_chain_p;
else
icc_header = (struct icc_header *)icc_buffer;
data_len = icc_header->data_len; /* NOTE: We're little endian */
data_len_so_far = (icc_next_p - (uint8_t *)icc_header) - ICC_MSG_HEADER_SIZE;
2010-08-18 03:57:45 +00:00
2011-12-28 03:27:16 +00:00
if (len == USB_LL_BUF_SIZE
&& data_len != data_len_so_far)
/* The sequence of transactions continues */
2010-12-07 05:34:25 +00:00
{
2012-01-16 03:17:45 +00:00
usb_lld_rx_enable (ENDP2);
2010-12-07 06:52:50 +00:00
if ((icc_next_p - icc_buffer) >= USB_BUF_SIZE)
/* No room to receive any more */
2010-12-07 05:34:25 +00:00
{
DEBUG_INFO ("ERR0F\r\n");
icc_next_p -= USB_LL_BUF_SIZE; /* Just for not overrun the buffer */
2010-12-07 06:52:50 +00:00
/*
* Receive until the end of the sequence
* (and discard the whole block)
*/
2010-12-07 05:34:25 +00:00
}
2011-12-28 03:27:16 +00:00
/*
* NOTE: It is possible a transaction may stall when the size of
* BULK_OUT transaction it's bigger than USB_BUF_SIZE and stops
* with just USB_LL_BUF_SIZE packet. Device will remain waiting
* another packet.
*/
2010-12-07 05:34:25 +00:00
}
else /* Finished */
{
2011-12-28 03:27:16 +00:00
icc_data_size = data_len_so_far;
icc_seq = icc_header->seq; /* NOTE: We're little endian */
2010-08-26 10:50:06 +00:00
2010-12-07 05:34:25 +00:00
if (icc_data_size != data_len)
{
DEBUG_INFO ("ERR0E\r\n");
/* Ignore the whole block */
icc_chain_p = NULL;
icc_prepare_receive (0);
}
else
2011-05-11 07:46:21 +00:00
/* Notify myself */
chEvtSignalI (icc_thread, EV_RX_DATA_READY);
2010-12-07 05:34:25 +00:00
}
2010-08-23 05:40:33 +00:00
}
2010-12-09 01:12:54 +00:00
volatile enum icc_state icc_state;
2010-08-18 05:21:58 +00:00
2010-09-09 16:25:44 +00:00
/*
* ATR (Answer To Reset) string
*
2010-12-12 23:51:48 +00:00
* TS = 0x3b: Direct conversion
2010-12-10 07:31:25 +00:00
* T0 = 0xda: TA1, TC1 and TD1 follow, 10 historical bytes
2010-09-09 16:25:44 +00:00
* TA1 = 0x11: FI=1, DI=1
2010-12-10 07:31:25 +00:00
* TC1 = 0xff
2010-09-09 16:25:44 +00:00
* TD1 = 0x81: TD2 follows, T=1
2010-12-10 07:31:25 +00:00
* TD2 = 0xb1: TA3, TB3 and TD3 follow, T=1
2010-09-09 16:25:44 +00:00
* TA3 = 0xFE: IFSC = 254 bytes
* TB3 = 0x55: BWI = 5, CWI = 5 (BWT timeout 3.2 sec)
2010-12-10 07:31:25 +00:00
* TD3 = 0x1f: TA4 follows, T=15
* TA4 = 0x03: 5V or 3.3V
* Historical bytes: to be explained...
2010-09-09 16:25:44 +00:00
* XOR check
*
2010-12-14 08:36:12 +00:00
* Minimum: 0x3b, 0x8a, 0x80, 0x01, + historical bytes, xor check
*
2010-09-09 16:25:44 +00:00
*/
static const char ATR[] = {
2010-12-10 07:31:25 +00:00
0x3b, 0xda, 0x11, 0xff, 0x81, 0xb1, 0xfe, 0x55, 0x1f, 0x03,
0x00,
0x31, 0x84, /* full DF name, GET DATA, MF */
0x73,
0x80, /* DF full name */
0x01, /* 1-byte */
0x40, /* Extended Lc and extended Le */
0x00,
0x90, 0x00,
(0xda^0x11^0xff^0x81^0xb1^0xfe^0x55^0x1f^0x03
^0x00^0x31^0x84^0x73^0x80^0x01^0x40^0x00^0x90^0x00)
2010-09-09 16:25:44 +00:00
};
2010-08-18 08:02:04 +00:00
2010-09-16 02:03:14 +00:00
/*
* Send back error
*/
void
icc_error (int offset)
{
2010-12-07 05:34:25 +00:00
uint8_t *icc_reply;
if (icc_chain_p)
icc_reply = icc_chain_p;
else
icc_reply = icc_buffer;
icc_reply[0] = ICC_SLOT_STATUS_RET; /* Any value should be OK */
icc_reply[1] = 0x00;
icc_reply[2] = 0x00;
icc_reply[3] = 0x00;
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
2010-09-16 02:03:14 +00:00
if (icc_state == ICC_STATE_START)
/* 1: ICC present but not activated 2: No ICC present */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
2010-09-16 02:03:14 +00:00
else
/* An ICC is present and active */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_STATUS_OFFSET] |= ICC_CMD_STATUS_ERROR; /* Failed */
icc_reply[ICC_MSG_ERROR_OFFSET] = offset;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
icc_next_p = NULL; /* This is a single transaction Bulk-IN */
icc_tx_size = ICC_MSG_HEADER_SIZE;
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, icc_reply, icc_tx_size);
2010-09-16 02:03:14 +00:00
}
2010-12-08 05:10:30 +00:00
static Thread *gpg_thread;
static WORKING_AREA(waGPGthread, 128*16);
extern msg_t GPGthread (void *arg);
2010-08-19 08:09:59 +00:00
/* Send back ATR (Answer To Reset) */
enum icc_state
2010-08-23 05:40:33 +00:00
icc_power_on (void)
2010-08-18 08:02:04 +00:00
{
2010-08-23 05:40:33 +00:00
int size_atr;
2010-08-18 08:02:04 +00:00
2010-12-08 05:10:30 +00:00
if (gpg_thread == NULL)
2011-05-11 07:46:21 +00:00
gpg_thread = chThdCreateStatic (waGPGthread, sizeof(waGPGthread),
NORMALPRIO, GPGthread, (void *)icc_thread);
2010-12-08 05:10:30 +00:00
2010-08-18 08:02:04 +00:00
size_atr = sizeof (ATR);
2010-12-07 06:52:50 +00:00
icc_buffer[0] = ICC_DATA_BLOCK_RET;
icc_buffer[1] = size_atr;
icc_buffer[2] = 0x00;
icc_buffer[3] = 0x00;
icc_buffer[4] = 0x00;
icc_buffer[5] = 0x00; /* Slot */
icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
icc_buffer[ICC_MSG_STATUS_OFFSET] = 0x00;
icc_buffer[ICC_MSG_ERROR_OFFSET] = 0x00;
icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x00;
memcpy (&icc_buffer[ICC_MSG_DATA_OFFSET], ATR, size_atr);
2010-08-23 05:40:33 +00:00
2010-12-07 05:34:25 +00:00
icc_next_p = NULL; /* This is a single transaction Bulk-IN */
icc_tx_size = ICC_MSG_HEADER_SIZE + size_atr;
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, icc_buffer, icc_tx_size);
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("ON\r\n");
2010-08-23 05:40:33 +00:00
2010-08-19 08:09:59 +00:00
return ICC_STATE_WAIT;
2010-08-18 08:02:04 +00:00
}
2010-08-19 08:09:59 +00:00
static void
2010-08-23 05:40:33 +00:00
icc_send_status (void)
2010-08-18 08:02:04 +00:00
{
2010-12-07 05:34:25 +00:00
uint8_t *icc_reply;
if (icc_chain_p)
icc_reply = icc_chain_p;
else
icc_reply = icc_buffer;
icc_reply[0] = ICC_SLOT_STATUS_RET;
icc_reply[1] = 0x00;
icc_reply[2] = 0x00;
icc_reply[3] = 0x00;
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
icc_reply[ICC_MSG_SEQ_OFFSET] = icc_seq;
2010-08-19 08:09:59 +00:00
if (icc_state == ICC_STATE_START)
2010-08-23 05:40:33 +00:00
/* 1: ICC present but not activated 2: No ICC present */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
2010-08-19 08:09:59 +00:00
else
2010-08-23 05:40:33 +00:00
/* An ICC is present and active */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
icc_next_p = NULL; /* This is a single transaction Bulk-IN */
icc_tx_size = ICC_MSG_HEADER_SIZE;
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, icc_reply, icc_tx_size);
2010-08-18 08:02:04 +00:00
2010-09-05 16:55:29 +00:00
#ifdef DEBUG_MORE
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("St\r\n");
2010-09-05 16:55:29 +00:00
#endif
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
2010-08-19 08:09:59 +00:00
enum icc_state
2010-08-23 05:40:33 +00:00
icc_power_off (void)
2010-08-19 08:09:59 +00:00
{
2010-12-09 01:12:54 +00:00
icc_data_size = 0;
2010-12-08 05:10:30 +00:00
if (gpg_thread)
{
chThdTerminate (gpg_thread);
2011-12-01 09:23:10 +00:00
chEvtSignal (gpg_thread, EV_NOP);
2010-12-08 05:10:30 +00:00
chThdWait (gpg_thread);
gpg_thread = NULL;
}
2011-01-28 08:38:52 +00:00
icc_state = ICC_STATE_START; /* This status change should be here */
2010-08-23 05:40:33 +00:00
icc_send_status ();
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("OFF\r\n");
2010-08-19 08:09:59 +00:00
return ICC_STATE_START;
2010-08-18 08:02:04 +00:00
}
2010-08-23 05:40:33 +00:00
static void
2011-01-11 00:16:40 +00:00
icc_send_data_block (int len, uint8_t status, uint8_t chain)
2010-08-23 05:40:33 +00:00
{
2010-12-07 05:34:25 +00:00
int tx_size = USB_LL_BUF_SIZE;
2011-01-11 00:16:40 +00:00
uint8_t *p;
2010-12-07 05:34:25 +00:00
2011-01-11 00:16:40 +00:00
if (icc_chain_p)
p = icc_chain_p;
else
p = icc_buffer;
p[0] = ICC_DATA_BLOCK_RET;
p[1] = len & 0xFF;
p[2] = (len >> 8)& 0xFF;
p[3] = (len >> 16)& 0xFF;
p[4] = (len >> 24)& 0xFF;
p[5] = 0x00; /* Slot */
p[ICC_MSG_SEQ_OFFSET] = icc_seq;
p[ICC_MSG_STATUS_OFFSET] = status;
p[ICC_MSG_ERROR_OFFSET] = 0;
p[ICC_MSG_CHAIN_OFFSET] = chain;
2010-12-07 05:34:25 +00:00
icc_tx_size = ICC_MSG_HEADER_SIZE + len;
2011-01-11 00:16:40 +00:00
if (icc_tx_size < USB_LL_BUF_SIZE)
2010-12-07 05:34:25 +00:00
{
icc_next_p = NULL;
2011-01-11 00:16:40 +00:00
tx_size = icc_tx_size;
2010-08-23 05:40:33 +00:00
}
else
2011-01-11 00:16:40 +00:00
icc_next_p = p + USB_LL_BUF_SIZE;
2010-12-07 05:34:25 +00:00
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, p, tx_size);
2010-09-09 16:25:44 +00:00
#ifdef DEBUG_MORE
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("DATA\r\n");
2010-09-09 16:25:44 +00:00
#endif
}
static void
icc_send_params (void)
{
2010-12-07 06:52:50 +00:00
icc_buffer[0] = ICC_PARAMS_RET;
icc_buffer[1] = 0x07; /* Length = 0x00000007 */
icc_buffer[2] = 0;
icc_buffer[3] = 0;
icc_buffer[4] = 0;
icc_buffer[5] = 0x00; /* Slot */
icc_buffer[ICC_MSG_SEQ_OFFSET] = icc_seq;
icc_buffer[ICC_MSG_STATUS_OFFSET] = 0;
icc_buffer[ICC_MSG_ERROR_OFFSET] = 0;
icc_buffer[ICC_MSG_CHAIN_OFFSET] = 0x01; /* ProtocolNum: T=1 */
icc_buffer[ICC_MSG_DATA_OFFSET] = 0x11; /* bmFindexDindex */
icc_buffer[ICC_MSG_DATA_OFFSET+1] = 0x11; /* bmTCCKST1 */
icc_buffer[ICC_MSG_DATA_OFFSET+2] = 0xFE; /* bGuardTimeT1 */
icc_buffer[ICC_MSG_DATA_OFFSET+3] = 0x55; /* bmWaitingIntegersT1 */
icc_buffer[ICC_MSG_DATA_OFFSET+4] = 0x03; /* bClockStop */
icc_buffer[ICC_MSG_DATA_OFFSET+5] = 0xFE; /* bIFSC */
icc_buffer[ICC_MSG_DATA_OFFSET+6] = 0; /* bNadValue */
2010-09-09 16:25:44 +00:00
2010-12-07 05:34:25 +00:00
icc_next_p = NULL; /* This is a single transaction Bulk-IN */
icc_tx_size = ICC_MSG_HEADER_SIZE + 7;
2012-01-16 03:17:45 +00:00
usb_lld_write (ENDP1, icc_buffer, icc_tx_size);
2010-09-05 16:55:29 +00:00
#ifdef DEBUG_MORE
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("DATA\r\n");
2010-09-05 16:55:29 +00:00
#endif
2010-08-23 05:40:33 +00:00
}
2011-01-11 00:16:40 +00:00
/* Supporting smaller buffer of libccid (<= 1.3.11) */
#define ICC_RESPONSE_MSG_DATA_SIZE 262
2011-01-04 12:06:55 +00:00
2010-08-19 08:09:59 +00:00
static enum icc_state
icc_handle_data (void)
2010-08-18 05:21:58 +00:00
{
2010-08-19 08:09:59 +00:00
enum icc_state next_state = icc_state;
2010-12-07 05:34:25 +00:00
struct icc_header *icc_header;
if (icc_chain_p)
icc_header = (struct icc_header *)icc_chain_p;
else
icc_header = (struct icc_header *)icc_buffer;
2010-08-18 08:02:04 +00:00
2010-08-19 08:09:59 +00:00
switch (icc_state)
{
case ICC_STATE_START:
2010-08-23 05:40:33 +00:00
if (icc_header->msg_type == ICC_POWER_ON)
next_state = icc_power_on ();
else if (icc_header->msg_type == ICC_POWER_OFF)
next_state = icc_power_off ();
else if (icc_header->msg_type == ICC_SLOT_STATUS)
icc_send_status ();
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR01\r\n");
2010-09-16 02:03:14 +00:00
icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
2010-08-19 08:09:59 +00:00
}
break;
case ICC_STATE_WAIT:
2010-08-23 05:40:33 +00:00
if (icc_header->msg_type == ICC_POWER_ON)
2010-09-09 16:25:44 +00:00
/* Not in the spec., but pcscd/libccid */
2010-08-23 05:40:33 +00:00
next_state = icc_power_on ();
else if (icc_header->msg_type == ICC_POWER_OFF)
next_state = icc_power_off ();
else if (icc_header->msg_type == ICC_SLOT_STATUS)
icc_send_status ();
else if (icc_header->msg_type == ICC_XFR_BLOCK)
2010-08-18 08:02:04 +00:00
{
2010-08-23 05:40:33 +00:00
if (icc_header->param == 0)
{ /* Give this message to GPG thread */
2012-01-18 09:59:16 +00:00
run_gpg_thread:
apdu.seq = icc_seq;
apdu.cmd_apdu_head = &icc_buffer[ICC_MSG_HEADER_SIZE];
apdu.sw = 0x9000;
apdu.res_apdu_data_len = 0;
apdu.res_apdu_data = &icc_buffer[ICC_MSG_HEADER_SIZE];
if (icc_data_size < 4)
{
icc_error (ICC_MSG_HEADER_SIZE + icc_data_size);
next_state = ICC_STATE_WAIT;
break;
}
else if (icc_data_size == 4)
{ /* No Lc and No Le */
apdu.cmd_apdu_data = NULL;
apdu.cmd_apdu_data_len = 0;
apdu.expected_res_size = 0;
apdu.cmd_apdu_head[4] = 0;
}
else if (icc_data_size == 5)
{ /* No Lc but Le */
apdu.cmd_apdu_data = NULL;
apdu.cmd_apdu_data_len = 0;
apdu.expected_res_size = apdu.cmd_apdu_head[4];
if (apdu.expected_res_size == 0)
apdu.expected_res_size = 256;
apdu.cmd_apdu_head[4] = 0;
}
else if (apdu.cmd_apdu_head[4] == 0)
{ /* extended Lc or extended Le */
apdu.cmd_apdu_data = apdu.cmd_apdu_head + 7;
apdu.cmd_apdu_data_len
= (apdu.cmd_apdu_head[5] << 8) + apdu.cmd_apdu_head[6];
if (icc_data_size == 7)
{ /* No Lc but extended Le */
apdu.expected_res_size = apdu.cmd_apdu_data_len;
apdu.cmd_apdu_data_len = 0;
}
else if (icc_data_size == apdu.cmd_apdu_data_len + 7)
apdu.expected_res_size = 0;
else if (icc_data_size == apdu.cmd_apdu_data_len + 7 + 2)
{
apdu.expected_res_size
= (icc_buffer[ICC_MSG_HEADER_SIZE + 7
+ apdu.cmd_apdu_data_len] << 8)
| icc_buffer[ICC_MSG_HEADER_SIZE + 7
+ apdu.cmd_apdu_data_len + 1];
if (apdu.expected_res_size == 0)
apdu.expected_res_size = 65536;
}
else
{
icc_error (ICC_MSG_HEADER_SIZE + 5);
next_state = ICC_STATE_WAIT;
break;
}
}
else /* short Lc */
{
apdu.cmd_apdu_data = apdu.cmd_apdu_head + 5;
apdu.cmd_apdu_data_len = apdu.cmd_apdu_head[4];
if (icc_data_size == apdu.cmd_apdu_data_len + 5)
apdu.expected_res_size = 0;
else if (icc_data_size == apdu.cmd_apdu_data_len + 5 + 1)
{
apdu.expected_res_size
= icc_buffer[ICC_MSG_HEADER_SIZE + 5
+ apdu.cmd_apdu_data_len];
if (apdu.expected_res_size == 0)
apdu.expected_res_size = 256;
}
else
{
icc_error (ICC_MSG_HEADER_SIZE + 5);
next_state = ICC_STATE_WAIT;
break;
}
}
2011-12-01 09:23:10 +00:00
chEvtSignal (gpg_thread, EV_CMD_AVAILABLE);
2010-08-23 05:40:33 +00:00
next_state = ICC_STATE_EXECUTE;
2010-08-19 08:09:59 +00:00
}
2010-08-23 05:40:33 +00:00
else if (icc_header->param == 1)
2010-08-19 08:09:59 +00:00
{
2010-12-07 05:34:25 +00:00
icc_chain_p = icc_next_p;
2011-01-11 00:16:40 +00:00
icc_send_data_block (0, 0, 0x10);
2010-08-19 08:09:59 +00:00
next_state = ICC_STATE_RECEIVE;
}
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR02\r\n");
2010-09-16 02:03:14 +00:00
icc_error (ICC_OFFSET_PARAM);
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
}
2010-11-12 14:19:27 +00:00
else if (icc_header->msg_type == ICC_SET_PARAMS
|| icc_header->msg_type == ICC_GET_PARAMS)
2010-09-09 16:25:44 +00:00
icc_send_params ();
2011-01-04 12:06:55 +00:00
else if (icc_header->msg_type == ICC_SECURE)
{
2011-01-19 05:36:04 +00:00
if (icc_buffer[10] == 0x00) /* PIN verification */
{
2012-01-18 09:59:16 +00:00
icc_buffer[ICC_MSG_HEADER_SIZE+0] = icc_buffer[25];
icc_buffer[ICC_MSG_HEADER_SIZE+1] = icc_buffer[26];
icc_buffer[ICC_MSG_HEADER_SIZE+2] = icc_buffer[27];
icc_buffer[ICC_MSG_HEADER_SIZE+3] = icc_buffer[28];
/**/
icc_buffer[ICC_MSG_HEADER_SIZE+5] = 0; /* bConfirmPIN */
icc_buffer[ICC_MSG_HEADER_SIZE+6] = icc_buffer[17]; /* bEntryValidationCondition */
icc_buffer[ICC_MSG_HEADER_SIZE+7] = icc_buffer[18]; /* bNumberMessage */
icc_buffer[ICC_MSG_HEADER_SIZE+8] = icc_buffer[19]; /* wLangId L */
icc_buffer[ICC_MSG_HEADER_SIZE+9] = icc_buffer[20]; /* wLangId H */
icc_buffer[ICC_MSG_HEADER_SIZE+10] = icc_buffer[21]; /* bMsgIndex */
apdu.seq = icc_seq;
apdu.cmd_apdu_head = &icc_buffer[ICC_MSG_HEADER_SIZE+0];
apdu.cmd_apdu_data = &icc_buffer[ICC_MSG_HEADER_SIZE+5];
apdu.cmd_apdu_data_len = 6;
apdu.expected_res_size = 0;
apdu.sw = 0x9000;
apdu.res_apdu_data_len = 0;
apdu.res_apdu_data = &icc_buffer[ICC_MSG_HEADER_SIZE];
2011-12-01 09:23:10 +00:00
chEvtSignal (gpg_thread, EV_VERIFY_CMD_AVAILABLE);
2011-01-19 05:36:04 +00:00
next_state = ICC_STATE_EXECUTE;
}
else if (icc_buffer[10] == 0x01) /* PIN Modification */
{
uint8_t num_msgs = icc_buffer[21];
if (num_msgs == 0x00)
num_msgs = 1;
else if (num_msgs == 0xff)
num_msgs = 3;
2012-01-18 09:59:16 +00:00
icc_buffer[ICC_MSG_HEADER_SIZE+0] = icc_buffer[27 + num_msgs];
icc_buffer[ICC_MSG_HEADER_SIZE+1] = icc_buffer[28 + num_msgs];
icc_buffer[ICC_MSG_HEADER_SIZE+2] = icc_buffer[29 + num_msgs];
icc_buffer[ICC_MSG_HEADER_SIZE+3] = icc_buffer[30 + num_msgs];
/**/
icc_buffer[ICC_MSG_HEADER_SIZE+5] = icc_buffer[19]; /* bConfirmPIN */
icc_buffer[ICC_MSG_HEADER_SIZE+6] = icc_buffer[20]; /* bEntryValidationCondition */
icc_buffer[ICC_MSG_HEADER_SIZE+7] = icc_buffer[21]; /* bNumberMessage */
icc_buffer[ICC_MSG_HEADER_SIZE+8] = icc_buffer[22]; /* wLangId L */
icc_buffer[ICC_MSG_HEADER_SIZE+9] = icc_buffer[23]; /* wLangId H */
icc_buffer[ICC_MSG_HEADER_SIZE+10] = icc_buffer[24]; /* bMsgIndex, bMsgIndex1 */
2011-12-07 00:38:48 +00:00
if (num_msgs >= 2)
2012-01-18 09:59:16 +00:00
icc_buffer[ICC_MSG_HEADER_SIZE+11] = icc_buffer[25]; /* bMsgIndex2 */
2011-12-07 00:38:48 +00:00
if (num_msgs == 3)
2012-01-18 09:59:16 +00:00
icc_buffer[ICC_MSG_HEADER_SIZE+12] = icc_buffer[26]; /* bMsgIndex3 */
apdu.seq = icc_seq;
apdu.cmd_apdu_head = &icc_buffer[ICC_MSG_HEADER_SIZE+0];
apdu.cmd_apdu_data = &icc_buffer[ICC_MSG_HEADER_SIZE+5];
apdu.cmd_apdu_data_len = 5 + num_msgs;
apdu.expected_res_size = 0;
apdu.sw = 0x9000;
apdu.res_apdu_data_len = 0;
apdu.res_apdu_data = &icc_buffer[ICC_MSG_HEADER_SIZE];
2011-12-01 09:23:10 +00:00
chEvtSignal (gpg_thread, EV_MODIFY_CMD_AVAILABLE);
2011-01-19 05:36:04 +00:00
next_state = ICC_STATE_EXECUTE;
}
else
icc_error (ICC_MSG_DATA_OFFSET);
2011-01-04 12:06:55 +00:00
}
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR03\r\n");
DEBUG_BYTE (icc_header->msg_type);
2010-09-16 02:03:14 +00:00
icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
2010-08-19 08:09:59 +00:00
}
break;
case ICC_STATE_RECEIVE:
2010-12-08 05:10:30 +00:00
if (icc_header->msg_type == ICC_POWER_OFF)
{
icc_chain_p = NULL;
next_state = icc_power_off ();
}
else if (icc_header->msg_type == ICC_SLOT_STATUS)
2010-08-23 05:40:33 +00:00
icc_send_status ();
else if (icc_header->msg_type == ICC_XFR_BLOCK)
2010-08-18 08:02:04 +00:00
{
2010-12-07 05:34:25 +00:00
if (icc_header->param == 2) /* Got the final block */
{ /* Give this message to GPG thread */
int len = icc_next_p - icc_chain_p - ICC_MSG_HEADER_SIZE;
memmove (icc_chain_p, icc_chain_p + ICC_MSG_HEADER_SIZE, len);
icc_next_p -= ICC_MSG_HEADER_SIZE;
icc_data_size = icc_next_p - icc_buffer - ICC_MSG_HEADER_SIZE;
icc_chain_p = NULL;
2012-01-18 09:59:16 +00:00
goto run_gpg_thread;
2010-08-23 05:40:33 +00:00
}
2010-12-07 05:34:25 +00:00
else /* icc_header->param == 3 is not supported. */
2010-08-23 05:40:33 +00:00
{
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("ERR08\r\n");
icc_error (ICC_OFFSET_PARAM);
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
}
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR05\r\n");
DEBUG_BYTE (icc_header->msg_type);
2010-12-07 05:34:25 +00:00
icc_chain_p = NULL;
2010-09-16 02:03:14 +00:00
icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
next_state = ICC_STATE_WAIT;
2010-08-19 08:09:59 +00:00
}
break;
2010-12-07 05:34:25 +00:00
case ICC_STATE_EXECUTE:
2010-08-23 05:40:33 +00:00
if (icc_header->msg_type == ICC_POWER_OFF)
2010-12-08 05:10:30 +00:00
next_state = icc_power_off ();
2010-12-07 05:34:25 +00:00
else if (icc_header->msg_type == ICC_SLOT_STATUS)
icc_send_status ();
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("ERR04\r\n");
2010-09-03 15:42:36 +00:00
DEBUG_BYTE (icc_header->msg_type);
2010-09-16 02:03:14 +00:00
icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
2010-08-18 08:02:04 +00:00
}
2010-08-19 08:09:59 +00:00
break;
2011-01-11 00:16:40 +00:00
case ICC_STATE_SEND:
if (icc_header->msg_type == ICC_POWER_OFF)
next_state = icc_power_off ();
else if (icc_header->msg_type == ICC_SLOT_STATUS)
icc_send_status ();
else if (icc_header->msg_type == ICC_XFR_BLOCK)
{
if (icc_header->param == 0x10)
{
2012-01-18 09:59:16 +00:00
int len = apdu.res_apdu_data_len;
if (apdu.res_apdu_data == NULL)
{ /* send last byte of 0x9000 */
icc_buffer[ICC_MSG_HEADER_SIZE] = 0x00;
len = 1;
}
else if (apdu.res_apdu_data != &icc_buffer[ICC_MSG_HEADER_SIZE])
2011-01-27 09:17:01 +00:00
{
2012-01-18 09:59:16 +00:00
if (apdu.res_apdu_data_len >= ICC_RESPONSE_MSG_DATA_SIZE)
{
memcpy (&icc_buffer[ICC_MSG_HEADER_SIZE],
apdu.res_apdu_data, ICC_RESPONSE_MSG_DATA_SIZE);
apdu.res_apdu_data += ICC_RESPONSE_MSG_DATA_SIZE;
}
else if (apdu.res_apdu_data_len
<= ICC_RESPONSE_MSG_DATA_SIZE - 2)
{
memcpy (&icc_buffer[ICC_MSG_HEADER_SIZE],
apdu.res_apdu_data, apdu.res_apdu_data_len);
icc_buffer[ICC_MSG_HEADER_SIZE+apdu.res_apdu_data_len]
= 0x90;
icc_buffer[ICC_MSG_HEADER_SIZE+apdu.res_apdu_data_len+1]
= 0x00;
apdu.res_apdu_data = NULL;
len += 2;
}
else if (apdu.res_apdu_data_len
== ICC_RESPONSE_MSG_DATA_SIZE - 1)
{
memcpy (&icc_buffer[ICC_MSG_HEADER_SIZE],
apdu.res_apdu_data, apdu.res_apdu_data_len);
icc_buffer[ICC_MSG_HEADER_SIZE+apdu.res_apdu_data_len]
= 0x90;
apdu.res_apdu_data = NULL;
len += 1;
}
2011-01-27 09:17:01 +00:00
}
else
2012-01-18 09:59:16 +00:00
memmove (&icc_buffer[ICC_MSG_HEADER_SIZE],
&icc_buffer[ICC_MSG_HEADER_SIZE]
+ICC_RESPONSE_MSG_DATA_SIZE,
apdu.res_apdu_data_len);
2011-01-27 09:17:01 +00:00
2012-01-18 09:59:16 +00:00
if (len <= ICC_RESPONSE_MSG_DATA_SIZE)
2011-01-11 00:16:40 +00:00
{
2012-01-18 09:59:16 +00:00
icc_send_data_block (len, 0, 0x02);
2011-01-11 00:16:40 +00:00
next_state = ICC_STATE_WAIT;
}
else
{
icc_send_data_block (ICC_RESPONSE_MSG_DATA_SIZE, 0, 0x03);
2012-01-18 09:59:16 +00:00
apdu.res_apdu_data_len -= ICC_RESPONSE_MSG_DATA_SIZE;
2011-01-11 00:16:40 +00:00
}
}
else
{
DEBUG_INFO ("ERR0A\r\n");
DEBUG_BYTE (icc_header->param >> 8);
DEBUG_BYTE (icc_header->param & 0xff);
icc_error (ICC_OFFSET_PARAM);
next_state = ICC_STATE_WAIT;
}
}
else
{
DEBUG_INFO ("ERR06\r\n");
DEBUG_BYTE (icc_header->msg_type);
icc_error (ICC_OFFSET_CMD_NOT_SUPPORTED);
next_state = ICC_STATE_WAIT;
}
2011-01-15 12:49:17 +00:00
break;
2010-12-07 05:34:25 +00:00
default:
next_state = ICC_STATE_START;
DEBUG_INFO ("ERR10\r\n");
break;
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
2010-08-19 08:09:59 +00:00
return next_state;
}
static enum icc_state
icc_handle_timeout (void)
{
enum icc_state next_state = icc_state;
2010-08-23 05:40:33 +00:00
switch (icc_state)
2010-08-19 08:09:59 +00:00
{
2010-08-23 05:40:33 +00:00
case ICC_STATE_EXECUTE:
2011-01-11 00:16:40 +00:00
icc_send_data_block (0, ICC_CMD_STATUS_TIMEEXT, 0);
2010-08-23 05:40:33 +00:00
break;
2010-12-07 05:34:25 +00:00
default:
2010-08-23 05:40:33 +00:00
break;
2010-08-19 08:09:59 +00:00
}
return next_state;
}
2010-09-08 05:24:12 +00:00
#define USB_ICC_TIMEOUT MS2ST(1950)
2010-08-19 08:09:59 +00:00
msg_t
USBthread (void *arg)
{
(void)arg;
2011-05-11 07:46:21 +00:00
icc_thread = chThdSelf ();
2010-08-19 08:09:59 +00:00
chEvtClear (ALL_EVENTS);
icc_state = ICC_STATE_START;
2010-12-07 05:34:25 +00:00
icc_prepare_receive (0);
2010-08-19 08:09:59 +00:00
while (1)
{
eventmask_t m;
m = chEvtWaitOneTimeout (ALL_EVENTS, USB_ICC_TIMEOUT);
if (m == EV_RX_DATA_READY)
icc_state = icc_handle_data ();
else if (m == EV_EXEC_FINISHED)
2011-01-28 08:38:52 +00:00
if (icc_state == ICC_STATE_EXECUTE)
{
2012-01-18 09:59:16 +00:00
if (apdu.res_apdu_data != &icc_buffer[ICC_MSG_HEADER_SIZE])
{
/* Assume that
* apdu.res_apdu_data_len > ICC_RESPONSE_MSG_DATA_SIZE
*/
memcpy (&icc_buffer[ICC_MSG_HEADER_SIZE],
apdu.res_apdu_data, ICC_RESPONSE_MSG_DATA_SIZE);
apdu.res_apdu_data += ICC_RESPONSE_MSG_DATA_SIZE;
}
else
2011-01-28 08:38:52 +00:00
{
2012-01-18 09:59:16 +00:00
icc_buffer[ICC_MSG_HEADER_SIZE+apdu.res_apdu_data_len] = (apdu.sw >> 8);
icc_buffer[ICC_MSG_HEADER_SIZE+apdu.res_apdu_data_len+1] = (apdu.sw & 0xff);
apdu.res_apdu_data_len += 2;
2011-01-28 08:38:52 +00:00
}
2012-01-18 09:59:16 +00:00
if (apdu.res_apdu_data_len <= ICC_RESPONSE_MSG_DATA_SIZE)
2011-01-28 08:38:52 +00:00
{
2012-01-18 09:59:16 +00:00
icc_send_data_block (apdu.res_apdu_data_len, 0, 0);
2011-01-28 08:38:52 +00:00
icc_state = ICC_STATE_WAIT;
}
else
{
icc_send_data_block (ICC_RESPONSE_MSG_DATA_SIZE, 0, 0x01);
2012-01-18 09:59:16 +00:00
apdu.res_apdu_data_len -= ICC_RESPONSE_MSG_DATA_SIZE;
2011-01-28 08:38:52 +00:00
icc_state = ICC_STATE_SEND;
}
}
else
2012-01-18 09:59:16 +00:00
{ /* error */
2011-01-28 08:38:52 +00:00
DEBUG_INFO ("ERR07\r\n");
}
2010-12-07 05:34:25 +00:00
else if (m == EV_TX_FINISHED)
{
2011-01-11 00:16:40 +00:00
if (icc_state == ICC_STATE_START || icc_state == ICC_STATE_WAIT
|| icc_state == ICC_STATE_SEND)
2010-12-07 05:34:25 +00:00
icc_prepare_receive (0);
else if (icc_state == ICC_STATE_RECEIVE)
icc_prepare_receive (1);
}
2010-08-19 08:09:59 +00:00
else /* Timeout */
icc_state = icc_handle_timeout ();
2010-08-18 05:21:58 +00:00
}
return 0;
}