Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.169
1.169 ! djm 1: /* $OpenBSD: ssh-add.c,v 1.168 2023/07/06 22:17:59 dtucker 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.16 markus 40:
1.141 djm 41: #ifdef WITH_OPENSSL
1.19 markus 42: #include <openssl/evp.h>
1.141 djm 43: #endif
1.81 stevesk 44:
1.116 djm 45: #include <errno.h>
1.82 stevesk 46: #include <fcntl.h>
1.81 stevesk 47: #include <pwd.h>
1.88 stevesk 48: #include <stdio.h>
1.87 stevesk 49: #include <stdlib.h>
1.85 stevesk 50: #include <string.h>
1.146 naddy 51: #include <stdarg.h>
1.84 stevesk 52: #include <unistd.h>
1.117 deraadt 53: #include <limits.h>
1.1 deraadt 54:
1.89 deraadt 55: #include "xmalloc.h"
1.27 markus 56: #include "ssh.h"
57: #include "log.h"
1.116 djm 58: #include "sshkey.h"
59: #include "sshbuf.h"
1.18 markus 60: #include "authfd.h"
1.16 markus 61: #include "authfile.h"
1.25 markus 62: #include "pathnames.h"
1.57 stevesk 63: #include "misc.h"
1.110 djm 64: #include "ssherr.h"
1.115 djm 65: #include "digest.h"
1.148 djm 66: #include "ssh-sk.h"
1.157 djm 67: #include "sk-api.h"
1.162 djm 68: #include "hostfile.h"
1.1 deraadt 69:
1.45 jakob 70: /* argv0 */
71: extern char *__progname;
72:
1.48 djm 73: /* Default files to add */
74: static char *default_files[] = {
75: _PATH_SSH_CLIENT_ID_RSA,
1.99 djm 76: _PATH_SSH_CLIENT_ID_ECDSA,
1.143 djm 77: _PATH_SSH_CLIENT_ID_ECDSA_SK,
1.107 pascal 78: _PATH_SSH_CLIENT_ID_ED25519,
1.144 markus 79: _PATH_SSH_CLIENT_ID_ED25519_SK,
1.135 markus 80: _PATH_SSH_CLIENT_ID_XMSS,
1.165 dtucker 81: _PATH_SSH_CLIENT_ID_DSA,
1.48 djm 82: NULL
83: };
84:
1.115 djm 85: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
86:
1.56 markus 87: /* Default lifetime (0 == forever) */
1.159 dtucker 88: static int lifetime = 0;
1.48 djm 89:
1.65 markus 90: /* User has to confirm key use */
91: static int confirm = 0;
92:
1.135 markus 93: /* Maximum number of signatures (XMSS) */
94: static u_int maxsign = 0;
95: static u_int minleft = 0;
96:
1.124 tim 97: /* we keep a cache of one passphrase */
1.33 markus 98: static char *pass = NULL;
1.39 itojun 99: static void
1.33 markus 100: clear_pass(void)
101: {
102: if (pass) {
1.154 jsg 103: freezero(pass, strlen(pass));
1.33 markus 104: pass = NULL;
105: }
106: }
107:
1.46 djm 108: static int
1.156 djm 109: delete_one(int agent_fd, const struct sshkey *key, const char *comment,
110: const char *path, int qflag)
111: {
112: int r;
113:
114: if ((r = ssh_remove_identity(agent_fd, key)) != 0) {
115: fprintf(stderr, "Could not remove identity \"%s\": %s\n",
116: path, ssh_err(r));
117: return r;
118: }
119: if (!qflag) {
120: fprintf(stderr, "Identity removed: %s %s (%s)\n", path,
1.166 dtucker 121: sshkey_type(key), comment ? comment : "no comment");
1.156 djm 122: }
123: return 0;
124: }
125:
126: static int
1.169 ! djm 127: delete_stdin(int agent_fd, int qflag, int key_only, int cert_only)
1.156 djm 128: {
129: char *line = NULL, *cp;
130: size_t linesize = 0;
131: struct sshkey *key = NULL;
132: int lnum = 0, r, ret = -1;
133:
134: while (getline(&line, &linesize, stdin) != -1) {
135: lnum++;
136: sshkey_free(key);
137: key = NULL;
138: line[strcspn(line, "\n")] = '\0';
139: cp = line + strspn(line, " \t");
140: if (*cp == '#' || *cp == '\0')
141: continue;
142: if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
1.158 djm 143: fatal_f("sshkey_new");
1.156 djm 144: if ((r = sshkey_read(key, &cp)) != 0) {
1.158 djm 145: error_r(r, "(stdin):%d: invalid key", lnum);
1.156 djm 146: continue;
147: }
1.169 ! djm 148: if ((!key_only && !cert_only) ||
! 149: (key_only && !sshkey_is_cert(key)) ||
! 150: (cert_only && sshkey_is_cert(key))) {
! 151: if (delete_one(agent_fd, key, cp,
! 152: "(stdin)", qflag) == 0)
! 153: ret = 0;
! 154: }
1.156 djm 155: }
156: sshkey_free(key);
157: free(line);
158: return ret;
159: }
160:
161: static int
1.169 ! djm 162: delete_file(int agent_fd, const char *filename, int key_only,
! 163: int cert_only, int qflag)
1.1 deraadt 164: {
1.116 djm 165: struct sshkey *public, *cert = NULL;
1.104 djm 166: char *certpath = NULL, *comment = NULL;
1.116 djm 167: int r, ret = -1;
1.1 deraadt 168:
1.156 djm 169: if (strcmp(filename, "-") == 0)
1.169 ! djm 170: return delete_stdin(agent_fd, qflag, key_only, cert_only);
1.156 djm 171:
1.116 djm 172: if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
173: printf("Bad key file %s: %s\n", filename, ssh_err(r));
1.46 djm 174: return -1;
1.12 markus 175: }
1.169 ! djm 176: if ((!key_only && !cert_only) ||
! 177: (key_only && !sshkey_is_cert(public)) ||
! 178: (cert_only && sshkey_is_cert(public))) {
! 179: if (delete_one(agent_fd, public, comment, filename, qflag) == 0)
! 180: ret = 0;
! 181: }
1.46 djm 182:
1.104 djm 183: if (key_only)
184: goto out;
185:
186: /* Now try to delete the corresponding certificate too */
187: free(comment);
1.105 markus 188: comment = NULL;
1.104 djm 189: xasprintf(&certpath, "%s-cert.pub", filename);
1.120 halex 190: if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
191: if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
1.158 djm 192: error_r(r, "Failed to load certificate \"%s\"", certpath);
1.104 djm 193: goto out;
1.120 halex 194: }
195:
1.116 djm 196: if (!sshkey_equal_public(cert, public))
1.104 djm 197: fatal("Certificate %s does not match private key %s",
198: certpath, filename);
199:
1.156 djm 200: if (delete_one(agent_fd, cert, comment, certpath, qflag) == 0)
1.104 djm 201: ret = 0;
202:
203: out:
1.127 mmcc 204: sshkey_free(cert);
205: sshkey_free(public);
1.104 djm 206: free(certpath);
207: free(comment);
1.47 deraadt 208:
1.46 djm 209: return ret;
1.1 deraadt 210: }
211:
1.19 markus 212: /* Send a request to remove all identities. */
1.46 djm 213: static int
1.136 djm 214: delete_all(int agent_fd, int qflag)
1.1 deraadt 215: {
1.46 djm 216: int ret = -1;
1.19 markus 217:
1.130 djm 218: /*
219: * Since the agent might be forwarded, old or non-OpenSSH, when asked
220: * to remove all keys, attempt to remove both protocol v.1 and v.2
221: * keys.
222: */
1.121 markus 223: if (ssh_remove_all_identities(agent_fd, 2) == 0)
1.46 djm 224: ret = 0;
1.121 markus 225: /* ignore error-code for ssh1 */
226: ssh_remove_all_identities(agent_fd, 1);
1.19 markus 227:
1.136 djm 228: if (ret != 0)
229: fprintf(stderr, "Failed to remove all identities.\n");
230: else if (!qflag)
1.12 markus 231: fprintf(stderr, "All identities removed.\n");
1.46 djm 232:
233: return ret;
1.1 deraadt 234: }
235:
1.46 djm 236: static int
1.169 ! djm 237: add_file(int agent_fd, const char *filename, int key_only, int cert_only,
! 238: int qflag, const char *skprovider,
! 239: struct dest_constraint **dest_constraints,
1.162 djm 240: size_t ndest_constraints)
1.1 deraadt 241: {
1.116 djm 242: struct sshkey *private, *cert;
1.36 markus 243: char *comment = NULL;
1.102 djm 244: char msg[1024], *certpath = NULL;
1.116 djm 245: int r, fd, ret = -1;
1.135 markus 246: size_t i;
247: u_int32_t left;
1.116 djm 248: struct sshbuf *keyblob;
1.135 markus 249: struct ssh_identitylist *idlist;
1.12 markus 250:
1.101 djm 251: if (strcmp(filename, "-") == 0) {
252: fd = STDIN_FILENO;
253: filename = "(stdin)";
1.140 deraadt 254: } else if ((fd = open(filename, O_RDONLY)) == -1) {
1.19 markus 255: perror(filename);
1.46 djm 256: return -1;
1.19 markus 257: }
1.76 dtucker 258:
259: /*
260: * Since we'll try to load a keyfile multiple times, permission errors
261: * will occur multiple times, so check perms first and bail if wrong.
262: */
1.101 djm 263: if (fd != STDIN_FILENO) {
1.116 djm 264: if (sshkey_perm_ok(fd, filename) != 0) {
1.101 djm 265: close(fd);
266: return -1;
267: }
268: }
1.151 djm 269: if ((r = sshbuf_load_fd(fd, &keyblob)) != 0) {
1.116 djm 270: fprintf(stderr, "Error loading key \"%s\": %s\n",
271: filename, ssh_err(r));
272: sshbuf_free(keyblob);
1.101 djm 273: close(fd);
274: return -1;
275: }
1.76 dtucker 276: close(fd);
277:
1.12 markus 278: /* At first, try empty passphrase */
1.125 tim 279: if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
280: &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.116 djm 281: fprintf(stderr, "Error loading key \"%s\": %s\n",
282: filename, ssh_err(r));
283: goto fail_load;
284: }
1.33 markus 285: /* try last */
1.110 djm 286: if (private == NULL && pass != NULL) {
1.125 tim 287: if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
288: &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.116 djm 289: fprintf(stderr, "Error loading key \"%s\": %s\n",
290: filename, ssh_err(r));
291: goto fail_load;
292: }
1.110 djm 293: }
1.31 markus 294: if (private == NULL) {
1.33 markus 295: /* clear passphrase since it did not work */
296: clear_pass();
1.124 tim 297: snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
298: filename, confirm ? " (will confirm each use)" : "");
1.12 markus 299: for (;;) {
1.40 markus 300: pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.116 djm 301: if (strcmp(pass, "") == 0)
302: goto fail_load;
303: if ((r = sshkey_parse_private_fileblob(keyblob, pass,
1.125 tim 304: &private, &comment)) == 0)
1.116 djm 305: break;
306: else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
307: fprintf(stderr,
308: "Error loading key \"%s\": %s\n",
309: filename, ssh_err(r));
310: fail_load:
1.35 markus 311: clear_pass();
1.116 djm 312: sshbuf_free(keyblob);
1.46 djm 313: return -1;
1.12 markus 314: }
1.33 markus 315: clear_pass();
1.68 markus 316: snprintf(msg, sizeof msg,
1.124 tim 317: "Bad passphrase, try again for %s%s: ", filename,
1.119 halex 318: confirm ? " (will confirm each use)" : "");
1.12 markus 319: }
320: }
1.124 tim 321: if (comment == NULL || *comment == '\0')
322: comment = xstrdup(filename);
1.116 djm 323: sshbuf_free(keyblob);
1.60 markus 324:
1.135 markus 325: /* For XMSS */
326: if ((r = sshkey_set_filename(private, filename)) != 0) {
327: fprintf(stderr, "Could not add filename to private key: %s (%s)\n",
328: filename, comment);
329: goto out;
330: }
331: if (maxsign && minleft &&
332: (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) {
333: for (i = 0; i < idlist->nkeys; i++) {
334: if (!sshkey_equal_public(idlist->keys[i], private))
335: continue;
336: left = sshkey_signatures_left(idlist->keys[i]);
337: if (left < minleft) {
338: fprintf(stderr,
339: "Only %d signatures left.\n", left);
340: break;
341: }
342: fprintf(stderr, "Skipping update: ");
343: if (left == minleft) {
344: fprintf(stderr,
1.160 djm 345: "required signatures left (%d).\n", left);
1.135 markus 346: } else {
347: fprintf(stderr,
1.160 djm 348: "more signatures left (%d) than"
1.135 markus 349: " required (%d).\n", left, minleft);
350: }
351: ssh_free_identitylist(idlist);
352: goto out;
353: }
354: ssh_free_identitylist(idlist);
355: }
356:
1.157 djm 357: if (sshkey_is_sk(private)) {
358: if (skprovider == NULL) {
359: fprintf(stderr, "Cannot load FIDO key %s "
360: "without provider\n", filename);
361: goto out;
362: }
363: } else {
364: /* Don't send provider constraint for other keys */
365: skprovider = NULL;
1.143 djm 366: }
367:
1.169 ! djm 368: if (!cert_only &&
! 369: (r = ssh_add_identity_constrained(agent_fd, private, comment,
1.162 djm 370: lifetime, confirm, maxsign, skprovider,
371: dest_constraints, ndest_constraints)) == 0) {
1.60 markus 372: ret = 0;
1.136 djm 373: if (!qflag) {
374: fprintf(stderr, "Identity added: %s (%s)\n",
375: filename, comment);
376: if (lifetime != 0) {
377: fprintf(stderr,
1.159 dtucker 378: "Lifetime set to %d seconds\n", lifetime);
1.136 djm 379: }
380: if (confirm != 0) {
381: fprintf(stderr, "The user must confirm "
382: "each use of the key\n");
383: }
384: }
1.60 markus 385: } else {
1.116 djm 386: fprintf(stderr, "Could not add identity \"%s\": %s\n",
387: filename, ssh_err(r));
1.56 markus 388: }
389:
1.102 djm 390: /* Skip trying to load the cert if requested */
391: if (key_only)
392: goto out;
1.93 djm 393:
394: /* Now try to add the certificate flavour too */
395: xasprintf(&certpath, "%s-cert.pub", filename);
1.116 djm 396: if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
397: if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
1.169 ! djm 398: error_r(r, "Failed to load certificate \"%s\"",
! 399: certpath);
1.96 djm 400: goto out;
1.116 djm 401: }
1.96 djm 402:
1.116 djm 403: if (!sshkey_equal_public(cert, private)) {
1.96 djm 404: error("Certificate %s does not match private key %s",
405: certpath, filename);
1.116 djm 406: sshkey_free(cert);
1.96 djm 407: goto out;
1.166 dtucker 408: }
1.93 djm 409:
1.96 djm 410: /* Graft with private bits */
1.123 djm 411: if ((r = sshkey_to_certified(private)) != 0) {
1.158 djm 412: error_fr(r, "sshkey_to_certified");
1.116 djm 413: sshkey_free(cert);
414: goto out;
415: }
416: if ((r = sshkey_cert_copy(cert, private)) != 0) {
1.158 djm 417: error_fr(r, "sshkey_cert_copy");
1.116 djm 418: sshkey_free(cert);
1.96 djm 419: goto out;
1.94 otto 420: }
1.116 djm 421: sshkey_free(cert);
1.93 djm 422:
1.116 djm 423: if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
1.162 djm 424: lifetime, confirm, maxsign, skprovider,
425: dest_constraints, ndest_constraints)) != 0) {
1.158 djm 426: error_r(r, "Certificate %s (%s) add failed", certpath,
427: private->cert->key_id);
1.116 djm 428: goto out;
1.96 djm 429: }
1.136 djm 430: /* success */
431: if (!qflag) {
432: fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
433: private->cert->key_id);
434: if (lifetime != 0) {
1.159 dtucker 435: fprintf(stderr, "Lifetime set to %d seconds\n",
1.136 djm 436: lifetime);
437: }
438: if (confirm != 0) {
439: fprintf(stderr, "The user must confirm each use "
440: "of the key\n");
441: }
442: }
443:
1.96 djm 444: out:
1.116 djm 445: free(certpath);
1.106 djm 446: free(comment);
1.116 djm 447: sshkey_free(private);
1.47 deraadt 448:
1.46 djm 449: return ret;
1.1 deraadt 450: }
451:
1.46 djm 452: static int
1.162 djm 453: update_card(int agent_fd, int add, const char *id, int qflag,
1.169 ! djm 454: int key_only, int cert_only,
! 455: struct dest_constraint **dest_constraints, size_t ndest_constraints,
! 456: struct sshkey **certs, size_t ncerts)
1.42 markus 457: {
1.108 djm 458: char *pin = NULL;
1.116 djm 459: int r, ret = -1;
1.53 rees 460:
1.169 ! djm 461: if (key_only)
! 462: ncerts = 0;
! 463:
1.108 djm 464: if (add) {
465: if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
466: RP_ALLOW_STDIN)) == NULL)
467: return -1;
468: }
1.53 rees 469:
1.116 djm 470: if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
1.169 ! djm 471: lifetime, confirm, dest_constraints, ndest_constraints,
! 472: cert_only, certs, ncerts)) == 0) {
1.66 markus 473: ret = 0;
1.136 djm 474: if (!qflag) {
475: fprintf(stderr, "Card %s: %s\n",
476: add ? "added" : "removed", id);
477: }
1.46 djm 478: } else {
1.116 djm 479: fprintf(stderr, "Could not %s card \"%s\": %s\n",
480: add ? "add" : "remove", id, ssh_err(r));
1.66 markus 481: ret = -1;
1.46 djm 482: }
1.106 djm 483: free(pin);
1.66 markus 484: return ret;
1.42 markus 485: }
486:
1.50 markus 487: static int
1.137 djm 488: test_key(int agent_fd, const char *filename)
489: {
490: struct sshkey *key = NULL;
491: u_char *sig = NULL;
1.167 djm 492: const char *alg = NULL;
1.137 djm 493: size_t slen = 0;
494: int r, ret = -1;
495: char data[1024];
496:
497: if ((r = sshkey_load_public(filename, &key, NULL)) != 0) {
1.158 djm 498: error_r(r, "Couldn't read public key %s", filename);
1.137 djm 499: return -1;
500: }
1.167 djm 501: if (sshkey_type_plain(key->type) == KEY_RSA)
502: alg = "rsa-sha2-256";
1.137 djm 503: arc4random_buf(data, sizeof(data));
504: if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data),
1.167 djm 505: alg, 0)) != 0) {
1.158 djm 506: error_r(r, "Agent signature failed for %s", filename);
1.137 djm 507: goto done;
508: }
509: if ((r = sshkey_verify(key, sig, slen, data, sizeof(data),
1.167 djm 510: alg, 0, NULL)) != 0) {
1.158 djm 511: error_r(r, "Signature verification failed for %s", filename);
1.137 djm 512: goto done;
513: }
514: /* success */
515: ret = 0;
516: done:
517: free(sig);
518: sshkey_free(key);
519: return ret;
520: }
521:
522: static int
1.116 djm 523: list_identities(int agent_fd, int do_fp)
1.1 deraadt 524: {
1.116 djm 525: char *fp;
1.131 naddy 526: int r;
1.116 djm 527: struct ssh_identitylist *idlist;
1.135 markus 528: u_int32_t left;
1.116 djm 529: size_t i;
1.12 markus 530:
1.131 naddy 531: if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
532: if (r != SSH_ERR_AGENT_NO_IDENTITIES)
533: fprintf(stderr, "error fetching identities: %s\n",
534: ssh_err(r));
535: else
536: printf("The agent has no identities.\n");
537: return -1;
538: }
539: for (i = 0; i < idlist->nkeys; i++) {
540: if (do_fp) {
541: fp = sshkey_fingerprint(idlist->keys[i],
542: fingerprint_hash, SSH_FP_DEFAULT);
543: printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
544: fp == NULL ? "(null)" : fp, idlist->comments[i],
545: sshkey_type(idlist->keys[i]));
546: free(fp);
547: } else {
548: if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
549: fprintf(stderr, "sshkey_write: %s\n",
550: ssh_err(r));
551: continue;
1.12 markus 552: }
1.135 markus 553: fprintf(stdout, " %s", idlist->comments[i]);
554: left = sshkey_signatures_left(idlist->keys[i]);
555: if (left > 0)
556: fprintf(stdout,
557: " [signatures left %d]", left);
558: fprintf(stdout, "\n");
1.12 markus 559: }
1.50 markus 560: }
1.131 naddy 561: ssh_free_identitylist(idlist);
1.50 markus 562: return 0;
1.1 deraadt 563: }
564:
1.48 djm 565: static int
1.116 djm 566: lock_agent(int agent_fd, int lock)
1.54 markus 567: {
568: char prompt[100], *p1, *p2;
1.116 djm 569: int r, passok = 1, ret = -1;
1.61 deraadt 570:
1.54 markus 571: strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
572: p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
573: if (lock) {
574: strlcpy(prompt, "Again: ", sizeof prompt);
575: p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
576: if (strcmp(p1, p2) != 0) {
577: fprintf(stderr, "Passwords do not match.\n");
578: passok = 0;
579: }
1.154 jsg 580: freezero(p2, strlen(p2));
1.54 markus 581: }
1.116 djm 582: if (passok) {
583: if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
584: fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
585: ret = 0;
586: } else {
587: fprintf(stderr, "Failed to %slock agent: %s\n",
588: lock ? "" : "un", ssh_err(r));
589: }
590: }
1.154 jsg 591: freezero(p1, strlen(p1));
1.62 markus 592: return (ret);
1.54 markus 593: }
594:
595: static int
1.162 djm 596: load_resident_keys(int agent_fd, const char *skprovider, int qflag,
597: struct dest_constraint **dest_constraints, size_t ndest_constraints)
1.148 djm 598: {
1.161 djm 599: struct sshsk_resident_key **srks;
600: size_t nsrks, i;
601: struct sshkey *key;
1.148 djm 602: int r, ok = 0;
603: char *fp;
604:
1.152 naddy 605: pass = read_passphrase("Enter PIN for authenticator: ", RP_ALLOW_STDIN);
1.161 djm 606: if ((r = sshsk_load_resident(skprovider, NULL, pass, 0,
607: &srks, &nsrks)) != 0) {
1.158 djm 608: error_r(r, "Unable to load resident keys");
1.148 djm 609: return r;
610: }
1.161 djm 611: for (i = 0; i < nsrks; i++) {
612: key = srks[i]->key;
613: if ((fp = sshkey_fingerprint(key,
1.148 djm 614: fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.158 djm 615: fatal_f("sshkey_fingerprint failed");
1.161 djm 616: if ((r = ssh_add_identity_constrained(agent_fd, key, "",
1.162 djm 617: lifetime, confirm, maxsign, skprovider,
618: dest_constraints, ndest_constraints)) != 0) {
619: error("Unable to add key %s %s",
620: sshkey_type(key), fp);
1.148 djm 621: free(fp);
622: ok = r;
623: continue;
624: }
625: if (ok == 0)
626: ok = 1;
627: if (!qflag) {
628: fprintf(stderr, "Resident identity added: %s %s\n",
1.161 djm 629: sshkey_type(key), fp);
1.148 djm 630: if (lifetime != 0) {
631: fprintf(stderr,
1.159 dtucker 632: "Lifetime set to %d seconds\n", lifetime);
1.148 djm 633: }
634: if (confirm != 0) {
635: fprintf(stderr, "The user must confirm "
636: "each use of the key\n");
637: }
638: }
639: free(fp);
640: }
1.161 djm 641: sshsk_free_resident_keys(srks, nsrks);
642: if (nsrks == 0)
1.148 djm 643: return SSH_ERR_KEY_NOT_FOUND;
644: return ok == 1 ? 0 : ok;
645: }
646:
647: static int
1.169 ! djm 648: do_file(int agent_fd, int deleting, int key_only, int cert_only,
! 649: char *file, int qflag, const char *skprovider,
! 650: struct dest_constraint **dest_constraints, size_t ndest_constraints)
1.48 djm 651: {
652: if (deleting) {
1.169 ! djm 653: if (delete_file(agent_fd, file, key_only,
! 654: cert_only, qflag) == -1)
1.48 djm 655: return -1;
656: } else {
1.169 ! djm 657: if (add_file(agent_fd, file, key_only, cert_only, qflag,
! 658: skprovider, dest_constraints, ndest_constraints) == -1)
1.48 djm 659: return -1;
660: }
661: return 0;
662: }
663:
1.162 djm 664: /* Append string 's' to a NULL-terminated array of strings */
665: static void
666: stringlist_append(char ***listp, const char *s)
667: {
668: size_t i = 0;
669:
670: if (*listp == NULL)
671: *listp = xcalloc(2, sizeof(**listp));
672: else {
673: for (i = 0; (*listp)[i] != NULL; i++)
674: ; /* count */
675: *listp = xrecallocarray(*listp, i + 1, i + 2, sizeof(**listp));
676: }
677: (*listp)[i] = xstrdup(s);
678: }
679:
680: static void
681: parse_dest_constraint_hop(const char *s, struct dest_constraint_hop *dch,
682: char **hostkey_files)
683: {
684: char *user = NULL, *host, *os, *path;
685: size_t i;
686: struct hostkeys *hostkeys;
687: const struct hostkey_entry *hke;
688: int r, want_ca;
689:
690: memset(dch, '\0', sizeof(*dch));
691: os = xstrdup(s);
692: if ((host = strchr(os, '@')) == NULL)
693: host = os;
694: else {
695: *host++ = '\0';
696: user = os;
697: }
698: cleanhostname(host);
699: /* Trivial case: username@ (all hosts) */
700: if (*host == '\0') {
701: if (user == NULL) {
702: fatal("Invalid key destination constraint \"%s\": "
703: "does not specify user or host", s);
704: }
705: dch->user = xstrdup(user);
706: /* other fields left blank */
707: free(os);
708: return;
709: }
710: if (hostkey_files == NULL)
711: fatal_f("no hostkey files");
712: /* Otherwise we need to look up the keys for this hostname */
713: hostkeys = init_hostkeys();
714: for (i = 0; hostkey_files[i]; i++) {
715: path = tilde_expand_filename(hostkey_files[i], getuid());
716: debug2_f("looking up host keys for \"%s\" in %s", host, path);
717: load_hostkeys(hostkeys, host, path, 0);
718: free(path);
719: }
720: dch->user = user == NULL ? NULL : xstrdup(user);
721: dch->hostname = xstrdup(host);
722: for (i = 0; i < hostkeys->num_entries; i++) {
723: hke = hostkeys->entries + i;
724: want_ca = hke->marker == MRK_CA;
725: if (hke->marker != MRK_NONE && !want_ca)
726: continue;
727: debug3_f("%s%s%s: adding %s %skey from %s:%lu as key %u",
728: user == NULL ? "": user, user == NULL ? "" : "@",
729: host, sshkey_type(hke->key), want_ca ? "CA " : "",
730: hke->file, hke->line, dch->nkeys);
731: dch->keys = xrecallocarray(dch->keys, dch->nkeys,
732: dch->nkeys + 1, sizeof(*dch->keys));
733: dch->key_is_ca = xrecallocarray(dch->key_is_ca, dch->nkeys,
734: dch->nkeys + 1, sizeof(*dch->key_is_ca));
735: if ((r = sshkey_from_private(hke->key,
736: &(dch->keys[dch->nkeys]))) != 0)
737: fatal_fr(r, "sshkey_from_private");
738: dch->key_is_ca[dch->nkeys] = want_ca;
739: dch->nkeys++;
740: }
741: if (dch->nkeys == 0)
742: fatal("No host keys found for destination \"%s\"", host);
743: free_hostkeys(hostkeys);
744: free(os);
745: return;
746: }
747:
748: static void
749: parse_dest_constraint(const char *s, struct dest_constraint ***dcp,
750: size_t *ndcp, char **hostkey_files)
751: {
752: struct dest_constraint *dc;
753: char *os, *cp;
754:
755: dc = xcalloc(1, sizeof(*dc));
756: os = xstrdup(s);
757: if ((cp = strchr(os, '>')) == NULL) {
758: /* initial hop; no 'from' hop specified */
759: parse_dest_constraint_hop(os, &dc->to, hostkey_files);
760: } else {
761: /* two hops specified */
762: *(cp++) = '\0';
763: parse_dest_constraint_hop(os, &dc->from, hostkey_files);
764: parse_dest_constraint_hop(cp, &dc->to, hostkey_files);
765: if (dc->from.user != NULL) {
766: fatal("Invalid key constraint %s: cannot specify "
767: "user on 'from' host", os);
768: }
769: }
770: /* XXX eliminate or error on duplicates */
771: debug2_f("constraint %zu: %s%s%s (%u keys) > %s%s%s (%u keys)", *ndcp,
772: dc->from.user ? dc->from.user : "", dc->from.user ? "@" : "",
773: dc->from.hostname ? dc->from.hostname : "(ORIGIN)", dc->from.nkeys,
774: dc->to.user ? dc->to.user : "", dc->to.user ? "@" : "",
775: dc->to.hostname ? dc->to.hostname : "(ANY)", dc->to.nkeys);
776: *dcp = xrecallocarray(*dcp, *ndcp, *ndcp + 1, sizeof(**dcp));
777: (*dcp)[(*ndcp)++] = dc;
778: free(os);
779: }
780:
781:
1.42 markus 782: static void
783: usage(void)
784: {
1.150 naddy 785: fprintf(stderr,
1.163 jmc 786: "usage: ssh-add [-cDdKkLlqvXx] [-E fingerprint_hash] [-H hostkey_file]\n"
787: " [-h destination_constraint] [-S provider] [-t life]\n"
1.150 naddy 788: #ifdef WITH_XMSS
789: " [-M maxsign] [-m minleft]\n"
790: #endif
791: " [file ...]\n"
792: " ssh-add -s pkcs11\n"
793: " ssh-add -e pkcs11\n"
794: " ssh-add -T pubkey ...\n"
795: );
1.42 markus 796: }
797:
1.2 provos 798: int
1.7 markus 799: main(int argc, char **argv)
1.1 deraadt 800: {
1.43 markus 801: extern char *optarg;
802: extern int optind;
1.116 djm 803: int agent_fd;
1.143 djm 804: char *pkcs11provider = NULL, *skprovider = NULL;
1.162 djm 805: char **dest_constraint_strings = NULL, **hostkey_files = NULL;
1.169 ! djm 806: int r, i, ch, deleting = 0, ret = 0, key_only = 0, cert_only = 0;
! 807: int do_download = 0, xflag = 0, lflag = 0, Dflag = 0;
! 808: int qflag = 0, Tflag = 0;
1.138 djm 809: SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
810: LogLevel log_level = SYSLOG_LEVEL_INFO;
1.169 ! djm 811: struct sshkey *k, **certs = NULL;
1.162 djm 812: struct dest_constraint **dest_constraints = NULL;
1.169 ! djm 813: size_t ndest_constraints = 0i, ncerts = 0;
1.73 djm 814:
815: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
816: sanitise_stdfd();
1.12 markus 817:
1.141 djm 818: #ifdef WITH_OPENSSL
1.100 djm 819: OpenSSL_add_all_algorithms();
1.141 djm 820: #endif
1.138 djm 821: log_init(__progname, log_level, log_facility, 1);
822:
1.114 millert 823: setvbuf(stdout, NULL, _IOLBF, 0);
1.19 markus 824:
1.116 djm 825: /* First, get a connection to the authentication agent. */
826: switch (r = ssh_get_authentication_socket(&agent_fd)) {
827: case 0:
828: break;
829: case SSH_ERR_AGENT_NOT_PRESENT:
830: fprintf(stderr, "Could not open a connection to your "
831: "authentication agent.\n");
832: exit(2);
833: default:
834: fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50 markus 835: exit(2);
1.12 markus 836: }
1.116 djm 837:
1.143 djm 838: skprovider = getenv("SSH_SK_PROVIDER");
839:
1.169 ! djm 840: while ((ch = getopt(argc, argv, "vkKlLCcdDTxXE:e:h:H:M:m:qs:S:t:")) != -1) {
1.43 markus 841: switch (ch) {
1.138 djm 842: case 'v':
843: if (log_level == SYSLOG_LEVEL_INFO)
844: log_level = SYSLOG_LEVEL_DEBUG1;
845: else if (log_level < SYSLOG_LEVEL_DEBUG3)
846: log_level++;
847: break;
1.115 djm 848: case 'E':
849: fingerprint_hash = ssh_digest_alg_by_name(optarg);
850: if (fingerprint_hash == -1)
851: fatal("Invalid hash algorithm \"%s\"", optarg);
852: break;
1.162 djm 853: case 'H':
854: stringlist_append(&hostkey_files, optarg);
855: break;
856: case 'h':
857: stringlist_append(&dest_constraint_strings, optarg);
858: break;
1.102 djm 859: case 'k':
860: key_only = 1;
861: break;
1.169 ! djm 862: case 'C':
! 863: cert_only = 1;
! 864: break;
1.150 naddy 865: case 'K':
866: do_download = 1;
867: break;
1.43 markus 868: case 'l':
869: case 'L':
1.115 djm 870: if (lflag != 0)
871: fatal("-%c flag already specified", lflag);
872: lflag = ch;
1.148 djm 873: break;
1.54 markus 874: case 'x':
875: case 'X':
1.115 djm 876: if (xflag != 0)
877: fatal("-%c flag already specified", xflag);
878: xflag = ch;
879: break;
1.65 markus 880: case 'c':
881: confirm = 1;
1.135 markus 882: break;
883: case 'm':
1.168 dtucker 884: minleft = (u_int)strtonum(optarg, 1, UINT_MAX, NULL);
1.135 markus 885: if (minleft == 0) {
886: usage();
887: ret = 1;
888: goto done;
889: }
890: break;
891: case 'M':
1.168 dtucker 892: maxsign = (u_int)strtonum(optarg, 1, UINT_MAX, NULL);
1.135 markus 893: if (maxsign == 0) {
894: usage();
895: ret = 1;
896: goto done;
897: }
1.43 markus 898: break;
899: case 'd':
1.12 markus 900: deleting = 1;
1.43 markus 901: break;
902: case 'D':
1.115 djm 903: Dflag = 1;
904: break;
1.43 markus 905: case 's':
1.92 markus 906: pkcs11provider = optarg;
1.43 markus 907: break;
1.143 djm 908: case 'S':
909: skprovider = optarg;
910: break;
1.43 markus 911: case 'e':
1.47 deraadt 912: deleting = 1;
1.92 markus 913: pkcs11provider = optarg;
1.56 markus 914: break;
915: case 't':
1.153 dtucker 916: if ((lifetime = convtime(optarg)) == -1 ||
1.155 dtucker 917: lifetime < 0 || (u_long)lifetime > UINT32_MAX) {
1.57 stevesk 918: fprintf(stderr, "Invalid lifetime\n");
919: ret = 1;
920: goto done;
921: }
1.43 markus 922: break;
1.134 dlg 923: case 'q':
924: qflag = 1;
925: break;
1.137 djm 926: case 'T':
927: Tflag = 1;
928: break;
1.43 markus 929: default:
930: usage();
1.46 djm 931: ret = 1;
932: goto done;
1.42 markus 933: }
934: }
1.138 djm 935: log_init(__progname, log_level, log_facility, 1);
1.115 djm 936:
937: if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
938: fatal("Invalid combination of actions");
939: else if (xflag) {
1.116 djm 940: if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115 djm 941: ret = 1;
942: goto done;
943: } else if (lflag) {
1.116 djm 944: if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115 djm 945: ret = 1;
946: goto done;
947: } else if (Dflag) {
1.136 djm 948: if (delete_all(agent_fd, qflag) == -1)
1.115 djm 949: ret = 1;
950: goto done;
951: }
1.145 djm 952:
953: if (skprovider == NULL)
954: skprovider = "internal";
1.162 djm 955: if (hostkey_files == NULL) {
956: /* use defaults from readconf.c */
957: stringlist_append(&hostkey_files, _PATH_SSH_USER_HOSTFILE);
958: stringlist_append(&hostkey_files, _PATH_SSH_USER_HOSTFILE2);
959: stringlist_append(&hostkey_files, _PATH_SSH_SYSTEM_HOSTFILE);
960: stringlist_append(&hostkey_files, _PATH_SSH_SYSTEM_HOSTFILE2);
961: }
962: if (dest_constraint_strings != NULL) {
963: for (i = 0; dest_constraint_strings[i] != NULL; i++) {
964: parse_dest_constraint(dest_constraint_strings[i],
965: &dest_constraints, &ndest_constraints, hostkey_files);
966: }
967: }
1.115 djm 968:
1.43 markus 969: argc -= optind;
970: argv += optind;
1.137 djm 971: if (Tflag) {
972: if (argc <= 0)
973: fatal("no keys to test");
974: for (r = i = 0; i < argc; i++)
975: r |= test_key(agent_fd, argv[i]);
976: ret = r == 0 ? 0 : 1;
977: goto done;
978: }
1.92 markus 979: if (pkcs11provider != NULL) {
1.169 ! djm 980: for (i = 0; i < argc; i++) {
! 981: if ((r = sshkey_load_public(argv[i], &k, NULL)) != 0)
! 982: fatal_fr(r, "load certificate %s", argv[i]);
! 983: certs = xrecallocarray(certs, ncerts, ncerts + 1,
! 984: sizeof(*certs));
! 985: debug2("%s: %s", argv[i], sshkey_ssh_name(k));
! 986: certs[ncerts++] = k;
! 987: }
! 988: debug2_f("loaded %zu certificates", ncerts);
1.136 djm 989: if (update_card(agent_fd, !deleting, pkcs11provider,
1.169 ! djm 990: qflag, key_only, cert_only,
! 991: dest_constraints, ndest_constraints,
! 992: certs, ncerts) == -1)
1.46 djm 993: ret = 1;
1.43 markus 994: goto done;
1.12 markus 995: }
1.148 djm 996: if (do_download) {
997: if (skprovider == NULL)
998: fatal("Cannot download keys without provider");
1.162 djm 999: if (load_resident_keys(agent_fd, skprovider, qflag,
1000: dest_constraints, ndest_constraints) != 0)
1.148 djm 1001: ret = 1;
1002: goto done;
1003: }
1.43 markus 1004: if (argc == 0) {
1.117 deraadt 1005: char buf[PATH_MAX];
1.48 djm 1006: struct passwd *pw;
1.52 markus 1007: struct stat st;
1008: int count = 0;
1.48 djm 1009:
1010: if ((pw = getpwuid(getuid())) == NULL) {
1.20 deraadt 1011: fprintf(stderr, "No user found with uid %u\n",
1012: (u_int)getuid());
1.46 djm 1013: ret = 1;
1014: goto done;
1.12 markus 1015: }
1.48 djm 1016:
1.71 deraadt 1017: for (i = 0; default_files[i]; i++) {
1.51 markus 1018: snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48 djm 1019: default_files[i]);
1.140 deraadt 1020: if (stat(buf, &st) == -1)
1.52 markus 1021: continue;
1.169 ! djm 1022: if (do_file(agent_fd, deleting, key_only, cert_only,
! 1023: buf, qflag, skprovider,
1.162 djm 1024: dest_constraints, ndest_constraints) == -1)
1.46 djm 1025: ret = 1;
1.52 markus 1026: else
1027: count++;
1.46 djm 1028: }
1.52 markus 1029: if (count == 0)
1030: ret = 1;
1.43 markus 1031: } else {
1.71 deraadt 1032: for (i = 0; i < argc; i++) {
1.169 ! djm 1033: if (do_file(agent_fd, deleting, key_only, cert_only,
1.162 djm 1034: argv[i], qflag, skprovider,
1035: dest_constraints, ndest_constraints) == -1)
1.48 djm 1036: ret = 1;
1.43 markus 1037: }
1.12 markus 1038: }
1.148 djm 1039: done:
1.33 markus 1040: clear_pass();
1.116 djm 1041: ssh_close_authentication_socket(agent_fd);
1.46 djm 1042: return ret;
1.1 deraadt 1043: }