summaryrefslogtreecommitdiff
path: root/tools/bio.c
blob: b8f9b386e3ad9f0b149afd853b9c95d92985461c (plain)
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
/*
 * Copyright (c) 2019 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 */

#include <fido.h>
#include <fido/bio.h>

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "../openbsd-compat/openbsd-compat.h"
#include "extern.h"

static void
print_template(const fido_bio_template_array_t *ta, size_t idx)
{
	char				*id = NULL;
	const fido_bio_template_t	*t = NULL;

	if ((t = fido_bio_template(ta, idx)) == NULL)
		errx(1, "fido_bio_template");

	if (base64_encode(fido_bio_template_id_ptr(t),
	    fido_bio_template_id_len(t), &id) < 0)
		errx(1, "output error");

	printf("%02u: %s %s\n", (unsigned)idx, id, fido_bio_template_name(t));

	free(id);
}

int
bio_list(char *path)
{
	char				 pin[1024];
	fido_bio_template_array_t	*ta = NULL;
	fido_dev_t			*dev = NULL;
	int				 r;

	if (path == NULL)
		usage();
	if ((ta = fido_bio_template_array_new()) == NULL)
		errx(1, "fido_bio_template_array_new");

	dev = open_dev(path);
	read_pin(path, pin, sizeof(pin));
	r = fido_bio_dev_get_template_array(dev, ta, pin);
	explicit_bzero(pin, sizeof(pin));

	if (r != FIDO_OK)
		errx(1, "fido_bio_dev_get_template_array: %s", fido_strerr(r));
	for (size_t i = 0; i < fido_bio_template_array_count(ta); i++)
		print_template(ta, i);

	fido_bio_template_array_free(&ta);
	fido_dev_close(dev);
	fido_dev_free(&dev);

	exit(0);
}

int
bio_set_name(char *path, char *id, char *name)
{
	char			 pin[1024];
	fido_bio_template_t	*t = NULL;
	fido_dev_t		*dev = NULL;
	int			 r;
	size_t			 id_blob_len = 0;
	void			*id_blob_ptr = NULL;

	if (path == NULL)
		usage();
	if ((t = fido_bio_template_new()) == NULL)
		errx(1, "fido_bio_template_new");

	if (base64_decode(id, &id_blob_ptr, &id_blob_len) < 0)
		errx(1, "base64_decode");

	if ((r = fido_bio_template_set_name(t, name)) != FIDO_OK)
		errx(1, "fido_bio_template_set_name: %s", fido_strerr(r));
	if ((r = fido_bio_template_set_id(t, id_blob_ptr,
	    id_blob_len)) != FIDO_OK)
		errx(1, "fido_bio_template_set_id: %s", fido_strerr(r));

	dev = open_dev(path);
	read_pin(path, pin, sizeof(pin));
	r = fido_bio_dev_set_template_name(dev, t, pin);
	explicit_bzero(pin, sizeof(pin));

	if (r != FIDO_OK)
		errx(1, "fido_bio_dev_set_template_name: %s", fido_strerr(r));

	free(id_blob_ptr);
	fido_bio_template_free(&t);
	fido_dev_close(dev);
	fido_dev_free(&dev);

	exit(0);
}

static const char *
plural(uint8_t n)
{
	if (n == 1)
		return "";
	return "s";
}

static const char *
enroll_strerr(uint8_t n)
{
	switch (n) {
	case FIDO_BIO_ENROLL_FP_GOOD:
		return "Sample ok";
	case FIDO_BIO_ENROLL_FP_TOO_HIGH:
		return "Sample too high";
	case FIDO_BIO_ENROLL_FP_TOO_LOW:
		return "Sample too low";
	case FIDO_BIO_ENROLL_FP_TOO_LEFT:
		return "Sample too left";
	case FIDO_BIO_ENROLL_FP_TOO_RIGHT:
		return "Sample too right";
	case FIDO_BIO_ENROLL_FP_TOO_FAST:
		return "Sample too fast";
	case FIDO_BIO_ENROLL_FP_TOO_SLOW:
		return "Sample too slow";
	case FIDO_BIO_ENROLL_FP_POOR_QUALITY:
		return "Poor quality sample";
	case FIDO_BIO_ENROLL_FP_TOO_SKEWED:
		return "Sample too skewed";
	case FIDO_BIO_ENROLL_FP_TOO_SHORT:
		return "Sample too short";
	case FIDO_BIO_ENROLL_FP_MERGE_FAILURE:
		return "Sample merge failure";
	case FIDO_BIO_ENROLL_FP_EXISTS:
		return "Sample exists";
	case FIDO_BIO_ENROLL_FP_DATABASE_FULL:
		return "Fingerprint database full";
	case FIDO_BIO_ENROLL_NO_USER_ACTIVITY:
		return "No user activity";
	case FIDO_BIO_ENROLL_NO_USER_PRESENCE_TRANSITION:
		return "No user presence transition";
	default:
		return "Unknown error";
	}
}

int
bio_enroll(char *path)
{
	char			 pin[1024];
	fido_bio_enroll_t	*e = NULL;
	fido_bio_template_t	*t = NULL;
	fido_dev_t		*dev = NULL;
	int			 r;

	if (path == NULL)
		usage();
	if ((t = fido_bio_template_new()) == NULL)
		errx(1, "fido_bio_template_new");
	if ((e = fido_bio_enroll_new()) == NULL)
		errx(1, "fido_bio_enroll_new");

	dev = open_dev(path);
	read_pin(path, pin, sizeof(pin));

	printf("Touch your security key.\n");

	r = fido_bio_dev_enroll_begin(dev, t, e, 10000, pin);
	explicit_bzero(pin, sizeof(pin));
	if (r != FIDO_OK)
		errx(1, "fido_bio_dev_enroll_begin: %s", fido_strerr(r));

	printf("%s.\n", enroll_strerr(fido_bio_enroll_last_status(e)));

	while (fido_bio_enroll_remaining_samples(e) > 0) {
		printf("Touch your security key (%u sample%s left).\n",
		    (unsigned)fido_bio_enroll_remaining_samples(e),
		    plural(fido_bio_enroll_remaining_samples(e)));
		if ((r = fido_bio_dev_enroll_continue(dev, t, e,
		    10000)) != FIDO_OK) {
			errx(1, "fido_bio_dev_enroll_continue: %s",
			    fido_strerr(r));
		}
		printf("%s.\n", enroll_strerr(fido_bio_enroll_last_status(e)));
	}

	fido_bio_template_free(&t);
	fido_bio_enroll_free(&e);
	fido_dev_close(dev);
	fido_dev_free(&dev);

	exit(0);
}

int
bio_delete(fido_dev_t *dev, char *path, char *id)
{
	char			 pin[1024];
	fido_bio_template_t	*t = NULL;
	int			 r;
	size_t			 id_blob_len = 0;
	void			*id_blob_ptr = NULL;

	if (path == NULL)
		usage();
	if ((t = fido_bio_template_new()) == NULL)
		errx(1, "fido_bio_template_new");

	if (base64_decode(id, &id_blob_ptr, &id_blob_len) < 0)
		errx(1, "base64_decode");
	if ((r = fido_bio_template_set_id(t, id_blob_ptr,
	    id_blob_len)) != FIDO_OK)
		errx(1, "fido_bio_template_set_id: %s", fido_strerr(r));

	read_pin(path, pin, sizeof(pin));
	r = fido_bio_dev_enroll_remove(dev, t, pin);
	explicit_bzero(pin, sizeof(pin));

	if (r != FIDO_OK)
		errx(1, "fido_bio_dev_enroll_remove: %s", fido_strerr(r));

	free(id_blob_ptr);
	fido_bio_template_free(&t);
	fido_dev_close(dev);
	fido_dev_free(&dev);

	exit(0);
}

static const char *
type_str(uint8_t t)
{
	switch (t) {
	case 1:
		return "touch";
	case 2:
		return "swipe";
	default:
		return "unknown";
	}
}

void
bio_info(fido_dev_t *dev)
{
	fido_bio_info_t	*i = NULL;
	int		 r;

	if ((i = fido_bio_info_new()) == NULL)
		errx(1, "fido_bio_info_new");
	if ((r = fido_bio_dev_get_info(dev, i)) != FIDO_OK) {
		fido_bio_info_free(&i);
		return;
	}

	printf("sensor type: %u (%s)\n", (unsigned)fido_bio_info_type(i),
	    type_str(fido_bio_info_type(i)));
	printf("max samples: %u\n", (unsigned)fido_bio_info_max_samples(i));

	fido_bio_info_free(&i);
}