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