ctap.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
24 #include <stdint.h>
25 
26 #include "mutex.h"
27 #include "cbor.h"
28 #include "assert.h"
29 #include "crypto/modes/ccm.h"
30 #include "timex.h"
31 #include "board.h"
32 
33 #include "fido2/ctap.h"
34 #include "fido2/ctap/ctap_crypto.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
47 #define CTAP_PIN_AUTH_SZ 16
48 
54 #define CTAP_AUTH_DATA_FLAG_UP (1 << 0)
55 #define CTAP_AUTH_DATA_FLAG_UV (1 << 2)
56 #define CTAP_AUTH_DATA_FLAG_AT (1 << 6)
57 #define CTAP_AUTH_DATA_FLAG_ED (1 << 7)
65 #define CTAP_VERSION_FLAG_FIDO_PRE 0x01
66 #define CTAP_VERSION_FLAG_FIDO 0x02
67 #define CTAP_VERSION_FLAG_U2F_V2 0x04
77 #define CTAP_GET_INFO_RESP_OPTIONS_ID_PLAT "plat"
78 #define CTAP_GET_INFO_RESP_OPTIONS_ID_RK "rk"
79 #define CTAP_GET_INFO_RESP_OPTIONS_ID_CLIENT_PIN "clientPin"
80 #define CTAP_GET_INFO_RESP_OPTIONS_ID_UP "up"
81 #define CTAP_GET_INFO_RESP_OPTIONS_ID_UV "uv"
89 #define CTAP_INFO_OPTIONS_FLAG_PLAT (1 << 0)
90 #define CTAP_INFO_OPTIONS_FLAG_RK (1 << 1)
91 #define CTAP_INFO_OPTIONS_FLAG_CLIENT_PIN (1 << 2)
92 #define CTAP_INFO_OPTIONS_FLAG_UP (1 << 3)
93 #define CTAP_INFO_OPTIONS_FLAG_UV (1 << 4)
101 typedef enum {
106  CTAP_PIN_GET_PIN_TOKEN = 0x05
113 #ifdef CONFIG_FIDO2_CTAP_STACK_SIZE
114 #define CTAP_STACKSIZE CONFIG_FIDO2_CTAP_STACK_SIZE
115 #else
116 #define CTAP_STACKSIZE 15000
117 #endif
118 
122 #if defined(CONFIG_FIDO2_CTAP_UP_BUTTON_PORT) && defined(CONFIG_FIDO2_CTAP_UP_BUTTON_PIN) && \
123  (CONFIG_FIDO2_CTAP_UP_BUTTON_PORT >= 0) && (CONFIG_FIDO2_CTAP_UP_BUTTON_PIN >= 0)
124 #define CTAP_UP_BUTTON GPIO_PIN(CONFIG_FIDO2_CTAP_UP_BUTTON_PORT, CONFIG_FIDO2_CTAP_UP_BUTTON_PIN)
125 #else
126 /* set default button if no button is configured */
127 #ifdef BTN0_PIN
128 #define CTAP_UP_BUTTON BTN0_PIN
129 #else
130 #define CTAP_UP_BUTTON 0
134 #define CONFIG_FIDO2_CTAP_DISABLE_UP 1
135 #endif /* BTN0_PIN */
136 #endif
137 
141 #if IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN_PU)
142 #define CTAP_UP_BUTTON_MODE GPIO_IN_PU
143 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN_PD)
144 #define CTAP_UP_BUTTON_MODE GPIO_IN_PD
145 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN)
146 #define CTAP_UP_BUTTON_MODE GPIO_IN
147 #else
148 #define CTAP_UP_BUTTON_MODE GPIO_IN_PU
149 #endif
150 
154 #if IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_FALLING)
155 #define CTAP_UP_BUTTON_FLANK GPIO_FALLING
156 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_RISING)
157 #define CTAP_UP_BUTTON_FLANK GPIO_RISING
158 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_BOTH)
159 #define CTAP_UP_BUTTON_FLANK GPIO_BOTH
160 #else
161 #define CTAP_UP_BUTTON_FLANK GPIO_FALLING
162 #endif
163 
167 #ifndef CONFIG_FIDO2_CTAP_DISABLE_UP
168 #define CONFIG_FIDO2_CTAP_DISABLE_UP 0
169 #endif
170 
174 #ifndef CONFIG_FIDO2_CTAP_DISABLE_LED
175 #define CONFIG_FIDO2_CTAP_DISABLE_LED 0
176 #endif
177 
181 #define CTAP_RP_MAX_NAME_SIZE 32
182 
186 #define CTAP_USER_MAX_NAME_SIZE 64 + 1
187 
191 #define CTAP_USER_ID_MAX_SIZE 64
192 
196 #define CTAP_DOMAIN_NAME_MAX_SIZE 253 + 1
197 
201 #define CTAP_ICON_MAX_SIZE 128 + 1
202 
206 #define CTAP_PIN_MIN_SIZE 4
207 
214 #define CTAP_PIN_ENC_MIN_SIZE 64
215 
220 #define CTAP_PIN_ENC_MAX_SIZE 256
221 
225 #define CTAP_PIN_MAX_SIZE 64
226 
230 #define CTAP_PIN_MAX_ATTS 8
231 
235 #define CTAP_PIN_MAX_ATTS_BOOT 3
236 
240 #define CTAP_PIN_PROT_VER 1
241 
245 #define CTAP_AMT_SUP_PIN_VER 1
246 
252 #define CTAP_PIN_TOKEN_SZ 16
253 
261 #define CTAP_CRED_KEY_LEN 16
262 
270 #define CTAP_AES_CCM_L 2
271 
275 #define CTAP_AES_CCM_NONCE_SIZE (15 - CTAP_AES_CCM_L)
276 
282 #define CTAP_CREDENTIAL_ID_ENC_SIZE (sizeof(struct ctap_resident_key) - \
283  sizeof(((struct ctap_resident_key *)0)-> \
284  cred_desc.cred_id) - \
285  sizeof(((struct ctap_resident_key *)0)-> \
286  cred_desc.has_nonce))
287 
291 #ifdef CONFIG_FIDO2_CTAP_UP_TIMEOUT
292 #define CTAP_UP_TIMEOUT (CONFIG_FIDO2_CTAP_UP_TIMEOUT * MS_PER_SEC)
293 #else
294 #define CTAP_UP_TIMEOUT (15 * MS_PER_SEC)
295 #endif
296 
301 #define CTAP_GET_NEXT_ASSERTION_TIMEOUT (30 * MS_PER_SEC)
302 
306 #ifdef CONFIG_FIDO2_CTAP_DEVICE_AAGUID
307 #define CTAP_AAGUID CONFIG_FIDO2_CTAP_DEVICE_AAGUID
308 #else
309 /* randomly generated fallback value */
310 #define CTAP_AAGUID "9c295865fa2c36b705a42320af9c8f16"
311 #endif
312 
318 #define CTAP_PUB_KEY_CRED_PUB_KEY 0x01
319 #define CTAP_PUB_KEY_CRED_UNKNOWN 0x02
327 #define CTAP_COSE_KEY_LABEL_KTY 1
328 #define CTAP_COSE_KEY_LABEL_ALG 3
329 #define CTAP_COSE_KEY_LABEL_CRV -1
330 #define CTAP_COSE_KEY_LABEL_X -2
331 #define CTAP_COSE_KEY_LABEL_Y -3
332 #define CTAP_COSE_KEY_KTY_EC2 2
333 #define CTAP_COSE_KEY_CRV_P256 1
339 #define CTAP_AAGUID_SIZE 16
340 
344 #define CTAP_COSE_ALG_ES256 -7
345 
349 #define CTAP_COSE_ALG_ECDH_ES_HKDF_256 -25
350 
355 #define CTAP_CREDENTIAL_ID_SIZE 16U
356 
363 #define CTAP_INITIALIZED_MARKER 0x4e
364 
368 #define CTAP_MAX_EXCLUDE_LIST_SIZE 0x14
369 
373 typedef struct ctap_cred_desc ctap_cred_desc_t;
374 
379 
384 
388 typedef struct {
389  uint8_t aaguid[CTAP_AAGUID_SIZE];
390  uint8_t options;
391 } ctap_config_t;
392 
398 typedef struct {
402  uint16_t rk_amount_stored;
404  uint8_t pin_hash[SHA256_DIGEST_LENGTH / 2];
405  uint8_t cred_key[CTAP_CRED_KEY_LEN];
407  bool pin_is_set;
408  uint32_t id_cnt;
409 } ctap_state_t;
410 
414 typedef struct {
415  int rk;
416  int uv;
417  int up;
419 
423 typedef struct {
424  uint8_t id[CTAP_USER_ID_MAX_SIZE];
425  uint8_t id_len;
426  uint8_t name[CTAP_USER_MAX_NAME_SIZE];
427  uint8_t display_name[CTAP_USER_MAX_NAME_SIZE];
430 
435 typedef struct {
436  uint8_t id[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
437  uint8_t id_len;
439  uint8_t name[CTAP_RP_MAX_NAME_SIZE + 1];
441  uint8_t icon[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
443 } ctap_rp_ent_t;
444 
450 typedef struct {
452  int kty;
453  int crv;
454  int32_t alg_type;
455  uint8_t cred_type;
457 
467  uint8_t cred_type;
468  union {
471  };
472  bool has_nonce;
474 };
475 
482 struct __attribute__((packed)) ctap_resident_key {
483  uint8_t rp_id_hash[SHA256_DIGEST_LENGTH];
484  uint8_t user_id[CTAP_USER_ID_MAX_SIZE];
485  uint8_t user_id_len;
486  uint8_t priv_key[CTAP_CRYPTO_KEY_SIZE];
487  uint16_t id;
488  uint32_t sign_count;
493 };
494 
501 typedef struct __attribute__((packed)) {
503  uint8_t mac[CCM_MAC_MAX_LEN];
504  uint8_t nonce[CTAP_AES_CCM_NONCE_SIZE];
506 
513  uint8_t cred_type;
515 };
516 
520 typedef struct {
526  uint8_t client_data_hash[SHA256_DIGEST_LENGTH];
527  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
528  size_t pin_auth_len;
529  int32_t alg_type;
531  uint8_t pin_protocol;
532  uint8_t cred_type;
534 
538 typedef struct {
541  uint8_t client_data_hash[SHA256_DIGEST_LENGTH];
542  uint8_t rp_id[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
543  uint8_t rp_id_len;
544  uint8_t allow_list_len;
545  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
546  size_t pin_auth_len;
547  uint8_t pin_protocol;
550 
554 typedef struct {
556  uint16_t new_pin_enc_size;
557  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
558  uint8_t new_pin_enc[CTAP_PIN_ENC_MAX_SIZE];
559  uint8_t pin_hash_enc[SHA256_DIGEST_LENGTH / 2];
561  uint8_t pin_protocol;
566 
572 typedef struct __attribute__((packed)){
573  uint8_t aaguid[CTAP_AAGUID_SIZE];
574  uint8_t cred_len_h;
575  uint8_t cred_len_l;
578 
582 typedef struct {
586 
592 typedef struct __attribute__((packed)){
593  uint8_t rp_id_hash[SHA256_DIGEST_LENGTH];
594  uint8_t flags;
595  uint32_t sign_count;
597 
601 typedef struct {
605 
609 typedef struct {
610  uint16_t max_msg_size;
611  uint8_t aaguid[CTAP_AAGUID_SIZE];
612  uint8_t versions;
613  uint8_t options;
614  uint8_t pin_protocol;
615  bool pin_is_set;
616 } ctap_info_t;
617 
632 int fido2_ctap_get_sig(const uint8_t *auth_data, size_t auth_data_len,
633  const uint8_t *client_data_hash,
634  const ctap_resident_key_t *rk,
635  uint8_t *sig, size_t *sig_len);
636 
646 bool fido2_ctap_cred_params_supported(uint8_t cred_type, int32_t alg_type);
647 
659  size_t nonce_len, ctap_cred_id_t *id);
660 
668 
675 
676 #ifdef __cplusplus
677 }
678 #endif
POSIX.1-2008 compliant version of the assert macro.
Functionality for encoding SenML values as CBOR.
Counter with CBC-MAC mode of operation for block ciphers.
#define CCM_MAC_MAX_LEN
Maximum length for the appended MAC.
Definition: ccm.h:44
Public FIDO2 CTAP defines, structures and function declarations.
FIDO2 CTAP crypto helper defines, structures and function declarations.
#define CTAP_CRYPTO_KEY_SIZE
Size in bytes of cryptographic keys used.
Definition: ctap_crypto.h:34
bool fido2_ctap_cred_params_supported(uint8_t cred_type, int32_t alg_type)
Check if requested algorithm is supported.
#define CTAP_CREDENTIAL_ID_SIZE
CTAP size of credential id.
Definition: ctap.h:355
int fido2_ctap_encrypt_rk(ctap_resident_key_t *rk, uint8_t *nonce, size_t nonce_len, ctap_cred_id_t *id)
Encrypt resident key with AES CCM.
int fido2_ctap_get_sig(const uint8_t *auth_data, size_t auth_data_len, const uint8_t *client_data_hash, const ctap_resident_key_t *rk, uint8_t *sig, size_t *sig_len)
Create signature from authenticator data.
#define CTAP_CREDENTIAL_ID_ENC_SIZE
Total size of AES CCM credential id.
Definition: ctap.h:282
#define CTAP_PIN_AUTH_SZ
Size of pin auth.
Definition: ctap.h:47
ctap_state_t * fido2_ctap_get_state(void)
Get a pointer to the authenticator state.
#define CTAP_RP_MAX_NAME_SIZE
Max size of relying party name.
Definition: ctap.h:181
#define CTAP_PIN_ENC_MAX_SIZE
Encrypted newPin max size.
Definition: ctap.h:220
ctap_pin_subcommand_t
CTAP Client PIN request subCommand CBOR key values.
Definition: ctap.h:101
#define CTAP_AAGUID_SIZE
CTAP size of authenticator AAGUID in bytes.
Definition: ctap.h:339
#define CTAP_DOMAIN_NAME_MAX_SIZE
Max size of a domain name including null character.
Definition: ctap.h:196
#define CTAP_MAX_EXCLUDE_LIST_SIZE
Max size of allow list.
Definition: ctap.h:368
#define CTAP_USER_ID_MAX_SIZE
Max size of user id.
Definition: ctap.h:191
bool fido2_ctap_pin_is_set(void)
Check if PIN has been set on authenticator.
#define CTAP_AES_CCM_NONCE_SIZE
AES CCM nonce size.
Definition: ctap.h:275
#define CTAP_USER_MAX_NAME_SIZE
Max size of username including null character.
Definition: ctap.h:186
#define CTAP_CRED_KEY_LEN
Size of key used to encrypt credential.
Definition: ctap.h:261
@ CTAP_PIN_SET_PIN
setPIN subCommand
Definition: ctap.h:104
@ CTAP_PIN_GET_PIN_TOKEN
getPinToken subCommand
Definition: ctap.h:106
@ CTAP_PIN_CHANGE_PIN
changePIN subCommand
Definition: ctap.h:105
@ CTAP_PIN_GET_KEY_AGREEMENT
getKeyAgreement subCommand
Definition: ctap.h:103
@ CTAP_PIN_GET_RETRIES
getRetries subCommand
Definition: ctap.h:102
#define SHA256_DIGEST_LENGTH
Length of SHA256 digests in bytes.
Definition: sha256.h:38
Mutex for thread synchronization.
CTAP attested credential data header struct.
Definition: ctap.h:572
ctap_cred_id_t cred_id
credential id
Definition: ctap.h:576
uint8_t cred_len_l
lower byte of credential length
Definition: ctap.h:575
uint8_t cred_len_h
higher byte of credential length
Definition: ctap.h:574
CTAP attested credential data struct.
Definition: ctap.h:582
ctap_public_key_cose_t key
cose key
Definition: ctap.h:584
ctap_attested_cred_data_header_t header
attested credential data header
Definition: ctap.h:583
CTAP authenticator data header struct.
Definition: ctap.h:592
uint8_t flags
flags indicating result of user verification
Definition: ctap.h:594
uint32_t sign_count
sign count of credential
Definition: ctap.h:595
CTAP authenticator data struct.
Definition: ctap.h:601
ctap_attested_cred_data_t attested_cred_data
attested credential data
Definition: ctap.h:603
ctap_auth_data_header_t header
auth data header
Definition: ctap.h:602
CTAP client pin request struct.
Definition: ctap.h:554
bool key_agreement_present
indicate if key_agreement present
Definition: ctap.h:564
uint16_t new_pin_enc_size
size of encrypted new pin
Definition: ctap.h:556
ctap_pin_subcommand_t sub_command
ClientPIN sub command.
Definition: ctap.h:560
ctap_public_key_cose_t key_agreement
public key of platform_key_agreement_key
Definition: ctap.h:555
bool pin_auth_present
indicate if pin_auth present
Definition: ctap.h:563
uint8_t pin_protocol
PIN protocol version chosen by the client.
Definition: ctap.h:561
bool pin_hash_enc_present
indicate pin_hash_enc is present
Definition: ctap.h:562
CTAP authenticator config struct.
Definition: ctap.h:388
uint8_t options
options
Definition: ctap.h:390
CTAP credential description alternative struct.
Definition: ctap.h:512
uint8_t cred_type
type of credential
Definition: ctap.h:513
ctap_cred_id_t cred_id
credential id
Definition: ctap.h:514
CTAP credential description struct.
Definition: ctap.h:466
uint8_t cred_id[CTAP_CREDENTIAL_ID_SIZE]
credential identifier
Definition: ctap.h:469
uint8_t nonce[CTAP_AES_CCM_NONCE_SIZE]
CTAP AES CCM nonce.
Definition: ctap.h:470
uint8_t cred_type
type of credential
Definition: ctap.h:467
bool has_nonce
Indicate if nonce or cred_id
Definition: ctap.h:472
CTAP credential ID.
Definition: ctap.h:501
Elliptic curve public key.
Definition: ctap_crypto.h:44
CTAP get assertion request struct.
Definition: ctap.h:538
uint8_t pin_protocol
PIN protocol version.
Definition: ctap.h:547
bool pin_auth_present
indicate if pin_auth present
Definition: ctap.h:548
size_t pin_auth_len
pin_auth length
Definition: ctap.h:546
uint8_t rp_id_len
Actual Length of Relying Party Identifier.
Definition: ctap.h:543
uint8_t allow_list_len
length of CBOR allow list array
Definition: ctap.h:544
ctap_options_t options
parameters to influence authenticator operation
Definition: ctap.h:539
CTAP info struct.
Definition: ctap.h:609
bool pin_is_set
PIN is set or not.
Definition: ctap.h:615
uint8_t options
supported options
Definition: ctap.h:613
uint8_t pin_protocol
supported PIN protocol versions
Definition: ctap.h:614
uint8_t versions
supported versions of FIDO
Definition: ctap.h:612
uint16_t max_msg_size
max message size
Definition: ctap.h:610
CTAP make credential request struct.
Definition: ctap.h:520
ctap_user_ent_t user
user
Definition: ctap.h:524
bool pin_auth_present
pin_auth present
Definition: ctap.h:530
int32_t alg_type
cryptographic algorithm identifier
Definition: ctap.h:529
size_t exclude_list_len
length of CBOR exclude list array
Definition: ctap.h:522
ctap_rp_ent_t rp
relying party
Definition: ctap.h:523
size_t pin_auth_len
pin_auth len
Definition: ctap.h:528
uint8_t cred_type
type of credential
Definition: ctap.h:532
ctap_options_t options
parameters to influence authenticator operation
Definition: ctap.h:525
uint8_t pin_protocol
PIN protocol version.
Definition: ctap.h:531
CTAP options struct.
Definition: ctap.h:414
int up
user presence
Definition: ctap.h:417
int uv
user verification
Definition: ctap.h:416
int rk
resident key
Definition: ctap.h:415
CTAP cose key struct.
Definition: ctap.h:450
int crv
EC identifier.
Definition: ctap.h:453
int32_t alg_type
COSEAlgorithmIdentifier.
Definition: ctap.h:454
int kty
identification of key type
Definition: ctap.h:452
ctap_crypto_pub_key_t pubkey
public key
Definition: ctap.h:451
uint8_t cred_type
type of credential
Definition: ctap.h:455
CTAP resident key struct.
Definition: ctap.h:482
ctap_cred_desc_t cred_desc
credential descriptor
Definition: ctap.h:492
uint32_t sign_count
signature counter.
Definition: ctap.h:488
uint8_t user_id_len
length of the user id
Definition: ctap.h:485
uint16_t id
internal id of key
Definition: ctap.h:487
CTAP relying party entity struct.
Definition: ctap.h:435
uint8_t id_len
actual length of relying party identifier
Definition: ctap.h:437
CTAP state struct.
Definition: ctap.h:398
uint16_t rk_amount_stored
total number of resident keys stored on device
Definition: ctap.h:402
int rem_pin_att
remaining PIN tries
Definition: ctap.h:401
bool pin_is_set
PIN is set or not.
Definition: ctap.h:407
ctap_config_t config
configuration of authenticator
Definition: ctap.h:399
bool cred_key_is_initialized
AES CCM key initialized flag.
Definition: ctap.h:406
uint8_t initialized_marker
CTAP initialized marker.
Definition: ctap.h:403
ctap_crypto_key_agreement_key_t ag_key
Platform key agreement key.
Definition: ctap.h:400
uint32_t id_cnt
id counter for credential id
Definition: ctap.h:408
CTAP user entity struct.
Definition: ctap.h:423
uint8_t id_len
actual length of user id
Definition: ctap.h:425
Utility library for comparing and computing timestamps.