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