1 /* -*- mode:C; c-file-style: "bsd" -*- */
3 * Copyright (c) 2008-2012 Yubico AB
4 * Copyright (c) 2010 Tollef Fog Heen <tfheen@err.no>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "ykpers-args.h"
44 static int reader(char *buf, size_t count, void *stream)
46 return (int)fread(buf, 1, count, (FILE *)stream);
48 static int writer(const char *buf, size_t count, void *stream)
50 return (int)fwrite(buf, 1, count, (FILE *)stream);
53 int main(int argc, char **argv)
55 FILE *inf = NULL; const char *infname = NULL;
56 FILE *outf = NULL; const char *outfname = NULL;
58 bool aesviahash = false;
59 bool use_access_code = false;
60 unsigned char access_code[256];
62 YKP_CONFIG *cfg = ykp_create_config();
63 YK_STATUS *st = ykds_alloc();
64 bool autocommit = false;
75 /* Assume the worst */
83 if (argc == 2 && strcmp (argv[1], "-h") == 0) {
88 if (!(yk = yk_open_first_key())) {
93 if (!yk_get_status(yk, st)) {
98 printf("Firmware version %d.%d.%d Touch level %d ",
99 ykds_version_major(st),
100 ykds_version_minor(st),
101 ykds_version_build(st),
102 ykds_touch_level(st));
103 if (ykds_pgm_seq(st))
104 printf("Program sequence %d\n",
107 printf("Unconfigured\n");
109 if (!(yk_check_firmware_version(yk))) {
110 if (yk_errno == YK_EFIRMWARE) {
111 printf("Unsupported firmware revision - some "
112 "features may not be available\n"
114 "http://code.google.com/p/yubikey-personalization/wiki/Compatibility\n"
115 "for more information.\n");
121 if (!ykp_configure_for(cfg, 1, st))
124 /* Parse all arguments in a testable way */
125 if (! args_to_config(argc, argv, cfg, yk,
129 access_code, &use_access_code,
135 if (verbose && (ykds_version_major(st) > 2 ||
136 (ykds_version_major(st) == 2 &&
137 ykds_version_minor(st) >= 2))) {
139 if (! yk_get_serial(yk, 0, 0, &serial)) {
140 printf ("Failed to read serial number (serial-api-visible disabled?).\n");
143 printf ("Serial number : %i\n", serial);
150 if (strcmp(infname, "-") == 0)
153 inf = fopen(infname, "r");
156 "Couldn't open %s for reading: %s\n",
165 if (strcmp(outfname, "-") == 0)
168 outf = fopen(outfname, "w");
171 "Couldn't open %s for writing: %s\n",
179 if (!ykp_read_config(cfg, reader, inf))
181 } else if (! aesviahash) {
182 char passphrasebuf[256]; size_t passphraselen;
183 fprintf(stderr, "Passphrase to create AES key: ");
185 fgets(passphrasebuf, sizeof(passphrasebuf), stdin);
186 passphraselen = strlen(passphrasebuf);
187 if (passphrasebuf[passphraselen - 1] == '\n')
188 passphrasebuf[passphraselen - 1] = '\0';
189 if (!ykp_AES_key_from_passphrase(cfg,
190 passphrasebuf, salt))
195 if (!ykp_write_config(cfg, writer, outf))
198 char commitbuf[256]; size_t commitlen;
200 fprintf(stderr, "Configuration data to be written to key configuration %d:\n\n", ykp_config_num(cfg));
201 ykp_write_config(cfg, writer, stderr);
202 fprintf(stderr, "\nCommit? (y/n) [n]: ");
204 strcpy(commitbuf, "yes");
207 fgets(commitbuf, sizeof(commitbuf), stdin);
209 commitlen = strlen(commitbuf);
210 if (commitbuf[commitlen - 1] == '\n')
211 commitbuf[commitlen - 1] = '\0';
212 if (strcmp(commitbuf, "y") == 0
213 || strcmp(commitbuf, "yes") == 0) {
217 printf("Attempting to write configuration to the yubikey...");
218 if (!yk_write_config(yk,
219 ykp_core_config(cfg), ykp_config_num(cfg),
220 use_access_code ? access_code : NULL)) {
222 printf(" failure\n");
227 printf(" success\n");
248 if (yk && !yk_close_key(yk)) {
259 ykp_free_config(cfg);