Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 1 | /* $OpenBSD: ssh-add.c,v 1.120 2015/02/21 21:46:57 halex Exp $ */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
| 6 | * Adds an identity to the authentication server, or removes an identity. |
| 7 | * |
| 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 | * |
| 14 | * SSH2 implementation, |
| 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
| 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. |
| 36 | */ |
| 37 | |
| 38 | #include "includes.h" |
| 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 42 | |
| 43 | #include <openssl/evp.h> |
| 44 | #include "openbsd-compat/openssl-compat.h" |
| 45 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 46 | #include <errno.h> |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 47 | #include <fcntl.h> |
| 48 | #include <pwd.h> |
| 49 | #include <stdarg.h> |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #include <unistd.h> |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 54 | #include <limits.h> |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 55 | |
| 56 | #include "xmalloc.h" |
| 57 | #include "ssh.h" |
| 58 | #include "rsa.h" |
| 59 | #include "log.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 60 | #include "sshkey.h" |
| 61 | #include "sshbuf.h" |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 62 | #include "authfd.h" |
| 63 | #include "authfile.h" |
| 64 | #include "pathnames.h" |
| 65 | #include "misc.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 66 | #include "ssherr.h" |
| 67 | #include "digest.h" |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 68 | |
| 69 | /* argv0 */ |
| 70 | extern char *__progname; |
| 71 | |
| 72 | /* Default files to add */ |
| 73 | static char *default_files[] = { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 74 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 75 | _PATH_SSH_CLIENT_ID_RSA, |
| 76 | _PATH_SSH_CLIENT_ID_DSA, |
| 77 | #ifdef OPENSSL_HAS_ECC |
| 78 | _PATH_SSH_CLIENT_ID_ECDSA, |
| 79 | #endif |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 80 | #endif /* WITH_OPENSSL */ |
| 81 | _PATH_SSH_CLIENT_ID_ED25519, |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 82 | _PATH_SSH_CLIENT_IDENTITY, |
| 83 | NULL |
| 84 | }; |
| 85 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 86 | static int fingerprint_hash = SSH_FP_HASH_DEFAULT; |
| 87 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 88 | /* Default lifetime (0 == forever) */ |
| 89 | static int lifetime = 0; |
| 90 | |
| 91 | /* User has to confirm key use */ |
| 92 | static int confirm = 0; |
| 93 | |
| 94 | /* we keep a cache of one passphrases */ |
| 95 | static char *pass = NULL; |
| 96 | static void |
| 97 | clear_pass(void) |
| 98 | { |
| 99 | if (pass) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 100 | explicit_bzero(pass, strlen(pass)); |
| 101 | free(pass); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 102 | pass = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 107 | delete_file(int agent_fd, const char *filename, int key_only) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 108 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 109 | struct sshkey *public, *cert = NULL; |
| 110 | char *certpath = NULL, *comment = NULL; |
| 111 | int r, ret = -1; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 112 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 113 | if ((r = sshkey_load_public(filename, &public, &comment)) != 0) { |
| 114 | printf("Bad key file %s: %s\n", filename, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 115 | return -1; |
| 116 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 117 | if ((r = ssh_remove_identity(agent_fd, public)) == 0) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 118 | fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment); |
| 119 | ret = 0; |
| 120 | } else |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 121 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 122 | filename, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 123 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 124 | if (key_only) |
| 125 | goto out; |
| 126 | |
| 127 | /* Now try to delete the corresponding certificate too */ |
| 128 | free(comment); |
| 129 | comment = NULL; |
| 130 | xasprintf(&certpath, "%s-cert.pub", filename); |
| 131 | if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) { |
| 132 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 133 | error("Failed to load certificate \"%s\": %s", |
| 134 | certpath, ssh_err(r)); |
| 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | if (!sshkey_equal_public(cert, public)) |
| 139 | fatal("Certificate %s does not match private key %s", |
| 140 | certpath, filename); |
| 141 | |
| 142 | if ((r = ssh_remove_identity(agent_fd, cert)) == 0) { |
| 143 | fprintf(stderr, "Identity removed: %s (%s)\n", certpath, |
| 144 | comment); |
| 145 | ret = 0; |
| 146 | } else |
| 147 | fprintf(stderr, "Could not remove identity \"%s\": %s\n", |
| 148 | certpath, ssh_err(r)); |
| 149 | |
| 150 | out: |
| 151 | if (cert != NULL) |
| 152 | sshkey_free(cert); |
| 153 | if (public != NULL) |
| 154 | sshkey_free(public); |
| 155 | free(certpath); |
| 156 | free(comment); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* Send a request to remove all identities. */ |
| 162 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 163 | delete_all(int agent_fd) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 164 | { |
| 165 | int ret = -1; |
| 166 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 167 | if (ssh_remove_all_identities(agent_fd, 1) == 0) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 168 | ret = 0; |
| 169 | /* ignore error-code for ssh2 */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 170 | /* XXX revisit */ |
| 171 | ssh_remove_all_identities(agent_fd, 2); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 172 | |
| 173 | if (ret == 0) |
| 174 | fprintf(stderr, "All identities removed.\n"); |
| 175 | else |
| 176 | fprintf(stderr, "Failed to remove all identities.\n"); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 182 | add_file(int agent_fd, const char *filename, int key_only) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 183 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 184 | struct sshkey *private, *cert; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 185 | char *comment = NULL; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 186 | char msg[1024], *certpath = NULL; |
| 187 | int r, fd, ret = -1; |
| 188 | struct sshbuf *keyblob; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 189 | |
| 190 | if (strcmp(filename, "-") == 0) { |
| 191 | fd = STDIN_FILENO; |
| 192 | filename = "(stdin)"; |
| 193 | } else if ((fd = open(filename, O_RDONLY)) < 0) { |
| 194 | perror(filename); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Since we'll try to load a keyfile multiple times, permission errors |
| 200 | * will occur multiple times, so check perms first and bail if wrong. |
| 201 | */ |
| 202 | if (fd != STDIN_FILENO) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 203 | if (sshkey_perm_ok(fd, filename) != 0) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 204 | close(fd); |
| 205 | return -1; |
| 206 | } |
| 207 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 208 | if ((keyblob = sshbuf_new()) == NULL) |
| 209 | fatal("%s: sshbuf_new failed", __func__); |
| 210 | if ((r = sshkey_load_file(fd, keyblob)) != 0) { |
| 211 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 212 | filename, ssh_err(r)); |
| 213 | sshbuf_free(keyblob); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 214 | close(fd); |
| 215 | return -1; |
| 216 | } |
| 217 | close(fd); |
| 218 | |
| 219 | /* At first, try empty passphrase */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 220 | if ((r = sshkey_parse_private_fileblob(keyblob, "", filename, |
| 221 | &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 222 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 223 | filename, ssh_err(r)); |
| 224 | goto fail_load; |
| 225 | } |
| 226 | /* try last */ |
| 227 | if (private == NULL && pass != NULL) { |
| 228 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename, |
| 229 | &private, &comment)) != 0 && |
| 230 | r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 231 | fprintf(stderr, "Error loading key \"%s\": %s\n", |
| 232 | filename, ssh_err(r)); |
| 233 | goto fail_load; |
| 234 | } |
| 235 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 236 | if (comment == NULL) |
| 237 | comment = xstrdup(filename); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 238 | if (private == NULL) { |
| 239 | /* clear passphrase since it did not work */ |
| 240 | clear_pass(); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 241 | snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ", |
| 242 | comment, confirm ? " (will confirm each use)" : ""); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 243 | for (;;) { |
| 244 | pass = read_passphrase(msg, RP_ALLOW_STDIN); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 245 | if (strcmp(pass, "") == 0) |
| 246 | goto fail_load; |
| 247 | if ((r = sshkey_parse_private_fileblob(keyblob, pass, |
| 248 | filename, &private, NULL)) == 0) |
| 249 | break; |
| 250 | else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) { |
| 251 | fprintf(stderr, |
| 252 | "Error loading key \"%s\": %s\n", |
| 253 | filename, ssh_err(r)); |
| 254 | fail_load: |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 255 | clear_pass(); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 256 | free(comment); |
| 257 | sshbuf_free(keyblob); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 258 | return -1; |
| 259 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 260 | clear_pass(); |
| 261 | snprintf(msg, sizeof msg, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 262 | "Bad passphrase, try again for %.200s%s: ", comment, |
| 263 | confirm ? " (will confirm each use)" : ""); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 266 | sshbuf_free(keyblob); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 267 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 268 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
| 269 | lifetime, confirm)) == 0) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 270 | fprintf(stderr, "Identity added: %s (%s)\n", filename, comment); |
| 271 | ret = 0; |
| 272 | if (lifetime != 0) |
| 273 | fprintf(stderr, |
| 274 | "Lifetime set to %d seconds\n", lifetime); |
| 275 | if (confirm != 0) |
| 276 | fprintf(stderr, |
| 277 | "The user must confirm each use of the key\n"); |
| 278 | } else { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 279 | fprintf(stderr, "Could not add identity \"%s\": %s\n", |
| 280 | filename, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 283 | /* Skip trying to load the cert if requested */ |
| 284 | if (key_only) |
| 285 | goto out; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 286 | |
| 287 | /* Now try to add the certificate flavour too */ |
| 288 | xasprintf(&certpath, "%s-cert.pub", filename); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 289 | if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) { |
| 290 | if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT) |
| 291 | error("Failed to load certificate \"%s\": %s", |
| 292 | certpath, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 293 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 294 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 295 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 296 | if (!sshkey_equal_public(cert, private)) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 297 | error("Certificate %s does not match private key %s", |
| 298 | certpath, filename); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 299 | sshkey_free(cert); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | /* Graft with private bits */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 304 | if ((r = sshkey_to_certified(private, |
| 305 | sshkey_cert_is_legacy(cert))) != 0) { |
| 306 | error("%s: sshkey_to_certified: %s", __func__, ssh_err(r)); |
| 307 | sshkey_free(cert); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 308 | goto out; |
| 309 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 310 | if ((r = sshkey_cert_copy(cert, private)) != 0) { |
| 311 | error("%s: key_cert_copy: %s", __func__, ssh_err(r)); |
| 312 | sshkey_free(cert); |
| 313 | goto out; |
| 314 | } |
| 315 | sshkey_free(cert); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 316 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 317 | if ((r = ssh_add_identity_constrained(agent_fd, private, comment, |
| 318 | lifetime, confirm)) != 0) { |
| 319 | error("Certificate %s (%s) add failed: %s", certpath, |
| 320 | private->cert->key_id, ssh_err(r)); |
| 321 | goto out; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 322 | } |
| 323 | fprintf(stderr, "Certificate added: %s (%s)\n", certpath, |
| 324 | private->cert->key_id); |
| 325 | if (lifetime != 0) |
| 326 | fprintf(stderr, "Lifetime set to %d seconds\n", lifetime); |
| 327 | if (confirm != 0) |
| 328 | fprintf(stderr, "The user must confirm each use of the key\n"); |
| 329 | out: |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 330 | free(certpath); |
| 331 | free(comment); |
| 332 | sshkey_free(private); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 338 | update_card(int agent_fd, int add, const char *id) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 339 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 340 | char *pin = NULL; |
| 341 | int r, ret = -1; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 342 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 343 | if (add) { |
| 344 | if ((pin = read_passphrase("Enter passphrase for PKCS#11: ", |
| 345 | RP_ALLOW_STDIN)) == NULL) |
| 346 | return -1; |
| 347 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 348 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 349 | if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin, |
| 350 | lifetime, confirm)) == 0) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 351 | fprintf(stderr, "Card %s: %s\n", |
| 352 | add ? "added" : "removed", id); |
| 353 | ret = 0; |
| 354 | } else { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 355 | fprintf(stderr, "Could not %s card \"%s\": %s\n", |
| 356 | add ? "add" : "remove", id, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 357 | ret = -1; |
| 358 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 359 | free(pin); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 364 | list_identities(int agent_fd, int do_fp) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 365 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 366 | char *fp; |
| 367 | int version, r, had_identities = 0; |
| 368 | struct ssh_identitylist *idlist; |
| 369 | size_t i; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 370 | |
| 371 | for (version = 1; version <= 2; version++) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 372 | if ((r = ssh_fetch_identitylist(agent_fd, version, |
| 373 | &idlist)) != 0) { |
| 374 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 375 | fprintf(stderr, "error fetching identities for " |
| 376 | "protocol %d: %s\n", version, ssh_err(r)); |
| 377 | continue; |
| 378 | } |
| 379 | for (i = 0; i < idlist->nkeys; i++) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 380 | had_identities = 1; |
| 381 | if (do_fp) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 382 | fp = sshkey_fingerprint(idlist->keys[i], |
| 383 | fingerprint_hash, SSH_FP_DEFAULT); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 384 | printf("%d %s %s (%s)\n", |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 385 | sshkey_size(idlist->keys[i]), |
| 386 | fp == NULL ? "(null)" : fp, |
| 387 | idlist->comments[i], |
| 388 | sshkey_type(idlist->keys[i])); |
| 389 | free(fp); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 390 | } else { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 391 | if ((r = sshkey_write(idlist->keys[i], |
| 392 | stdout)) != 0) { |
| 393 | fprintf(stderr, "sshkey_write: %s\n", |
| 394 | ssh_err(r)); |
| 395 | continue; |
| 396 | } |
| 397 | fprintf(stdout, " %s\n", idlist->comments[i]); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 398 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 399 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 400 | ssh_free_identitylist(idlist); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 401 | } |
| 402 | if (!had_identities) { |
| 403 | printf("The agent has no identities.\n"); |
| 404 | return -1; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 410 | lock_agent(int agent_fd, int lock) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 411 | { |
| 412 | char prompt[100], *p1, *p2; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 413 | int r, passok = 1, ret = -1; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 414 | |
| 415 | strlcpy(prompt, "Enter lock password: ", sizeof(prompt)); |
| 416 | p1 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 417 | if (lock) { |
| 418 | strlcpy(prompt, "Again: ", sizeof prompt); |
| 419 | p2 = read_passphrase(prompt, RP_ALLOW_STDIN); |
| 420 | if (strcmp(p1, p2) != 0) { |
| 421 | fprintf(stderr, "Passwords do not match.\n"); |
| 422 | passok = 0; |
| 423 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 424 | explicit_bzero(p2, strlen(p2)); |
| 425 | free(p2); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 426 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 427 | if (passok) { |
| 428 | if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) { |
| 429 | fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un"); |
| 430 | ret = 0; |
| 431 | } else { |
| 432 | fprintf(stderr, "Failed to %slock agent: %s\n", |
| 433 | lock ? "" : "un", ssh_err(r)); |
| 434 | } |
| 435 | } |
| 436 | explicit_bzero(p1, strlen(p1)); |
| 437 | free(p1); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 438 | return (ret); |
| 439 | } |
| 440 | |
| 441 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 442 | do_file(int agent_fd, int deleting, int key_only, char *file) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 443 | { |
| 444 | if (deleting) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 445 | if (delete_file(agent_fd, file, key_only) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 446 | return -1; |
| 447 | } else { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 448 | if (add_file(agent_fd, file, key_only) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 449 | return -1; |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | usage(void) |
| 456 | { |
| 457 | fprintf(stderr, "usage: %s [options] [file ...]\n", __progname); |
| 458 | fprintf(stderr, "Options:\n"); |
| 459 | fprintf(stderr, " -l List fingerprints of all identities.\n"); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 460 | fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n"); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 461 | fprintf(stderr, " -L List public key parameters of all identities.\n"); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 462 | fprintf(stderr, " -k Load only keys and not certificates.\n"); |
| 463 | fprintf(stderr, " -c Require confirmation to sign using identities\n"); |
| 464 | fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n"); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 465 | fprintf(stderr, " -d Delete identity.\n"); |
| 466 | fprintf(stderr, " -D Delete all identities.\n"); |
| 467 | fprintf(stderr, " -x Lock agent.\n"); |
| 468 | fprintf(stderr, " -X Unlock agent.\n"); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 469 | fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n"); |
| 470 | fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n"); |
| 471 | } |
| 472 | |
| 473 | int |
| 474 | main(int argc, char **argv) |
| 475 | { |
| 476 | extern char *optarg; |
| 477 | extern int optind; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 478 | int agent_fd; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 479 | char *pkcs11provider = NULL; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 480 | int r, i, ch, deleting = 0, ret = 0, key_only = 0; |
| 481 | int xflag = 0, lflag = 0, Dflag = 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 482 | |
| 483 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 484 | sanitise_stdfd(); |
| 485 | |
| 486 | __progname = ssh_get_progname(argv[0]); |
| 487 | seed_rng(); |
| 488 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 489 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 490 | OpenSSL_add_all_algorithms(); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 491 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 492 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 493 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 494 | |
| 495 | /* First, get a connection to the authentication agent. */ |
| 496 | switch (r = ssh_get_authentication_socket(&agent_fd)) { |
| 497 | case 0: |
| 498 | break; |
| 499 | case SSH_ERR_AGENT_NOT_PRESENT: |
| 500 | fprintf(stderr, "Could not open a connection to your " |
| 501 | "authentication agent.\n"); |
| 502 | exit(2); |
| 503 | default: |
| 504 | fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 505 | exit(2); |
| 506 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 507 | |
| 508 | while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 509 | switch (ch) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 510 | case 'E': |
| 511 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 512 | if (fingerprint_hash == -1) |
| 513 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 514 | break; |
| 515 | case 'k': |
| 516 | key_only = 1; |
| 517 | break; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 518 | case 'l': |
| 519 | case 'L': |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 520 | if (lflag != 0) |
| 521 | fatal("-%c flag already specified", lflag); |
| 522 | lflag = ch; |
| 523 | break; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 524 | case 'x': |
| 525 | case 'X': |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 526 | if (xflag != 0) |
| 527 | fatal("-%c flag already specified", xflag); |
| 528 | xflag = ch; |
| 529 | break; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 530 | case 'c': |
| 531 | confirm = 1; |
| 532 | break; |
| 533 | case 'd': |
| 534 | deleting = 1; |
| 535 | break; |
| 536 | case 'D': |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 537 | Dflag = 1; |
| 538 | break; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 539 | case 's': |
| 540 | pkcs11provider = optarg; |
| 541 | break; |
| 542 | case 'e': |
| 543 | deleting = 1; |
| 544 | pkcs11provider = optarg; |
| 545 | break; |
| 546 | case 't': |
| 547 | if ((lifetime = convtime(optarg)) == -1) { |
| 548 | fprintf(stderr, "Invalid lifetime\n"); |
| 549 | ret = 1; |
| 550 | goto done; |
| 551 | } |
| 552 | break; |
| 553 | default: |
| 554 | usage(); |
| 555 | ret = 1; |
| 556 | goto done; |
| 557 | } |
| 558 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 559 | |
| 560 | if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1) |
| 561 | fatal("Invalid combination of actions"); |
| 562 | else if (xflag) { |
| 563 | if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1) |
| 564 | ret = 1; |
| 565 | goto done; |
| 566 | } else if (lflag) { |
| 567 | if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1) |
| 568 | ret = 1; |
| 569 | goto done; |
| 570 | } else if (Dflag) { |
| 571 | if (delete_all(agent_fd) == -1) |
| 572 | ret = 1; |
| 573 | goto done; |
| 574 | } |
| 575 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 576 | argc -= optind; |
| 577 | argv += optind; |
| 578 | if (pkcs11provider != NULL) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 579 | if (update_card(agent_fd, !deleting, pkcs11provider) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 580 | ret = 1; |
| 581 | goto done; |
| 582 | } |
| 583 | if (argc == 0) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 584 | char buf[PATH_MAX]; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 585 | struct passwd *pw; |
| 586 | struct stat st; |
| 587 | int count = 0; |
| 588 | |
| 589 | if ((pw = getpwuid(getuid())) == NULL) { |
| 590 | fprintf(stderr, "No user found with uid %u\n", |
| 591 | (u_int)getuid()); |
| 592 | ret = 1; |
| 593 | goto done; |
| 594 | } |
| 595 | |
| 596 | for (i = 0; default_files[i]; i++) { |
| 597 | snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, |
| 598 | default_files[i]); |
| 599 | if (stat(buf, &st) < 0) |
| 600 | continue; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 601 | if (do_file(agent_fd, deleting, key_only, buf) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 602 | ret = 1; |
| 603 | else |
| 604 | count++; |
| 605 | } |
| 606 | if (count == 0) |
| 607 | ret = 1; |
| 608 | } else { |
| 609 | for (i = 0; i < argc; i++) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 610 | if (do_file(agent_fd, deleting, key_only, |
| 611 | argv[i]) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 612 | ret = 1; |
| 613 | } |
| 614 | } |
| 615 | clear_pass(); |
| 616 | |
| 617 | done: |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 618 | ssh_close_authentication_socket(agent_fd); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 619 | return ret; |
| 620 | } |