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