blob: 2afd483305ccc34c8f9bf83ceb5e08dafae8817c [file] [log] [blame]
dlg@openbsd.org530591a2017-08-29 09:42:29 +00001/* $OpenBSD: ssh-add.c,v 1.134 2017/08/29 09:42:29 dlg 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,
Damien Miller6e1057c2002-01-22 23:05:59 +110081 NULL
82};
83
djm@openbsd.org56d1c832014-12-21 22:27:55 +000084static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
85
Ben Lindstrom61d328a2002-06-06 21:54:57 +000086/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000087static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110088
Damien Miller6c711792003-01-24 11:36:23 +110089/* User has to confirm key use */
90static int confirm = 0;
91
tim@openbsd.org2681cdb2015-09-13 13:48:19 +000092/* we keep a cache of one passphrase */
Ben Lindstromee617942001-04-10 02:45:32 +000093static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000094static void
Ben Lindstromee617942001-04-10 02:45:32 +000095clear_pass(void)
96{
97 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +110098 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +100099 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +0000100 pass = NULL;
101 }
102}
103
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000104static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000105delete_file(int agent_fd, const char *filename, int key_only, int qflag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000107 struct sshkey *public, *cert = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100108 char *certpath = NULL, *comment = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000109 int r, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
djm@openbsd.org141efe42015-01-14 20:05:27 +0000111 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
112 printf("Bad key file %s: %s\n", filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000113 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100114 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000115 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000116 if (!qflag) {
117 fprintf(stderr, "Identity removed: %s (%s)\n",
118 filename, comment);
119 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000120 ret = 0;
121 } else
djm@openbsd.org141efe42015-01-14 20:05:27 +0000122 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
123 filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000124
Damien Miller33a81362012-12-03 09:50:24 +1100125 if (key_only)
126 goto out;
127
128 /* Now try to delete the corresponding certificate too */
129 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100130 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100131 xasprintf(&certpath, "%s-cert.pub", filename);
halex@openbsd.org1797f492015-02-21 21:46:57 +0000132 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
133 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
134 error("Failed to load certificate \"%s\": %s",
135 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100136 goto out;
halex@openbsd.org1797f492015-02-21 21:46:57 +0000137 }
138
djm@openbsd.org141efe42015-01-14 20:05:27 +0000139 if (!sshkey_equal_public(cert, public))
Damien Miller33a81362012-12-03 09:50:24 +1100140 fatal("Certificate %s does not match private key %s",
141 certpath, filename);
142
halex@openbsd.org1797f492015-02-21 21:46:57 +0000143 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000144 if (!qflag) {
145 fprintf(stderr, "Identity removed: %s (%s)\n",
146 certpath, comment);
147 }
Damien Miller33a81362012-12-03 09:50:24 +1100148 ret = 0;
149 } else
halex@openbsd.org1797f492015-02-21 21:46:57 +0000150 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
151 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100152
153 out:
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000154 sshkey_free(cert);
155 sshkey_free(public);
Damien Miller33a81362012-12-03 09:50:24 +1100156 free(certpath);
157 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100158
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000159 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160}
161
Damien Millerad833b32000-08-23 10:46:23 +1000162/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000163static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000164delete_all(int agent_fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000166 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000167
djm@openbsd.org744bde72017-05-04 06:10:57 +0000168 /*
169 * Since the agent might be forwarded, old or non-OpenSSH, when asked
170 * to remove all keys, attempt to remove both protocol v.1 and v.2
171 * keys.
172 */
markus@openbsd.org47842f72015-03-25 19:29:58 +0000173 if (ssh_remove_all_identities(agent_fd, 2) == 0)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000174 ret = 0;
markus@openbsd.org47842f72015-03-25 19:29:58 +0000175 /* ignore error-code for ssh1 */
176 ssh_remove_all_identities(agent_fd, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000177
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000178 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100179 fprintf(stderr, "All identities removed.\n");
180 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000181 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000182
183 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184}
185
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000186static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000187add_file(int agent_fd, const char *filename, int key_only, int qflag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000189 struct sshkey *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000190 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100191 char msg[1024], *certpath = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000192 int r, fd, ret = -1;
193 struct sshbuf *keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Miller2ce12ef2011-05-05 14:17:18 +1000195 if (strcmp(filename, "-") == 0) {
196 fd = STDIN_FILENO;
197 filename = "(stdin)";
198 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000199 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000200 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000201 }
Damien Miller8275fad2006-03-15 12:06:23 +1100202
203 /*
204 * Since we'll try to load a keyfile multiple times, permission errors
205 * will occur multiple times, so check perms first and bail if wrong.
206 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000207 if (fd != STDIN_FILENO) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000208 if (sshkey_perm_ok(fd, filename) != 0) {
Damien Miller2ce12ef2011-05-05 14:17:18 +1000209 close(fd);
210 return -1;
211 }
212 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000213 if ((keyblob = sshbuf_new()) == NULL)
214 fatal("%s: sshbuf_new failed", __func__);
215 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
216 fprintf(stderr, "Error loading key \"%s\": %s\n",
217 filename, ssh_err(r));
218 sshbuf_free(keyblob);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000219 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100220 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000221 }
222 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100223
Damien Miller95def091999-11-25 00:26:21 +1100224 /* At first, try empty passphrase */
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000225 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
226 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000227 fprintf(stderr, "Error loading key \"%s\": %s\n",
228 filename, ssh_err(r));
229 goto fail_load;
230 }
Ben Lindstromee617942001-04-10 02:45:32 +0000231 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000232 if (private == NULL && pass != NULL) {
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000233 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &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 }
Damien Miller86687062014-07-02 15:28:02 +1000239 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000240 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000241 /* clear passphrase since it did not work */
242 clear_pass();
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000243 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
244 filename, confirm ? " (will confirm each use)" : "");
Damien Miller5428f641999-11-25 11:54:57 +1100245 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000246 pass = read_passphrase(msg, RP_ALLOW_STDIN);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000247 if (strcmp(pass, "") == 0)
248 goto fail_load;
249 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000250 &private, &comment)) == 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000251 break;
252 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
253 fprintf(stderr,
254 "Error loading key \"%s\": %s\n",
255 filename, ssh_err(r));
256 fail_load:
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000257 clear_pass();
djm@openbsd.org141efe42015-01-14 20:05:27 +0000258 sshbuf_free(keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000259 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100260 }
Ben Lindstromee617942001-04-10 02:45:32 +0000261 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000262 snprintf(msg, sizeof msg,
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000263 "Bad passphrase, try again for %s%s: ", filename,
halex@openbsd.org5d34aa92015-02-03 00:34:14 +0000264 confirm ? " (will confirm each use)" : "");
Damien Miller95def091999-11-25 00:26:21 +1100265 }
266 }
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000267 if (comment == NULL || *comment == '\0')
268 comment = xstrdup(filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000269 sshbuf_free(keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000270
djm@openbsd.org141efe42015-01-14 20:05:27 +0000271 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
272 lifetime, confirm)) == 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000273 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000274 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000275 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000276 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000277 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100278 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100279 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000280 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000281 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000282 fprintf(stderr, "Could not add identity \"%s\": %s\n",
283 filename, ssh_err(r));
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000284 }
285
Damien Miller8f4279e2011-10-18 16:06:33 +1100286 /* Skip trying to load the cert if requested */
287 if (key_only)
288 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100289
290 /* Now try to add the certificate flavour too */
291 xasprintf(&certpath, "%s-cert.pub", filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000292 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
293 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
294 error("Failed to load certificate \"%s\": %s",
295 certpath, ssh_err(r));
Damien Millerc6afb5f2010-05-21 14:56:47 +1000296 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000297 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000298
djm@openbsd.org141efe42015-01-14 20:05:27 +0000299 if (!sshkey_equal_public(cert, private)) {
Damien Millerc6afb5f2010-05-21 14:56:47 +1000300 error("Certificate %s does not match private key %s",
301 certpath, filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000302 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000303 goto out;
304 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100305
Damien Millerc6afb5f2010-05-21 14:56:47 +1000306 /* Graft with private bits */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000307 if ((r = sshkey_to_certified(private)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000308 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
309 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000310 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100311 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000312 if ((r = sshkey_cert_copy(cert, private)) != 0) {
markus@openbsd.org7da5df12017-05-30 14:16:41 +0000313 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +0000314 sshkey_free(cert);
315 goto out;
316 }
317 sshkey_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100318
djm@openbsd.org141efe42015-01-14 20:05:27 +0000319 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
320 lifetime, confirm)) != 0) {
321 error("Certificate %s (%s) add failed: %s", certpath,
322 private->cert->key_id, ssh_err(r));
323 goto out;
Damien Millerc6afb5f2010-05-21 14:56:47 +1000324 }
325 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
326 private->cert->key_id);
327 if (lifetime != 0)
328 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
329 if (confirm != 0)
330 fprintf(stderr, "The user must confirm each use of the key\n");
331 out:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000332 free(certpath);
Darren Tuckera627d422013-06-02 07:31:17 +1000333 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000334 sshkey_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100335
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000336 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337}
338
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000339static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000340update_card(int agent_fd, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000341{
Damien Milleref275ea2013-12-29 17:44:07 +1100342 char *pin = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000343 int r, ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000344
Damien Milleref275ea2013-12-29 17:44:07 +1100345 if (add) {
346 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
347 RP_ALLOW_STDIN)) == NULL)
348 return -1;
349 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000350
djm@openbsd.org141efe42015-01-14 20:05:27 +0000351 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
352 lifetime, confirm)) == 0) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000353 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100354 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100355 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000356 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000357 fprintf(stderr, "Could not %s card \"%s\": %s\n",
358 add ? "add" : "remove", id, ssh_err(r));
Damien Miller00111382003-03-10 11:21:17 +1100359 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000360 }
Darren Tuckera627d422013-06-02 07:31:17 +1000361 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100362 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000363}
364
Damien Miller43cba342002-02-05 12:12:49 +1100365static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000366list_identities(int agent_fd, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000368 char *fp;
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000369 int r;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000370 struct ssh_identitylist *idlist;
371 size_t i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000373 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
374 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
375 fprintf(stderr, "error fetching identities: %s\n",
376 ssh_err(r));
377 else
378 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100379 return -1;
380 }
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000381 for (i = 0; i < idlist->nkeys; i++) {
382 if (do_fp) {
383 fp = sshkey_fingerprint(idlist->keys[i],
384 fingerprint_hash, SSH_FP_DEFAULT);
385 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
386 fp == NULL ? "(null)" : fp, idlist->comments[i],
387 sshkey_type(idlist->keys[i]));
388 free(fp);
389 } else {
390 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
391 fprintf(stderr, "sshkey_write: %s\n",
392 ssh_err(r));
393 continue;
394 }
395 fprintf(stdout, " %s\n", idlist->comments[i]);
396 }
397 }
398 ssh_free_identitylist(idlist);
Damien Miller43cba342002-02-05 12:12:49 +1100399 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
401
Damien Miller6e1057c2002-01-22 23:05:59 +1100402static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000403lock_agent(int agent_fd, int lock)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000404{
405 char prompt[100], *p1, *p2;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000406 int r, passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000407
Ben Lindstrom2f717042002-06-06 21:52:03 +0000408 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
409 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
410 if (lock) {
411 strlcpy(prompt, "Again: ", sizeof prompt);
412 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
413 if (strcmp(p1, p2) != 0) {
414 fprintf(stderr, "Passwords do not match.\n");
415 passok = 0;
416 }
Damien Millera5103f42014-02-04 11:20:14 +1100417 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000418 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000419 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000420 if (passok) {
421 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
422 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
423 ret = 0;
424 } else {
425 fprintf(stderr, "Failed to %slock agent: %s\n",
426 lock ? "" : "un", ssh_err(r));
427 }
428 }
Damien Millera5103f42014-02-04 11:20:14 +1100429 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000430 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000431 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000432}
433
434static int
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000435do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
Damien Miller6e1057c2002-01-22 23:05:59 +1100436{
437 if (deleting) {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000438 if (delete_file(agent_fd, file, key_only, qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100439 return -1;
440 } else {
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000441 if (add_file(agent_fd, file, key_only, qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100442 return -1;
443 }
444 return 0;
445}
446
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000447static void
448usage(void)
449{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000450 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000451 fprintf(stderr, "Options:\n");
452 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000453 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000454 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100455 fprintf(stderr, " -k Load only keys and not certificates.\n");
456 fprintf(stderr, " -c Require confirmation to sign using identities\n");
457 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000458 fprintf(stderr, " -d Delete identity.\n");
459 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000460 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000461 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100462 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
463 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000464 fprintf(stderr, " -q Be quiet after a successful operation.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000465}
466
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467int
Damien Miller01ab4a21999-10-28 15:23:30 +1000468main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000470 extern char *optarg;
471 extern int optind;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000472 int agent_fd;
Damien Miller7ea845e2010-02-12 09:21:02 +1100473 char *pkcs11provider = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000474 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000475 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +0000477 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +1000478 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
479 sanitise_stdfd();
480
Damien Miller59d3d5b2003-08-22 09:34:41 +1000481 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000482 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000483
Damien Miller72ef7c12015-01-15 02:21:31 +1100484#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000485 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +1100486#endif
Damien Millerad833b32000-08-23 10:46:23 +1000487
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000488 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000489
djm@openbsd.org141efe42015-01-14 20:05:27 +0000490 /* First, get a connection to the authentication agent. */
491 switch (r = ssh_get_authentication_socket(&agent_fd)) {
492 case 0:
493 break;
494 case SSH_ERR_AGENT_NOT_PRESENT:
495 fprintf(stderr, "Could not open a connection to your "
496 "authentication agent.\n");
497 exit(2);
498 default:
499 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
Damien Miller43cba342002-02-05 12:12:49 +1100500 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000502
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000503 while ((ch = getopt(argc, argv, "klLcdDxXE:e:qs:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000504 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000505 case 'E':
506 fingerprint_hash = ssh_digest_alg_by_name(optarg);
507 if (fingerprint_hash == -1)
508 fatal("Invalid hash algorithm \"%s\"", optarg);
509 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100510 case 'k':
511 key_only = 1;
512 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000513 case 'l':
514 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000515 if (lflag != 0)
516 fatal("-%c flag already specified", lflag);
517 lflag = ch;
518 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000519 case 'x':
520 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000521 if (xflag != 0)
522 fatal("-%c flag already specified", xflag);
523 xflag = ch;
524 break;
Damien Miller6c711792003-01-24 11:36:23 +1100525 case 'c':
526 confirm = 1;
527 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000528 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100529 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000530 break;
531 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000532 Dflag = 1;
533 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000534 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100535 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000536 break;
537 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100538 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100539 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000540 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000541 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000542 if ((lifetime = convtime(optarg)) == -1) {
543 fprintf(stderr, "Invalid lifetime\n");
544 ret = 1;
545 goto done;
546 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000547 break;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000548 case 'q':
549 qflag = 1;
550 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000551 default:
552 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000553 ret = 1;
554 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000555 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000557
558 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
559 fatal("Invalid combination of actions");
560 else if (xflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000561 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000562 ret = 1;
563 goto done;
564 } else if (lflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000565 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000566 ret = 1;
567 goto done;
568 } else if (Dflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000569 if (delete_all(agent_fd) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000570 ret = 1;
571 goto done;
572 }
573
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000574 argc -= optind;
575 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100576 if (pkcs11provider != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000577 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000578 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000579 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000580 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000581 if (argc == 0) {
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000582 char buf[PATH_MAX];
Damien Miller6e1057c2002-01-22 23:05:59 +1100583 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000584 struct stat st;
585 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100586
587 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100588 fprintf(stderr, "No user found with uid %u\n",
589 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000590 ret = 1;
591 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100592 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100593
Darren Tucker47eede72005-03-14 23:08:12 +1100594 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000595 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100596 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000597 if (stat(buf, &st) < 0)
598 continue;
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000599 if (do_file(agent_fd, deleting, key_only, buf,
600 qflag) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000601 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000602 else
603 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000604 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000605 if (count == 0)
606 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000607 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100608 for (i = 0; i < argc; i++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000609 if (do_file(agent_fd, deleting, key_only,
dlg@openbsd.org530591a2017-08-29 09:42:29 +0000610 argv[i], qflag) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100611 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000612 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000613 }
Ben Lindstromee617942001-04-10 02:45:32 +0000614 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000615
616done:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000617 ssh_close_authentication_socket(agent_fd);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000618 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619}