| 1 | /* |
| 2 | * valley-piv-signer: one PIV session on a YubiKey for a run of approval |
| 3 | * signatures. The approval command (approval/piv.go) starts it, sends it |
| 4 | * statements one at a time, and reads back raw Ed25519 signatures. The |
| 5 | * helper holds the YubiKey for the whole run, so the PIN is asked for once |
| 6 | * and the key's touch policy asks for a touch on every signature. |
| 7 | * |
| 8 | * valley-piv-signer [--slot HEX] [--reader NAME] |
| 9 | * |
| 10 | * The helper first writes one line, the key it signs with: |
| 11 | * |
| 12 | * key <serial> <hex of the slot's attestation certificate, DER> |
| 13 | * |
| 14 | * Then, for each request on stdin, |
| 15 | * |
| 16 | * sign <length> <session|each|none>\n<length bytes of statement text> |
| 17 | * |
| 18 | * it writes "touch" just before the device signs, then the signature: |
| 19 | * |
| 20 | * signature <hex of the 64-byte Ed25519 signature> |
| 21 | * |
| 22 | * A refusal is one line in place of either reply, and the helper reads no |
| 23 | * request after it: |
| 24 | * |
| 25 | * error <device|pin|pin-format|cancelled|touch|request> <detail> |
| 26 | * |
| 27 | * The YubiKey is the helper's alone for the session: it connects in |
| 28 | * exclusive mode, so no other program can send the card a command while |
| 29 | * the PIN is verified. libykpiv answers a reset or a removed card by |
| 30 | * reconnecting (_ykpiv_begin_transaction), in shared mode upstream and in |
| 31 | * exclusive mode in the copy packaged with the helper (nix/packages.nix). |
| 32 | * Before each call into libykpiv the helper asks pcscd whether the card |
| 33 | * was reset or removed, and ends the session if it was, so no session |
| 34 | * carries on with a card that changed. A new session asks for the PIN |
| 35 | * again. |
| 36 | * |
| 37 | * Every exit resets the YubiKey, which forgets the verified PIN. pcscd |
| 38 | * leaves the card as it is when a client dies, so a signal does not kill |
| 39 | * the helper: every signal that would end it (SIGINT, SIGTERM, SIGHUP, |
| 40 | * SIGQUIT, and the ones nothing here uses, such as SIGUSR1 or SIGALRM) |
| 41 | * ends the session, which disconnects with a reset, and the helper then |
| 42 | * exits by the same signal. Those signals are held while the helper |
| 43 | * decides whether to wait for input and let through only inside the wait, |
| 44 | * so none is lost between the decision and the wait. SIGPIPE is ignored, |
| 45 | * and a reply that cannot be written ends the session. Fault signals |
| 46 | * (SIGSEGV, SIGBUS, SIGFPE, SIGILL, SIGABRT) keep their default: a |
| 47 | * process that faulted cannot reset the card reliably. SIGKILL cannot be |
| 48 | * caught. Both leave the PIN verified until the YubiKey is reset or |
| 49 | * removed. |
| 50 | * |
| 51 | * The PIN is read from the controlling terminal with echo off, never from |
| 52 | * the arguments, the environment or stdin, and is never written anywhere |
| 53 | * but to the device. The process cannot be traced or dump core. It is |
| 54 | * asked for whenever the device says the PIN is not verified, which in one |
| 55 | * session is at the first signature. "each" asks for it before every |
| 56 | * signature, for a key whose PIN policy is always, and "none" never asks, |
| 57 | * for a key whose PIN policy is never. A PIN the device refuses is |
| 58 | * reported, with the tries left, and never sent again. The PIN is verified |
| 59 | * with a raw VERIFY so that libykpiv keeps no copy of it. |
| 60 | */ |
| 61 | #define _GNU_SOURCE |
| 62 | |
| 63 | #include <errno.h> |
| 64 | #include <fcntl.h> |
| 65 | #include <poll.h> |
| 66 | #include <signal.h> |
| 67 | #include <stdarg.h> |
| 68 | #include <stdint.h> |
| 69 | #include <stdio.h> |
| 70 | #include <stdlib.h> |
| 71 | #include <string.h> |
| 72 | #include <strings.h> |
| 73 | #include <sys/prctl.h> |
| 74 | #include <termios.h> |
| 75 | #include <unistd.h> |
| 76 | |
| 77 | #include <winscard.h> |
| 78 | #include <ykpiv/ykpiv.h> |
| 79 | |
| 80 | /* The largest statement the helper signs: approval.MaxStatement. */ |
| 81 | #define MAX_STATEMENT 1024 |
| 82 | #define PIN_MIN 6 |
| 83 | #define PIN_MAX 8 |
| 84 | |
| 85 | static struct termios saved; |
| 86 | /* The signal that ends the session, 0 while none has. */ |
| 87 | static volatile sig_atomic_t stopped; |
| 88 | /* The signals that end the session. */ |
| 89 | static sigset_t ending; |
| 90 | /* Set when a reply could not be written: the reader is gone. */ |
| 91 | static int broken; |
| 92 | /* The errno of a failed wait or read on stdin or the terminal, 0 while |
| 93 | * none has failed. An I/O error ends the session like a refusal, apart |
| 94 | * from the end of input, which ends it clean. */ |
| 95 | static int failed; |
| 96 | |
| 97 | static SCARDCONTEXT context; |
| 98 | static SCARDHANDLE card; |
| 99 | static int established, connected; |
| 100 | /* Set once libykpiv holds the card and context. From then on they are |
| 101 | * closed through it: when SCardBeginTransaction fails inside a call, |
| 102 | * _ykpiv_begin_transaction may reset and disconnect the card, release |
| 103 | * the context and open replacements, which only libykpiv's state names. */ |
| 104 | static int handed; |
| 105 | |
| 106 | static void restore_tty(int fd) { |
| 107 | while (tcsetattr(fd, TCSAFLUSH, &saved) != 0 && errno == EINTR) { |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* A signal ends the session in the main loop, which restores the terminal |
| 112 | * and resets the YubiKey. */ |
| 113 | static void on_signal(int sig) { stopped = sig; } |
| 114 | |
| 115 | /* Exits by the signal that ended the session, once the YubiKey is reset. |
| 116 | * The ending signals are blocked while stopped is read, so a second one |
| 117 | * cannot change it between restoring the default and raising; the raised |
| 118 | * signal is delivered, to its default, when they are unblocked. Returns |
| 119 | * when no signal ended the session. */ |
| 120 | static void exit_by_signal(void) { |
| 121 | sigprocmask(SIG_BLOCK, &ending, NULL); |
| 122 | int sig = stopped; |
| 123 | if (sig) { |
| 124 | signal(sig, SIG_DFL); |
| 125 | raise(sig); |
| 126 | } |
| 127 | sigprocmask(SIG_UNBLOCK, &ending, NULL); |
| 128 | } |
| 129 | |
| 130 | /* Waits until fd has input or a signal ends the session. The ending |
| 131 | * signals are blocked while stopped is checked and let through only inside |
| 132 | * ppoll, so one arriving between the check and the wait ends the wait. |
| 133 | * Returns 0 for input, -1 when the session ended or the wait failed. */ |
| 134 | static int wait_readable(int fd) { |
| 135 | sigset_t was; |
| 136 | sigprocmask(SIG_BLOCK, &ending, &was); |
| 137 | struct pollfd pfd = {fd, POLLIN, 0}; |
| 138 | int r = -1; |
| 139 | while (!stopped) { |
| 140 | r = ppoll(&pfd, 1, NULL, &was); |
| 141 | if (r > 0) { |
| 142 | break; |
| 143 | } |
| 144 | if (r < 0 && errno != EINTR) { |
| 145 | failed = errno; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | sigprocmask(SIG_SETMASK, &was, NULL); |
| 150 | return stopped || r <= 0 ? -1 : 0; |
| 151 | } |
| 152 | |
| 153 | /* Reads n bytes from fd, fewer at the end of input, when a signal ends |
| 154 | * the session, or when a read fails. */ |
| 155 | static size_t read_in(int fd, void *buf, size_t n) { |
| 156 | size_t got = 0; |
| 157 | while (got < n && wait_readable(fd) == 0) { |
| 158 | ssize_t r = read(fd, (char *)buf + got, n - got); |
| 159 | if (r < 0 && errno == EINTR) { |
| 160 | continue; |
| 161 | } |
| 162 | if (r < 0) { |
| 163 | failed = errno; |
| 164 | } |
| 165 | if (r <= 0) { |
| 166 | break; |
| 167 | } |
| 168 | got += (size_t)r; |
| 169 | } |
| 170 | return got; |
| 171 | } |
| 172 | |
| 173 | /* Reads a line of at most cap - 1 bytes into line, NUL-terminated, and |
| 174 | * returns its length with the newline, 0 at the end of input or when |
| 175 | * reading failed. A longer line is cut, and fails the request's parse. */ |
| 176 | static size_t read_line(int fd, char *line, size_t cap) { |
| 177 | size_t n = 0; |
| 178 | while (n < cap - 1 && read_in(fd, line + n, 1) == 1) { |
| 179 | if (line[n++] == '\n') { |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | line[n] = 0; |
| 184 | return n; |
| 185 | } |
| 186 | |
| 187 | static void reply(const char *fmt, ...) { |
| 188 | va_list ap; |
| 189 | va_start(ap, fmt); |
| 190 | vfprintf(stdout, fmt, ap); |
| 191 | va_end(ap); |
| 192 | fputc('\n', stdout); |
| 193 | if (fflush(stdout) != 0) { |
| 194 | broken = 1; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void reply_hex(const char *word, const char *prefix, const unsigned char *b, size_t n) { |
| 199 | fputs(word, stdout); |
| 200 | fputc(' ', stdout); |
| 201 | fputs(prefix, stdout); |
| 202 | for (size_t i = 0; i < n; i++) { |
| 203 | fprintf(stdout, "%02x", b[i]); |
| 204 | } |
| 205 | fputc('\n', stdout); |
| 206 | if (fflush(stdout) != 0) { |
| 207 | broken = 1; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* Connects to the first reader whose name holds wanted (any, when NULL) |
| 212 | * that takes an exclusive connection. Replies with the refusal when none |
| 213 | * does. */ |
| 214 | static int connect_exclusive(const char *wanted) { |
| 215 | LONG rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &context); |
| 216 | if (rc != SCARD_S_SUCCESS) { |
| 217 | reply("error device no YubiKey could be reached: %s", pcsc_stringify_error(rc)); |
| 218 | return -1; |
| 219 | } |
| 220 | established = 1; |
| 221 | char readers[2048]; |
| 222 | DWORD len = sizeof readers; |
| 223 | rc = SCardListReaders(context, NULL, readers, &len); |
| 224 | int busy = 0; |
| 225 | for (const char *r = readers; rc == SCARD_S_SUCCESS && *r; r += strlen(r) + 1) { |
| 226 | int holds = wanted == NULL; |
| 227 | for (const char *p = r; !holds && strlen(p) >= strlen(wanted); p++) { |
| 228 | holds = strncasecmp(p, wanted, strlen(wanted)) == 0; |
| 229 | } |
| 230 | DWORD protocol = 0; |
| 231 | if (holds) { |
| 232 | LONG c = SCardConnect(context, r, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, |
| 233 | &card, &protocol); |
| 234 | if (c == SCARD_S_SUCCESS) { |
| 235 | connected = 1; |
| 236 | return 0; |
| 237 | } |
| 238 | busy |= c == SCARD_E_SHARING_VIOLATION; |
| 239 | } |
| 240 | } |
| 241 | if (busy) { |
| 242 | reply("error device another program is using it"); |
| 243 | } else { |
| 244 | reply("error device no YubiKey could be reached: %s", |
| 245 | pcsc_stringify_error(rc == SCARD_S_SUCCESS ? SCARD_E_NO_SMARTCARD : rc)); |
| 246 | } |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | /* Says whether the card is the one connected to, replying with the |
| 251 | * refusal when it is not. pcscd flags a reset or a removal on the |
| 252 | * connection until it is reconnected, so the flag is seen here before |
| 253 | * libykpiv's next call would reconnect. */ |
| 254 | static int exclusive(void) { |
| 255 | LONG rc = SCardStatus(card, NULL, NULL, NULL, NULL, NULL, NULL); |
| 256 | if (rc == SCARD_S_SUCCESS) { |
| 257 | return 1; |
| 258 | } |
| 259 | if (rc == SCARD_W_RESET_CARD || rc == SCARD_W_REMOVED_CARD) { |
| 260 | reply("error device it was reset or removed during the session"); |
| 261 | } else { |
| 262 | reply("error device %s", pcsc_stringify_error(rc)); |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* Reads the PIN from the terminal. Returns its length, 0 when none was |
| 268 | * entered (an empty line, end of input, or a signal), -1 when there is no |
| 269 | * terminal, and -2 when what was entered is not 6 to 8 characters. */ |
| 270 | static int read_pin(char pin[PIN_MAX + 1], uint32_t serial, int slot) { |
| 271 | int fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC); |
| 272 | if (fd < 0) { |
| 273 | return -1; |
| 274 | } |
| 275 | if (tcgetattr(fd, &saved) != 0) { |
| 276 | close(fd); |
| 277 | return -1; |
| 278 | } |
| 279 | /* Stopping the helper while echo is off would leave the shell without |
| 280 | * it, so job control waits until the PIN is read. */ |
| 281 | const int held[] = {SIGTSTP, SIGTTIN, SIGTTOU}; |
| 282 | struct sigaction ignore, before[3]; |
| 283 | memset(&ignore, 0, sizeof ignore); |
| 284 | ignore.sa_handler = SIG_IGN; |
| 285 | for (int i = 0; i < 3; i++) { |
| 286 | sigaction(held[i], &ignore, &before[i]); |
| 287 | } |
| 288 | struct termios quiet = saved; |
| 289 | quiet.c_lflag &= ~(tcflag_t)ECHO; |
| 290 | char prompt[80]; |
| 291 | int n = snprintf(prompt, sizeof prompt, "PIN for YubiKey %u, slot %02x: ", serial, slot); |
| 292 | size_t len = 0; |
| 293 | int over = 0, ended = 0; |
| 294 | if (tcsetattr(fd, TCSAFLUSH, &quiet) == 0 && write(fd, prompt, (size_t)n) == n) { |
| 295 | char c = 0; |
| 296 | while (wait_readable(fd) == 0) { |
| 297 | ssize_t r = read(fd, &c, 1); |
| 298 | if (r < 0 && errno == EINTR) { |
| 299 | continue; |
| 300 | } |
| 301 | if (r < 0) { |
| 302 | failed = errno; |
| 303 | } |
| 304 | if (r <= 0) { |
| 305 | break; |
| 306 | } |
| 307 | if (c == '\n' || c == '\r') { |
| 308 | ended = 1; |
| 309 | break; |
| 310 | } |
| 311 | if (len < PIN_MAX) { |
| 312 | pin[len++] = c; |
| 313 | } else { |
| 314 | over = 1; |
| 315 | } |
| 316 | } |
| 317 | explicit_bzero(&c, sizeof c); |
| 318 | if (write(fd, "\n", 1) < 0) { |
| 319 | /* The prompt's line stays open; nothing else depends on it. */ |
| 320 | } |
| 321 | } else { |
| 322 | n = -1; |
| 323 | } |
| 324 | restore_tty(fd); |
| 325 | for (int i = 0; i < 3; i++) { |
| 326 | sigaction(held[i], &before[i], NULL); |
| 327 | } |
| 328 | close(fd); |
| 329 | pin[len] = 0; |
| 330 | if (n < 0) { |
| 331 | return -1; |
| 332 | } |
| 333 | if (!ended || stopped || len == 0) { |
| 334 | explicit_bzero(pin, PIN_MAX + 1); |
| 335 | return 0; |
| 336 | } |
| 337 | if (over || len < PIN_MIN) { |
| 338 | explicit_bzero(pin, PIN_MAX + 1); |
| 339 | return -2; |
| 340 | } |
| 341 | return (int)len; |
| 342 | } |
| 343 | |
| 344 | /* Sends VERIFY with the PIN. 0 is verified, a positive number the tries |
| 345 | * left after a refusal, -1 a blocked PIN, and -2 a device failure. |
| 346 | * libykpiv copies the APDU onto its own stack and does not wipe it; the |
| 347 | * process neither dumps core nor can be traced, so the copy stays in it. */ |
| 348 | static int verify_pin(ykpiv_state *state, const char *pin, size_t len) { |
| 349 | const unsigned char templ[4] = {0x00, 0x20, 0x00, 0x80}; |
| 350 | unsigned char data[PIN_MAX]; |
| 351 | memset(data, 0xff, sizeof data); |
| 352 | memcpy(data, pin, len); |
| 353 | unsigned char out[32]; |
| 354 | unsigned long out_len = sizeof out; |
| 355 | int sw = 0; |
| 356 | ykpiv_rc rc = ykpiv_transfer_data(state, templ, data, sizeof data, out, &out_len, &sw); |
| 357 | explicit_bzero(data, sizeof data); |
| 358 | if (rc != YKPIV_OK) { |
| 359 | return -2; |
| 360 | } |
| 361 | if (sw == 0x9000) { |
| 362 | return 0; |
| 363 | } |
| 364 | if ((sw & 0xfff0) == 0x63c0) { |
| 365 | return (sw & 0xf) == 0 ? -1 : (sw & 0xf); |
| 366 | } |
| 367 | if (sw == 0x6983) { |
| 368 | return -1; |
| 369 | } |
| 370 | return -2; |
| 371 | } |
| 372 | |
| 373 | /* Makes sure the PIN is verified before a signature, asking for it when |
| 374 | * the device says it is not, or always when each is set. Returns 0 when |
| 375 | * the PIN is verified; otherwise it has replied with the refusal, unless |
| 376 | * a signal ended the session. */ |
| 377 | static int ensure_pin(ykpiv_state *state, uint32_t serial, int slot, int each) { |
| 378 | if (!exclusive()) { |
| 379 | return -1; |
| 380 | } |
| 381 | int tries = 0; |
| 382 | ykpiv_rc rc = ykpiv_verify(state, NULL, &tries); |
| 383 | if (rc == YKPIV_OK && !each) { |
| 384 | return 0; |
| 385 | } |
| 386 | if (rc == YKPIV_PIN_LOCKED) { |
| 387 | reply("error pin 0"); |
| 388 | return -1; |
| 389 | } |
| 390 | if (rc != YKPIV_OK && rc != YKPIV_WRONG_PIN) { |
| 391 | reply("error device %s", ykpiv_strerror(rc)); |
| 392 | return -1; |
| 393 | } |
| 394 | char pin[PIN_MAX + 1]; |
| 395 | int len = read_pin(pin, serial, slot); |
| 396 | int ret = -1; |
| 397 | if (stopped) { |
| 398 | /* A signal ends the session without a reply. */ |
| 399 | } else if (failed) { |
| 400 | reply("error device the terminal could not be read: %s", strerror(failed)); |
| 401 | } else if (len == -1) { |
| 402 | reply("error device there is no terminal to ask for the PIN on"); |
| 403 | } else if (len == -2) { |
| 404 | reply("error pin-format"); |
| 405 | } else if (len == 0) { |
| 406 | reply("error cancelled"); |
| 407 | } else { |
| 408 | int got = exclusive() ? verify_pin(state, pin, (size_t)len) : -3; |
| 409 | if (got == 0) { |
| 410 | ret = 0; |
| 411 | } else if (got == -2) { |
| 412 | reply("error device the PIN could not be sent to the YubiKey"); |
| 413 | } else if (got != -3) { |
| 414 | reply("error pin %d", got < 0 ? 0 : got); |
| 415 | } |
| 416 | } |
| 417 | /* The one exit: the PIN leaves the stack whichever way the check went. */ |
| 418 | explicit_bzero(pin, sizeof pin); |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static int parse_slot(const char *s) { |
| 423 | char *end = NULL; |
| 424 | long v = strtol(s, &end, 16); |
| 425 | if (!*s || *end || v < 0) { |
| 426 | return -1; |
| 427 | } |
| 428 | if (v == YKPIV_KEY_AUTHENTICATION || v == YKPIV_KEY_SIGNATURE || v == YKPIV_KEY_KEYMGM || |
| 429 | v == YKPIV_KEY_CARDAUTH || (v >= YKPIV_KEY_RETIRED1 && v <= YKPIV_KEY_RETIRED20)) { |
| 430 | return (int)v; |
| 431 | } |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | /* Reads one request. Returns the statement's length, 0 at the end of |
| 436 | * input or after a signal, and -1 after replying with the refusal, which |
| 437 | * a failed read is too. */ |
| 438 | static long read_request(unsigned char *buf, int *each, int *none) { |
| 439 | char line[64]; |
| 440 | if (read_line(STDIN_FILENO, line, sizeof line) == 0 || stopped) { |
| 441 | if (failed && !stopped) { |
| 442 | reply("error request the request could not be read: %s", strerror(failed)); |
| 443 | return -1; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | /* The length is one to four decimal digits: MAX_STATEMENT has four. */ |
| 448 | const char *p = line; |
| 449 | long n = 0; |
| 450 | int digits = 0; |
| 451 | if (strncmp(p, "sign ", 5) == 0) { |
| 452 | for (p += 5; *p >= '0' && *p <= '9' && digits <= 4; p++, digits++) { |
| 453 | n = n * 10 + (*p - '0'); |
| 454 | } |
| 455 | } |
| 456 | if (digits < 1 || digits > 4 || *p++ != ' ') { |
| 457 | reply("error request a request is: sign <length> <session|each|none>"); |
| 458 | return -1; |
| 459 | } |
| 460 | *each = strcmp(p, "each\n") == 0; |
| 461 | *none = strcmp(p, "none\n") == 0; |
| 462 | if (!*each && !*none && strcmp(p, "session\n") != 0) { |
| 463 | reply("error request the PIN mode is session, each or none"); |
| 464 | return -1; |
| 465 | } |
| 466 | if (n < 1 || n > MAX_STATEMENT) { |
| 467 | reply("error request a statement is 1 to %d bytes", MAX_STATEMENT); |
| 468 | return -1; |
| 469 | } |
| 470 | if (read_in(STDIN_FILENO, buf, (size_t)n) != (size_t)n) { |
| 471 | if (stopped) { |
| 472 | return 0; |
| 473 | } |
| 474 | if (failed) { |
| 475 | reply("error request the statement could not be read: %s", strerror(failed)); |
| 476 | } else { |
| 477 | reply("error request the statement ended early"); |
| 478 | } |
| 479 | return -1; |
| 480 | } |
| 481 | return n; |
| 482 | } |
| 483 | |
| 484 | /* Signs one statement. Returns 0 after replying with the signature, and |
| 485 | * -1 after replying with the refusal. */ |
| 486 | static int sign(ykpiv_state *state, const unsigned char *statement, size_t n, int slot) { |
| 487 | if (!exclusive()) { |
| 488 | return -1; |
| 489 | } |
| 490 | reply("touch"); |
| 491 | if (broken) { |
| 492 | return -1; |
| 493 | } |
| 494 | unsigned char sig[128]; |
| 495 | size_t sig_len = sizeof sig; |
| 496 | ykpiv_rc rc = ykpiv_sign_data(state, statement, n, sig, &sig_len, YKPIV_ALGO_ED25519, |
| 497 | (unsigned char)slot); |
| 498 | if (rc == YKPIV_AUTHENTICATION_ERROR) { |
| 499 | /* The YubiKey refuses with 6982 both a signature not touched in time |
| 500 | * and one without a verified PIN, and libykpiv returns both as |
| 501 | * YKPIV_AUTHENTICATION_ERROR, so the PIN's state names the cause. */ |
| 502 | int tries = 0; |
| 503 | if (!exclusive()) { |
| 504 | return -1; |
| 505 | } |
| 506 | if (ykpiv_verify(state, NULL, &tries) == YKPIV_OK) { |
| 507 | reply("error touch"); |
| 508 | } else { |
| 509 | reply("error device it refused to sign without a verified PIN"); |
| 510 | } |
| 511 | return -1; |
| 512 | } |
| 513 | if (rc != YKPIV_OK || sig_len != 64) { |
| 514 | reply("error device the YubiKey did not sign: %s", ykpiv_strerror(rc)); |
| 515 | return -1; |
| 516 | } |
| 517 | reply_hex("signature", "", sig, sig_len); |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | /* Opens the session: the YubiKey in exclusive mode, its PIV application |
| 522 | * selected, and the key line written. Returns 0, or -1 after replying with |
| 523 | * the refusal. */ |
| 524 | static int open_session(ykpiv_state *state, const char *reader, int slot, uint32_t *serial) { |
| 525 | if (connect_exclusive(reader) != 0) { |
| 526 | return -1; |
| 527 | } |
| 528 | ykpiv_rc rc = ykpiv_connect_with_external_card(state, (uintptr_t)context, (uintptr_t)card); |
| 529 | handed = rc == YKPIV_OK; |
| 530 | /* An external card is not selected for; reading the PIN's tries selects |
| 531 | * the PIV application, as ykpiv_connect does, and reads the YubiKey's |
| 532 | * version and serial. */ |
| 533 | int tries = 0; |
| 534 | if (rc == YKPIV_OK && exclusive()) { |
| 535 | rc = ykpiv_get_pin_retries(state, &tries); |
| 536 | } else if (rc == YKPIV_OK) { |
| 537 | return -1; |
| 538 | } |
| 539 | if (rc != YKPIV_OK) { |
| 540 | reply("error device no YubiKey could be reached: %s", ykpiv_strerror(rc)); |
| 541 | return -1; |
| 542 | } |
| 543 | if (!exclusive()) { |
| 544 | return -1; |
| 545 | } |
| 546 | rc = ykpiv_get_serial(state, serial); |
| 547 | if (rc != YKPIV_OK) { |
| 548 | reply("error device the YubiKey's serial number could not be read: %s", ykpiv_strerror(rc)); |
| 549 | return -1; |
| 550 | } |
| 551 | unsigned char cert[YKPIV_OBJ_MAX_SIZE]; |
| 552 | size_t cert_len = sizeof cert; |
| 553 | if (!exclusive()) { |
| 554 | return -1; |
| 555 | } |
| 556 | rc = ykpiv_attest(state, (unsigned char)slot, cert, &cert_len); |
| 557 | if (rc != YKPIV_OK) { |
| 558 | reply("error device slot %02x holds no key generated on the YubiKey: %s", slot, ykpiv_strerror(rc)); |
| 559 | return -1; |
| 560 | } |
| 561 | char prefix[16]; |
| 562 | snprintf(prefix, sizeof prefix, "%u ", *serial); |
| 563 | reply_hex("key", prefix, cert, cert_len); |
| 564 | return broken ? -1 : 0; |
| 565 | } |
| 566 | |
| 567 | int main(int argc, char **argv) { |
| 568 | prctl(PR_SET_DUMPABLE, 0); |
| 569 | /* A closed standard descriptor would be reused by the next open, so the |
| 570 | * pcscd socket could take stdout's place and receive the replies, or the |
| 571 | * terminal stdin's and have requests read from it. */ |
| 572 | for (int fd = 0; fd <= 2; fd++) { |
| 573 | if (fcntl(fd, F_GETFD) < 0 && errno == EBADF) { |
| 574 | open("/dev/null", fd == 0 ? O_RDONLY : O_WRONLY); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | int slot = YKPIV_KEY_SIGNATURE; |
| 579 | const char *reader = NULL; |
| 580 | for (int i = 1; i < argc; i++) { |
| 581 | if (strcmp(argv[i], "--slot") == 0 && i + 1 < argc) { |
| 582 | slot = parse_slot(argv[++i]); |
| 583 | if (slot < 0) { |
| 584 | reply("error request the slot is one of 9a, 9c, 9d, 9e and 82 to 95"); |
| 585 | return 2; |
| 586 | } |
| 587 | } else if (strcmp(argv[i], "--reader") == 0 && i + 1 < argc) { |
| 588 | reader = argv[++i]; |
| 589 | } else { |
| 590 | reply("error request usage: valley-piv-signer [--slot HEX] [--reader NAME]"); |
| 591 | return 2; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* No SA_RESTART: a signal makes a blocking read return, so the session |
| 596 | * ends at once. pcsc-lite's client retries its reads and writes on |
| 597 | * EINTR, so a signal during a libykpiv call leaves the call whole. */ |
| 598 | struct sigaction sa; |
| 599 | memset(&sa, 0, sizeof sa); |
| 600 | sa.sa_handler = on_signal; |
| 601 | sigemptyset(&sa.sa_mask); |
| 602 | const int ends[] = {SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGUSR1, SIGUSR2, |
| 603 | SIGALRM, SIGVTALRM, SIGPROF, SIGXCPU, SIGXFSZ, SIGIO, SIGPWR}; |
| 604 | sigemptyset(&ending); |
| 605 | for (size_t i = 0; i < sizeof ends / sizeof ends[0]; i++) { |
| 606 | sigaction(ends[i], &sa, NULL); |
| 607 | sigaddset(&ending, ends[i]); |
| 608 | } |
| 609 | signal(SIGPIPE, SIG_IGN); |
| 610 | |
| 611 | ykpiv_state *state = NULL; |
| 612 | ykpiv_rc rc = ykpiv_init(&state, 0); |
| 613 | if (rc != YKPIV_OK) { |
| 614 | reply("error device %s", ykpiv_strerror(rc)); |
| 615 | exit_by_signal(); |
| 616 | return 1; |
| 617 | } |
| 618 | uint32_t serial = 0; |
| 619 | int status = open_session(state, reader, slot, &serial) == 0 ? 0 : 1; |
| 620 | static unsigned char statement[MAX_STATEMENT]; |
| 621 | while (status == 0 && !stopped && !broken) { |
| 622 | int each = 0, none = 0; |
| 623 | long n = read_request(statement, &each, &none); |
| 624 | if (n == 0) { |
| 625 | break; |
| 626 | } |
| 627 | if (n < 0 || (!none && ensure_pin(state, serial, slot, each) != 0) || |
| 628 | sign(state, statement, (size_t)n, slot) != 0) { |
| 629 | status = 1; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* The reset is what makes the YubiKey forget the verified PIN. Once |
| 634 | * handed, ykpiv_done disconnects with a reset whichever card libykpiv |
| 635 | * holds, the helper's or a replacement, and releases its context; |
| 636 | * the helper's card, if replaced, was reset when libykpiv let go of |
| 637 | * it. Before that, the handles are the helper's alone. */ |
| 638 | if (handed) { |
| 639 | ykpiv_done(state); |
| 640 | } else { |
| 641 | ykpiv_done_with_external_card(state); |
| 642 | if (connected) { |
| 643 | SCardDisconnect(card, SCARD_RESET_CARD); |
| 644 | } |
| 645 | if (established) { |
| 646 | SCardReleaseContext(context); |
| 647 | } |
| 648 | } |
| 649 | exit_by_signal(); |
| 650 | return status != 0 || broken || stopped || failed != 0; |
| 651 | } |