| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 18:44:12 +00:00
										 |  |  | #include "device.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | #include "ctaphid.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | #include "ctap.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  | #include "u2f.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | #include "time.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | #include "util.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  | #include "log.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef enum | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     IDLE = 0, | 
					
						
							|  |  |  |     HANDLING_REQUEST, | 
					
						
							|  |  |  | } CTAP_STATE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | typedef enum | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     EMPTY = 0, | 
					
						
							|  |  |  |     BUFFERING, | 
					
						
							|  |  |  |     BUFFERED, | 
					
						
							|  |  |  | } CTAP_BUFFER_STATE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint8_t cmd; | 
					
						
							|  |  |  |     uint32_t cid; | 
					
						
							|  |  |  |     uint16_t bcnt; | 
					
						
							|  |  |  |     int offset; | 
					
						
							|  |  |  |     int bytes_written; | 
					
						
							|  |  |  |     uint8_t seq; | 
					
						
							|  |  |  |     uint8_t buf[HID_MESSAGE_SIZE]; | 
					
						
							|  |  |  | } CTAPHID_WRITE_BUFFER; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | struct CID | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t cid; | 
					
						
							|  |  |  |     uint64_t last_used; | 
					
						
							|  |  |  |     uint8_t busy; | 
					
						
							|  |  |  |     uint8_t last_cmd; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | #define SUCESS          0
 | 
					
						
							|  |  |  | #define SEQUENCE_ERROR  1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | static int state; | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static struct CID CIDS[10]; | 
					
						
							|  |  |  | #define CID_MAX (sizeof(CIDS)/sizeof(struct CID))
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | static uint64_t active_cid_timestamp; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | static uint8_t ctap_buffer[CTAPHID_BUFFER_SIZE]; | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  | static uint32_t ctap_buffer_cid; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int ctap_buffer_cmd; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | static uint16_t ctap_buffer_bcnt; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int ctap_buffer_offset; | 
					
						
							|  |  |  | static int ctap_packet_seq; | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  | static void buffer_reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  | #define CTAPHID_WRITE_INIT      0x01
 | 
					
						
							|  |  |  | #define CTAPHID_WRITE_FLUSH     0x02
 | 
					
						
							|  |  |  | #define CTAPHID_WRITE_RESET     0x04
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define     ctaphid_write_buffer_init(x)    memset(x,0,sizeof(CTAPHID_WRITE_BUFFER))
 | 
					
						
							|  |  |  | static void ctaphid_write(CTAPHID_WRITE_BUFFER * wb, void * _data, int len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | void ctaphid_init() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     state = IDLE; | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  |     buffer_reset(); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     ctap_reset_state(); | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static uint32_t get_new_cid() | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     static uint32_t cid = 1; | 
					
						
							|  |  |  |     do | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         cid++; | 
					
						
							|  |  |  |     }while(cid == 0 || cid == 0xffffffff); | 
					
						
							|  |  |  |     return cid; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static int8_t add_cid(uint32_t cid) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     int i; | 
					
						
							|  |  |  |     for(i = 0; i < CID_MAX-1; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!CIDS[i].busy) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             CIDS[i].cid = cid; | 
					
						
							|  |  |  |             CIDS[i].busy = 1; | 
					
						
							|  |  |  |             CIDS[i].last_used = millis(); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static int8_t cid_exists(uint32_t cid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for(i = 0; i < CID_MAX-1; i++) | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |         if (CIDS[i].cid == cid) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-05-01 03:35:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static int8_t cid_refresh(uint32_t cid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for(i = 0; i < CID_MAX-1; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (CIDS[i].cid == cid) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             CIDS[i].last_used = millis(); | 
					
						
							| 
									
										
										
										
											2018-05-29 04:16:26 +00:00
										 |  |  |             CIDS[i].busy = 1; | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 20:04:06 +00:00
										 |  |  | static int8_t cid_del(uint32_t cid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for(i = 0; i < CID_MAX-1; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (CIDS[i].cid == cid) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             CIDS[i].busy = 0; | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | static int is_broadcast(CTAPHID_PACKET * pkt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (pkt->cid == CTAPHID_BROADCAST_CID); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int is_init_pkt(CTAPHID_PACKET * pkt) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-26 21:41:13 +00:00
										 |  |  |     return (pkt->pkt.init.cmd == CTAPHID_INIT); | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int is_cont_pkt(CTAPHID_PACKET * pkt) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-13 03:22:23 +00:00
										 |  |  |     return !(pkt->pkt.init.cmd & TYPE_INIT); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int buffer_packet(CTAPHID_PACKET * pkt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (pkt->pkt.init.cmd & TYPE_INIT) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ctap_buffer_bcnt = ctaphid_packet_len(pkt); | 
					
						
							|  |  |  |         int pkt_len = (ctap_buffer_bcnt < CTAPHID_INIT_PAYLOAD_SIZE) ? ctap_buffer_bcnt : CTAPHID_INIT_PAYLOAD_SIZE; | 
					
						
							|  |  |  |         ctap_buffer_cmd = pkt->pkt.init.cmd; | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |         ctap_buffer_cid = pkt->cid; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |         ctap_buffer_offset = pkt_len; | 
					
						
							|  |  |  |         ctap_packet_seq = -1; | 
					
						
							|  |  |  |         memmove(ctap_buffer, pkt->pkt.init.payload, pkt_len); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int leftover = ctap_buffer_bcnt - ctap_buffer_offset; | 
					
						
							|  |  |  |         int diff = leftover - CTAPHID_CONT_PAYLOAD_SIZE; | 
					
						
							|  |  |  |         ctap_packet_seq++; | 
					
						
							|  |  |  |         if (ctap_packet_seq != pkt->pkt.cont.seq) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return SEQUENCE_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (diff <= 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // only move the leftover amount
 | 
					
						
							|  |  |  |             memmove(ctap_buffer + ctap_buffer_offset, pkt->pkt.cont.payload, leftover); | 
					
						
							|  |  |  |             ctap_buffer_offset += leftover; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             memmove(ctap_buffer + ctap_buffer_offset, pkt->pkt.cont.payload, CTAPHID_CONT_PAYLOAD_SIZE); | 
					
						
							|  |  |  |             ctap_buffer_offset += CTAPHID_CONT_PAYLOAD_SIZE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return SUCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  | static void buffer_reset() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-28 20:04:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  |     ctap_buffer_bcnt = 0; | 
					
						
							|  |  |  |     ctap_buffer_offset = 0; | 
					
						
							|  |  |  |     ctap_packet_seq = 0; | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |     ctap_buffer_cid = 0; | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int buffer_status() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (ctap_buffer_bcnt == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return EMPTY; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (ctap_buffer_offset == ctap_buffer_bcnt) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return BUFFERED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return BUFFERING; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | static int buffer_cmd() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ctap_buffer_cmd; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  | static uint32_t buffer_cid() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ctap_buffer_cid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | static int buffer_len() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ctap_buffer_bcnt; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  | // Buffer data and send in HID_MESSAGE_SIZE chunks
 | 
					
						
							|  |  |  | // if len == 0, FLUSH
 | 
					
						
							|  |  |  | static void ctaphid_write(CTAPHID_WRITE_BUFFER * wb, void * _data, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint8_t * data = (uint8_t *)_data; | 
					
						
							| 
									
										
										
										
											2018-05-12 16:23:14 +00:00
										 |  |  |     if (_data == NULL) | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (wb->offset == 0 && wb->bytes_written == 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             memmove(wb->buf, &wb->cid, 4); | 
					
						
							|  |  |  |             wb->offset += 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             wb->buf[4] = wb->cmd; | 
					
						
							|  |  |  |             wb->buf[5] = (wb->bcnt & 0xff00) >> 8; | 
					
						
							|  |  |  |             wb->buf[6] = (wb->bcnt & 0xff) >> 0; | 
					
						
							|  |  |  |             wb->offset += 3; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (wb->offset > 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             memset(wb->buf + wb->offset, 0, HID_MESSAGE_SIZE - wb->offset); | 
					
						
							|  |  |  |             ctaphid_write_block(wb->buf); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for (i = 0; i < len; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (wb->offset == 0 ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             memmove(wb->buf, &wb->cid, 4); | 
					
						
							|  |  |  |             wb->offset += 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (wb->bytes_written == 0) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 wb->buf[4] = wb->cmd; | 
					
						
							|  |  |  |                 wb->buf[5] = (wb->bcnt & 0xff00) >> 8; | 
					
						
							|  |  |  |                 wb->buf[6] = (wb->bcnt & 0xff) >> 0; | 
					
						
							|  |  |  |                 wb->offset += 3; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 wb->buf[4] = wb->seq++; | 
					
						
							|  |  |  |                 wb->offset += 1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         wb->buf[wb->offset++] = data[i]; | 
					
						
							|  |  |  |         wb->bytes_written += 1; | 
					
						
							|  |  |  |         if (wb->offset == HID_MESSAGE_SIZE) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ctaphid_write_block(wb->buf); | 
					
						
							|  |  |  |             wb->offset = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void ctaphid_send_error(uint32_t cid, uint8_t error) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |     CTAPHID_WRITE_BUFFER wb; | 
					
						
							|  |  |  |     ctaphid_write_buffer_init(&wb); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     wb.cid = cid; | 
					
						
							|  |  |  |     wb.cmd = CTAPHID_ERROR; | 
					
						
							|  |  |  |     wb.bcnt = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctaphid_write(&wb, &error, 1); | 
					
						
							|  |  |  |     ctaphid_write(&wb, NULL, 0); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | static void send_init_response(uint32_t oldcid, uint32_t newcid, uint8_t * nonce) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CTAPHID_INIT_RESPONSE init_resp; | 
					
						
							|  |  |  |     CTAPHID_WRITE_BUFFER wb; | 
					
						
							|  |  |  |     ctaphid_write_buffer_init(&wb); | 
					
						
							|  |  |  |     wb.cid = oldcid; | 
					
						
							|  |  |  |     wb.cmd = CTAPHID_INIT; | 
					
						
							|  |  |  |     wb.bcnt = 17; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memmove(init_resp.nonce, nonce, 8); | 
					
						
							|  |  |  |     init_resp.cid = newcid; | 
					
						
							|  |  |  |     init_resp.protocol_version = 0;//?
 | 
					
						
							|  |  |  |     init_resp.version_major = 0;//?
 | 
					
						
							|  |  |  |     init_resp.version_minor = 0;//?
 | 
					
						
							|  |  |  |     init_resp.build_version = 0;//?
 | 
					
						
							|  |  |  |     init_resp.capabilities = CTAP_CAPABILITIES; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctaphid_write(&wb,&init_resp,sizeof(CTAPHID_INIT_RESPONSE)); | 
					
						
							|  |  |  |     ctaphid_write(&wb,NULL,0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 18:44:12 +00:00
										 |  |  | void ctaphid_check_timeouts() | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     uint8_t i; | 
					
						
							|  |  |  |     for(i = 0; i < CID_MAX; i++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (CIDS[i].busy && ((millis() - CIDS[i].last_used) >= 750)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf1(TAG_HID, "TIMEOUT CID: %08x\n", CIDS[i].cid); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             ctaphid_send_error(CIDS[i].cid, CTAP1_ERR_TIMEOUT); | 
					
						
							|  |  |  |             memset(CIDS + i, 0, sizeof(struct CID)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | void ctaphid_handle_packet(uint8_t * pkt_raw) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     CTAPHID_PACKET * pkt = (CTAPHID_PACKET *)(pkt_raw); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |     printf1(TAG_HID, "Recv packet\n"); | 
					
						
							|  |  |  |     printf1(TAG_HID, "  CID: %08x \n", pkt->cid); | 
					
						
							|  |  |  |     printf1(TAG_HID, "  cmd: %02x\n", pkt->pkt.init.cmd); | 
					
						
							|  |  |  |     if (!is_cont_pkt(pkt)) printf1(TAG_HID, "  length: %d\n", ctaphid_packet_len(pkt)); | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  |     uint8_t status; | 
					
						
							|  |  |  |     uint32_t oldcid; | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     uint32_t newcid; | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |     static CTAPHID_WRITE_BUFFER wb; | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     uint32_t active_cid; | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     CTAP_RESPONSE ctap_resp; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |     if (is_init_pkt(pkt)) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |         if (ctaphid_packet_len(pkt) != 8) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR, "Error,invalid length field for init packet\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_LENGTH); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (pkt->cid == 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR,"Error, invalid cid 0\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |             ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_CHANNEL); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ctaphid_init(); | 
					
						
							|  |  |  |         if (is_broadcast(pkt)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             // Check if any existing cids are busy first ?
 | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf1(TAG_HID,"adding a new cid\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             oldcid = CTAPHID_BROADCAST_CID; | 
					
						
							|  |  |  |             newcid = get_new_cid(); | 
					
						
							|  |  |  |             ret = add_cid(newcid); | 
					
						
							|  |  |  |             // handle init here
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf1(TAG_HID, "synchronizing to cid\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             oldcid = pkt->cid; | 
					
						
							|  |  |  |             newcid = pkt->cid; | 
					
						
							|  |  |  |             if (cid_exists(newcid)) | 
					
						
							|  |  |  |                 ret = cid_refresh(newcid); | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                 ret = add_cid(newcid); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (ret == -1) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR, "Error, not enough memory for new CID.  return BUSY.\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             ctaphid_send_error(pkt->cid, CTAP1_ERR_CHANNEL_BUSY); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         send_init_response(oldcid, newcid, pkt->pkt.init.payload); | 
					
						
							| 
									
										
										
										
											2018-06-02 18:44:12 +00:00
										 |  |  |         cid_del(newcid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Check if matches existing CID
 | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |         if (pkt->cid == CTAPHID_BROADCAST_CID) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_CHANNEL); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |         if (cid_exists(pkt->cid)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |             if (buffer_status() == BUFFERING) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (pkt->cid == buffer_cid() && ! is_cont_pkt(pkt)) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf2(TAG_ERR,"INVALID_SEQ\n"); | 
					
						
							|  |  |  |                     printf2(TAG_ERR,"Have %d/%d bytes\n", ctap_buffer_offset, ctap_buffer_bcnt); | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |                     ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_SEQ); | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else if (pkt->cid != buffer_cid()) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf2(TAG_ERR,"BUSY with %08x\n", buffer_cid()); | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |                     ctaphid_send_error(pkt->cid, CTAP1_ERR_CHANNEL_BUSY); | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             if (! is_cont_pkt(pkt)) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                 if (ctaphid_packet_len(pkt) > CTAPHID_BUFFER_SIZE) | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                     ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_LENGTH); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  |                     return; | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 if (buffer_status() == EMPTY || pkt->cid != buffer_cid()) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf2(TAG_ERR,"ignoring random cont packet\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             if (buffer_packet(pkt) == SEQUENCE_ERROR) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                 printf2(TAG_ERR,"Buffering sequence error\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                 ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_SEQ); | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             ret = cid_refresh(pkt->cid); | 
					
						
							|  |  |  |             if (ret != 0) | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                 printf2(TAG_ERR,"Error, refresh cid failed\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                 exit(1); | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             active_cid = pkt->cid; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (is_cont_pkt(pkt)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR,"ignoring unwarranted cont packet\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             // Ignore
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR,"BUSY\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 19:38:30 +00:00
										 |  |  |             ctaphid_send_error(pkt->cid, CTAP1_ERR_CHANNEL_BUSY); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |     switch(buffer_status()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         case BUFFERING: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf1(TAG_HID,"BUFFERING\n"); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |             active_cid_timestamp = millis(); | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         case EMPTY: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf1(TAG_HID,"empty buffer!\n"); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |         case BUFFERED: | 
					
						
							|  |  |  |             switch(buffer_cmd()) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 case CTAPHID_INIT: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf2(TAG_ERR,"CTAPHID_INIT, error this should already be handled\n"); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                     exit(1); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                     break; | 
					
						
							|  |  |  |                 case CTAPHID_PING: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf1(TAG_HID,"CTAPHID_PING\n"); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write_buffer_init(&wb); | 
					
						
							|  |  |  |                     wb.cid = active_cid; | 
					
						
							|  |  |  |                     wb.cmd = CTAPHID_PING; | 
					
						
							|  |  |  |                     wb.bcnt = buffer_len(); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write(&wb, ctap_buffer, buffer_len()); | 
					
						
							|  |  |  |                     ctaphid_write(&wb, NULL,0); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                 case CTAPHID_WINK: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf1(TAG_HID,"CTAPHID_WINK\n"); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write_buffer_init(&wb); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     wb.cid = active_cid; | 
					
						
							|  |  |  |                     wb.cmd = CTAPHID_WINK; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write(&wb,NULL,0); | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  |                 case CTAPHID_CBOR: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf1(TAG_HID,"CTAPHID_CBOR\n"); | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  |                     if (buffer_len() == 0) | 
					
						
							|  |  |  |                     { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                         printf2(TAG_ERR,"Error,invalid 0 length field for cbor packet\n"); | 
					
						
							| 
									
										
										
										
											2018-05-26 21:27:10 +00:00
										 |  |  |                         ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_LENGTH); | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 19:29:37 +00:00
										 |  |  |                     ctap_response_init(&ctap_resp); | 
					
						
							| 
									
										
										
										
											2018-06-02 18:44:12 +00:00
										 |  |  |                     status = ctap_request(ctap_buffer, buffer_len(), &ctap_resp); | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write_buffer_init(&wb); | 
					
						
							|  |  |  |                     wb.cid = active_cid; | 
					
						
							|  |  |  |                     wb.cmd = CTAPHID_CBOR; | 
					
						
							|  |  |  |                     wb.bcnt = (ctap_resp.length+1); | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 04:00:06 +00:00
										 |  |  |                     ctaphid_write(&wb, &status, 1); | 
					
						
							|  |  |  |                     ctaphid_write(&wb, ctap_resp.data, ctap_resp.length); | 
					
						
							|  |  |  |                     ctaphid_write(&wb, NULL, 0); | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  |                 case CTAPHID_MSG: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf1(TAG_HID,"CTAPHID_MSG\n"); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  |                     if (buffer_len() == 0) | 
					
						
							|  |  |  |                     { | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                         printf2(TAG_ERR,"Error,invalid 0 length field for MSG/U2F packet\n"); | 
					
						
							| 
									
										
										
										
											2018-05-26 21:27:10 +00:00
										 |  |  |                         ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_LENGTH); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 19:29:37 +00:00
										 |  |  |                     ctap_response_init(&ctap_resp); | 
					
						
							|  |  |  |                     u2f_request((struct u2f_request_apdu*)ctap_buffer, &ctap_resp); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 19:29:37 +00:00
										 |  |  |                     ctaphid_write_buffer_init(&wb); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  |                     wb.cid = active_cid; | 
					
						
							| 
									
										
										
										
											2018-05-26 19:29:37 +00:00
										 |  |  |                     wb.cmd = CTAPHID_MSG; | 
					
						
							|  |  |  |                     wb.bcnt = (ctap_resp.length); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:51:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     ctaphid_write(&wb, ctap_resp.data, ctap_resp.length); | 
					
						
							|  |  |  |                     ctaphid_write(&wb, NULL, 0); | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                 default: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |                     printf2(TAG_ERR,"error, unimplemented HID cmd: %02x\r\n", buffer_cmd()); | 
					
						
							| 
									
										
										
										
											2018-05-28 18:14:59 +00:00
										 |  |  |                     ctaphid_send_error(pkt->cid, CTAP1_ERR_INVALID_COMMAND); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |                     break; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-05-28 20:04:06 +00:00
										 |  |  |             cid_del(buffer_cid()); | 
					
						
							| 
									
										
										
										
											2018-05-04 01:27:29 +00:00
										 |  |  |             buffer_reset(); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-05-02 02:22:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |             printf2(TAG_ERR,"invalid buffer state; abort\n"); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  |             exit(1); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2018-06-03 04:50:46 +00:00
										 |  |  |     printf1(TAG_HID,"\n"); | 
					
						
							| 
									
										
										
										
											2018-05-01 03:27:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 02:52:41 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-29 01:40:13 +00:00
										 |  |  | 
 |