blob: 3680ab07ab7a9e875e09f2d1e3b5f7acf4e24f6c [file] [log] [blame]
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001/* $OpenBSD: ssh-add.c,v 1.115 2014/12/21 22:27:56 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
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>
Damien Miller9f2abc42006-07-10 20:53:08 +100054
Damien Millerd7834352006-08-05 12:39:39 +100055#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "rsa.h"
58#include "log.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100060#include "buffer.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[] = {
73 _PATH_SSH_CLIENT_ID_RSA,
74 _PATH_SSH_CLIENT_ID_DSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110075#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100076 _PATH_SSH_CLIENT_ID_ECDSA,
Damien Millerdd190dd2010-11-11 14:17:02 +110077#endif
Damien Miller059321d2013-12-18 17:49:48 +110078 _PATH_SSH_CLIENT_ID_ED25519,
Ben Lindstrom6328ab32002-03-22 02:54:23 +000079 _PATH_SSH_CLIENT_IDENTITY,
Damien Miller6e1057c2002-01-22 23:05:59 +110080 NULL
81};
82
djm@openbsd.org56d1c832014-12-21 22:27:55 +000083static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
84
Ben Lindstrom61d328a2002-06-06 21:54:57 +000085/* Default lifetime (0 == forever) */
Ben Lindstrom1775c9c2002-06-11 15:51:54 +000086static int lifetime = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +110087
Damien Miller6c711792003-01-24 11:36:23 +110088/* User has to confirm key use */
89static int confirm = 0;
90
Ben Lindstromee617942001-04-10 02:45:32 +000091/* we keep a cache of one passphrases */
92static char *pass = NULL;
Ben Lindstrombba81212001-06-25 05:01:22 +000093static void
Ben Lindstromee617942001-04-10 02:45:32 +000094clear_pass(void)
95{
96 if (pass) {
Damien Millera5103f42014-02-04 11:20:14 +110097 explicit_bzero(pass, strlen(pass));
Darren Tuckera627d422013-06-02 07:31:17 +100098 free(pass);
Ben Lindstromee617942001-04-10 02:45:32 +000099 pass = NULL;
100 }
101}
102
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000103static int
Damien Miller33a81362012-12-03 09:50:24 +1100104delete_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105{
Damien Miller33a81362012-12-03 09:50:24 +1100106 Key *public = NULL, *cert = NULL;
107 char *certpath = NULL, *comment = NULL;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000108 int ret = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Ben Lindstromd0fca422001-03-26 13:44:06 +0000110 public = key_load_public(filename, &comment);
111 if (public == NULL) {
112 printf("Bad key file %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000113 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100114 }
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000115 if (ssh_remove_identity(ac, public)) {
Damien Miller95def091999-11-25 00:26:21 +1100116 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000117 ret = 0;
118 } else
Damien Miller95def091999-11-25 00:26:21 +1100119 fprintf(stderr, "Could not remove identity: %s\n", filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000120
Damien Miller33a81362012-12-03 09:50:24 +1100121 if (key_only)
122 goto out;
123
124 /* Now try to delete the corresponding certificate too */
125 free(comment);
Darren Tucker8a965222012-12-07 13:07:02 +1100126 comment = NULL;
Damien Miller33a81362012-12-03 09:50:24 +1100127 xasprintf(&certpath, "%s-cert.pub", filename);
128 if ((cert = key_load_public(certpath, &comment)) == NULL)
129 goto out;
130 if (!key_equal_public(cert, public))
131 fatal("Certificate %s does not match private key %s",
132 certpath, filename);
133
134 if (ssh_remove_identity(ac, cert)) {
135 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
136 comment);
137 ret = 0;
138 } else
139 fprintf(stderr, "Could not remove identity: %s\n", certpath);
140
141 out:
142 if (cert != NULL)
143 key_free(cert);
144 if (public != NULL)
145 key_free(public);
146 free(certpath);
147 free(comment);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100148
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000149 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150}
151
Damien Millerad833b32000-08-23 10:46:23 +1000152/* Send a request to remove all identities. */
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000153static int
Damien Miller01ab4a21999-10-28 15:23:30 +1000154delete_all(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155{
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000156 int ret = -1;
Damien Millerad833b32000-08-23 10:46:23 +1000157
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000158 if (ssh_remove_all_identities(ac, 1))
159 ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000160 /* ignore error-code for ssh2 */
161 ssh_remove_all_identities(ac, 2);
162
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000163 if (ret == 0)
Damien Miller95def091999-11-25 00:26:21 +1100164 fprintf(stderr, "All identities removed.\n");
165 else
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000166 fprintf(stderr, "Failed to remove all identities.\n");
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000167
168 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169}
170
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000171static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100172add_file(AuthenticationConnection *ac, const char *filename, int key_only)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller0a80ca12010-02-27 07:55:05 +1100174 Key *private, *cert;
Ben Lindstrom5eb97b62001-04-19 20:33:07 +0000175 char *comment = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100176 char msg[1024], *certpath = NULL;
Damien Miller86687062014-07-02 15:28:02 +1000177 int r, fd, perms_ok, ret = -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000178 Buffer keyblob;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller2ce12ef2011-05-05 14:17:18 +1000180 if (strcmp(filename, "-") == 0) {
181 fd = STDIN_FILENO;
182 filename = "(stdin)";
183 } else if ((fd = open(filename, O_RDONLY)) < 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000184 perror(filename);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000185 return -1;
Damien Millerad833b32000-08-23 10:46:23 +1000186 }
Damien Miller8275fad2006-03-15 12:06:23 +1100187
188 /*
189 * Since we'll try to load a keyfile multiple times, permission errors
190 * will occur multiple times, so check perms first and bail if wrong.
191 */
Damien Miller2ce12ef2011-05-05 14:17:18 +1000192 if (fd != STDIN_FILENO) {
193 perms_ok = key_perm_ok(fd, filename);
194 if (!perms_ok) {
195 close(fd);
196 return -1;
197 }
198 }
199 buffer_init(&keyblob);
200 if (!key_load_file(fd, filename, &keyblob)) {
201 buffer_free(&keyblob);
202 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100203 return -1;
Damien Miller2ce12ef2011-05-05 14:17:18 +1000204 }
205 close(fd);
Damien Miller8275fad2006-03-15 12:06:23 +1100206
Damien Miller95def091999-11-25 00:26:21 +1100207 /* At first, try empty passphrase */
Damien Millerec3d0e22014-07-02 15:30:00 +1000208 if ((r = sshkey_parse_private_fileblob(&keyblob, "", filename,
Damien Miller86687062014-07-02 15:28:02 +1000209 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE)
Damien Millerec3d0e22014-07-02 15:30:00 +1000210 fatal("Cannot parse %s: %s", filename, ssh_err(r));
Ben Lindstromee617942001-04-10 02:45:32 +0000211 /* try last */
Damien Miller86687062014-07-02 15:28:02 +1000212 if (private == NULL && pass != NULL) {
Damien Millerec3d0e22014-07-02 15:30:00 +1000213 if ((r = sshkey_parse_private_fileblob(&keyblob, pass, filename,
Damien Miller86687062014-07-02 15:28:02 +1000214 &private, &comment)) != 0 &&
215 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
216 fatal("Cannot parse %s: %s", filename, ssh_err(r));
217 }
Damien Miller1d0df322014-07-11 09:19:04 +1000218 if (comment == NULL)
219 comment = xstrdup(filename);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000220 if (private == NULL) {
Ben Lindstromee617942001-04-10 02:45:32 +0000221 /* clear passphrase since it did not work */
222 clear_pass();
Ben Lindstrom8a137132001-05-02 22:40:12 +0000223 snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
Damien Miller0dc1bef2005-07-17 17:22:45 +1000224 comment);
Damien Miller5428f641999-11-25 11:54:57 +1100225 for (;;) {
Ben Lindstrom949974b2001-06-25 05:20:31 +0000226 pass = read_passphrase(msg, RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100227 if (strcmp(pass, "") == 0) {
Ben Lindstrom7457f2a2001-04-14 23:10:09 +0000228 clear_pass();
Darren Tuckera627d422013-06-02 07:31:17 +1000229 free(comment);
Damien Miller2ce12ef2011-05-05 14:17:18 +1000230 buffer_free(&keyblob);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000231 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100232 }
Damien Miller86687062014-07-02 15:28:02 +1000233 if ((r = sshkey_parse_private_fileblob(&keyblob,
Damien Millerec3d0e22014-07-02 15:30:00 +1000234 pass, filename, &private, NULL)) != 0 &&
Damien Miller86687062014-07-02 15:28:02 +1000235 r != SSH_ERR_KEY_WRONG_PASSPHRASE)
236 fatal("Cannot parse %s: %s",
237 filename, ssh_err(r));
Ben Lindstromd0fca422001-03-26 13:44:06 +0000238 if (private != NULL)
Damien Miller95def091999-11-25 00:26:21 +1100239 break;
Ben Lindstromee617942001-04-10 02:45:32 +0000240 clear_pass();
Damien Miller80163e12003-06-18 20:29:18 +1000241 snprintf(msg, sizeof msg,
242 "Bad passphrase, try again for %.200s: ", comment);
Damien Miller95def091999-11-25 00:26:21 +1100243 }
244 }
Damien Miller2ce12ef2011-05-05 14:17:18 +1000245 buffer_free(&keyblob);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000246
Damien Millera8e06ce2003-11-21 23:48:55 +1100247 if (ssh_add_identity_constrained(ac, private, comment, lifetime,
248 confirm)) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000249 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000250 ret = 0;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000251 if (lifetime != 0)
Ben Lindstrom93576d92002-12-23 02:06:19 +0000252 fprintf(stderr,
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000253 "Lifetime set to %d seconds\n", lifetime);
Damien Millera8e06ce2003-11-21 23:48:55 +1100254 if (confirm != 0)
Damien Miller6c711792003-01-24 11:36:23 +1100255 fprintf(stderr,
Damien Millerc6afb5f2010-05-21 14:56:47 +1000256 "The user must confirm each use of the key\n");
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000257 } else {
Damien Miller95def091999-11-25 00:26:21 +1100258 fprintf(stderr, "Could not add identity: %s\n", filename);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000259 }
260
Damien Miller8f4279e2011-10-18 16:06:33 +1100261 /* Skip trying to load the cert if requested */
262 if (key_only)
263 goto out;
Damien Miller0a80ca12010-02-27 07:55:05 +1100264
265 /* Now try to add the certificate flavour too */
266 xasprintf(&certpath, "%s-cert.pub", filename);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000267 if ((cert = key_load_public(certpath, NULL)) == NULL)
268 goto out;
269
270 if (!key_equal_public(cert, private)) {
271 error("Certificate %s does not match private key %s",
272 certpath, filename);
Damien Miller0a80ca12010-02-27 07:55:05 +1100273 key_free(cert);
Damien Millerc6afb5f2010-05-21 14:56:47 +1000274 goto out;
275 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100276
Damien Millerc6afb5f2010-05-21 14:56:47 +1000277 /* Graft with private bits */
278 if (key_to_certified(private, key_cert_is_legacy(cert)) != 0) {
279 error("%s: key_to_certified failed", __func__);
280 key_free(cert);
281 goto out;
Damien Miller0bd41862010-03-03 10:25:41 +1100282 }
Damien Millerc6afb5f2010-05-21 14:56:47 +1000283 key_cert_copy(cert, private);
284 key_free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100285
Damien Millerc6afb5f2010-05-21 14:56:47 +1000286 if (!ssh_add_identity_constrained(ac, private, comment,
287 lifetime, confirm)) {
288 error("Certificate %s (%s) add failed", certpath,
289 private->cert->key_id);
290 }
291 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
292 private->cert->key_id);
293 if (lifetime != 0)
294 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
295 if (confirm != 0)
296 fprintf(stderr, "The user must confirm each use of the key\n");
297 out:
Damien Miller8f4279e2011-10-18 16:06:33 +1100298 if (certpath != NULL)
Darren Tuckera627d422013-06-02 07:31:17 +1000299 free(certpath);
300 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000301 key_free(private);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100302
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000303 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304}
305
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000306static int
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000307update_card(AuthenticationConnection *ac, int add, const char *id)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000308{
Damien Milleref275ea2013-12-29 17:44:07 +1100309 char *pin = NULL;
Damien Miller00111382003-03-10 11:21:17 +1100310 int ret = -1;
Ben Lindstromba72d302002-03-22 03:51:06 +0000311
Damien Milleref275ea2013-12-29 17:44:07 +1100312 if (add) {
313 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
314 RP_ALLOW_STDIN)) == NULL)
315 return -1;
316 }
Ben Lindstromba72d302002-03-22 03:51:06 +0000317
Damien Milleref275ea2013-12-29 17:44:07 +1100318 if (ssh_update_card(ac, add, id, pin == NULL ? "" : pin,
319 lifetime, confirm)) {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000320 fprintf(stderr, "Card %s: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100321 add ? "added" : "removed", id);
Damien Miller00111382003-03-10 11:21:17 +1100322 ret = 0;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000323 } else {
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000324 fprintf(stderr, "Could not %s card: %s\n",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100325 add ? "add" : "remove", id);
Damien Miller00111382003-03-10 11:21:17 +1100326 ret = -1;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000327 }
Darren Tuckera627d422013-06-02 07:31:17 +1000328 free(pin);
Damien Miller00111382003-03-10 11:21:17 +1100329 return ret;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000330}
331
Damien Miller43cba342002-02-05 12:12:49 +1100332static int
Ben Lindstromcfccef92001-03-13 04:57:58 +0000333list_identities(AuthenticationConnection *ac, int do_fp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334{
Damien Millerad833b32000-08-23 10:46:23 +1000335 Key *key;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000336 char *comment, *fp;
Damien Millerad833b32000-08-23 10:46:23 +1000337 int had_identities = 0;
338 int version;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Damien Millerad833b32000-08-23 10:46:23 +1000340 for (version = 1; version <= 2; version++) {
341 for (key = ssh_get_first_identity(ac, &comment, version);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100342 key != NULL;
343 key = ssh_get_next_identity(ac, &comment, version)) {
Damien Millerad833b32000-08-23 10:46:23 +1000344 had_identities = 1;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000345 if (do_fp) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000346 fp = key_fingerprint(key, fingerprint_hash,
347 SSH_FP_DEFAULT);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100348 printf("%d %s %s (%s)\n",
Ben Lindstromcfccef92001-03-13 04:57:58 +0000349 key_size(key), fp, comment, key_type(key));
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(fp);
Damien Miller95def091999-11-25 00:26:21 +1100351 } else {
Damien Millerad833b32000-08-23 10:46:23 +1000352 if (!key_write(key, stdout))
353 fprintf(stderr, "key_write failed");
354 fprintf(stdout, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +1100355 }
Damien Millerad833b32000-08-23 10:46:23 +1000356 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000357 free(comment);
Damien Miller95def091999-11-25 00:26:21 +1100358 }
Damien Miller10f6f6b1999-11-17 17:29:08 +1100359 }
Damien Miller43cba342002-02-05 12:12:49 +1100360 if (!had_identities) {
Damien Miller95def091999-11-25 00:26:21 +1100361 printf("The agent has no identities.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100362 return -1;
363 }
364 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365}
366
Damien Miller6e1057c2002-01-22 23:05:59 +1100367static int
Ben Lindstrom2f717042002-06-06 21:52:03 +0000368lock_agent(AuthenticationConnection *ac, int lock)
369{
370 char prompt[100], *p1, *p2;
371 int passok = 1, ret = -1;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000372
Ben Lindstrom2f717042002-06-06 21:52:03 +0000373 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
374 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
375 if (lock) {
376 strlcpy(prompt, "Again: ", sizeof prompt);
377 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
378 if (strcmp(p1, p2) != 0) {
379 fprintf(stderr, "Passwords do not match.\n");
380 passok = 0;
381 }
Damien Millera5103f42014-02-04 11:20:14 +1100382 explicit_bzero(p2, strlen(p2));
Darren Tuckera627d422013-06-02 07:31:17 +1000383 free(p2);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000384 }
385 if (passok && ssh_lock_agent(ac, lock, p1)) {
386 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
387 ret = 0;
388 } else
389 fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
Damien Millera5103f42014-02-04 11:20:14 +1100390 explicit_bzero(p1, strlen(p1));
Darren Tuckera627d422013-06-02 07:31:17 +1000391 free(p1);
Ben Lindstrom33907492002-06-27 00:21:59 +0000392 return (ret);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000393}
394
395static int
Damien Miller8f4279e2011-10-18 16:06:33 +1100396do_file(AuthenticationConnection *ac, int deleting, int key_only, char *file)
Damien Miller6e1057c2002-01-22 23:05:59 +1100397{
398 if (deleting) {
Damien Miller33a81362012-12-03 09:50:24 +1100399 if (delete_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100400 return -1;
401 } else {
Damien Miller8f4279e2011-10-18 16:06:33 +1100402 if (add_file(ac, file, key_only) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100403 return -1;
404 }
405 return 0;
406}
407
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000408static void
409usage(void)
410{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000411 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000412 fprintf(stderr, "Options:\n");
413 fprintf(stderr, " -l List fingerprints of all identities.\n");
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000414 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000415 fprintf(stderr, " -L List public key parameters of all identities.\n");
Darren Tuckere68cf842011-11-04 10:51:51 +1100416 fprintf(stderr, " -k Load only keys and not certificates.\n");
417 fprintf(stderr, " -c Require confirmation to sign using identities\n");
418 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000419 fprintf(stderr, " -d Delete identity.\n");
420 fprintf(stderr, " -D Delete all identities.\n");
Ben Lindstrom163f3b82002-06-06 21:53:11 +0000421 fprintf(stderr, " -x Lock agent.\n");
Damien Miller2138d152002-09-22 01:26:00 +1000422 fprintf(stderr, " -X Unlock agent.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +1100423 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
424 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000425}
426
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427int
Damien Miller01ab4a21999-10-28 15:23:30 +1000428main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000430 extern char *optarg;
431 extern int optind;
Damien Miller95def091999-11-25 00:26:21 +1100432 AuthenticationConnection *ac = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100433 char *pkcs11provider = NULL;
Damien Miller8f4279e2011-10-18 16:06:33 +1100434 int i, ch, deleting = 0, ret = 0, key_only = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000435 int xflag = 0, lflag = 0, Dflag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436
Darren Tuckerce321d82005-10-03 18:11:24 +1000437 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
438 sanitise_stdfd();
439
Damien Miller59d3d5b2003-08-22 09:34:41 +1000440 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom93d1fe82001-05-06 02:57:20 +0000441 seed_rng();
Damien Millerf9b625c2000-07-09 22:42:32 +1000442
Damien Miller4314c2b2010-09-10 11:12:09 +1000443 OpenSSL_add_all_algorithms();
Damien Millerad833b32000-08-23 10:46:23 +1000444
millert@openbsd.orgdb995f22014-11-26 18:34:51 +0000445 setvbuf(stdout, NULL, _IOLBF, 0);
Damien Millerd7ca2cd2014-07-03 21:23:01 +1000446
Damien Miller95def091999-11-25 00:26:21 +1100447 /* At first, get a connection to the authentication agent. */
448 ac = ssh_get_authentication_connection();
449 if (ac == NULL) {
Darren Tuckerb736d8d2005-11-22 19:37:08 +1100450 fprintf(stderr,
451 "Could not open a connection to your authentication agent.\n");
Damien Miller43cba342002-02-05 12:12:49 +1100452 exit(2);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000454 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000455 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000456 case 'E':
457 fingerprint_hash = ssh_digest_alg_by_name(optarg);
458 if (fingerprint_hash == -1)
459 fatal("Invalid hash algorithm \"%s\"", optarg);
460 break;
Damien Miller8f4279e2011-10-18 16:06:33 +1100461 case 'k':
462 key_only = 1;
463 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000464 case 'l':
465 case 'L':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000466 if (lflag != 0)
467 fatal("-%c flag already specified", lflag);
468 lflag = ch;
469 break;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000470 case 'x':
471 case 'X':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000472 if (xflag != 0)
473 fatal("-%c flag already specified", xflag);
474 xflag = ch;
475 break;
Damien Miller6c711792003-01-24 11:36:23 +1100476 case 'c':
477 confirm = 1;
478 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000479 case 'd':
Damien Miller95def091999-11-25 00:26:21 +1100480 deleting = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000481 break;
482 case 'D':
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000483 Dflag = 1;
484 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000485 case 's':
Damien Miller7ea845e2010-02-12 09:21:02 +1100486 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000487 break;
488 case 'e':
Damien Miller9f0f5c62001-12-21 14:45:46 +1100489 deleting = 1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100490 pkcs11provider = optarg;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000491 break;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000492 case 't':
Ben Lindstrom1775c9c2002-06-11 15:51:54 +0000493 if ((lifetime = convtime(optarg)) == -1) {
494 fprintf(stderr, "Invalid lifetime\n");
495 ret = 1;
496 goto done;
497 }
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000498 break;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000499 default:
500 usage();
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000501 ret = 1;
502 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000503 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000505
506 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
507 fatal("Invalid combination of actions");
508 else if (xflag) {
509 if (lock_agent(ac, xflag == 'x' ? 1 : 0) == -1)
510 ret = 1;
511 goto done;
512 } else if (lflag) {
513 if (list_identities(ac, lflag == 'l' ? 1 : 0) == -1)
514 ret = 1;
515 goto done;
516 } else if (Dflag) {
517 if (delete_all(ac) == -1)
518 ret = 1;
519 goto done;
520 }
521
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000522 argc -= optind;
523 argv += optind;
Damien Miller7ea845e2010-02-12 09:21:02 +1100524 if (pkcs11provider != NULL) {
525 if (update_card(ac, !deleting, pkcs11provider) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000526 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000527 goto done;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000528 }
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000529 if (argc == 0) {
Damien Miller6e1057c2002-01-22 23:05:59 +1100530 char buf[MAXPATHLEN];
531 struct passwd *pw;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000532 struct stat st;
533 int count = 0;
Damien Miller6e1057c2002-01-22 23:05:59 +1100534
535 if ((pw = getpwuid(getuid())) == NULL) {
Damien Millercaf6dd62000-08-29 11:33:50 +1100536 fprintf(stderr, "No user found with uid %u\n",
537 (u_int)getuid());
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000538 ret = 1;
539 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100540 }
Damien Miller6e1057c2002-01-22 23:05:59 +1100541
Darren Tucker47eede72005-03-14 23:08:12 +1100542 for (i = 0; default_files[i]; i++) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000543 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
Damien Miller6e1057c2002-01-22 23:05:59 +1100544 default_files[i]);
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000545 if (stat(buf, &st) < 0)
546 continue;
Damien Miller8f4279e2011-10-18 16:06:33 +1100547 if (do_file(ac, deleting, key_only, buf) == -1)
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000548 ret = 1;
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000549 else
550 count++;
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000551 }
Ben Lindstrom58b391b2002-03-22 03:21:16 +0000552 if (count == 0)
553 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000554 } else {
Darren Tucker47eede72005-03-14 23:08:12 +1100555 for (i = 0; i < argc; i++) {
Damien Miller8f4279e2011-10-18 16:06:33 +1100556 if (do_file(ac, deleting, key_only, argv[i]) == -1)
Damien Miller6e1057c2002-01-22 23:05:59 +1100557 ret = 1;
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 }
Ben Lindstromee617942001-04-10 02:45:32 +0000560 clear_pass();
Ben Lindstrom44e49af2001-07-04 05:03:51 +0000561
562done:
Damien Miller95def091999-11-25 00:26:21 +1100563 ssh_close_authentication_connection(ac);
Ben Lindstrom569f88d2001-10-03 17:43:01 +0000564 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565}