djm@openbsd.org | 39736be | 2015-12-11 02:20:28 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: sshconnect2.c,v 1.234 2015/12/11 02:20:28 djm Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 4 | * Copyright (c) 2008 Damien Miller. All rights reserved. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | 2eb6340 | 2006-03-15 11:09:42 +1100 | [diff] [blame] | 28 | |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 29 | #include <sys/types.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/socket.h> |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 31 | #include <sys/wait.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 32 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 33 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 34 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 35 | #include <fcntl.h> |
Darren Tucker | f520ea1 | 2007-05-20 15:11:33 +1000 | [diff] [blame] | 36 | #include <netdb.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
| 38 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 39 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 40 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 41 | #include <string.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 42 | #include <unistd.h> |
Damien Miller | 63b4bcd | 2013-03-20 12:55:14 +1100 | [diff] [blame] | 43 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 44 | #include <vis.h> |
Damien Miller | 2e28d86 | 2008-07-17 19:15:43 +1000 | [diff] [blame] | 45 | #endif |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 46 | |
Damien Miller | 9c61769 | 2003-05-14 14:31:11 +1000 | [diff] [blame] | 47 | #include "openbsd-compat/sys-queue.h" |
| 48 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "buffer.h" |
| 53 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 55 | #include "cipher.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | #include "kex.h" |
| 58 | #include "myproposal.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 59 | #include "sshconnect.h" |
| 60 | #include "authfile.h" |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 61 | #include "dh.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 62 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 63 | #include "log.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 64 | #include "misc.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 65 | #include "readconf.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 66 | #include "match.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 67 | #include "dispatch.h" |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 68 | #include "canohost.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 69 | #include "msg.h" |
| 70 | #include "pathnames.h" |
Damien Miller | 6b4069a | 2006-06-13 13:05:15 +1000 | [diff] [blame] | 71 | #include "uidswap.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 72 | #include "hostfile.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 73 | #include "ssherr.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 74 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 75 | #ifdef GSSAPI |
| 76 | #include "ssh-gss.h" |
| 77 | #endif |
| 78 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 79 | /* import */ |
| 80 | extern char *client_version_string; |
| 81 | extern char *server_version_string; |
| 82 | extern Options options; |
| 83 | |
| 84 | /* |
| 85 | * SSH2 key exchange |
| 86 | */ |
| 87 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 88 | u_char *session_id2 = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 89 | u_int session_id2_len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 90 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 91 | char *xxx_host; |
| 92 | struct sockaddr *xxx_hostaddr; |
| 93 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 94 | static int |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 95 | verify_host_key_callback(Key *hostkey, struct ssh *ssh) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 96 | { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 97 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 98 | fatal("Host key verification failed."); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 102 | static char * |
| 103 | order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) |
| 104 | { |
| 105 | char *oavail, *avail, *first, *last, *alg, *hostname, *ret; |
| 106 | size_t maxlen; |
| 107 | struct hostkeys *hostkeys; |
| 108 | int ktype; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 109 | u_int i; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 110 | |
| 111 | /* Find all hostkeys for this hostname */ |
| 112 | get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); |
| 113 | hostkeys = init_hostkeys(); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 114 | for (i = 0; i < options.num_user_hostfiles; i++) |
| 115 | load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); |
| 116 | for (i = 0; i < options.num_system_hostfiles; i++) |
| 117 | load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 118 | |
| 119 | oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG); |
| 120 | maxlen = strlen(avail) + 1; |
| 121 | first = xmalloc(maxlen); |
| 122 | last = xmalloc(maxlen); |
| 123 | *first = *last = '\0'; |
| 124 | |
| 125 | #define ALG_APPEND(to, from) \ |
| 126 | do { \ |
| 127 | if (*to != '\0') \ |
| 128 | strlcat(to, ",", maxlen); \ |
| 129 | strlcat(to, from, maxlen); \ |
| 130 | } while (0) |
| 131 | |
| 132 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 133 | if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 134 | fatal("%s: unknown alg %s", __func__, alg); |
| 135 | if (lookup_key_in_hostkeys_by_type(hostkeys, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 136 | sshkey_type_plain(ktype), NULL)) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 137 | ALG_APPEND(first, alg); |
| 138 | else |
| 139 | ALG_APPEND(last, alg); |
| 140 | } |
| 141 | #undef ALG_APPEND |
djm@openbsd.org | 35d6022 | 2015-01-18 13:33:34 +0000 | [diff] [blame] | 142 | xasprintf(&ret, "%s%s%s", first, |
| 143 | (*first == '\0' || *last == '\0') ? "" : ",", last); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 144 | if (*first != '\0') |
| 145 | debug3("%s: prefer hostkeyalgs: %s", __func__, first); |
| 146 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 147 | free(first); |
| 148 | free(last); |
| 149 | free(hostname); |
| 150 | free(oavail); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 151 | free_hostkeys(hostkeys); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 156 | void |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 157 | ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 158 | { |
Damien Miller | 9235a03 | 2014-04-20 13:17:20 +1000 | [diff] [blame] | 159 | char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 160 | char *s; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 161 | struct kex *kex; |
markus@openbsd.org | 57e783c | 2015-01-20 20:16:21 +0000 | [diff] [blame] | 162 | int r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 163 | |
| 164 | xxx_host = host; |
| 165 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 166 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 167 | if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL) |
| 168 | fatal("%s: kex_names_cat", __func__); |
| 169 | myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s); |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 170 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 171 | compat_cipher_proposal(options.ciphers); |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 172 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 173 | compat_cipher_proposal(options.ciphers); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 174 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 175 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 176 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 177 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 178 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 179 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 180 | } |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 181 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 182 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 183 | if (options.hostkeyalgorithms != NULL) { |
| 184 | if (kex_assemble_names(KEX_DEFAULT_PK_ALG, |
| 185 | &options.hostkeyalgorithms) != 0) |
| 186 | fatal("%s: kex_assemble_namelist", __func__); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 187 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 188 | compat_pkalg_proposal(options.hostkeyalgorithms); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 189 | } else { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 190 | /* Enforce default */ |
| 191 | options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 192 | /* Prefer algorithms that we already have keys for */ |
| 193 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 194 | compat_pkalg_proposal( |
| 195 | order_hostkeyalgs(host, hostaddr, port)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 196 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 197 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 198 | if (options.rekey_limit || options.rekey_interval) |
| 199 | packet_set_rekey_limits((u_int32_t)options.rekey_limit, |
| 200 | (time_t)options.rekey_interval); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 201 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 202 | /* start key exchange */ |
markus@openbsd.org | 57e783c | 2015-01-20 20:16:21 +0000 | [diff] [blame] | 203 | if ((r = kex_setup(active_state, myproposal)) != 0) |
| 204 | fatal("kex_setup: %s", ssh_err(r)); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 205 | kex = active_state->kex; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 206 | #ifdef WITH_OPENSSL |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 207 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 208 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 209 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 210 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 211 | # ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 212 | kex->kex[KEX_ECDH_SHA2] = kexecdh_client; |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 213 | # endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 214 | #endif |
Damien Miller | 1e12426 | 2013-11-04 08:26:52 +1100 | [diff] [blame] | 215 | kex->kex[KEX_C25519_SHA256] = kexc25519_client; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 216 | kex->client_version_string=client_version_string; |
| 217 | kex->server_version_string=server_version_string; |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 218 | kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 219 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 220 | dispatch_run(DISPATCH_BLOCK, &kex->done, active_state); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 221 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 222 | if (options.use_roaming && !kex->roaming) { |
| 223 | debug("Roaming not allowed by server"); |
| 224 | options.use_roaming = 0; |
| 225 | } |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 226 | /* remove ext-info from the KEX proposals for rekeying */ |
| 227 | myproposal[PROPOSAL_KEX_ALGS] = |
| 228 | compat_kex_proposal(options.kex_algorithms); |
| 229 | if ((r = kex_prop2buf(kex->my, myproposal)) != 0) |
| 230 | fatal("kex_prop2buf: %s", ssh_err(r)); |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 231 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 232 | session_id2 = kex->session_id; |
| 233 | session_id2_len = kex->session_id_len; |
| 234 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 235 | #ifdef DEBUG_KEXDH |
| 236 | /* send 1st encrypted/maced/compressed message */ |
| 237 | packet_start(SSH2_MSG_IGNORE); |
| 238 | packet_put_cstring("markus"); |
| 239 | packet_send(); |
| 240 | packet_write_wait(); |
| 241 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 242 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 243 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 244 | /* |
| 245 | * Authenticate user |
| 246 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 247 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 248 | typedef struct cauthctxt Authctxt; |
| 249 | typedef struct cauthmethod Authmethod; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 250 | typedef struct identity Identity; |
| 251 | typedef struct idlist Idlist; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 252 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 253 | struct identity { |
| 254 | TAILQ_ENTRY(identity) next; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 255 | int agent_fd; /* >=0 if agent supports key */ |
| 256 | struct sshkey *key; /* public/private key */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 257 | char *filename; /* comment for agent-only keys */ |
| 258 | int tried; |
| 259 | int isprivate; /* key points to the private key */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 260 | int userprovided; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 261 | }; |
| 262 | TAILQ_HEAD(idlist, identity); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 263 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 264 | struct cauthctxt { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 265 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 266 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 267 | const char *host; |
| 268 | const char *service; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 269 | struct cauthmethod *method; |
Damien Miller | 79442c0 | 2010-05-10 11:55:38 +1000 | [diff] [blame] | 270 | sig_atomic_t success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 271 | char *authlist; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 272 | int attempt; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 273 | /* pubkey */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 274 | struct idlist keys; |
| 275 | int agent_fd; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 276 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 277 | Sensitive *sensitive; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 278 | char *oktypes, *ktypes; |
| 279 | const char *active_ktype; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 280 | /* kbd-interactive */ |
| 281 | int info_req_seen; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 282 | /* generic */ |
| 283 | void *methoddata; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 284 | }; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 285 | |
| 286 | struct cauthmethod { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 287 | char *name; /* string to compare against server's list */ |
| 288 | int (*userauth)(Authctxt *authctxt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 289 | void (*cleanup)(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 290 | int *enabled; /* flag in option struct that enables method */ |
| 291 | int *batch_flag; /* flag in option struct that disables method */ |
| 292 | }; |
| 293 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 294 | int input_userauth_service_accept(int, u_int32_t, void *); |
| 295 | int input_userauth_ext_info(int, u_int32_t, void *); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 296 | int input_userauth_success(int, u_int32_t, void *); |
| 297 | int input_userauth_success_unexpected(int, u_int32_t, void *); |
| 298 | int input_userauth_failure(int, u_int32_t, void *); |
| 299 | int input_userauth_banner(int, u_int32_t, void *); |
| 300 | int input_userauth_error(int, u_int32_t, void *); |
| 301 | int input_userauth_info_req(int, u_int32_t, void *); |
| 302 | int input_userauth_pk_ok(int, u_int32_t, void *); |
| 303 | int input_userauth_passwd_changereq(int, u_int32_t, void *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 304 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 305 | int userauth_none(Authctxt *); |
| 306 | int userauth_pubkey(Authctxt *); |
| 307 | int userauth_passwd(Authctxt *); |
| 308 | int userauth_kbdint(Authctxt *); |
| 309 | int userauth_hostbased(Authctxt *); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 310 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 311 | #ifdef GSSAPI |
| 312 | int userauth_gssapi(Authctxt *authctxt); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 313 | int input_gssapi_response(int type, u_int32_t, void *); |
| 314 | int input_gssapi_token(int type, u_int32_t, void *); |
| 315 | int input_gssapi_hash(int type, u_int32_t, void *); |
| 316 | int input_gssapi_error(int, u_int32_t, void *); |
| 317 | int input_gssapi_errtok(int, u_int32_t, void *); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 318 | #endif |
| 319 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 320 | void userauth(Authctxt *, char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 321 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 322 | static int sign_and_send_pubkey(Authctxt *, Identity *); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 323 | static void pubkey_prepare(Authctxt *); |
| 324 | static void pubkey_cleanup(Authctxt *); |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 325 | static Key *load_identity_file(Identity *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 326 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 327 | static Authmethod *authmethod_get(char *authlist); |
| 328 | static Authmethod *authmethod_lookup(const char *name); |
| 329 | static char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 330 | |
| 331 | Authmethod authmethods[] = { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 332 | #ifdef GSSAPI |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 333 | {"gssapi-with-mic", |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 334 | userauth_gssapi, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 335 | NULL, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 336 | &options.gss_authentication, |
| 337 | NULL}, |
| 338 | #endif |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 339 | {"hostbased", |
| 340 | userauth_hostbased, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 341 | NULL, |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 342 | &options.hostbased_authentication, |
| 343 | NULL}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 344 | {"publickey", |
| 345 | userauth_pubkey, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 346 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 347 | &options.pubkey_authentication, |
| 348 | NULL}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 349 | {"keyboard-interactive", |
| 350 | userauth_kbdint, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 351 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 352 | &options.kbd_interactive_authentication, |
| 353 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 354 | {"password", |
| 355 | userauth_passwd, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 356 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 357 | &options.password_authentication, |
| 358 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | {"none", |
| 360 | userauth_none, |
| 361 | NULL, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 362 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 363 | NULL}, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 364 | {NULL, NULL, NULL, NULL, NULL} |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 368 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 369 | Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 370 | { |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 371 | struct ssh *ssh = active_state; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 372 | Authctxt authctxt; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 373 | int r; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 374 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 375 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 376 | options.kbd_interactive_authentication = 1; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 377 | if (options.preferred_authentications == NULL) |
| 378 | options.preferred_authentications = authmethods_get(); |
| 379 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 380 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 381 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 382 | pubkey_prepare(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 383 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 384 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 385 | authctxt.host = host; |
| 386 | authctxt.service = "ssh-connection"; /* service name */ |
| 387 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 388 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 389 | authctxt.authlist = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 390 | authctxt.methoddata = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 391 | authctxt.sensitive = sensitive; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 392 | authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 393 | authctxt.info_req_seen = 0; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 394 | authctxt.agent_fd = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 395 | if (authctxt.method == NULL) |
| 396 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 397 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 398 | if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 || |
| 399 | (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 || |
| 400 | (r = sshpkt_send(ssh)) != 0) |
| 401 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 402 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 403 | ssh_dispatch_init(ssh, &input_userauth_error); |
| 404 | ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info); |
| 405 | ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept); |
| 406 | ssh_dispatch_run(ssh, DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 407 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 408 | pubkey_cleanup(&authctxt); |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 409 | ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 410 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 411 | debug("Authentication succeeded (%s).", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 412 | } |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 413 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 414 | /* ARGSUSED */ |
| 415 | int |
| 416 | input_userauth_service_accept(int type, u_int32_t seqnr, void *ctxt) |
| 417 | { |
| 418 | Authctxt *authctxt = ctxt; |
| 419 | struct ssh *ssh = active_state; |
| 420 | int r; |
| 421 | |
| 422 | if (ssh_packet_remaining(ssh) > 0) { |
| 423 | char *reply; |
| 424 | |
| 425 | if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0) |
| 426 | goto out; |
| 427 | debug2("service_accept: %s", reply); |
| 428 | free(reply); |
| 429 | } else { |
| 430 | debug2("buggy server: service_accept w/o service"); |
| 431 | } |
| 432 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 433 | goto out; |
| 434 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
| 435 | |
| 436 | /* initial userauth request */ |
| 437 | userauth_none(authctxt); |
| 438 | |
| 439 | ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error); |
| 440 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 441 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
| 442 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
| 443 | r = 0; |
| 444 | out: |
| 445 | return r; |
| 446 | } |
| 447 | |
| 448 | /* ARGSUSED */ |
| 449 | int |
| 450 | input_userauth_ext_info(int type, u_int32_t seqnr, void *ctxt) |
| 451 | { |
| 452 | return kex_input_ext_info(type, seqnr, active_state); |
| 453 | } |
| 454 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 455 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 456 | userauth(Authctxt *authctxt, char *authlist) |
| 457 | { |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 458 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 459 | authctxt->method->cleanup(authctxt); |
| 460 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 461 | free(authctxt->methoddata); |
| 462 | authctxt->methoddata = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 463 | if (authlist == NULL) { |
| 464 | authlist = authctxt->authlist; |
| 465 | } else { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 466 | free(authctxt->authlist); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 467 | authctxt->authlist = authlist; |
| 468 | } |
| 469 | for (;;) { |
| 470 | Authmethod *method = authmethod_get(authlist); |
| 471 | if (method == NULL) |
| 472 | fatal("Permission denied (%s).", authlist); |
| 473 | authctxt->method = method; |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 474 | |
| 475 | /* reset the per method handler */ |
| 476 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, |
| 477 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); |
| 478 | |
| 479 | /* and try new method */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 480 | if (method->userauth(authctxt) != 0) { |
| 481 | debug2("we sent a %s packet, wait for reply", method->name); |
| 482 | break; |
| 483 | } else { |
| 484 | debug2("we did not send a packet, disable method"); |
| 485 | method->enabled = NULL; |
| 486 | } |
| 487 | } |
| 488 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 489 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 490 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 491 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 492 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 493 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 494 | fatal("input_userauth_error: bad message during authentication: " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 495 | "type %d", type); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 496 | return 0; |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 497 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 498 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 499 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 500 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 501 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 502 | { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 503 | char *msg, *raw, *lang; |
| 504 | u_int len; |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 505 | |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 506 | debug3("input_userauth_banner"); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 507 | raw = packet_get_string(&len); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 508 | lang = packet_get_string(NULL); |
Damien Miller | c674d58 | 2008-11-03 19:16:57 +1100 | [diff] [blame] | 509 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 510 | if (len > 65536) |
| 511 | len = 65536; |
Damien Miller | c4d1b36 | 2008-11-03 19:22:09 +1100 | [diff] [blame] | 512 | msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ |
Darren Tucker | 0c348f5 | 2010-01-08 18:58:05 +1100 | [diff] [blame] | 513 | strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH); |
Darren Tucker | 046dff2 | 2003-10-08 17:32:02 +1000 | [diff] [blame] | 514 | fprintf(stderr, "%s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 515 | free(msg); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 516 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 517 | free(raw); |
| 518 | free(lang); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 519 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 520 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 521 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 522 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 523 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 524 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 525 | { |
| 526 | Authctxt *authctxt = ctxt; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 527 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 528 | if (authctxt == NULL) |
| 529 | fatal("input_userauth_success: no authentication context"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 530 | free(authctxt->authlist); |
| 531 | authctxt->authlist = NULL; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 532 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 533 | authctxt->method->cleanup(authctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 534 | free(authctxt->methoddata); |
| 535 | authctxt->methoddata = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 536 | authctxt->success = 1; /* break out */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 537 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 538 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 539 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 540 | int |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 541 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) |
| 542 | { |
| 543 | Authctxt *authctxt = ctxt; |
| 544 | |
| 545 | if (authctxt == NULL) |
| 546 | fatal("%s: no authentication context", __func__); |
| 547 | |
| 548 | fatal("Unexpected authentication success during %s.", |
| 549 | authctxt->method->name); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 550 | return 0; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 551 | } |
| 552 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 553 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 554 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 555 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 556 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 557 | Authctxt *authctxt = ctxt; |
| 558 | char *authlist = NULL; |
| 559 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 560 | |
| 561 | if (authctxt == NULL) |
| 562 | fatal("input_userauth_failure: no authentication context"); |
| 563 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 564 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 565 | partial = packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 566 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 567 | |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 568 | if (partial != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 569 | logit("Authenticated with partial success."); |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 570 | /* reset state */ |
| 571 | pubkey_cleanup(authctxt); |
| 572 | pubkey_prepare(authctxt); |
| 573 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 574 | debug("Authentications that can continue: %s", authlist); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 575 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 576 | userauth(authctxt, authlist); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 577 | return 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 578 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 579 | |
| 580 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 581 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 582 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 583 | { |
| 584 | Authctxt *authctxt = ctxt; |
| 585 | Key *key = NULL; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 586 | Identity *id = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 587 | Buffer b; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 588 | int pktype, sent = 0; |
| 589 | u_int alen, blen; |
| 590 | char *pkalg, *fp; |
| 591 | u_char *pkblob; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 592 | |
| 593 | if (authctxt == NULL) |
| 594 | fatal("input_userauth_pk_ok: no authentication context"); |
| 595 | if (datafellows & SSH_BUG_PKOK) { |
| 596 | /* this is similar to SSH_BUG_PKAUTH */ |
| 597 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 598 | pkblob = packet_get_string(&blen); |
| 599 | buffer_init(&b); |
| 600 | buffer_append(&b, pkblob, blen); |
| 601 | pkalg = buffer_get_string(&b, &alen); |
| 602 | buffer_free(&b); |
| 603 | } else { |
| 604 | pkalg = packet_get_string(&alen); |
| 605 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 606 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 607 | packet_check_eom(); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 608 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 609 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 610 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 611 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
| 612 | debug("unknown pkalg %s", pkalg); |
| 613 | goto done; |
| 614 | } |
| 615 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 616 | debug("no key from blob. pkalg %s", pkalg); |
| 617 | goto done; |
| 618 | } |
| 619 | if (key->type != pktype) { |
| 620 | error("input_userauth_pk_ok: type mismatch " |
| 621 | "for decoded key (received %d, expected %d)", |
| 622 | key->type, pktype); |
| 623 | goto done; |
| 624 | } |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 625 | if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 626 | SSH_FP_DEFAULT)) == NULL) |
| 627 | goto done; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 628 | debug2("input_userauth_pk_ok: fp %s", fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 629 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 630 | |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 631 | /* |
| 632 | * search keys in the reverse order, because last candidate has been |
| 633 | * moved to the end of the queue. this also avoids confusion by |
| 634 | * duplicate keys |
| 635 | */ |
Damien Miller | 0b51a52 | 2004-04-20 20:07:19 +1000 | [diff] [blame] | 636 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 637 | if (key_equal(key, id->key)) { |
| 638 | sent = sign_and_send_pubkey(authctxt, id); |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | done: |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 643 | if (key != NULL) |
| 644 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 645 | free(pkalg); |
| 646 | free(pkblob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 647 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 648 | /* try another method if we did not send a packet */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 649 | if (sent == 0) |
| 650 | userauth(authctxt, NULL); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 651 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 652 | } |
| 653 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 654 | #ifdef GSSAPI |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 655 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 656 | userauth_gssapi(Authctxt *authctxt) |
| 657 | { |
| 658 | Gssctxt *gssctxt = NULL; |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 659 | static gss_OID_set gss_supported = NULL; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 660 | static u_int mech = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 661 | OM_uint32 min; |
| 662 | int ok = 0; |
| 663 | |
| 664 | /* Try one GSSAPI method at a time, rather than sending them all at |
| 665 | * once. */ |
| 666 | |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 667 | if (gss_supported == NULL) |
| 668 | gss_indicate_mechs(&min, &gss_supported); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 669 | |
| 670 | /* Check to see if the mechanism is usable before we offer it */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 671 | while (mech < gss_supported->count && !ok) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 672 | /* My DER encoding requires length<128 */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 673 | if (gss_supported->elements[mech].length < 128 && |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 674 | ssh_gssapi_check_mechanism(&gssctxt, |
| 675 | &gss_supported->elements[mech], authctxt->host)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 676 | ok = 1; /* Mechanism works */ |
| 677 | } else { |
| 678 | mech++; |
| 679 | } |
| 680 | } |
| 681 | |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 682 | if (!ok) |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 683 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 684 | |
| 685 | authctxt->methoddata=(void *)gssctxt; |
| 686 | |
| 687 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 688 | packet_put_cstring(authctxt->server_user); |
| 689 | packet_put_cstring(authctxt->service); |
| 690 | packet_put_cstring(authctxt->method->name); |
| 691 | |
| 692 | packet_put_int(1); |
| 693 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 694 | packet_put_int((gss_supported->elements[mech].length) + 2); |
| 695 | packet_put_char(SSH_GSS_OIDTYPE); |
| 696 | packet_put_char(gss_supported->elements[mech].length); |
| 697 | packet_put_raw(gss_supported->elements[mech].elements, |
| 698 | gss_supported->elements[mech].length); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 699 | |
| 700 | packet_send(); |
| 701 | |
| 702 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); |
| 703 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); |
| 704 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); |
| 705 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); |
| 706 | |
| 707 | mech++; /* Move along to next candidate */ |
| 708 | |
| 709 | return 1; |
| 710 | } |
| 711 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 712 | static OM_uint32 |
| 713 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) |
| 714 | { |
| 715 | Authctxt *authctxt = ctxt; |
| 716 | Gssctxt *gssctxt = authctxt->methoddata; |
| 717 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
Damien Miller | da9984f | 2005-08-31 19:46:26 +1000 | [diff] [blame] | 718 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
| 719 | gss_buffer_desc gssbuf; |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 720 | OM_uint32 status, ms, flags; |
| 721 | Buffer b; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 722 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 723 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 724 | recv_tok, &send_tok, &flags); |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 725 | |
| 726 | if (send_tok.length > 0) { |
| 727 | if (GSS_ERROR(status)) |
| 728 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); |
| 729 | else |
| 730 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 731 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 732 | packet_put_string(send_tok.value, send_tok.length); |
| 733 | packet_send(); |
| 734 | gss_release_buffer(&ms, &send_tok); |
| 735 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 736 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 737 | if (status == GSS_S_COMPLETE) { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 738 | /* send either complete or MIC, depending on mechanism */ |
| 739 | if (!(flags & GSS_C_INTEG_FLAG)) { |
| 740 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); |
| 741 | packet_send(); |
| 742 | } else { |
| 743 | ssh_gssapi_buildmic(&b, authctxt->server_user, |
| 744 | authctxt->service, "gssapi-with-mic"); |
| 745 | |
| 746 | gssbuf.value = buffer_ptr(&b); |
| 747 | gssbuf.length = buffer_len(&b); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 748 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 749 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 750 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 751 | if (!GSS_ERROR(status)) { |
| 752 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); |
| 753 | packet_put_string(mic.value, mic.length); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 754 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 755 | packet_send(); |
| 756 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 757 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 758 | buffer_free(&b); |
| 759 | gss_release_buffer(&ms, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 760 | } |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 761 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 762 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 763 | return status; |
| 764 | } |
| 765 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 766 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 767 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 768 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
| 769 | { |
| 770 | Authctxt *authctxt = ctxt; |
| 771 | Gssctxt *gssctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 772 | int oidlen; |
| 773 | char *oidv; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 774 | |
| 775 | if (authctxt == NULL) |
| 776 | fatal("input_gssapi_response: no authentication context"); |
| 777 | gssctxt = authctxt->methoddata; |
| 778 | |
| 779 | /* Setup our OID */ |
| 780 | oidv = packet_get_string(&oidlen); |
| 781 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 782 | if (oidlen <= 2 || |
| 783 | oidv[0] != SSH_GSS_OIDTYPE || |
| 784 | oidv[1] != oidlen - 2) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 785 | free(oidv); |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 786 | debug("Badly encoded mechanism OID received"); |
| 787 | userauth(authctxt, NULL); |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 788 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 789 | } |
| 790 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 791 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
| 792 | fatal("Server returned different OID than expected"); |
| 793 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 794 | packet_check_eom(); |
| 795 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 796 | free(oidv); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 797 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 798 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 799 | /* Start again with next method on list */ |
| 800 | debug("Trying to start again"); |
| 801 | userauth(authctxt, NULL); |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 802 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 803 | } |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 804 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 805 | } |
| 806 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 807 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 808 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 809 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
| 810 | { |
| 811 | Authctxt *authctxt = ctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 812 | gss_buffer_desc recv_tok; |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 813 | OM_uint32 status; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 814 | u_int slen; |
| 815 | |
| 816 | if (authctxt == NULL) |
| 817 | fatal("input_gssapi_response: no authentication context"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 818 | |
| 819 | recv_tok.value = packet_get_string(&slen); |
| 820 | recv_tok.length = slen; /* safe typecast */ |
| 821 | |
| 822 | packet_check_eom(); |
| 823 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 824 | status = process_gssapi_token(ctxt, &recv_tok); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 825 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 826 | free(recv_tok.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 827 | |
| 828 | if (GSS_ERROR(status)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 829 | /* Start again with the next method in the list */ |
| 830 | userauth(authctxt, NULL); |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 831 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 832 | } |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 833 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 834 | } |
| 835 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 836 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 837 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 838 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) |
| 839 | { |
| 840 | Authctxt *authctxt = ctxt; |
| 841 | Gssctxt *gssctxt; |
| 842 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 843 | gss_buffer_desc recv_tok; |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 844 | OM_uint32 ms; |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 845 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 846 | |
| 847 | if (authctxt == NULL) |
| 848 | fatal("input_gssapi_response: no authentication context"); |
| 849 | gssctxt = authctxt->methoddata; |
| 850 | |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 851 | recv_tok.value = packet_get_string(&len); |
| 852 | recv_tok.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 853 | |
| 854 | packet_check_eom(); |
| 855 | |
| 856 | /* Stick it into GSSAPI and see what it says */ |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 857 | (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 858 | &recv_tok, &send_tok, NULL); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 859 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 860 | free(recv_tok.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 861 | gss_release_buffer(&ms, &send_tok); |
| 862 | |
| 863 | /* Server will be returning a failed packet after this one */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 864 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 865 | } |
| 866 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 867 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 868 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 869 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) |
| 870 | { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 871 | char *msg; |
| 872 | char *lang; |
| 873 | |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 874 | /* maj */(void)packet_get_int(); |
| 875 | /* min */(void)packet_get_int(); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 876 | msg=packet_get_string(NULL); |
| 877 | lang=packet_get_string(NULL); |
| 878 | |
| 879 | packet_check_eom(); |
| 880 | |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 881 | debug("Server GSSAPI Error:\n%s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 882 | free(msg); |
| 883 | free(lang); |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 884 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 885 | } |
| 886 | #endif /* GSSAPI */ |
| 887 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 888 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 889 | userauth_none(Authctxt *authctxt) |
| 890 | { |
| 891 | /* initial userauth request */ |
| 892 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 893 | packet_put_cstring(authctxt->server_user); |
| 894 | packet_put_cstring(authctxt->service); |
| 895 | packet_put_cstring(authctxt->method->name); |
| 896 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 901 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 902 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 903 | static int attempt = 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 904 | char prompt[150]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 905 | char *password; |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 906 | const char *host = options.host_key_alias ? options.host_key_alias : |
| 907 | authctxt->host; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 908 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 909 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 910 | return 0; |
| 911 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 912 | if (attempt != 1) |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 913 | error("Permission denied, please try again."); |
| 914 | |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 915 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 916 | authctxt->server_user, host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 917 | password = read_passphrase(prompt, 0); |
| 918 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 919 | packet_put_cstring(authctxt->server_user); |
| 920 | packet_put_cstring(authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 921 | packet_put_cstring(authctxt->method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 922 | packet_put_char(0); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 923 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 924 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 925 | free(password); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 926 | packet_add_padding(64); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 927 | packet_send(); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 928 | |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 929 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 930 | &input_userauth_passwd_changereq); |
| 931 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 932 | return 1; |
| 933 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 934 | |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 935 | /* |
| 936 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
| 937 | */ |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 938 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 939 | int |
Tim Rice | c854962 | 2002-03-31 12:49:38 -0800 | [diff] [blame] | 940 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 941 | { |
| 942 | Authctxt *authctxt = ctxt; |
| 943 | char *info, *lang, *password = NULL, *retype = NULL; |
| 944 | char prompt[150]; |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 945 | const char *host = options.host_key_alias ? options.host_key_alias : |
| 946 | authctxt->host; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 947 | |
| 948 | debug2("input_userauth_passwd_changereq"); |
| 949 | |
| 950 | if (authctxt == NULL) |
| 951 | fatal("input_userauth_passwd_changereq: " |
| 952 | "no authentication context"); |
| 953 | |
| 954 | info = packet_get_string(NULL); |
| 955 | lang = packet_get_string(NULL); |
| 956 | if (strlen(info) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 957 | logit("%s", info); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 958 | free(info); |
| 959 | free(lang); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 960 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 961 | packet_put_cstring(authctxt->server_user); |
| 962 | packet_put_cstring(authctxt->service); |
| 963 | packet_put_cstring(authctxt->method->name); |
| 964 | packet_put_char(1); /* additional info */ |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 965 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 966 | "Enter %.30s@%.128s's old password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 967 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 968 | password = read_passphrase(prompt, 0); |
| 969 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 970 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 971 | free(password); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 972 | password = NULL; |
| 973 | while (password == NULL) { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 974 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 975 | "Enter %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 976 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 977 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
| 978 | if (password == NULL) { |
| 979 | /* bail out */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 980 | return 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 981 | } |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 982 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 983 | "Retype %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 984 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 985 | retype = read_passphrase(prompt, 0); |
| 986 | if (strcmp(password, retype) != 0) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 987 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 988 | free(password); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 989 | logit("Mismatch; try again, EOF to quit."); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 990 | password = NULL; |
| 991 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 992 | explicit_bzero(retype, strlen(retype)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 993 | free(retype); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 994 | } |
| 995 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 996 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 997 | free(password); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 998 | packet_add_padding(64); |
| 999 | packet_send(); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 1000 | |
| 1001 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1002 | &input_userauth_passwd_changereq); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1003 | return 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1004 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1005 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1006 | static const char * |
| 1007 | identity_sign_encode(struct identity *id) |
| 1008 | { |
| 1009 | struct ssh *ssh = active_state; |
| 1010 | |
| 1011 | if (id->key->type == KEY_RSA) { |
| 1012 | switch (ssh->kex->rsa_sha2) { |
| 1013 | case 256: |
| 1014 | return "rsa-sha2-256"; |
| 1015 | case 512: |
| 1016 | return "rsa-sha2-512"; |
| 1017 | } |
| 1018 | } |
| 1019 | return key_ssh_name(id->key); |
| 1020 | } |
| 1021 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1022 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1023 | identity_sign(struct identity *id, u_char **sigp, size_t *lenp, |
| 1024 | const u_char *data, size_t datalen, u_int compat) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1025 | { |
| 1026 | Key *prv; |
| 1027 | int ret; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1028 | const char *alg; |
| 1029 | |
| 1030 | alg = identity_sign_encode(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1031 | |
| 1032 | /* the agent supports this key */ |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1033 | if (id->agent_fd != -1) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1034 | return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1035 | data, datalen, alg, compat); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1036 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1037 | /* |
| 1038 | * we have already loaded the private key or |
| 1039 | * the private key is stored in external hardware |
| 1040 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1041 | if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1042 | return (sshkey_sign(id->key, sigp, lenp, data, datalen, alg, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1043 | compat)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1044 | /* load the private key from the file */ |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1045 | if ((prv = load_identity_file(id)) == NULL) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1046 | return (-1); /* XXX return decent error code */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1047 | ret = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1048 | sshkey_free(prv); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1049 | return (ret); |
| 1050 | } |
| 1051 | |
| 1052 | static int |
| 1053 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1054 | { |
| 1055 | Buffer b; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1056 | Identity *private_id; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1057 | u_char *blob, *signature; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1058 | size_t slen; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1059 | u_int bloblen, skip = 0; |
| 1060 | int matched, ret = -1, have_sig = 1; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1061 | char *fp; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1062 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1063 | if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash, |
| 1064 | SSH_FP_DEFAULT)) == NULL) |
| 1065 | return 0; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1066 | debug3("%s: %s %s", __func__, key_type(id->key), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1067 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1068 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1069 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1070 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1071 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1074 | /* data to be signed */ |
| 1075 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1076 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1077 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1078 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1079 | } else { |
| 1080 | buffer_put_string(&b, session_id2, session_id2_len); |
| 1081 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1082 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1083 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1084 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1085 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1086 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1087 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1088 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1089 | if (datafellows & SSH_BUG_PKAUTH) { |
| 1090 | buffer_put_char(&b, have_sig); |
| 1091 | } else { |
| 1092 | buffer_put_cstring(&b, authctxt->method->name); |
| 1093 | buffer_put_char(&b, have_sig); |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1094 | buffer_put_cstring(&b, identity_sign_encode(id)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1095 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1096 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1097 | |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | * If the key is an certificate, try to find a matching private key |
| 1100 | * and use it to complete the signature. |
| 1101 | * If no such private key exists, return failure and continue with |
| 1102 | * other methods of authentication. |
| 1103 | */ |
| 1104 | if (key_is_cert(id->key)) { |
| 1105 | matched = 0; |
| 1106 | TAILQ_FOREACH(private_id, &authctxt->keys, next) { |
| 1107 | if (sshkey_equal_public(id->key, private_id->key) && |
| 1108 | id->key->type != private_id->key->type) { |
| 1109 | id = private_id; |
| 1110 | matched = 1; |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | if (matched) { |
| 1115 | debug2("%s: using private key \"%s\"%s for " |
| 1116 | "certificate", __func__, id->filename, |
| 1117 | id->agent_fd != -1 ? " from agent" : ""); |
| 1118 | } else { |
| 1119 | /* XXX maybe verbose/error? */ |
| 1120 | debug("%s: no private key for certificate " |
| 1121 | "\"%s\"", __func__, id->filename); |
| 1122 | free(blob); |
| 1123 | buffer_free(&b); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1128 | /* generate signature */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1129 | ret = identity_sign(id, &signature, &slen, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1130 | buffer_ptr(&b), buffer_len(&b), datafellows); |
| 1131 | if (ret != 0) { |
djm@openbsd.org | 39736be | 2015-12-11 02:20:28 +0000 | [diff] [blame^] | 1132 | error("%s: signing failed: %s", __func__, ssh_err(ret)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1133 | free(blob); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1134 | buffer_free(&b); |
| 1135 | return 0; |
| 1136 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1137 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1138 | buffer_dump(&b); |
| 1139 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1140 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1141 | buffer_clear(&b); |
| 1142 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1143 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1144 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1145 | buffer_put_cstring(&b, authctxt->server_user); |
| 1146 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1147 | buffer_put_cstring(&b, authctxt->method->name); |
| 1148 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1149 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1150 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1151 | buffer_put_string(&b, blob, bloblen); |
| 1152 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1153 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1154 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1155 | /* append signature */ |
| 1156 | buffer_put_string(&b, signature, slen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1157 | free(signature); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1158 | |
| 1159 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1160 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1161 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1162 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1163 | |
| 1164 | /* put remaining data from buffer into packet */ |
| 1165 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1166 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 1167 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1168 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1169 | |
| 1170 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1171 | } |
| 1172 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1173 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1174 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1175 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1176 | u_char *blob; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1177 | u_int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1178 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1179 | debug3("send_pubkey_test"); |
| 1180 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1181 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1182 | /* we cannot handle this key */ |
| 1183 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1186 | /* register callback for USERAUTH_PK_OK message */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1187 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1188 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1189 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1190 | packet_put_cstring(authctxt->server_user); |
| 1191 | packet_put_cstring(authctxt->service); |
| 1192 | packet_put_cstring(authctxt->method->name); |
| 1193 | packet_put_char(have_sig); |
| 1194 | if (!(datafellows & SSH_BUG_PKAUTH)) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1195 | packet_put_cstring(identity_sign_encode(id)); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1196 | packet_put_string(blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1197 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1198 | packet_send(); |
| 1199 | return 1; |
| 1200 | } |
| 1201 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1202 | static Key * |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1203 | load_identity_file(Identity *id) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1204 | { |
| 1205 | Key *private; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1206 | char prompt[300], *passphrase, *comment; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1207 | int r, perm_ok = 0, quit = 0, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1208 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1209 | |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1210 | if (stat(id->filename, &st) < 0) { |
| 1211 | (id->userprovided ? logit : debug3)("no such identity: %s: %s", |
| 1212 | id->filename, strerror(errno)); |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1213 | return NULL; |
| 1214 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1215 | snprintf(prompt, sizeof prompt, |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1216 | "Enter passphrase for key '%.100s': ", id->filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1217 | for (i = 0; i <= options.number_of_password_prompts; i++) { |
| 1218 | if (i == 0) |
| 1219 | passphrase = ""; |
| 1220 | else { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1221 | passphrase = read_passphrase(prompt, 0); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1222 | if (*passphrase == '\0') { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1223 | debug2("no passphrase given, try next key"); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1224 | free(passphrase); |
| 1225 | break; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1226 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1227 | } |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1228 | switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename, |
| 1229 | passphrase, &private, &comment, &perm_ok))) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1230 | case 0: |
| 1231 | break; |
| 1232 | case SSH_ERR_KEY_WRONG_PASSPHRASE: |
| 1233 | if (options.batch_mode) { |
| 1234 | quit = 1; |
| 1235 | break; |
| 1236 | } |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1237 | if (i != 0) |
| 1238 | debug2("bad passphrase given, try again..."); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1239 | break; |
| 1240 | case SSH_ERR_SYSTEM_ERROR: |
| 1241 | if (errno == ENOENT) { |
| 1242 | debug2("Load key \"%s\": %s", |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1243 | id->filename, ssh_err(r)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1244 | quit = 1; |
| 1245 | break; |
| 1246 | } |
| 1247 | /* FALLTHROUGH */ |
| 1248 | default: |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1249 | error("Load key \"%s\": %s", id->filename, ssh_err(r)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1250 | quit = 1; |
| 1251 | break; |
| 1252 | } |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1253 | if (!quit && private != NULL && id->agent_fd == -1 && |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1254 | !(id->key && id->isprivate)) |
| 1255 | maybe_add_key_to_agent(id->filename, private, comment, |
| 1256 | passphrase); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1257 | if (i > 0) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1258 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1259 | free(passphrase); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1260 | } |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 1261 | free(comment); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1262 | if (private != NULL || quit) |
| 1263 | break; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1264 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1265 | return private; |
| 1266 | } |
| 1267 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1268 | /* |
| 1269 | * try keys in the following order: |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1270 | * 1. certificates listed in the config file |
| 1271 | * 2. other input certificates |
| 1272 | * 3. agent keys that are found in the config file |
| 1273 | * 4. other agent keys |
| 1274 | * 5. keys that are only listed in the config file |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1275 | */ |
| 1276 | static void |
| 1277 | pubkey_prepare(Authctxt *authctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1278 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1279 | struct identity *id, *id2, *tmp; |
| 1280 | struct idlist agent, files, *preferred; |
| 1281 | struct sshkey *key; |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1282 | int agent_fd = -1, i, r, found; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1283 | size_t j; |
| 1284 | struct ssh_identitylist *idlist; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1285 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1286 | TAILQ_INIT(&agent); /* keys from the agent */ |
| 1287 | TAILQ_INIT(&files); /* keys from the config file */ |
| 1288 | preferred = &authctxt->keys; |
| 1289 | TAILQ_INIT(preferred); /* preferred order of keys */ |
| 1290 | |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1291 | /* list of keys stored in the filesystem and PKCS#11 */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1292 | for (i = 0; i < options.num_identity_files; i++) { |
| 1293 | key = options.identity_keys[i]; |
| 1294 | if (key && key->type == KEY_RSA1) |
| 1295 | continue; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1296 | if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) |
| 1297 | continue; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1298 | options.identity_keys[i] = NULL; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1299 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1300 | id->agent_fd = -1; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1301 | id->key = key; |
| 1302 | id->filename = xstrdup(options.identity_files[i]); |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1303 | id->userprovided = options.identity_file_userprovided[i]; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1304 | TAILQ_INSERT_TAIL(&files, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1305 | } |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1306 | /* Prefer PKCS11 keys that are explicitly listed */ |
| 1307 | TAILQ_FOREACH_SAFE(id, &files, next, tmp) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1308 | if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0) |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1309 | continue; |
| 1310 | found = 0; |
| 1311 | TAILQ_FOREACH(id2, &files, next) { |
| 1312 | if (id2->key == NULL || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1313 | (id2->key->flags & SSHKEY_FLAG_EXT) == 0) |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1314 | continue; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1315 | if (sshkey_equal(id->key, id2->key)) { |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1316 | TAILQ_REMOVE(&files, id, next); |
| 1317 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1318 | found = 1; |
| 1319 | break; |
| 1320 | } |
| 1321 | } |
| 1322 | /* If IdentitiesOnly set and key not found then don't use it */ |
| 1323 | if (!found && options.identities_only) { |
| 1324 | TAILQ_REMOVE(&files, id, next); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 1325 | explicit_bzero(id, sizeof(*id)); |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1326 | free(id); |
| 1327 | } |
| 1328 | } |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1329 | /* list of certificates specified by user */ |
| 1330 | for (i = 0; i < options.num_certificate_files; i++) { |
| 1331 | key = options.certificates[i]; |
| 1332 | if (!key_is_cert(key) || key->cert == NULL || |
| 1333 | key->cert->type != SSH2_CERT_TYPE_USER) |
| 1334 | continue; |
| 1335 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1336 | id->agent_fd = -1; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1337 | id->key = key; |
| 1338 | id->filename = xstrdup(options.certificate_files[i]); |
| 1339 | id->userprovided = options.certificate_file_userprovided[i]; |
| 1340 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1341 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1342 | /* list of keys supported by the agent */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1343 | if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { |
| 1344 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1345 | debug("%s: ssh_get_authentication_socket: %s", |
| 1346 | __func__, ssh_err(r)); |
| 1347 | } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) { |
| 1348 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 1349 | debug("%s: ssh_fetch_identitylist: %s", |
| 1350 | __func__, ssh_err(r)); |
| 1351 | } else { |
| 1352 | for (j = 0; j < idlist->nkeys; j++) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1353 | found = 0; |
| 1354 | TAILQ_FOREACH(id, &files, next) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1355 | /* |
| 1356 | * agent keys from the config file are |
| 1357 | * preferred |
| 1358 | */ |
| 1359 | if (sshkey_equal(idlist->keys[j], id->key)) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1360 | TAILQ_REMOVE(&files, id, next); |
| 1361 | TAILQ_INSERT_TAIL(preferred, id, next); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1362 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1363 | found = 1; |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1367 | if (!found && !options.identities_only) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1368 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1369 | /* XXX "steals" key/comment from idlist */ |
| 1370 | id->key = idlist->keys[j]; |
| 1371 | id->filename = idlist->comments[j]; |
| 1372 | idlist->keys[j] = NULL; |
| 1373 | idlist->comments[j] = NULL; |
| 1374 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1375 | TAILQ_INSERT_TAIL(&agent, id, next); |
| 1376 | } |
| 1377 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1378 | ssh_free_identitylist(idlist); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1379 | /* append remaining agent keys */ |
| 1380 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
| 1381 | TAILQ_REMOVE(&agent, id, next); |
| 1382 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1383 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1384 | authctxt->agent_fd = agent_fd; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1385 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1386 | /* append remaining keys from the config file */ |
| 1387 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
| 1388 | TAILQ_REMOVE(&files, id, next); |
| 1389 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1390 | } |
djm@openbsd.org | e679c09 | 2015-10-13 16:15:21 +0000 | [diff] [blame] | 1391 | /* finally, filter by PubkeyAcceptedKeyTypes */ |
| 1392 | TAILQ_FOREACH_SAFE(id, preferred, next, id2) { |
| 1393 | if (id->key != NULL && |
| 1394 | match_pattern_list(sshkey_ssh_name(id->key), |
| 1395 | options.pubkey_key_types, 0) != 1) { |
| 1396 | debug("Skipping %s key %s - " |
| 1397 | "not in PubkeyAcceptedKeyTypes", |
| 1398 | sshkey_ssh_name(id->key), id->filename); |
| 1399 | TAILQ_REMOVE(preferred, id, next); |
| 1400 | sshkey_free(id->key); |
| 1401 | free(id->filename); |
| 1402 | memset(id, 0, sizeof(*id)); |
| 1403 | continue; |
| 1404 | } |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1405 | debug2("key: %s (%p)%s%s", id->filename, id->key, |
| 1406 | id->userprovided ? ", explicit" : "", |
| 1407 | id->agent_fd != -1 ? ", agent" : ""); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | static void |
| 1412 | pubkey_cleanup(Authctxt *authctxt) |
| 1413 | { |
| 1414 | Identity *id; |
| 1415 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1416 | if (authctxt->agent_fd != -1) |
| 1417 | ssh_close_authentication_socket(authctxt->agent_fd); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1418 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
| 1419 | id = TAILQ_FIRST(&authctxt->keys)) { |
| 1420 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1421 | if (id->key) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1422 | sshkey_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1423 | free(id->filename); |
| 1424 | free(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1425 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1426 | } |
| 1427 | |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1428 | static int |
| 1429 | try_identity(Identity *id) |
| 1430 | { |
| 1431 | if (!id->key) |
| 1432 | return (0); |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1433 | if (key_type_plain(id->key->type) == KEY_RSA && |
| 1434 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 1435 | debug("Skipped %s key %s for RSA/MD5 server", |
| 1436 | key_type(id->key), id->filename); |
| 1437 | return (0); |
| 1438 | } |
| 1439 | return (id->key->type != KEY_RSA1); |
| 1440 | } |
| 1441 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1442 | int |
| 1443 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1444 | { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1445 | Identity *id; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1446 | int sent = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1447 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1448 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
| 1449 | if (id->tried++) |
| 1450 | return (0); |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 1451 | /* move key to the end of the queue */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1452 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1453 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); |
| 1454 | /* |
| 1455 | * send a test message if we have the public key. for |
| 1456 | * encrypted keys we cannot do this and have to load the |
| 1457 | * private key instead |
| 1458 | */ |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1459 | if (id->key != NULL) { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1460 | if (try_identity(id)) { |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1461 | debug("Offering %s public key: %s", |
| 1462 | key_type(id->key), id->filename); |
| 1463 | sent = send_pubkey_test(authctxt, id); |
| 1464 | } |
| 1465 | } else { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1466 | debug("Trying private key: %s", id->filename); |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1467 | id->key = load_identity_file(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1468 | if (id->key != NULL) { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1469 | if (try_identity(id)) { |
| 1470 | id->isprivate = 1; |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1471 | sent = sign_and_send_pubkey( |
| 1472 | authctxt, id); |
| 1473 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1474 | key_free(id->key); |
| 1475 | id->key = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1476 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1477 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1478 | if (sent) |
| 1479 | return (sent); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1480 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1481 | return (0); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1482 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1483 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1484 | /* |
| 1485 | * Send userauth request message specifying keyboard-interactive method. |
| 1486 | */ |
| 1487 | int |
| 1488 | userauth_kbdint(Authctxt *authctxt) |
| 1489 | { |
| 1490 | static int attempt = 0; |
| 1491 | |
| 1492 | if (attempt++ >= options.number_of_password_prompts) |
| 1493 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1494 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 1495 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 1496 | debug3("userauth_kbdint: disable: no info_req_seen"); |
| 1497 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
| 1498 | return 0; |
| 1499 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1500 | |
| 1501 | debug2("userauth_kbdint"); |
| 1502 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1503 | packet_put_cstring(authctxt->server_user); |
| 1504 | packet_put_cstring(authctxt->service); |
| 1505 | packet_put_cstring(authctxt->method->name); |
| 1506 | packet_put_cstring(""); /* lang */ |
| 1507 | packet_put_cstring(options.kbd_interactive_devices ? |
| 1508 | options.kbd_interactive_devices : ""); |
| 1509 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1510 | |
| 1511 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 1512 | return 1; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1516 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1517 | */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1518 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1519 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1520 | { |
| 1521 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1522 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1523 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1524 | int echo = 0; |
| 1525 | |
| 1526 | debug2("input_userauth_info_req"); |
| 1527 | |
| 1528 | if (authctxt == NULL) |
| 1529 | fatal("input_userauth_info_req: no authentication context"); |
| 1530 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1531 | authctxt->info_req_seen = 1; |
| 1532 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1533 | name = packet_get_string(NULL); |
| 1534 | inst = packet_get_string(NULL); |
| 1535 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1536 | if (strlen(name) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1537 | logit("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1538 | if (strlen(inst) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1539 | logit("%s", inst); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1540 | free(name); |
| 1541 | free(inst); |
| 1542 | free(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1543 | |
| 1544 | num_prompts = packet_get_int(); |
| 1545 | /* |
| 1546 | * Begin to build info response packet based on prompts requested. |
| 1547 | * We commit to providing the correct number of responses, so if |
| 1548 | * further on we run into a problem that prevents this, we have to |
| 1549 | * be sure and clean this up and send a correct error response. |
| 1550 | */ |
| 1551 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 1552 | packet_put_int(num_prompts); |
| 1553 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1554 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1555 | for (i = 0; i < num_prompts; i++) { |
| 1556 | prompt = packet_get_string(NULL); |
| 1557 | echo = packet_get_char(); |
| 1558 | |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1559 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1560 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1561 | packet_put_cstring(response); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1562 | explicit_bzero(response, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1563 | free(response); |
| 1564 | free(prompt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1565 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1566 | packet_check_eom(); /* done with parsing incoming message. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1567 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1568 | packet_add_padding(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1569 | packet_send(); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1570 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1571 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1572 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1573 | static int |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1574 | ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp, |
| 1575 | const u_char *data, size_t datalen) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1576 | { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1577 | struct sshbuf *b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1578 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1579 | pid_t pid; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1580 | int i, r, to[2], from[2], status, sock = packet_get_connection_in(); |
| 1581 | u_char rversion = 0, version = 2; |
| 1582 | void (*osigchld)(int); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1583 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1584 | *sigp = NULL; |
| 1585 | *lenp = 0; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1586 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1587 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1588 | error("%s: not installed: %s", __func__, strerror(errno)); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1589 | return -1; |
| 1590 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1591 | if (fflush(stdout) != 0) { |
| 1592 | error("%s: fflush: %s", __func__, strerror(errno)); |
| 1593 | return -1; |
| 1594 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1595 | if (pipe(to) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1596 | error("%s: pipe: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1597 | return -1; |
| 1598 | } |
| 1599 | if (pipe(from) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1600 | error("%s: pipe: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1601 | return -1; |
| 1602 | } |
| 1603 | if ((pid = fork()) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1604 | error("%s: fork: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1605 | return -1; |
| 1606 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1607 | osigchld = signal(SIGCHLD, SIG_DFL); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1608 | if (pid == 0) { |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 1609 | /* keep the socket on exec */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1610 | fcntl(sock, F_SETFD, 0); |
Damien Miller | 2e5fe88 | 2006-06-13 13:10:00 +1000 | [diff] [blame] | 1611 | permanently_drop_suid(getuid()); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1612 | close(from[0]); |
| 1613 | if (dup2(from[1], STDOUT_FILENO) < 0) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1614 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1615 | close(to[1]); |
| 1616 | if (dup2(to[0], STDIN_FILENO) < 0) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1617 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1618 | close(from[1]); |
| 1619 | close(to[0]); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1620 | /* Close everything but stdio and the socket */ |
| 1621 | for (i = STDERR_FILENO + 1; i < sock; i++) |
| 1622 | close(i); |
| 1623 | closefrom(sock + 1); |
| 1624 | debug3("%s: [child] pid=%ld, exec %s", |
| 1625 | __func__, (long)getpid(), _PATH_SSH_KEY_SIGN); |
mmcc@openbsd.org | 94141b7 | 2015-12-11 00:20:04 +0000 | [diff] [blame] | 1626 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1627 | fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1628 | strerror(errno)); |
| 1629 | } |
| 1630 | close(from[1]); |
| 1631 | close(to[0]); |
| 1632 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1633 | if ((b = sshbuf_new()) == NULL) |
| 1634 | fatal("%s: sshbuf_new failed", __func__); |
| 1635 | /* send # of sock, data to be signed */ |
| 1636 | if ((r = sshbuf_put_u32(b, sock) != 0) || |
| 1637 | (r = sshbuf_put_string(b, data, datalen)) != 0) |
| 1638 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1639 | if (ssh_msg_send(to[1], version, b) == -1) |
| 1640 | fatal("%s: couldn't send request", __func__); |
| 1641 | sshbuf_reset(b); |
| 1642 | r = ssh_msg_recv(from[0], b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1643 | close(from[0]); |
| 1644 | close(to[1]); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1645 | if (r < 0) { |
| 1646 | error("%s: no reply", __func__); |
| 1647 | goto fail; |
| 1648 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1649 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1650 | errno = 0; |
| 1651 | while (waitpid(pid, &status, 0) < 0) { |
| 1652 | if (errno != EINTR) { |
| 1653 | error("%s: waitpid %ld: %s", |
| 1654 | __func__, (long)pid, strerror(errno)); |
| 1655 | goto fail; |
| 1656 | } |
| 1657 | } |
| 1658 | if (!WIFEXITED(status)) { |
| 1659 | error("%s: exited abnormally", __func__); |
| 1660 | goto fail; |
| 1661 | } |
| 1662 | if (WEXITSTATUS(status) != 0) { |
| 1663 | error("%s: exited with status %d", |
| 1664 | __func__, WEXITSTATUS(status)); |
| 1665 | goto fail; |
| 1666 | } |
| 1667 | if ((r = sshbuf_get_u8(b, &rversion)) != 0) { |
| 1668 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1669 | goto fail; |
| 1670 | } |
| 1671 | if (rversion != version) { |
| 1672 | error("%s: bad version", __func__); |
| 1673 | goto fail; |
| 1674 | } |
| 1675 | if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { |
| 1676 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1677 | fail: |
| 1678 | signal(SIGCHLD, osigchld); |
| 1679 | sshbuf_free(b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1680 | return -1; |
| 1681 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1682 | signal(SIGCHLD, osigchld); |
| 1683 | sshbuf_free(b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1684 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1685 | return 0; |
| 1686 | } |
| 1687 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1688 | int |
| 1689 | userauth_hostbased(Authctxt *authctxt) |
| 1690 | { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1691 | struct ssh *ssh = active_state; |
| 1692 | struct sshkey *private = NULL; |
| 1693 | struct sshbuf *b = NULL; |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1694 | const char *service; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1695 | u_char *sig = NULL, *keyblob = NULL; |
| 1696 | char *fp = NULL, *chost = NULL, *lname = NULL; |
| 1697 | size_t siglen = 0, keylen = 0; |
| 1698 | int i, r, success = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1699 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1700 | if (authctxt->ktypes == NULL) { |
| 1701 | authctxt->oktypes = xstrdup(options.hostbased_key_types); |
| 1702 | authctxt->ktypes = authctxt->oktypes; |
| 1703 | } |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 1704 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | * Work through each listed type pattern in HostbasedKeyTypes, |
| 1707 | * trying each hostkey that matches the type in turn. |
| 1708 | */ |
| 1709 | for (;;) { |
| 1710 | if (authctxt->active_ktype == NULL) |
| 1711 | authctxt->active_ktype = strsep(&authctxt->ktypes, ","); |
| 1712 | if (authctxt->active_ktype == NULL || |
| 1713 | *authctxt->active_ktype == '\0') |
| 1714 | break; |
| 1715 | debug3("%s: trying key type %s", __func__, |
| 1716 | authctxt->active_ktype); |
| 1717 | |
| 1718 | /* check for a useful key */ |
| 1719 | private = NULL; |
| 1720 | for (i = 0; i < authctxt->sensitive->nkeys; i++) { |
| 1721 | if (authctxt->sensitive->keys[i] == NULL || |
| 1722 | authctxt->sensitive->keys[i]->type == KEY_RSA1 || |
| 1723 | authctxt->sensitive->keys[i]->type == KEY_UNSPEC) |
| 1724 | continue; |
| 1725 | if (match_pattern_list( |
| 1726 | sshkey_ssh_name(authctxt->sensitive->keys[i]), |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 1727 | authctxt->active_ktype, 0) != 1) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1728 | continue; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1729 | /* we take and free the key */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1730 | private = authctxt->sensitive->keys[i]; |
| 1731 | authctxt->sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1734 | /* Found one */ |
| 1735 | if (private != NULL) |
| 1736 | break; |
| 1737 | /* No more keys of this type; advance */ |
| 1738 | authctxt->active_ktype = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1739 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1740 | if (private == NULL) { |
| 1741 | free(authctxt->oktypes); |
| 1742 | authctxt->oktypes = authctxt->ktypes = NULL; |
| 1743 | authctxt->active_ktype = NULL; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1744 | debug("No more client hostkeys for hostbased authentication."); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1745 | goto out; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1746 | } |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1747 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1748 | if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, |
| 1749 | SSH_FP_DEFAULT)) == NULL) { |
| 1750 | error("%s: sshkey_fingerprint failed", __func__); |
| 1751 | goto out; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1752 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1753 | debug("%s: trying hostkey %s %s", |
| 1754 | __func__, sshkey_ssh_name(private), fp); |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1755 | |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1756 | /* figure out a name for the client host */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1757 | if ((lname = get_local_name(packet_get_connection_in())) == NULL) { |
| 1758 | error("%s: cannot get local ipaddr/name", __func__); |
| 1759 | goto out; |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1760 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1761 | |
| 1762 | /* XXX sshbuf_put_stringf? */ |
| 1763 | xasprintf(&chost, "%s.", lname); |
| 1764 | debug2("%s: chost %s", __func__, chost); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1765 | |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1766 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 1767 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1768 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1769 | /* construct data */ |
| 1770 | if ((b = sshbuf_new()) == NULL) { |
| 1771 | error("%s: sshbuf_new failed", __func__); |
| 1772 | goto out; |
| 1773 | } |
| 1774 | if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { |
| 1775 | error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); |
| 1776 | goto out; |
| 1777 | } |
| 1778 | if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || |
| 1779 | (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1780 | (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || |
| 1781 | (r = sshbuf_put_cstring(b, service)) != 0 || |
| 1782 | (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || |
| 1783 | (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 || |
| 1784 | (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || |
| 1785 | (r = sshbuf_put_cstring(b, chost)) != 0 || |
| 1786 | (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { |
| 1787 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1788 | goto out; |
| 1789 | } |
| 1790 | |
| 1791 | #ifdef DEBUG_PK |
| 1792 | sshbuf_dump(b, stderr); |
| 1793 | #endif |
| 1794 | if (authctxt->sensitive->external_keysign) |
| 1795 | r = ssh_keysign(private, &sig, &siglen, |
| 1796 | sshbuf_ptr(b), sshbuf_len(b)); |
| 1797 | else if ((r = sshkey_sign(private, &sig, &siglen, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 1798 | sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1799 | debug("%s: sshkey_sign: %s", __func__, ssh_err(r)); |
| 1800 | if (r != 0) { |
| 1801 | error("sign using hostkey %s %s failed", |
| 1802 | sshkey_ssh_name(private), fp); |
| 1803 | goto out; |
| 1804 | } |
| 1805 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1806 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 1807 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 1808 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 1809 | (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 || |
| 1810 | (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || |
| 1811 | (r = sshpkt_put_cstring(ssh, chost)) != 0 || |
| 1812 | (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || |
| 1813 | (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || |
| 1814 | (r = sshpkt_send(ssh)) != 0) { |
| 1815 | error("%s: packet error: %s", __func__, ssh_err(r)); |
| 1816 | goto out; |
| 1817 | } |
| 1818 | success = 1; |
| 1819 | |
| 1820 | out: |
| 1821 | if (sig != NULL) { |
| 1822 | explicit_bzero(sig, siglen); |
| 1823 | free(sig); |
| 1824 | } |
| 1825 | free(keyblob); |
| 1826 | free(lname); |
| 1827 | free(fp); |
| 1828 | free(chost); |
| 1829 | sshkey_free(private); |
| 1830 | sshbuf_free(b); |
| 1831 | |
| 1832 | return success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1835 | /* find auth method */ |
| 1836 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1837 | /* |
| 1838 | * given auth method name, if configurable options permit this method fill |
| 1839 | * in auth_ident field and return true, otherwise return false. |
| 1840 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1841 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1842 | authmethod_is_enabled(Authmethod *method) |
| 1843 | { |
| 1844 | if (method == NULL) |
| 1845 | return 0; |
| 1846 | /* return false if options indicate this method is disabled */ |
| 1847 | if (method->enabled == NULL || *method->enabled == 0) |
| 1848 | return 0; |
| 1849 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1850 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1851 | return 0; |
| 1852 | return 1; |
| 1853 | } |
| 1854 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1855 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1856 | authmethod_lookup(const char *name) |
| 1857 | { |
| 1858 | Authmethod *method = NULL; |
| 1859 | if (name != NULL) |
| 1860 | for (method = authmethods; method->name != NULL; method++) |
| 1861 | if (strcmp(name, method->name) == 0) |
| 1862 | return method; |
| 1863 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1867 | /* XXX internal state */ |
| 1868 | static Authmethod *current = NULL; |
| 1869 | static char *supported = NULL; |
| 1870 | static char *preferred = NULL; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1871 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1872 | /* |
| 1873 | * Given the authentication method list sent by the server, return the |
| 1874 | * next method we should try. If the server initially sends a nil list, |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 1875 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1876 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1877 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1878 | authmethod_get(char *authlist) |
| 1879 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1880 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1881 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1882 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1883 | /* Use a suitable default if we're passed a nil list. */ |
| 1884 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1885 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1886 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1887 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1888 | debug3("start over, passed a different list %s", authlist); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1889 | free(supported); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1890 | supported = xstrdup(authlist); |
| 1891 | preferred = options.preferred_authentications; |
| 1892 | debug3("preferred %s", preferred); |
| 1893 | current = NULL; |
| 1894 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1895 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1896 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1897 | for (;;) { |
| 1898 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1899 | debug("No more authentication methods to try."); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1900 | current = NULL; |
| 1901 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1902 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1903 | preferred += next; |
| 1904 | debug3("authmethod_lookup %s", name); |
| 1905 | debug3("remaining preferred: %s", preferred); |
| 1906 | if ((current = authmethod_lookup(name)) != NULL && |
| 1907 | authmethod_is_enabled(current)) { |
| 1908 | debug3("authmethod_is_enabled %s", name); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1909 | debug("Next authentication method: %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1910 | free(name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1911 | return current; |
| 1912 | } |
Darren Tucker | e52a260 | 2013-06-06 08:22:05 +1000 | [diff] [blame] | 1913 | free(name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1914 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1915 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1916 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 1917 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1918 | authmethods_get(void) |
| 1919 | { |
| 1920 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1921 | Buffer b; |
| 1922 | char *list; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1923 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1924 | buffer_init(&b); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1925 | for (method = authmethods; method->name != NULL; method++) { |
| 1926 | if (authmethod_is_enabled(method)) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1927 | if (buffer_len(&b) > 0) |
| 1928 | buffer_append(&b, ",", 1); |
| 1929 | buffer_append(&b, method->name, strlen(method->name)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1930 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1931 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1932 | buffer_append(&b, "\0", 1); |
| 1933 | list = xstrdup(buffer_ptr(&b)); |
| 1934 | buffer_free(&b); |
| 1935 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1936 | } |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1937 | |