1 /* $OpenBSD: ssh-add.c,v 1.135 2018/02/23 15:58:37 markus Exp $ */ 2 /* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * Adds an identity to the authentication server, or removes an identity. 7 * 8 * As far as I am concerned, the code I have written for this software 9 * can be used freely for any purpose. Any derived versions of this 10 * software must be clearly marked as such, and if the derived work is 11 * incompatible with the protocol description in the RFC file, it must be 12 * called by a name other than "ssh" or "Secure Shell". 13 * 14 * SSH2 implementation, 15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 41 #include <openssl/evp.h> 42 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <pwd.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <limits.h> 51 52 #include "xmalloc.h" 53 #include "ssh.h" 54 #include "log.h" 55 #include "sshkey.h" 56 #include "sshbuf.h" 57 #include "authfd.h" 58 #include "authfile.h" 59 #include "pathnames.h" 60 #include "misc.h" 61 #include "ssherr.h" 62 #include "digest.h" 63 64 /* argv0 */ 65 extern char *__progname; 66 67 /* Default files to add */ 68 static char *default_files[] = { 69 _PATH_SSH_CLIENT_ID_RSA, 70 _PATH_SSH_CLIENT_ID_DSA, 71 _PATH_SSH_CLIENT_ID_ECDSA, 72 _PATH_SSH_CLIENT_ID_ED25519, 73 _PATH_SSH_CLIENT_ID_XMSS, 74 NULL 75 }; 76 77 static int fingerprint_hash = SSH_FP_HASH_DEFAULT; 78 79 /* Default lifetime (0 == forever) */ 80 static int lifetime = 0; 81 82 /* User has to confirm key use */ 83 static int confirm = 0; 84 85 /* Maximum number of signatures (XMSS) */ 86 static u_int maxsign = 0; 87 static u_int minleft = 0; 88 89 /* we keep a cache of one passphrase */ 90 static char *pass = NULL; 91 static void 92 clear_pass(void) 93 { 94 if (pass) { 95 explicit_bzero(pass, strlen(pass)); 96 free(pass); 97 pass = NULL; 98 } 99 } 100 101 static int 102 delete_file(int agent_fd, const char *filename, int key_only, int qflag) 103 { 104 struct sshkey *public, *cert = NULL; 105 char *certpath = NULL, *comment = NULL; 106 int r, ret = -1; 107 108 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 109 printf("Bad key file %s: %s\n", filename, ssh_err(r)); 110 return -1; 111 } 112 if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 113 if (!qflag) { 114 fprintf(stderr, "Identity removed: %s (%s)\n", 115 filename, comment); 116 } 117 ret = 0; 118 } else 119 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 120 filename, ssh_err(r)); 121 122 if (key_only) 123 goto out; 124 125 /* Now try to delete the corresponding certificate too */ 126 free(comment); 127 comment = NULL; 128 xasprintf(&certpath, "%s-cert.pub", filename); 129 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 130 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 131 error("Failed to load certificate \"%s\": %s", 132 certpath, ssh_err(r)); 133 goto out; 134 } 135 136 if (!sshkey_equal_public(cert, public)) 137 fatal("Certificate %s does not match private key %s", 138 certpath, filename); 139 140 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { 141 if (!qflag) { 142 fprintf(stderr, "Identity removed: %s (%s)\n", 143 certpath, comment); 144 } 145 ret = 0; 146 } else 147 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 148 certpath, ssh_err(r)); 149 150 out: 151 sshkey_free(cert); 152 sshkey_free(public); 153 free(certpath); 154 free(comment); 155 156 return ret; 157 } 158 159 /* Send a request to remove all identities. */ 160 static int 161 delete_all(int agent_fd) 162 { 163 int ret = -1; 164 165 /* 166 * Since the agent might be forwarded, old or non-OpenSSH, when asked 167 * to remove all keys, attempt to remove both protocol v.1 and v.2 168 * keys. 169 */ 170 if (ssh_remove_all_identities(agent_fd, 2) == 0) 171 ret = 0; 172 /* ignore error-code for ssh1 */ 173 ssh_remove_all_identities(agent_fd, 1); 174 175 if (ret == 0) 176 fprintf(stderr, "All identities removed.\n"); 177 else 178 fprintf(stderr, "Failed to remove all identities.\n"); 179 180 return ret; 181 } 182 183 static int 184 add_file(int agent_fd, const char *filename, int key_only, int qflag) 185 { 186 struct sshkey *private, *cert; 187 char *comment = NULL; 188 char msg[1024], *certpath = NULL; 189 int r, fd, ret = -1; 190 size_t i; 191 u_int32_t left; 192 struct sshbuf *keyblob; 193 struct ssh_identitylist *idlist; 194 195 if (strcmp(filename, "-") == 0) { 196 fd = STDIN_FILENO; 197 filename = "(stdin)"; 198 } else if ((fd = open(filename, O_RDONLY)) < 0) { 199 perror(filename); 200 return -1; 201 } 202 203 /* 204 * Since we'll try to load a keyfile multiple times, permission errors 205 * will occur multiple times, so check perms first and bail if wrong. 206 */ 207 if (fd != STDIN_FILENO) { 208 if (sshkey_perm_ok(fd, filename) != 0) { 209 close(fd); 210 return -1; 211 } 212 } 213 if ((keyblob = sshbuf_new()) == NULL) 214 fatal("%s: sshbuf_new failed", __func__); 215 if ((r = sshkey_load_file(fd, keyblob)) != 0) { 216 fprintf(stderr, "Error loading key \"%s\": %s\n", 217 filename, ssh_err(r)); 218 sshbuf_free(keyblob); 219 close(fd); 220 return -1; 221 } 222 close(fd); 223 224 /* At first, try empty passphrase */ 225 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, 226 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 227 fprintf(stderr, "Error loading key \"%s\": %s\n", 228 filename, ssh_err(r)); 229 goto fail_load; 230 } 231 /* try last */ 232 if (private == NULL && pass != NULL) { 233 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, 234 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 235 fprintf(stderr, "Error loading key \"%s\": %s\n", 236 filename, ssh_err(r)); 237 goto fail_load; 238 } 239 } 240 if (private == NULL) { 241 /* clear passphrase since it did not work */ 242 clear_pass(); 243 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", 244 filename, confirm ? " (will confirm each use)" : ""); 245 for (;;) { 246 pass = read_passphrase(msg, RP_ALLOW_STDIN); 247 if (strcmp(pass, "") == 0) 248 goto fail_load; 249 if ((r = sshkey_parse_private_fileblob(keyblob, pass, 250 &private, &comment)) == 0) 251 break; 252 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 253 fprintf(stderr, 254 "Error loading key \"%s\": %s\n", 255 filename, ssh_err(r)); 256 fail_load: 257 clear_pass(); 258 sshbuf_free(keyblob); 259 return -1; 260 } 261 clear_pass(); 262 snprintf(msg, sizeof msg, 263 "Bad passphrase, try again for %s%s: ", filename, 264 confirm ? " (will confirm each use)" : ""); 265 } 266 } 267 if (comment == NULL || *comment == '\0') 268 comment = xstrdup(filename); 269 sshbuf_free(keyblob); 270 271 /* For XMSS */ 272 if ((r = sshkey_set_filename(private, filename)) != 0) { 273 fprintf(stderr, "Could not add filename to private key: %s (%s)\n", 274 filename, comment); 275 goto out; 276 } 277 if (maxsign && minleft && 278 (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) { 279 for (i = 0; i < idlist->nkeys; i++) { 280 if (!sshkey_equal_public(idlist->keys[i], private)) 281 continue; 282 left = sshkey_signatures_left(idlist->keys[i]); 283 if (left < minleft) { 284 fprintf(stderr, 285 "Only %d signatures left.\n", left); 286 break; 287 } 288 fprintf(stderr, "Skipping update: "); 289 if (left == minleft) { 290 fprintf(stderr, 291 "required signatures left (%d).\n", left); 292 } else { 293 fprintf(stderr, 294 "more signatures left (%d) than" 295 " required (%d).\n", left, minleft); 296 } 297 ssh_free_identitylist(idlist); 298 goto out; 299 } 300 ssh_free_identitylist(idlist); 301 } 302 303 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 304 lifetime, confirm, maxsign)) == 0) { 305 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 306 ret = 0; 307 if (lifetime != 0) 308 fprintf(stderr, 309 "Lifetime set to %d seconds\n", lifetime); 310 if (confirm != 0) 311 fprintf(stderr, 312 "The user must confirm each use of the key\n"); 313 } else { 314 fprintf(stderr, "Could not add identity \"%s\": %s\n", 315 filename, ssh_err(r)); 316 } 317 318 /* Skip trying to load the cert if requested */ 319 if (key_only) 320 goto out; 321 322 /* Now try to add the certificate flavour too */ 323 xasprintf(&certpath, "%s-cert.pub", filename); 324 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 325 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 326 error("Failed to load certificate \"%s\": %s", 327 certpath, ssh_err(r)); 328 goto out; 329 } 330 331 if (!sshkey_equal_public(cert, private)) { 332 error("Certificate %s does not match private key %s", 333 certpath, filename); 334 sshkey_free(cert); 335 goto out; 336 } 337 338 /* Graft with private bits */ 339 if ((r = sshkey_to_certified(private)) != 0) { 340 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 341 sshkey_free(cert); 342 goto out; 343 } 344 if ((r = sshkey_cert_copy(cert, private)) != 0) { 345 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r)); 346 sshkey_free(cert); 347 goto out; 348 } 349 sshkey_free(cert); 350 351 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 352 lifetime, confirm, maxsign)) != 0) { 353 error("Certificate %s (%s) add failed: %s", certpath, 354 private->cert->key_id, ssh_err(r)); 355 goto out; 356 } 357 fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 358 private->cert->key_id); 359 if (lifetime != 0) 360 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); 361 if (confirm != 0) 362 fprintf(stderr, "The user must confirm each use of the key\n"); 363 out: 364 free(certpath); 365 free(comment); 366 sshkey_free(private); 367 368 return ret; 369 } 370 371 static int 372 update_card(int agent_fd, int add, const char *id) 373 { 374 char *pin = NULL; 375 int r, ret = -1; 376 377 if (add) { 378 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 379 RP_ALLOW_STDIN)) == NULL) 380 return -1; 381 } 382 383 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 384 lifetime, confirm)) == 0) { 385 fprintf(stderr, "Card %s: %s\n", 386 add ? "added" : "removed", id); 387 ret = 0; 388 } else { 389 fprintf(stderr, "Could not %s card \"%s\": %s\n", 390 add ? "add" : "remove", id, ssh_err(r)); 391 ret = -1; 392 } 393 free(pin); 394 return ret; 395 } 396 397 static int 398 list_identities(int agent_fd, int do_fp) 399 { 400 char *fp; 401 int r; 402 struct ssh_identitylist *idlist; 403 u_int32_t left; 404 size_t i; 405 406 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { 407 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 408 fprintf(stderr, "error fetching identities: %s\n", 409 ssh_err(r)); 410 else 411 printf("The agent has no identities.\n"); 412 return -1; 413 } 414 for (i = 0; i < idlist->nkeys; i++) { 415 if (do_fp) { 416 fp = sshkey_fingerprint(idlist->keys[i], 417 fingerprint_hash, SSH_FP_DEFAULT); 418 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]), 419 fp == NULL ? "(null)" : fp, idlist->comments[i], 420 sshkey_type(idlist->keys[i])); 421 free(fp); 422 } else { 423 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) { 424 fprintf(stderr, "sshkey_write: %s\n", 425 ssh_err(r)); 426 continue; 427 } 428 fprintf(stdout, " %s", idlist->comments[i]); 429 left = sshkey_signatures_left(idlist->keys[i]); 430 if (left > 0) 431 fprintf(stdout, 432 " [signatures left %d]", left); 433 fprintf(stdout, "\n"); 434 } 435 } 436 ssh_free_identitylist(idlist); 437 return 0; 438 } 439 440 static int 441 lock_agent(int agent_fd, int lock) 442 { 443 char prompt[100], *p1, *p2; 444 int r, passok = 1, ret = -1; 445 446 strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 447 p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 448 if (lock) { 449 strlcpy(prompt, "Again: ", sizeof prompt); 450 p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 451 if (strcmp(p1, p2) != 0) { 452 fprintf(stderr, "Passwords do not match.\n"); 453 passok = 0; 454 } 455 explicit_bzero(p2, strlen(p2)); 456 free(p2); 457 } 458 if (passok) { 459 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 460 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 461 ret = 0; 462 } else { 463 fprintf(stderr, "Failed to %slock agent: %s\n", 464 lock ? "" : "un", ssh_err(r)); 465 } 466 } 467 explicit_bzero(p1, strlen(p1)); 468 free(p1); 469 return (ret); 470 } 471 472 static int 473 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag) 474 { 475 if (deleting) { 476 if (delete_file(agent_fd, file, key_only, qflag) == -1) 477 return -1; 478 } else { 479 if (add_file(agent_fd, file, key_only, qflag) == -1) 480 return -1; 481 } 482 return 0; 483 } 484 485 static void 486 usage(void) 487 { 488 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); 489 fprintf(stderr, "Options:\n"); 490 fprintf(stderr, " -l List fingerprints of all identities.\n"); 491 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); 492 fprintf(stderr, " -L List public key parameters of all identities.\n"); 493 fprintf(stderr, " -k Load only keys and not certificates.\n"); 494 fprintf(stderr, " -c Require confirmation to sign using identities\n"); 495 fprintf(stderr, " -m minleft Maxsign is only changed if less than minleft are left (for XMSS)\n"); 496 fprintf(stderr, " -M maxsign Maximum number of signatures allowed (for XMSS)\n"); 497 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); 498 fprintf(stderr, " -d Delete identity.\n"); 499 fprintf(stderr, " -D Delete all identities.\n"); 500 fprintf(stderr, " -x Lock agent.\n"); 501 fprintf(stderr, " -X Unlock agent.\n"); 502 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); 503 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); 504 fprintf(stderr, " -q Be quiet after a successful operation.\n"); 505 } 506 507 int 508 main(int argc, char **argv) 509 { 510 extern char *optarg; 511 extern int optind; 512 int agent_fd; 513 char *pkcs11provider = NULL; 514 int r, i, ch, deleting = 0, ret = 0, key_only = 0; 515 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0; 516 517 ssh_malloc_init(); /* must be called before any mallocs */ 518 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 519 sanitise_stdfd(); 520 521 OpenSSL_add_all_algorithms(); 522 523 setvbuf(stdout, NULL, _IOLBF, 0); 524 525 /* First, get a connection to the authentication agent. */ 526 switch (r = ssh_get_authentication_socket(&agent_fd)) { 527 case 0: 528 break; 529 case SSH_ERR_AGENT_NOT_PRESENT: 530 fprintf(stderr, "Could not open a connection to your " 531 "authentication agent.\n"); 532 exit(2); 533 default: 534 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 535 exit(2); 536 } 537 538 while ((ch = getopt(argc, argv, "klLcdDxXE:e:M:m:qs:t:")) != -1) { 539 switch (ch) { 540 case 'E': 541 fingerprint_hash = ssh_digest_alg_by_name(optarg); 542 if (fingerprint_hash == -1) 543 fatal("Invalid hash algorithm \"%s\"", optarg); 544 break; 545 case 'k': 546 key_only = 1; 547 break; 548 case 'l': 549 case 'L': 550 if (lflag != 0) 551 fatal("-%c flag already specified", lflag); 552 lflag = ch; 553 break; 554 case 'x': 555 case 'X': 556 if (xflag != 0) 557 fatal("-%c flag already specified", xflag); 558 xflag = ch; 559 break; 560 case 'c': 561 confirm = 1; 562 break; 563 case 'm': 564 minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL); 565 if (minleft == 0) { 566 usage(); 567 ret = 1; 568 goto done; 569 } 570 break; 571 case 'M': 572 maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL); 573 if (maxsign == 0) { 574 usage(); 575 ret = 1; 576 goto done; 577 } 578 break; 579 case 'd': 580 deleting = 1; 581 break; 582 case 'D': 583 Dflag = 1; 584 break; 585 case 's': 586 pkcs11provider = optarg; 587 break; 588 case 'e': 589 deleting = 1; 590 pkcs11provider = optarg; 591 break; 592 case 't': 593 if ((lifetime = convtime(optarg)) == -1) { 594 fprintf(stderr, "Invalid lifetime\n"); 595 ret = 1; 596 goto done; 597 } 598 break; 599 case 'q': 600 qflag = 1; 601 break; 602 default: 603 usage(); 604 ret = 1; 605 goto done; 606 } 607 } 608 609 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 610 fatal("Invalid combination of actions"); 611 else if (xflag) { 612 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 613 ret = 1; 614 goto done; 615 } else if (lflag) { 616 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 617 ret = 1; 618 goto done; 619 } else if (Dflag) { 620 if (delete_all(agent_fd) == -1) 621 ret = 1; 622 goto done; 623 } 624 625 argc -= optind; 626 argv += optind; 627 if (pkcs11provider != NULL) { 628 if (update_card(agent_fd, !deleting, pkcs11provider) == -1) 629 ret = 1; 630 goto done; 631 } 632 if (argc == 0) { 633 char buf[PATH_MAX]; 634 struct passwd *pw; 635 struct stat st; 636 int count = 0; 637 638 if ((pw = getpwuid(getuid())) == NULL) { 639 fprintf(stderr, "No user found with uid %u\n", 640 (u_int)getuid()); 641 ret = 1; 642 goto done; 643 } 644 645 for (i = 0; default_files[i]; i++) { 646 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 647 default_files[i]); 648 if (stat(buf, &st) < 0) 649 continue; 650 if (do_file(agent_fd, deleting, key_only, buf, 651 qflag) == -1) 652 ret = 1; 653 else 654 count++; 655 } 656 if (count == 0) 657 ret = 1; 658 } else { 659 for (i = 0; i < argc; i++) { 660 if (do_file(agent_fd, deleting, key_only, 661 argv[i], qflag) == -1) 662 ret = 1; 663 } 664 } 665 clear_pass(); 666 667 done: 668 ssh_close_authentication_socket(agent_fd); 669 return ret; 670 } 671