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