1 /* $OpenBSD: ssh-add.c,v 1.134 2017/08/29 09:42:29 dlg 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"
63 #include "pathnames.h"
69 extern char *__progname;
71 /* Default files to add */
72 static char *default_files[] = {
74 _PATH_SSH_CLIENT_ID_RSA,
75 _PATH_SSH_CLIENT_ID_DSA,
76 #ifdef OPENSSL_HAS_ECC
77 _PATH_SSH_CLIENT_ID_ECDSA,
79 #endif /* WITH_OPENSSL */
80 _PATH_SSH_CLIENT_ID_ED25519,
84 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
86 /* Default lifetime (0 == forever) */
87 static int lifetime = 0;
89 /* User has to confirm key use */
90 static int confirm = 0;
92 /* we keep a cache of one passphrase */
93 static char *pass = NULL;
98 explicit_bzero(pass, strlen(pass));
105 delete_file(int agent_fd, const char *filename, int key_only, int qflag)
107 struct sshkey *public, *cert = NULL;
108 char *certpath = NULL, *comment = NULL;
111 if ((r = sshkey_load_public(filename, &public, &comment)) != 0) {
112 printf("Bad key file %s: %s\n", filename, ssh_err(r));
115 if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
117 fprintf(stderr, "Identity removed: %s (%s)\n",
122 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
123 filename, ssh_err(r));
128 /* Now try to delete the corresponding certificate too */
131 xasprintf(&certpath, "%s-cert.pub", filename);
132 if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
133 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
134 error("Failed to load certificate \"%s\": %s",
135 certpath, ssh_err(r));
139 if (!sshkey_equal_public(cert, public))
140 fatal("Certificate %s does not match private key %s",
143 if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
145 fprintf(stderr, "Identity removed: %s (%s)\n",
150 fprintf(stderr, "Could not remove identity \"%s\": %s\n",
151 certpath, ssh_err(r));
162 /* Send a request to remove all identities. */
164 delete_all(int agent_fd)
169 * Since the agent might be forwarded, old or non-OpenSSH, when asked
170 * to remove all keys, attempt to remove both protocol v.1 and v.2
173 if (ssh_remove_all_identities(agent_fd, 2) == 0)
175 /* ignore error-code for ssh1 */
176 ssh_remove_all_identities(agent_fd, 1);
179 fprintf(stderr, "All identities removed.\n");
181 fprintf(stderr, "Failed to remove all identities.\n");
187 add_file(int agent_fd, const char *filename, int key_only, int qflag)
189 struct sshkey *private, *cert;
190 char *comment = NULL;
191 char msg[1024], *certpath = NULL;
193 struct sshbuf *keyblob;
195 if (strcmp(filename, "-") == 0) {
197 filename = "(stdin)";
198 } else if ((fd = open(filename, O_RDONLY)) < 0) {
204 * Since we'll try to load a keyfile multiple times, permission errors
205 * will occur multiple times, so check perms first and bail if wrong.
207 if (fd != STDIN_FILENO) {
208 if (sshkey_perm_ok(fd, filename) != 0) {
213 if ((keyblob = sshbuf_new()) == NULL)
214 fatal("%s: sshbuf_new failed", __func__);
215 if ((r = sshkey_load_file(fd, keyblob)) != 0) {
216 fprintf(stderr, "Error loading key \"%s\": %s\n",
217 filename, ssh_err(r));
218 sshbuf_free(keyblob);
224 /* At first, try empty passphrase */
225 if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
226 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
227 fprintf(stderr, "Error loading key \"%s\": %s\n",
228 filename, ssh_err(r));
232 if (private == NULL && pass != NULL) {
233 if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
234 &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
235 fprintf(stderr, "Error loading key \"%s\": %s\n",
236 filename, ssh_err(r));
240 if (private == NULL) {
241 /* clear passphrase since it did not work */
243 snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
244 filename, confirm ? " (will confirm each use)" : "");
246 pass = read_passphrase(msg, RP_ALLOW_STDIN);
247 if (strcmp(pass, "") == 0)
249 if ((r = sshkey_parse_private_fileblob(keyblob, pass,
250 &private, &comment)) == 0)
252 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
254 "Error loading key \"%s\": %s\n",
255 filename, ssh_err(r));
258 sshbuf_free(keyblob);
262 snprintf(msg, sizeof msg,
263 "Bad passphrase, try again for %s%s: ", filename,
264 confirm ? " (will confirm each use)" : "");
267 if (comment == NULL || *comment == '\0')
268 comment = xstrdup(filename);
269 sshbuf_free(keyblob);
271 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
272 lifetime, confirm)) == 0) {
273 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
277 "Lifetime set to %d seconds\n", lifetime);
280 "The user must confirm each use of the key\n");
282 fprintf(stderr, "Could not add identity \"%s\": %s\n",
283 filename, ssh_err(r));
286 /* Skip trying to load the cert if requested */
290 /* Now try to add the certificate flavour too */
291 xasprintf(&certpath, "%s-cert.pub", filename);
292 if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
293 if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
294 error("Failed to load certificate \"%s\": %s",
295 certpath, ssh_err(r));
299 if (!sshkey_equal_public(cert, private)) {
300 error("Certificate %s does not match private key %s",
306 /* Graft with private bits */
307 if ((r = sshkey_to_certified(private)) != 0) {
308 error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
312 if ((r = sshkey_cert_copy(cert, private)) != 0) {
313 error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
319 if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
320 lifetime, confirm)) != 0) {
321 error("Certificate %s (%s) add failed: %s", certpath,
322 private->cert->key_id, ssh_err(r));
325 fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
326 private->cert->key_id);
328 fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
330 fprintf(stderr, "The user must confirm each use of the key\n");
334 sshkey_free(private);
340 update_card(int agent_fd, int add, const char *id)
346 if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
347 RP_ALLOW_STDIN)) == NULL)
351 if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
352 lifetime, confirm)) == 0) {
353 fprintf(stderr, "Card %s: %s\n",
354 add ? "added" : "removed", id);
357 fprintf(stderr, "Could not %s card \"%s\": %s\n",
358 add ? "add" : "remove", id, ssh_err(r));
366 list_identities(int agent_fd, int do_fp)
370 struct ssh_identitylist *idlist;
373 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
374 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
375 fprintf(stderr, "error fetching identities: %s\n",
378 printf("The agent has no identities.\n");
381 for (i = 0; i < idlist->nkeys; i++) {
383 fp = sshkey_fingerprint(idlist->keys[i],
384 fingerprint_hash, SSH_FP_DEFAULT);
385 printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
386 fp == NULL ? "(null)" : fp, idlist->comments[i],
387 sshkey_type(idlist->keys[i]));
390 if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
391 fprintf(stderr, "sshkey_write: %s\n",
395 fprintf(stdout, " %s\n", idlist->comments[i]);
398 ssh_free_identitylist(idlist);
403 lock_agent(int agent_fd, int lock)
405 char prompt[100], *p1, *p2;
406 int r, passok = 1, ret = -1;
408 strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
409 p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
411 strlcpy(prompt, "Again: ", sizeof prompt);
412 p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
413 if (strcmp(p1, p2) != 0) {
414 fprintf(stderr, "Passwords do not match.\n");
417 explicit_bzero(p2, strlen(p2));
421 if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
422 fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
425 fprintf(stderr, "Failed to %slock agent: %s\n",
426 lock ? "" : "un", ssh_err(r));
429 explicit_bzero(p1, strlen(p1));
435 do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
438 if (delete_file(agent_fd, file, key_only, qflag) == -1)
441 if (add_file(agent_fd, file, key_only, qflag) == -1)
450 fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
451 fprintf(stderr, "Options:\n");
452 fprintf(stderr, " -l List fingerprints of all identities.\n");
453 fprintf(stderr, " -E hash Specify hash algorithm used for fingerprints.\n");
454 fprintf(stderr, " -L List public key parameters of all identities.\n");
455 fprintf(stderr, " -k Load only keys and not certificates.\n");
456 fprintf(stderr, " -c Require confirmation to sign using identities\n");
457 fprintf(stderr, " -t life Set lifetime (in seconds) when adding identities.\n");
458 fprintf(stderr, " -d Delete identity.\n");
459 fprintf(stderr, " -D Delete all identities.\n");
460 fprintf(stderr, " -x Lock agent.\n");
461 fprintf(stderr, " -X Unlock agent.\n");
462 fprintf(stderr, " -s pkcs11 Add keys from PKCS#11 provider.\n");
463 fprintf(stderr, " -e pkcs11 Remove keys provided by PKCS#11 provider.\n");
464 fprintf(stderr, " -q Be quiet after a successful operation.\n");
468 main(int argc, char **argv)
473 char *pkcs11provider = NULL;
474 int r, i, ch, deleting = 0, ret = 0, key_only = 0;
475 int xflag = 0, lflag = 0, Dflag = 0, qflag = 0;
477 ssh_malloc_init(); /* must be called before any mallocs */
478 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
481 __progname = ssh_get_progname(argv[0]);
485 OpenSSL_add_all_algorithms();
488 setvbuf(stdout, NULL, _IOLBF, 0);
490 /* First, get a connection to the authentication agent. */
491 switch (r = ssh_get_authentication_socket(&agent_fd)) {
494 case SSH_ERR_AGENT_NOT_PRESENT:
495 fprintf(stderr, "Could not open a connection to your "
496 "authentication agent.\n");
499 fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
503 while ((ch = getopt(argc, argv, "klLcdDxXE:e:qs:t:")) != -1) {
506 fingerprint_hash = ssh_digest_alg_by_name(optarg);
507 if (fingerprint_hash == -1)
508 fatal("Invalid hash algorithm \"%s\"", optarg);
516 fatal("-%c flag already specified", lflag);
522 fatal("-%c flag already specified", xflag);
535 pkcs11provider = optarg;
539 pkcs11provider = optarg;
542 if ((lifetime = convtime(optarg)) == -1) {
543 fprintf(stderr, "Invalid lifetime\n");
558 if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
559 fatal("Invalid combination of actions");
561 if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
565 if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
569 if (delete_all(agent_fd) == -1)
576 if (pkcs11provider != NULL) {
577 if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
587 if ((pw = getpwuid(getuid())) == NULL) {
588 fprintf(stderr, "No user found with uid %u\n",
594 for (i = 0; default_files[i]; i++) {
595 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
597 if (stat(buf, &st) < 0)
599 if (do_file(agent_fd, deleting, key_only, buf,
608 for (i = 0; i < argc; i++) {
609 if (do_file(agent_fd, deleting, key_only,
610 argv[i], qflag) == -1)
617 ssh_close_authentication_socket(agent_fd);