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