1 /* $OpenBSD: ssh-add.c,v 1.122 2015/03/26 12:32:38 naddy 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 "includes.h" 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 43 #include <openssl/evp.h> 44 #include "openbsd-compat/openssl-compat.h" 45 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <pwd.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <limits.h> 55 56 #include "xmalloc.h" 57 #include "ssh.h" 58 #include "rsa.h" 59 #include "log.h" 60 #include "sshkey.h" 61 #include "sshbuf.h" 62 #include "authfd.h" 63 #include "authfile.h" 64 #include "pathnames.h" 65 #include "misc.h" 66 #include "ssherr.h" 67 #include "digest.h" 68 69 /* argv0 */ 70 extern char *__progname; 71 72 /* Default files to add */ 73 static char *default_files[] = { 74 #ifdef WITH_OPENSSL 75 _PATH_SSH_CLIENT_ID_RSA, 76 _PATH_SSH_CLIENT_ID_DSA, 77 #ifdef OPENSSL_HAS_ECC 78 _PATH_SSH_CLIENT_ID_ECDSA, 79 #endif 80 #endif /* WITH_OPENSSL */ 81 _PATH_SSH_CLIENT_ID_ED25519, 82 #ifdef WITH_SSH1 83 _PATH_SSH_CLIENT_IDENTITY, 84 #endif 85 NULL 86 }; 87 88 static int fingerprint_hash = SSH_FP_HASH_DEFAULT; 89 90 /* Default lifetime (0 == forever) */ 91 static int lifetime = 0; 92 93 /* User has to confirm key use */ 94 static int confirm = 0; 95 96 /* we keep a cache of one passphrases */ 97 static char *pass = NULL; 98 static void 99 clear_pass(void) 100 { 101 if (pass) { 102 explicit_bzero(pass, strlen(pass)); 103 free(pass); 104 pass = NULL; 105 } 106 } 107 108 static int 109 delete_file(int agent_fd, const char *filename, int key_only) 110 { 111 struct sshkey *public, *cert = NULL; 112 char *certpath = NULL, *comment = NULL; 113 int r, ret = -1; 114 115 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { 116 printf("Bad key file %s: %s\n", filename, ssh_err(r)); 117 return -1; 118 } 119 if ((r = ssh_remove_identity(agent_fd, public)) == 0) { 120 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 121 ret = 0; 122 } else 123 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 124 filename, ssh_err(r)); 125 126 if (key_only) 127 goto out; 128 129 /* Now try to delete the corresponding certificate too */ 130 free(comment); 131 comment = NULL; 132 xasprintf(&certpath, "%s-cert.pub", filename); 133 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { 134 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 135 error("Failed to load certificate \"%s\": %s", 136 certpath, ssh_err(r)); 137 goto out; 138 } 139 140 if (!sshkey_equal_public(cert, public)) 141 fatal("Certificate %s does not match private key %s", 142 certpath, filename); 143 144 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { 145 fprintf(stderr, "Identity removed: %s (%s)\n", certpath, 146 comment); 147 ret = 0; 148 } else 149 fprintf(stderr, "Could not remove identity \"%s\": %s\n", 150 certpath, ssh_err(r)); 151 152 out: 153 if (cert != NULL) 154 sshkey_free(cert); 155 if (public != NULL) 156 sshkey_free(public); 157 free(certpath); 158 free(comment); 159 160 return ret; 161 } 162 163 /* Send a request to remove all identities. */ 164 static int 165 delete_all(int agent_fd) 166 { 167 int ret = -1; 168 169 if (ssh_remove_all_identities(agent_fd, 2) == 0) 170 ret = 0; 171 /* ignore error-code for ssh1 */ 172 ssh_remove_all_identities(agent_fd, 1); 173 174 if (ret == 0) 175 fprintf(stderr, "All identities removed.\n"); 176 else 177 fprintf(stderr, "Failed to remove all identities.\n"); 178 179 return ret; 180 } 181 182 static int 183 add_file(int agent_fd, const char *filename, int key_only) 184 { 185 struct sshkey *private, *cert; 186 char *comment = NULL; 187 char msg[1024], *certpath = NULL; 188 int r, fd, ret = -1; 189 struct sshbuf *keyblob; 190 191 if (strcmp(filename, "-") == 0) { 192 fd = STDIN_FILENO; 193 filename = "(stdin)"; 194 } else if ((fd = open(filename, O_RDONLY)) < 0) { 195 perror(filename); 196 return -1; 197 } 198 199 /* 200 * Since we'll try to load a keyfile multiple times, permission errors 201 * will occur multiple times, so check perms first and bail if wrong. 202 */ 203 if (fd != STDIN_FILENO) { 204 if (sshkey_perm_ok(fd, filename) != 0) { 205 close(fd); 206 return -1; 207 } 208 } 209 if ((keyblob = sshbuf_new()) == NULL) 210 fatal("%s: sshbuf_new failed", __func__); 211 if ((r = sshkey_load_file(fd, keyblob)) != 0) { 212 fprintf(stderr, "Error loading key \"%s\": %s\n", 213 filename, ssh_err(r)); 214 sshbuf_free(keyblob); 215 close(fd); 216 return -1; 217 } 218 close(fd); 219 220 /* At first, try empty passphrase */ 221 if ((r = sshkey_parse_private_fileblob(keyblob, "", filename, 222 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 223 fprintf(stderr, "Error loading key \"%s\": %s\n", 224 filename, ssh_err(r)); 225 goto fail_load; 226 } 227 /* try last */ 228 if (private == NULL && pass != NULL) { 229 if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename, 230 &private, &comment)) != 0 && 231 r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 232 fprintf(stderr, "Error loading key \"%s\": %s\n", 233 filename, ssh_err(r)); 234 goto fail_load; 235 } 236 } 237 if (comment == NULL) 238 comment = xstrdup(filename); 239 if (private == NULL) { 240 /* clear passphrase since it did not work */ 241 clear_pass(); 242 snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ", 243 comment, confirm ? " (will confirm each use)" : ""); 244 for (;;) { 245 pass = read_passphrase(msg, RP_ALLOW_STDIN); 246 if (strcmp(pass, "") == 0) 247 goto fail_load; 248 if ((r = sshkey_parse_private_fileblob(keyblob, pass, 249 filename, &private, NULL)) == 0) 250 break; 251 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { 252 fprintf(stderr, 253 "Error loading key \"%s\": %s\n", 254 filename, ssh_err(r)); 255 fail_load: 256 clear_pass(); 257 free(comment); 258 sshbuf_free(keyblob); 259 return -1; 260 } 261 clear_pass(); 262 snprintf(msg, sizeof msg, 263 "Bad passphrase, try again for %.200s%s: ", comment, 264 confirm ? " (will confirm each use)" : ""); 265 } 266 } 267 sshbuf_free(keyblob); 268 269 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 270 lifetime, confirm)) == 0) { 271 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 272 ret = 0; 273 if (lifetime != 0) 274 fprintf(stderr, 275 "Lifetime set to %d seconds\n", lifetime); 276 if (confirm != 0) 277 fprintf(stderr, 278 "The user must confirm each use of the key\n"); 279 } else { 280 fprintf(stderr, "Could not add identity \"%s\": %s\n", 281 filename, ssh_err(r)); 282 } 283 284 /* Skip trying to load the cert if requested */ 285 if (key_only) 286 goto out; 287 288 /* Now try to add the certificate flavour too */ 289 xasprintf(&certpath, "%s-cert.pub", filename); 290 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { 291 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) 292 error("Failed to load certificate \"%s\": %s", 293 certpath, ssh_err(r)); 294 goto out; 295 } 296 297 if (!sshkey_equal_public(cert, private)) { 298 error("Certificate %s does not match private key %s", 299 certpath, filename); 300 sshkey_free(cert); 301 goto out; 302 } 303 304 /* Graft with private bits */ 305 if ((r = sshkey_to_certified(private, 306 sshkey_cert_is_legacy(cert))) != 0) { 307 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); 308 sshkey_free(cert); 309 goto out; 310 } 311 if ((r = sshkey_cert_copy(cert, private)) != 0) { 312 error("%s: key_cert_copy: %s", __func__, ssh_err(r)); 313 sshkey_free(cert); 314 goto out; 315 } 316 sshkey_free(cert); 317 318 if ((r = ssh_add_identity_constrained(agent_fd, private, comment, 319 lifetime, confirm)) != 0) { 320 error("Certificate %s (%s) add failed: %s", certpath, 321 private->cert->key_id, ssh_err(r)); 322 goto out; 323 } 324 fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 325 private->cert->key_id); 326 if (lifetime != 0) 327 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); 328 if (confirm != 0) 329 fprintf(stderr, "The user must confirm each use of the key\n"); 330 out: 331 free(certpath); 332 free(comment); 333 sshkey_free(private); 334 335 return ret; 336 } 337 338 static int 339 update_card(int agent_fd, int add, const char *id) 340 { 341 char *pin = NULL; 342 int r, ret = -1; 343 344 if (add) { 345 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", 346 RP_ALLOW_STDIN)) == NULL) 347 return -1; 348 } 349 350 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, 351 lifetime, confirm)) == 0) { 352 fprintf(stderr, "Card %s: %s\n", 353 add ? "added" : "removed", id); 354 ret = 0; 355 } else { 356 fprintf(stderr, "Could not %s card \"%s\": %s\n", 357 add ? "add" : "remove", id, ssh_err(r)); 358 ret = -1; 359 } 360 free(pin); 361 return ret; 362 } 363 364 static int 365 list_identities(int agent_fd, int do_fp) 366 { 367 char *fp; 368 int r, had_identities = 0; 369 struct ssh_identitylist *idlist; 370 size_t i; 371 #ifdef WITH_SSH1 372 int version = 1; 373 #else 374 int version = 2; 375 #endif 376 377 for (; version <= 2; version++) { 378 if ((r = ssh_fetch_identitylist(agent_fd, version, 379 &idlist)) != 0) { 380 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 381 fprintf(stderr, "error fetching identities for " 382 "protocol %d: %s\n", version, ssh_err(r)); 383 continue; 384 } 385 for (i = 0; i < idlist->nkeys; i++) { 386 had_identities = 1; 387 if (do_fp) { 388 fp = sshkey_fingerprint(idlist->keys[i], 389 fingerprint_hash, SSH_FP_DEFAULT); 390 printf("%d %s %s (%s)\n", 391 sshkey_size(idlist->keys[i]), 392 fp == NULL ? "(null)" : fp, 393 idlist->comments[i], 394 sshkey_type(idlist->keys[i])); 395 free(fp); 396 } else { 397 if ((r = sshkey_write(idlist->keys[i], 398 stdout)) != 0) { 399 fprintf(stderr, "sshkey_write: %s\n", 400 ssh_err(r)); 401 continue; 402 } 403 fprintf(stdout, " %s\n", idlist->comments[i]); 404 } 405 } 406 ssh_free_identitylist(idlist); 407 } 408 if (!had_identities) { 409 printf("The agent has no identities.\n"); 410 return -1; 411 } 412 return 0; 413 } 414 415 static int 416 lock_agent(int agent_fd, int lock) 417 { 418 char prompt[100], *p1, *p2; 419 int r, passok = 1, ret = -1; 420 421 strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 422 p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 423 if (lock) { 424 strlcpy(prompt, "Again: ", sizeof prompt); 425 p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 426 if (strcmp(p1, p2) != 0) { 427 fprintf(stderr, "Passwords do not match.\n"); 428 passok = 0; 429 } 430 explicit_bzero(p2, strlen(p2)); 431 free(p2); 432 } 433 if (passok) { 434 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { 435 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 436 ret = 0; 437 } else { 438 fprintf(stderr, "Failed to %slock agent: %s\n", 439 lock ? "" : "un", ssh_err(r)); 440 } 441 } 442 explicit_bzero(p1, strlen(p1)); 443 free(p1); 444 return (ret); 445 } 446 447 static int 448 do_file(int agent_fd, int deleting, int key_only, char *file) 449 { 450 if (deleting) { 451 if (delete_file(agent_fd, file, key_only) == -1) 452 return -1; 453 } else { 454 if (add_file(agent_fd, file, key_only) == -1) 455 return -1; 456 } 457 return 0; 458 } 459 460 static void 461 usage(void) 462 { 463 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); 464 fprintf(stderr, "Options:\n"); 465 fprintf(stderr, " -l List fingerprints of all identities.\n"); 466 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); 467 fprintf(stderr, " -L List public key parameters of all identities.\n"); 468 fprintf(stderr, " -k Load only keys and not certificates.\n"); 469 fprintf(stderr, " -c Require confirmation to sign using identities\n"); 470 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); 471 fprintf(stderr, " -d Delete identity.\n"); 472 fprintf(stderr, " -D Delete all identities.\n"); 473 fprintf(stderr, " -x Lock agent.\n"); 474 fprintf(stderr, " -X Unlock agent.\n"); 475 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); 476 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); 477 } 478 479 int 480 main(int argc, char **argv) 481 { 482 extern char *optarg; 483 extern int optind; 484 int agent_fd; 485 char *pkcs11provider = NULL; 486 int r, i, ch, deleting = 0, ret = 0, key_only = 0; 487 int xflag = 0, lflag = 0, Dflag = 0; 488 489 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 490 sanitise_stdfd(); 491 492 __progname = ssh_get_progname(argv[0]); 493 seed_rng(); 494 495 #ifdef WITH_OPENSSL 496 OpenSSL_add_all_algorithms(); 497 #endif 498 499 setvbuf(stdout, NULL, _IOLBF, 0); 500 501 /* First, get a connection to the authentication agent. */ 502 switch (r = ssh_get_authentication_socket(&agent_fd)) { 503 case 0: 504 break; 505 case SSH_ERR_AGENT_NOT_PRESENT: 506 fprintf(stderr, "Could not open a connection to your " 507 "authentication agent.\n"); 508 exit(2); 509 default: 510 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); 511 exit(2); 512 } 513 514 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) { 515 switch (ch) { 516 case 'E': 517 fingerprint_hash = ssh_digest_alg_by_name(optarg); 518 if (fingerprint_hash == -1) 519 fatal("Invalid hash algorithm \"%s\"", optarg); 520 break; 521 case 'k': 522 key_only = 1; 523 break; 524 case 'l': 525 case 'L': 526 if (lflag != 0) 527 fatal("-%c flag already specified", lflag); 528 lflag = ch; 529 break; 530 case 'x': 531 case 'X': 532 if (xflag != 0) 533 fatal("-%c flag already specified", xflag); 534 xflag = ch; 535 break; 536 case 'c': 537 confirm = 1; 538 break; 539 case 'd': 540 deleting = 1; 541 break; 542 case 'D': 543 Dflag = 1; 544 break; 545 case 's': 546 pkcs11provider = optarg; 547 break; 548 case 'e': 549 deleting = 1; 550 pkcs11provider = optarg; 551 break; 552 case 't': 553 if ((lifetime = convtime(optarg)) == -1) { 554 fprintf(stderr, "Invalid lifetime\n"); 555 ret = 1; 556 goto done; 557 } 558 break; 559 default: 560 usage(); 561 ret = 1; 562 goto done; 563 } 564 } 565 566 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) 567 fatal("Invalid combination of actions"); 568 else if (xflag) { 569 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) 570 ret = 1; 571 goto done; 572 } else if (lflag) { 573 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) 574 ret = 1; 575 goto done; 576 } else if (Dflag) { 577 if (delete_all(agent_fd) == -1) 578 ret = 1; 579 goto done; 580 } 581 582 argc -= optind; 583 argv += optind; 584 if (pkcs11provider != NULL) { 585 if (update_card(agent_fd, !deleting, pkcs11provider) == -1) 586 ret = 1; 587 goto done; 588 } 589 if (argc == 0) { 590 char buf[PATH_MAX]; 591 struct passwd *pw; 592 struct stat st; 593 int count = 0; 594 595 if ((pw = getpwuid(getuid())) == NULL) { 596 fprintf(stderr, "No user found with uid %u\n", 597 (u_int)getuid()); 598 ret = 1; 599 goto done; 600 } 601 602 for (i = 0; default_files[i]; i++) { 603 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 604 default_files[i]); 605 if (stat(buf, &st) < 0) 606 continue; 607 if (do_file(agent_fd, deleting, key_only, buf) == -1) 608 ret = 1; 609 else 610 count++; 611 } 612 if (count == 0) 613 ret = 1; 614 } else { 615 for (i = 0; i < argc; i++) { 616 if (do_file(agent_fd, deleting, key_only, 617 argv[i]) == -1) 618 ret = 1; 619 } 620 } 621 clear_pass(); 622 623 done: 624 ssh_close_authentication_socket(agent_fd); 625 return ret; 626 } 627