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