1 /* $OpenBSD: ssh-add.c,v 1.105 2012/12/05 15:42:52 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 "includes.h" 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #include <sys/param.h> 43 44 #include <openssl/evp.h> 45 #include "openbsd-compat/openssl-compat.h" 46 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 55 #include "xmalloc.h" 56 #include "ssh.h" 57 #include "rsa.h" 58 #include "log.h" 59 #include "key.h" 60 #include "buffer.h" 61 #include "authfd.h" 62 #include "authfile.h" 63 #include "pathnames.h" 64 #include "misc.h" 65 66 /* argv0 */ 67 extern char *__progname; 68 69 /* Default files to add */ 70 static char *default_files[] = { 71 _PATH_SSH_CLIENT_ID_RSA, 72 _PATH_SSH_CLIENT_ID_DSA, 73 #ifdef OPENSSL_HAS_ECC 74 _PATH_SSH_CLIENT_ID_ECDSA, 75 #endif 76 _PATH_SSH_CLIENT_IDENTITY, 77 NULL 78 }; 79 80 /* Default lifetime (0 == forever) */ 81 static int lifetime = 0; 82 83 /* User has to confirm key use */ 84 static int confirm = 0; 85 86 /* we keep a cache of one passphrases */ 87 static char *pass = NULL; 88 static void 89 clear_pass(void) 90 { 91 if (pass) { 92 memset(pass, 0, strlen(pass)); 93 xfree(pass); 94 pass = NULL; 95 } 96 } 97 98 static int 99 delete_file(AuthenticationConnection *ac, const char *filename, int key_only) 100 { 101 Key *public = NULL, *cert = NULL; 102 char *certpath = NULL, *comment = NULL; 103 int ret = -1; 104 105 public = key_load_public(filename, &comment); 106 if (public == NULL) { 107 printf("Bad key file %s\n", filename); 108 return -1; 109 } 110 if (ssh_remove_identity(ac, public)) { 111 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); 112 ret = 0; 113 } else 114 fprintf(stderr, "Could not remove identity: %s\n", filename); 115 116 if (key_only) 117 goto out; 118 119 /* Now try to delete the corresponding certificate too */ 120 free(comment); 121 comment = NULL; 122 xasprintf(&certpath, "%s-cert.pub", filename); 123 if ((cert = key_load_public(certpath, &comment)) == NULL) 124 goto out; 125 if (!key_equal_public(cert, public)) 126 fatal("Certificate %s does not match private key %s", 127 certpath, filename); 128 129 if (ssh_remove_identity(ac, cert)) { 130 fprintf(stderr, "Identity removed: %s (%s)\n", certpath, 131 comment); 132 ret = 0; 133 } else 134 fprintf(stderr, "Could not remove identity: %s\n", certpath); 135 136 out: 137 if (cert != NULL) 138 key_free(cert); 139 if (public != NULL) 140 key_free(public); 141 free(certpath); 142 free(comment); 143 144 return ret; 145 } 146 147 /* Send a request to remove all identities. */ 148 static int 149 delete_all(AuthenticationConnection *ac) 150 { 151 int ret = -1; 152 153 if (ssh_remove_all_identities(ac, 1)) 154 ret = 0; 155 /* ignore error-code for ssh2 */ 156 ssh_remove_all_identities(ac, 2); 157 158 if (ret == 0) 159 fprintf(stderr, "All identities removed.\n"); 160 else 161 fprintf(stderr, "Failed to remove all identities.\n"); 162 163 return ret; 164 } 165 166 static int 167 add_file(AuthenticationConnection *ac, const char *filename, int key_only) 168 { 169 Key *private, *cert; 170 char *comment = NULL; 171 char msg[1024], *certpath = NULL; 172 int fd, perms_ok, ret = -1; 173 Buffer keyblob; 174 175 if (strcmp(filename, "-") == 0) { 176 fd = STDIN_FILENO; 177 filename = "(stdin)"; 178 } else if ((fd = open(filename, O_RDONLY)) < 0) { 179 perror(filename); 180 return -1; 181 } 182 183 /* 184 * Since we'll try to load a keyfile multiple times, permission errors 185 * will occur multiple times, so check perms first and bail if wrong. 186 */ 187 if (fd != STDIN_FILENO) { 188 perms_ok = key_perm_ok(fd, filename); 189 if (!perms_ok) { 190 close(fd); 191 return -1; 192 } 193 } 194 buffer_init(&keyblob); 195 if (!key_load_file(fd, filename, &keyblob)) { 196 buffer_free(&keyblob); 197 close(fd); 198 return -1; 199 } 200 close(fd); 201 202 /* At first, try empty passphrase */ 203 private = key_parse_private(&keyblob, filename, "", &comment); 204 if (comment == NULL) 205 comment = xstrdup(filename); 206 /* try last */ 207 if (private == NULL && pass != NULL) 208 private = key_parse_private(&keyblob, filename, pass, NULL); 209 if (private == NULL) { 210 /* clear passphrase since it did not work */ 211 clear_pass(); 212 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ", 213 comment); 214 for (;;) { 215 pass = read_passphrase(msg, RP_ALLOW_STDIN); 216 if (strcmp(pass, "") == 0) { 217 clear_pass(); 218 xfree(comment); 219 buffer_free(&keyblob); 220 return -1; 221 } 222 private = key_parse_private(&keyblob, filename, pass, 223 &comment); 224 if (private != NULL) 225 break; 226 clear_pass(); 227 snprintf(msg, sizeof msg, 228 "Bad passphrase, try again for %.200s: ", comment); 229 } 230 } 231 buffer_free(&keyblob); 232 233 if (ssh_add_identity_constrained(ac, private, comment, lifetime, 234 confirm)) { 235 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); 236 ret = 0; 237 if (lifetime != 0) 238 fprintf(stderr, 239 "Lifetime set to %d seconds\n", lifetime); 240 if (confirm != 0) 241 fprintf(stderr, 242 "The user must confirm each use of the key\n"); 243 } else { 244 fprintf(stderr, "Could not add identity: %s\n", filename); 245 } 246 247 /* Skip trying to load the cert if requested */ 248 if (key_only) 249 goto out; 250 251 /* Now try to add the certificate flavour too */ 252 xasprintf(&certpath, "%s-cert.pub", filename); 253 if ((cert = key_load_public(certpath, NULL)) == NULL) 254 goto out; 255 256 if (!key_equal_public(cert, private)) { 257 error("Certificate %s does not match private key %s", 258 certpath, filename); 259 key_free(cert); 260 goto out; 261 } 262 263 /* Graft with private bits */ 264 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) { 265 error("%s: key_to_certified failed", __func__); 266 key_free(cert); 267 goto out; 268 } 269 key_cert_copy(cert, private); 270 key_free(cert); 271 272 if (!ssh_add_identity_constrained(ac, private, comment, 273 lifetime, confirm)) { 274 error("Certificate %s (%s) add failed", certpath, 275 private->cert->key_id); 276 } 277 fprintf(stderr, "Certificate added: %s (%s)\n", certpath, 278 private->cert->key_id); 279 if (lifetime != 0) 280 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); 281 if (confirm != 0) 282 fprintf(stderr, "The user must confirm each use of the key\n"); 283 out: 284 if (certpath != NULL) 285 xfree(certpath); 286 xfree(comment); 287 key_free(private); 288 289 return ret; 290 } 291 292 static int 293 update_card(AuthenticationConnection *ac, int add, const char *id) 294 { 295 char *pin; 296 int ret = -1; 297 298 pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN); 299 if (pin == NULL) 300 return -1; 301 302 if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) { 303 fprintf(stderr, "Card %s: %s\n", 304 add ? "added" : "removed", id); 305 ret = 0; 306 } else { 307 fprintf(stderr, "Could not %s card: %s\n", 308 add ? "add" : "remove", id); 309 ret = -1; 310 } 311 xfree(pin); 312 return ret; 313 } 314 315 static int 316 list_identities(AuthenticationConnection *ac, int do_fp) 317 { 318 Key *key; 319 char *comment, *fp; 320 int had_identities = 0; 321 int version; 322 323 for (version = 1; version <= 2; version++) { 324 for (key = ssh_get_first_identity(ac, &comment, version); 325 key != NULL; 326 key = ssh_get_next_identity(ac, &comment, version)) { 327 had_identities = 1; 328 if (do_fp) { 329 fp = key_fingerprint(key, SSH_FP_MD5, 330 SSH_FP_HEX); 331 printf("%d %s %s (%s)\n", 332 key_size(key), fp, comment, key_type(key)); 333 xfree(fp); 334 } else { 335 if (!key_write(key, stdout)) 336 fprintf(stderr, "key_write failed"); 337 fprintf(stdout, " %s\n", comment); 338 } 339 key_free(key); 340 xfree(comment); 341 } 342 } 343 if (!had_identities) { 344 printf("The agent has no identities.\n"); 345 return -1; 346 } 347 return 0; 348 } 349 350 static int 351 lock_agent(AuthenticationConnection *ac, int lock) 352 { 353 char prompt[100], *p1, *p2; 354 int passok = 1, ret = -1; 355 356 strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); 357 p1 = read_passphrase(prompt, RP_ALLOW_STDIN); 358 if (lock) { 359 strlcpy(prompt, "Again: ", sizeof prompt); 360 p2 = read_passphrase(prompt, RP_ALLOW_STDIN); 361 if (strcmp(p1, p2) != 0) { 362 fprintf(stderr, "Passwords do not match.\n"); 363 passok = 0; 364 } 365 memset(p2, 0, strlen(p2)); 366 xfree(p2); 367 } 368 if (passok && ssh_lock_agent(ac, lock, p1)) { 369 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); 370 ret = 0; 371 } else 372 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un"); 373 memset(p1, 0, strlen(p1)); 374 xfree(p1); 375 return (ret); 376 } 377 378 static int 379 do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file) 380 { 381 if (deleting) { 382 if (delete_file(ac, file, key_only) == -1) 383 return -1; 384 } else { 385 if (add_file(ac, file, key_only) == -1) 386 return -1; 387 } 388 return 0; 389 } 390 391 static void 392 usage(void) 393 { 394 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); 395 fprintf(stderr, "Options:\n"); 396 fprintf(stderr, " -l List fingerprints of all identities.\n"); 397 fprintf(stderr, " -L List public key parameters of all identities.\n"); 398 fprintf(stderr, " -k Load only keys and not certificates.\n"); 399 fprintf(stderr, " -c Require confirmation to sign using identities\n"); 400 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); 401 fprintf(stderr, " -d Delete identity.\n"); 402 fprintf(stderr, " -D Delete all identities.\n"); 403 fprintf(stderr, " -x Lock agent.\n"); 404 fprintf(stderr, " -X Unlock agent.\n"); 405 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); 406 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); 407 } 408 409 int 410 main(int argc, char **argv) 411 { 412 extern char *optarg; 413 extern int optind; 414 AuthenticationConnection *ac = NULL; 415 char *pkcs11provider = NULL; 416 int i, ch, deleting = 0, ret = 0, key_only = 0; 417 418 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 419 sanitise_stdfd(); 420 421 __progname = ssh_get_progname(argv[0]); 422 seed_rng(); 423 424 OpenSSL_add_all_algorithms(); 425 426 /* At first, get a connection to the authentication agent. */ 427 ac = ssh_get_authentication_connection(); 428 if (ac == NULL) { 429 fprintf(stderr, 430 "Could not open a connection to your authentication agent.\n"); 431 exit(2); 432 } 433 while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) { 434 switch (ch) { 435 case 'k': 436 key_only = 1; 437 break; 438 case 'l': 439 case 'L': 440 if (list_identities(ac, ch == 'l' ? 1 : 0) == -1) 441 ret = 1; 442 goto done; 443 case 'x': 444 case 'X': 445 if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1) 446 ret = 1; 447 goto done; 448 case 'c': 449 confirm = 1; 450 break; 451 case 'd': 452 deleting = 1; 453 break; 454 case 'D': 455 if (delete_all(ac) == -1) 456 ret = 1; 457 goto done; 458 case 's': 459 pkcs11provider = optarg; 460 break; 461 case 'e': 462 deleting = 1; 463 pkcs11provider = optarg; 464 break; 465 case 't': 466 if ((lifetime = convtime(optarg)) == -1) { 467 fprintf(stderr, "Invalid lifetime\n"); 468 ret = 1; 469 goto done; 470 } 471 break; 472 default: 473 usage(); 474 ret = 1; 475 goto done; 476 } 477 } 478 argc -= optind; 479 argv += optind; 480 if (pkcs11provider != NULL) { 481 if (update_card(ac, !deleting, pkcs11provider) == -1) 482 ret = 1; 483 goto done; 484 } 485 if (argc == 0) { 486 char buf[MAXPATHLEN]; 487 struct passwd *pw; 488 struct stat st; 489 int count = 0; 490 491 if ((pw = getpwuid(getuid())) == NULL) { 492 fprintf(stderr, "No user found with uid %u\n", 493 (u_int)getuid()); 494 ret = 1; 495 goto done; 496 } 497 498 for (i = 0; default_files[i]; i++) { 499 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, 500 default_files[i]); 501 if (stat(buf, &st) < 0) 502 continue; 503 if (do_file(ac, deleting, key_only, buf) == -1) 504 ret = 1; 505 else 506 count++; 507 } 508 if (count == 0) 509 ret = 1; 510 } else { 511 for (i = 0; i < argc; i++) { 512 if (do_file(ac, deleting, key_only, argv[i]) == -1) 513 ret = 1; 514 } 515 } 516 clear_pass(); 517 518 done: 519 ssh_close_authentication_connection(ac); 520 return ret; 521 } 522