-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
151 lines (137 loc) · 4.94 KB
/
main.c
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
/*
* Copyright (c) 2023 Nitrokey GmbH
*
* This file is part of Nitrokey HOTP verification project.
*
* Nitrokey HOTP verification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Nitrokey HOTP verification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nitrokey HOTP verification. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "ccid.h"
#include "operations.h"
#include "return_codes.h"
#include "utils.h"
#include "version.h"
#include <stdio.h>
#include <string.h>
static struct Device dev = {};
int parse_cmd_and_run(int argc, char *const *argv);
void print_help(char *app_name) {
printf("Available commands: \n"
"\t%s id\n"
"\t%s info\n"
"\t%s version\n"
"\t%s check <HOTP CODE>\n"
"\t%s regenerate <ADMIN PIN>\n"
"\t%s set <BASE32 HOTP SECRET> <ADMIN PIN> [COUNTER]\n",
app_name, app_name, app_name, app_name, app_name, app_name);
}
int main(int argc, char *argv[]) {
printf("HOTP code verification application, version %s\n", VERSION);
int res;
if (argc != 1 && argv[1][0] != 'v') {
res = device_connect(&dev);
if (res != RET_NO_ERROR) {
printf("Could not connect to the device\n");
return EXIT_CONNECTION_ERROR;
}
}
res = parse_cmd_and_run(argc, argv);
if (res != dev_ok && res != RET_NO_ERROR && res != RET_VALIDATION_PASSED && res != RET_VALIDATION_FAILED) {
printf("Error occurred, status code %d: %s\n", res, res_to_error_string(res));
} else {
printf("%s\n", res_to_error_string(res));
}
#ifdef _DEBUG
if (res < dev_command_status_range && res != dev_ok) {
printf("Device error: %s\n", command_status_to_string((uint8_t) res));
}
#endif
device_disconnect(&dev);
res = res_to_exit_code(res);
return res;
}
void print_card_serial(struct ResponseStatus *status) {
if ((*status).card_serial_u32 != 0) {
printf("0x%X\n", (*status).card_serial_u32);
} else {
printf("N/A\n");
}
}
int parse_cmd_and_run(int argc, char *const *argv) {
int res = RET_INVALID_PARAMS;
if (argc > 1) {
switch (argv[1][0]) {
case 'v':
printf("%s\n", VERSION);
printf("%s\n", VERSION_GIT);
res = RET_NO_ERROR;
break;
case 'i': {// id | info
struct ResponseStatus status;
res = device_get_status(&dev, &status);
check_ret((res != RET_NO_ERROR) && (res != RET_NO_PIN_ATTEMPTS), res);
if (strnlen(argv[1], 10) == 2 && argv[1][1] == 'd') {
// id command - print ID only
print_card_serial(&status);
} else {
// info command - print status
printf("Connected device status:\n");
printf("\tCard serial: ");
print_card_serial(&status);
printf("\tFirmware: v%d.%d\n",
status.firmware_version_st.major,
status.firmware_version_st.minor);
if (res != RET_NO_PIN_ATTEMPTS) {
printf("\tCard counters: Admin %d, User %d\n",
status.retry_admin, status.retry_user);
} else {
printf("\tCard counters: PIN is not set - set PIN before the first use\n");
}
}
if (res == RET_NO_PIN_ATTEMPTS) {
// Ignore if PIN is not set here
res = RET_NO_ERROR;
}
} break;
case 'c':
if (argc != 3) break;
res = check_code_on_device(&dev, argv[2]);
break;
case 's':
if (argc != 4 && argc != 5) break;
{
uint64_t counter = 0;
if (argc == 5) {
counter = strtol10_s(argv[4]);
}
res = set_secret_on_device(&dev, argv[2], argv[3], counter);
}
break;
case 'r':
if (argc != 3) break;
res = regenerate_AES_key(&dev, argv[2]);
break;
default:
break;
}
}
if (argc == 1) {
print_help(argv[0]);
res = RET_NO_ERROR;
} else if (res == RET_INVALID_PARAMS) {
print_help(argv[0]);
}
return res;
}