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