1 /* $OpenBSD: ssh-add.c,v 1.123 2015/07/03 03:43:18 djm 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,
83 _PATH_SSH_CLIENT_IDENTITY,
88 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
90 /* Default lifetime (0 == forever) */
91 static int lifetime = 0;
93 /* User has to confirm key use */
94 static int confirm = 0;
96 /* we keep a cache of one passphrases */
97 static char *pass = NULL;
102 explicit_bzero(pass, strlen(pass));
109 delete_file(int agent_fd, const char *filename, int key_only)
111 struct sshkey *public, *cert = NULL;
112 char *certpath = NULL, *comment = NULL;
115 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
116 printf("Bad key file %s: %s\n", filename, ssh_err(r));
119 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
120 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
123 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
124 filename, ssh_err(r));
129 /* Now try to delete the corresponding certificate too */
132 xasprintf(&certpath, "%s-cert.pub", filename);
133 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
134 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
135 error("Failed to load certificate \"%s\": %s",
136 certpath, ssh_err(r));
140 if (!sshkey_equal_public(cert, public))
141 fatal("Certificate %s does not match private key %s",
144 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
145 fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
149 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
150 certpath, ssh_err(r));
163 /* Send a request to remove all identities. */
165 delete_all(int agent_fd)
169 if (ssh_remove_all_identities(agent_fd, 2) == 0)
171 /* ignore error-code for ssh1 */
172 ssh_remove_all_identities(agent_fd, 1);
175 fprintf(stderr, "All identities removed.\n");
177 fprintf(stderr, "Failed to remove all identities.\n");
183 add_file(int agent_fd, const char *filename, int key_only)
185 struct sshkey *private, *cert;
186 char *comment = NULL;
187 char msg[1024], *certpath = NULL;
189 struct sshbuf *keyblob;
191 if (strcmp(filename, "-") == 0) {
193 filename = "(stdin)";
194 } else if ((fd = open(filename, O_RDONLY)) < 0) {
200 * Since we'll try to load a keyfile multiple times, permission errors
201 * will occur multiple times, so check perms first and bail if wrong.
203 if (fd != STDIN_FILENO) {
204 if (sshkey_perm_ok(fd, filename) != 0) {
209 if ((keyblob = sshbuf_new()) == NULL)
210 fatal("%s: sshbuf_new failed", __func__);
211 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
212 fprintf(stderr, "Error loading key \"%s\": %s\n",
213 filename, ssh_err(r));
214 sshbuf_free(keyblob);
220 /* At first, try empty passphrase */
221 if ((r = sshkey_parse_private_fileblob(keyblob, "", filename,
222 &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
223 fprintf(stderr, "Error loading key \"%s\": %s\n",
224 filename, ssh_err(r));
228 if (private == NULL && pass != NULL) {
229 if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename,
230 &private, &comment)) != 0 &&
231 r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
232 fprintf(stderr, "Error loading key \"%s\": %s\n",
233 filename, ssh_err(r));
238 comment = xstrdup(filename);
239 if (private == NULL) {
240 /* clear passphrase since it did not work */
242 snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ",
243 comment, confirm ? " (will confirm each use)" : "");
245 pass = read_passphrase(msg, RP_ALLOW_STDIN);
246 if (strcmp(pass, "") == 0)
248 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
249 filename, &private, NULL)) == 0)
251 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
253 "Error loading key \"%s\": %s\n",
254 filename, ssh_err(r));
258 sshbuf_free(keyblob);
262 snprintf(msg, sizeof msg,
263 "Bad passphrase, try again for %.200s%s: ", comment,
264 confirm ? " (will confirm each use)" : "");
267 sshbuf_free(keyblob);
269 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
270 lifetime, confirm)) == 0) {
271 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
275 "Lifetime set to %d seconds\n", lifetime);
278 "The user must confirm each use of the key\n");
280 fprintf(stderr, "Could not add identity \"%s\": %s\n",
281 filename, ssh_err(r));
284 /* Skip trying to load the cert if requested */
288 /* Now try to add the certificate flavour too */
289 xasprintf(&certpath, "%s-cert.pub", filename);
290 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
291 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
292 error("Failed to load certificate \"%s\": %s",
293 certpath, ssh_err(r));
297 if (!sshkey_equal_public(cert, private)) {
298 error("Certificate %s does not match private key %s",
304 /* Graft with private bits */
305 if ((r = sshkey_to_certified(private)) != 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 r, had_identities = 0;
368 struct ssh_identitylist *idlist;
376 for (; version <= 2; version++) {
377 if ((r = ssh_fetch_identitylist(agent_fd, version,
379 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
380 fprintf(stderr, "error fetching identities for "
381 "protocol %d: %s\n", version, ssh_err(r));
384 for (i = 0; i < idlist->nkeys; i++) {
387 fp = sshkey_fingerprint(idlist->keys[i],
388 fingerprint_hash, SSH_FP_DEFAULT);
389 printf("%d %s %s (%s)\n",
390 sshkey_size(idlist->keys[i]),
391 fp == NULL ? "(null)" : fp,
393 sshkey_type(idlist->keys[i]));
396 if ((r = sshkey_write(idlist->keys[i],
398 fprintf(stderr, "sshkey_write: %s\n",
402 fprintf(stdout, " %s\n", idlist->comments[i]);
405 ssh_free_identitylist(idlist);
407 if (!had_identities) {
408 printf("The agent has no identities.\n");
415 lock_agent(int agent_fd, int lock)
417 char prompt[100], *p1, *p2;
418 int r, passok = 1, ret = -1;
420 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
421 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
423 strlcpy(prompt, "Again: ", sizeof prompt);
424 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
425 if (strcmp(p1, p2) != 0) {
426 fprintf(stderr, "Passwords do not match.\n");
429 explicit_bzero(p2, strlen(p2));
433 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
434 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
437 fprintf(stderr, "Failed to %slock agent: %s\n",
438 lock ? "" : "un", ssh_err(r));
441 explicit_bzero(p1, strlen(p1));
447 do_file(int agent_fd, int deleting, int key_only, char *file)
450 if (delete_file(agent_fd, file, key_only) == -1)
453 if (add_file(agent_fd, file, key_only) == -1)
462 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
463 fprintf(stderr, "Options:\n");
464 fprintf(stderr, " -l List fingerprints of all identities.\n");
465 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
466 fprintf(stderr, " -L List public key parameters of all identities.\n");
467 fprintf(stderr, " -k Load only keys and not certificates.\n");
468 fprintf(stderr, " -c Require confirmation to sign using identities\n");
469 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
470 fprintf(stderr, " -d Delete identity.\n");
471 fprintf(stderr, " -D Delete all identities.\n");
472 fprintf(stderr, " -x Lock agent.\n");
473 fprintf(stderr, " -X Unlock agent.\n");
474 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
475 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
479 main(int argc, char **argv)
484 char *pkcs11provider = NULL;
485 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
486 int xflag = 0, lflag = 0, Dflag = 0;
488 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
491 __progname = ssh_get_progname(argv[0]);
495 OpenSSL_add_all_algorithms();
498 setvbuf(stdout, NULL, _IOLBF, 0);
500 /* First, get a connection to the authentication agent. */
501 switch (r = ssh_get_authentication_socket(&agent_fd)) {
504 case SSH_ERR_AGENT_NOT_PRESENT:
505 fprintf(stderr, "Could not open a connection to your "
506 "authentication agent.\n");
509 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
513 while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
516 fingerprint_hash = ssh_digest_alg_by_name(optarg);
517 if (fingerprint_hash == -1)
518 fatal("Invalid hash algorithm \"%s\"", optarg);
526 fatal("-%c flag already specified", lflag);
532 fatal("-%c flag already specified", xflag);
545 pkcs11provider = optarg;
549 pkcs11provider = optarg;
552 if ((lifetime = convtime(optarg)) == -1) {
553 fprintf(stderr, "Invalid lifetime\n");
565 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
566 fatal("Invalid combination of actions");
568 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
572 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
576 if (delete_all(agent_fd) == -1)
583 if (pkcs11provider != NULL) {
584 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
594 if ((pw = getpwuid(getuid())) == NULL) {
595 fprintf(stderr, "No user found with uid %u\n",
601 for (i = 0; default_files[i]; i++) {
602 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
604 if (stat(buf, &st) < 0)
606 if (do_file(agent_fd, deleting, key_only, buf) == -1)
614 for (i = 0; i < argc; i++) {
615 if (do_file(agent_fd, deleting, key_only,
623 ssh_close_authentication_socket(agent_fd);