| 1 |
/* |
| 2 |
* Copyright (c) 2026 Yubico AB. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style |
| 4 |
* license that can be found in the LICENSE file. |
| 5 |
* SPDX-License-Identifier: BSD-2-Clause |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "../openbsd-compat/openbsd-compat.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#include <fido.h> |
| 13 |
#include <fido/bio.h> |
| 14 |
|
| 15 |
#define TIMEOUT_MS 10000 |
| 16 |
#define TEMPLATE_NAME "bio-example" |
| 17 |
|
| 18 |
struct context { |
| 19 |
fido_bio_template_t *our; |
| 20 |
bool found; |
| 21 |
}; |
| 22 |
|
| 23 |
enum list_bio_action { |
| 24 |
LIST_BIO_CONTINUE, |
| 25 |
LIST_BIO_STOP, |
| 26 |
}; |
| 27 |
|
| 28 |
typedef enum list_bio_action list_bio_templates_cb_t( |
| 29 |
const fido_bio_template_t *, void *opaq); |
| 30 |
|
| 31 |
static int |
| 32 |
list_bio_templates(fido_dev_t *dev, list_bio_templates_cb_t *cb, void *opaq) |
| 33 |
{ |
| 34 |
fido_bio_template_array_t *templates; |
| 35 |
int r; |
| 36 |
|
| 37 |
if ((templates = fido_bio_template_array_new()) == NULL) |
| 38 |
errx(1, "fido_bio_template_array_new"); |
| 39 |
|
| 40 |
if ((r = fido_bio_dev_get_template_array(dev, templates, |
| 41 |
NULL)) != FIDO_OK) { |
| 42 |
warnx("fido_bio_dev_get_template_array: %s", fido_strerr(r)); |
| 43 |
goto exit; |
| 44 |
} |
| 45 |
|
| 46 |
for (size_t idx = 0; idx < fido_bio_template_array_count(templates); |
| 47 |
idx++) { |
| 48 |
const fido_bio_template_t *templ; |
| 49 |
|
| 50 |
if ((templ = fido_bio_template(templates, idx)) == NULL) { |
| 51 |
warnx("fido_bio_template %zu -> NULL", idx); |
| 52 |
goto exit; |
| 53 |
} |
| 54 |
|
| 55 |
if (cb(templ, opaq) == LIST_BIO_STOP) |
| 56 |
break; |
| 57 |
} |
| 58 |
|
| 59 |
r = 0; |
| 60 |
exit: |
| 61 |
fido_bio_template_array_free(&templates); |
| 62 |
return r; |
| 63 |
} |
| 64 |
|
| 65 |
static int |
| 66 |
remove_bio_template(fido_dev_t *dev, const fido_bio_template_t *our) |
| 67 |
{ |
| 68 |
int r; |
| 69 |
|
| 70 |
if ((r = fido_bio_dev_enroll_remove(dev, our, NULL)) != FIDO_OK) |
| 71 |
warnx("fido_bio_dev_enroll_remove: %s", fido_strerr(r)); |
| 72 |
return r; |
| 73 |
} |
| 74 |
|
| 75 |
static int |
| 76 |
rename_bio_template(fido_dev_t *dev, fido_bio_template_t *templ, |
| 77 |
const char *name) |
| 78 |
{ |
| 79 |
int r; |
| 80 |
|
| 81 |
if ((r = fido_bio_template_set_name(templ, name)) != FIDO_OK) |
| 82 |
warnx("fido_bio_template_set_name: %s", fido_strerr(r)); |
| 83 |
|
| 84 |
if ((r = fido_bio_dev_set_template_name(dev, templ, NULL)) != FIDO_OK) |
| 85 |
warnx("fido_bio_dev_set_template_name: %s", fido_strerr(r)); |
| 86 |
|
| 87 |
return r; |
| 88 |
} |
| 89 |
|
| 90 |
static int |
| 91 |
enroll_new(fido_dev_t *dev, fido_bio_template_t **templ_out) |
| 92 |
{ |
| 93 |
fido_bio_template_t *templ = NULL; |
| 94 |
fido_bio_enroll_t *enroll = NULL; |
| 95 |
int r; |
| 96 |
|
| 97 |
if ((templ = fido_bio_template_new()) == NULL) |
| 98 |
errx(1, "fido_bio_template_new"); |
| 99 |
|
| 100 |
if ((enroll = fido_bio_enroll_new()) == NULL) |
| 101 |
errx(1, "fido_bio_enroll_new"); |
| 102 |
|
| 103 |
warnx("Touch your authenticator."); |
| 104 |
if ((r = fido_bio_dev_enroll_begin(dev, templ, enroll, TIMEOUT_MS, |
| 105 |
NULL)) != FIDO_OK) { |
| 106 |
warnx("fido_bio_dev_enroll_begin: %s", fido_strerr(r)); |
| 107 |
goto exit; |
| 108 |
} |
| 109 |
|
| 110 |
while (fido_bio_enroll_remaining_samples(enroll) > 0) { |
| 111 |
warnx("Touch your authenticator (%hhu sample(s) left).", |
| 112 |
fido_bio_enroll_remaining_samples(enroll)); |
| 113 |
|
| 114 |
if ((r = fido_bio_dev_enroll_continue(dev, templ, enroll, |
| 115 |
TIMEOUT_MS)) != FIDO_OK) { |
| 116 |
fido_dev_cancel(dev); |
| 117 |
warnx("fido_bio_dev_enroll_continue: %s", |
| 118 |
fido_strerr(r)); |
| 119 |
goto exit; |
| 120 |
} |
| 121 |
|
| 122 |
if ((r = fido_bio_enroll_last_status( |
| 123 |
enroll)) != FIDO_BIO_ENROLL_FP_GOOD) |
| 124 |
warnx("fido_bio_enroll_last_status: %d", r); |
| 125 |
} |
| 126 |
|
| 127 |
exit: |
| 128 |
fido_bio_enroll_free(&enroll); |
| 129 |
if (r == FIDO_OK) |
| 130 |
*templ_out = templ; |
| 131 |
else |
| 132 |
fido_bio_template_free(&templ); |
| 133 |
return r; |
| 134 |
} |
| 135 |
|
| 136 |
static enum list_bio_action |
| 137 |
search_template(const fido_bio_template_t *t, void *opaq) |
| 138 |
{ |
| 139 |
struct context *c = opaq; |
| 140 |
size_t len; |
| 141 |
const char *our_name; |
| 142 |
|
| 143 |
len = fido_bio_template_id_len(t); |
| 144 |
if (len != fido_bio_template_id_len(c->our) || memcmp( |
| 145 |
fido_bio_template_id_ptr(t), fido_bio_template_id_ptr(c->our), |
| 146 |
len)) |
| 147 |
return LIST_BIO_CONTINUE; |
| 148 |
|
| 149 |
if ((our_name = fido_bio_template_name(c->our)) != NULL && |
| 150 |
our_name[0] != '\0') { |
| 151 |
const char *their_name; |
| 152 |
|
| 153 |
if ((their_name = fido_bio_template_name(t)) == NULL || |
| 154 |
strcmp(their_name, our_name) != 0) |
| 155 |
return LIST_BIO_CONTINUE; |
| 156 |
} |
| 157 |
|
| 158 |
c->found = true; |
| 159 |
return LIST_BIO_STOP; |
| 160 |
} |
| 161 |
|
| 162 |
static int |
| 163 |
run(fido_dev_t *dev, const char *pin) |
| 164 |
{ |
| 165 |
int r; |
| 166 |
struct context ctx; |
| 167 |
|
| 168 |
if ((r = fido_dev_get_puat(dev, FIDO_PUAT_BIOENROLL, NULL, |
| 169 |
pin)) != FIDO_OK) { |
| 170 |
warnx("could not get puat: %s", fido_strerr(r)); |
| 171 |
return r; |
| 172 |
} |
| 173 |
|
| 174 |
memset(&ctx, 0, sizeof(ctx)); |
| 175 |
|
| 176 |
warnx("1. Enrollment..."); |
| 177 |
if ((r = enroll_new(dev, &ctx.our)) != FIDO_OK) |
| 178 |
goto exit; |
| 179 |
|
| 180 |
warnx("2. Rename"); |
| 181 |
if ((r = rename_bio_template(dev, ctx.our, TEMPLATE_NAME)) != FIDO_OK) |
| 182 |
goto exit; |
| 183 |
|
| 184 |
warnx("3. Scan"); |
| 185 |
if ((r = list_bio_templates(dev, search_template, &ctx)) != FIDO_OK) |
| 186 |
goto exit; |
| 187 |
if (!ctx.found) { |
| 188 |
warnx("unexpectedly missing template"); |
| 189 |
r = -1; |
| 190 |
goto exit; |
| 191 |
} |
| 192 |
|
| 193 |
warnx("4. Remove"); |
| 194 |
r = remove_bio_template(dev, ctx.our); |
| 195 |
exit: |
| 196 |
fido_bio_template_free(&ctx.our); |
| 197 |
return r; |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
usage(void) |
| 202 |
{ |
| 203 |
fprintf(stderr, "usage: bio [-P pin] <device>\n"); |
| 204 |
exit(1); |
| 205 |
} |
| 206 |
|
| 207 |
int |
| 208 |
main(int argc, char **argv) |
| 209 |
{ |
| 210 |
fido_dev_t *dev = NULL; |
| 211 |
int r, ch; |
| 212 |
const char *path, *pin = NULL; |
| 213 |
|
| 214 |
while ((ch = getopt(argc, argv, "P:")) != -1) { |
| 215 |
switch (ch) { |
| 216 |
case 'P': |
| 217 |
pin = optarg; |
| 218 |
break; |
| 219 |
default: |
| 220 |
usage(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (argc - optind < 1) |
| 225 |
usage(); |
| 226 |
path = argv[optind]; |
| 227 |
|
| 228 |
fido_init(0); |
| 229 |
|
| 230 |
if ((dev = fido_dev_new()) == NULL) |
| 231 |
return 1; |
| 232 |
|
| 233 |
if ((r = fido_dev_open(dev, path)) != FIDO_OK) { |
| 234 |
warnx("could not open %s: %s", path, fido_strerr(r)); |
| 235 |
goto quit; |
| 236 |
} |
| 237 |
|
| 238 |
r = run(dev, pin); |
| 239 |
quit: |
| 240 |
fido_dev_close(dev); |
| 241 |
fido_dev_free(&dev); |
| 242 |
return r != FIDO_OK; |
| 243 |
} |