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