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