1 /* -*- mode:C; c-file-style: "bsd" -*- */
3 * Copyright (c) 2012-2013 Yubico AB.
6 * Some basic code copied from ykchalresp.c.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <ykpers-version.h>
44 "Usage: ykinfo [options]\n"
48 "\t-s Get serial in decimal from YubiKey\n"
49 "\t-m Get serial in modhex from YubiKey\n"
50 "\t-H Get serial in hex from YubiKey\n"
51 "\t-v Get version from YubiKey\n"
52 "\t-t Get touchlevel from YubiKey\n"
53 "\t-1 Check if slot 1 is programmed\n"
54 "\t-2 Check if slot 2 is programmed\n"
55 "\t-p Get programming sequence from YubiKey\n"
56 "\t-a Get all information above\n"
58 "\t-q Only output information from YubiKey\n"
60 "\t-V Get the tool version\n"
61 "\t-h help (this text)\n"
65 const char *optstring = "asmHvtpqhV12";
67 static void report_yk_error(void)
70 if (yk_errno == YK_EUSBERR) {
71 fprintf(stderr, "USB error: %s\n",
74 fprintf(stderr, "Yubikey core error: %s\n",
75 yk_strerror(yk_errno));
80 static int parse_args(int argc, char **argv,
81 bool *serial_dec, bool *serial_modhex, bool *serial_hex,
82 bool *version, bool *touch_level, bool *pgm_seq, bool *quiet,
83 bool *slot1, bool *slot2,
88 while((c = getopt(argc, argv, optstring)) != -1) {
92 *serial_modhex = true;
104 *serial_modhex = true;
128 fputs(YKPERS_VERSION_STRING "\n", stderr);
133 fputs(usage, stderr);
139 if (!*serial_dec && !*serial_modhex && !*serial_hex &&
140 !*version && !*touch_level && !*pgm_seq && !*slot1 && !*slot2) {
141 /* no options at all */
142 fputs("You must give at least one option.\n", stderr);
143 fputs(usage, stderr);
151 int main(int argc, char **argv)
158 bool serial_dec = false;
159 bool serial_modhex = false;
160 bool serial_hex = false;
161 bool version = false;
162 bool touch_level = false;
163 bool pgm_seq = false;
171 if (! parse_args(argc, argv,
172 &serial_dec, &serial_modhex, &serial_hex,
173 &version, &touch_level, &pgm_seq, &quiet,
183 if (!(yk = yk_open_first_key())) {
188 if(serial_dec || serial_modhex || serial_hex) {
190 int ret = yk_get_serial(yk, 1, 0, &serial);
198 printf("%d\n", serial);
202 printf("serial_hex: ");
203 printf("%x\n", serial);
208 char modhex_serial[64];
210 snprintf(buf, 64, "%x", serial);
211 yubikey_hex_decode(hex_serial, buf, strlen(buf));
212 yubikey_modhex_encode(modhex_serial, hex_serial, strlen(hex_serial));
214 printf("serial_modhex: ");
215 printf("%s\n", modhex_serial);
218 if(version || touch_level || pgm_seq || slot1 || slot2) {
219 YK_STATUS *st = ykds_alloc();
220 if(!yk_get_status(yk, st)) {
229 printf("%d.%d.%d\n", ykds_version_major(st), ykds_version_minor(st), ykds_version_build(st));
233 printf("touch_level: ");
234 printf("%d\n", ykds_touch_level(st));
238 printf("programming_sequence: ");
239 printf("%d\n", ykds_pgm_seq(st));
243 printf("slot1_status: ");
244 printf("%d\n", (ykds_touch_level(st) & CONFIG1_VALID) == CONFIG1_VALID);
248 printf("slot2_status: ");
249 printf("%d\n", (ykds_touch_level(st) & CONFIG2_VALID) == CONFIG2_VALID);
258 if (error || exit_code != 0) {
262 if (yk && !yk_close_key(yk)) {