xref: /freebsd/contrib/libfido2/tools/fido2-token.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 /*
2  * Copyright (c) 2018 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  */
6 
7 #include <fido.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 #include "../openbsd-compat/openbsd-compat.h"
12 #include "extern.h"
13 
14 static int action;
15 
16 void
17 usage(void)
18 {
19 	fprintf(stderr,
20 "usage: fido2-token -C [-d] device\n"
21 "       fido2-token -Db [-k key_path] [-i cred_id -n rp_id] device\n"
22 "       fido2-token -Dei template_id device\n"
23 "       fido2-token -Du device\n"
24 "       fido2-token -Gb [-k key_path] [-i cred_id -n rp_id] blob_path device\n"
25 "       fido2-token -I [-cd] [-k rp_id -i cred_id]  device\n"
26 "       fido2-token -L [-bder] [-k rp_id] [device]\n"
27 "       fido2-token -R [-d] device\n"
28 "       fido2-token -S [-adefu] [-l pin_length] [-i template_id -n template_name] device\n"
29 "       fido2-token -Sb [-k key_path] [-i cred_id -n rp_id] blob_path device\n"
30 "       fido2-token -Sc -i cred_id -k user_id -n name -p display_name device\n"
31 "       fido2-token -V\n"
32 	);
33 
34 	exit(1);
35 }
36 
37 static void
38 setaction(int ch)
39 {
40 	if (action)
41 		usage();
42 	action = ch;
43 }
44 
45 int
46 main(int argc, char **argv)
47 {
48 	int ch;
49 	int flags = 0;
50 	char *device;
51 
52 	while ((ch = getopt(argc, argv, TOKEN_OPT)) != -1) {
53 		switch (ch) {
54 		case 'a':
55 		case 'b':
56 		case 'c':
57 		case 'e':
58 		case 'f':
59 		case 'i':
60 		case 'k':
61 		case 'l':
62 		case 'n':
63 		case 'p':
64 		case 'r':
65 		case 'u':
66 			break; /* ignore */
67 		case 'd':
68 			flags = FIDO_DEBUG;
69 			break;
70 		default:
71 			setaction(ch);
72 			break;
73 		}
74 	}
75 
76 	if (argc - optind < 1)
77 		device = NULL;
78 	else
79 		device = argv[argc - 1];
80 
81 	fido_init(flags);
82 
83 	switch (action) {
84 	case 'C':
85 		return (pin_change(device));
86 	case 'D':
87 		return (token_delete(argc, argv, device));
88 	case 'G':
89 		return (token_get(argc, argv, device));
90 	case 'I':
91 		return (token_info(argc, argv, device));
92 	case 'L':
93 		return (token_list(argc, argv, device));
94 	case 'R':
95 		return (token_reset(device));
96 	case 'S':
97 		return (token_set(argc, argv, device));
98 	case 'V':
99 		fprintf(stderr, "%d.%d.%d\n", _FIDO_MAJOR, _FIDO_MINOR,
100 		    _FIDO_PATCH);
101 		exit(0);
102 	}
103 
104 	usage();
105 
106 	/* NOTREACHED */
107 }
108