1 /* $OpenBSD: ssh-add.c,v 1.120 2015/02/21 21:46:57 halex Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Adds an identity to the authentication server, or removes an identity.
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".
14 * SSH2 implementation,
15 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/types.h>
43 #include <openssl/evp.h>
44 #include "openbsd-compat/openssl-compat.h"
64 #include "pathnames.h"
70 extern char *__progname;
72 /* Default files to add */
73 static char *default_files[] = {
75 _PATH_SSH_CLIENT_ID_RSA,
76 _PATH_SSH_CLIENT_ID_DSA,
77 #ifdef OPENSSL_HAS_ECC
78 _PATH_SSH_CLIENT_ID_ECDSA,
80 #endif /* WITH_OPENSSL */
81 _PATH_SSH_CLIENT_ID_ED25519,
82 _PATH_SSH_CLIENT_IDENTITY,
86 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
88 /* Default lifetime (0 == forever) */
89 static int lifetime = 0;
91 /* User has to confirm key use */
92 static int confirm = 0;
94 /* we keep a cache of one passphrases */
95 static char *pass = NULL;
100 explicit_bzero(pass, strlen(pass));
107 delete_file(int agent_fd, const char *filename, int key_only)
109 struct sshkey *public, *cert = NULL;
110 char *certpath = NULL, *comment = NULL;
113 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
114 printf("Bad key file %s: %s\n", filename, ssh_err(r));
117 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
118 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
121 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
122 filename, ssh_err(r));
127 /* Now try to delete the corresponding certificate too */
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));
138 if (!sshkey_equal_public(cert, public))
139 fatal("Certificate %s does not match private key %s",
142 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
143 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
147 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
148 certpath, ssh_err(r));
161 /* Send a request to remove all identities. */
163 delete_all(int agent_fd)
167 if (ssh_remove_all_identities(agent_fd, 1) == 0)
169 /* ignore error-code for ssh2 */
171 ssh_remove_all_identities(agent_fd, 2);
174 fprintf(stderr, "All identities removed.\n");
176 fprintf(stderr, "Failed to remove all identities.\n");
182 add_file(int agent_fd, const char *filename, int key_only)
184 struct sshkey *private, *cert;
185 char *comment = NULL;
186 char msg[1024], *certpath = NULL;
188 struct sshbuf *keyblob;
190 if (strcmp(filename, "-") == 0) {
192 filename = "(stdin)";
193 } else if ((fd = open(filename, O_RDONLY)) < 0) {
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.
202 if (fd != STDIN_FILENO) {
203 if (sshkey_perm_ok(fd, filename) != 0) {
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);
219 /* At first, try empty passphrase */
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));
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));
237 comment = xstrdup(filename);
238 if (private == NULL) {
239 /* clear passphrase since it did not work */
241 snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ",
242 comment, confirm ? " (will confirm each use)" : "");
244 pass = read_passphrase(msg, RP_ALLOW_STDIN);
245 if (strcmp(pass, "") == 0)
247 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
248 filename, &private, NULL)) == 0)
250 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
252 "Error loading key \"%s\": %s\n",
253 filename, ssh_err(r));
257 sshbuf_free(keyblob);
261 snprintf(msg, sizeof msg,
262 "Bad passphrase, try again for %.200s%s: ", comment,
263 confirm ? " (will confirm each use)" : "");
266 sshbuf_free(keyblob);
268 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
269 lifetime, confirm)) == 0) {
270 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
274 "Lifetime set to %d seconds\n", lifetime);
277 "The user must confirm each use of the key\n");
279 fprintf(stderr, "Could not add identity \"%s\": %s\n",
280 filename, ssh_err(r));
283 /* Skip trying to load the cert if requested */
287 /* Now try to add the certificate flavour too */
288 xasprintf(&certpath, "%s-cert.pub", filename);
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));
296 if (!sshkey_equal_public(cert, private)) {
297 error("Certificate %s does not match private key %s",
303 /* Graft with private bits */
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));
310 if ((r = sshkey_cert_copy(cert, private)) != 0) {
311 error("%s: key_cert_copy: %s", __func__, ssh_err(r));
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));
323 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
324 private->cert->key_id);
326 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
328 fprintf(stderr, "The user must confirm each use of the key\n");
332 sshkey_free(private);
338 update_card(int agent_fd, int add, const char *id)
344 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
345 RP_ALLOW_STDIN)) == NULL)
349 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
350 lifetime, confirm)) == 0) {
351 fprintf(stderr, "Card %s: %s\n",
352 add ? "added" : "removed", id);
355 fprintf(stderr, "Could not %s card \"%s\": %s\n",
356 add ? "add" : "remove", id, ssh_err(r));
364 list_identities(int agent_fd, int do_fp)
367 int version, r, had_identities = 0;
368 struct ssh_identitylist *idlist;
371 for (version = 1; version <= 2; version++) {
372 if ((r = ssh_fetch_identitylist(agent_fd, version,
374 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
375 fprintf(stderr, "error fetching identities for "
376 "protocol %d: %s\n", version, ssh_err(r));
379 for (i = 0; i < idlist->nkeys; i++) {
382 fp = sshkey_fingerprint(idlist->keys[i],
383 fingerprint_hash, SSH_FP_DEFAULT);
384 printf("%d %s %s (%s)\n",
385 sshkey_size(idlist->keys[i]),
386 fp == NULL ? "(null)" : fp,
388 sshkey_type(idlist->keys[i]));
391 if ((r = sshkey_write(idlist->keys[i],
393 fprintf(stderr, "sshkey_write: %s\n",
397 fprintf(stdout, " %s\n", idlist->comments[i]);
400 ssh_free_identitylist(idlist);
402 if (!had_identities) {
403 printf("The agent has no identities.\n");
410 lock_agent(int agent_fd, int lock)
412 char prompt[100], *p1, *p2;
413 int r, passok = 1, ret = -1;
415 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
416 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
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");
424 explicit_bzero(p2, strlen(p2));
428 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
429 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
432 fprintf(stderr, "Failed to %slock agent: %s\n",
433 lock ? "" : "un", ssh_err(r));
436 explicit_bzero(p1, strlen(p1));
442 do_file(int agent_fd, int deleting, int key_only, char *file)
445 if (delete_file(agent_fd, file, key_only) == -1)
448 if (add_file(agent_fd, file, key_only) == -1)
457 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
458 fprintf(stderr, "Options:\n");
459 fprintf(stderr, " -l List fingerprints of all identities.\n");
460 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
461 fprintf(stderr, " -L List public key parameters of all identities.\n");
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");
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");
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");
474 main(int argc, char **argv)
479 char *pkcs11provider = NULL;
480 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
481 int xflag = 0, lflag = 0, Dflag = 0;
483 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
486 __progname = ssh_get_progname(argv[0]);
490 OpenSSL_add_all_algorithms();
493 setvbuf(stdout, NULL, _IOLBF, 0);
495 /* First, get a connection to the authentication agent. */
496 switch (r = ssh_get_authentication_socket(&agent_fd)) {
499 case SSH_ERR_AGENT_NOT_PRESENT:
500 fprintf(stderr, "Could not open a connection to your "
501 "authentication agent.\n");
504 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
508 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
511 fingerprint_hash = ssh_digest_alg_by_name(optarg);
512 if (fingerprint_hash == -1)
513 fatal("Invalid hash algorithm \"%s\"", optarg);
521 fatal("-%c flag already specified", lflag);
527 fatal("-%c flag already specified", xflag);
540 pkcs11provider = optarg;
544 pkcs11provider = optarg;
547 if ((lifetime = convtime(optarg)) == -1) {
548 fprintf(stderr, "Invalid lifetime\n");
560 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
561 fatal("Invalid combination of actions");
563 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
567 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
571 if (delete_all(agent_fd) == -1)
578 if (pkcs11provider != NULL) {
579 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
589 if ((pw = getpwuid(getuid())) == NULL) {
590 fprintf(stderr, "No user found with uid %u\n",
596 for (i = 0; default_files[i]; i++) {
597 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
599 if (stat(buf, &st) < 0)
601 if (do_file(agent_fd, deleting, key_only, buf) == -1)
609 for (i = 0; i < argc; i++) {
610 if (do_file(agent_fd, deleting, key_only,
618 ssh_close_authentication_socket(agent_fd);