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