xref: /netbsd-src/external/bsd/libfido2/dist/examples/setpin.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
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 /*
8  * Configure a PIN on a given authenticator.
9  */
10 
11 #include <openssl/ec.h>
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #include "../openbsd-compat/openbsd-compat.h"
19 
20 #include "fido.h"
21 
22 static void
23 setpin(const char *path, const char *pin, const char *oldpin)
24 {
25 	fido_dev_t *dev;
26 	int r;
27 
28 	fido_init(0);
29 
30 	if ((dev = fido_dev_new()) == NULL)
31 		errx(1, "fido_dev_new");
32 
33 	if ((r = fido_dev_open(dev, path)) != FIDO_OK)
34 		errx(1, "fido_dev_open: %s (0x%x)", fido_strerr(r), r);
35 
36 	if ((r = fido_dev_set_pin(dev, pin, oldpin)) != FIDO_OK)
37 		errx(1, "fido_setpin: %s (0x%x)", fido_strerr(r), r);
38 
39 	if ((r = fido_dev_close(dev)) != FIDO_OK)
40 		errx(1, "fido_dev_close: %s (0x%x)", fido_strerr(r), r);
41 
42 	fido_dev_free(&dev);
43 }
44 
45 int
46 main(int argc, char **argv)
47 {
48 	if (argc < 3 || argc > 4) {
49 		fprintf(stderr, "usage: setpin <pin> [oldpin] <device>\n");
50 		exit(EXIT_FAILURE);
51 	}
52 
53 	if (argc == 3)
54 		setpin(argv[2], argv[1], NULL);
55 	else
56 		setpin(argv[3], argv[1], argv[2]);
57 
58 	exit(0);
59 }
60