blob: fb9a53e64cfe15720e73ecc8110908e64f0c2f47 [file] [log] [blame]
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001/* $OpenBSD: ssh-add.c,v 1.128 2016/02/15 09:47:49 dtucker 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 "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,
naddy@openbsd.org1b0ef382015-03-26 12:32:38 +000082#ifdef WITH_SSH1
Ben Lindstrom6328ab32002-03-22 02:54:23 +000083 _PATH_SSH_CLIENT_IDENTITY,
naddy@openbsd.org1b0ef382015-03-26 12:32:38 +000084#endif
Damien Miller6e1057c2002-01-22 23:05:59 +110085 NULL
86};
87
djm@openbsd.org56d1c832014-12-21 22:27:55 +000088static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
89
Ben Lindstrom61d328a2002-06-06 21:54:57 +000090/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000091static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110092
Damien Miller6c711792003-01-24 11:36:23 +110093/* User has to confirm key use */
94static int confirm = 0;
95
tim@openbsd.org2681cdb2015-09-13 13:48:19 +000096/* we keep a cache of one passphrase */
Ben Lindstromee617942001-04-10 02:45:32 +000097static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000098static void
Ben Lindstromee617942001-04-10 02:45:32 +000099clear_pass(void)
100{
101 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +1100102 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +1000103 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +0000104 pass = NULL;
105 }
106}
107
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000108static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000109delete_file(int agent_fd, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000111 struct sshkey *public, *cert = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100112 char *certpath = NULL, *comment = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000113 int r, ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
djm@openbsd.org141efe42015-01-14 20:05:27 +0000115 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
116 printf("Bad key file %s: %s\n", filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100118 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000119 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100120 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000121 ret = 0;
122 } else
djm@openbsd.org141efe42015-01-14 20:05:27 +0000123 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
124 filename, ssh_err(r));
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000125
Damien Miller33a81362012-12-03 09:50:24 +1100126 if (key_only)
127 goto out;
128
129 /* Now try to delete the corresponding certificate too */
130 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100131 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100132 xasprintf(&certpath, "%s-cert.pub", filename);
halex@openbsd.org1797f492015-02-21 21:46:57 +0000133 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
134 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
135 error("Failed to load certificate \"%s\": %s",
136 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100137 goto out;
halex@openbsd.org1797f492015-02-21 21:46:57 +0000138 }
139
djm@openbsd.org141efe42015-01-14 20:05:27 +0000140 if (!sshkey_equal_public(cert, public))
Damien Miller33a81362012-12-03 09:50:24 +1100141 fatal("Certificate %s does not match private key %s",
142 certpath, filename);
143
halex@openbsd.org1797f492015-02-21 21:46:57 +0000144 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
Damien Miller33a81362012-12-03 09:50:24 +1100145 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
146 comment);
147 ret = 0;
148 } else
halex@openbsd.org1797f492015-02-21 21:46:57 +0000149 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
150 certpath, ssh_err(r));
Damien Miller33a81362012-12-03 09:50:24 +1100151
152 out:
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000153 sshkey_free(cert);
154 sshkey_free(public);
Damien Miller33a81362012-12-03 09:50:24 +1100155 free(certpath);
156 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100157
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000158 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159}
160
Damien Millerad833b32000-08-23 10:46:23 +1000161/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000162static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000163delete_all(int agent_fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000165 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000166
markus@openbsd.org47842f72015-03-25 19:29:58 +0000167 if (ssh_remove_all_identities(agent_fd, 2) == 0)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000168 ret = 0;
markus@openbsd.org47842f72015-03-25 19:29:58 +0000169 /* ignore error-code for ssh1 */
170 ssh_remove_all_identities(agent_fd, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000171
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000172 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100173 fprintf(stderr, "All identities removed.\n");
174 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000175 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000176
177 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178}
179
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000180static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000181add_file(int agent_fd, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000183 struct sshkey *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000184 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100185 char msg[1024], *certpath = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000186 int r, fd, ret = -1;
187 struct sshbuf *keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller2ce12ef2011-05-05 14:17:18 +1000189 if (strcmp(filename, "-") == 0) {
190 fd = STDIN_FILENO;
191 filename = "(stdin)";
192 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000193 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000194 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000195 }
Damien Miller8275fad2006-03-15 12:06:23 +1100196
197 /*
198 * Since we'll try to load a keyfile multiple times, permission errors
199 * will occur multiple times, so check perms first and bail if wrong.
200 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000201 if (fd != STDIN_FILENO) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000202 if (sshkey_perm_ok(fd, filename) != 0) {
Damien Miller2ce12ef2011-05-05 14:17:18 +1000203 close(fd);
204 return -1;
205 }
206 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000207 if ((keyblob = sshbuf_new()) == NULL)
208 fatal("%s: sshbuf_new failed", __func__);
209 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
210 fprintf(stderr, "Error loading key \"%s\": %s\n",
211 filename, ssh_err(r));
212 sshbuf_free(keyblob);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000213 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100214 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000215 }
216 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* At first, try empty passphrase */
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000219 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
220 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000221 fprintf(stderr, "Error loading key \"%s\": %s\n",
222 filename, ssh_err(r));
223 goto fail_load;
224 }
Ben Lindstromee617942001-04-10 02:45:32 +0000225 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000226 if (private == NULL && pass != NULL) {
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000227 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
228 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000229 fprintf(stderr, "Error loading key \"%s\": %s\n",
230 filename, ssh_err(r));
231 goto fail_load;
232 }
Damien Miller86687062014-07-02 15:28:02 +1000233 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000234 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000235 /* clear passphrase since it did not work */
236 clear_pass();
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000237 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
238 filename, confirm ? " (will confirm each use)" : "");
Damien Miller5428f641999-11-25 11:54:57 +1100239 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000240 pass = read_passphrase(msg, RP_ALLOW_STDIN);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000241 if (strcmp(pass, "") == 0)
242 goto fail_load;
243 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
tim@openbsd.org3c019a92015-09-13 14:39:16 +0000244 &private, &comment)) == 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000245 break;
246 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
247 fprintf(stderr,
248 "Error loading key \"%s\": %s\n",
249 filename, ssh_err(r));
250 fail_load:
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000251 clear_pass();
djm@openbsd.org141efe42015-01-14 20:05:27 +0000252 sshbuf_free(keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000253 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100254 }
Ben Lindstromee617942001-04-10 02:45:32 +0000255 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000256 snprintf(msg, sizeof msg,
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000257 "Bad passphrase, try again for %s%s: ", filename,
halex@openbsd.org5d34aa92015-02-03 00:34:14 +0000258 confirm ? " (will confirm each use)" : "");
Damien Miller95def091999-11-25 00:26:21 +1100259 }
260 }
tim@openbsd.org2681cdb2015-09-13 13:48:19 +0000261 if (comment == NULL || *comment == '\0')
262 comment = xstrdup(filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000263 sshbuf_free(keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000264
djm@openbsd.org141efe42015-01-14 20:05:27 +0000265 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
266 lifetime, confirm)) == 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000267 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000268 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000269 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000270 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000271 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100272 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100273 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000274 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000275 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000276 fprintf(stderr, "Could not add identity \"%s\": %s\n",
277 filename, ssh_err(r));
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000278 }
279
Damien Miller8f4279e2011-10-18 16:06:33 +1100280 /* Skip trying to load the cert if requested */
281 if (key_only)
282 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100283
284 /* Now try to add the certificate flavour too */
285 xasprintf(&certpath, "%s-cert.pub", filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000286 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
287 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
288 error("Failed to load certificate \"%s\": %s",
289 certpath, ssh_err(r));
Damien Millerc6afb5f2010-05-21 14:56:47 +1000290 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000291 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000292
djm@openbsd.org141efe42015-01-14 20:05:27 +0000293 if (!sshkey_equal_public(cert, private)) {
Damien Millerc6afb5f2010-05-21 14:56:47 +1000294 error("Certificate %s does not match private key %s",
295 certpath, filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000296 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000297 goto out;
298 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100299
Damien Millerc6afb5f2010-05-21 14:56:47 +1000300 /* Graft with private bits */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000301 if ((r = sshkey_to_certified(private)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000302 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
303 sshkey_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000304 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100305 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000306 if ((r = sshkey_cert_copy(cert, private)) != 0) {
307 error("%s: key_cert_copy: %s", __func__, ssh_err(r));
308 sshkey_free(cert);
309 goto out;
310 }
311 sshkey_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100312
djm@openbsd.org141efe42015-01-14 20:05:27 +0000313 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
314 lifetime, confirm)) != 0) {
315 error("Certificate %s (%s) add failed: %s", certpath,
316 private->cert->key_id, ssh_err(r));
317 goto out;
Damien Millerc6afb5f2010-05-21 14:56:47 +1000318 }
319 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
320 private->cert->key_id);
321 if (lifetime != 0)
322 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
323 if (confirm != 0)
324 fprintf(stderr, "The user must confirm each use of the key\n");
325 out:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000326 free(certpath);
Darren Tuckera627d422013-06-02 07:31:17 +1000327 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000328 sshkey_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100329
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000330 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
332
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000333static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000334update_card(int agent_fd, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000335{
Damien Milleref275ea2013-12-29 17:44:07 +1100336 char *pin = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000337 int r, ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000338
Damien Milleref275ea2013-12-29 17:44:07 +1100339 if (add) {
340 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
341 RP_ALLOW_STDIN)) == NULL)
342 return -1;
343 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000344
djm@openbsd.org141efe42015-01-14 20:05:27 +0000345 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
346 lifetime, confirm)) == 0) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000347 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100348 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100349 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000350 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000351 fprintf(stderr, "Could not %s card \"%s\": %s\n",
352 add ? "add" : "remove", id, ssh_err(r));
Damien Miller00111382003-03-10 11:21:17 +1100353 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000354 }
Darren Tuckera627d422013-06-02 07:31:17 +1000355 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100356 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000357}
358
Damien Miller43cba342002-02-05 12:12:49 +1100359static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000360list_identities(int agent_fd, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000362 char *fp;
markus@openbsd.org47842f72015-03-25 19:29:58 +0000363 int r, had_identities = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000364 struct ssh_identitylist *idlist;
365 size_t i;
markus@openbsd.org47842f72015-03-25 19:29:58 +0000366#ifdef WITH_SSH1
367 int version = 1;
368#else
369 int version = 2;
370#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
markus@openbsd.org47842f72015-03-25 19:29:58 +0000372 for (; version <= 2; version++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000373 if ((r = ssh_fetch_identitylist(agent_fd, version,
374 &idlist)) != 0) {
375 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
376 fprintf(stderr, "error fetching identities for "
377 "protocol %d: %s\n", version, ssh_err(r));
378 continue;
379 }
380 for (i = 0; i < idlist->nkeys; i++) {
Damien Millerad833b32000-08-23 10:46:23 +1000381 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000382 if (do_fp) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000383 fp = sshkey_fingerprint(idlist->keys[i],
384 fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000385 printf("%u %s %s (%s)\n",
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000386 sshkey_size(idlist->keys[i]),
387 fp == NULL ? "(null)" : fp,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000388 idlist->comments[i],
389 sshkey_type(idlist->keys[i]));
Darren Tuckera627d422013-06-02 07:31:17 +1000390 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100391 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000392 if ((r = sshkey_write(idlist->keys[i],
393 stdout)) != 0) {
394 fprintf(stderr, "sshkey_write: %s\n",
395 ssh_err(r));
396 continue;
397 }
398 fprintf(stdout, " %s\n", idlist->comments[i]);
Damien Miller95def091999-11-25 00:26:21 +1100399 }
400 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000401 ssh_free_identitylist(idlist);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100402 }
Damien Miller43cba342002-02-05 12:12:49 +1100403 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100404 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100405 return -1;
406 }
407 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408}
409
Damien Miller6e1057c2002-01-22 23:05:59 +1100410static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000411lock_agent(int agent_fd, int lock)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000412{
413 char prompt[100], *p1, *p2;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000414 int r, passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000415
Ben Lindstrom2f717042002-06-06 21:52:03 +0000416 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
417 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
418 if (lock) {
419 strlcpy(prompt, "Again: ", sizeof prompt);
420 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
421 if (strcmp(p1, p2) != 0) {
422 fprintf(stderr, "Passwords do not match.\n");
423 passok = 0;
424 }
Damien Millera5103f42014-02-04 11:20:14 +1100425 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000426 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000427 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000428 if (passok) {
429 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
430 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
431 ret = 0;
432 } else {
433 fprintf(stderr, "Failed to %slock agent: %s\n",
434 lock ? "" : "un", ssh_err(r));
435 }
436 }
Damien Millera5103f42014-02-04 11:20:14 +1100437 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000438 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000439 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000440}
441
442static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000443do_file(int agent_fd, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100444{
445 if (deleting) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000446 if (delete_file(agent_fd, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100447 return -1;
448 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000449 if (add_file(agent_fd, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100450 return -1;
451 }
452 return 0;
453}
454
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000455static void
456usage(void)
457{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000458 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000459 fprintf(stderr, "Options:\n");
460 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000461 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000462 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100463 fprintf(stderr, " -k Load only keys and not certificates.\n");
464 fprintf(stderr, " -c Require confirmation to sign using identities\n");
465 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000466 fprintf(stderr, " -d Delete identity.\n");
467 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000468 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000469 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100470 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
471 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000472}
473
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474int
Damien Miller01ab4a21999-10-28 15:23:30 +1000475main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000477 extern char *optarg;
478 extern int optind;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000479 int agent_fd;
Damien Miller7ea845e2010-02-12 09:21:02 +1100480 char *pkcs11provider = NULL;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000481 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000482 int xflag = 0, lflag = 0, Dflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +0000484 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +1000485 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
486 sanitise_stdfd();
487
Damien Miller59d3d5b2003-08-22 09:34:41 +1000488 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000489 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000490
Damien Miller72ef7c12015-01-15 02:21:31 +1100491#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +1000492 OpenSSL_add_all_algorithms();
Damien Miller72ef7c12015-01-15 02:21:31 +1100493#endif
Damien Millerad833b32000-08-23 10:46:23 +1000494
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000495 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000496
djm@openbsd.org141efe42015-01-14 20:05:27 +0000497 /* First, get a connection to the authentication agent. */
498 switch (r = ssh_get_authentication_socket(&agent_fd)) {
499 case 0:
500 break;
501 case SSH_ERR_AGENT_NOT_PRESENT:
502 fprintf(stderr, "Could not open a connection to your "
503 "authentication agent.\n");
504 exit(2);
505 default:
506 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
Damien Miller43cba342002-02-05 12:12:49 +1100507 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000509
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000510 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000511 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000512 case 'E':
513 fingerprint_hash = ssh_digest_alg_by_name(optarg);
514 if (fingerprint_hash == -1)
515 fatal("Invalid hash algorithm \"%s\"", optarg);
516 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100517 case 'k':
518 key_only = 1;
519 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000520 case 'l':
521 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000522 if (lflag != 0)
523 fatal("-%c flag already specified", lflag);
524 lflag = ch;
525 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000526 case 'x':
527 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000528 if (xflag != 0)
529 fatal("-%c flag already specified", xflag);
530 xflag = ch;
531 break;
Damien Miller6c711792003-01-24 11:36:23 +1100532 case 'c':
533 confirm = 1;
534 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000535 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100536 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000537 break;
538 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000539 Dflag = 1;
540 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000541 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100542 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000543 break;
544 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100545 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100546 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000547 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000548 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000549 if ((lifetime = convtime(optarg)) == -1) {
550 fprintf(stderr, "Invalid lifetime\n");
551 ret = 1;
552 goto done;
553 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000554 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000555 default:
556 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000557 ret = 1;
558 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000559 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000561
562 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
563 fatal("Invalid combination of actions");
564 else if (xflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000565 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000566 ret = 1;
567 goto done;
568 } else if (lflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000569 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000570 ret = 1;
571 goto done;
572 } else if (Dflag) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000573 if (delete_all(agent_fd) == -1)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000574 ret = 1;
575 goto done;
576 }
577
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000578 argc -= optind;
579 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100580 if (pkcs11provider != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000581 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000582 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000583 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000584 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000585 if (argc == 0) {
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000586 char buf[PATH_MAX];
Damien Miller6e1057c2002-01-22 23:05:59 +1100587 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000588 struct stat st;
589 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100590
591 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100592 fprintf(stderr, "No user found with uid %u\n",
593 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000594 ret = 1;
595 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100596 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100597
Darren Tucker47eede72005-03-14 23:08:12 +1100598 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000599 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100600 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000601 if (stat(buf, &st) < 0)
602 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000603 if (do_file(agent_fd, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000604 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000605 else
606 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000607 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000608 if (count == 0)
609 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000610 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100611 for (i = 0; i < argc; i++) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000612 if (do_file(agent_fd, deleting, key_only,
613 argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100614 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000615 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616 }
Ben Lindstromee617942001-04-10 02:45:32 +0000617 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000618
619done:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000620 ssh_close_authentication_socket(agent_fd);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000621 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622}