xref: /freebsd/contrib/libfido2/tools/fido2-token.c (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
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 -Sm rp_id device\n"
32 "       fido2-token -V\n"
33 	);
34 
35 	exit(1);
36 }
37 
38 static void
39 setaction(int ch)
40 {
41 	if (action)
42 		usage();
43 	action = ch;
44 }
45 
46 int
47 main(int argc, char **argv)
48 {
49 	int ch;
50 	int flags = 0;
51 	char *device;
52 
53 	while ((ch = getopt(argc, argv, TOKEN_OPT)) != -1) {
54 		switch (ch) {
55 		case 'a':
56 		case 'b':
57 		case 'c':
58 		case 'e':
59 		case 'f':
60 		case 'i':
61 		case 'k':
62 		case 'l':
63 		case 'm':
64 		case 'n':
65 		case 'p':
66 		case 'r':
67 		case 'u':
68 			break; /* ignore */
69 		case 'd':
70 			flags = FIDO_DEBUG;
71 			break;
72 		default:
73 			setaction(ch);
74 			break;
75 		}
76 	}
77 
78 	if (argc - optind < 1)
79 		device = NULL;
80 	else
81 		device = argv[argc - 1];
82 
83 	fido_init(flags);
84 
85 	switch (action) {
86 	case 'C':
87 		return (pin_change(device));
88 	case 'D':
89 		return (token_delete(argc, argv, device));
90 	case 'G':
91 		return (token_get(argc, argv, device));
92 	case 'I':
93 		return (token_info(argc, argv, device));
94 	case 'L':
95 		return (token_list(argc, argv, device));
96 	case 'R':
97 		return (token_reset(device));
98 	case 'S':
99 		return (token_set(argc, argv, device));
100 	case 'V':
101 		fprintf(stderr, "%d.%d.%d\n", _FIDO_MAJOR, _FIDO_MINOR,
102 		    _FIDO_PATCH);
103 		exit(0);
104 	}
105 
106 	usage();
107 
108 	/* NOTREACHED */
109 }
110