blob: 32add8807b0f0e7c02a272010c8228ff5f981cf0 [file] [log] [blame]
djm@openbsd.org141efe42015-01-14 20:05:27 +00001/* $OpenBSD: ssh-add.c,v 1.116 2015/01/14 20:05:27 djm 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 Miller8dbffe72006-08-05 11:02:17 +100042#include <sys/param.h>
Damien Millereba71ba2000-04-29 23:57:08 +100043
Damien Millerad833b32000-08-23 10:46:23 +100044#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110045#include "openbsd-compat/openssl-compat.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
djm@openbsd.org141efe42015-01-14 20:05:27 +000047#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100048#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100049#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100050#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100051#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100052#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100053#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100054#include <unistd.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 "rsa.h"
59#include "log.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000060#include "sshkey.h"
61#include "sshbuf.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100063#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "pathnames.h"
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000065#include "misc.h"
Damien Miller86687062014-07-02 15:28:02 +100066#include "ssherr.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000067#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Ben Lindstromddfb1e32001-08-06 22:06:35 +000069/* argv0 */
70extern char *__progname;
71
Damien Miller6e1057c2002-01-22 23:05:59 +110072/* Default files to add */
73static char *default_files[] = {
Damien Miller72ef7c12015-01-15 02:21:31 +110074#ifdef WITH_OPENSSL
Damien Miller6e1057c2002-01-22 23:05:59 +110075 _PATH_SSH_CLIENT_ID_RSA,
76 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110077#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100078 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110079#endif
Damien Miller72ef7c12015-01-15 02:21:31 +110080#endif /* WITH_OPENSSL */
Damien Miller059321d2013-12-18 17:49:48 +110081 _PATH_SSH_CLIENT_ID_ED25519,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000082 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110083 NULL
84};
85
djm@openbsd.org56d1c832014-12-21 22:27:55 +000086static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
87
Ben Lindstrom61d328a2002-06-06 21:54:57 +000088/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000089static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110090
Damien Miller6c711792003-01-24 11:36:23 +110091/* User has to confirm key use */
92static int confirm = 0;
93
Ben Lindstromee617942001-04-10 02:45:32 +000094/* we keep a cache of one passphrases */
95static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000096static void
Ben Lindstromee617942001-04-10 02:45:32 +000097clear_pass(void)
98{
99 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +1100100 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000101 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +0000102 pass = NULL;
103 }
104}
105
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000106static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000107delete_file(int agent_fd, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000109 struct sshkey *public, *cert = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100110 char *certpath = NULL, *comment = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000111 int r, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
djm@openbsd.org141efe42015-01-14 20:05:27 +0000113 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
114 printf("Bad key file %s: %s\n", filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000115 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100116 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000117 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100118 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000119 ret = 0;
120 } else
djm@openbsd.org141efe42015-01-14 20:05:27 +0000121 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
122 filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000123
Damien Miller33a81362012-12-03 09:50:24 +1100124 if (key_only)
125 goto out;
126
127 /* Now try to delete the corresponding certificate too */
128 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100129 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100130 xasprintf(&certpath, "%s-cert.pub", filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000131 if ((r = sshkey_load_public(certpath, &cert, &comment)) == 0)
Damien Miller33a81362012-12-03 09:50:24 +1100132 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000133 if (!sshkey_equal_public(cert, public))
Damien Miller33a81362012-12-03 09:50:24 +1100134 fatal("Certificate %s does not match private key %s",
135 certpath, filename);
136
djm@openbsd.org141efe42015-01-14 20:05:27 +0000137 if (ssh_remove_identity(agent_fd, cert)) {
Damien Miller33a81362012-12-03 09:50:24 +1100138 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
139 comment);
140 ret = 0;
141 } else
142 fprintf(stderr, "Could not remove identity: %s\n", certpath);
143
144 out:
145 if (cert != NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000146 sshkey_free(cert);
Damien Miller33a81362012-12-03 09:50:24 +1100147 if (public != NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000148 sshkey_free(public);
Damien Miller33a81362012-12-03 09:50:24 +1100149 free(certpath);
150 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100151
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000152 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153}
154
Damien Millerad833b32000-08-23 10:46:23 +1000155/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000156static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000157delete_all(int agent_fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000159 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000160
djm@openbsd.org141efe42015-01-14 20:05:27 +0000161 if (ssh_remove_all_identities(agent_fd, 1) == 0)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000162 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000163 /* ignore error-code for ssh2 */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000164 /* XXX revisit */
165 ssh_remove_all_identities(agent_fd, 2);
Damien Millerad833b32000-08-23 10:46:23 +1000166
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000167 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100168 fprintf(stderr, "All identities removed.\n");
169 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000170 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000171
172 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000175static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000176add_file(int agent_fd, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000178 struct sshkey *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000179 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100180 char msg[1024], *certpath = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000181 int r, fd, ret = -1;
182 struct sshbuf *keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183
Damien Miller2ce12ef2011-05-05 14:17:18 +1000184 if (strcmp(filename, "-") == 0) {
185 fd = STDIN_FILENO;
186 filename = "(stdin)";
187 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000188 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000189 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000190 }
Damien Miller8275fad2006-03-15 12:06:23 +1100191
192 /*
193 * Since we'll try to load a keyfile multiple times, permission errors
194 * will occur multiple times, so check perms first and bail if wrong.
195 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000196 if (fd != STDIN_FILENO) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000197 if (sshkey_perm_ok(fd, filename) != 0) {
Damien Miller2ce12ef2011-05-05 14:17:18 +1000198 close(fd);
199 return -1;
200 }
201 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000202 if ((keyblob = sshbuf_new()) == NULL)
203 fatal("%s: sshbuf_new failed", __func__);
204 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
205 fprintf(stderr, "Error loading key \"%s\": %s\n",
206 filename, ssh_err(r));
207 sshbuf_free(keyblob);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000208 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100209 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000210 }
211 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100212
Damien Miller95def091999-11-25 00:26:21 +1100213 /* At first, try empty passphrase */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000214 if ((r = sshkey_parse_private_fileblob(keyblob, "", filename,
215 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
216 fprintf(stderr, "Error loading key \"%s\": %s\n",
217 filename, ssh_err(r));
218 goto fail_load;
219 }
Ben Lindstromee617942001-04-10 02:45:32 +0000220 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000221 if (private == NULL && pass != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000222 if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename,
Damien Miller86687062014-07-02 15:28:02 +1000223 &private, &comment)) != 0 &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000224 r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
225 fprintf(stderr, "Error loading key \"%s\": %s\n",
226 filename, ssh_err(r));
227 goto fail_load;
228 }
Damien Miller86687062014-07-02 15:28:02 +1000229 }
Damien Miller1d0df322014-07-11 09:19:04 +1000230 if (comment == NULL)
231 comment = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000232 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000233 /* clear passphrase since it did not work */
234 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000235 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000236 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100237 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000238 pass = read_passphrase(msg, RP_ALLOW_STDIN);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000239 if (strcmp(pass, "") == 0)
240 goto fail_load;
241 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
242 filename, &private, NULL)) == 0)
243 break;
244 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
245 fprintf(stderr,
246 "Error loading key \"%s\": %s\n",
247 filename, ssh_err(r));
248 fail_load:
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000249 clear_pass();
Darren Tuckera627d422013-06-02 07:31:17 +1000250 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000251 sshbuf_free(keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000252 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100253 }
Ben Lindstromee617942001-04-10 02:45:32 +0000254 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000255 snprintf(msg, sizeof msg,
256 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100257 }
258 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000259 sshbuf_free(keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000260
djm@openbsd.org141efe42015-01-14 20:05:27 +0000261 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
262 lifetime, confirm)) == 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000263 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000264 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000265 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000266 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000267 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100268 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100269 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000270 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000271 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000272 fprintf(stderr, "Could not add identity \"%s\": %s\n",
273 filename, ssh_err(r));
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000274 }
275
Damien Miller8f4279e2011-10-18 16:06:33 +1100276 /* Skip trying to load the cert if requested */
277 if (key_only)
278 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100279
280 /* Now try to add the certificate flavour too */
281 xasprintf(&certpath, "%s-cert.pub", filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000282 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
283 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
284 error("Failed to load certificate \"%s\": %s",
285 certpath, ssh_err(r));
Damien Millerc6afb5f2010-05-21 14:56:47 +1000286 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000287 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000288
djm@openbsd.org141efe42015-01-14 20:05:27 +0000289 if (!sshkey_equal_public(cert, private)) {
Damien Millerc6afb5f2010-05-21 14:56:47 +1000290 error("Certificate %s does not match private key %s",
291 certpath, filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000292 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000293 goto out;
294 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100295
Damien Millerc6afb5f2010-05-21 14:56:47 +1000296 /* Graft with private bits */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000297 if ((r = sshkey_to_certified(private,
298 sshkey_cert_is_legacy(cert))) != 0) {
299 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
300 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000301 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100302 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000303 if ((r = sshkey_cert_copy(cert, private)) != 0) {
304 error("%s: key_cert_copy: %s", __func__, ssh_err(r));
305 sshkey_free(cert);
306 goto out;
307 }
308 sshkey_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100309
djm@openbsd.org141efe42015-01-14 20:05:27 +0000310 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
311 lifetime, confirm)) != 0) {
312 error("Certificate %s (%s) add failed: %s", certpath,
313 private->cert->key_id, ssh_err(r));
314 goto out;
Damien Millerc6afb5f2010-05-21 14:56:47 +1000315 }
316 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
317 private->cert->key_id);
318 if (lifetime != 0)
319 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
320 if (confirm != 0)
321 fprintf(stderr, "The user must confirm each use of the key\n");
322 out:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000323 free(certpath);
Darren Tuckera627d422013-06-02 07:31:17 +1000324 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000325 sshkey_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100326
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000327 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000330static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000331update_card(int agent_fd, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000332{
Damien Milleref275ea2013-12-29 17:44:07 +1100333 char *pin = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000334 int r, ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000335
Damien Milleref275ea2013-12-29 17:44:07 +1100336 if (add) {
337 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
338 RP_ALLOW_STDIN)) == NULL)
339 return -1;
340 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000341
djm@openbsd.org141efe42015-01-14 20:05:27 +0000342 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
343 lifetime, confirm)) == 0) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000344 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100345 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100346 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000347 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000348 fprintf(stderr, "Could not %s card \"%s\": %s\n",
349 add ? "add" : "remove", id, ssh_err(r));
Damien Miller00111382003-03-10 11:21:17 +1100350 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000351 }
Darren Tuckera627d422013-06-02 07:31:17 +1000352 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100353 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000354}
355
Damien Miller43cba342002-02-05 12:12:49 +1100356static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000357list_identities(int agent_fd, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000359 char *fp;
360 int version, r, had_identities = 0;
361 struct ssh_identitylist *idlist;
362 size_t i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Millerad833b32000-08-23 10:46:23 +1000364 for (version = 1; version <= 2; version++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000365 if ((r = ssh_fetch_identitylist(agent_fd, version,
366 &idlist)) != 0) {
367 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
368 fprintf(stderr, "error fetching identities for "
369 "protocol %d: %s\n", version, ssh_err(r));
370 continue;
371 }
372 for (i = 0; i < idlist->nkeys; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000373 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000374 if (do_fp) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000375 fp = sshkey_fingerprint(idlist->keys[i],
376 fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 printf("%d %s %s (%s)\n",
djm@openbsd.org141efe42015-01-14 20:05:27 +0000378 sshkey_size(idlist->keys[i]), fp,
379 idlist->comments[i],
380 sshkey_type(idlist->keys[i]));
Darren Tuckera627d422013-06-02 07:31:17 +1000381 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100382 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000383 if ((r = sshkey_write(idlist->keys[i],
384 stdout)) != 0) {
385 fprintf(stderr, "sshkey_write: %s\n",
386 ssh_err(r));
387 continue;
388 }
389 fprintf(stdout, " %s\n", idlist->comments[i]);
Damien Miller95def091999-11-25 00:26:21 +1100390 }
391 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000392 ssh_free_identitylist(idlist);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100393 }
Damien Miller43cba342002-02-05 12:12:49 +1100394 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100395 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100396 return -1;
397 }
398 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399}
400
Damien Miller6e1057c2002-01-22 23:05:59 +1100401static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000402lock_agent(int agent_fd, int lock)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000403{
404 char prompt[100], *p1, *p2;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000405 int r, passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000406
Ben Lindstrom2f717042002-06-06 21:52:03 +0000407 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
408 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
409 if (lock) {
410 strlcpy(prompt, "Again: ", sizeof prompt);
411 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
412 if (strcmp(p1, p2) != 0) {
413 fprintf(stderr, "Passwords do not match.\n");
414 passok = 0;
415 }
Damien Millera5103f42014-02-04 11:20:14 +1100416 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000417 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000418 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000419 if (passok) {
420 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
421 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
422 ret = 0;
423 } else {
424 fprintf(stderr, "Failed to %slock agent: %s\n",
425 lock ? "" : "un", ssh_err(r));
426 }
427 }
Damien Millera5103f42014-02-04 11:20:14 +1100428 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000429 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000430 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000431}
432
433static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000434do_file(int agent_fd, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100435{
436 if (deleting) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000437 if (delete_file(agent_fd, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100438 return -1;
439 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000440 if (add_file(agent_fd, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100441 return -1;
442 }
443 return 0;
444}
445
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000446static void
447usage(void)
448{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000449 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000450 fprintf(stderr, "Options:\n");
451 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000452 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000453 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100454 fprintf(stderr, " -k Load only keys and not certificates.\n");
455 fprintf(stderr, " -c Require confirmation to sign using identities\n");
456 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000457 fprintf(stderr, " -d Delete identity.\n");
458 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000459 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000460 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100461 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
462 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000463}
464
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465int
Damien Miller01ab4a21999-10-28 15:23:30 +1000466main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000468 extern char *optarg;
469 extern int optind;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000470 int agent_fd;
Damien Miller7ea845e2010-02-12 09:21:02 +1100471 char *pkcs11provider = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000472 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000473 int xflag = 0, lflag = 0, Dflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Darren Tuckerce321d82005-10-03 18:11:24 +1000475 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
476 sanitise_stdfd();
477
Damien Miller59d3d5b2003-08-22 09:34:41 +1000478 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000479 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000480
Damien Miller72ef7c12015-01-15 02:21:31 +1100481#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000482 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +1100483#endif
Damien Millerad833b32000-08-23 10:46:23 +1000484
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000485 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000486
djm@openbsd.org141efe42015-01-14 20:05:27 +0000487 /* First, get a connection to the authentication agent. */
488 switch (r = ssh_get_authentication_socket(&agent_fd)) {
489 case 0:
490 break;
491 case SSH_ERR_AGENT_NOT_PRESENT:
492 fprintf(stderr, "Could not open a connection to your "
493 "authentication agent.\n");
494 exit(2);
495 default:
496 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
Damien Miller43cba342002-02-05 12:12:49 +1100497 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000499
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000500 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000501 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000502 case 'E':
503 fingerprint_hash = ssh_digest_alg_by_name(optarg);
504 if (fingerprint_hash == -1)
505 fatal("Invalid hash algorithm \"%s\"", optarg);
506 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100507 case 'k':
508 key_only = 1;
509 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000510 case 'l':
511 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000512 if (lflag != 0)
513 fatal("-%c flag already specified", lflag);
514 lflag = ch;
515 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000516 case 'x':
517 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000518 if (xflag != 0)
519 fatal("-%c flag already specified", xflag);
520 xflag = ch;
521 break;
Damien Miller6c711792003-01-24 11:36:23 +1100522 case 'c':
523 confirm = 1;
524 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000525 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100526 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000527 break;
528 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000529 Dflag = 1;
530 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000531 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100532 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000533 break;
534 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100535 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100536 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000537 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000538 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000539 if ((lifetime = convtime(optarg)) == -1) {
540 fprintf(stderr, "Invalid lifetime\n");
541 ret = 1;
542 goto done;
543 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000544 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000545 default:
546 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000547 ret = 1;
548 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000549 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000551
552 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
553 fatal("Invalid combination of actions");
554 else if (xflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000555 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000556 ret = 1;
557 goto done;
558 } else if (lflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000559 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000560 ret = 1;
561 goto done;
562 } else if (Dflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000563 if (delete_all(agent_fd) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000564 ret = 1;
565 goto done;
566 }
567
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000568 argc -= optind;
569 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100570 if (pkcs11provider != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000571 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000572 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000573 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000574 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000575 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100576 char buf[MAXPATHLEN];
577 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000578 struct stat st;
579 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100580
581 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100582 fprintf(stderr, "No user found with uid %u\n",
583 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000584 ret = 1;
585 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100586 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100587
Darren Tucker47eede72005-03-14 23:08:12 +1100588 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000589 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100590 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000591 if (stat(buf, &st) < 0)
592 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000593 if (do_file(agent_fd, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000594 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000595 else
596 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000597 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000598 if (count == 0)
599 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000600 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100601 for (i = 0; i < argc; i++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000602 if (do_file(agent_fd, deleting, key_only,
603 argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100604 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000605 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606 }
Ben Lindstromee617942001-04-10 02:45:32 +0000607 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000608
609done:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000610 ssh_close_authentication_socket(agent_fd);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000611 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612}