Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.107
1.107 ! pascal 1: /* $OpenBSD: ssh-add.c,v 1.106 2013/05/17 00:13:14 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) {
87: memset(pass, 0, 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.53 rees 290: char *pin;
1.66 markus 291: int ret = -1;
1.53 rees 292:
1.92 markus 293: pin = read_passphrase("Enter passphrase for PKCS#11: ", RP_ALLOW_STDIN);
1.53 rees 294: if (pin == NULL)
295: return -1;
296:
1.67 djm 297: if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
1.44 markus 298: fprintf(stderr, "Card %s: %s\n",
1.47 deraadt 299: add ? "added" : "removed", id);
1.66 markus 300: ret = 0;
1.46 djm 301: } else {
1.44 markus 302: fprintf(stderr, "Could not %s card: %s\n",
1.47 deraadt 303: add ? "add" : "remove", id);
1.66 markus 304: ret = -1;
1.46 djm 305: }
1.106 djm 306: free(pin);
1.66 markus 307: return ret;
1.42 markus 308: }
309:
1.50 markus 310: static int
1.30 markus 311: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 312: {
1.19 markus 313: Key *key;
1.30 markus 314: char *comment, *fp;
1.19 markus 315: int had_identities = 0;
316: int version;
1.12 markus 317:
1.19 markus 318: for (version = 1; version <= 2; version++) {
319: for (key = ssh_get_first_identity(ac, &comment, version);
1.47 deraadt 320: key != NULL;
321: key = ssh_get_next_identity(ac, &comment, version)) {
1.19 markus 322: had_identities = 1;
1.30 markus 323: if (do_fp) {
324: fp = key_fingerprint(key, SSH_FP_MD5,
325: SSH_FP_HEX);
1.23 markus 326: printf("%d %s %s (%s)\n",
1.30 markus 327: key_size(key), fp, comment, key_type(key));
1.106 djm 328: free(fp);
1.12 markus 329: } else {
1.19 markus 330: if (!key_write(key, stdout))
331: fprintf(stderr, "key_write failed");
332: fprintf(stdout, " %s\n", comment);
1.12 markus 333: }
1.19 markus 334: key_free(key);
1.106 djm 335: free(comment);
1.12 markus 336: }
337: }
1.50 markus 338: if (!had_identities) {
1.12 markus 339: printf("The agent has no identities.\n");
1.50 markus 340: return -1;
341: }
342: return 0;
1.1 deraadt 343: }
344:
1.48 djm 345: static int
1.54 markus 346: lock_agent(AuthenticationConnection *ac, int lock)
347: {
348: char prompt[100], *p1, *p2;
349: int passok = 1, ret = -1;
1.61 deraadt 350:
1.54 markus 351: strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
352: p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
353: if (lock) {
354: strlcpy(prompt, "Again: ", sizeof prompt);
355: p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
356: if (strcmp(p1, p2) != 0) {
357: fprintf(stderr, "Passwords do not match.\n");
358: passok = 0;
359: }
360: memset(p2, 0, strlen(p2));
1.106 djm 361: free(p2);
1.54 markus 362: }
363: if (passok && ssh_lock_agent(ac, lock, p1)) {
364: fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
365: ret = 0;
366: } else
367: fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
368: memset(p1, 0, strlen(p1));
1.106 djm 369: free(p1);
1.62 markus 370: return (ret);
1.54 markus 371: }
372:
373: static int
1.102 djm 374: do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
1.48 djm 375: {
376: if (deleting) {
1.104 djm 377: if (delete_file(ac, file, key_only) == -1)
1.48 djm 378: return -1;
379: } else {
1.102 djm 380: if (add_file(ac, file, key_only) == -1)
1.48 djm 381: return -1;
382: }
383: return 0;
384: }
385:
1.42 markus 386: static void
387: usage(void)
388: {
1.90 sobrado 389: fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45 jakob 390: fprintf(stderr, "Options:\n");
391: fprintf(stderr, " -l List fingerprints of all identities.\n");
392: fprintf(stderr, " -L List public key parameters of all identities.\n");
1.103 djm 393: fprintf(stderr, " -k Load only keys and not certificates.\n");
394: fprintf(stderr, " -c Require confirmation to sign using identities\n");
395: fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
1.45 jakob 396: fprintf(stderr, " -d Delete identity.\n");
397: fprintf(stderr, " -D Delete all identities.\n");
1.55 markus 398: fprintf(stderr, " -x Lock agent.\n");
1.63 markus 399: fprintf(stderr, " -X Unlock agent.\n");
1.92 markus 400: fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
401: fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
1.42 markus 402: }
403:
1.2 provos 404: int
1.7 markus 405: main(int argc, char **argv)
1.1 deraadt 406: {
1.43 markus 407: extern char *optarg;
408: extern int optind;
1.12 markus 409: AuthenticationConnection *ac = NULL;
1.92 markus 410: char *pkcs11provider = NULL;
1.102 djm 411: int i, ch, deleting = 0, ret = 0, key_only = 0;
1.73 djm 412:
413: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
414: sanitise_stdfd();
1.12 markus 415:
1.100 djm 416: OpenSSL_add_all_algorithms();
1.19 markus 417:
1.12 markus 418: /* At first, get a connection to the authentication agent. */
419: ac = ssh_get_authentication_connection();
420: if (ac == NULL) {
1.74 deraadt 421: fprintf(stderr,
422: "Could not open a connection to your authentication agent.\n");
1.50 markus 423: exit(2);
1.12 markus 424: }
1.102 djm 425: while ((ch = getopt(argc, argv, "klLcdDxXe:s:t:")) != -1) {
1.43 markus 426: switch (ch) {
1.102 djm 427: case 'k':
428: key_only = 1;
429: break;
1.43 markus 430: case 'l':
431: case 'L':
1.50 markus 432: if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
1.54 markus 433: ret = 1;
434: goto done;
435: case 'x':
436: case 'X':
437: if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
1.50 markus 438: ret = 1;
1.43 markus 439: goto done;
1.65 markus 440: case 'c':
441: confirm = 1;
1.43 markus 442: break;
443: case 'd':
1.12 markus 444: deleting = 1;
1.43 markus 445: break;
446: case 'D':
1.46 djm 447: if (delete_all(ac) == -1)
448: ret = 1;
1.43 markus 449: goto done;
450: case 's':
1.92 markus 451: pkcs11provider = optarg;
1.43 markus 452: break;
453: case 'e':
1.47 deraadt 454: deleting = 1;
1.92 markus 455: pkcs11provider = optarg;
1.56 markus 456: break;
457: case 't':
1.57 stevesk 458: if ((lifetime = convtime(optarg)) == -1) {
459: fprintf(stderr, "Invalid lifetime\n");
460: ret = 1;
461: goto done;
462: }
1.43 markus 463: break;
464: default:
465: usage();
1.46 djm 466: ret = 1;
467: goto done;
1.42 markus 468: }
469: }
1.43 markus 470: argc -= optind;
471: argv += optind;
1.92 markus 472: if (pkcs11provider != NULL) {
473: if (update_card(ac, !deleting, pkcs11provider) == -1)
1.46 djm 474: ret = 1;
1.43 markus 475: goto done;
1.12 markus 476: }
1.43 markus 477: if (argc == 0) {
1.48 djm 478: char buf[MAXPATHLEN];
479: struct passwd *pw;
1.52 markus 480: struct stat st;
481: int count = 0;
1.48 djm 482:
483: if ((pw = getpwuid(getuid())) == NULL) {
1.20 deraadt 484: fprintf(stderr, "No user found with uid %u\n",
485: (u_int)getuid());
1.46 djm 486: ret = 1;
487: goto done;
1.12 markus 488: }
1.48 djm 489:
1.71 deraadt 490: for (i = 0; default_files[i]; i++) {
1.51 markus 491: snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48 djm 492: default_files[i]);
1.52 markus 493: if (stat(buf, &st) < 0)
494: continue;
1.102 djm 495: if (do_file(ac, deleting, key_only, buf) == -1)
1.46 djm 496: ret = 1;
1.52 markus 497: else
498: count++;
1.46 djm 499: }
1.52 markus 500: if (count == 0)
501: ret = 1;
1.43 markus 502: } else {
1.71 deraadt 503: for (i = 0; i < argc; i++) {
1.102 djm 504: if (do_file(ac, deleting, key_only, argv[i]) == -1)
1.48 djm 505: ret = 1;
1.43 markus 506: }
1.12 markus 507: }
1.33 markus 508: clear_pass();
1.43 markus 509:
510: done:
1.12 markus 511: ssh_close_authentication_connection(ac);
1.46 djm 512: return ret;
1.1 deraadt 513: }