ctap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
27 #include <stdint.h>
28 
29 #include "mutex.h"
30 #include "cbor.h"
31 #include "assert.h"
32 #include "crypto/modes/ccm.h"
33 #include "timex.h"
34 #include "board.h"
35 
36 #include "fido2/ctap.h"
37 #include "fido2/ctap/ctap_crypto.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
50 #define CTAP_PIN_AUTH_SZ 16
51 
57 #define CTAP_AUTH_DATA_FLAG_UP (1 << 0)
58 #define CTAP_AUTH_DATA_FLAG_UV (1 << 2)
59 #define CTAP_AUTH_DATA_FLAG_AT (1 << 6)
60 #define CTAP_AUTH_DATA_FLAG_ED (1 << 7)
68 #define CTAP_VERSION_FLAG_FIDO_PRE 0x01
69 #define CTAP_VERSION_FLAG_FIDO 0x02
70 #define CTAP_VERSION_FLAG_U2F_V2 0x04
80 #define CTAP_GET_INFO_RESP_OPTIONS_ID_PLAT "plat"
81 #define CTAP_GET_INFO_RESP_OPTIONS_ID_RK "rk"
82 #define CTAP_GET_INFO_RESP_OPTIONS_ID_CLIENT_PIN "clientPin"
83 #define CTAP_GET_INFO_RESP_OPTIONS_ID_UP "up"
84 #define CTAP_GET_INFO_RESP_OPTIONS_ID_UV "uv"
92 #define CTAP_INFO_OPTIONS_FLAG_PLAT (1 << 0)
93 #define CTAP_INFO_OPTIONS_FLAG_RK (1 << 1)
94 #define CTAP_INFO_OPTIONS_FLAG_CLIENT_PIN (1 << 2)
95 #define CTAP_INFO_OPTIONS_FLAG_UP (1 << 3)
96 #define CTAP_INFO_OPTIONS_FLAG_UV (1 << 4)
104 typedef enum {
109  CTAP_PIN_GET_PIN_TOKEN = 0x05
116 #ifdef CONFIG_FIDO2_CTAP_STACK_SIZE
117 #define CTAP_STACKSIZE CONFIG_FIDO2_CTAP_STACK_SIZE
118 #else
119 #define CTAP_STACKSIZE 15000
120 #endif
121 
125 #if defined(CONFIG_FIDO2_CTAP_UP_BUTTON_PORT) && defined(CONFIG_FIDO2_CTAP_UP_BUTTON_PIN) && \
126  (CONFIG_FIDO2_CTAP_UP_BUTTON_PORT >= 0) && (CONFIG_FIDO2_CTAP_UP_BUTTON_PIN >= 0)
127 #define CTAP_UP_BUTTON GPIO_PIN(CONFIG_FIDO2_CTAP_UP_BUTTON_PORT, CONFIG_FIDO2_CTAP_UP_BUTTON_PIN)
128 #else
129 /* set default button if no button is configured */
130 #ifdef BTN0_PIN
131 #define CTAP_UP_BUTTON BTN0_PIN
132 #else
133 #define CTAP_UP_BUTTON 0
137 #define CONFIG_FIDO2_CTAP_DISABLE_UP 1
138 #endif /* BTN0_PIN */
139 #endif
140 
144 #if IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN_PU)
145 #define CTAP_UP_BUTTON_MODE GPIO_IN_PU
146 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN_PD)
147 #define CTAP_UP_BUTTON_MODE GPIO_IN_PD
148 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_MODE_IN)
149 #define CTAP_UP_BUTTON_MODE GPIO_IN
150 #else
151 #define CTAP_UP_BUTTON_MODE GPIO_IN_PU
152 #endif
153 
157 #if IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_FALLING)
158 #define CTAP_UP_BUTTON_FLANK GPIO_FALLING
159 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_RISING)
160 #define CTAP_UP_BUTTON_FLANK GPIO_RISING
161 #elif IS_ACTIVE(CONFIG_FIDO2_CTAP_UP_BUTTON_FLANK_BOTH)
162 #define CTAP_UP_BUTTON_FLANK GPIO_BOTH
163 #else
164 #define CTAP_UP_BUTTON_FLANK GPIO_FALLING
165 #endif
166 
170 #ifndef CONFIG_FIDO2_CTAP_DISABLE_UP
171 #define CONFIG_FIDO2_CTAP_DISABLE_UP 0
172 #endif
173 
177 #ifndef CONFIG_FIDO2_CTAP_DISABLE_LED
178 #define CONFIG_FIDO2_CTAP_DISABLE_LED 0
179 #endif
180 
184 #define CTAP_RP_MAX_NAME_SIZE 32
185 
189 #define CTAP_USER_MAX_NAME_SIZE 64 + 1
190 
194 #define CTAP_USER_ID_MAX_SIZE 64
195 
199 #define CTAP_DOMAIN_NAME_MAX_SIZE 253 + 1
200 
204 #define CTAP_ICON_MAX_SIZE 128 + 1
205 
209 #define CTAP_PIN_MIN_SIZE 4
210 
217 #define CTAP_PIN_ENC_MIN_SIZE 64
218 
223 #define CTAP_PIN_ENC_MAX_SIZE 256
224 
228 #define CTAP_PIN_MAX_SIZE 64
229 
233 #define CTAP_PIN_MAX_ATTS 8
234 
238 #define CTAP_PIN_MAX_ATTS_BOOT 3
239 
243 #define CTAP_PIN_PROT_VER 1
244 
248 #define CTAP_AMT_SUP_PIN_VER 1
249 
255 #define CTAP_PIN_TOKEN_SZ 16
256 
264 #define CTAP_CRED_KEY_LEN 16
265 
273 #define CTAP_AES_CCM_L 2
274 
278 #define CTAP_AES_CCM_NONCE_SIZE (15 - CTAP_AES_CCM_L)
279 
285 #define CTAP_CREDENTIAL_ID_ENC_SIZE (sizeof(struct ctap_resident_key) - \
286  sizeof(((struct ctap_resident_key *)0)-> \
287  cred_desc.cred_id) - \
288  sizeof(((struct ctap_resident_key *)0)-> \
289  cred_desc.has_nonce))
290 
294 #ifdef CONFIG_FIDO2_CTAP_UP_TIMEOUT
295 #define CTAP_UP_TIMEOUT (CONFIG_FIDO2_CTAP_UP_TIMEOUT * MS_PER_SEC)
296 #else
297 #define CTAP_UP_TIMEOUT (15 * MS_PER_SEC)
298 #endif
299 
304 #define CTAP_GET_NEXT_ASSERTION_TIMEOUT (30 * MS_PER_SEC)
305 
309 #ifdef CONFIG_FIDO2_CTAP_DEVICE_AAGUID
310 #define CTAP_AAGUID CONFIG_FIDO2_CTAP_DEVICE_AAGUID
311 #else
312 /* randomly generated fallback value */
313 #define CTAP_AAGUID "9c295865fa2c36b705a42320af9c8f16"
314 #endif
315 
321 #define CTAP_PUB_KEY_CRED_PUB_KEY 0x01
322 #define CTAP_PUB_KEY_CRED_UNKNOWN 0x02
330 #define CTAP_COSE_KEY_LABEL_KTY 1
331 #define CTAP_COSE_KEY_LABEL_ALG 3
332 #define CTAP_COSE_KEY_LABEL_CRV -1
333 #define CTAP_COSE_KEY_LABEL_X -2
334 #define CTAP_COSE_KEY_LABEL_Y -3
335 #define CTAP_COSE_KEY_KTY_EC2 2
336 #define CTAP_COSE_KEY_CRV_P256 1
342 #define CTAP_AAGUID_SIZE 16
343 
347 #define CTAP_COSE_ALG_ES256 -7
348 
352 #define CTAP_COSE_ALG_ECDH_ES_HKDF_256 -25
353 
358 #define CTAP_CREDENTIAL_ID_SIZE 16U
359 
366 #define CTAP_INITIALIZED_MARKER 0x4e
367 
371 #define CTAP_MAX_EXCLUDE_LIST_SIZE 0x14
372 
376 typedef struct ctap_cred_desc ctap_cred_desc_t;
377 
382 
387 
391 typedef struct {
392  uint8_t aaguid[CTAP_AAGUID_SIZE];
393  uint8_t options;
394 } ctap_config_t;
395 
401 typedef struct {
405  uint16_t rk_amount_stored;
407  uint8_t pin_hash[SHA256_DIGEST_LENGTH / 2];
408  uint8_t cred_key[CTAP_CRED_KEY_LEN];
410  bool pin_is_set;
411  uint32_t id_cnt;
412 } ctap_state_t;
413 
417 typedef struct {
418  int rk;
419  int uv;
420  int up;
422 
426 typedef struct {
427  uint8_t id[CTAP_USER_ID_MAX_SIZE];
428  uint8_t id_len;
429  uint8_t name[CTAP_USER_MAX_NAME_SIZE];
430  uint8_t display_name[CTAP_USER_MAX_NAME_SIZE];
433 
438 typedef struct {
439  uint8_t id[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
440  uint8_t id_len;
442  uint8_t name[CTAP_RP_MAX_NAME_SIZE + 1];
444  uint8_t icon[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
446 } ctap_rp_ent_t;
447 
453 typedef struct {
455  int kty;
456  int crv;
457  int32_t alg_type;
458  uint8_t cred_type;
460 
470  uint8_t cred_type;
471  union {
474  };
475  bool has_nonce;
477 };
478 
485 struct __attribute__((packed)) ctap_resident_key {
486  uint8_t rp_id_hash[SHA256_DIGEST_LENGTH];
487  uint8_t user_id[CTAP_USER_ID_MAX_SIZE];
488  uint8_t user_id_len;
489  uint8_t priv_key[CTAP_CRYPTO_KEY_SIZE];
490  uint16_t id;
491  uint32_t sign_count;
496 };
497 
504 typedef struct __attribute__((packed)) {
506  uint8_t mac[CCM_MAC_MAX_LEN];
507  uint8_t nonce[CTAP_AES_CCM_NONCE_SIZE];
509 
516  uint8_t cred_type;
518 };
519 
523 typedef struct {
529  uint8_t client_data_hash[SHA256_DIGEST_LENGTH];
530  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
531  size_t pin_auth_len;
532  int32_t alg_type;
534  uint8_t pin_protocol;
535  uint8_t cred_type;
537 
541 typedef struct {
544  uint8_t client_data_hash[SHA256_DIGEST_LENGTH];
545  uint8_t rp_id[CTAP_DOMAIN_NAME_MAX_SIZE + 1];
546  uint8_t rp_id_len;
547  uint8_t allow_list_len;
548  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
549  size_t pin_auth_len;
550  uint8_t pin_protocol;
553 
557 typedef struct {
559  uint16_t new_pin_enc_size;
560  uint8_t pin_auth[CTAP_PIN_AUTH_SZ];
561  uint8_t new_pin_enc[CTAP_PIN_ENC_MAX_SIZE];
562  uint8_t pin_hash_enc[SHA256_DIGEST_LENGTH / 2];
564  uint8_t pin_protocol;
569 
575 typedef struct __attribute__((packed)){
576  uint8_t aaguid[CTAP_AAGUID_SIZE];
577  uint8_t cred_len_h;
578  uint8_t cred_len_l;
581 
585 typedef struct {
589 
595 typedef struct __attribute__((packed)){
596  uint8_t rp_id_hash[SHA256_DIGEST_LENGTH];
597  uint8_t flags;
598  uint32_t sign_count;
600 
604 typedef struct {
608 
612 typedef struct {
613  uint16_t max_msg_size;
614  uint8_t aaguid[CTAP_AAGUID_SIZE];
615  uint8_t versions;
616  uint8_t options;
617  uint8_t pin_protocol;
618  bool pin_is_set;
619 } ctap_info_t;
620 
635 int fido2_ctap_get_sig(const uint8_t *auth_data, size_t auth_data_len,
636  const uint8_t *client_data_hash,
637  const ctap_resident_key_t *rk,
638  uint8_t *sig, size_t *sig_len);
639 
649 bool fido2_ctap_cred_params_supported(uint8_t cred_type, int32_t alg_type);
650 
662  size_t nonce_len, ctap_cred_id_t *id);
663 
671 
678 
679 #ifdef __cplusplus
680 }
681 #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:47
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:37
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:358
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:285
#define CTAP_PIN_AUTH_SZ
Size of pin auth.
Definition: ctap.h:50
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:184
#define CTAP_PIN_ENC_MAX_SIZE
Encrypted newPin max size.
Definition: ctap.h:223
ctap_pin_subcommand_t
CTAP Client PIN request subCommand CBOR key values.
Definition: ctap.h:104
#define CTAP_AAGUID_SIZE
CTAP size of authenticator AAGUID in bytes.
Definition: ctap.h:342
#define CTAP_DOMAIN_NAME_MAX_SIZE
Max size of a domain name including null character.
Definition: ctap.h:199
#define CTAP_MAX_EXCLUDE_LIST_SIZE
Max size of allow list.
Definition: ctap.h:371
#define CTAP_USER_ID_MAX_SIZE
Max size of user id.
Definition: ctap.h:194
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:278
#define CTAP_USER_MAX_NAME_SIZE
Max size of username including null character.
Definition: ctap.h:189
#define CTAP_CRED_KEY_LEN
Size of key used to encrypt credential.
Definition: ctap.h:264
@ CTAP_PIN_SET_PIN
setPIN subCommand
Definition: ctap.h:107
@ CTAP_PIN_GET_PIN_TOKEN
getPinToken subCommand
Definition: ctap.h:109
@ CTAP_PIN_CHANGE_PIN
changePIN subCommand
Definition: ctap.h:108
@ CTAP_PIN_GET_KEY_AGREEMENT
getKeyAgreement subCommand
Definition: ctap.h:106
@ CTAP_PIN_GET_RETRIES
getRetries subCommand
Definition: ctap.h:105
#define SHA256_DIGEST_LENGTH
Length of SHA256 digests in bytes.
Definition: sha256.h:61
Mutex for thread synchronization.
CTAP attested credential data header struct.
Definition: ctap.h:575
ctap_cred_id_t cred_id
credential id
Definition: ctap.h:579
uint8_t cred_len_l
lower byte of credential length
Definition: ctap.h:578
uint8_t cred_len_h
higher byte of credential length
Definition: ctap.h:577
CTAP attested credential data struct.
Definition: ctap.h:585
ctap_public_key_cose_t key
cose key
Definition: ctap.h:587
ctap_attested_cred_data_header_t header
attested credential data header
Definition: ctap.h:586
CTAP authenticator data header struct.
Definition: ctap.h:595
uint8_t flags
flags indicating result of user verification
Definition: ctap.h:597
uint32_t sign_count
sign count of credential
Definition: ctap.h:598
CTAP authenticator data struct.
Definition: ctap.h:604
ctap_attested_cred_data_t attested_cred_data
attested credential data
Definition: ctap.h:606
ctap_auth_data_header_t header
auth data header
Definition: ctap.h:605
CTAP client pin request struct.
Definition: ctap.h:557
bool key_agreement_present
indicate if key_agreement present
Definition: ctap.h:567
uint16_t new_pin_enc_size
size of encrypted new pin
Definition: ctap.h:559
ctap_pin_subcommand_t sub_command
ClientPIN sub command.
Definition: ctap.h:563
ctap_public_key_cose_t key_agreement
public key of platform_key_agreement_key
Definition: ctap.h:558
bool pin_auth_present
indicate if pin_auth present
Definition: ctap.h:566
uint8_t pin_protocol
PIN protocol version chosen by the client.
Definition: ctap.h:564
bool pin_hash_enc_present
indicate pin_hash_enc is present
Definition: ctap.h:565
CTAP authenticator config struct.
Definition: ctap.h:391
uint8_t options
options
Definition: ctap.h:393
CTAP credential description alternative struct.
Definition: ctap.h:515
uint8_t cred_type
type of credential
Definition: ctap.h:516
ctap_cred_id_t cred_id
credential id
Definition: ctap.h:517
CTAP credential description struct.
Definition: ctap.h:469
uint8_t cred_id[CTAP_CREDENTIAL_ID_SIZE]
credential identifier
Definition: ctap.h:472
uint8_t nonce[CTAP_AES_CCM_NONCE_SIZE]
CTAP AES CCM nonce.
Definition: ctap.h:473
uint8_t cred_type
type of credential
Definition: ctap.h:470
bool has_nonce
Indicate if nonce or cred_id
Definition: ctap.h:475
CTAP credential ID.
Definition: ctap.h:504
Elliptic curve public key.
Definition: ctap_crypto.h:47
CTAP get assertion request struct.
Definition: ctap.h:541
uint8_t pin_protocol
PIN protocol version.
Definition: ctap.h:550
bool pin_auth_present
indicate if pin_auth present
Definition: ctap.h:551
size_t pin_auth_len
pin_auth length
Definition: ctap.h:549
uint8_t rp_id_len
Actual Length of Relying Party Identifier.
Definition: ctap.h:546
uint8_t allow_list_len
length of CBOR allow list array
Definition: ctap.h:547
ctap_options_t options
parameters to influence authenticator operation
Definition: ctap.h:542
CTAP info struct.
Definition: ctap.h:612
bool pin_is_set
PIN is set or not.
Definition: ctap.h:618
uint8_t options
supported options
Definition: ctap.h:616
uint8_t pin_protocol
supported PIN protocol versions
Definition: ctap.h:617
uint8_t versions
supported versions of FIDO
Definition: ctap.h:615
uint16_t max_msg_size
max message size
Definition: ctap.h:613
CTAP make credential request struct.
Definition: ctap.h:523
ctap_user_ent_t user
user
Definition: ctap.h:527
bool pin_auth_present
pin_auth present
Definition: ctap.h:533
int32_t alg_type
cryptographic algorithm identifier
Definition: ctap.h:532
size_t exclude_list_len
length of CBOR exclude list array
Definition: ctap.h:525
ctap_rp_ent_t rp
relying party
Definition: ctap.h:526
size_t pin_auth_len
pin_auth len
Definition: ctap.h:531
uint8_t cred_type
type of credential
Definition: ctap.h:535
ctap_options_t options
parameters to influence authenticator operation
Definition: ctap.h:528
uint8_t pin_protocol
PIN protocol version.
Definition: ctap.h:534
CTAP options struct.
Definition: ctap.h:417
int up
user presence
Definition: ctap.h:420
int uv
user verification
Definition: ctap.h:419
int rk
resident key
Definition: ctap.h:418
CTAP cose key struct.
Definition: ctap.h:453
int crv
EC identifier.
Definition: ctap.h:456
int32_t alg_type
COSEAlgorithmIdentifier.
Definition: ctap.h:457
int kty
identification of key type
Definition: ctap.h:455
ctap_crypto_pub_key_t pubkey
public key
Definition: ctap.h:454
uint8_t cred_type
type of credential
Definition: ctap.h:458
CTAP resident key struct.
Definition: ctap.h:485
ctap_cred_desc_t cred_desc
credential descriptor
Definition: ctap.h:495
uint32_t sign_count
signature counter.
Definition: ctap.h:491
uint8_t user_id_len
length of the user id
Definition: ctap.h:488
uint16_t id
internal id of key
Definition: ctap.h:490
CTAP relying party entity struct.
Definition: ctap.h:438
uint8_t id_len
actual length of relying party identifier
Definition: ctap.h:440
CTAP state struct.
Definition: ctap.h:401
uint16_t rk_amount_stored
total number of resident keys stored on device
Definition: ctap.h:405
int rem_pin_att
remaining PIN tries
Definition: ctap.h:404
bool pin_is_set
PIN is set or not.
Definition: ctap.h:410
ctap_config_t config
configuration of authenticator
Definition: ctap.h:402
bool cred_key_is_initialized
AES CCM key initialized flag.
Definition: ctap.h:409
uint8_t initialized_marker
CTAP initialized marker.
Definition: ctap.h:406
ctap_crypto_key_agreement_key_t ag_key
Platform key agreement key.
Definition: ctap.h:403
uint32_t id_cnt
id counter for credential id
Definition: ctap.h:411
CTAP user entity struct.
Definition: ctap.h:426
uint8_t id_len
actual length of user id
Definition: ctap.h:428
Utility library for comparing and computing timestamps.