1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "macro.h"
5 
6 typedef enum Fido2EnrollFlags {
7         FIDO2ENROLL_PIN           = 1 << 0,
8         FIDO2ENROLL_UP            = 1 << 1, /* User presence (ie: touching token) */
9         FIDO2ENROLL_UV            = 1 << 2, /* User verification (ie: fingerprint) */
10         FIDO2ENROLL_PIN_IF_NEEDED = 1 << 3, /* If auth doesn't work without PIN ask for one, as in systemd 248 */
11         FIDO2ENROLL_UP_IF_NEEDED  = 1 << 4, /* If auth doesn't work without UP, enable it, as in systemd 248 */
12         FIDO2ENROLL_UV_OMIT       = 1 << 5, /* Leave "uv" untouched, as in systemd 248 */
13         _FIDO2ENROLL_TYPE_MAX,
14         _FIDO2ENROLL_TYPE_INVALID = -EINVAL,
15 } Fido2EnrollFlags;
16 
17 #if HAVE_LIBFIDO2
18 #include <fido.h>
19 
20 extern int (*sym_fido_assert_allow_cred)(fido_assert_t *, const unsigned char *, size_t);
21 extern void (*sym_fido_assert_free)(fido_assert_t **);
22 extern size_t (*sym_fido_assert_hmac_secret_len)(const fido_assert_t *, size_t);
23 extern const unsigned char* (*sym_fido_assert_hmac_secret_ptr)(const fido_assert_t *, size_t);
24 extern fido_assert_t* (*sym_fido_assert_new)(void);
25 extern int (*sym_fido_assert_set_clientdata_hash)(fido_assert_t *, const unsigned char *, size_t);
26 extern int (*sym_fido_assert_set_extensions)(fido_assert_t *, int);
27 extern int (*sym_fido_assert_set_hmac_salt)(fido_assert_t *, const unsigned char *, size_t);
28 extern int (*sym_fido_assert_set_rp)(fido_assert_t *, const char *);
29 extern int (*sym_fido_assert_set_up)(fido_assert_t *, fido_opt_t);
30 extern int (*sym_fido_assert_set_uv)(fido_assert_t *, fido_opt_t);
31 extern size_t (*sym_fido_cbor_info_extensions_len)(const fido_cbor_info_t *);
32 extern char **(*sym_fido_cbor_info_extensions_ptr)(const fido_cbor_info_t *);
33 extern void (*sym_fido_cbor_info_free)(fido_cbor_info_t **);
34 extern fido_cbor_info_t* (*sym_fido_cbor_info_new)(void);
35 extern size_t (*sym_fido_cbor_info_options_len)(const fido_cbor_info_t *);
36 extern char** (*sym_fido_cbor_info_options_name_ptr)(const fido_cbor_info_t *);
37 extern const bool* (*sym_fido_cbor_info_options_value_ptr)(const fido_cbor_info_t *);
38 extern void (*sym_fido_cred_free)(fido_cred_t **);
39 extern size_t (*sym_fido_cred_id_len)(const fido_cred_t *);
40 extern const unsigned char* (*sym_fido_cred_id_ptr)(const fido_cred_t *);
41 extern fido_cred_t* (*sym_fido_cred_new)(void);
42 extern int (*sym_fido_cred_set_clientdata_hash)(fido_cred_t *, const unsigned char *, size_t);
43 extern int (*sym_fido_cred_set_extensions)(fido_cred_t *, int);
44 extern int (*sym_fido_cred_set_rk)(fido_cred_t *, fido_opt_t);
45 extern int (*sym_fido_cred_set_rp)(fido_cred_t *, const char *, const char *);
46 extern int (*sym_fido_cred_set_type)(fido_cred_t *, int);
47 extern int (*sym_fido_cred_set_user)(fido_cred_t *, const unsigned char *, size_t, const char *, const char *, const char *);
48 extern int (*sym_fido_cred_set_uv)(fido_cred_t *, fido_opt_t);
49 extern void (*sym_fido_dev_free)(fido_dev_t **);
50 extern int (*sym_fido_dev_get_assert)(fido_dev_t *, fido_assert_t *, const char *);
51 extern int (*sym_fido_dev_get_cbor_info)(fido_dev_t *, fido_cbor_info_t *);
52 extern void (*sym_fido_dev_info_free)(fido_dev_info_t **, size_t);
53 extern int (*sym_fido_dev_info_manifest)(fido_dev_info_t *, size_t, size_t *);
54 extern const char* (*sym_fido_dev_info_manufacturer_string)(const fido_dev_info_t *);
55 extern const char* (*sym_fido_dev_info_product_string)(const fido_dev_info_t *);
56 extern fido_dev_info_t* (*sym_fido_dev_info_new)(size_t);
57 extern const char* (*sym_fido_dev_info_path)(const fido_dev_info_t *);
58 extern const fido_dev_info_t* (*sym_fido_dev_info_ptr)(const fido_dev_info_t *, size_t);
59 extern bool (*sym_fido_dev_is_fido2)(const fido_dev_t *);
60 extern int (*sym_fido_dev_make_cred)(fido_dev_t *, fido_cred_t *, const char *);
61 extern fido_dev_t* (*sym_fido_dev_new)(void);
62 extern int (*sym_fido_dev_open)(fido_dev_t *, const char *);
63 extern int (*sym_fido_dev_close)(fido_dev_t *);
64 extern const char* (*sym_fido_strerr)(int);
65 
66 int dlopen_libfido2(void);
67 
fido_cbor_info_free_wrapper(fido_cbor_info_t ** p)68 static inline void fido_cbor_info_free_wrapper(fido_cbor_info_t **p) {
69         if (*p)
70                 sym_fido_cbor_info_free(p);
71 }
72 
fido_assert_free_wrapper(fido_assert_t ** p)73 static inline void fido_assert_free_wrapper(fido_assert_t **p) {
74         if (*p)
75                 sym_fido_assert_free(p);
76 }
77 
fido_dev_free_wrapper(fido_dev_t ** p)78 static inline void fido_dev_free_wrapper(fido_dev_t **p) {
79         if (*p) {
80                 sym_fido_dev_close(*p);
81                 sym_fido_dev_free(p);
82         }
83 }
84 
fido_cred_free_wrapper(fido_cred_t ** p)85 static inline void fido_cred_free_wrapper(fido_cred_t **p) {
86         if (*p)
87                 sym_fido_cred_free(p);
88 }
89 
90 int fido2_use_hmac_hash(
91                 const char *device,
92                 const char *rp_id,
93                 const void *salt,
94                 size_t salt_size,
95                 const void *cid,
96                 size_t cid_size,
97                 char **pins,
98                 Fido2EnrollFlags required,
99                 void **ret_hmac,
100                 size_t *ret_hmac_size);
101 
102 int fido2_generate_hmac_hash(
103                 const char *device,
104                 const char *rp_id,
105                 const char *rp_name,
106                 const void *user_id, size_t user_id_len,
107                 const char *user_name,
108                 const char *user_display_name,
109                 const char *user_icon,
110                 const char *askpw_icon_name,
111                 Fido2EnrollFlags lock_with,
112                 int cred_alg,
113                 void **ret_cid, size_t *ret_cid_size,
114                 void **ret_salt, size_t *ret_salt_size,
115                 void **ret_secret, size_t *ret_secret_size,
116                 char **ret_usedpin,
117                 Fido2EnrollFlags *ret_locked_with);
118 
119 int parse_fido2_algorithm(const char *s, int *ret);
120 #else
parse_fido2_algorithm(const char * s,int * ret)121 static inline int parse_fido2_algorithm(const char *s, int *ret) {
122         return -EOPNOTSUPP;
123 }
124 #endif
125 
126 int fido2_list_devices(void);
127 int fido2_find_device_auto(char **ret);
128 
129 int fido2_have_device(const char *device);
130