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