dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-add.c,v 1.153 2020/02/18 08:58:33 dtucker Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Adds an identity to the authentication server, or removes an identity. |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * 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 Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 14 | * SSH2 implementation, |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | |
djm@openbsd.org | 670104b | 2019-09-06 05:23:55 +0000 | [diff] [blame] | 43 | #ifdef WITH_OPENSSL |
| 44 | # include <openssl/evp.h> |
| 45 | # include "openbsd-compat/openssl-compat.h" |
| 46 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 48 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 49 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 50 | #include <pwd.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 51 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 52 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 53 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 54 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 55 | #include <unistd.h> |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 56 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 57 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 58 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 60 | #include "log.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 61 | #include "sshkey.h" |
| 62 | #include "sshbuf.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 63 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 64 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 65 | #include "pathnames.h" |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 66 | #include "misc.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 67 | #include "ssherr.h" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 68 | #include "digest.h" |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 69 | #include "ssh-sk.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | |
Ben Lindstrom | ddfb1e3 | 2001-08-06 22:06:35 +0000 | [diff] [blame] | 71 | /* argv0 */ |
| 72 | extern char *__progname; |
| 73 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 74 | /* Default files to add */ |
| 75 | static char *default_files[] = { |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 76 | #ifdef WITH_OPENSSL |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 77 | _PATH_SSH_CLIENT_ID_RSA, |
| 78 | _PATH_SSH_CLIENT_ID_DSA, |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 79 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 80 | _PATH_SSH_CLIENT_ID_ECDSA, |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 81 | _PATH_SSH_CLIENT_ID_ECDSA_SK, |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 82 | #endif |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 83 | #endif /* WITH_OPENSSL */ |
Damien Miller | 059321d | 2013-12-18 17:49:48 +1100 | [diff] [blame] | 84 | _PATH_SSH_CLIENT_ID_ED25519, |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 85 | _PATH_SSH_CLIENT_ID_ED25519_SK, |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 86 | _PATH_SSH_CLIENT_ID_XMSS, |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 87 | NULL |
| 88 | }; |
| 89 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 90 | static int fingerprint_hash = SSH_FP_HASH_DEFAULT; |
| 91 | |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 92 | /* Default lifetime (0 == forever) */ |
dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 93 | static long lifetime = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 94 | |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 95 | /* User has to confirm key use */ |
| 96 | static int confirm = 0; |
| 97 | |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 98 | /* Maximum number of signatures (XMSS) */ |
| 99 | static u_int maxsign = 0; |
| 100 | static u_int minleft = 0; |
| 101 | |
tim@openbsd.org | 2681cdb | 2015-09-13 13:48:19 +0000 | [diff] [blame] | 102 | /* we keep a cache of one passphrase */ |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 103 | static char *pass = NULL; |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 104 | static void |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 105 | clear_pass(void) |
| 106 | { |
| 107 | if (pass) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 108 | explicit_bzero(pass, strlen(pass)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 109 | free(pass); |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 110 | pass = NULL; |
| 111 | } |
| 112 | } |
| 113 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 114 | static int |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 115 | delete_file(int agent_fd, const char *filename, int key_only, int qflag) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 117 | struct sshkey *public, *cert = NULL; |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 118 | char *certpath = NULL, *comment = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 119 | int r, ret = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 121 | if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { |
| 122 | printf("Bad key file %s: %s\n", filename, ssh_err(r)); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 123 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 125 | if ((r = ssh_remove_identity(agent_fd, public)) == 0) { |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 126 | if (!qflag) { |
| 127 | fprintf(stderr, "Identity removed: %s (%s)\n", |
| 128 | filename, comment); |
| 129 | } |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 130 | ret = 0; |
| 131 | } else |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 132 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 133 | filename, ssh_err(r)); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 134 | |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 135 | if (key_only) |
| 136 | goto out; |
| 137 | |
| 138 | /* Now try to delete the corresponding certificate too */ |
| 139 | free(comment); |
Darren Tucker | 8a96522 | 2012-12-07 13:07:02 +1100 | [diff] [blame] | 140 | comment = NULL; |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 141 | xasprintf(&certpath, "%s-cert.pub", filename); |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 142 | if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { |
| 143 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 144 | error("Failed to load certificate \"%s\": %s", |
| 145 | certpath, ssh_err(r)); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 146 | goto out; |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 147 | } |
| 148 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 149 | if (!sshkey_equal_public(cert, public)) |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 150 | fatal("Certificate %s does not match private key %s", |
| 151 | certpath, filename); |
| 152 | |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 153 | if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 154 | if (!qflag) { |
| 155 | fprintf(stderr, "Identity removed: %s (%s)\n", |
| 156 | certpath, comment); |
| 157 | } |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 158 | ret = 0; |
| 159 | } else |
halex@openbsd.org | 1797f49 | 2015-02-21 21:46:57 +0000 | [diff] [blame] | 160 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 161 | certpath, ssh_err(r)); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 162 | |
| 163 | out: |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 164 | sshkey_free(cert); |
| 165 | sshkey_free(public); |
Damien Miller | 33a8136 | 2012-12-03 09:50:24 +1100 | [diff] [blame] | 166 | free(certpath); |
| 167 | free(comment); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 168 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 169 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | } |
| 171 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 172 | /* Send a request to remove all identities. */ |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 173 | static int |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 174 | delete_all(int agent_fd, int qflag) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | { |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 176 | int ret = -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 177 | |
djm@openbsd.org | 744bde7 | 2017-05-04 06:10:57 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Since the agent might be forwarded, old or non-OpenSSH, when asked |
| 180 | * to remove all keys, attempt to remove both protocol v.1 and v.2 |
| 181 | * keys. |
| 182 | */ |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 183 | if (ssh_remove_all_identities(agent_fd, 2) == 0) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 184 | ret = 0; |
markus@openbsd.org | 47842f7 | 2015-03-25 19:29:58 +0000 | [diff] [blame] | 185 | /* ignore error-code for ssh1 */ |
| 186 | ssh_remove_all_identities(agent_fd, 1); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 187 | |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 188 | if (ret != 0) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 189 | fprintf(stderr, "Failed to remove all identities.\n"); |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 190 | else if (!qflag) |
| 191 | fprintf(stderr, "All identities removed.\n"); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 192 | |
| 193 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | } |
| 195 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 196 | static int |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 197 | add_file(int agent_fd, const char *filename, int key_only, int qflag, |
| 198 | const char *skprovider) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 200 | struct sshkey *private, *cert; |
Ben Lindstrom | 5eb97b6 | 2001-04-19 20:33:07 +0000 | [diff] [blame] | 201 | char *comment = NULL; |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 202 | char msg[1024], *certpath = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 203 | int r, fd, ret = -1; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 204 | size_t i; |
| 205 | u_int32_t left; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 206 | struct sshbuf *keyblob; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 207 | struct ssh_identitylist *idlist; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 209 | if (strcmp(filename, "-") == 0) { |
| 210 | fd = STDIN_FILENO; |
| 211 | filename = "(stdin)"; |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 212 | } else if ((fd = open(filename, O_RDONLY)) == -1) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 213 | perror(filename); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 214 | return -1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 215 | } |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Since we'll try to load a keyfile multiple times, permission errors |
| 219 | * will occur multiple times, so check perms first and bail if wrong. |
| 220 | */ |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 221 | if (fd != STDIN_FILENO) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 222 | if (sshkey_perm_ok(fd, filename) != 0) { |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 223 | close(fd); |
| 224 | return -1; |
| 225 | } |
| 226 | } |
djm@openbsd.org | 99aa803 | 2020-01-25 23:02:13 +0000 | [diff] [blame] | 227 | if ((r = sshbuf_load_fd(fd, &keyblob)) != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 228 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 229 | filename, ssh_err(r)); |
| 230 | sshbuf_free(keyblob); |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 231 | close(fd); |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 232 | return -1; |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 233 | } |
| 234 | close(fd); |
Damien Miller | 8275fad | 2006-03-15 12:06:23 +1100 | [diff] [blame] | 235 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 236 | /* At first, try empty passphrase */ |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 237 | if ((r = sshkey_parse_private_fileblob(keyblob, "", &private, |
| 238 | &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 239 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 240 | filename, ssh_err(r)); |
| 241 | goto fail_load; |
| 242 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 243 | /* try last */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 244 | if (private == NULL && pass != NULL) { |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 245 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private, |
| 246 | &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 247 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 248 | filename, ssh_err(r)); |
| 249 | goto fail_load; |
| 250 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 251 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 252 | if (private == NULL) { |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 253 | /* clear passphrase since it did not work */ |
| 254 | clear_pass(); |
tim@openbsd.org | 2681cdb | 2015-09-13 13:48:19 +0000 | [diff] [blame] | 255 | snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ", |
| 256 | filename, confirm ? " (will confirm each use)" : ""); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 257 | for (;;) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 258 | pass = read_passphrase(msg, RP_ALLOW_STDIN); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 259 | if (strcmp(pass, "") == 0) |
| 260 | goto fail_load; |
| 261 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, |
tim@openbsd.org | 3c019a9 | 2015-09-13 14:39:16 +0000 | [diff] [blame] | 262 | &private, &comment)) == 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 263 | break; |
| 264 | else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 265 | fprintf(stderr, |
| 266 | "Error loading key \"%s\": %s\n", |
| 267 | filename, ssh_err(r)); |
| 268 | fail_load: |
Ben Lindstrom | 7457f2a | 2001-04-14 23:10:09 +0000 | [diff] [blame] | 269 | clear_pass(); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 270 | sshbuf_free(keyblob); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 271 | return -1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 272 | } |
Ben Lindstrom | ee61794 | 2001-04-10 02:45:32 +0000 | [diff] [blame] | 273 | clear_pass(); |
Damien Miller | 80163e1 | 2003-06-18 20:29:18 +1000 | [diff] [blame] | 274 | snprintf(msg, sizeof msg, |
tim@openbsd.org | 2681cdb | 2015-09-13 13:48:19 +0000 | [diff] [blame] | 275 | "Bad passphrase, try again for %s%s: ", filename, |
halex@openbsd.org | 5d34aa9 | 2015-02-03 00:34:14 +0000 | [diff] [blame] | 276 | confirm ? " (will confirm each use)" : ""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | } |
| 278 | } |
tim@openbsd.org | 2681cdb | 2015-09-13 13:48:19 +0000 | [diff] [blame] | 279 | if (comment == NULL || *comment == '\0') |
| 280 | comment = xstrdup(filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 281 | sshbuf_free(keyblob); |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 282 | |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 283 | /* For XMSS */ |
| 284 | if ((r = sshkey_set_filename(private, filename)) != 0) { |
| 285 | fprintf(stderr, "Could not add filename to private key: %s (%s)\n", |
| 286 | filename, comment); |
| 287 | goto out; |
| 288 | } |
| 289 | if (maxsign && minleft && |
| 290 | (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) { |
| 291 | for (i = 0; i < idlist->nkeys; i++) { |
| 292 | if (!sshkey_equal_public(idlist->keys[i], private)) |
| 293 | continue; |
| 294 | left = sshkey_signatures_left(idlist->keys[i]); |
| 295 | if (left < minleft) { |
| 296 | fprintf(stderr, |
| 297 | "Only %d signatures left.\n", left); |
| 298 | break; |
| 299 | } |
| 300 | fprintf(stderr, "Skipping update: "); |
| 301 | if (left == minleft) { |
| 302 | fprintf(stderr, |
| 303 | "required signatures left (%d).\n", left); |
| 304 | } else { |
| 305 | fprintf(stderr, |
| 306 | "more signatures left (%d) than" |
| 307 | " required (%d).\n", left, minleft); |
| 308 | } |
| 309 | ssh_free_identitylist(idlist); |
| 310 | goto out; |
| 311 | } |
| 312 | ssh_free_identitylist(idlist); |
| 313 | } |
| 314 | |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 315 | if (!sshkey_is_sk(private)) |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 316 | skprovider = NULL; /* Don't send constraint for other keys */ |
| 317 | else if (skprovider == NULL) { |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 318 | fprintf(stderr, "Cannot load authenticator-hosted key %s " |
| 319 | "without provider\n", filename); |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 320 | goto out; |
| 321 | } |
| 322 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 323 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 324 | lifetime, confirm, maxsign, skprovider)) == 0) { |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 325 | ret = 0; |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 326 | if (!qflag) { |
| 327 | fprintf(stderr, "Identity added: %s (%s)\n", |
| 328 | filename, comment); |
| 329 | if (lifetime != 0) { |
| 330 | fprintf(stderr, |
dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 331 | "Lifetime set to %ld seconds\n", lifetime); |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 332 | } |
| 333 | if (confirm != 0) { |
| 334 | fprintf(stderr, "The user must confirm " |
| 335 | "each use of the key\n"); |
| 336 | } |
| 337 | } |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 338 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 339 | fprintf(stderr, "Could not add identity \"%s\": %s\n", |
| 340 | filename, ssh_err(r)); |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 343 | /* Skip trying to load the cert if requested */ |
| 344 | if (key_only) |
| 345 | goto out; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 346 | |
| 347 | /* Now try to add the certificate flavour too */ |
| 348 | xasprintf(&certpath, "%s-cert.pub", filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 349 | if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { |
| 350 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 351 | error("Failed to load certificate \"%s\": %s", |
| 352 | certpath, ssh_err(r)); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 353 | goto out; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 354 | } |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 355 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 356 | if (!sshkey_equal_public(cert, private)) { |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 357 | error("Certificate %s does not match private key %s", |
| 358 | certpath, filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 359 | sshkey_free(cert); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 360 | goto out; |
| 361 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 362 | |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 363 | /* Graft with private bits */ |
djm@openbsd.org | c28fc62 | 2015-07-03 03:43:18 +0000 | [diff] [blame] | 364 | if ((r = sshkey_to_certified(private)) != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 365 | error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); |
| 366 | sshkey_free(cert); |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 367 | goto out; |
Damien Miller | 0bd4186 | 2010-03-03 10:25:41 +1100 | [diff] [blame] | 368 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 369 | if ((r = sshkey_cert_copy(cert, private)) != 0) { |
markus@openbsd.org | 7da5df1 | 2017-05-30 14:16:41 +0000 | [diff] [blame] | 370 | error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 371 | sshkey_free(cert); |
| 372 | goto out; |
| 373 | } |
| 374 | sshkey_free(cert); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 375 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 376 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 377 | lifetime, confirm, maxsign, skprovider)) != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 378 | error("Certificate %s (%s) add failed: %s", certpath, |
| 379 | private->cert->key_id, ssh_err(r)); |
| 380 | goto out; |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 381 | } |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 382 | /* success */ |
| 383 | if (!qflag) { |
| 384 | fprintf(stderr, "Certificate added: %s (%s)\n", certpath, |
| 385 | private->cert->key_id); |
| 386 | if (lifetime != 0) { |
dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 387 | fprintf(stderr, "Lifetime set to %ld seconds\n", |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 388 | lifetime); |
| 389 | } |
| 390 | if (confirm != 0) { |
| 391 | fprintf(stderr, "The user must confirm each use " |
| 392 | "of the key\n"); |
| 393 | } |
| 394 | } |
| 395 | |
Damien Miller | c6afb5f | 2010-05-21 14:56:47 +1000 | [diff] [blame] | 396 | out: |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 397 | free(certpath); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 398 | free(comment); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 399 | sshkey_free(private); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 400 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 401 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 402 | } |
| 403 | |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 404 | static int |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 405 | update_card(int agent_fd, int add, const char *id, int qflag) |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 406 | { |
Damien Miller | ef275ea | 2013-12-29 17:44:07 +1100 | [diff] [blame] | 407 | char *pin = NULL; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 408 | int r, ret = -1; |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 409 | |
Damien Miller | ef275ea | 2013-12-29 17:44:07 +1100 | [diff] [blame] | 410 | if (add) { |
| 411 | if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", |
| 412 | RP_ALLOW_STDIN)) == NULL) |
| 413 | return -1; |
| 414 | } |
Ben Lindstrom | ba72d30 | 2002-03-22 03:51:06 +0000 | [diff] [blame] | 415 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 416 | if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, |
| 417 | lifetime, confirm)) == 0) { |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 418 | ret = 0; |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 419 | if (!qflag) { |
| 420 | fprintf(stderr, "Card %s: %s\n", |
| 421 | add ? "added" : "removed", id); |
| 422 | } |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 423 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 424 | fprintf(stderr, "Could not %s card \"%s\": %s\n", |
| 425 | add ? "add" : "remove", id, ssh_err(r)); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 426 | ret = -1; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 427 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 428 | free(pin); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 429 | return ret; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 432 | static int |
djm@openbsd.org | aa22c20 | 2019-01-20 22:03:29 +0000 | [diff] [blame] | 433 | test_key(int agent_fd, const char *filename) |
| 434 | { |
| 435 | struct sshkey *key = NULL; |
| 436 | u_char *sig = NULL; |
| 437 | size_t slen = 0; |
| 438 | int r, ret = -1; |
| 439 | char data[1024]; |
| 440 | |
| 441 | if ((r = sshkey_load_public(filename, &key, NULL)) != 0) { |
| 442 | error("Couldn't read public key %s: %s", filename, ssh_err(r)); |
| 443 | return -1; |
| 444 | } |
| 445 | arc4random_buf(data, sizeof(data)); |
| 446 | if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data), |
| 447 | NULL, 0)) != 0) { |
| 448 | error("Agent signature failed for %s: %s", |
| 449 | filename, ssh_err(r)); |
| 450 | goto done; |
| 451 | } |
| 452 | if ((r = sshkey_verify(key, sig, slen, data, sizeof(data), |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 453 | NULL, 0, NULL)) != 0) { |
djm@openbsd.org | aa22c20 | 2019-01-20 22:03:29 +0000 | [diff] [blame] | 454 | error("Signature verification failed for %s: %s", |
| 455 | filename, ssh_err(r)); |
| 456 | goto done; |
| 457 | } |
| 458 | /* success */ |
| 459 | ret = 0; |
| 460 | done: |
| 461 | free(sig); |
| 462 | sshkey_free(key); |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 467 | list_identities(int agent_fd, int do_fp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 468 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 469 | char *fp; |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 470 | int r; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 471 | struct ssh_identitylist *idlist; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 472 | u_int32_t left; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 473 | size_t i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 474 | |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 475 | if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { |
| 476 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 477 | fprintf(stderr, "error fetching identities: %s\n", |
| 478 | ssh_err(r)); |
| 479 | else |
| 480 | printf("The agent has no identities.\n"); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 481 | return -1; |
| 482 | } |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 483 | for (i = 0; i < idlist->nkeys; i++) { |
| 484 | if (do_fp) { |
| 485 | fp = sshkey_fingerprint(idlist->keys[i], |
| 486 | fingerprint_hash, SSH_FP_DEFAULT); |
| 487 | printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]), |
| 488 | fp == NULL ? "(null)" : fp, idlist->comments[i], |
| 489 | sshkey_type(idlist->keys[i])); |
| 490 | free(fp); |
| 491 | } else { |
| 492 | if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) { |
| 493 | fprintf(stderr, "sshkey_write: %s\n", |
| 494 | ssh_err(r)); |
| 495 | continue; |
| 496 | } |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 497 | fprintf(stdout, " %s", idlist->comments[i]); |
| 498 | left = sshkey_signatures_left(idlist->keys[i]); |
| 499 | if (left > 0) |
| 500 | fprintf(stdout, |
| 501 | " [signatures left %d]", left); |
| 502 | fprintf(stdout, "\n"); |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | ssh_free_identitylist(idlist); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 506 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 507 | } |
| 508 | |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 509 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 510 | lock_agent(int agent_fd, int lock) |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 511 | { |
| 512 | char prompt[100], *p1, *p2; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 513 | int r, passok = 1, ret = -1; |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 514 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 515 | strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); |
| 516 | p1 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 517 | if (lock) { |
| 518 | strlcpy(prompt, "Again: ", sizeof prompt); |
| 519 | p2 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 520 | if (strcmp(p1, p2) != 0) { |
| 521 | fprintf(stderr, "Passwords do not match.\n"); |
| 522 | passok = 0; |
| 523 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 524 | explicit_bzero(p2, strlen(p2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 525 | free(p2); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 526 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 527 | if (passok) { |
| 528 | if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { |
| 529 | fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); |
| 530 | ret = 0; |
| 531 | } else { |
| 532 | fprintf(stderr, "Failed to %slock agent: %s\n", |
| 533 | lock ? "" : "un", ssh_err(r)); |
| 534 | } |
| 535 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 536 | explicit_bzero(p1, strlen(p1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 537 | free(p1); |
Ben Lindstrom | 3390749 | 2002-06-27 00:21:59 +0000 | [diff] [blame] | 538 | return (ret); |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static int |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 542 | load_resident_keys(int agent_fd, const char *skprovider, int qflag) |
| 543 | { |
| 544 | struct sshkey **keys; |
| 545 | size_t nkeys, i; |
| 546 | int r, ok = 0; |
| 547 | char *fp; |
| 548 | |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 549 | pass = read_passphrase("Enter PIN for authenticator: ", RP_ALLOW_STDIN); |
djm@openbsd.org | c312ca0 | 2020-01-06 02:00:46 +0000 | [diff] [blame] | 550 | if ((r = sshsk_load_resident(skprovider, NULL, pass, |
| 551 | &keys, &nkeys)) != 0) { |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 552 | error("Unable to load resident keys: %s", ssh_err(r)); |
| 553 | return r; |
| 554 | } |
| 555 | for (i = 0; i < nkeys; i++) { |
| 556 | if ((fp = sshkey_fingerprint(keys[i], |
| 557 | fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 558 | fatal("%s: sshkey_fingerprint failed", __func__); |
| 559 | if ((r = ssh_add_identity_constrained(agent_fd, keys[i], "", |
| 560 | lifetime, confirm, maxsign, skprovider)) != 0) { |
| 561 | error("Unable to add key %s %s", |
| 562 | sshkey_type(keys[i]), fp); |
| 563 | free(fp); |
| 564 | ok = r; |
| 565 | continue; |
| 566 | } |
| 567 | if (ok == 0) |
| 568 | ok = 1; |
| 569 | if (!qflag) { |
| 570 | fprintf(stderr, "Resident identity added: %s %s\n", |
| 571 | sshkey_type(keys[i]), fp); |
| 572 | if (lifetime != 0) { |
| 573 | fprintf(stderr, |
dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 574 | "Lifetime set to %ld seconds\n", lifetime); |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 575 | } |
| 576 | if (confirm != 0) { |
| 577 | fprintf(stderr, "The user must confirm " |
| 578 | "each use of the key\n"); |
| 579 | } |
| 580 | } |
| 581 | free(fp); |
| 582 | sshkey_free(keys[i]); |
| 583 | } |
| 584 | free(keys); |
| 585 | if (nkeys == 0) |
| 586 | return SSH_ERR_KEY_NOT_FOUND; |
| 587 | return ok == 1 ? 0 : ok; |
| 588 | } |
| 589 | |
| 590 | static int |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 591 | do_file(int agent_fd, int deleting, int key_only, char *file, int qflag, |
| 592 | const char *skprovider) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 593 | { |
| 594 | if (deleting) { |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 595 | if (delete_file(agent_fd, file, key_only, qflag) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 596 | return -1; |
| 597 | } else { |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 598 | if (add_file(agent_fd, file, key_only, qflag, skprovider) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 599 | return -1; |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 604 | static void |
| 605 | usage(void) |
| 606 | { |
naddy@openbsd.org | e8c06c4 | 2020-01-17 20:13:47 +0000 | [diff] [blame] | 607 | fprintf(stderr, |
| 608 | "usage: ssh-add [-cDdKkLlqvXx] [-E fingerprint_hash] [-S provider] [-t life]\n" |
| 609 | #ifdef WITH_XMSS |
| 610 | " [-M maxsign] [-m minleft]\n" |
| 611 | #endif |
| 612 | " [file ...]\n" |
| 613 | " ssh-add -s pkcs11\n" |
| 614 | " ssh-add -e pkcs11\n" |
| 615 | " ssh-add -T pubkey ...\n" |
| 616 | ); |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 619 | int |
Damien Miller | 01ab4a2 | 1999-10-28 15:23:30 +1000 | [diff] [blame] | 620 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 621 | { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 622 | extern char *optarg; |
| 623 | extern int optind; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 624 | int agent_fd; |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 625 | char *pkcs11provider = NULL, *skprovider = NULL; |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 626 | int r, i, ch, deleting = 0, ret = 0, key_only = 0, do_download = 0; |
djm@openbsd.org | aa22c20 | 2019-01-20 22:03:29 +0000 | [diff] [blame] | 627 | int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0; |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 628 | SyslogFacility log_facility = SYSLOG_FACILITY_AUTH; |
| 629 | LogLevel log_level = SYSLOG_LEVEL_INFO; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 630 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 631 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 632 | sanitise_stdfd(); |
| 633 | |
Damien Miller | 59d3d5b | 2003-08-22 09:34:41 +1000 | [diff] [blame] | 634 | __progname = ssh_get_progname(argv[0]); |
Ben Lindstrom | 93d1fe8 | 2001-05-06 02:57:20 +0000 | [diff] [blame] | 635 | seed_rng(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 636 | |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 637 | log_init(__progname, log_level, log_facility, 1); |
| 638 | |
millert@openbsd.org | db995f2 | 2014-11-26 18:34:51 +0000 | [diff] [blame] | 639 | setvbuf(stdout, NULL, _IOLBF, 0); |
Damien Miller | d7ca2cd | 2014-07-03 21:23:01 +1000 | [diff] [blame] | 640 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 641 | /* First, get a connection to the authentication agent. */ |
| 642 | switch (r = ssh_get_authentication_socket(&agent_fd)) { |
| 643 | case 0: |
| 644 | break; |
| 645 | case SSH_ERR_AGENT_NOT_PRESENT: |
| 646 | fprintf(stderr, "Could not open a connection to your " |
| 647 | "authentication agent.\n"); |
| 648 | exit(2); |
| 649 | default: |
| 650 | fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); |
Damien Miller | 43cba34 | 2002-02-05 12:12:49 +1100 | [diff] [blame] | 651 | exit(2); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 652 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 653 | |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 654 | skprovider = getenv("SSH_SK_PROVIDER"); |
| 655 | |
naddy@openbsd.org | e8c06c4 | 2020-01-17 20:13:47 +0000 | [diff] [blame] | 656 | while ((ch = getopt(argc, argv, "vkKlLcdDTxXE:e:M:m:qs:S:t:")) != -1) { |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 657 | switch (ch) { |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 658 | case 'v': |
| 659 | if (log_level == SYSLOG_LEVEL_INFO) |
| 660 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 661 | else if (log_level < SYSLOG_LEVEL_DEBUG3) |
| 662 | log_level++; |
| 663 | break; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 664 | case 'E': |
| 665 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 666 | if (fingerprint_hash == -1) |
| 667 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 668 | break; |
Damien Miller | 8f4279e | 2011-10-18 16:06:33 +1100 | [diff] [blame] | 669 | case 'k': |
| 670 | key_only = 1; |
| 671 | break; |
naddy@openbsd.org | e8c06c4 | 2020-01-17 20:13:47 +0000 | [diff] [blame] | 672 | case 'K': |
| 673 | do_download = 1; |
| 674 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 675 | case 'l': |
| 676 | case 'L': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 677 | if (lflag != 0) |
| 678 | fatal("-%c flag already specified", lflag); |
| 679 | lflag = ch; |
| 680 | break; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 681 | case 'x': |
| 682 | case 'X': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 683 | if (xflag != 0) |
| 684 | fatal("-%c flag already specified", xflag); |
| 685 | xflag = ch; |
| 686 | break; |
Damien Miller | 6c71179 | 2003-01-24 11:36:23 +1100 | [diff] [blame] | 687 | case 'c': |
| 688 | confirm = 1; |
| 689 | break; |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 690 | case 'm': |
| 691 | minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL); |
| 692 | if (minleft == 0) { |
| 693 | usage(); |
| 694 | ret = 1; |
| 695 | goto done; |
| 696 | } |
| 697 | break; |
| 698 | case 'M': |
| 699 | maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL); |
| 700 | if (maxsign == 0) { |
| 701 | usage(); |
| 702 | ret = 1; |
| 703 | goto done; |
| 704 | } |
| 705 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 706 | case 'd': |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 707 | deleting = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 708 | break; |
| 709 | case 'D': |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 710 | Dflag = 1; |
| 711 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 712 | case 's': |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 713 | pkcs11provider = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 714 | break; |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 715 | case 'S': |
| 716 | skprovider = optarg; |
| 717 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 718 | case 'e': |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 719 | deleting = 1; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 720 | pkcs11provider = optarg; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 721 | break; |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 722 | case 't': |
dtucker@openbsd.org | 264a966 | 2020-02-18 08:58:33 +0000 | [diff] [blame] | 723 | if ((lifetime = convtime(optarg)) == -1 || |
| 724 | lifetime < 0 || lifetime > UINT32_MAX) { |
Ben Lindstrom | 1775c9c | 2002-06-11 15:51:54 +0000 | [diff] [blame] | 725 | fprintf(stderr, "Invalid lifetime\n"); |
| 726 | ret = 1; |
| 727 | goto done; |
| 728 | } |
Ben Lindstrom | 61d328a | 2002-06-06 21:54:57 +0000 | [diff] [blame] | 729 | break; |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 730 | case 'q': |
| 731 | qflag = 1; |
| 732 | break; |
djm@openbsd.org | aa22c20 | 2019-01-20 22:03:29 +0000 | [diff] [blame] | 733 | case 'T': |
| 734 | Tflag = 1; |
| 735 | break; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 736 | default: |
| 737 | usage(); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 738 | ret = 1; |
| 739 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 740 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 741 | } |
djm@openbsd.org | c7670b0 | 2019-01-21 12:53:35 +0000 | [diff] [blame] | 742 | log_init(__progname, log_level, log_facility, 1); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 743 | |
| 744 | if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) |
| 745 | fatal("Invalid combination of actions"); |
| 746 | else if (xflag) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 747 | if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 748 | ret = 1; |
| 749 | goto done; |
| 750 | } else if (lflag) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 751 | if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 752 | ret = 1; |
| 753 | goto done; |
| 754 | } else if (Dflag) { |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 755 | if (delete_all(agent_fd, qflag) == -1) |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 756 | ret = 1; |
| 757 | goto done; |
| 758 | } |
| 759 | |
djm@openbsd.org | 6bff952 | 2019-11-14 21:27:29 +0000 | [diff] [blame] | 760 | #ifdef ENABLE_SK_INTERNAL |
| 761 | if (skprovider == NULL) |
| 762 | skprovider = "internal"; |
| 763 | #endif |
| 764 | |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 765 | argc -= optind; |
| 766 | argv += optind; |
djm@openbsd.org | aa22c20 | 2019-01-20 22:03:29 +0000 | [diff] [blame] | 767 | if (Tflag) { |
| 768 | if (argc <= 0) |
| 769 | fatal("no keys to test"); |
| 770 | for (r = i = 0; i < argc; i++) |
| 771 | r |= test_key(agent_fd, argv[i]); |
| 772 | ret = r == 0 ? 0 : 1; |
| 773 | goto done; |
| 774 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 775 | if (pkcs11provider != NULL) { |
djm@openbsd.org | f80e68e | 2018-09-19 02:03:02 +0000 | [diff] [blame] | 776 | if (update_card(agent_fd, !deleting, pkcs11provider, |
| 777 | qflag) == -1) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 778 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 779 | goto done; |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 780 | } |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 781 | if (do_download) { |
| 782 | if (skprovider == NULL) |
| 783 | fatal("Cannot download keys without provider"); |
| 784 | if (load_resident_keys(agent_fd, skprovider, qflag) != 0) |
| 785 | ret = 1; |
| 786 | goto done; |
| 787 | } |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 788 | if (argc == 0) { |
deraadt@openbsd.org | 2ae4f33 | 2015-01-16 06:40:12 +0000 | [diff] [blame] | 789 | char buf[PATH_MAX]; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 790 | struct passwd *pw; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 791 | struct stat st; |
| 792 | int count = 0; |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 793 | |
| 794 | if ((pw = getpwuid(getuid())) == NULL) { |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 795 | fprintf(stderr, "No user found with uid %u\n", |
| 796 | (u_int)getuid()); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 797 | ret = 1; |
| 798 | goto done; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 799 | } |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 800 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 801 | for (i = 0; default_files[i]; i++) { |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 802 | snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 803 | default_files[i]); |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 804 | if (stat(buf, &st) == -1) |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 805 | continue; |
dlg@openbsd.org | 530591a | 2017-08-29 09:42:29 +0000 | [diff] [blame] | 806 | if (do_file(agent_fd, deleting, key_only, buf, |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 807 | qflag, skprovider) == -1) |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 808 | ret = 1; |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 809 | else |
| 810 | count++; |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 811 | } |
Ben Lindstrom | 58b391b | 2002-03-22 03:21:16 +0000 | [diff] [blame] | 812 | if (count == 0) |
| 813 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 814 | } else { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 815 | for (i = 0; i < argc; i++) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 816 | if (do_file(agent_fd, deleting, key_only, |
djm@openbsd.org | 486164d | 2019-10-31 21:19:56 +0000 | [diff] [blame] | 817 | argv[i], qflag, skprovider) == -1) |
Damien Miller | 6e1057c | 2002-01-22 23:05:59 +1100 | [diff] [blame] | 818 | ret = 1; |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 819 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 820 | } |
Ben Lindstrom | 44e49af | 2001-07-04 05:03:51 +0000 | [diff] [blame] | 821 | done: |
djm@openbsd.org | 79fe22d | 2019-12-30 09:22:49 +0000 | [diff] [blame] | 822 | clear_pass(); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 823 | ssh_close_authentication_socket(agent_fd); |
Ben Lindstrom | 569f88d | 2001-10-03 17:43:01 +0000 | [diff] [blame] | 824 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 825 | } |