ssh-add.c revision 255767
1198892Srdivacky/* $OpenBSD: ssh-add.c,v 1.106 2013/05/17 00:13:14 djm Exp $ */ 2198892Srdivacky/* 3198892Srdivacky * Author: Tatu Ylonen <ylo@cs.hut.fi> 4198892Srdivacky * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5198892Srdivacky * All rights reserved 6198892Srdivacky * Adds an identity to the authentication server, or removes an identity. 7198892Srdivacky * 8198892Srdivacky * As far as I am concerned, the code I have written for this software 9198892Srdivacky * can be used freely for any purpose. Any derived versions of this 10198892Srdivacky * software must be clearly marked as such, and if the derived work is 11198892Srdivacky * incompatible with the protocol description in the RFC file, it must be 12198892Srdivacky * called by a name other than "ssh" or "Secure Shell". 13198892Srdivacky * 14198892Srdivacky * SSH2 implementation, 15198892Srdivacky * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 16198892Srdivacky * 17198892Srdivacky * Redistribution and use in source and binary forms, with or without 18198892Srdivacky * modification, are permitted provided that the following conditions 19198892Srdivacky * are met: 20198892Srdivacky * 1. Redistributions of source code must retain the above copyright 21198892Srdivacky * notice, this list of conditions and the following disclaimer. 22198892Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 23198892Srdivacky * notice, this list of conditions and the following disclaimer in the 24198892Srdivacky * documentation and/or other materials provided with the distribution. 25198892Srdivacky * 26198892Srdivacky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27199989Srdivacky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28198892Srdivacky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29198892Srdivacky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30210299Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31210299Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32210299Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33198892Srdivacky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34198892Srdivacky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35198892Srdivacky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36210299Sed */ 37198892Srdivacky 38198892Srdivacky#include "includes.h" 39198892Srdivacky 40198892Srdivacky#include <sys/types.h> 41198892Srdivacky#include <sys/stat.h> 42198892Srdivacky#include <sys/param.h> 43198892Srdivacky 44198892Srdivacky#include <openssl/evp.h> 45198892Srdivacky#include "openbsd-compat/openssl-compat.h" 46198892Srdivacky 47198892Srdivacky#include <fcntl.h> 48198892Srdivacky#include <pwd.h> 49198892Srdivacky#include <stdarg.h> 50198892Srdivacky#include <stdio.h> 51198892Srdivacky#include <stdlib.h> 52198892Srdivacky#include <string.h> 53198892Srdivacky#include <unistd.h> 54198892Srdivacky 55198892Srdivacky#include "xmalloc.h" 56198892Srdivacky#include "ssh.h" 57198892Srdivacky#include "rsa.h" 58198892Srdivacky#include "log.h" 59198892Srdivacky#include "key.h" 60198892Srdivacky#include "buffer.h" 61198892Srdivacky#include "authfd.h" 62198892Srdivacky#include "authfile.h" 63198892Srdivacky#include "pathnames.h" 64198892Srdivacky#include "misc.h" 65198892Srdivacky 66198892Srdivacky/* argv0 */ 67198892Srdivackyextern char *__progname; 68198892Srdivacky 69198892Srdivacky/* Default files to add */ 70202375Srdivackystatic char *default_files[] = { 71198892Srdivacky _PATH_SSH_CLIENT_ID_RSA, 72198892Srdivacky _PATH_SSH_CLIENT_ID_DSA, 73198892Srdivacky#ifdef OPENSSL_HAS_ECC 74202375Srdivacky _PATH_SSH_CLIENT_ID_ECDSA, 75202375Srdivacky#endif 76198892Srdivacky _PATH_SSH_CLIENT_IDENTITY, 77198892Srdivacky NULL 78207618Srdivacky}; 79207618Srdivacky 80207618Srdivacky/* Default lifetime (0 == forever) */ 81198892Srdivackystatic int lifetime = 0; 82198892Srdivacky 83198892Srdivacky/* User has to confirm key use */ 84198892Srdivackystatic int confirm = 0; 85198892Srdivacky 86198892Srdivacky/* we keep a cache of one passphrases */ 87198892Srdivackystatic char *pass = NULL; 88198892Srdivackystatic void 89198892Srdivackyclear_pass(void) 90198892Srdivacky{ 91198892Srdivacky if (pass) { 92198892Srdivacky memset(pass, 0, strlen(pass)); 93198892Srdivacky free(pass); 94202375Srdivacky pass = NULL; 95202375Srdivacky } 96198892Srdivacky} 97198892Srdivacky 98198892Srdivackystatic int 99198892Srdivackydelete_file(AuthenticationConnection *ac, const char *filename, int key_only) 100198892Srdivacky{ 101198892Srdivacky 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. */ 148static int 149delete_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 166static int 167add_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 free(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 free(certpath); 286 free(comment); 287 key_free(private); 288 289 return ret; 290} 291 292static int 293update_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 free(pin); 312 return ret; 313} 314 315static int 316list_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 free(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 free(comment); 341 } 342 } 343 if (!had_identities) { 344 printf("The agent has no identities.\n"); 345 return -1; 346 } 347 return 0; 348} 349 350static int 351lock_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 free(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 free(p1); 375 return (ret); 376} 377 378static int 379do_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 391static void 392usage(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 409int 410main(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 518done: 519 ssh_close_authentication_connection(ac); 520 return ret; 521} 522