CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ctap2.h
Go to the documentation of this file.
1// CTAP2 Protocol Implementation (FIDO2)
2// Handles CBOR-encoded CTAP2 commands
3
4#pragma once
5#include <stdint.h>
6#include <stdbool.h>
7#include "fido2.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13// ============================================================================
14// CTAP2 Commands
15// ============================================================================
16
17#define CTAP2_CMD_MAKE_CREDENTIAL 0x01
18#define CTAP2_CMD_GET_ASSERTION 0x02
19#define CTAP2_CMD_GET_INFO 0x04
20#define CTAP2_CMD_CLIENT_PIN 0x06
21#define CTAP2_CMD_RESET 0x07
22#define CTAP2_CMD_GET_NEXT_ASSERTION 0x08
23#define CTAP2_CMD_CRED_MANAGEMENT 0x0A
24#define CTAP2_CMD_SELECTION 0x0B
25#define CTAP2_CMD_LARGE_BLOBS 0x0C
26#define CTAP2_CMD_CONFIG 0x0D
27
28// Vendor commands
29#define CTAP2_CMD_VENDOR_FIRST 0x40
30#define CTAP2_CMD_VENDOR_LAST 0xBF
31
32// ============================================================================
33// CTAP2 Status Codes
34// ============================================================================
35
36#define CTAP2_OK 0x00
37#define CTAP1_ERR_INVALID_COMMAND 0x01
38#define CTAP1_ERR_INVALID_PARAMETER 0x02
39#define CTAP1_ERR_INVALID_LENGTH 0x03
40#define CTAP1_ERR_INVALID_SEQ 0x04
41#define CTAP1_ERR_TIMEOUT 0x05
42#define CTAP1_ERR_CHANNEL_BUSY 0x06
43#define CTAP1_ERR_LOCK_REQUIRED 0x0A
44#define CTAP1_ERR_INVALID_CHANNEL 0x0B
45#define CTAP2_ERR_CBOR_UNEXPECTED_TYPE 0x11
46#define CTAP2_ERR_INVALID_CBOR 0x12
47#define CTAP2_ERR_MISSING_PARAMETER 0x14
48#define CTAP2_ERR_LIMIT_EXCEEDED 0x15
49#define CTAP2_ERR_UNSUPPORTED_EXT 0x16
50#define CTAP2_ERR_LARGE_BLOB_STORAGE_FULL 0x18
51#define CTAP2_ERR_CREDENTIAL_EXCLUDED 0x19
52#define CTAP2_ERR_PROCESSING 0x21
53#define CTAP2_ERR_INVALID_CREDENTIAL 0x22
54#define CTAP2_ERR_USER_ACTION_PENDING 0x23
55#define CTAP2_ERR_OPERATION_PENDING 0x24
56#define CTAP2_ERR_NO_OPERATIONS 0x25
57#define CTAP2_ERR_UNSUPPORTED_ALGORITHM 0x26
58#define CTAP2_ERR_OPERATION_DENIED 0x27
59#define CTAP2_ERR_KEY_STORE_FULL 0x28
60#define CTAP2_ERR_NO_OPERATION_PENDING 0x2A
61#define CTAP2_ERR_UNSUPPORTED_OPTION 0x2B
62#define CTAP2_ERR_INVALID_OPTION 0x2C
63#define CTAP2_ERR_KEEPALIVE_CANCEL 0x2D
64#define CTAP2_ERR_NO_CREDENTIALS 0x2E
65#define CTAP2_ERR_USER_ACTION_TIMEOUT 0x2F
66#define CTAP2_ERR_NOT_ALLOWED 0x30
67#define CTAP2_ERR_PIN_INVALID 0x31
68#define CTAP2_ERR_PIN_BLOCKED 0x32
69#define CTAP2_ERR_PIN_AUTH_INVALID 0x33
70#define CTAP2_ERR_PIN_AUTH_BLOCKED 0x34
71#define CTAP2_ERR_PIN_NOT_SET 0x35
72#define CTAP2_ERR_PIN_REQUIRED 0x36
73#define CTAP2_ERR_PIN_POLICY_VIOLATION 0x37
74#define CTAP2_ERR_PIN_TOKEN_EXPIRED 0x38
75#define CTAP2_ERR_REQUEST_TOO_LARGE 0x39
76#define CTAP2_ERR_ACTION_TIMEOUT 0x3A
77#define CTAP2_ERR_UP_REQUIRED 0x3B
78#define CTAP2_ERR_UV_BLOCKED 0x3C
79#define CTAP2_ERR_INTEGRITY_FAILURE 0x3D
80#define CTAP2_ERR_OTHER 0x7F
81
82// ============================================================================
83// CTAP2 Algorithms
84// ============================================================================
85
86#define COSE_ALG_ES256 -7 // ECDSA with SHA-256 (P-256)
87#define COSE_ALG_EDDSA -8 // EdDSA (Ed25519)
88#define COSE_ALG_RS256 -257 // RSASSA-PKCS1-v1_5 with SHA-256
89#define COSE_ALG_ECDH_ES_HKDF_256 -25 // ECDH-ES + HKDF-256 (RFC 8152)
90
91// ============================================================================
92// COSE Key Constants (RFC 8152 / RFC 8037)
93// See: https://datatracker.ietf.org/doc/html/rfc8152
94// https://www.iana.org/assignments/cose/cose.xhtml
95// ============================================================================
96
97// COSE Key Common Parameter labels (negative values are curve-specific labels)
98#define COSE_KEY_LABEL_KTY 1 // Key type
99#define COSE_KEY_LABEL_KID 2 // Key identifier
100#define COSE_KEY_LABEL_ALG 3 // Algorithm
101#define COSE_KEY_LABEL_OPS 4 // Key operations
102#define COSE_KEY_LABEL_BASE_IV 5 // Base IV
103#define COSE_KEY_LABEL_CRV -1 // Curve (EC2/OKP)
104#define COSE_KEY_LABEL_X -2 // X coordinate (EC2) / public key (OKP)
105#define COSE_KEY_LABEL_Y -3 // Y coordinate (EC2)
106#define COSE_KEY_LABEL_D -4 // Private key
107
108// COSE Key Type (kty) values
109#define COSE_KEY_TYPE_OKP 1 // Octet Key Pair (Ed25519, X25519)
110#define COSE_KEY_TYPE_EC2 2 // Elliptic Curve with x/y coordinates
111#define COSE_KEY_TYPE_SYMMETRIC 4 // Symmetric key
112
113// COSE Elliptic Curves (crv) values
114#define COSE_CRV_P256 1 // NIST P-256 (secp256r1)
115#define COSE_CRV_P384 2 // NIST P-384
116#define COSE_CRV_P521 3 // NIST P-521
117#define COSE_CRV_X25519 4 // X25519 ECDH
118#define COSE_CRV_X448 5 // X448 ECDH
119#define COSE_CRV_ED25519 6 // Ed25519 EdDSA
120#define COSE_CRV_ED448 7 // Ed448 EdDSA
121
122// ============================================================================
123// CTAP2 CBOR Map Keys (CTAP 2.1 Specification)
124// See: https://fidoalliance.org/specs/fido-v2.1-rd-20191217/fido-client-to-authenticator-protocol-v2.1-rd-20191217.html
125// ============================================================================
126
127// authenticatorGetInfo response keys (Section 6.4)
128#define CTAP2_INFO_VERSIONS 0x01
129#define CTAP2_INFO_EXTENSIONS 0x02
130#define CTAP2_INFO_AAGUID 0x03
131#define CTAP2_INFO_OPTIONS 0x04
132#define CTAP2_INFO_MAX_MSG_SIZE 0x05
133#define CTAP2_INFO_PIN_UV_AUTH_PROTOCOLS 0x06
134#define CTAP2_INFO_MAX_CRED_COUNT_IN_LIST 0x07
135#define CTAP2_INFO_MAX_CRED_ID_LENGTH 0x08
136#define CTAP2_INFO_TRANSPORTS 0x09
137#define CTAP2_INFO_ALGORITHMS 0x0A
138#define CTAP2_INFO_MAX_SERIALIZED_LARGE_BLOB_ARRAY 0x0B
139#define CTAP2_INFO_MIN_PIN_LENGTH 0x0D
140
141// authenticatorMakeCredential parameter keys (Section 6.1)
142#define CTAP2_MC_CLIENT_DATA_HASH 0x01
143#define CTAP2_MC_RP 0x02
144#define CTAP2_MC_USER 0x03
145#define CTAP2_MC_PUB_KEY_CRED_PARAMS 0x04
146#define CTAP2_MC_EXCLUDE_LIST 0x05
147#define CTAP2_MC_EXTENSIONS 0x06
148#define CTAP2_MC_OPTIONS 0x07
149#define CTAP2_MC_PIN_UV_AUTH_PARAM 0x08
150#define CTAP2_MC_PIN_UV_AUTH_PROTOCOL 0x09
151
152// authenticatorMakeCredential response keys (Section 6.1)
153#define CTAP2_MC_RESP_FMT 0x01
154#define CTAP2_MC_RESP_AUTH_DATA 0x02
155#define CTAP2_MC_RESP_ATT_STMT 0x03
156
157// authenticatorGetAssertion parameter keys (Section 6.2)
158#define CTAP2_GA_RP_ID 0x01
159#define CTAP2_GA_CLIENT_DATA_HASH 0x02
160#define CTAP2_GA_ALLOW_LIST 0x03
161#define CTAP2_GA_EXTENSIONS 0x04
162#define CTAP2_GA_OPTIONS 0x05
163#define CTAP2_GA_PIN_UV_AUTH_PARAM 0x06
164#define CTAP2_GA_PIN_UV_AUTH_PROTOCOL 0x07
165
166// authenticatorGetAssertion response keys (Section 6.2)
167#define CTAP2_GA_RESP_CREDENTIAL 0x01
168#define CTAP2_GA_RESP_AUTH_DATA 0x02
169#define CTAP2_GA_RESP_SIGNATURE 0x03
170#define CTAP2_GA_RESP_USER 0x04
171#define CTAP2_GA_RESP_NUMBER_OF_CREDS 0x05
172
173// authenticatorClientPIN parameter keys (Section 6.5)
174#define CTAP2_PIN_PROTOCOL 0x01
175#define CTAP2_PIN_SUBCOMMAND 0x02
176#define CTAP2_PIN_KEY_AGREEMENT 0x03
177#define CTAP2_PIN_AUTH 0x04
178#define CTAP2_PIN_NEW_PIN_ENC 0x05
179#define CTAP2_PIN_HASH_ENC 0x06
180#define CTAP2_PIN_PERMISSIONS 0x09
181#define CTAP2_PIN_PERMISSIONS_RPID 0x0A
182
183// authenticatorClientPIN response keys (Section 6.5)
184#define CTAP2_PIN_RESP_KEY_AGREEMENT 0x01
185#define CTAP2_PIN_RESP_PIN_TOKEN 0x02
186#define CTAP2_PIN_RESP_PIN_RETRIES 0x03
187#define CTAP2_PIN_RESP_POWER_CYCLE_STATE 0x04
188#define CTAP2_PIN_RESP_UV_RETRIES 0x05
189
190// authenticatorCredentialManagement parameter keys (Section 6.8)
191#define CTAP2_CM_SUBCOMMAND 0x01
192#define CTAP2_CM_SUBCOMMAND_PARAMS 0x02
193#define CTAP2_CM_PIN_UV_AUTH_PROTOCOL 0x03
194#define CTAP2_CM_PIN_UV_AUTH_PARAM 0x04
195
196// authenticatorCredentialManagement subcommand parameter keys
197#define CTAP2_CM_SUB_RP_ID_HASH 0x01
198#define CTAP2_CM_SUB_CREDENTIAL_ID 0x02
199
200// authenticatorCredentialManagement response keys (Section 6.8)
201#define CTAP2_CM_RESP_EXISTING_CRED_COUNT 0x01
202#define CTAP2_CM_RESP_REMAINING_CRED_COUNT 0x02
203#define CTAP2_CM_RESP_RP 0x03
204#define CTAP2_CM_RESP_RP_ID_HASH 0x04
205#define CTAP2_CM_RESP_TOTAL_RPS 0x05
206#define CTAP2_CM_RESP_USER 0x06
207#define CTAP2_CM_RESP_CREDENTIAL_ID 0x07
208#define CTAP2_CM_RESP_PUBLIC_KEY 0x08
209#define CTAP2_CM_RESP_TOTAL_CREDENTIALS 0x09
210#define CTAP2_CM_RESP_CRED_PROTECT 0x0A
211
212// authenticatorLargeBlobs parameter keys (Section 6.10)
213#define CTAP2_LB_GET 0x01
214#define CTAP2_LB_SET 0x02
215#define CTAP2_LB_OFFSET 0x03
216#define CTAP2_LB_LENGTH 0x04
217#define CTAP2_LB_PIN_UV_AUTH_PARAM 0x05
218#define CTAP2_LB_PIN_UV_AUTH_PROTOCOL 0x06
219#define CTAP2_LB_RESP_CONFIG 0x01
220
221// authenticatorConfig parameter keys (Section 6.11)
222#define CTAP2_CONFIG_SUBCOMMAND 0x01
223#define CTAP2_CONFIG_SUBCOMMAND_PARAMS 0x02
224#define CTAP2_CONFIG_PIN_UV_AUTH_PROTOCOL 0x03
225#define CTAP2_CONFIG_PIN_UV_AUTH_PARAM 0x04
226
227// authenticatorConfig subcommands (Section 6.11)
228#define CTAP2_CONFIG_SUB_ENABLE_EP 0x01
229#define CTAP2_CONFIG_SUB_TOGGLE_ALWAYS_UV 0x02
230#define CTAP2_CONFIG_SUB_SET_MIN_PIN_LENGTH 0x03
231#define CTAP2_CONFIG_SUB_VENDOR_PROTOTYPE 0xFF
232
233// setMinPINLength subCommandParams keys (Section 6.11)
234#define CTAP2_CONFIG_PARAM_NEW_MIN_PIN_LEN 0x01
235#define CTAP2_CONFIG_PARAM_MIN_PIN_RPIDS 0x02
236#define CTAP2_CONFIG_PARAM_FORCE_CHANGE_PIN 0x03
237
238// ============================================================================
239// Processing Functions
240// ============================================================================
241
247bool ctap2_init(void);
248
258uint8_t ctap2_process_command(const uint8_t *cmd, uint16_t cmd_len,
259 uint8_t *response, uint16_t *response_len);
260
265void ctap2_send_keepalive(uint8_t status);
266
270void ctap2_cancel(void);
271
277void ctap2_clear_cancel(void);
278
282bool ctap2_is_cancelled(void);
283
284// ============================================================================
285// Individual Command Handlers
286// ============================================================================
287
288uint8_t ctap2_make_credential(const uint8_t *params, uint16_t params_len,
289 uint8_t *response, uint16_t *response_len);
290
291uint8_t ctap2_get_assertion(const uint8_t *params, uint16_t params_len,
292 uint8_t *response, uint16_t *response_len);
293
294uint8_t ctap2_get_info(uint8_t *response, uint16_t *response_len);
295
296uint8_t ctap2_client_pin(const uint8_t *params, uint16_t params_len,
297 uint8_t *response, uint16_t *response_len);
298
299uint8_t ctap2_reset(uint8_t *response, uint16_t *response_len);
300
301uint8_t ctap2_get_next_assertion(uint8_t *response, uint16_t *response_len);
302
303uint8_t ctap2_cred_management(const uint8_t *params, uint16_t params_len,
304 uint8_t *response, uint16_t *response_len);
305
306uint8_t ctap2_selection(uint8_t *response, uint16_t *response_len);
307
308uint8_t ctap2_large_blobs(const uint8_t *params, uint16_t params_len,
309 uint8_t *response, uint16_t *response_len);
310
311uint8_t ctap2_config(const uint8_t *params, uint16_t params_len,
312 uint8_t *response, uint16_t *response_len);
313
314#ifdef __cplusplus
315}
316#endif
317
uint8_t ctap2_reset(uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorReset (0x07).
Definition ctap2.cpp:3072
uint8_t ctap2_client_pin(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorClientPIN (0x06).
Definition ctap2.cpp:2993
void ctap2_cancel(void)
Marks current CTAP2 operation as cancelled.
Definition ctap2.cpp:3979
uint8_t ctap2_process_command(const uint8_t *cmd, uint16_t cmd_len, uint8_t *response, uint16_t *response_len)
Dispatches one CTAP2 command and writes response payload.
Definition ctap2.cpp:3881
void ctap2_clear_cancel(void)
Clears the cancel flag. Called when a new CTAPHID channel is opened so a cancel from a previous chann...
Definition ctap2.cpp:3989
bool ctap2_init(void)
Initializes CTAP2 runtime state.
Definition ctap2.cpp:3865
uint8_t ctap2_selection(uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorSelection (0x0B).
Definition ctap2.cpp:3541
bool ctap2_is_cancelled(void)
Returns true if the current CTAP2 operation has been cancelled.
Definition ctap2.cpp:3996
uint8_t ctap2_large_blobs(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorLargeBlobs (0x0C).
Definition ctap2.cpp:3606
uint8_t ctap2_config(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorConfig (0x0D).
Definition ctap2.cpp:3746
void ctap2_send_keepalive(uint8_t status)
Sends CTAPHID keepalive for currently active channel.
Definition ctap2.cpp:3969
uint8_t ctap2_get_assertion(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorGetAssertion (0x02).
Definition ctap2.cpp:1869
uint8_t ctap2_make_credential(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
uint8_t ctap2_cred_management(const uint8_t *params, uint16_t params_len, uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorCredentialManagement (0x0A).
Definition ctap2.cpp:3265
uint8_t ctap2_get_next_assertion(uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorGetNextAssertion (0x08).
Definition ctap2.cpp:2018
uint8_t ctap2_get_info(uint8_t *response, uint16_t *response_len)
Handles CTAP2 authenticatorGetInfo (0x04).
Definition ctap2.cpp:694