summaryrefslogtreecommitdiff
path: root/tools/pin.c
blob: eab178a475aa6762d24df52e4709ee42285a2a0e (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
/*
 * Copyright (c) 2018 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 <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"

int
pin_set(char *path)
{
	fido_dev_t *dev = NULL;
	char prompt[1024];
	char pin1[1024];
	char pin2[1024];
	int r;
	int status = 1;

	if (path == NULL)
		usage();

	dev = open_dev(path);

	r = snprintf(prompt, sizeof(prompt), "Enter new PIN for %s: ", path);
	if (r < 0 || (size_t)r >= sizeof(prompt)) {
		warnx("snprintf");
		goto out;
	}

	if (!readpassphrase(prompt, pin1, sizeof(pin1), RPP_ECHO_OFF)) {
		warnx("readpassphrase");
		goto out;
	}

	r = snprintf(prompt, sizeof(prompt), "Enter the same PIN again: ");
	if (r < 0 || (size_t)r >= sizeof(prompt)) {
		warnx("snprintf");
		goto out;
	}

	if (!readpassphrase(prompt, pin2, sizeof(pin2), RPP_ECHO_OFF)) {
		warnx("readpassphrase");
		goto out;
	}

	if (strcmp(pin1, pin2) != 0) {
		fprintf(stderr, "PINs do not match. Try again.\n");
		goto out;
	}

	if ((r = fido_dev_set_pin(dev, pin1, NULL)) != FIDO_OK) {
		warnx("fido_dev_set_pin: %s", fido_strerr(r));
		goto out;
	}

	fido_dev_close(dev);
	fido_dev_free(&dev);

	status = 0;
out:
	explicit_bzero(pin1, sizeof(pin1));
	explicit_bzero(pin2, sizeof(pin2));

	exit(status);
}

int
pin_change(char *path)
{
	fido_dev_t *dev = NULL;
	char prompt[1024];
	char pin0[1024];
	char pin1[1024];
	char pin2[1024];
	int r;
	int status = 1;

	if (path == NULL)
		usage();

	dev = open_dev(path);

	r = snprintf(prompt, sizeof(prompt), "Enter current PIN for %s: ", path);
	if (r < 0 || (size_t)r >= sizeof(prompt)) {
		warnx("snprintf");
		goto out;
	}

	if (!readpassphrase(prompt, pin0, sizeof(pin0), RPP_ECHO_OFF)) {
		warnx("readpassphrase");
		goto out;
	}

	r = snprintf(prompt, sizeof(prompt), "Enter new PIN for %s: ", path);
	if (r < 0 || (size_t)r >= sizeof(prompt)) {
		warnx("snprintf");
		goto out;
	}

	if (!readpassphrase(prompt, pin1, sizeof(pin1), RPP_ECHO_OFF)) {
		warnx("readpassphrase");
		goto out;
	}

	r = snprintf(prompt, sizeof(prompt), "Enter the same PIN again: ");
	if (r < 0 || (size_t)r >= sizeof(prompt)) {
		warnx("snprintf");
		goto out;
	}

	if (!readpassphrase(prompt, pin2, sizeof(pin2), RPP_ECHO_OFF)) {
		warnx("readpassphrase");
		goto out;
	}

	if (strcmp(pin1, pin2) != 0) {
		fprintf(stderr, "PINs do not match. Try again.\n");
		goto out;
	}

	if ((r = fido_dev_set_pin(dev, pin1, pin0)) != FIDO_OK) {
		warnx("fido_dev_set_pin: %s", fido_strerr(r));
		goto out;
	}

	fido_dev_close(dev);
	fido_dev_free(&dev);

	status = 0;
out:
	explicit_bzero(pin0, sizeof(pin0));
	explicit_bzero(pin1, sizeof(pin1));
	explicit_bzero(pin2, sizeof(pin2));

	exit(status);
}