blob: adcc459987eb0468945cc9a7ce21e051473be590 [file] [log] [blame]
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00001/* $OpenBSD: ssh-add.c,v 1.135 2018/02/23 15:58:37 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Adds an identity to the authentication server, or removes an identity.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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 *
Damien Millerad833b32000-08-23 10:46:23 +100014 * SSH2 implementation,
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
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.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110039
40#include <sys/types.h>
41#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100042
Damien Millerad833b32000-08-23 10:46:23 +100043#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110044#include "openbsd-compat/openssl-compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
djm@openbsd.org141efe42015-01-14 20:05:27 +000046#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100047#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100048#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100049#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100050#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100051#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100053#include <unistd.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000054#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100055
Damien Millerd7834352006-08-05 12:39:39 +100056#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000059#include "sshkey.h"
60#include "sshbuf.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100062#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000064#include "misc.h"
Damien Miller86687062014-07-02 15:28:02 +100065#include "ssherr.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000066#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Ben Lindstromddfb1e32001-08-06 22:06:35 +000068/* argv0 */
69extern char *__progname;
70
Damien Miller6e1057c2002-01-22 23:05:59 +110071/* Default files to add */
72static char *default_files[] = {
Damien Miller72ef7c12015-01-15 02:21:31 +110073#ifdef WITH_OPENSSL
Damien Miller6e1057c2002-01-22 23:05:59 +110074 _PATH_SSH_CLIENT_ID_RSA,
75 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110076#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100077 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110078#endif
Damien Miller72ef7c12015-01-15 02:21:31 +110079#endif /* WITH_OPENSSL */
Damien Miller059321d2013-12-18 17:49:48 +110080 _PATH_SSH_CLIENT_ID_ED25519,
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000081 _PATH_SSH_CLIENT_ID_XMSS,
Damien Miller6e1057c2002-01-22 23:05:59 +110082 NULL
83};
84
djm@openbsd.org56d1c832014-12-21 22:27:55 +000085static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
86
Ben Lindstrom61d328a2002-06-06 21:54:57 +000087/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000088static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110089
Damien Miller6c711792003-01-24 11:36:23 +110090/* User has to confirm key use */
91static int confirm = 0;
92
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000093/* Maximum number of signatures (XMSS) */
94static u_int maxsign = 0;
95static u_int minleft = 0;
96
tim@openbsd.org2681cdb2015-09-13 13:48:19 +000097/* we keep a cache of one passphrase */
Ben Lindstromee617942001-04-10 02:45:32 +000098static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Ben Lindstromee617942001-04-10 02:45:32 +0000100clear_pass(void)
101{
102 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +1100103 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000104 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +0000105 pass = NULL;
106 }
107}
108
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000109static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000110delete_file(int agent_fd, const char *filename, int key_only, int qflag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000112 struct sshkey *public, *cert = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100113 char *certpath = NULL, *comment = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000114 int r, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
djm@openbsd.org141efe42015-01-14 20:05:27 +0000116 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
117 printf("Bad key file %s: %s\n", filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000118 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100119 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000120 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000121 if (!qflag) {
122 fprintf(stderr, "Identity removed: %s (%s)\n",
123 filename, comment);
124 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000125 ret = 0;
126 } else
djm@openbsd.org141efe42015-01-14 20:05:27 +0000127 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
128 filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000129
Damien Miller33a81362012-12-03 09:50:24 +1100130 if (key_only)
131 goto out;
132
133 /* Now try to delete the corresponding certificate too */
134 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100135 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100136 xasprintf(&certpath, "%s-cert.pub", filename);
halex@openbsd.org1797f492015-02-21 21:46:57 +0000137 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
138 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
139 error("Failed to load certificate \"%s\": %s",
140 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100141 goto out;
halex@openbsd.org1797f492015-02-21 21:46:57 +0000142 }
143
djm@openbsd.org141efe42015-01-14 20:05:27 +0000144 if (!sshkey_equal_public(cert, public))
Damien Miller33a81362012-12-03 09:50:24 +1100145 fatal("Certificate %s does not match private key %s",
146 certpath, filename);
147
halex@openbsd.org1797f492015-02-21 21:46:57 +0000148 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000149 if (!qflag) {
150 fprintf(stderr, "Identity removed: %s (%s)\n",
151 certpath, comment);
152 }
Damien Miller33a81362012-12-03 09:50:24 +1100153 ret = 0;
154 } else
halex@openbsd.org1797f492015-02-21 21:46:57 +0000155 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
156 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100157
158 out:
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000159 sshkey_free(cert);
160 sshkey_free(public);
Damien Miller33a81362012-12-03 09:50:24 +1100161 free(certpath);
162 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100163
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000164 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Damien Millerad833b32000-08-23 10:46:23 +1000167/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000168static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000169delete_all(int agent_fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000171 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000172
djm@openbsd.org744bde72017-05-04 06:10:57 +0000173 /*
174 * Since the agent might be forwarded, old or non-OpenSSH, when asked
175 * to remove all keys, attempt to remove both protocol v.1 and v.2
176 * keys.
177 */
markus@openbsd.org47842f72015-03-25 19:29:58 +0000178 if (ssh_remove_all_identities(agent_fd, 2) == 0)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000179 ret = 0;
markus@openbsd.org47842f72015-03-25 19:29:58 +0000180 /* ignore error-code for ssh1 */
181 ssh_remove_all_identities(agent_fd, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000182
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000183 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100184 fprintf(stderr, "All identities removed.\n");
185 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000186 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000187
188 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189}
190
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000191static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000192add_file(int agent_fd, const char *filename, int key_only, int qflag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000194 struct sshkey *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000195 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100196 char msg[1024], *certpath = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000197 int r, fd, ret = -1;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000198 size_t i;
199 u_int32_t left;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000200 struct sshbuf *keyblob;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000201 struct ssh_identitylist *idlist;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Miller2ce12ef2011-05-05 14:17:18 +1000203 if (strcmp(filename, "-") == 0) {
204 fd = STDIN_FILENO;
205 filename = "(stdin)";
206 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000207 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000208 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000209 }
Damien Miller8275fad2006-03-15 12:06:23 +1100210
211 /*
212 * Since we'll try to load a keyfile multiple times, permission errors
213 * will occur multiple times, so check perms first and bail if wrong.
214 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000215 if (fd != STDIN_FILENO) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000216 if (sshkey_perm_ok(fd, filename) != 0) {
Damien Miller2ce12ef2011-05-05 14:17:18 +1000217 close(fd);
218 return -1;
219 }
220 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000221 if ((keyblob = sshbuf_new()) == NULL)
222 fatal("%s: sshbuf_new failed", __func__);
223 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
224 fprintf(stderr, "Error loading key \"%s\": %s\n",
225 filename, ssh_err(r));
226 sshbuf_free(keyblob);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000227 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100228 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000229 }
230 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100231
Damien Miller95def091999-11-25 00:26:21 +1100232 /* At first, try empty passphrase */
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000233 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
234 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000235 fprintf(stderr, "Error loading key \"%s\": %s\n",
236 filename, ssh_err(r));
237 goto fail_load;
238 }
Ben Lindstromee617942001-04-10 02:45:32 +0000239 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000240 if (private == NULL && pass != NULL) {
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000241 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
242 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000243 fprintf(stderr, "Error loading key \"%s\": %s\n",
244 filename, ssh_err(r));
245 goto fail_load;
246 }
Damien Miller86687062014-07-02 15:28:02 +1000247 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000248 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000249 /* clear passphrase since it did not work */
250 clear_pass();
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000251 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
252 filename, confirm ? " (will confirm each use)" : "");
Damien Miller5428f641999-11-25 11:54:57 +1100253 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000254 pass = read_passphrase(msg, RP_ALLOW_STDIN);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000255 if (strcmp(pass, "") == 0)
256 goto fail_load;
257 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000258 &private, &comment)) == 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000259 break;
260 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
261 fprintf(stderr,
262 "Error loading key \"%s\": %s\n",
263 filename, ssh_err(r));
264 fail_load:
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000265 clear_pass();
djm@openbsd.org141efe42015-01-14 20:05:27 +0000266 sshbuf_free(keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000267 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100268 }
Ben Lindstromee617942001-04-10 02:45:32 +0000269 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000270 snprintf(msg, sizeof msg,
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000271 "Bad passphrase, try again for %s%s: ", filename,
halex@openbsd.org5d34aa92015-02-03 00:34:14 +0000272 confirm ? " (will confirm each use)" : "");
Damien Miller95def091999-11-25 00:26:21 +1100273 }
274 }
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000275 if (comment == NULL || *comment == '\0')
276 comment = xstrdup(filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000277 sshbuf_free(keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000278
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000279 /* For XMSS */
280 if ((r = sshkey_set_filename(private, filename)) != 0) {
281 fprintf(stderr, "Could not add filename to private key: %s (%s)\n",
282 filename, comment);
283 goto out;
284 }
285 if (maxsign && minleft &&
286 (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) {
287 for (i = 0; i < idlist->nkeys; i++) {
288 if (!sshkey_equal_public(idlist->keys[i], private))
289 continue;
290 left = sshkey_signatures_left(idlist->keys[i]);
291 if (left < minleft) {
292 fprintf(stderr,
293 "Only %d signatures left.\n", left);
294 break;
295 }
296 fprintf(stderr, "Skipping update: ");
297 if (left == minleft) {
298 fprintf(stderr,
299 "required signatures left (%d).\n", left);
300 } else {
301 fprintf(stderr,
302 "more signatures left (%d) than"
303 " required (%d).\n", left, minleft);
304 }
305 ssh_free_identitylist(idlist);
306 goto out;
307 }
308 ssh_free_identitylist(idlist);
309 }
310
djm@openbsd.org141efe42015-01-14 20:05:27 +0000311 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000312 lifetime, confirm, maxsign)) == 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000313 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000314 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000315 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000316 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000317 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100318 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100319 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000320 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000321 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000322 fprintf(stderr, "Could not add identity \"%s\": %s\n",
323 filename, ssh_err(r));
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000324 }
325
Damien Miller8f4279e2011-10-18 16:06:33 +1100326 /* Skip trying to load the cert if requested */
327 if (key_only)
328 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100329
330 /* Now try to add the certificate flavour too */
331 xasprintf(&certpath, "%s-cert.pub", filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000332 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
333 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
334 error("Failed to load certificate \"%s\": %s",
335 certpath, ssh_err(r));
Damien Millerc6afb5f2010-05-21 14:56:47 +1000336 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000337 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000338
djm@openbsd.org141efe42015-01-14 20:05:27 +0000339 if (!sshkey_equal_public(cert, private)) {
Damien Millerc6afb5f2010-05-21 14:56:47 +1000340 error("Certificate %s does not match private key %s",
341 certpath, filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000342 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000343 goto out;
344 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100345
Damien Millerc6afb5f2010-05-21 14:56:47 +1000346 /* Graft with private bits */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000347 if ((r = sshkey_to_certified(private)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000348 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
349 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000350 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100351 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000352 if ((r = sshkey_cert_copy(cert, private)) != 0) {
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000353 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +0000354 sshkey_free(cert);
355 goto out;
356 }
357 sshkey_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100358
djm@openbsd.org141efe42015-01-14 20:05:27 +0000359 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000360 lifetime, confirm, maxsign)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000361 error("Certificate %s (%s) add failed: %s", certpath,
362 private->cert->key_id, ssh_err(r));
363 goto out;
Damien Millerc6afb5f2010-05-21 14:56:47 +1000364 }
365 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
366 private->cert->key_id);
367 if (lifetime != 0)
368 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
369 if (confirm != 0)
370 fprintf(stderr, "The user must confirm each use of the key\n");
371 out:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000372 free(certpath);
Darren Tuckera627d422013-06-02 07:31:17 +1000373 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000374 sshkey_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100375
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000376 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377}
378
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000379static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000380update_card(int agent_fd, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000381{
Damien Milleref275ea2013-12-29 17:44:07 +1100382 char *pin = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000383 int r, ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000384
Damien Milleref275ea2013-12-29 17:44:07 +1100385 if (add) {
386 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
387 RP_ALLOW_STDIN)) == NULL)
388 return -1;
389 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000390
djm@openbsd.org141efe42015-01-14 20:05:27 +0000391 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
392 lifetime, confirm)) == 0) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000393 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100394 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100395 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000396 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000397 fprintf(stderr, "Could not %s card \"%s\": %s\n",
398 add ? "add" : "remove", id, ssh_err(r));
Damien Miller00111382003-03-10 11:21:17 +1100399 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000400 }
Darren Tuckera627d422013-06-02 07:31:17 +1000401 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100402 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000403}
404
Damien Miller43cba342002-02-05 12:12:49 +1100405static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000406list_identities(int agent_fd, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000408 char *fp;
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000409 int r;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000410 struct ssh_identitylist *idlist;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000411 u_int32_t left;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000412 size_t i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000414 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
415 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
416 fprintf(stderr, "error fetching identities: %s\n",
417 ssh_err(r));
418 else
419 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100420 return -1;
421 }
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000422 for (i = 0; i < idlist->nkeys; i++) {
423 if (do_fp) {
424 fp = sshkey_fingerprint(idlist->keys[i],
425 fingerprint_hash, SSH_FP_DEFAULT);
426 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
427 fp == NULL ? "(null)" : fp, idlist->comments[i],
428 sshkey_type(idlist->keys[i]));
429 free(fp);
430 } else {
431 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
432 fprintf(stderr, "sshkey_write: %s\n",
433 ssh_err(r));
434 continue;
435 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000436 fprintf(stdout, " %s", idlist->comments[i]);
437 left = sshkey_signatures_left(idlist->keys[i]);
438 if (left > 0)
439 fprintf(stdout,
440 " [signatures left %d]", left);
441 fprintf(stdout, "\n");
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000442 }
443 }
444 ssh_free_identitylist(idlist);
Damien Miller43cba342002-02-05 12:12:49 +1100445 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446}
447
Damien Miller6e1057c2002-01-22 23:05:59 +1100448static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000449lock_agent(int agent_fd, int lock)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000450{
451 char prompt[100], *p1, *p2;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000452 int r, passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000453
Ben Lindstrom2f717042002-06-06 21:52:03 +0000454 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
455 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
456 if (lock) {
457 strlcpy(prompt, "Again: ", sizeof prompt);
458 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
459 if (strcmp(p1, p2) != 0) {
460 fprintf(stderr, "Passwords do not match.\n");
461 passok = 0;
462 }
Damien Millera5103f42014-02-04 11:20:14 +1100463 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000464 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000465 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000466 if (passok) {
467 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
468 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
469 ret = 0;
470 } else {
471 fprintf(stderr, "Failed to %slock agent: %s\n",
472 lock ? "" : "un", ssh_err(r));
473 }
474 }
Damien Millera5103f42014-02-04 11:20:14 +1100475 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000477 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000478}
479
480static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000481do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
Damien Miller6e1057c2002-01-22 23:05:59 +1100482{
483 if (deleting) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000484 if (delete_file(agent_fd, file, key_only, qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100485 return -1;
486 } else {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000487 if (add_file(agent_fd, file, key_only, qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100488 return -1;
489 }
490 return 0;
491}
492
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000493static void
494usage(void)
495{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000496 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000497 fprintf(stderr, "Options:\n");
498 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000499 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000500 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100501 fprintf(stderr, " -k Load only keys and not certificates.\n");
502 fprintf(stderr, " -c Require confirmation to sign using identities\n");
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000503 fprintf(stderr, " -m minleft Maxsign is only changed if less than minleft are left (for XMSS)\n");
504 fprintf(stderr, " -M maxsign Maximum number of signatures allowed (for XMSS)\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100505 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000506 fprintf(stderr, " -d Delete identity.\n");
507 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000508 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000509 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100510 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
511 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000512 fprintf(stderr, " -q Be quiet after a successful operation.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000513}
514
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515int
Damien Miller01ab4a21999-10-28 15:23:30 +1000516main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000518 extern char *optarg;
519 extern int optind;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000520 int agent_fd;
Damien Miller7ea845e2010-02-12 09:21:02 +1100521 char *pkcs11provider = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000522 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000523 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +0000525 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +1000526 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
527 sanitise_stdfd();
528
Damien Miller59d3d5b2003-08-22 09:34:41 +1000529 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000530 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000531
Damien Miller72ef7c12015-01-15 02:21:31 +1100532#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000533 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +1100534#endif
Damien Millerad833b32000-08-23 10:46:23 +1000535
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000536 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000537
djm@openbsd.org141efe42015-01-14 20:05:27 +0000538 /* First, get a connection to the authentication agent. */
539 switch (r = ssh_get_authentication_socket(&agent_fd)) {
540 case 0:
541 break;
542 case SSH_ERR_AGENT_NOT_PRESENT:
543 fprintf(stderr, "Could not open a connection to your "
544 "authentication agent.\n");
545 exit(2);
546 default:
547 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
Damien Miller43cba342002-02-05 12:12:49 +1100548 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000550
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000551 while ((ch = getopt(argc, argv, "klLcdDxXE:e:M:m:qs:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000552 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000553 case 'E':
554 fingerprint_hash = ssh_digest_alg_by_name(optarg);
555 if (fingerprint_hash == -1)
556 fatal("Invalid hash algorithm \"%s\"", optarg);
557 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100558 case 'k':
559 key_only = 1;
560 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000561 case 'l':
562 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000563 if (lflag != 0)
564 fatal("-%c flag already specified", lflag);
565 lflag = ch;
566 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000567 case 'x':
568 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000569 if (xflag != 0)
570 fatal("-%c flag already specified", xflag);
571 xflag = ch;
572 break;
Damien Miller6c711792003-01-24 11:36:23 +1100573 case 'c':
574 confirm = 1;
575 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000576 case 'm':
577 minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL);
578 if (minleft == 0) {
579 usage();
580 ret = 1;
581 goto done;
582 }
583 break;
584 case 'M':
585 maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL);
586 if (maxsign == 0) {
587 usage();
588 ret = 1;
589 goto done;
590 }
591 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000592 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100593 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000594 break;
595 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000596 Dflag = 1;
597 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000598 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100599 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000600 break;
601 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100602 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100603 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000604 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000605 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000606 if ((lifetime = convtime(optarg)) == -1) {
607 fprintf(stderr, "Invalid lifetime\n");
608 ret = 1;
609 goto done;
610 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000611 break;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000612 case 'q':
613 qflag = 1;
614 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000615 default:
616 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000617 ret = 1;
618 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000619 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000621
622 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
623 fatal("Invalid combination of actions");
624 else if (xflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000625 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000626 ret = 1;
627 goto done;
628 } else if (lflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000629 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000630 ret = 1;
631 goto done;
632 } else if (Dflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000633 if (delete_all(agent_fd) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000634 ret = 1;
635 goto done;
636 }
637
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000638 argc -= optind;
639 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100640 if (pkcs11provider != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000641 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000642 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000643 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000644 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000645 if (argc == 0) {
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000646 char buf[PATH_MAX];
Damien Miller6e1057c2002-01-22 23:05:59 +1100647 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000648 struct stat st;
649 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100650
651 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100652 fprintf(stderr, "No user found with uid %u\n",
653 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000654 ret = 1;
655 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100656 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100657
Darren Tucker47eede72005-03-14 23:08:12 +1100658 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000659 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100660 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000661 if (stat(buf, &st) < 0)
662 continue;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000663 if (do_file(agent_fd, deleting, key_only, buf,
664 qflag) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000665 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000666 else
667 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000668 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000669 if (count == 0)
670 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000671 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100672 for (i = 0; i < argc; i++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000673 if (do_file(agent_fd, deleting, key_only,
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000674 argv[i], qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100675 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000676 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677 }
Ben Lindstromee617942001-04-10 02:45:32 +0000678 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000679
680done:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000681 ssh_close_authentication_socket(agent_fd);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000682 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683}