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