1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/*
SSSD
Helper child to commmunicate with passkey devices
Authors:
Iker Pedrosa <ipedrosa@redhat.com>
Copyright (C) 2022 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __PASSKEY_CHILD_H__
#define __PASSKEY_CHILD_H__
#include <fido.h>
#define DEFAULT_PROMPT "Insert your passkey device, then press ENTER."
#define DEFAULT_CUE "Please touch the device."
#define DEVLIST_SIZE 64
#define USER_ID_SIZE 32
#define TIMEOUT 15
#define FREQUENCY 1
enum action_opt {
ACTION_NONE,
ACTION_REGISTER,
ACTION_AUTHENTICATE,
ACTION_GET_ASSERT,
ACTION_VERIFY_ASSERT
};
enum credential_type {
CRED_SERVER_SIDE,
CRED_DISCOVERABLE
};
struct passkey_data {
enum action_opt action;
const char *shortname;
const char *domain;
char **key_handle_list;
int key_handle_size;
char **public_key_list;
int public_key_size;
const char *crypto_challenge;
const char *auth_data;
const char *signature;
int type;
fido_opt_t user_verification;
enum credential_type cred_type;
unsigned char *user_id;
char *mapping_file;
bool quiet;
bool debug_libfido2;
};
struct pk_data_t {
void *public_key;
int type;
};
/**
* @brief Parse arguments
*
* @param[in] mem_ctx Memory context
* @param[in] argc Number of arguments
* @param[in] argv Argument list
* @param[out] data passkey data
*
* @return 0 if the arguments were parsed properly,
* another value on error.
*/
errno_t
parse_arguments(TALLOC_CTX *mem_ctx, int argc, const char *argv[],
struct passkey_data *data);
/**
* @brief Check that all the arguments have been set
*
* @param[in] data passkey data
*
* @return 0 if the arguments were set properly,
* another value on error.
*/
errno_t
check_arguments(const struct passkey_data *data);
/**
* @brief Register a key for a user
*
* @param[in] data passkey data
*
* @return 0 if the key was registered properly,
* another value on error.
*/
errno_t
register_key(struct passkey_data *data);
/**
* @brief Translate COSE type from string to int
*
* @param[in] type string COSE type
* @param[out] out int COSE type
*
* @return 0 if the COSE type has been translated,
* another value if the COSE type doesn't exist.
*/
errno_t
cose_str_to_int(const char *type, int *out);
/**
* @brief Prepare user credentials
*
* @param[in] data passkey data
* @param[in] dev Device information
* @param[out] cred Credentials
*
* @return 0 if the credentials were prepared properly,
* another value on error.
*/
errno_t
prepare_credentials(struct passkey_data *data, fido_dev_t *dev,
fido_cred_t *cred);
/**
* @brief List connected passkey devices
*
* @param[out] dev_list passkey device list
* @param[out] dev_number Number of passkey devices
*
* @return 0 if the list was retrieved properly, another value on error.
*/
errno_t
list_devices(fido_dev_info_t *dev_list, size_t *dev_number);
/**
* @brief Select passkey device
*
* @param[in] action Action to perform with the key
* @param[in] dev_list passkey device list
* @param[in] dev_index passkey device index
* @param[in] assert Assert
* @param[out] dev Device information
*
* @return 0 if the device was opened properly, another value on error.
*/
errno_t
select_device(enum action_opt action, fido_dev_info_t *dev_list,
size_t dev_list_len, fido_assert_t *assert,
fido_dev_t **_dev);
/**
* @brief Get authenticator data from assert
*
* @param[in] dev_list passkey device list
* @param[in] dev_list_len passkey device list length
* @param[in] assert Assert
* @param[out] dev Authenticator data
*
* @return 0 if the authenticator data was retrieved properly,
* another value on error.
*/
errno_t
select_from_multiple_devices(fido_dev_info_t *dev_list,
size_t dev_list_len,
fido_assert_t *assert,
fido_dev_t **_dev);
/**
* @brief Receive PIN via stdin
*
* @param[in] mem_ctx Memory context
* @param[in] fd File descriptor
* @param[out] pin Pin
*
* @return 0 if the authenticator data was received properly,
* error code otherwise.
*/
errno_t
passkey_recv_pin(TALLOC_CTX *mem_ctx, int fd, char **_pin);
/**
* @brief Disable echoing and read PIN from stdin
*
* @param[out] line_ptr PIN
*
* @return Number of bytes read, or -1 on error.
*/
ssize_t
read_pin(char **line_ptr);
/**
* @brief Generate passkey credentials
*
* @param[in] data passkey data
* @param[in] dev Device information
* @param[out] cred Credentials
*
* @return 0 if the credentials were generated properly,
* another value on error.
*/
errno_t
generate_credentials(struct passkey_data *data, fido_dev_t *dev,
fido_cred_t *cred);
/**
* @brief Verify passkey credentials
*
* @param[in] cred Credentials
*
* @return 0 if the credentials were verified properly,
* another value on error.
*/
errno_t
verify_credentials(const fido_cred_t *const cred);
/**
* @brief Print passkey credentials
*
* @param[in] data passkey data
* @param[out] cred Credentials
*
* @return 0 if the credentials were printed properly,
* another value on error.
*/
errno_t
print_credentials(const struct passkey_data *data,
const fido_cred_t *const cred);
/**
* @brief Print passkey credentials
*
* @param[in] data passkey data
* @param[in] b64_cred_id Credential ID in b64
* @param[in] pem_key Public key in PEM format
*
* @return 0 if the credentials were printed properly,
* another value on error.
*/
errno_t
print_credentials_to_file(const struct passkey_data *data,
const char *b64_cred_id,
const char *pem_key);
/**
* @brief Format libfido2's es256 data structure to EVP_PKEY
*
* @param[in] mem_ctx Memory context
* @param[in] es256_key Public key pointer
* @param[in] es256_key_len Public key length
* @param[out] _evp_pkey Pointer to public key structure
*
* @return 0 if the key was formatted properly, error code otherwise.
*/
int
es256_pubkey_to_evp_pkey(TALLOC_CTX *mem_ctx, const void *es256_key,
size_t es256_key_len, EVP_PKEY **_evp_pkey);
/**
* @brief Format libfido2's rs256 data structure to EVP_PKEY
*
* @param[in] mem_ctx Memory context
* @param[in] rs256_key Public key pointer
* @param[in] rs256_key_len Public key length
* @param[out] _evp_pkey Pointer to public key structure
*
* @return 0 if the key was formatted properly, error code otherwise.
*/
int
rs256_pubkey_to_evp_pkey(TALLOC_CTX *mem_ctx, const void *rs256_key,
size_t rs256_key_len, EVP_PKEY **_evp_pkey);
/**
* @brief Format libfido2's eddsa data structure to EVP_PKEY
*
* @param[in] mem_ctx Memory context
* @param[in] eddsa_key Public key pointer
* @param[in] eddsa_key_len Public key length
* @param[out] _evp_pkey Pointer to public key structure
*
* @return 0 if the key was formatted properly, error code otherwise.
*/
int
eddsa_pubkey_to_evp_pkey(TALLOC_CTX *mem_ctx, const void *eddsa_key,
size_t eddsa_key_len, EVP_PKEY **_evp_pkey);
/**
* @brief Format the public key to base64
*
* @param[in] mem_ctx Memory context
* @param[in] data passkey data
* @param[in] public_key Public key
* @param[in] pk_len Public key length
* @param[out] _pem_key Public key in PEM format
*
* @return 0 if the key was formatted properly, error code otherwise.
*/
errno_t
public_key_to_base64(TALLOC_CTX *mem_ctx, const struct passkey_data *data,
const unsigned char *public_key, size_t pk_len,
char **_pem_key);
/*
* @brief Authenticate a user
*
* Prepare the assertion request data, select the device to use, get the device
* options and compare them with the organization policy, decode the public
* key, request the assert and verify it.
*
* @param[in] data passkey data
*
* @return 0 if the user was authenticated properly,
* error code otherwise.
*/
errno_t
authenticate(struct passkey_data *data);
/*
* @brief Select authenticator for verification
*
*
* @param[in] data passkey data
* @param[out] _dev Device information
* @param[out] _assert Assert
* @param[out] _index Index for key handle list
*
* @return 0 if the authenticator was selected properly,
* error code otherwise.
*/
errno_t
select_authenticator(struct passkey_data *data, fido_dev_t **_dev,
fido_assert_t **_assert, int *_index);
/**
* @brief Set client data hash in the assert
*
* @param[in] data passkey data
* @param[in,out] _assert Assert
*
* @return 0 if the data was set properly,
* error code otherwise.
*/
errno_t
set_assert_client_data_hash(const struct passkey_data *data,
fido_assert_t *_assert);
/**
* @brief Set authenticator data and signature in the assert
*
* @param[in] data passkey data
* @param[in,out] _assert Assert
*
* @return 0 if the data was set properly,
* error code otherwise.
*/
errno_t
set_assert_auth_data_signature(const struct passkey_data *data,
fido_assert_t *_assert);
/**
* @brief Set options in the assert
*
* @param[in] up User presence check
* @param[in] uv User verification check
* @param[out] assert Assert
*
* @return 0 if the data was set properly,
* error code otherwise.
*/
errno_t
set_assert_options(fido_opt_t up, fido_opt_t uv, fido_assert_t *_assert);
/**
* @brief Get authentication data and signature from assert
*
* @param[in] mem_ctx Memory context
* @param[in] assert Assert
* @param[out] _auth_data Authentication data
* @param[out] _signature Signature
*
* @return 0 if the data was get properly,
* error code otherwise.
*/
errno_t
get_assert_auth_data_signature(TALLOC_CTX *mem_ctx, fido_assert_t *assert,
const char **_auth_data,
const char **_signature);
/**
* @brief Prepare assert
*
* @param[in] data passkey data
* @param[in] index Index for key handle list
* @param[in,out] _assert Assert
*
* @return 0 if the assert was prepared properly,
* error code otherwise.
*/
errno_t
prepare_assert(const struct passkey_data *data, int index,
fido_assert_t *_assert);
/**
* @brief Reset and free public key
*
* @param[out] _pk_data Public key data
*
* @return 0 if the public key was reset properly,
* error code otherwise.
*/
errno_t
reset_public_key(struct pk_data_t *_pk_data);
/**
* @brief Format EVP_PKEY to libfido2's es256 data structure
*
* @param[in] evp_pkey EVP_PKEY public key
* @param[out] _pk_data Public key data
*
* @return 0 if the public key was formatted properly,
* error code otherwise.
*/
errno_t
evp_pkey_to_es256_pubkey(const EVP_PKEY *evp_pkey, struct pk_data_t *_pk_data);
/**
* @brief Format EVP_PKEY to libfido2's rs256 data structure
*
* @param[in] evp_pkey EVP_PKEY public key
* @param[out] _pk_data Public key data
*
* @return 0 if the public key was formatted properly,
* error code otherwise.
*/
errno_t
evp_pkey_to_rs256_pubkey(const EVP_PKEY *evp_pkey, struct pk_data_t *_pk_data);
/**
* @brief Format EVP_PKEY to libfido2's eddsa data structure
*
* @param[in] evp_pkey EVP_PKEY public key
* @param[out] _pk_data Public key data
*
* @return 0 if the public key was formatted properly,
* error code otherwise.
*/
errno_t
evp_pkey_to_eddsa_pubkey(const EVP_PKEY *evp_pkey, struct pk_data_t *_pk_data);
/**
* @brief Format the public key to the libfido2 data structure
*
* @param[in] pem_public_key PEM formatter public key
* @param[out] _pk_data Public key data
*
* @return 0 if the public key was formatted properly,
* error code otherwise.
*/
errno_t
public_key_to_libfido2(const char *pem_public_key, struct pk_data_t *_pk_data);
/**
* @brief Get device options and compare with the policy options expectations
*
* @param[in] dev Device information
* @param[out] data passkey data
*
* @return 0 if the device data was retrieved and the options match properly,
* error code otherwise.
*/
errno_t
get_device_options(fido_dev_t *dev, struct passkey_data *_data);
/**
* @brief Get assertion data
*
* @param[in] data passkey data
* @param[in] dev Device information
* @param[out] assert Assert
*
* @return 0 if the assertion was verified properly,
* error code otherwise.
*/
errno_t
request_assert(struct passkey_data *data, fido_dev_t *dev,
fido_assert_t *_assert);
/**
* @brief Verify assertion
*
* @param[in] pk_data Public key data
* @param[in] assert Assert
*
* @return 0 if the assertion was verified properly,
* error code otherwise.
*/
errno_t
verify_assert(struct pk_data_t *data, fido_assert_t *assert);
/**
* @brief Print assert request data in JSON format
*
* @param[in] key_handle Key handle
* @param[in] crypto_challenge Cryptographic challenge
* @param[in] auth_data Authenticator data
* @param[in] signature Assertion signature
*
*/
void
print_assert_data(const char *key_handle, const char *crypto_challenge,
const char *auth_data, const char *signature);
/**
* @brief Obtain assertion data
*
* Prepare the assertion request data, select the device to use, select the
* authenticator, get the device options and compare them with the organization
* policy, request the assert, get the authenticator data, get the signature
* and print this all information.
*
* @param[in] data passkey data
*
* @return 0 if the assertion was obtained properly,
* error code otherwise.
*/
errno_t
get_assert_data(struct passkey_data *data);
/**
* @brief Verify assertion data
*
* Prepare the assertion data, including the authenticator data and the
* signature; decode the public key and verify the assertion.
*
* @param[in] data passkey data
*
* @return 0 if the assertion was obtained properly,
* error code otherwise.
*/
errno_t
verify_assert_data(struct passkey_data *data);
#endif /* __PASSKEY_CHILD_H__ */
|