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