From 0ff987061297d497de156680502bf3b91f17ca2e Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 18 May 2019 21:26:18 -0400 Subject: [PATCH 01/42] add interface descriptor --- targets/stm32l432/lib/usbd/usbd_composite.c | 79 ++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index a1a868c..ed41812 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -26,8 +26,10 @@ static uint8_t *USBD_Composite_GetOtherSpeedCfgDesc (uint16_t *length); static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); -#define NUM_INTERFACES 2 +#define NUM_INTERFACES 3 +#if NUM_INTERFACES>2 +#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90) #if NUM_INTERFACES>1 #define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90) #else @@ -168,6 +170,81 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x00, /* bInterval: ignore for Bulk transfer */ #endif +#if NUM_INTERFACES>2 + +/* CCID Interface Descriptor */ +9, /* bLength: Interface Descriptor size */ +USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ +0, /* bInterfaceNumber: CCID Interface */ +0, /* Alternate setting for this interface */ +3, /* bNumEndpoints: Bulk-IN, Bulk-OUT, Intr-IN */ +0x0B, /* CCID class */ +0x00, /* CCID subclass */ +0x00, /* CCID protocol */ +0, /* string index for interface */ + +/* ICC Descriptor */ +54, /* bLength: */ +0x21, /* bDescriptorType: USBDESCR_ICC */ +0x10, 0x01, /* bcdCCID: revision 1.1 (of CCID) */ +0, /* bMaxSlotIndex: */ +1, /* bVoltageSupport: 5V-only */ +0x02, 0, 0, 0, /* dwProtocols: T=1 */ +0xa0, 0x0f, 0, 0, /* dwDefaultClock: 4000 */ +0xa0, 0x0f, 0, 0, /* dwMaximumClock: 4000 */ +0, /* bNumClockSupported: 0x00 */ +0x80, 0x25, 0, 0, /* dwDataRate: 9600 */ +0x80, 0x25, 0, 0, /* dwMaxDataRate: 9600 */ +0, /* bNumDataRateSupported: 0x00 */ +0xfe, 0, 0, 0, /* dwMaxIFSD: 254 */ +0, 0, 0, 0, /* dwSynchProtocols: 0 */ +0, 0, 0, 0, /* dwMechanical: 0 */ +0x7a, 0x04, 0x02, 0x00, /* dwFeatures: + * Short and extended APDU level: 0x40000 ---- + * Short APDU level : 0x20000 * + * (ICCD?) : 0x00800 ---- + * Automatic IFSD : 0x00400 * + * NAD value other than 0x00 : 0x00200 + * Can set ICC in clock stop : 0x00100 + * Automatic PPS CUR : 0x00080 + * Automatic PPS PROP : 0x00040 * + * Auto baud rate change : 0x00020 * + * Auto clock change : 0x00010 * + * Auto voltage selection : 0x00008 * + * Auto activaction of ICC : 0x00004 + * Automatic conf. based on ATR : 0x00002 * + */ +0x0f, 0x01, 0, 0, /* dwMaxCCIDMessageLength: 271 */ +0xff, /* bClassGetResponse: 0xff */ +0x00, /* bClassEnvelope: 0 */ +0, 0, /* wLCDLayout: 0 */ +0, /* bPinSupport: No PIN pad */ + +1, /* bMaxCCIDBusySlots: 1 */ +/*Endpoint IN1 Descriptor*/ +7, /* bLength: Endpoint Descriptor size */ +USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ +0x81, /* bEndpointAddress: (IN1) */ +0x02, /* bmAttributes: Bulk */ +HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ +0x00, /* bInterval */ +/*Endpoint OUT1 Descriptor*/ +7, /* bLength: Endpoint Descriptor size */ +USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ +0x01, /* bEndpointAddress: (OUT1) */ +0x02, /* bmAttributes: Bulk */ +HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ +0x00, /* bInterval */ +/*Endpoint IN2 Descriptor*/ +7, /* bLength: Endpoint Descriptor size */ +USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ +0x82, /* bEndpointAddress: (IN2) */ +0x03, /* bmAttributes: Interrupt */ +0x04, 0x00, /* wMaxPacketSize: 4 */ +0xFF, /* bInterval (255ms) */ + +#endif + }; From 4fad28ea470aff8656a5665c41138e67890e7a26 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 18 May 2019 21:47:51 -0400 Subject: [PATCH 02/42] compile new class --- targets/stm32l432/lib/usbd/usbd_ccid.h | 8 ++++++++ targets/stm32l432/lib/usbd/usbd_composite.c | 11 ++++++----- targets/stm32l432/lib/usbd/usbd_conf.c | 16 +++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 targets/stm32l432/lib/usbd/usbd_ccid.h diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.h b/targets/stm32l432/lib/usbd/usbd_ccid.h new file mode 100644 index 0000000..ce38be1 --- /dev/null +++ b/targets/stm32l432/lib/usbd/usbd_ccid.h @@ -0,0 +1,8 @@ +#ifndef _USBD_H_ +#define _USBD_H_ + +#define CCID_IN_EP 0x84U /* EP1 for data IN */ +#define CCID_OUT_EP 0x04U /* EP1 for data OUT */ +#define CCID_CMD_EP 0x85U /* EP2 for CDC commands */ + +#endif diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index ed41812..cf820d2 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -2,6 +2,7 @@ #include "usbd_desc.h" #include "usbd_hid.h" #include "usbd_cdc.h" +#include "usbd_ccid.h" #include "usbd_ctlreq.h" static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); @@ -29,8 +30,8 @@ static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); #define NUM_INTERFACES 3 #if NUM_INTERFACES>2 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90) -#if NUM_INTERFACES>1 +#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 84) +#elif NUM_INTERFACES>1 #define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90) #else #define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (41) @@ -224,21 +225,21 @@ USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ /*Endpoint IN1 Descriptor*/ 7, /* bLength: Endpoint Descriptor size */ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ -0x81, /* bEndpointAddress: (IN1) */ +CCID_IN_EP, /* bEndpointAddress: (IN1) */ 0x02, /* bmAttributes: Bulk */ HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ 0x00, /* bInterval */ /*Endpoint OUT1 Descriptor*/ 7, /* bLength: Endpoint Descriptor size */ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ -0x01, /* bEndpointAddress: (OUT1) */ +CCID_OUT_EP, /* bEndpointAddress: (OUT1) */ 0x02, /* bmAttributes: Bulk */ HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ 0x00, /* bInterval */ /*Endpoint IN2 Descriptor*/ 7, /* bLength: Endpoint Descriptor size */ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ -0x82, /* bEndpointAddress: (IN2) */ +CCID_CMD_EP, /* bEndpointAddress: (IN2) */ 0x03, /* bmAttributes: Interrupt */ 0x04, 0x00, /* wMaxPacketSize: 4 */ 0xFF, /* bInterval (255ms) */ diff --git a/targets/stm32l432/lib/usbd/usbd_conf.c b/targets/stm32l432/lib/usbd/usbd_conf.c index 1d46743..793a0a2 100644 --- a/targets/stm32l432/lib/usbd/usbd_conf.c +++ b/targets/stm32l432/lib/usbd/usbd_conf.c @@ -50,6 +50,8 @@ #include "stm32l4xx_hal.h" #include "usbd_core.h" #include "usbd_hid.h" +#include "usbd_cdc.h" +#include "usbd_ccid.h" void SystemClock_Config(void); @@ -252,14 +254,18 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58); // HID - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x98); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xd8); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , HID_EPOUT_ADDR , PCD_SNG_BUF, 0x98); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , HID_EPIN_ADDR , PCD_SNG_BUF, 0xd8); // CDC / uart - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x02 , PCD_SNG_BUF, 0xd8 + 64); // data OUT - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0xd8 + 64*2); // data IN - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x83 , PCD_SNG_BUF, 0xd8 + 64*3); // commands + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, 0xd8 + 64); // data OUT + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_IN_EP , PCD_SNG_BUF, 0xd8 + 64*2); // data IN + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*3); // commands + // CCID + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_OUT_EP , PCD_SNG_BUF, 0xd8 + 64*4); // data OUT + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_IN_EP , PCD_SNG_BUF, 0xd8 + 64*5); // data IN + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*6); // commands return USBD_OK; } From 3a5cd786dc0b4dbb8505e2cbc0e81863014af51e Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 21 May 2019 20:17:37 -0400 Subject: [PATCH 03/42] enumerates correctly --- targets/stm32l432/build/common.mk | 3 +- targets/stm32l432/lib/usbd/usbd_ccid.c | 334 ++++++++++++++++++++ targets/stm32l432/lib/usbd/usbd_ccid.h | 33 ++ targets/stm32l432/lib/usbd/usbd_composite.c | 23 +- targets/stm32l432/lib/usbd/usbd_composite.h | 2 +- targets/stm32l432/src/init.c | 6 +- 6 files changed, 392 insertions(+), 9 deletions(-) create mode 100644 targets/stm32l432/lib/usbd/usbd_ccid.c diff --git a/targets/stm32l432/build/common.mk b/targets/stm32l432/build/common.mk index 7c0ae32..f018bce 100644 --- a/targets/stm32l432/build/common.mk +++ b/targets/stm32l432/build/common.mk @@ -10,7 +10,8 @@ DRIVER_LIBS := lib/stm32l4xx_hal_pcd.c lib/stm32l4xx_hal_pcd_ex.c lib/stm32l4xx_ USB_LIB := lib/usbd/usbd_cdc.c lib/usbd/usbd_cdc_if.c lib/usbd/usbd_composite.c \ lib/usbd/usbd_conf.c lib/usbd/usbd_core.c lib/usbd/usbd_ioreq.c \ - lib/usbd/usbd_ctlreq.c lib/usbd/usbd_desc.c lib/usbd/usbd_hid.c + lib/usbd/usbd_ctlreq.c lib/usbd/usbd_desc.c lib/usbd/usbd_hid.c \ + lib/usbd/usbd_ccid.c VERSION:=$(shell git describe --abbrev=0 ) VERSION_FULL:=$(shell git describe) diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.c b/targets/stm32l432/lib/usbd/usbd_ccid.c new file mode 100644 index 0000000..e202ffe --- /dev/null +++ b/targets/stm32l432/lib/usbd/usbd_ccid.c @@ -0,0 +1,334 @@ +#include +#include "usbd_ccid.h" +#include "usbd_ctlreq.h" +#include "usbd_conf.h" +#include "usbd_core.h" + +#include "log.h" + +static uint8_t USBD_CCID_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_CCID_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_CCID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_CCID_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum); + +static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum); + +static uint8_t USBD_CCID_EP0_RxReady (USBD_HandleTypeDef *pdev); + + +USBD_ClassTypeDef USBD_CCID = +{ + USBD_CCID_Init, + USBD_CCID_DeInit, + USBD_CCID_Setup, + NULL, /* EP0_TxSent, */ + USBD_CCID_EP0_RxReady, + USBD_CCID_DataIn, + USBD_CCID_DataOut, + NULL, + NULL, + NULL, + + NULL, + NULL, + NULL, + NULL, +}; + +static uint8_t ccidmsg_buf[CCID_DATA_PACKET_SIZE]; + +static uint8_t USBD_CCID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + USBD_CCID_HandleTypeDef *hcdc; + + //Y + USBD_LL_OpenEP(pdev, CCID_IN_EP, USBD_EP_TYPE_BULK, + CCID_DATA_PACKET_SIZE); + + pdev->ep_in[CCID_IN_EP & 0xFU].is_used = 1U; + + USBD_LL_OpenEP(pdev, CCID_OUT_EP, USBD_EP_TYPE_BULK, + CCID_DATA_PACKET_SIZE); + + pdev->ep_out[CCID_OUT_EP & 0xFU].is_used = 1U; + + + USBD_LL_OpenEP(pdev, CCID_CMD_EP, USBD_EP_TYPE_INTR, CCID_DATA_PACKET_SIZE); + pdev->ep_in[CCID_CMD_EP & 0xFU].is_used = 1U; + + static USBD_CCID_HandleTypeDef mem; + pdev->pClassData = &mem; + + hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; + + // init transfer states + hcdc->TxState = 0U; + hcdc->RxState = 0U; + + USBD_LL_PrepareReceive(&Solo_USBD_Device, CCID_OUT_EP, ccidmsg_buf, + CCID_DATA_PACKET_SIZE); + + return ret; +} + +static uint8_t USBD_CCID_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + //N + + USBD_LL_CloseEP(pdev, CCID_IN_EP); + pdev->ep_in[CCID_IN_EP & 0xFU].is_used = 0U; + + USBD_LL_CloseEP(pdev, CCID_OUT_EP); + pdev->ep_out[CCID_OUT_EP & 0xFU].is_used = 0U; + + USBD_LL_CloseEP(pdev, CCID_CMD_EP); + pdev->ep_in[CCID_CMD_EP & 0xFU].is_used = 0U; + + /* DeInit physical Interface components */ + if(pdev->pClassData != NULL) + { + pdev->pClassData = NULL; + } + + return ret; +} + +/** + * @brief USBD_CDC_Setup + * Handle the CDC specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_CCID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; + uint8_t ifalt = 0U; + uint16_t status_info = 0U; + uint8_t ret = USBD_OK; + //N + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS : + if (req->wLength) + { + if (req->bmRequest & 0x80U) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + else + { + hcdc->CmdOpCode = req->bRequest; + hcdc->CmdLength = (uint8_t)req->wLength; + + USBD_CtlPrepareRx (pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + } + else + { + + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, &ifalt, 1U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state != USBD_STATE_CONFIGURED) + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + case USB_REQ_GET_DESCRIPTOR: + break; + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + + +/** + * @brief USBD_CDC_DataIn + * Data sent on non-control IN endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CCID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + //N + USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*)pdev->pClassData; + PCD_HandleTypeDef *hpcd = pdev->pData; + + if(pdev->pClassData != NULL) + { + if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) + { + /* Update the packet total length */ + pdev->ep_in[epnum].total_length = 0U; + + /* Send ZLP */ + USBD_LL_Transmit (pdev, epnum, NULL, 0U); + } + else + { + hcdc->TxState = 0U; + } + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +uint8_t USBD_CCID_TransmitPacket(uint8_t * msg, int len) +{ + /* Update the packet total length */ + Solo_USBD_Device.ep_in[CCID_IN_EP & 0xFU].total_length = len; + + /* Transmit next packet */ + USBD_LL_Transmit(&Solo_USBD_Device, CCID_IN_EP, msg, + len); + + return USBD_OK; +} + +#define CCID_HEADER_SIZE 10 +typedef struct +{ + uint8_t type; + uint32_t len; + uint8_t slot; + uint8_t seq; + uint8_t rsvd; + uint16_t param; +} __attribute__((packed)) CCID_HEADER; + +void ccid_send_status(CCID_HEADER * c) +{ + uint8_t msg[CCID_HEADER_SIZE]; + memset(msg,0,sizeof(msg)); + + msg[0] = CCID_SLOT_STATUS_RES; + msg[6] = c->seq; + + USBD_CCID_TransmitPacket(msg, sizeof(msg)); + +} + + + + +void handle_ccid(uint8_t * msg, int len) +{ + CCID_HEADER * h = (CCID_HEADER *) msg; + switch(h->type) + { + case CCID_SLOT_STATUS: + ccid_send_status(h); + break; + default: + break; + } +} + +/** + * @brief USBD_CDC_DataOut + * Data received on non-control Out endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + while(1) + { + led_rgb(0xff3520); + } + USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; + + /* Get the received data length */ + hcdc->RxLength = USBD_LL_GetRxDataSize (pdev, epnum); + + /* USB data will be immediately processed, this allow next USB traffic being + NAKed till the end of the application Xfer */ + if(pdev->pClassData != NULL) + { + // printf1(TAG_GREEN,"ccid>> "); + // dump_hex1(TAG_GREEN, hcdc->RxBuffer, hcdc->RxLength); + + handle_ccid(hcdc->RxBuffer, hcdc->RxLength); + + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_EP0_RxReady + * Handle EP0 Rx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_CCID_EP0_RxReady (USBD_HandleTypeDef *pdev) +{ + USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; + + if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) + { + hcdc->CmdOpCode = 0xFFU; + } + return USBD_OK; +} diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.h b/targets/stm32l432/lib/usbd/usbd_ccid.h index ce38be1..cecf170 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.h +++ b/targets/stm32l432/lib/usbd/usbd_ccid.h @@ -1,8 +1,41 @@ #ifndef _USBD_H_ #define _USBD_H_ +#include "usbd_ioreq.h" + #define CCID_IN_EP 0x84U /* EP1 for data IN */ #define CCID_OUT_EP 0x04U /* EP1 for data OUT */ #define CCID_CMD_EP 0x85U /* EP2 for CDC commands */ +#define CCID_DATA_PACKET_SIZE 64 + +#define CCID_SET_PARAMS 0x61 +#define CCID_POWER_ON 0x62 +#define CCID_POWER_OFF 0x63 +#define CCID_SLOT_STATUS 0x65 +#define CCID_SECURE 0x69 +#define CCID_GET_PARAMS 0x6C +#define CCID_RESET_PARAMS 0x6D +#define CCID_XFR_BLOCK 0x6F +#define CCID_DATA_BLOCK_RES 0x80 +#define CCID_SLOT_STATUS_RES 0x81 +#define CCID_PARAMS_RES 0x82 + +extern USBD_ClassTypeDef USBD_CCID; + +typedef struct +{ + uint32_t data[CCID_DATA_PACKET_SIZE / 4U]; + uint8_t CmdOpCode; + uint8_t CmdLength; + uint8_t *RxBuffer; + uint8_t *TxBuffer; + uint32_t RxLength; + uint32_t TxLength; + + __IO uint32_t TxState; + __IO uint32_t RxState; +} +USBD_CCID_HandleTypeDef; + #endif diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index cf820d2..a70db6a 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -39,6 +39,7 @@ static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); #define HID_INTF_NUM 0 #define CDC_INTF_NUM 1 +#define CCID_INTF_NUM 2 __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_SIZE] __ALIGN_END = { /*Configuration Descriptor*/ @@ -176,7 +177,7 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ /* CCID Interface Descriptor */ 9, /* bLength: Interface Descriptor size */ USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ -0, /* bInterfaceNumber: CCID Interface */ +CCID_INTF_NUM, /* bInterfaceNumber: CCID Interface */ 0, /* Alternate setting for this interface */ 3, /* bNumEndpoints: Bulk-IN, Bulk-OUT, Intr-IN */ 0x0B, /* CCID class */ @@ -273,19 +274,26 @@ int in_endpoint_to_class[MAX_ENDPOINTS]; int out_endpoint_to_class[MAX_ENDPOINTS]; -void USBD_Composite_Set_Classes(USBD_ClassTypeDef *class0, USBD_ClassTypeDef *class1) { +void USBD_Composite_Set_Classes(USBD_ClassTypeDef *class0, USBD_ClassTypeDef *class1, USBD_ClassTypeDef *class2) +{ + //Y USBD_Classes[0] = class0; USBD_Classes[1] = class1; + USBD_Classes[2] = class2; + //Y } static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { int i; + + //N + for(i = 0; i < NUM_INTERFACES; i++) { if (USBD_Classes[i]->Init(pdev, cfgidx) != USBD_OK) { return USBD_FAIL; } } - + //N return USBD_OK; } @@ -302,6 +310,7 @@ static uint8_t USBD_Composite_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx) static uint8_t USBD_Composite_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) { int i; + //N switch (req->bmRequest & USB_REQ_TYPE_MASK) { case USB_REQ_TYPE_CLASS : if (req->wIndex < NUM_INTERFACES) @@ -363,16 +372,19 @@ static uint8_t USBD_Composite_EP0_RxReady (USBD_HandleTypeDef *pdev) { } static uint8_t *USBD_Composite_GetFSCfgDesc (uint16_t *length) { + //Y *length = COMPOSITE_CDC_HID_DESCRIPTOR_SIZE; return COMPOSITE_CDC_HID_DESCRIPTOR; } static uint8_t *USBD_Composite_GetHSCfgDesc (uint16_t *length) { + //N *length = COMPOSITE_CDC_HID_DESCRIPTOR_SIZE; return COMPOSITE_CDC_HID_DESCRIPTOR; } static uint8_t *USBD_Composite_GetOtherSpeedCfgDesc (uint16_t *length) { + *length = COMPOSITE_CDC_HID_DESCRIPTOR_SIZE; return COMPOSITE_CDC_HID_DESCRIPTOR; } @@ -393,6 +405,7 @@ __ALIGN_BEGIN static uint8_t USBD_Composite_DeviceQualifierDesc[USB_LEN_DEV_QUAL }; uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length) { - *length = sizeof (USBD_Composite_DeviceQualifierDesc); - return USBD_Composite_DeviceQualifierDesc; + //N + *length = sizeof (USBD_Composite_DeviceQualifierDesc); + return USBD_Composite_DeviceQualifierDesc; } diff --git a/targets/stm32l432/lib/usbd/usbd_composite.h b/targets/stm32l432/lib/usbd/usbd_composite.h index 7599b39..0fdc513 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.h +++ b/targets/stm32l432/lib/usbd/usbd_composite.h @@ -17,7 +17,7 @@ extern int in_endpoint_to_class[MAX_ENDPOINTS]; extern int out_endpoint_to_class[MAX_ENDPOINTS]; -void USBD_Composite_Set_Classes(USBD_ClassTypeDef *class0, USBD_ClassTypeDef *class1); +void USBD_Composite_Set_Classes(USBD_ClassTypeDef *class0, USBD_ClassTypeDef *class1, USBD_ClassTypeDef *class2); #ifdef __cplusplus } diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index e7fa953..93ca071 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -27,6 +27,7 @@ #include "usbd_desc.h" #include "usbd_hid.h" #include "usbd_cdc.h" +#include "usbd_ccid.h" #include "usbd_composite.h" #include "usbd_cdc_if.h" #include "device.h" @@ -706,7 +707,7 @@ void init_usb() SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); #if DEBUG_LEVEL > 0 - USBD_Composite_Set_Classes(&USBD_HID, &USBD_CDC); + USBD_Composite_Set_Classes(&USBD_HID, &USBD_CDC, &USBD_CCID); in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; @@ -723,8 +724,9 @@ void init_usb() USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); #endif - + //Y USBD_Start(&Solo_USBD_Device); + //Y } void init_pwm(void) From ba581db49c27d0a16c24e358c1bed88d6ee2f170 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 21 May 2019 20:17:44 -0400 Subject: [PATCH 04/42] delete excess --- targets/stm32l432/lib/usbd/usbd_cdc.c | 345 +------------------------- 1 file changed, 1 insertion(+), 344 deletions(-) diff --git a/targets/stm32l432/lib/usbd/usbd_cdc.c b/targets/stm32l432/lib/usbd/usbd_cdc.c index 58d1301..0d8eb36 100644 --- a/targets/stm32l432/lib/usbd/usbd_cdc.c +++ b/targets/stm32l432/lib/usbd/usbd_cdc.c @@ -195,302 +195,9 @@ USBD_ClassTypeDef USBD_CDC = NULL, NULL, NULL, - // USBD_CDC_GetHSCfgDesc, - // USBD_CDC_GetFSCfgDesc, - // USBD_CDC_GetOtherSpeedCfgDesc, - // USBD_CDC_GetDeviceQualifierDescriptor, }; -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_HS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - - -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - -__ALIGN_BEGIN uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, - USB_CDC_CONFIG_DESC_SIZ, - 0x00, - 0x02, /* bNumInterfaces: 2 interfaces */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT , /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00 /* bInterval */ -}; - -/** - * @} - */ - -/** @defgroup USBD_CDC_Private_Functions - * @{ - */ /** * @brief USBD_CDC_Init @@ -782,45 +489,7 @@ static uint8_t USBD_CDC_EP0_RxReady (USBD_HandleTypeDef *pdev) return USBD_OK; } -/** - * @brief USBD_CDC_GetFSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -/*static uint8_t *USBD_CDC_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_CfgFSDesc); - return USBD_CDC_CfgFSDesc; -} -*/ -/** - * @brief USBD_CDC_GetHSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -/*static uint8_t *USBD_CDC_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_CfgHSDesc); - return USBD_CDC_CfgHSDesc; -} -*/ -/** - * @brief USBD_CDC_GetCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -/*static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_OtherSpeedCfgDesc); - return USBD_CDC_OtherSpeedCfgDesc; -} -*/ + /** * @brief DeviceQualifierDescriptor * return Device Qualifier descriptor @@ -939,22 +608,10 @@ uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) /* Suspend or Resume USB Out process */ if(pdev->pClassData != NULL) { - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, - CDC_OUT_EP, - hcdc->RxBuffer, - CDC_DATA_HS_OUT_PACKET_SIZE); - } - else - { - /* Prepare Out endpoint to receive next packet */ USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, CDC_DATA_FS_OUT_PACKET_SIZE); - } return USBD_OK; } else From a51417bf61da53c19c61333ded2f565b40a0118c Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 31 May 2019 15:58:13 -0400 Subject: [PATCH 05/42] fix epout connection --- targets/stm32l432/lib/usbd/usbd_ccid.c | 33 +++++++++++++++++--------- targets/stm32l432/lib/usbd/usbd_ccid.h | 2 ++ targets/stm32l432/lib/usbd/usbd_conf.c | 5 +++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.c b/targets/stm32l432/lib/usbd/usbd_ccid.c index e202ffe..c452aa3 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.c +++ b/targets/stm32l432/lib/usbd/usbd_ccid.c @@ -32,7 +32,7 @@ USBD_ClassTypeDef USBD_CCID = NULL, /* EP0_TxSent, */ USBD_CCID_EP0_RxReady, USBD_CCID_DataIn, - USBD_CCID_DataOut, + usb_ccid_recieve_callback, NULL, NULL, NULL, @@ -259,7 +259,7 @@ void ccid_send_status(CCID_HEADER * c) memset(msg,0,sizeof(msg)); msg[0] = CCID_SLOT_STATUS_RES; - msg[6] = c->seq; + msg[6] = 1; USBD_CCID_TransmitPacket(msg, sizeof(msg)); @@ -277,6 +277,12 @@ void handle_ccid(uint8_t * msg, int len) ccid_send_status(h); break; default: + // while(1) + // { + // led_rgb(0xff3520); + // } + //Y + ccid_send_status(h); break; } } @@ -288,12 +294,9 @@ void handle_ccid(uint8_t * msg, int len) * @param epnum: endpoint number * @retval status */ -static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) +uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum) { - while(1) - { - led_rgb(0xff3520); - } + USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; /* Get the received data length */ @@ -312,10 +315,18 @@ static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) } else { + + + while(1) + { + led_rgb(0xff3520); + } + return USBD_FAIL; } } + /** * @brief USBD_CDC_EP0_RxReady * Handle EP0 Rx Ready event @@ -326,9 +337,9 @@ static uint8_t USBD_CCID_EP0_RxReady (USBD_HandleTypeDef *pdev) { USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; - if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) - { - hcdc->CmdOpCode = 0xFFU; - } + // if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) + // { + // hcdc->CmdOpCode = 0xFFU; + // } return USBD_OK; } diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.h b/targets/stm32l432/lib/usbd/usbd_ccid.h index cecf170..6535987 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.h +++ b/targets/stm32l432/lib/usbd/usbd_ccid.h @@ -38,4 +38,6 @@ typedef struct } USBD_CCID_HandleTypeDef; +uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum); + #endif diff --git a/targets/stm32l432/lib/usbd/usbd_conf.c b/targets/stm32l432/lib/usbd/usbd_conf.c index 793a0a2..61bcecd 100644 --- a/targets/stm32l432/lib/usbd/usbd_conf.c +++ b/targets/stm32l432/lib/usbd/usbd_conf.c @@ -119,9 +119,12 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); switch(epnum) { - case HID_ENDPOINT: + case HID_EPOUT_ADDR: usb_hid_recieve_callback(epnum); break; + case CCID_OUT_EP: + usb_ccid_recieve_callback((USBD_HandleTypeDef*)hpcd->pData, epnum); + break; } } From 4b6619b705e04e904c2b500c111f3457f289a40a Mon Sep 17 00:00:00 2001 From: Nicolas Stalder Date: Wed, 21 Aug 2019 02:31:20 +0200 Subject: [PATCH 06/42] Update udev docs --- docs/solo/udev.md | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/solo/udev.md b/docs/solo/udev.md index 6866f99..7dae7e4 100644 --- a/docs/solo/udev.md +++ b/docs/solo/udev.md @@ -1,20 +1,20 @@ # Summary -On Linux, by default USB dongles can't be accessed by users, for security reasons. To allow user access, so-called "udev rules" must be installed. (Under Fedora, your key may work without such a rule.) +On Linux, by default USB dongles can't be accessed by users, for security reasons. To allow user access, so-called "udev rules" must be installed. -Create a file like [`70-solokeys-access.rules`](https://github.com/solokeys/solo/blob/master/udev/70-solokeys-access.rules) in your `/etc/udev/rules.d` directory, for instance the following rule should cover normal access (it has to be on one line): +For some users, things will work automatically: + - Fedora seems to use a ["universal" udev rule for FIDO devices](https://github.com/amluto/u2f-hidraw-policy) + - Our udev rule made it into [libu2f-host](https://github.com/Yubico/libu2f-host/) v1.1.10 + - Arch Linux [has this package](https://www.archlinux.org/packages/community/x86_64/libu2f-host/) + - [Debian sid](https://packages.debian.org/sid/libu2f-udev) and [Ubuntu Eon](https://packages.ubuntu.com/eoan/libu2f-udev) can use the `libu2f-udev` package + - Debian Buster and Ubuntu Disco still distribute v1.1.10, so need the manual rule + - FreeBSD has support in [u2f-devd](https://github.com/solokeys/solo/issues/144#issuecomment-500216020) -``` -SUBSYSTEM=="hidraw", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="a2ca", TAG+="uaccess", MODE="0660", GROUP="plugdev" -``` +There is hope that `udev` itself will adopt the Fedora approach (which is to check for HID usage page `F1D0`, and avoids manually whitelisting each U2F/FIDO2 key): . -Additionally, run the following command after you create this file (it is not necessary to do this again in the future): +Further progress is tracked in: . -``` -sudo udevadm control --reload-rules && sudo udevadm trigger -``` - -A simple way to setup both the udev rule and the udevadm reload is: +If you still need to setup a rule, a simple way to do it is: ``` git clone git@github.com:solokeys/solo.git @@ -22,9 +22,11 @@ cd solo/udev make setup ``` -We are working on getting user access to Solo keys enabled automatically in common Linux distributions: . - - +Or, manually, create a file like [`70-solokeys-access.rules`](https://github.com/solokeys/solo/blob/master/udev/70-solokeys-access.rules) in your `/etc/udev/rules.d` directory. +Additionally, run the following command after you create this file (it is not necessary to do this again in the future): +``` +sudo udevadm control --reload-rules && sudo udevadm trigger +``` # How do udev rules work and why are they needed From b706cc30b02e21e45d5d5075a3acf63f444d0d8d Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 20 Aug 2019 20:23:18 +0800 Subject: [PATCH 07/42] for now, always gen key agreement --- fido2/ctap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fido2/ctap.c b/fido2/ctap.c index dea0f3f..c364005 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -1678,7 +1678,7 @@ uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp) break; default: status = CTAP1_ERR_INVALID_COMMAND; - printf2(TAG_ERR,"error, invalid cmd\n"); + printf2(TAG_ERR,"error, invalid cmd: %x\n", cmd); } done: @@ -1767,7 +1767,7 @@ void ctap_init() exit(1); } - if (device_is_nfc() != NFC_IS_ACTIVE) + // if (device_is_nfc() != NFC_IS_ACTIVE) { ctap_reset_key_agreement(); } From d931954a1395904e796109f7c30901bdf4a40de7 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 20 Aug 2019 20:28:38 +0800 Subject: [PATCH 08/42] remove WTX, move debug log --- targets/stm32l432/src/nfc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index f35f42b..3b42d79 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -489,9 +489,6 @@ void nfc_process_iblock(uint8_t * buf, int len) CTAP_RESPONSE ctap_resp; int status; uint16_t reslen; - - printf1(TAG_NFC,"Iblock: "); - dump_hex1(TAG_NFC, buf, len); uint8_t block_offset = p14443_block_offset(buf[0]); @@ -630,13 +627,15 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC, "FIDO2 CTAP message. %d\r\n", timestamp()); - WTX_on(WTX_TIME_DEFAULT); + // WTX_on(WTX_TIME_DEFAULT); request_from_nfc(true); + // if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); ctap_response_init(&ctap_resp); status = ctap_request(apdu.data, apdu.lc, &ctap_resp); + // if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); request_from_nfc(false); - if (!WTX_off()) - return; + // if (!WTX_off()) + // return; printf1(TAG_NFC, "CTAP resp: 0x%02x len: %d\r\n", status, ctap_resp.length); @@ -688,6 +687,9 @@ void nfc_process_iblock(uint8_t * buf, int len) nfc_write_response(buf[0], SW_INS_INVALID); break; } + + printf1(TAG_NFC,"prev.Iblock: "); + dump_hex1(TAG_NFC, buf, len); } static uint8_t ibuf[1024]; From adcbd3aeb8f6a6f851238ab6d98c7df92e55d3be Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 20 Aug 2019 21:06:37 +0800 Subject: [PATCH 09/42] speed up public key derivation slightly for nfc --- fido2/ctap.c | 4 ++++ targets/stm32l432/src/nfc.c | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fido2/ctap.c b/fido2/ctap.c index c364005..9302e1d 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -256,7 +256,9 @@ static int ctap_generate_cose_key(CborEncoder * cose_key, uint8_t * hmac_input, switch(algtype) { case COSE_ALG_ES256: + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); crypto_ecc256_derive_public_key(hmac_input, len, x, y); + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); break; default: printf2(TAG_ERR,"Error, COSE alg %d not supported\n", algtype); @@ -1969,7 +1971,9 @@ int8_t ctap_load_key(uint8_t index, uint8_t * key) static void ctap_reset_key_agreement() { + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); crypto_ecc256_make_key_pair(KEY_AGREEMENT_PUB, KEY_AGREEMENT_PRIV); + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); } void ctap_reset() diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 3b42d79..726c47e 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -629,10 +629,8 @@ void nfc_process_iblock(uint8_t * buf, int len) // WTX_on(WTX_TIME_DEFAULT); request_from_nfc(true); - // if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); ctap_response_init(&ctap_resp); status = ctap_request(apdu.data, apdu.lc, &ctap_resp); - // if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); request_from_nfc(false); // if (!WTX_off()) // return; From a72f0ede05ceb04d722156c23bf34dfa5e96869d Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 20 Aug 2019 21:27:52 +0800 Subject: [PATCH 10/42] take a lazy approach to key agreement generation to not hold up boot time for nfc --- fido2/crypto.c | 5 +++++ fido2/crypto.h | 1 + fido2/ctap.c | 14 +++++++------- targets/stm32l432/src/crypto.c | 5 +++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/fido2/crypto.c b/fido2/crypto.c index 63520c1..6aea29f 100644 --- a/fido2/crypto.c +++ b/fido2/crypto.c @@ -262,6 +262,11 @@ void crypto_ecc256_derive_public_key(uint8_t * data, int len, uint8_t * x, uint8 memmove(y,pubkey+32,32); } +void crypto_ecc256_compute_public_key(uint8_t * privkey, uint8_t * pubkey) +{ + uECC_compute_public_key(privkey, pubkey, _es256_curve); +} + void crypto_load_external_key(uint8_t * key, int len) { _signing_key = key; diff --git a/fido2/crypto.h b/fido2/crypto.h index e9e4433..6b67b02 100644 --- a/fido2/crypto.h +++ b/fido2/crypto.h @@ -26,6 +26,7 @@ void crypto_sha512_final(uint8_t * hash); void crypto_ecc256_init(); void crypto_ecc256_derive_public_key(uint8_t * data, int len, uint8_t * x, uint8_t * y); +void crypto_ecc256_compute_public_key(uint8_t * privkey, uint8_t * pubkey); void crypto_ecc256_load_key(uint8_t * data, int len, uint8_t * data2, int len2); void crypto_ecc256_load_attestation_key(); diff --git a/fido2/ctap.c b/fido2/ctap.c index 9302e1d..8edf591 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -1481,6 +1481,11 @@ uint8_t ctap_client_pin(CborEncoder * encoder, uint8_t * request, int length) ret = cbor_encode_int(&map, RESP_keyAgreement); check_ret(ret); + + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); + crypto_ecc256_compute_public_key(KEY_AGREEMENT_PRIV, KEY_AGREEMENT_PUB); + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); + ret = ctap_add_cose_key(&map, KEY_AGREEMENT_PUB, KEY_AGREEMENT_PUB+32, PUB_KEY_CRED_PUB_KEY, COSE_ALG_ECDH_ES_HKDF_256); check_retr(ret); @@ -1769,10 +1774,7 @@ void ctap_init() exit(1); } - // if (device_is_nfc() != NFC_IS_ACTIVE) - { - ctap_reset_key_agreement(); - } + ctap_reset_key_agreement(); #ifdef BRIDGE_TO_WALLET wallet_init(); @@ -1971,9 +1973,7 @@ int8_t ctap_load_key(uint8_t index, uint8_t * key) static void ctap_reset_key_agreement() { - if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); - crypto_ecc256_make_key_pair(KEY_AGREEMENT_PUB, KEY_AGREEMENT_PRIV); - if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); + ctap_generate_rng(KEY_AGREEMENT_PRIV, sizeof(KEY_AGREEMENT_PRIV)); } void ctap_reset() diff --git a/targets/stm32l432/src/crypto.c b/targets/stm32l432/src/crypto.c index 33fef68..1dde2f3 100644 --- a/targets/stm32l432/src/crypto.c +++ b/targets/stm32l432/src/crypto.c @@ -282,6 +282,11 @@ void crypto_ecc256_derive_public_key(uint8_t * data, int len, uint8_t * x, uint8 memmove(x,pubkey,32); memmove(y,pubkey+32,32); } +void crypto_ecc256_compute_public_key(uint8_t * privkey, uint8_t * pubkey) +{ + uECC_compute_public_key(privkey, pubkey, _es256_curve); +} + void crypto_load_external_key(uint8_t * key, int len) { From dccfb0d1b3c4d02185e9c8725772221541fc4ac7 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Tue, 20 Aug 2019 21:31:02 +0800 Subject: [PATCH 11/42] stub pc build --- pc/device.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pc/device.c b/pc/device.c index 2a0e166..0944254 100644 --- a/pc/device.c +++ b/pc/device.c @@ -628,3 +628,8 @@ int device_is_nfc() { return 0; } + +void device_set_clock_rate(DEVICE_CLOCK_RATE param) +{ + +} From b743d5fac56a1c598054890f00d4efc58ccdbda2 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 20 Aug 2019 18:42:20 +0300 Subject: [PATCH 12/42] sketch --- fido2/apdu.h | 2 + targets/stm32l432/src/nfc.c | 87 ++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/fido2/apdu.h b/fido2/apdu.h index 44029b3..7ce8477 100644 --- a/fido2/apdu.h +++ b/fido2/apdu.h @@ -44,12 +44,14 @@ extern int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu); #define APDU_FIDO_NFCCTAP_MSG 0x10 #define APDU_INS_SELECT 0xA4 #define APDU_INS_READ_BINARY 0xB0 +#define APDU_GET_RESPONSE 0xC0 #define SW_SUCCESS 0x9000 #define SW_GET_RESPONSE 0x6100 // Command successfully executed; 'XX' bytes of data are available and can be requested using GET RESPONSE. #define SW_WRONG_LENGTH 0x6700 #define SW_COND_USE_NOT_SATISFIED 0x6985 #define SW_FILE_NOT_FOUND 0x6a82 +#define SW_INCORRECT_P1P2 0x6a86 #define SW_INS_INVALID 0x6d00 // Instruction code not supported or invalid #define SW_CLA_INVALID 0x6e00 #define SW_INTERNAL_EXCEPTION 0x6f00 diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 726c47e..bf484da 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -14,6 +14,10 @@ #define IS_IRQ_ACTIVE() (1 == (LL_GPIO_ReadInputPort(SOLO_AMS_IRQ_PORT) & SOLO_AMS_IRQ_PIN)) +// chain buffer for 61XX responses +static uint8_t resp_chain_buffer[2048] = {0}; +static size_t resp_chain_buffer_len = 0; + uint8_t p14443_block_offset(uint8_t pcb) { uint8_t offset = 1; // NAD following @@ -213,7 +217,7 @@ bool nfc_write_response(uint8_t req0, uint16_t resp) return nfc_write_response_ex(req0, NULL, 0, resp); } -void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) +void nfc_write_response_chaining_plain(uint8_t req0, uint8_t * data, int len) { uint8_t res[32 + 2]; uint8_t iBlock = NFC_CMD_IBLOCK | (req0 & 0x0f); @@ -284,6 +288,38 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len) } } +void append_get_response(uint8_t *data, size_t rest_len) +{ + data[0] = 0x61; + data[1] = 0x00; + if (rest_len <= 0xff) + data[1] = rest_len & 0xff; +} + +void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len, bool extapdu) +{ + resp_chain_buffer_len = 0; + + // if we dont need to break data to parts that need to exchange via GET RESPONSE command (ISO 7816-4 7.1.3) +printf1(TAG_NFC, "---%d.\r\n", extapdu); + if (len <= 255 || extapdu) + { + nfc_write_response_chaining_plain(req0, data, len); + } else { + size_t pcklen = MIN(253, len); + resp_chain_buffer_len = len - pcklen; + printf1(TAG_NFC, "61XX chaining %d/%d.\r\n", pcklen, resp_chain_buffer_len); + + memmove(resp_chain_buffer, data, pcklen); + append_get_response(&resp_chain_buffer[pcklen], resp_chain_buffer_len); + + nfc_write_response_chaining_plain(req0, data, pcklen + 2); // 2 for 61XX + + // put the rest data into chain buffer + memmove(resp_chain_buffer, &data[pcklen], resp_chain_buffer_len); + } +} + // WTX on/off: // sends/receives WTX frame to reader every `WTX_time` time in ms // works via timer interrupts @@ -511,6 +547,47 @@ void nfc_process_iblock(uint8_t * buf, int len) // TODO this needs to be organized better switch(apdu.ins) { + // ISO 7816. 7.1 GET RESPONSE command + case APDU_GET_RESPONSE: + if (apdu.p1 != 0x00 || apdu.p2 != 0x00) + { + nfc_write_response(buf[0], SW_INCORRECT_P1P2); + return; + } + + // too many bytes needs. 0x00 and 0x100 - any length + if (apdu.le != 0 && apdu.le != 0x100 && apdu.le > resp_chain_buffer_len) + { + uint16_t wlresp = SW_WRONG_LENGTH; // here can be 6700, 6C00, 6FXX. but the most standard way - 67XX or 6700 + if (resp_chain_buffer_len <= 0xff) + wlresp += resp_chain_buffer_len & 0xff; + nfc_write_response(buf[0], wlresp); + return; + } + + // create temporary packet + uint8_t pck[255] = {0}; + int pcklen = MIN(253, apdu.le); + if (pcklen > resp_chain_buffer_len) + pcklen = resp_chain_buffer_len; + + // create packet and add 61XX there if we have another portion(s) of data + memmove(pck, resp_chain_buffer, pcklen); + size_t dlen = 0; + if (resp_chain_buffer_len - pcklen) + { + append_get_response(&pck[pcklen], resp_chain_buffer_len - pcklen); + dlen = 2; + } + + // send + nfc_write_response_chaining(buf[0], pck, pcklen + dlen, apdu.extended_apdu); // dlen for 61XX + + // shift the buffer + resp_chain_buffer_len -= pcklen; + memmove(resp_chain_buffer, &resp_chain_buffer[pcklen], resp_chain_buffer_len); + break; + case APDU_INS_SELECT: // if (apdu->p1 == 0 && apdu->p2 == 0x0c) // { @@ -554,7 +631,7 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC, "U2F GetVersion command.\r\n"); u2f_request_nfc(&buf[block_offset], apdu.data, apdu.lc, &ctap_resp); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); break; case APDU_FIDO_U2F_REGISTER: @@ -586,7 +663,7 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); printf1(TAG_NFC,"U2F Register P2 took %d\r\n", timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); printf1(TAG_NFC,"U2F Register answered %d (took %d)\r\n", millis(), timestamp()); break; @@ -615,7 +692,7 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); printf1(TAG_NFC,"U2F Authenticate processing %d (took %d)\r\n", millis(), timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); printf1(TAG_NFC,"U2F Authenticate answered %d (took %d)\r\n", millis(), timestamp); break; @@ -649,7 +726,7 @@ void nfc_process_iblock(uint8_t * buf, int len) ctap_resp.data[ctap_resp.length - 1] = SW_SUCCESS & 0xff; printf1(TAG_NFC,"CTAP processing %d (took %d)\r\n", millis(), timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length); + nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); printf1(TAG_NFC,"CTAP answered %d (took %d)\r\n", millis(), timestamp()); break; From 8059a9765f7aa2d4a1517024ec3a17f14618026e Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 20 Aug 2019 18:53:46 +0300 Subject: [PATCH 13/42] was wrong buffer --- targets/stm32l432/src/nfc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index bf484da..85dace4 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -301,7 +301,6 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len, bool ext resp_chain_buffer_len = 0; // if we dont need to break data to parts that need to exchange via GET RESPONSE command (ISO 7816-4 7.1.3) -printf1(TAG_NFC, "---%d.\r\n", extapdu); if (len <= 255 || extapdu) { nfc_write_response_chaining_plain(req0, data, len); @@ -313,7 +312,7 @@ printf1(TAG_NFC, "---%d.\r\n", extapdu); memmove(resp_chain_buffer, data, pcklen); append_get_response(&resp_chain_buffer[pcklen], resp_chain_buffer_len); - nfc_write_response_chaining_plain(req0, data, pcklen + 2); // 2 for 61XX + nfc_write_response_chaining_plain(req0, resp_chain_buffer, pcklen + 2); // 2 for 61XX // put the rest data into chain buffer memmove(resp_chain_buffer, &data[pcklen], resp_chain_buffer_len); From 62b4418dac714b79d984270d856e0bf05ad5dd03 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 20 Aug 2019 19:07:33 +0300 Subject: [PATCH 14/42] fix pck length math --- targets/stm32l432/src/nfc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 85dace4..9b75efa 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -551,6 +551,7 @@ void nfc_process_iblock(uint8_t * buf, int len) if (apdu.p1 != 0x00 || apdu.p2 != 0x00) { nfc_write_response(buf[0], SW_INCORRECT_P1P2); + printf1(TAG_NFC, "P1 or P2 error\r\n"); return; } @@ -561,15 +562,20 @@ void nfc_process_iblock(uint8_t * buf, int len) if (resp_chain_buffer_len <= 0xff) wlresp += resp_chain_buffer_len & 0xff; nfc_write_response(buf[0], wlresp); + printf1(TAG_NFC, "buffer length less than requesteds\r\n"); return; } // create temporary packet uint8_t pck[255] = {0}; - int pcklen = MIN(253, apdu.le); + size_t pcklen = 253; + if (apdu.le) + pcklen = apdu.le; if (pcklen > resp_chain_buffer_len) pcklen = resp_chain_buffer_len; + printf1(TAG_NFC, "--- %d/%d\r\n", pcklen, resp_chain_buffer_len); + // create packet and add 61XX there if we have another portion(s) of data memmove(pck, resp_chain_buffer, pcklen); size_t dlen = 0; From 728acc1671292f655022f5511af3a00058337511 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 20 Aug 2019 19:29:25 +0300 Subject: [PATCH 15/42] chaining not needs to go to the start --- targets/stm32l432/src/nfc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 9b75efa..1d935b0 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -574,8 +574,8 @@ void nfc_process_iblock(uint8_t * buf, int len) if (pcklen > resp_chain_buffer_len) pcklen = resp_chain_buffer_len; - printf1(TAG_NFC, "--- %d/%d\r\n", pcklen, resp_chain_buffer_len); - + printf1(TAG_NFC, "GET RESPONSE. pck len: %d buffer len: %d\r\n", pcklen, resp_chain_buffer_len); + // create packet and add 61XX there if we have another portion(s) of data memmove(pck, resp_chain_buffer, pcklen); size_t dlen = 0; @@ -586,7 +586,7 @@ void nfc_process_iblock(uint8_t * buf, int len) } // send - nfc_write_response_chaining(buf[0], pck, pcklen + dlen, apdu.extended_apdu); // dlen for 61XX + nfc_write_response_chaining_plain(buf[0], pck, pcklen + dlen); // dlen for 61XX // shift the buffer resp_chain_buffer_len -= pcklen; From 32f920e372b6d6c924f48512a714a9628ef5fea3 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Thu, 22 Aug 2019 19:52:21 +0800 Subject: [PATCH 16/42] compile/crash fixes --- targets/stm32l432/lib/usbd/usbd_ccid.c | 68 ++++---- targets/stm32l432/lib/usbd/usbd_composite.c | 166 ++++++++++---------- targets/stm32l432/src/init.c | 9 +- 3 files changed, 128 insertions(+), 115 deletions(-) diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.c b/targets/stm32l432/lib/usbd/usbd_ccid.c index c452aa3..2a81ef7 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.c +++ b/targets/stm32l432/lib/usbd/usbd_ccid.c @@ -32,7 +32,7 @@ USBD_ClassTypeDef USBD_CCID = NULL, /* EP0_TxSent, */ USBD_CCID_EP0_RxReady, USBD_CCID_DataIn, - usb_ccid_recieve_callback, + USBD_CCID_DataOut, NULL, NULL, NULL, @@ -54,11 +54,10 @@ static uint8_t USBD_CCID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) USBD_LL_OpenEP(pdev, CCID_IN_EP, USBD_EP_TYPE_BULK, CCID_DATA_PACKET_SIZE); - pdev->ep_in[CCID_IN_EP & 0xFU].is_used = 1U; - USBD_LL_OpenEP(pdev, CCID_OUT_EP, USBD_EP_TYPE_BULK, CCID_DATA_PACKET_SIZE); + pdev->ep_in[CCID_IN_EP & 0xFU].is_used = 1U; pdev->ep_out[CCID_OUT_EP & 0xFU].is_used = 1U; @@ -202,32 +201,38 @@ static uint8_t USBD_CCID_Setup (USBD_HandleTypeDef *pdev, * @param epnum: endpoint number * @retval status */ +static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + return USBD_OK; +} static uint8_t USBD_CCID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { //N USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*)pdev->pClassData; PCD_HandleTypeDef *hpcd = pdev->pData; - if(pdev->pClassData != NULL) - { - if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) - { - /* Update the packet total length */ - pdev->ep_in[epnum].total_length = 0U; + // if(pdev->pClassData != NULL) + // { + // if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) + // { + // /* Update the packet total length */ + // pdev->ep_in[epnum].total_length = 0U; - /* Send ZLP */ - USBD_LL_Transmit (pdev, epnum, NULL, 0U); - } - else - { - hcdc->TxState = 0U; - } - return USBD_OK; - } - else - { - return USBD_FAIL; - } + // /* Send ZLP */ + // USBD_LL_Transmit (pdev, epnum, NULL, 0U); + // } + // else + // { + // hcdc->TxState = 0U; + // } + // return USBD_OK; + // } + // else + // { + // return USBD_FAIL; + // } + hcdc->TxState = 0U; + return USBD_OK; } uint8_t USBD_CCID_TransmitPacket(uint8_t * msg, int len) @@ -235,10 +240,21 @@ uint8_t USBD_CCID_TransmitPacket(uint8_t * msg, int len) /* Update the packet total length */ Solo_USBD_Device.ep_in[CCID_IN_EP & 0xFU].total_length = len; + while (PCD_GET_EP_TX_STATUS(USB, CCID_IN_EP & 0x0f) == USB_EP_TX_VALID) + ; /* Transmit next packet */ USBD_LL_Transmit(&Solo_USBD_Device, CCID_IN_EP, msg, len); + + // /* Update the packet total length */ + // Solo_USBD_Device.ep_in[CCID_CMD_EP & 0xFU].total_length = len; + + // /* Transmit next packet */ + // USBD_LL_Transmit(&Solo_USBD_Device, CCID_CMD_EP, msg, + // len); + + return USBD_OK; } @@ -266,8 +282,6 @@ void ccid_send_status(CCID_HEADER * c) } - - void handle_ccid(uint8_t * msg, int len) { CCID_HEADER * h = (CCID_HEADER *) msg; @@ -316,11 +330,7 @@ uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum) else { - - while(1) - { - led_rgb(0xff3520); - } + while(1){ led_rgb(0xff3520); } return USBD_FAIL; } diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index 1aa641d..7d5fdb4 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -34,17 +34,17 @@ static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); #define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4 + 84) #define NUM_INTERFACES 4 #elif NUM_CLASSES>1 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 84) -#define NUM_INTERFACES 2 +#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4) +#define NUM_INTERFACES 3 #else #define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (41) #define NUM_INTERFACES 1 #endif #define HID_INTF_NUM 0 -#define CCID_INTF_NUM 1 -#define CDC_MASTER_INTF_NUM 2 -#define CDC_SLAVE_INTF_NUM 3 +#define CDC_MASTER_INTF_NUM 1 +#define CDC_SLAVE_INTF_NUM 2 +#define CCID_INTF_NUM 3 __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_SIZE] __ALIGN_END = { /*Configuration Descriptor*/ @@ -101,81 +101,6 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x00, HID_BINTERVAL, /*bInterval: Polling Interval */ -#if NUM_INTERFACES>1 - - /* CCID Interface Descriptor */ - 9, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - CCID_INTF_NUM, /* bInterfaceNumber: CCID Interface */ - 0, /* Alternate setting for this interface */ - 3, /* bNumEndpoints: Bulk-IN, Bulk-OUT, Intr-IN */ - 0x0B, /* CCID class */ - 0x00, /* CCID subclass */ - 0x00, /* CCID protocol */ - 0, /* string index for interface */ - - /* ICC Descriptor */ - 54, /* bLength: */ - 0x21, /* bDescriptorType: USBDESCR_ICC */ - 0x10, 0x01, /* bcdCCID: revision 1.1 (of CCID) */ - 0, /* bMaxSlotIndex: */ - 1, /* bVoltageSupport: 5V-only */ - 0x02, 0, 0, 0, /* dwProtocols: T=1 */ - 0xa0, 0x0f, 0, 0, /* dwDefaultClock: 4000 */ - 0xa0, 0x0f, 0, 0, /* dwMaximumClock: 4000 */ - 0, /* bNumClockSupported: 0x00 */ - 0x80, 0x25, 0, 0, /* dwDataRate: 9600 */ - 0x80, 0x25, 0, 0, /* dwMaxDataRate: 9600 */ - 0, /* bNumDataRateSupported: 0x00 */ - 0xfe, 0, 0, 0, /* dwMaxIFSD: 254 */ - 0, 0, 0, 0, /* dwSynchProtocols: 0 */ - 0, 0, 0, 0, /* dwMechanical: 0 */ - 0x7a, 0x04, 0x02, 0x00, /* dwFeatures: - * Short and extended APDU level: 0x40000 ---- - * Short APDU level : 0x20000 * - * (ICCD?) : 0x00800 ---- - * Automatic IFSD : 0x00400 * - * NAD value other than 0x00 : 0x00200 - * Can set ICC in clock stop : 0x00100 - * Automatic PPS CUR : 0x00080 - * Automatic PPS PROP : 0x00040 * - * Auto baud rate change : 0x00020 * - * Auto clock change : 0x00010 * - * Auto voltage selection : 0x00008 * - * Auto activaction of ICC : 0x00004 - * Automatic conf. based on ATR : 0x00002 * - */ - 0x0f, 0x01, 0, 0, /* dwMaxCCIDMessageLength: 271 */ - 0xff, /* bClassGetResponse: 0xff */ - 0x00, /* bClassEnvelope: 0 */ - 0, 0, /* wLCDLayout: 0 */ - 0, /* bPinSupport: No PIN pad */ - - 1, /* bMaxCCIDBusySlots: 1 */ - /*Endpoint IN1 Descriptor*/ - 7, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CCID_IN_EP, /* bEndpointAddress: (IN1) */ - 0x02, /* bmAttributes: Bulk */ - HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ - 0x00, /* bInterval */ - /*Endpoint OUT1 Descriptor*/ - 7, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CCID_OUT_EP, /* bEndpointAddress: (OUT1) */ - 0x02, /* bmAttributes: Bulk */ - HID_FIDO_REPORT_DESC_SIZE, 0x00, /* wMaxPacketSize: */ - 0x00, /* bInterval */ - /*Endpoint IN2 Descriptor*/ - 7, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CCID_CMD_EP, /* bEndpointAddress: (IN2) */ - 0x03, /* bmAttributes: Interrupt */ - 0x04, 0x00, /* wMaxPacketSize: 4 */ - 0xFF, /* bInterval (255ms) */ - -#endif - #if NUM_INTERFACES > 2 /* */ @@ -274,6 +199,81 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x04, #endif +#if NUM_INTERFACES>3 + + /* CCID Interface Descriptor */ + 9, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + CCID_INTF_NUM, /* bInterfaceNumber: CCID Interface */ + 0, /* Alternate setting for this interface */ + 3, /* bNumEndpoints: Bulk-IN, Bulk-OUT, Intr-IN */ + 0x0B, /* CCID class */ + 0x00, /* CCID subclass */ + 0x00, /* CCID protocol */ + 0, /* string index for interface */ + + /* ICC Descriptor */ + 54, /* bLength: */ + 0x21, /* bDescriptorType: USBDESCR_ICC */ + 0x10, 0x01, /* bcdCCID: revision 1.1 (of CCID) */ + 0, /* bMaxSlotIndex: */ + 1, /* bVoltageSupport: 5V-only */ + 0x02, 0, 0, 0, /* dwProtocols: T=1 */ + 0xa0, 0x0f, 0, 0, /* dwDefaultClock: 4000 */ + 0xa0, 0x0f, 0, 0, /* dwMaximumClock: 4000 */ + 0, /* bNumClockSupported: 0x00 */ + 0x80, 0x25, 0, 0, /* dwDataRate: 9600 */ + 0x80, 0x25, 0, 0, /* dwMaxDataRate: 9600 */ + 0, /* bNumDataRateSupported: 0x00 */ + 0xfe, 0, 0, 0, /* dwMaxIFSD: 254 */ + 0, 0, 0, 0, /* dwSynchProtocols: 0 */ + 0, 0, 0, 0, /* dwMechanical: 0 */ + 0x7a, 0x04, 0x02, 0x00, /* dwFeatures: + * Short and extended APDU level: 0x40000 ---- + * Short APDU level : 0x20000 * + * (ICCD?) : 0x00800 ---- + * Automatic IFSD : 0x00400 * + * NAD value other than 0x00 : 0x00200 + * Can set ICC in clock stop : 0x00100 + * Automatic PPS CUR : 0x00080 + * Automatic PPS PROP : 0x00040 * + * Auto baud rate change : 0x00020 * + * Auto clock change : 0x00010 * + * Auto voltage selection : 0x00008 * + * Auto activaction of ICC : 0x00004 + * Automatic conf. based on ATR : 0x00002 * + */ + 0x0f, 0x01, 0, 0, /* dwMaxCCIDMessageLength: 271 */ + 0xff, /* bClassGetResponse: 0xff */ + 0x00, /* bClassEnvelope: 0 */ + 0, 0, /* wLCDLayout: 0 */ + 0, /* bPinSupport: No PIN pad */ + + 1, /* bMaxCCIDBusySlots: 1 */ + /*Endpoint IN1 Descriptor*/ + 7, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CCID_IN_EP, /* bEndpointAddress: (IN1) */ + 0x02, /* bmAttributes: Bulk */ + CCID_DATA_PACKET_SIZE, 0x00, /* wMaxPacketSize: */ + 0x00, /* bInterval */ + /*Endpoint OUT1 Descriptor*/ + 7, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CCID_OUT_EP, /* bEndpointAddress: (OUT1) */ + 0x02, /* bmAttributes: Bulk */ + CCID_DATA_PACKET_SIZE, 0x00, /* wMaxPacketSize: */ + 0x00, /* bInterval */ + /*Endpoint IN2 Descriptor*/ + 7, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CCID_CMD_EP, /* bEndpointAddress: (IN2) */ + 0x03, /* bmAttributes: Interrupt */ + CCID_DATA_PACKET_SIZE, 0x00, /* wMaxPacketSize: 4 */ + 0xFF, /* bInterval (255ms) */ + +#endif + }; @@ -301,7 +301,7 @@ int in_endpoint_to_class[MAX_ENDPOINTS]; int out_endpoint_to_class[MAX_ENDPOINTS]; -void USBD_Composite_Set_Classes(USBD_ClassTypeDef *hid_class, USBD_ClassTypeDef *ccid_class, USBD_ClassTypeDef *cdc_class)) { +void USBD_Composite_Set_Classes(USBD_ClassTypeDef *hid_class, USBD_ClassTypeDef *ccid_class, USBD_ClassTypeDef *cdc_class) { USBD_Classes[0] = hid_class; USBD_Classes[1] = ccid_class; USBD_Classes[2] = cdc_class; @@ -314,10 +314,10 @@ static USBD_ClassTypeDef * getClass(uint8_t index) case HID_INTF_NUM: return USBD_Classes[0]; case CCID_INTF_NUM: - return USBD_Classes[2]; + return USBD_Classes[1]; case CDC_MASTER_INTF_NUM: case CDC_SLAVE_INTF_NUM: - return USBD_Classes[3]; + return USBD_Classes[2]; } return NULL; diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index 2475bdf..c57392c 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -708,12 +708,15 @@ void init_usb() SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); #if DEBUG_LEVEL > 0 - USBD_Composite_Set_Classes(&USBD_HID, &USBD_CDC, &USBD_CCID); + USBD_Composite_Set_Classes(&USBD_HID, &USBD_CCID, &USBD_CDC); in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; - in_endpoint_to_class[CDC_IN_EP & 0x7F] = 1; - out_endpoint_to_class[CDC_OUT_EP & 0x7F] = 1; + in_endpoint_to_class[CCID_IN_EP & 0x7F] = 1; + out_endpoint_to_class[CCID_OUT_EP & 0x7F] = 1; + + in_endpoint_to_class[CDC_IN_EP & 0x7F] = 2; + out_endpoint_to_class[CDC_OUT_EP & 0x7F] = 2; USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); From 0d621d13f9e855748b4469adc72703c67294c584 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Wed, 21 Aug 2019 17:30:37 +0300 Subject: [PATCH 17/42] fix decoding apdu --- fido2/apdu.c | 2 +- fido2/ctap.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fido2/apdu.c b/fido2/apdu.c index df89099..3d5bf49 100644 --- a/fido2/apdu.c +++ b/fido2/apdu.c @@ -13,7 +13,7 @@ int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) { EXT_APDU_HEADER *hapdu = (EXT_APDU_HEADER *)data; - apdu->cla = hapdu->cla; + apdu->cla = hapdu->cla & 0xef; // mask chaining bit if any apdu->ins = hapdu->ins; apdu->p1 = hapdu->p1; apdu->p2 = hapdu->p2; diff --git a/fido2/ctap.c b/fido2/ctap.c index 8edf591..2536c9e 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -1685,7 +1685,7 @@ uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp) break; default: status = CTAP1_ERR_INVALID_COMMAND; - printf2(TAG_ERR,"error, invalid cmd: %x\n", cmd); + printf2(TAG_ERR,"error, invalid cmd: 0x%02x\n", cmd); } done: From 972760eb78d596352b162475d28b0e661176f1ed Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Wed, 21 Aug 2019 20:55:57 +0300 Subject: [PATCH 18/42] added APDU input chaining --- targets/stm32l432/src/nfc.c | 85 ++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 24 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 1d935b0..803f09d 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -15,8 +15,9 @@ #define IS_IRQ_ACTIVE() (1 == (LL_GPIO_ReadInputPort(SOLO_AMS_IRQ_PORT) & SOLO_AMS_IRQ_PIN)) // chain buffer for 61XX responses -static uint8_t resp_chain_buffer[2048] = {0}; -static size_t resp_chain_buffer_len = 0; +static uint8_t chain_buffer[2048] = {0}; +static size_t chain_buffer_len = 0; +static bool chain_buffer_tx = false; uint8_t p14443_block_offset(uint8_t pcb) { uint8_t offset = 1; @@ -298,7 +299,8 @@ void append_get_response(uint8_t *data, size_t rest_len) void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len, bool extapdu) { - resp_chain_buffer_len = 0; + chain_buffer_len = 0; + chain_buffer_tx = true; // if we dont need to break data to parts that need to exchange via GET RESPONSE command (ISO 7816-4 7.1.3) if (len <= 255 || extapdu) @@ -306,16 +308,16 @@ void nfc_write_response_chaining(uint8_t req0, uint8_t * data, int len, bool ext nfc_write_response_chaining_plain(req0, data, len); } else { size_t pcklen = MIN(253, len); - resp_chain_buffer_len = len - pcklen; - printf1(TAG_NFC, "61XX chaining %d/%d.\r\n", pcklen, resp_chain_buffer_len); + chain_buffer_len = len - pcklen; + printf1(TAG_NFC, "61XX chaining %d/%d.\r\n", pcklen, chain_buffer_len); - memmove(resp_chain_buffer, data, pcklen); - append_get_response(&resp_chain_buffer[pcklen], resp_chain_buffer_len); + memmove(chain_buffer, data, pcklen); + append_get_response(&chain_buffer[pcklen], chain_buffer_len); - nfc_write_response_chaining_plain(req0, resp_chain_buffer, pcklen + 2); // 2 for 61XX + nfc_write_response_chaining_plain(req0, chain_buffer, pcklen + 2); // 2 for 61XX // put the rest data into chain buffer - memmove(resp_chain_buffer, &data[pcklen], resp_chain_buffer_len); + memmove(chain_buffer, &data[pcklen], chain_buffer_len); } } @@ -526,7 +528,13 @@ void nfc_process_iblock(uint8_t * buf, int len) uint16_t reslen; uint8_t block_offset = p14443_block_offset(buf[0]); - + + // clear tx chain buffer if we have some other command than GET RESPONSE + if (chain_buffer_tx && buf[block_offset + 1] != APDU_GET_RESPONSE) { + chain_buffer_len = 0; + chain_buffer_tx = false; + } + APDU_STRUCT apdu; if (apdu_decode(buf + block_offset, len - block_offset, &apdu)) { printf1(TAG_NFC,"apdu decode error\r\n"); @@ -536,6 +544,31 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC,"apdu ok. %scase=%02x cla=%02x ins=%02x p1=%02x p2=%02x lc=%d le=%d\r\n", apdu.extended_apdu ? "[e]":"", apdu.case_type, apdu.cla, apdu.ins, apdu.p1, apdu.p2, apdu.lc, apdu.le); + // APDU level chaining. ISO7816-4, 5.1.1. class byte + if (!chain_buffer_tx && buf[block_offset] & 0x10) { + + if (chain_buffer_len + len > sizeof(chain_buffer)) { + nfc_write_response(buf[0], SW_WRONG_LENGTH); + return; + } + + memmove(&chain_buffer[chain_buffer_len], apdu.data, apdu.lc); + chain_buffer_len += apdu.lc; + delay(1); + nfc_write_response(buf[0], SW_SUCCESS); + printf1(TAG_NFC, "APDU chaining ok. %d/%d\r\n", apdu.lc, chain_buffer_len); + return; + } + + // if we have ISO 7816 APDU chain - move there all the data + if (!chain_buffer_tx && chain_buffer_len > 0) { + delay(1); + memmove(&apdu.data[chain_buffer_len], apdu.data, apdu.lc); + memmove(apdu.data, chain_buffer, chain_buffer_len); + apdu.lc += chain_buffer_len; // here apdu struct does not match with memory! + printf1(TAG_NFC, "APDU chaining merge. %d/%d\r\n", chain_buffer_len, apdu.lc); + } + // check CLA if (apdu.cla != 0x00 && apdu.cla != 0x80) { printf1(TAG_NFC, "Unknown CLA %02x\r\n", apdu.cla); @@ -556,11 +589,11 @@ void nfc_process_iblock(uint8_t * buf, int len) } // too many bytes needs. 0x00 and 0x100 - any length - if (apdu.le != 0 && apdu.le != 0x100 && apdu.le > resp_chain_buffer_len) + if (apdu.le != 0 && apdu.le != 0x100 && apdu.le > chain_buffer_len) { uint16_t wlresp = SW_WRONG_LENGTH; // here can be 6700, 6C00, 6FXX. but the most standard way - 67XX or 6700 - if (resp_chain_buffer_len <= 0xff) - wlresp += resp_chain_buffer_len & 0xff; + if (chain_buffer_len <= 0xff) + wlresp += chain_buffer_len & 0xff; nfc_write_response(buf[0], wlresp); printf1(TAG_NFC, "buffer length less than requesteds\r\n"); return; @@ -571,17 +604,17 @@ void nfc_process_iblock(uint8_t * buf, int len) size_t pcklen = 253; if (apdu.le) pcklen = apdu.le; - if (pcklen > resp_chain_buffer_len) - pcklen = resp_chain_buffer_len; + if (pcklen > chain_buffer_len) + pcklen = chain_buffer_len; - printf1(TAG_NFC, "GET RESPONSE. pck len: %d buffer len: %d\r\n", pcklen, resp_chain_buffer_len); + printf1(TAG_NFC, "GET RESPONSE. pck len: %d buffer len: %d\r\n", pcklen, chain_buffer_len); // create packet and add 61XX there if we have another portion(s) of data - memmove(pck, resp_chain_buffer, pcklen); + memmove(pck, chain_buffer, pcklen); size_t dlen = 0; - if (resp_chain_buffer_len - pcklen) + if (chain_buffer_len - pcklen) { - append_get_response(&pck[pcklen], resp_chain_buffer_len - pcklen); + append_get_response(&pck[pcklen], chain_buffer_len - pcklen); dlen = 2; } @@ -589,8 +622,8 @@ void nfc_process_iblock(uint8_t * buf, int len) nfc_write_response_chaining_plain(buf[0], pck, pcklen + dlen); // dlen for 61XX // shift the buffer - resp_chain_buffer_len -= pcklen; - memmove(resp_chain_buffer, &resp_chain_buffer[pcklen], resp_chain_buffer_len); + chain_buffer_len -= pcklen; + memmove(chain_buffer, &chain_buffer[pcklen], chain_buffer_len); break; case APDU_INS_SELECT: @@ -712,6 +745,9 @@ void nfc_process_iblock(uint8_t * buf, int len) // WTX_on(WTX_TIME_DEFAULT); request_from_nfc(true); ctap_response_init(&ctap_resp); + delay(1); + printf1(TAG_NFC,"[%d] ", apdu.lc); + dump_hex1(TAG_NFC,apdu.data, apdu.lc); status = ctap_request(apdu.data, apdu.lc, &ctap_resp); request_from_nfc(false); // if (!WTX_off()) @@ -803,7 +839,7 @@ void nfc_process_block(uint8_t * buf, unsigned int len) uint8_t block_offset = p14443_block_offset(buf[0]); if (buf[0] & 0x10) { - printf1(TAG_NFC_APDU, "NFC_CMD_IBLOCK chaining blen=%d len=%d\r\n", ibuflen, len); + printf1(TAG_NFC_APDU, "NFC_CMD_IBLOCK chaining blen=%d len=%d offs=%d\r\n", ibuflen, len, block_offset); if (ibuflen + len > sizeof(ibuf)) { printf1(TAG_NFC, "I block memory error! must have %d but have only %d\r\n", ibuflen + len, sizeof(ibuf)); @@ -836,14 +872,15 @@ void nfc_process_block(uint8_t * buf, unsigned int len) memmove(ibuf, buf, block_offset); ibuflen += block_offset; - printf1(TAG_NFC_APDU, "NFC_CMD_IBLOCK chaining last block. blen=%d len=%d\r\n", ibuflen, len); + printf1(TAG_NFC_APDU, "NFC_CMD_IBLOCK chaining last block. blen=%d len=%d offset=%d\r\n", ibuflen, len, block_offset); printf1(TAG_NFC_APDU,"i> "); dump_hex1(TAG_NFC_APDU, buf, len); nfc_process_iblock(ibuf, ibuflen); } else { - nfc_process_iblock(buf, len); + memcpy(ibuf, buf, len); // because buf only 32b + nfc_process_iblock(ibuf, len); } clear_ibuf(); } From dfb124dc8bdd3b3234072660a84952c901c73f32 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:04:19 +0300 Subject: [PATCH 19/42] refactoring --- targets/stm32l432/src/nfc.c | 474 ++++++++++++++++++------------------ 1 file changed, 240 insertions(+), 234 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 803f09d..00a2d63 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -520,13 +520,251 @@ int select_applet(uint8_t * aid, int len) return APP_NOTHING; } -void nfc_process_iblock(uint8_t * buf, int len) +void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) { int selected; CTAP_RESPONSE ctap_resp; int status; uint16_t reslen; + // check CLA + if (apdu->cla != 0x00 && apdu->cla != 0x80) { + printf1(TAG_NFC, "Unknown CLA %02x\r\n", apdu->cla); + nfc_write_response(buf0, SW_CLA_INVALID); + return; + } + + // TODO this needs to be organized better + switch(apdu->ins) + { + // ISO 7816. 7.1 GET RESPONSE command + case APDU_GET_RESPONSE: + if (apdu->p1 != 0x00 || apdu->p2 != 0x00) + { + nfc_write_response(buf0, SW_INCORRECT_P1P2); + printf1(TAG_NFC, "P1 or P2 error\r\n"); + return; + } + + // too many bytes needs. 0x00 and 0x100 - any length + if (apdu->le != 0 && apdu->le != 0x100 && apdu->le > chain_buffer_len) + { + uint16_t wlresp = SW_WRONG_LENGTH; // here can be 6700, 6C00, 6FXX. but the most standard way - 67XX or 6700 + if (chain_buffer_len <= 0xff) + wlresp += chain_buffer_len & 0xff; + nfc_write_response(buf0, wlresp); + printf1(TAG_NFC, "buffer length less than requesteds\r\n"); + return; + } + + // create temporary packet + uint8_t pck[255] = {0}; + size_t pcklen = 253; + if (apdu->le) + pcklen = apdu->le; + if (pcklen > chain_buffer_len) + pcklen = chain_buffer_len; + + printf1(TAG_NFC, "GET RESPONSE. pck len: %d buffer len: %d\r\n", pcklen, chain_buffer_len); + + // create packet and add 61XX there if we have another portion(s) of data + memmove(pck, chain_buffer, pcklen); + size_t dlen = 0; + if (chain_buffer_len - pcklen) + { + append_get_response(&pck[pcklen], chain_buffer_len - pcklen); + dlen = 2; + } + + // send + nfc_write_response_chaining_plain(buf0, pck, pcklen + dlen); // dlen for 61XX + + // shift the buffer + chain_buffer_len -= pcklen; + memmove(chain_buffer, &chain_buffer[pcklen], chain_buffer_len); + break; + + case APDU_INS_SELECT: + // if (apdu->p1 == 0 && apdu->p2 == 0x0c) + // { + // printf1(TAG_NFC,"Select NDEF\r\n"); + // + // NFC_STATE.selected_applet = APP_NDEF_TAG; + // // Select NDEF file! + // res[0] = NFC_CMD_IBLOCK | (buf[0] & 1); + // res[1] = SW_SUCCESS>>8; + // res[2] = SW_SUCCESS & 0xff; + // nfc_write_frame(res, 3); + // printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3); + // } + // else + { + selected = select_applet(apdu->data, apdu->lc); + if (selected == APP_FIDO) + { + nfc_write_response_ex(buf0, (uint8_t *)"U2F_V2", 6, SW_SUCCESS); + printf1(TAG_NFC, "FIDO applet selected.\r\n"); + } + else if (selected != APP_NOTHING) + { + nfc_write_response(buf0, SW_SUCCESS); + printf1(TAG_NFC, "SELECTED %d\r\n", selected); + } + else + { + nfc_write_response(buf0, SW_FILE_NOT_FOUND); + printf1(TAG_NFC, "NOT selected "); dump_hex1(TAG_NFC, apdu->data, apdu->lc); + } + } + break; + + case APDU_FIDO_U2F_VERSION: + if (NFC_STATE.selected_applet != APP_FIDO) { + nfc_write_response(buf0, SW_INS_INVALID); + break; + } + + printf1(TAG_NFC, "U2F GetVersion command.\r\n"); + + u2f_request_nfc(apduptr, apdu->data, apdu->lc, &ctap_resp); + nfc_write_response_chaining(buf0, ctap_resp.data, ctap_resp.length, apdu->extended_apdu); + break; + + case APDU_FIDO_U2F_REGISTER: + if (NFC_STATE.selected_applet != APP_FIDO) { + nfc_write_response(buf0, SW_INS_INVALID); + break; + } + + printf1(TAG_NFC, "U2F Register command.\r\n"); + + if (apdu->lc != 64) + { + printf1(TAG_NFC, "U2F Register request length error. len=%d.\r\n", apdu->lc); + nfc_write_response(buf0, SW_WRONG_LENGTH); + return; + } + + timestamp(); + + + // WTX_on(WTX_TIME_DEFAULT); + // SystemClock_Config_LF32(); + // delay(300); + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); + u2f_request_nfc(apduptr, apdu->data, apdu->lc, &ctap_resp); + if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); + // if (!WTX_off()) + // return; + + printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); + printf1(TAG_NFC,"U2F Register P2 took %d\r\n", timestamp()); + nfc_write_response_chaining(buf0, ctap_resp.data, ctap_resp.length, apdu->extended_apdu); + + printf1(TAG_NFC,"U2F Register answered %d (took %d)\r\n", millis(), timestamp()); + break; + + case APDU_FIDO_U2F_AUTHENTICATE: + if (NFC_STATE.selected_applet != APP_FIDO) { + nfc_write_response(buf0, SW_INS_INVALID); + break; + } + + printf1(TAG_NFC, "U2F Authenticate command.\r\n"); + + if (apdu->lc != 64 + 1 + apdu->data[64]) + { + delay(5); + printf1(TAG_NFC, "U2F Authenticate request length error. len=%d keyhlen=%d.\r\n", apdu->lc, apdu->data[64]); + nfc_write_response(buf0, SW_WRONG_LENGTH); + return; + } + + timestamp(); + // WTX_on(WTX_TIME_DEFAULT); + u2f_request_nfc(apduptr, apdu->data, apdu->lc, &ctap_resp); + // if (!WTX_off()) + // return; + + printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); + printf1(TAG_NFC,"U2F Authenticate processing %d (took %d)\r\n", millis(), timestamp()); + nfc_write_response_chaining(buf0, ctap_resp.data, ctap_resp.length, apdu->extended_apdu); + printf1(TAG_NFC,"U2F Authenticate answered %d (took %d)\r\n", millis(), timestamp); + break; + + case APDU_FIDO_NFCCTAP_MSG: + if (NFC_STATE.selected_applet != APP_FIDO) { + nfc_write_response(buf0, SW_INS_INVALID); + return; + } + + printf1(TAG_NFC, "FIDO2 CTAP message. %d\r\n", timestamp()); + + // WTX_on(WTX_TIME_DEFAULT); + request_from_nfc(true); + ctap_response_init(&ctap_resp); + delay(1); + printf1(TAG_NFC,"[%d] ", apdu->lc); + dump_hex1(TAG_NFC, apdu->data, apdu->lc); + status = ctap_request(apdu->data, apdu->lc, &ctap_resp); + request_from_nfc(false); + // if (!WTX_off()) + // return; + + printf1(TAG_NFC, "CTAP resp: 0x%02x len: %d\r\n", status, ctap_resp.length); + + if (status == CTAP1_ERR_SUCCESS) + { + memmove(&ctap_resp.data[1], &ctap_resp.data[0], ctap_resp.length); + ctap_resp.length += 3; + } else { + ctap_resp.length = 3; + } + ctap_resp.data[0] = status; + ctap_resp.data[ctap_resp.length - 2] = SW_SUCCESS >> 8; + ctap_resp.data[ctap_resp.length - 1] = SW_SUCCESS & 0xff; + + printf1(TAG_NFC,"CTAP processing %d (took %d)\r\n", millis(), timestamp()); + nfc_write_response_chaining(buf0, ctap_resp.data, ctap_resp.length, apdu->extended_apdu); + printf1(TAG_NFC,"CTAP answered %d (took %d)\r\n", millis(), timestamp()); + break; + + case APDU_INS_READ_BINARY: + // response length + reslen = apdu->le & 0xffff; + switch(NFC_STATE.selected_applet) + { + case APP_CAPABILITY_CONTAINER: + printf1(TAG_NFC,"APP_CAPABILITY_CONTAINER\r\n"); + if (reslen == 0 || reslen > sizeof(NFC_CC)) + reslen = sizeof(NFC_CC); + nfc_write_response_ex(buf0, (uint8_t *)&NFC_CC, reslen, SW_SUCCESS); + ams_wait_for_tx(10); + break; + case APP_NDEF_TAG: + printf1(TAG_NFC,"APP_NDEF_TAG\r\n"); + if (reslen == 0 || reslen > sizeof(NDEF_SAMPLE) - 1) + reslen = sizeof(NDEF_SAMPLE) - 1; + nfc_write_response_ex(buf0, NDEF_SAMPLE, reslen, SW_SUCCESS); + ams_wait_for_tx(10); + break; + default: + nfc_write_response(buf0, SW_FILE_NOT_FOUND); + printf1(TAG_ERR, "No binary applet selected!\r\n"); + return; + break; + } + break; + + default: + printf1(TAG_NFC, "Unknown INS %02x\r\n", apdu->ins); + nfc_write_response(buf0, SW_INS_INVALID); + break; + } +} + +void nfc_process_iblock(uint8_t * buf, int len) +{ uint8_t block_offset = p14443_block_offset(buf[0]); // clear tx chain buffer if we have some other command than GET RESPONSE @@ -569,240 +807,8 @@ void nfc_process_iblock(uint8_t * buf, int len) printf1(TAG_NFC, "APDU chaining merge. %d/%d\r\n", chain_buffer_len, apdu.lc); } - // check CLA - if (apdu.cla != 0x00 && apdu.cla != 0x80) { - printf1(TAG_NFC, "Unknown CLA %02x\r\n", apdu.cla); - nfc_write_response(buf[0], SW_CLA_INVALID); - return; - } - // TODO this needs to be organized better - switch(apdu.ins) - { - // ISO 7816. 7.1 GET RESPONSE command - case APDU_GET_RESPONSE: - if (apdu.p1 != 0x00 || apdu.p2 != 0x00) - { - nfc_write_response(buf[0], SW_INCORRECT_P1P2); - printf1(TAG_NFC, "P1 or P2 error\r\n"); - return; - } - - // too many bytes needs. 0x00 and 0x100 - any length - if (apdu.le != 0 && apdu.le != 0x100 && apdu.le > chain_buffer_len) - { - uint16_t wlresp = SW_WRONG_LENGTH; // here can be 6700, 6C00, 6FXX. but the most standard way - 67XX or 6700 - if (chain_buffer_len <= 0xff) - wlresp += chain_buffer_len & 0xff; - nfc_write_response(buf[0], wlresp); - printf1(TAG_NFC, "buffer length less than requesteds\r\n"); - return; - } - - // create temporary packet - uint8_t pck[255] = {0}; - size_t pcklen = 253; - if (apdu.le) - pcklen = apdu.le; - if (pcklen > chain_buffer_len) - pcklen = chain_buffer_len; - - printf1(TAG_NFC, "GET RESPONSE. pck len: %d buffer len: %d\r\n", pcklen, chain_buffer_len); - - // create packet and add 61XX there if we have another portion(s) of data - memmove(pck, chain_buffer, pcklen); - size_t dlen = 0; - if (chain_buffer_len - pcklen) - { - append_get_response(&pck[pcklen], chain_buffer_len - pcklen); - dlen = 2; - } - - // send - nfc_write_response_chaining_plain(buf[0], pck, pcklen + dlen); // dlen for 61XX - - // shift the buffer - chain_buffer_len -= pcklen; - memmove(chain_buffer, &chain_buffer[pcklen], chain_buffer_len); - break; - - case APDU_INS_SELECT: - // if (apdu->p1 == 0 && apdu->p2 == 0x0c) - // { - // printf1(TAG_NFC,"Select NDEF\r\n"); - // - // NFC_STATE.selected_applet = APP_NDEF_TAG; - // // Select NDEF file! - // res[0] = NFC_CMD_IBLOCK | (buf[0] & 1); - // res[1] = SW_SUCCESS>>8; - // res[2] = SW_SUCCESS & 0xff; - // nfc_write_frame(res, 3); - // printf1(TAG_NFC,"<< "); dump_hex1(TAG_NFC,res, 3); - // } - // else - { - selected = select_applet(apdu.data, apdu.lc); - if (selected == APP_FIDO) - { - nfc_write_response_ex(buf[0], (uint8_t *)"U2F_V2", 6, SW_SUCCESS); - printf1(TAG_NFC, "FIDO applet selected.\r\n"); - } - else if (selected != APP_NOTHING) - { - nfc_write_response(buf[0], SW_SUCCESS); - printf1(TAG_NFC, "SELECTED %d\r\n", selected); - } - else - { - nfc_write_response(buf[0], SW_FILE_NOT_FOUND); - printf1(TAG_NFC, "NOT selected "); dump_hex1(TAG_NFC, apdu.data, apdu.lc); - } - } - break; - - case APDU_FIDO_U2F_VERSION: - if (NFC_STATE.selected_applet != APP_FIDO) { - nfc_write_response(buf[0], SW_INS_INVALID); - break; - } - - printf1(TAG_NFC, "U2F GetVersion command.\r\n"); - - u2f_request_nfc(&buf[block_offset], apdu.data, apdu.lc, &ctap_resp); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); - break; - - case APDU_FIDO_U2F_REGISTER: - if (NFC_STATE.selected_applet != APP_FIDO) { - nfc_write_response(buf[0], SW_INS_INVALID); - break; - } - - printf1(TAG_NFC, "U2F Register command.\r\n"); - - if (apdu.lc != 64) - { - printf1(TAG_NFC, "U2F Register request length error. len=%d.\r\n", apdu.lc); - nfc_write_response(buf[0], SW_WRONG_LENGTH); - return; - } - - timestamp(); - - - // WTX_on(WTX_TIME_DEFAULT); - // SystemClock_Config_LF32(); - // delay(300); - if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_FAST); - u2f_request_nfc(&buf[block_offset], apdu.data, apdu.lc, &ctap_resp); - if (device_is_nfc() == NFC_IS_ACTIVE) device_set_clock_rate(DEVICE_LOW_POWER_IDLE); - // if (!WTX_off()) - // return; - - printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); - printf1(TAG_NFC,"U2F Register P2 took %d\r\n", timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); - - printf1(TAG_NFC,"U2F Register answered %d (took %d)\r\n", millis(), timestamp()); - break; - - case APDU_FIDO_U2F_AUTHENTICATE: - if (NFC_STATE.selected_applet != APP_FIDO) { - nfc_write_response(buf[0], SW_INS_INVALID); - break; - } - - printf1(TAG_NFC, "U2F Authenticate command.\r\n"); - - if (apdu.lc != 64 + 1 + apdu.data[64]) - { - delay(5); - printf1(TAG_NFC, "U2F Authenticate request length error. len=%d keyhlen=%d.\r\n", apdu.lc, apdu.data[64]); - nfc_write_response(buf[0], SW_WRONG_LENGTH); - return; - } - - timestamp(); - // WTX_on(WTX_TIME_DEFAULT); - u2f_request_nfc(&buf[block_offset], apdu.data, apdu.lc, &ctap_resp); - // if (!WTX_off()) - // return; - - printf1(TAG_NFC, "U2F resp len: %d\r\n", ctap_resp.length); - printf1(TAG_NFC,"U2F Authenticate processing %d (took %d)\r\n", millis(), timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); - printf1(TAG_NFC,"U2F Authenticate answered %d (took %d)\r\n", millis(), timestamp); - break; - - case APDU_FIDO_NFCCTAP_MSG: - if (NFC_STATE.selected_applet != APP_FIDO) { - nfc_write_response(buf[0], SW_INS_INVALID); - return; - } - - printf1(TAG_NFC, "FIDO2 CTAP message. %d\r\n", timestamp()); - - // WTX_on(WTX_TIME_DEFAULT); - request_from_nfc(true); - ctap_response_init(&ctap_resp); - delay(1); - printf1(TAG_NFC,"[%d] ", apdu.lc); - dump_hex1(TAG_NFC,apdu.data, apdu.lc); - status = ctap_request(apdu.data, apdu.lc, &ctap_resp); - request_from_nfc(false); - // if (!WTX_off()) - // return; - - printf1(TAG_NFC, "CTAP resp: 0x%02x len: %d\r\n", status, ctap_resp.length); - - if (status == CTAP1_ERR_SUCCESS) - { - memmove(&ctap_resp.data[1], &ctap_resp.data[0], ctap_resp.length); - ctap_resp.length += 3; - } else { - ctap_resp.length = 3; - } - ctap_resp.data[0] = status; - ctap_resp.data[ctap_resp.length - 2] = SW_SUCCESS >> 8; - ctap_resp.data[ctap_resp.length - 1] = SW_SUCCESS & 0xff; - - printf1(TAG_NFC,"CTAP processing %d (took %d)\r\n", millis(), timestamp()); - nfc_write_response_chaining(buf[0], ctap_resp.data, ctap_resp.length, apdu.extended_apdu); - printf1(TAG_NFC,"CTAP answered %d (took %d)\r\n", millis(), timestamp()); - break; - - case APDU_INS_READ_BINARY: - // response length - reslen = apdu.le & 0xffff; - switch(NFC_STATE.selected_applet) - { - case APP_CAPABILITY_CONTAINER: - printf1(TAG_NFC,"APP_CAPABILITY_CONTAINER\r\n"); - if (reslen == 0 || reslen > sizeof(NFC_CC)) - reslen = sizeof(NFC_CC); - nfc_write_response_ex(buf[0], (uint8_t *)&NFC_CC, reslen, SW_SUCCESS); - ams_wait_for_tx(10); - break; - case APP_NDEF_TAG: - printf1(TAG_NFC,"APP_NDEF_TAG\r\n"); - if (reslen == 0 || reslen > sizeof(NDEF_SAMPLE) - 1) - reslen = sizeof(NDEF_SAMPLE) - 1; - nfc_write_response_ex(buf[0], NDEF_SAMPLE, reslen, SW_SUCCESS); - ams_wait_for_tx(10); - break; - default: - nfc_write_response(buf[0], SW_FILE_NOT_FOUND); - printf1(TAG_ERR, "No binary applet selected!\r\n"); - return; - break; - } - break; - - default: - printf1(TAG_NFC, "Unknown INS %02x\r\n", apdu.ins); - nfc_write_response(buf[0], SW_INS_INVALID); - break; - } + apdu_process(buf[0], &buf[block_offset], &apdu); printf1(TAG_NFC,"prev.Iblock: "); dump_hex1(TAG_NFC, buf, len); From c79b7abfb6a86822c88ed21bc6132f1d638f7eca Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:12:59 +0300 Subject: [PATCH 20/42] add reset placeholder --- fido2/apdu.h | 4 ++++ targets/stm32l432/src/nfc.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/fido2/apdu.h b/fido2/apdu.h index 7ce8477..420fea6 100644 --- a/fido2/apdu.h +++ b/fido2/apdu.h @@ -42,6 +42,10 @@ extern int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu); #define APDU_FIDO_U2F_AUTHENTICATE 0x02 #define APDU_FIDO_U2F_VERSION 0x03 #define APDU_FIDO_NFCCTAP_MSG 0x10 +#define APDU_FIDO_U2F_VENDOR_FIRST 0xc0 // First vendor defined command +#define APDU_FIDO_U2F_VENDOR_LAST 0xff // Last vendor defined command +#define APDU_SOLO_RESET 0xee + #define APDU_INS_SELECT 0xA4 #define APDU_INS_READ_BINARY 0xB0 #define APDU_GET_RESPONSE 0xC0 diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 00a2d63..b2aded8 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -756,6 +756,12 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) } break; + case APDU_SOLO_RESET: + printf1(TAG_NFC, "Reset...\r\n"); + delay(10); + nfc_write_response(buf0, SW_INS_INVALID); + break; + default: printf1(TAG_NFC, "Unknown INS %02x\r\n", apdu->ins); nfc_write_response(buf0, SW_INS_INVALID); From a95e62e2ea80eeee08ecd936837af8bd50ba5217 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:33:28 +0300 Subject: [PATCH 21/42] reset --- targets/stm32l432/src/nfc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index b2aded8..d49782d 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -757,9 +757,15 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) break; case APDU_SOLO_RESET: - printf1(TAG_NFC, "Reset...\r\n"); - delay(10); - nfc_write_response(buf0, SW_INS_INVALID); + if (apdu.lc == 18 && !memcmp(apdu.data, "Reset solo please>", 18)) { + printf1(TAG_NFC, "Reset...\r\n"); + delay(10); + NVIC_SystemReset(); + while(1); + } else { + printf1(TAG_NFC, "Reset FAIL\r\n"); + nfc_write_response(buf[0], SW_INS_INVALID); + } break; default: From c972a130345fed901408228ebcbe15c153ecedaa Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:49:33 +0300 Subject: [PATCH 22/42] fix reboot --- fido2/device.h | 1 + pc/device.c | 6 +++++- targets/stm32l432/src/nfc.c | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fido2/device.h b/fido2/device.h index 0c11fe8..d440ca6 100644 --- a/fido2/device.h +++ b/fido2/device.h @@ -30,6 +30,7 @@ void main_loop_delay(); void heartbeat(); +void device_reboot(); void authenticator_read_state(AuthenticatorState * ); diff --git a/pc/device.c b/pc/device.c index 0944254..cf91610 100644 --- a/pc/device.c +++ b/pc/device.c @@ -43,7 +43,11 @@ void device_set_status(uint32_t status) __device_status = status; } - +void device_reboot() +{ + printf1(TAG_RED, "REBOOT command recieved!\r\n"); + exit(100); +} int udp_server() { diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index d49782d..ed01e9a 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -757,10 +757,10 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) break; case APDU_SOLO_RESET: - if (apdu.lc == 18 && !memcmp(apdu.data, "Reset solo please>", 18)) { + if (apdu.lc == 4 && !memcmp(apdu.data, "\x12\x56\xab\xf0", 4)) { printf1(TAG_NFC, "Reset...\r\n"); delay(10); - NVIC_SystemReset(); + device_reboot(); while(1); } else { printf1(TAG_NFC, "Reset FAIL\r\n"); From d39d7978fdf4aa3e76af17e9409ad6fc62639201 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 15:58:53 +0300 Subject: [PATCH 23/42] small fix --- targets/stm32l432/src/nfc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index ed01e9a..3dd8725 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -757,14 +757,14 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) break; case APDU_SOLO_RESET: - if (apdu.lc == 4 && !memcmp(apdu.data, "\x12\x56\xab\xf0", 4)) { + if (apdu->lc == 4 && !memcmp(apdu->data, "\x12\x56\xab\xf0", 4)) { printf1(TAG_NFC, "Reset...\r\n"); delay(10); device_reboot(); while(1); } else { printf1(TAG_NFC, "Reset FAIL\r\n"); - nfc_write_response(buf[0], SW_INS_INVALID); + nfc_write_response(buf0, SW_INS_INVALID); } break; From 8af6505f6d7e672a652fad2146874d1322710344 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Thu, 22 Aug 2019 19:40:02 +0000 Subject: [PATCH 24/42] docs: update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba13ea7..a86fb7b 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Piotr Esden-Tempski
Piotr Esden-Tempski

💼 f.m3hm00d
f.m3hm00d

📖 Richard Hughes
Richard Hughes

🤔 💻 🚇 🔧 + Kim Schulz
Kim Schulz

💼 🤔 @@ -167,7 +168,7 @@ You can buy Solo, Solo Tap, and Solo for Hackers at [solokeys.com](https://solok
[![License](https://img.shields.io/github/license/solokeys/solo.svg)](https://github.com/solokeys/solo/blob/master/LICENSE) -[![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors) [![Build Status](https://travis-ci.com/solokeys/solo.svg?branch=master)](https://travis-ci.com/solokeys/solo) [![Discourse Users](https://img.shields.io/discourse/https/discourse.solokeys.com/users.svg)](https://discourse.solokeys.com) [![Keybase Chat](https://img.shields.io/badge/chat-on%20keybase-brightgreen.svg)](https://keybase.io/team/solokeys.public) From 8973608f59d461eacfda4f1423531b4d311f3b97 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Thu, 22 Aug 2019 19:40:03 +0000 Subject: [PATCH 25/42] docs: update .all-contributorsrc --- .all-contributorsrc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index aed6148..253fb36 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -168,6 +168,16 @@ "infra", "tool" ] + }, + { + "login": "kimusan", + "name": "Kim Schulz", + "avatar_url": "https://avatars1.githubusercontent.com/u/1150049?v=4", + "profile": "http://www.schulz.dk", + "contributions": [ + "business", + "ideas" + ] } ], "contributorsPerLine": 7, From 3fad9a7a7db234ba3709222d001b207dc1843b99 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 22 Aug 2019 17:43:11 +0300 Subject: [PATCH 26/42] add response to reset command and delete debug --- targets/stm32l432/src/nfc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 3dd8725..73f1d3a 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -703,9 +703,6 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) // WTX_on(WTX_TIME_DEFAULT); request_from_nfc(true); ctap_response_init(&ctap_resp); - delay(1); - printf1(TAG_NFC,"[%d] ", apdu->lc); - dump_hex1(TAG_NFC, apdu->data, apdu->lc); status = ctap_request(apdu->data, apdu->lc, &ctap_resp); request_from_nfc(false); // if (!WTX_off()) @@ -759,7 +756,8 @@ void apdu_process(uint8_t buf0, uint8_t *apduptr, APDU_STRUCT *apdu) case APDU_SOLO_RESET: if (apdu->lc == 4 && !memcmp(apdu->data, "\x12\x56\xab\xf0", 4)) { printf1(TAG_NFC, "Reset...\r\n"); - delay(10); + nfc_write_response(buf0, SW_SUCCESS); + delay(20); device_reboot(); while(1); } else { From 3b53537077184090170ec65b4db9ef23f7a66853 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 23 Aug 2019 13:09:23 +0800 Subject: [PATCH 27/42] refactor fido2 user presence handling & increase timeout to 29s --- fido2/ctap.c | 41 ++++++++++++++++++----------------------- fido2/ctap.h | 2 +- fido2/ctap_errors.h | 1 + 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/fido2/ctap.c b/fido2/ctap.c index 2536c9e..8f49102 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -437,7 +437,19 @@ static unsigned int get_credential_id_size(CTAP_credentialDescriptor * cred) static int ctap2_user_presence_test() { device_set_status(CTAPHID_STATUS_UPNEEDED); - return ctap_user_presence_test(CTAP2_UP_DELAY_MS); + int ret = ctap_user_presence_test(CTAP2_UP_DELAY_MS); + if ( ret > 0 ) + { + return CTAP1_ERR_SUCCESS; + } + else if (ret < 0) + { + return CTAP2_ERR_KEEPALIVE_CANCEL; + } + else + { + return CTAP2_ERR_ACTION_TIMEOUT; + } } static int ctap_make_auth_data(struct rpId * rp, CborEncoder * map, uint8_t * auth_data_buf, uint32_t * len, CTAP_credInfo * credInfo) @@ -470,15 +482,7 @@ static int ctap_make_auth_data(struct rpId * rp, CborEncoder * map, uint8_t * au int but; but = ctap2_user_presence_test(CTAP2_UP_DELAY_MS); - - if (!but) - { - return CTAP2_ERR_OPERATION_DENIED; - } - else if (but < 0) // Cancel - { - return CTAP2_ERR_KEEPALIVE_CANCEL; - } + check_retr(but); device_set_status(CTAPHID_STATUS_PROCESSING); @@ -707,10 +711,7 @@ uint8_t ctap_make_credential(CborEncoder * encoder, uint8_t * request, int lengt } if (MC.pinAuthEmpty) { - if (!ctap2_user_presence_test(CTAP2_UP_DELAY_MS)) - { - return CTAP2_ERR_OPERATION_DENIED; - } + check_retr( ctap2_user_presence_test(CTAP2_UP_DELAY_MS) ); return ctap_is_pin_set() == 1 ? CTAP2_ERR_PIN_AUTH_INVALID : CTAP2_ERR_PIN_NOT_SET; } if ((MC.paramsParsed & MC_requiredMask) != MC_requiredMask) @@ -1143,10 +1144,7 @@ uint8_t ctap_get_assertion(CborEncoder * encoder, uint8_t * request, int length) if (GA.pinAuthEmpty) { - if (!ctap2_user_presence_test(CTAP2_UP_DELAY_MS)) - { - return CTAP2_ERR_OPERATION_DENIED; - } + check_retr( ctap2_user_presence_test(CTAP2_UP_DELAY_MS) ); return ctap_is_pin_set() == 1 ? CTAP2_ERR_PIN_AUTH_INVALID : CTAP2_ERR_PIN_NOT_SET; } if (GA.pinAuthPresent) @@ -1656,14 +1654,11 @@ uint8_t ctap_request(uint8_t * pkt_raw, int length, CTAP_RESPONSE * resp) break; case CTAP_RESET: printf1(TAG_CTAP,"CTAP_RESET\n"); - if (ctap2_user_presence_test(CTAP2_UP_DELAY_MS)) + status = ctap2_user_presence_test(CTAP2_UP_DELAY_MS); + if (status == CTAP1_ERR_SUCCESS) { ctap_reset(); } - else - { - status = CTAP2_ERR_OPERATION_DENIED; - } break; case GET_NEXT_ASSERTION: printf1(TAG_CTAP,"CTAP_NEXT_ASSERTION\n"); diff --git a/fido2/ctap.h b/fido2/ctap.h index 51a5c11..6bfdf57 100644 --- a/fido2/ctap.h +++ b/fido2/ctap.h @@ -131,7 +131,7 @@ #define PIN_LOCKOUT_ATTEMPTS 8 // Number of attempts total #define PIN_BOOT_ATTEMPTS 3 // number of attempts per boot -#define CTAP2_UP_DELAY_MS 5000 +#define CTAP2_UP_DELAY_MS 29000 typedef struct { diff --git a/fido2/ctap_errors.h b/fido2/ctap_errors.h index a749651..fc2f257 100644 --- a/fido2/ctap_errors.h +++ b/fido2/ctap_errors.h @@ -49,6 +49,7 @@ #define CTAP2_ERR_PIN_POLICY_VIOLATION 0x37 #define CTAP2_ERR_PIN_TOKEN_EXPIRED 0x38 #define CTAP2_ERR_REQUEST_TOO_LARGE 0x39 +#define CTAP2_ERR_ACTION_TIMEOUT 0x3A #define CTAP1_ERR_OTHER 0x7F #define CTAP2_ERR_SPEC_LAST 0xDF #define CTAP2_ERR_EXTENSION_FIRST 0xE0 From b3ac739a359afc1617ca64a71b9253f85a1242b6 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 23 Aug 2019 13:33:34 +0800 Subject: [PATCH 28/42] make touch sensor edge based to avoid approving >1 transaction --- targets/stm32l432/src/device.c | 37 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 49bbfe7..8c5f679 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -60,6 +60,29 @@ static int is_touch_button_pressed() int (*IS_BUTTON_PRESSED)() = is_physical_button_pressed; +static void edge_detect_touch_button() +{ + static uint8_t last_touch = 0; + uint8_t current_touch = 0; + if (is_touch_button_pressed == IS_BUTTON_PRESSED) + { + current_touch = IS_BUTTON_PRESSED(); + + // 1 sample per 25 ms + if ((millis() - __last_button_bounce_time) > 25) + { + // Detect "touch / rising edge" + if (!last_touch && current_touch) + { + __last_button_press_time = millis(); + } + __last_button_bounce_time = millis(); + last_touch = current_touch; + } + } + +} + void request_from_nfc(bool request_active) { _RequestComeFromNFC = request_active; } @@ -78,19 +101,7 @@ void TIM6_DAC_IRQHandler() } } - - if (is_touch_button_pressed == IS_BUTTON_PRESSED) - { - if (IS_BUTTON_PRESSED()) - { - // Only allow 1 press per 25 ms. - if ((millis() - __last_button_bounce_time) > 25) - { - __last_button_press_time = millis(); - } - __last_button_bounce_time = millis(); - } - } + edge_detect_touch_button(); #ifndef IS_BOOTLOADER // NFC sending WTX if needs From affc256ca21657b4b809624a34285f372404eb54 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 23 Aug 2019 14:28:25 +0800 Subject: [PATCH 29/42] add delay to cap button improve reliability --- targets/stm32l432/src/device.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 8c5f679..65bfdb4 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -55,7 +55,14 @@ static int is_physical_button_pressed() static int is_touch_button_pressed() { - return tsc_read_button(0) || tsc_read_button(1); + int is_pressed = (tsc_read_button(0) || tsc_read_button(1)); + if (is_pressed) + { + // delay for debounce, and longer than polling timer period. + delay(95); + return (tsc_read_button(0) || tsc_read_button(1)); + } + return is_pressed; } int (*IS_BUTTON_PRESSED)() = is_physical_button_pressed; @@ -66,7 +73,7 @@ static void edge_detect_touch_button() uint8_t current_touch = 0; if (is_touch_button_pressed == IS_BUTTON_PRESSED) { - current_touch = IS_BUTTON_PRESSED(); + current_touch = (tsc_read_button(0) || tsc_read_button(1)); // 1 sample per 25 ms if ((millis() - __last_button_bounce_time) > 25) @@ -153,7 +160,6 @@ void device_set_status(uint32_t status) int device_is_button_pressed() { - return IS_BUTTON_PRESSED(); } From 8e192f236310330ed5b0d0278652cc03452491e2 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 23 Aug 2019 14:31:03 +0800 Subject: [PATCH 30/42] do not delay bootloader --- targets/stm32l432/src/device.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 65bfdb4..e6f14e3 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -56,12 +56,14 @@ static int is_physical_button_pressed() static int is_touch_button_pressed() { int is_pressed = (tsc_read_button(0) || tsc_read_button(1)); +#ifndef IS_BOOTLOADER if (is_pressed) { // delay for debounce, and longer than polling timer period. delay(95); return (tsc_read_button(0) || tsc_read_button(1)); } +#endif return is_pressed; } From 41ceb78f6ca7aa13157d9e32f3602c075b3e97fe Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 23 Aug 2019 14:48:21 +0800 Subject: [PATCH 31/42] add user presence to flags --- fido2/ctap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fido2/ctap.c b/fido2/ctap.c index 8f49102..f721990 100644 --- a/fido2/ctap.c +++ b/fido2/ctap.c @@ -486,7 +486,7 @@ static int ctap_make_auth_data(struct rpId * rp, CborEncoder * map, uint8_t * au device_set_status(CTAPHID_STATUS_PROCESSING); - authData->head.flags = (but << 0); + authData->head.flags = (1 << 0); // User presence authData->head.flags |= (ctap_is_pin_set() << 2); From 5d3914bc5e8c7326d7f0cdd2afd1c042a6f821d9 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 23 Aug 2019 14:36:15 +0300 Subject: [PATCH 32/42] remove delays --- targets/stm32l432/src/nfc.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 73f1d3a..6a3cab3 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -802,7 +802,6 @@ void nfc_process_iblock(uint8_t * buf, int len) memmove(&chain_buffer[chain_buffer_len], apdu.data, apdu.lc); chain_buffer_len += apdu.lc; - delay(1); nfc_write_response(buf[0], SW_SUCCESS); printf1(TAG_NFC, "APDU chaining ok. %d/%d\r\n", apdu.lc, chain_buffer_len); return; @@ -810,7 +809,6 @@ void nfc_process_iblock(uint8_t * buf, int len) // if we have ISO 7816 APDU chain - move there all the data if (!chain_buffer_tx && chain_buffer_len > 0) { - delay(1); memmove(&apdu.data[chain_buffer_len], apdu.data, apdu.lc); memmove(apdu.data, chain_buffer, chain_buffer_len); apdu.lc += chain_buffer_len; // here apdu struct does not match with memory! From bde4c09c21ad943bf20634455b90f6a29ab5e527 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 24 Aug 2019 15:06:16 +0800 Subject: [PATCH 33/42] CCID basics working --- targets/stm32l432/lib/usbd/usbd_ccid.c | 104 ++++++++----------------- targets/stm32l432/lib/usbd/usbd_ccid.h | 17 +++- targets/stm32l432/lib/usbd/usbd_conf.c | 21 ++--- 3 files changed, 62 insertions(+), 80 deletions(-) diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.c b/targets/stm32l432/lib/usbd/usbd_ccid.c index 2a81ef7..76b8b8e 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.c +++ b/targets/stm32l432/lib/usbd/usbd_ccid.c @@ -64,6 +64,8 @@ static uint8_t USBD_CCID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) USBD_LL_OpenEP(pdev, CCID_CMD_EP, USBD_EP_TYPE_INTR, CCID_DATA_PACKET_SIZE); pdev->ep_in[CCID_CMD_EP & 0xFU].is_used = 1U; + // dump_pma_header("ccid.c"); + static USBD_CCID_HandleTypeDef mem; pdev->pClassData = &mem; @@ -207,30 +209,8 @@ static uint8_t USBD_CCID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) } static uint8_t USBD_CCID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { - //N USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*)pdev->pClassData; - PCD_HandleTypeDef *hpcd = pdev->pData; - // if(pdev->pClassData != NULL) - // { - // if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) - // { - // /* Update the packet total length */ - // pdev->ep_in[epnum].total_length = 0U; - - // /* Send ZLP */ - // USBD_LL_Transmit (pdev, epnum, NULL, 0U); - // } - // else - // { - // hcdc->TxState = 0U; - // } - // return USBD_OK; - // } - // else - // { - // return USBD_FAIL; - // } hcdc->TxState = 0U; return USBD_OK; } @@ -246,41 +226,39 @@ uint8_t USBD_CCID_TransmitPacket(uint8_t * msg, int len) USBD_LL_Transmit(&Solo_USBD_Device, CCID_IN_EP, msg, len); - - // /* Update the packet total length */ - // Solo_USBD_Device.ep_in[CCID_CMD_EP & 0xFU].total_length = len; - - // /* Transmit next packet */ - // USBD_LL_Transmit(&Solo_USBD_Device, CCID_CMD_EP, msg, - // len); - + printf1(TAG_GREEN,"ccid<< "); + dump_hex1(TAG_GREEN, msg, len); return USBD_OK; } -#define CCID_HEADER_SIZE 10 -typedef struct -{ - uint8_t type; - uint32_t len; - uint8_t slot; - uint8_t seq; - uint8_t rsvd; - uint16_t param; -} __attribute__((packed)) CCID_HEADER; -void ccid_send_status(CCID_HEADER * c) + +void ccid_send_status(CCID_HEADER * c, uint8_t status) { uint8_t msg[CCID_HEADER_SIZE]; memset(msg,0,sizeof(msg)); msg[0] = CCID_SLOT_STATUS_RES; - msg[6] = 1; + msg[6] = c->seq; + msg[7] = status; USBD_CCID_TransmitPacket(msg, sizeof(msg)); } +void ccid_send_data_block(CCID_HEADER * c, uint8_t status) +{ + uint8_t msg[CCID_HEADER_SIZE]; + memset(msg,0,sizeof(msg)); + + msg[0] = CCID_DATA_BLOCK_RES; + msg[6] = c->seq; + msg[7] = status; + + USBD_CCID_TransmitPacket(msg, sizeof(msg)); + +} void handle_ccid(uint8_t * msg, int len) { @@ -288,15 +266,16 @@ void handle_ccid(uint8_t * msg, int len) switch(h->type) { case CCID_SLOT_STATUS: - ccid_send_status(h); + ccid_send_status(h, CCID_STATUS_ON); + break; + case CCID_POWER_ON: + ccid_send_data_block(h, CCID_STATUS_ON); + break; + case CCID_POWER_OFF: + ccid_send_status(h, CCID_STATUS_OFF); break; default: - // while(1) - // { - // led_rgb(0xff3520); - // } - //Y - ccid_send_status(h); + ccid_send_status(h, CCID_STATUS_ON); break; } } @@ -316,24 +295,15 @@ uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum) /* Get the received data length */ hcdc->RxLength = USBD_LL_GetRxDataSize (pdev, epnum); - /* USB data will be immediately processed, this allow next USB traffic being - NAKed till the end of the application Xfer */ - if(pdev->pClassData != NULL) - { - // printf1(TAG_GREEN,"ccid>> "); - // dump_hex1(TAG_GREEN, hcdc->RxBuffer, hcdc->RxLength); + printf1(TAG_GREEN, "ccid>> "); + dump_hex1(TAG_GREEN, ccidmsg_buf, hcdc->RxLength); - handle_ccid(hcdc->RxBuffer, hcdc->RxLength); + handle_ccid(ccidmsg_buf, hcdc->RxLength); - return USBD_OK; - } - else - { + USBD_LL_PrepareReceive(&Solo_USBD_Device, CCID_OUT_EP, ccidmsg_buf, + CCID_DATA_PACKET_SIZE); - while(1){ led_rgb(0xff3520); } - - return USBD_FAIL; - } + return USBD_OK; } @@ -345,11 +315,5 @@ uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum) */ static uint8_t USBD_CCID_EP0_RxReady (USBD_HandleTypeDef *pdev) { - USBD_CCID_HandleTypeDef *hcdc = (USBD_CCID_HandleTypeDef*) pdev->pClassData; - - // if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) - // { - // hcdc->CmdOpCode = 0xFFU; - // } return USBD_OK; } diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.h b/targets/stm32l432/lib/usbd/usbd_ccid.h index 6535987..513c796 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.h +++ b/targets/stm32l432/lib/usbd/usbd_ccid.h @@ -3,7 +3,18 @@ #include "usbd_ioreq.h" -#define CCID_IN_EP 0x84U /* EP1 for data IN */ +#define CCID_HEADER_SIZE 10 +typedef struct +{ + uint8_t type; + uint32_t len; + uint8_t slot; + uint8_t seq; + uint8_t rsvd; + uint16_t param; +} __attribute__((packed)) CCID_HEADER; + +#define CCID_IN_EP 0x86U /* EP1 for data IN */ #define CCID_OUT_EP 0x04U /* EP1 for data OUT */ #define CCID_CMD_EP 0x85U /* EP2 for CDC commands */ @@ -17,6 +28,10 @@ #define CCID_GET_PARAMS 0x6C #define CCID_RESET_PARAMS 0x6D #define CCID_XFR_BLOCK 0x6F + +#define CCID_STATUS_ON 0x00 +#define CCID_STATUS_OFF 0x02 + #define CCID_DATA_BLOCK_RES 0x80 #define CCID_SLOT_STATUS_RES 0x81 #define CCID_PARAMS_RES 0x82 diff --git a/targets/stm32l432/lib/usbd/usbd_conf.c b/targets/stm32l432/lib/usbd/usbd_conf.c index 61bcecd..ad5b211 100644 --- a/targets/stm32l432/lib/usbd/usbd_conf.c +++ b/targets/stm32l432/lib/usbd/usbd_conf.c @@ -52,6 +52,7 @@ #include "usbd_hid.h" #include "usbd_cdc.h" #include "usbd_ccid.h" +#include "log.h" void SystemClock_Config(void); @@ -223,7 +224,6 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) { USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData); } - /** * @brief Initializes the low level portion of the device driver. * @param pdev: Device handle @@ -260,15 +260,17 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , HID_EPOUT_ADDR , PCD_SNG_BUF, 0x98); HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , HID_EPIN_ADDR , PCD_SNG_BUF, 0xd8); - // CDC / uart - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, 0xd8 + 64); // data OUT - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_IN_EP , PCD_SNG_BUF, 0xd8 + 64*2); // data IN - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*3); // commands - // CCID - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_OUT_EP , PCD_SNG_BUF, 0xd8 + 64*4); // data OUT - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_IN_EP , PCD_SNG_BUF, 0xd8 + 64*5); // data IN - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*6); // commands + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_OUT_EP , PCD_SNG_BUF, 0xd8 + 64); // data OUT + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_IN_EP , PCD_SNG_BUF, 0xd8 + 64*2); // data IN + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CCID_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*3); // commands + + // CDC / uart + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, 0xd8 + 64*4); // commands + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, 0xd8 + 64*5); // data OUT + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_IN_EP , PCD_SNG_BUF, 0xd8 + 64*6); // data IN + + // dump_pma_header("usbd_conf"); return USBD_OK; } @@ -319,6 +321,7 @@ USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_type, uint16_t ep_mps) { + // printf1(TAG_RED,"LL_Open. ep: %x, %x\r\n", ep_addr, ep_type); HAL_PCD_EP_Open((PCD_HandleTypeDef*) pdev->pData, ep_addr, ep_mps, From ccd9a04146c5ebdc241e9ee3077597d62e07092b Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 24 Aug 2019 15:08:14 +0800 Subject: [PATCH 34/42] add ccid log tag --- fido2/log.c | 1 + fido2/log.h | 1 + fido2/main.c | 1 + targets/stm32l432/lib/usbd/usbd_ccid.c | 8 ++++---- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/fido2/log.c b/fido2/log.c index c4d874e..af2cd55 100644 --- a/fido2/log.c +++ b/fido2/log.c @@ -50,6 +50,7 @@ struct logtag tagtable[] = { {TAG_EXT,"EXT"}, {TAG_NFC,"NFC"}, {TAG_NFC_APDU, "NAPDU"}, + {TAG_CCID, "CCID"}, }; diff --git a/fido2/log.h b/fido2/log.h index d7190cf..17a2d6b 100644 --- a/fido2/log.h +++ b/fido2/log.h @@ -44,6 +44,7 @@ typedef enum TAG_EXT = (1 << 18), TAG_NFC = (1 << 19), TAG_NFC_APDU = (1 << 20), + TAG_CCID = (1 << 21), TAG_NO_TAG = (1UL << 30), TAG_FILENO = (1UL << 31) diff --git a/fido2/main.c b/fido2/main.c index f794375..a22d48c 100644 --- a/fido2/main.c +++ b/fido2/main.c @@ -46,6 +46,7 @@ int main(int argc, char *argv[]) TAG_GREEN| TAG_RED| TAG_EXT| + TAG_CCID| TAG_ERR ); diff --git a/targets/stm32l432/lib/usbd/usbd_ccid.c b/targets/stm32l432/lib/usbd/usbd_ccid.c index 76b8b8e..0a0a797 100644 --- a/targets/stm32l432/lib/usbd/usbd_ccid.c +++ b/targets/stm32l432/lib/usbd/usbd_ccid.c @@ -226,8 +226,8 @@ uint8_t USBD_CCID_TransmitPacket(uint8_t * msg, int len) USBD_LL_Transmit(&Solo_USBD_Device, CCID_IN_EP, msg, len); - printf1(TAG_GREEN,"ccid<< "); - dump_hex1(TAG_GREEN, msg, len); + printf1(TAG_CCID,"<< "); + dump_hex1(TAG_CCID, msg, len); return USBD_OK; } @@ -295,8 +295,8 @@ uint8_t usb_ccid_recieve_callback(USBD_HandleTypeDef *pdev, uint8_t epnum) /* Get the received data length */ hcdc->RxLength = USBD_LL_GetRxDataSize (pdev, epnum); - printf1(TAG_GREEN, "ccid>> "); - dump_hex1(TAG_GREEN, ccidmsg_buf, hcdc->RxLength); + printf1(TAG_CCID, ">> "); + dump_hex1(TAG_CCID, ccidmsg_buf, hcdc->RxLength); handle_ccid(ccidmsg_buf, hcdc->RxLength); From 3b4c154fd17607fc14ddb5ce7eb29773055a0379 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 24 Aug 2019 15:49:02 +0800 Subject: [PATCH 35/42] add enable macro for CCID interface --- targets/stm32l432/lib/usbd/usbd_composite.c | 60 ++++++++++++++------- targets/stm32l432/src/app.h | 4 ++ targets/stm32l432/src/init.c | 10 +--- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/targets/stm32l432/lib/usbd/usbd_composite.c b/targets/stm32l432/lib/usbd/usbd_composite.c index 7d5fdb4..b1de7d2 100644 --- a/targets/stm32l432/lib/usbd/usbd_composite.c +++ b/targets/stm32l432/lib/usbd/usbd_composite.c @@ -4,6 +4,7 @@ #include "usbd_cdc.h" #include "usbd_ccid.h" #include "usbd_ctlreq.h" +#include "app.h" static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); @@ -27,20 +28,29 @@ static uint8_t *USBD_Composite_GetOtherSpeedCfgDesc (uint16_t *length); static uint8_t *USBD_Composite_GetDeviceQualifierDescriptor (uint16_t *length); +#ifdef ENABLE_CCID +#define CCID_SIZE 84 +#define CCID_NUM_INTERFACE 1 +#else +#define CCID_NUM_INTERFACE 0 +#define CCID_SIZE 0 +#endif + +#if DEBUG_LEVEL > 0 +#define CDC_SIZE (49 + 8 + 9 + 4) +#define CDC_NUM_INTERFACE 2 +#else +#define CDC_SIZE 0 +#define CDC_NUM_INTERFACE 0 +#endif + +#define HID_SIZE 41 + +#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (HID_SIZE + CDC_SIZE + CCID_SIZE) +#define NUM_INTERFACES (1 + CDC_NUM_INTERFACE + CCID_NUM_INTERFACE) #define NUM_CLASSES 3 -#if NUM_CLASSES>2 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4 + 84) -#define NUM_INTERFACES 4 -#elif NUM_CLASSES>1 -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (90 + 8+9 + 4) -#define NUM_INTERFACES 3 -#else -#define COMPOSITE_CDC_HID_DESCRIPTOR_SIZE (41) -#define NUM_INTERFACES 1 -#endif - #define HID_INTF_NUM 0 #define CDC_MASTER_INTF_NUM 1 #define CDC_SLAVE_INTF_NUM 2 @@ -101,7 +111,7 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x00, HID_BINTERVAL, /*bInterval: Polling Interval */ -#if NUM_INTERFACES > 2 +#if DEBUG_LEVEL > 0 /* */ /* CDC */ @@ -199,7 +209,7 @@ __ALIGN_BEGIN uint8_t COMPOSITE_CDC_HID_DESCRIPTOR[COMPOSITE_CDC_HID_DESCRIPTOR_ 0x04, #endif -#if NUM_INTERFACES>3 +#ifdef ENABLE_CCID /* CCID Interface Descriptor */ 9, /* bLength: Interface Descriptor size */ @@ -295,16 +305,21 @@ USBD_ClassTypeDef USBD_Composite = USBD_Composite_GetDeviceQualifierDescriptor, }; -static USBD_ClassTypeDef *USBD_Classes[MAX_CLASSES]; +static USBD_ClassTypeDef * USBD_Classes[MAX_CLASSES]; int in_endpoint_to_class[MAX_ENDPOINTS]; int out_endpoint_to_class[MAX_ENDPOINTS]; void USBD_Composite_Set_Classes(USBD_ClassTypeDef *hid_class, USBD_ClassTypeDef *ccid_class, USBD_ClassTypeDef *cdc_class) { + memset(USBD_Classes, 0 , sizeof(USBD_Classes)); USBD_Classes[0] = hid_class; +#ifdef ENABLE_CCID USBD_Classes[1] = ccid_class; +#endif +#if DEBUG_LEVEL > 0 USBD_Classes[2] = cdc_class; +#endif } static USBD_ClassTypeDef * getClass(uint8_t index) @@ -313,12 +328,15 @@ static USBD_ClassTypeDef * getClass(uint8_t index) { case HID_INTF_NUM: return USBD_Classes[0]; +#ifdef ENABLE_CCID case CCID_INTF_NUM: return USBD_Classes[1]; +#endif +#if DEBUG_LEVEL > 0 case CDC_MASTER_INTF_NUM: case CDC_SLAVE_INTF_NUM: return USBD_Classes[2]; - +#endif } return NULL; } @@ -326,7 +344,7 @@ static USBD_ClassTypeDef * getClass(uint8_t index) static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->Init(pdev, cfgidx) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->Init(pdev, cfgidx) != USBD_OK) { return USBD_FAIL; } } @@ -337,7 +355,7 @@ static uint8_t USBD_Composite_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { static uint8_t USBD_Composite_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->DeInit(pdev, cfgidx) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->DeInit(pdev, cfgidx) != USBD_OK) { return USBD_FAIL; } } @@ -363,7 +381,7 @@ static uint8_t USBD_Composite_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqType case USB_REQ_GET_DESCRIPTOR : for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->Setup(pdev, req) != USBD_OK) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->Setup(pdev, req) != USBD_OK) { return USBD_FAIL; } } @@ -386,6 +404,8 @@ static uint8_t USBD_Composite_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) { i = in_endpoint_to_class[epnum]; + if (USBD_Classes[i] == NULL) return USBD_FAIL; + return USBD_Classes[i]->DataIn(pdev, epnum); } @@ -394,6 +414,8 @@ static uint8_t USBD_Composite_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) i = out_endpoint_to_class[epnum]; + if (USBD_Classes[i] == NULL) return USBD_FAIL; + return USBD_Classes[i]->DataOut(pdev, epnum); } @@ -401,7 +423,7 @@ static uint8_t USBD_Composite_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) static uint8_t USBD_Composite_EP0_RxReady (USBD_HandleTypeDef *pdev) { int i; for(i = 0; i < NUM_CLASSES; i++) { - if (USBD_Classes[i]->EP0_RxReady != NULL) { + if (USBD_Classes[i] != NULL && USBD_Classes[i]->EP0_RxReady != NULL) { if (USBD_Classes[i]->EP0_RxReady(pdev) != USBD_OK) { return USBD_FAIL; } diff --git a/targets/stm32l432/src/app.h b/targets/stm32l432/src/app.h index 308e5f6..fcd5629 100644 --- a/targets/stm32l432/src/app.h +++ b/targets/stm32l432/src/app.h @@ -12,9 +12,13 @@ #define DEBUG_UART USART1 #ifndef DEBUG_LEVEL +// Enable the CDC ACM USB interface & debug logs (DEBUG_LEVEL > 0) #define DEBUG_LEVEL 0 #endif +// Enable the CCID USB interface +// #define ENABLE_CCID + #define NON_BLOCK_PRINTING 0 diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index c57392c..a51e8b8 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -707,7 +707,6 @@ void init_usb() // Enable USB Clock SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); -#if DEBUG_LEVEL > 0 USBD_Composite_Set_Classes(&USBD_HID, &USBD_CCID, &USBD_CDC); in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; @@ -720,17 +719,10 @@ void init_usb() USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); - // - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_CDC); +#if DEBUG_LEVEL > 0 USBD_CDC_RegisterInterface(&Solo_USBD_Device, &USBD_Interface_fops_FS); -#else - USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); - USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); #endif - //Y USBD_Start(&Solo_USBD_Device); - //Y } void init_pwm(void) From 3ba9b671fc1eec23547d5a4314513c7dc6886cd9 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 24 Aug 2019 16:01:44 +0800 Subject: [PATCH 36/42] dont use composit for bootloader --- targets/stm32l432/src/init.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index a51e8b8..bdf42fa 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -706,7 +706,7 @@ void init_usb() // Enable USB Clock SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); - +#ifndef IS_BOOTLOADER USBD_Composite_Set_Classes(&USBD_HID, &USBD_CCID, &USBD_CDC); in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; @@ -721,6 +721,10 @@ void init_usb() USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); #if DEBUG_LEVEL > 0 USBD_CDC_RegisterInterface(&Solo_USBD_Device, &USBD_Interface_fops_FS); +#endif +#else + USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); + USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); #endif USBD_Start(&Solo_USBD_Device); } From 8bf1921263fedb173e333b8d41fee5fc484cf7bc Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Sat, 24 Aug 2019 16:20:52 +0800 Subject: [PATCH 37/42] dont reference not-enabled ccid --- targets/stm32l432/lib/usbd/usbd_conf.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/targets/stm32l432/lib/usbd/usbd_conf.c b/targets/stm32l432/lib/usbd/usbd_conf.c index ad5b211..bd3c442 100644 --- a/targets/stm32l432/lib/usbd/usbd_conf.c +++ b/targets/stm32l432/lib/usbd/usbd_conf.c @@ -123,9 +123,11 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) case HID_EPOUT_ADDR: usb_hid_recieve_callback(epnum); break; +#ifdef ENABLE_CCID case CCID_OUT_EP: usb_ccid_recieve_callback((USBD_HandleTypeDef*)hpcd->pData, epnum); break; +#endif } } From 83dd92d9ba46502e429c3ac031f829fcd329d5ab Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Thu, 29 Aug 2019 21:45:21 +0800 Subject: [PATCH 38/42] Update STABLE_VERSION --- STABLE_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STABLE_VERSION b/STABLE_VERSION index 35cee72..73462a5 100644 --- a/STABLE_VERSION +++ b/STABLE_VERSION @@ -1 +1 @@ -2.4.3 +2.5.1 From 8b9e44c3ed791adea443d548ea915183e6c3f72a Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Thu, 29 Aug 2019 20:44:39 +0000 Subject: [PATCH 39/42] docs: update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a86fb7b..a15a24c 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d f.m3hm00d
f.m3hm00d

📖 Richard Hughes
Richard Hughes

🤔 💻 🚇 🔧 Kim Schulz
Kim Schulz

💼 🤔 + Jakub
Jakub

🐛 @@ -168,7 +169,7 @@ You can buy Solo, Solo Tap, and Solo for Hackers at [solokeys.com](https://solok
[![License](https://img.shields.io/github/license/solokeys/solo.svg)](https://github.com/solokeys/solo/blob/master/LICENSE) -[![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors) [![Build Status](https://travis-ci.com/solokeys/solo.svg?branch=master)](https://travis-ci.com/solokeys/solo) [![Discourse Users](https://img.shields.io/discourse/https/discourse.solokeys.com/users.svg)](https://discourse.solokeys.com) [![Keybase Chat](https://img.shields.io/badge/chat-on%20keybase-brightgreen.svg)](https://keybase.io/team/solokeys.public) From 689d4716888c4c9f65e562e2b7f7dc0edc986463 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Thu, 29 Aug 2019 20:44:40 +0000 Subject: [PATCH 40/42] docs: update .all-contributorsrc --- .all-contributorsrc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index 253fb36..16a5a37 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -178,6 +178,15 @@ "business", "ideas" ] + }, + { + "login": "oplik0", + "name": "Jakub", + "avatar_url": "https://avatars2.githubusercontent.com/u/25460763?v=4", + "profile": "https://github.com/oplik0", + "contributions": [ + "bug" + ] } ], "contributorsPerLine": 7, From 9041e5903c3b7a1c998b78dfa58ea9d09d137761 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 30 Aug 2019 14:34:57 +0800 Subject: [PATCH 41/42] return SW_WRONG_LENGTH for incorrect lc --- fido2/apdu.c | 18 ++++++++++++++++-- fido2/apdu.h | 2 +- targets/stm32l432/src/nfc.c | 5 +++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/fido2/apdu.c b/fido2/apdu.c index 3d5bf49..ef24a25 100644 --- a/fido2/apdu.c +++ b/fido2/apdu.c @@ -9,7 +9,7 @@ #include "apdu.h" -int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) +uint16_t apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) { EXT_APDU_HEADER *hapdu = (EXT_APDU_HEADER *)data; @@ -62,6 +62,11 @@ int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) if (len >= 7 && b0 == 0) { uint16_t extlen = (hapdu->lc[1] << 8) + hapdu->lc[2]; + + if (len - 7 < extlen) + { + return SW_WRONG_LENGTH; + } // case 2E (Le) - extended if (len == 7) @@ -103,9 +108,18 @@ int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu) apdu->le = 0x10000; } } + else + { + if ((len > 5) && (len - 5 < hapdu->lc[0])) + { + return SW_WRONG_LENGTH; + } + } if (!apdu->case_type) - return 1; + { + return SW_COND_USE_NOT_SATISFIED; + } if (apdu->lc) { diff --git a/fido2/apdu.h b/fido2/apdu.h index 420fea6..d361c94 100644 --- a/fido2/apdu.h +++ b/fido2/apdu.h @@ -36,7 +36,7 @@ typedef struct uint8_t case_type; } __attribute__((packed)) APDU_STRUCT; -extern int apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu); +extern uint16_t apdu_decode(uint8_t *data, size_t len, APDU_STRUCT *apdu); #define APDU_FIDO_U2F_REGISTER 0x01 #define APDU_FIDO_U2F_AUTHENTICATE 0x02 diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 6a3cab3..3a513e9 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -784,9 +784,10 @@ void nfc_process_iblock(uint8_t * buf, int len) } APDU_STRUCT apdu; - if (apdu_decode(buf + block_offset, len - block_offset, &apdu)) { + uint16_t ret = apdu_decode(buf + block_offset, len - block_offset, &apdu); + if (ret != 0) { printf1(TAG_NFC,"apdu decode error\r\n"); - nfc_write_response(buf[0], SW_COND_USE_NOT_SATISFIED); + nfc_write_response(buf[0], ret); return; } printf1(TAG_NFC,"apdu ok. %scase=%02x cla=%02x ins=%02x p1=%02x p2=%02x lc=%d le=%d\r\n", From 1ce191343fe271180a4d3ea68ed5d4613eb147a8 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Fri, 30 Aug 2019 16:47:20 +0300 Subject: [PATCH 42/42] add checking some rare case in iso14443-4 chaining. add NAK checking and aborting the data sending. --- targets/stm32l432/src/nfc.c | 18 +++++++++++++++++- targets/stm32l432/src/nfc.h | 6 +++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/targets/stm32l432/src/nfc.c b/targets/stm32l432/src/nfc.c index 3a513e9..2250cff 100644 --- a/targets/stm32l432/src/nfc.c +++ b/targets/stm32l432/src/nfc.c @@ -228,6 +228,8 @@ void nfc_write_response_chaining_plain(uint8_t req0, uint8_t * data, int len) { uint8_t res[32] = {0}; res[0] = iBlock; + res[1] = 0; + res[2] = 0; if (len && data) memcpy(&res[block_offset], data, len); nfc_write_frame(res, len + block_offset); @@ -268,6 +270,19 @@ void nfc_write_response_chaining_plain(uint8_t req0, uint8_t * data, int len) printf1(TAG_NFC, "R block RX timeout %d/%d.\r\n",sendlen,len); break; } + + if (!IS_RBLOCK(recbuf[0])) + { + printf1(TAG_NFC, "R block RX error. Not a R block(0x%02x) %d/%d.\r\n", recbuf[0], sendlen, len); + break; + } + + // NAK check + if (recbuf[0] & NFC_CMD_RBLOCK_ACK) + { + printf1(TAG_NFC, "R block RX error. NAK received. %d/%d.\r\n", recbuf[0], sendlen, len); + break; + } uint8_t rblock_offset = p14443_block_offset(recbuf[0]); if (reclen != rblock_offset) @@ -466,7 +481,8 @@ void rblock_acknowledge(uint8_t req0, bool ack) NFC_STATE.block_num = !NFC_STATE.block_num; buf[0] = NFC_CMD_RBLOCK | (req0 & 0x0f); - if (ack) + // iso14443-4:2001 page 16. ACK, if bit is set to 0, NAK, if bit is set to 1 + if (!ack) buf[0] |= NFC_CMD_RBLOCK_ACK; nfc_write_frame(buf, block_offset); diff --git a/targets/stm32l432/src/nfc.h b/targets/stm32l432/src/nfc.h index f284a8e..96f7cab 100644 --- a/targets/stm32l432/src/nfc.h +++ b/targets/stm32l432/src/nfc.h @@ -34,9 +34,9 @@ typedef struct #define IS_PPSS_CMD(x) (((x) & 0xf0) == NFC_CMD_PPSS) #define NFC_CMD_IBLOCK 0x00 #define IS_IBLOCK(x) ( (((x) & 0xc0) == NFC_CMD_IBLOCK) && (((x) & 0x02) == 0x02) ) -#define NFC_CMD_RBLOCK 0x80 -#define NFC_CMD_RBLOCK_ACK 0x20 -#define IS_RBLOCK(x) ( (((x) & 0xc0) == NFC_CMD_RBLOCK) && (((x) & 0x02) == 0x02) ) +#define NFC_CMD_RBLOCK 0xa0 +#define NFC_CMD_RBLOCK_ACK 0x10 +#define IS_RBLOCK(x) ( (((x) & 0xe0) == NFC_CMD_RBLOCK) && (((x) & 0x02) == 0x02) ) #define NFC_CMD_SBLOCK 0xc0 #define IS_SBLOCK(x) ( (((x) & 0xc0) == NFC_CMD_SBLOCK) && (((x) & 0x02) == 0x02) )