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