djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sshconnect2.c,v 1.291 2018/12/27 03:25:25 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" |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 52 | #include "sshbuf.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 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" |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 56 | #include "sshkey.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 |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 158 | ssh_kex2(struct ssh *ssh, 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 }; |
djm@openbsd.org | 312d2f2 | 2018-07-04 13:49:31 +0000 | [diff] [blame] | 161 | char *s, *all_key; |
markus@openbsd.org | 57e783c | 2015-01-20 20:16:21 +0000 | [diff] [blame] | 162 | int r; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 163 | |
| 164 | xxx_host = host; |
| 165 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 166 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 167 | if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL) |
| 168 | fatal("%s: kex_names_cat", __func__); |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 169 | myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s); |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 170 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 171 | compat_cipher_proposal(options.ciphers); |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 172 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 173 | compat_cipher_proposal(options.ciphers); |
dtucker@openbsd.org | 8c02e36 | 2016-05-24 04:43:45 +0000 | [diff] [blame] | 174 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
| 175 | myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ? |
sf@openbsd.org | 168b46f | 2018-07-09 13:37:10 +0000 | [diff] [blame] | 176 | "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib"; |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 177 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 178 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 179 | if (options.hostkeyalgorithms != NULL) { |
djm@openbsd.org | 312d2f2 | 2018-07-04 13:49:31 +0000 | [diff] [blame] | 180 | all_key = sshkey_alg_list(0, 0, 1, ','); |
| 181 | if (kex_assemble_names(&options.hostkeyalgorithms, |
| 182 | KEX_DEFAULT_PK_ALG, all_key) != 0) |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 183 | fatal("%s: kex_assemble_namelist", __func__); |
djm@openbsd.org | 312d2f2 | 2018-07-04 13:49:31 +0000 | [diff] [blame] | 184 | free(all_key); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 185 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 186 | compat_pkalg_proposal(options.hostkeyalgorithms); |
djm@openbsd.org | f9eca24 | 2015-07-30 00:01:34 +0000 | [diff] [blame] | 187 | } else { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 188 | /* Enforce default */ |
| 189 | options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 190 | /* Prefer algorithms that we already have keys for */ |
| 191 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 192 | compat_pkalg_proposal( |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 193 | order_hostkeyalgs(host, hostaddr, port)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 194 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 195 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 196 | if (options.rekey_limit || options.rekey_interval) |
dtucker@openbsd.org | c998bf0 | 2017-02-03 02:56:00 +0000 | [diff] [blame] | 197 | packet_set_rekey_limits(options.rekey_limit, |
| 198 | options.rekey_interval); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 199 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 200 | /* start key exchange */ |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 201 | if ((r = kex_setup(ssh, myproposal)) != 0) |
markus@openbsd.org | 57e783c | 2015-01-20 20:16:21 +0000 | [diff] [blame] | 202 | fatal("kex_setup: %s", ssh_err(r)); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 203 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 204 | ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
| 205 | ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
| 206 | ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client; |
| 207 | ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client; |
| 208 | ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client; |
| 209 | ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
| 210 | ssh->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 |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 212 | ssh->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 |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 215 | ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client; |
| 216 | ssh->kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 217 | |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 218 | ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 219 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 220 | /* remove ext-info from the KEX proposals for rekeying */ |
| 221 | myproposal[PROPOSAL_KEX_ALGS] = |
djm@openbsd.org | c3903c3 | 2018-08-13 02:41:05 +0000 | [diff] [blame] | 222 | compat_kex_proposal(options.kex_algorithms); |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 223 | if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 224 | fatal("kex_prop2buf: %s", ssh_err(r)); |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 225 | |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 226 | session_id2 = ssh->kex->session_id; |
| 227 | session_id2_len = ssh->kex->session_id_len; |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 228 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 229 | #ifdef DEBUG_KEXDH |
| 230 | /* send 1st encrypted/maced/compressed message */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 231 | if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 || |
| 232 | (r = sshpkt_put_cstring(ssh, "markus")) != 0 || |
markus@openbsd.org | 394a842 | 2018-07-11 18:55:11 +0000 | [diff] [blame] | 233 | (r = sshpkt_send(ssh)) != 0 || |
| 234 | (r = ssh_packet_write_wait(ssh)) != 0) |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 235 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 236 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 237 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 238 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 239 | /* |
| 240 | * Authenticate user |
| 241 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 242 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 243 | typedef struct cauthctxt Authctxt; |
| 244 | typedef struct cauthmethod Authmethod; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 245 | typedef struct identity Identity; |
| 246 | typedef struct idlist Idlist; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 247 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 248 | struct identity { |
| 249 | TAILQ_ENTRY(identity) next; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 250 | int agent_fd; /* >=0 if agent supports key */ |
| 251 | struct sshkey *key; /* public/private key */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 252 | char *filename; /* comment for agent-only keys */ |
| 253 | int tried; |
| 254 | int isprivate; /* key points to the private key */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 255 | int userprovided; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 256 | }; |
| 257 | TAILQ_HEAD(idlist, identity); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 258 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 259 | struct cauthctxt { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 260 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 261 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 262 | const char *host; |
| 263 | const char *service; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 264 | struct cauthmethod *method; |
Damien Miller | 79442c0 | 2010-05-10 11:55:38 +1000 | [diff] [blame] | 265 | sig_atomic_t success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 266 | char *authlist; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 267 | int attempt; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 268 | /* pubkey */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 269 | struct idlist keys; |
| 270 | int agent_fd; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 271 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 272 | Sensitive *sensitive; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 273 | char *oktypes, *ktypes; |
| 274 | const char *active_ktype; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 275 | /* kbd-interactive */ |
| 276 | int info_req_seen; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 277 | /* generic */ |
| 278 | void *methoddata; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 279 | }; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 280 | |
| 281 | struct cauthmethod { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 282 | char *name; /* string to compare against server's list */ |
| 283 | int (*userauth)(Authctxt *authctxt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 284 | void (*cleanup)(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 285 | int *enabled; /* flag in option struct that enables method */ |
| 286 | int *batch_flag; /* flag in option struct that disables method */ |
| 287 | }; |
| 288 | |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 289 | int input_userauth_service_accept(int, u_int32_t, struct ssh *); |
| 290 | int input_userauth_ext_info(int, u_int32_t, struct ssh *); |
| 291 | int input_userauth_success(int, u_int32_t, struct ssh *); |
| 292 | int input_userauth_success_unexpected(int, u_int32_t, struct ssh *); |
| 293 | int input_userauth_failure(int, u_int32_t, struct ssh *); |
| 294 | int input_userauth_banner(int, u_int32_t, struct ssh *); |
| 295 | int input_userauth_error(int, u_int32_t, struct ssh *); |
| 296 | int input_userauth_info_req(int, u_int32_t, struct ssh *); |
| 297 | int input_userauth_pk_ok(int, u_int32_t, struct ssh *); |
| 298 | int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 299 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 300 | int userauth_none(Authctxt *); |
| 301 | int userauth_pubkey(Authctxt *); |
| 302 | int userauth_passwd(Authctxt *); |
| 303 | int userauth_kbdint(Authctxt *); |
| 304 | int userauth_hostbased(Authctxt *); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 305 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 306 | #ifdef GSSAPI |
| 307 | int userauth_gssapi(Authctxt *authctxt); |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 308 | int input_gssapi_response(int type, u_int32_t, struct ssh *); |
| 309 | int input_gssapi_token(int type, u_int32_t, struct ssh *); |
| 310 | int input_gssapi_hash(int type, u_int32_t, struct ssh *); |
| 311 | int input_gssapi_error(int, u_int32_t, struct ssh *); |
| 312 | int input_gssapi_errtok(int, u_int32_t, struct ssh *); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 313 | #endif |
| 314 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 315 | void userauth(Authctxt *, char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 316 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 317 | static int sign_and_send_pubkey(struct ssh *ssh, Authctxt *, Identity *); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 318 | static void pubkey_prepare(Authctxt *); |
| 319 | static void pubkey_cleanup(Authctxt *); |
djm@openbsd.org | b9844a4 | 2016-12-04 23:54:02 +0000 | [diff] [blame] | 320 | static void pubkey_reset(Authctxt *); |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 321 | static struct sshkey *load_identity_file(Identity *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 322 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 323 | static Authmethod *authmethod_get(char *authlist); |
| 324 | static Authmethod *authmethod_lookup(const char *name); |
| 325 | static char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 326 | |
| 327 | Authmethod authmethods[] = { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 328 | #ifdef GSSAPI |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 329 | {"gssapi-with-mic", |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 330 | userauth_gssapi, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 331 | NULL, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 332 | &options.gss_authentication, |
| 333 | NULL}, |
| 334 | #endif |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 335 | {"hostbased", |
| 336 | userauth_hostbased, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 337 | NULL, |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 338 | &options.hostbased_authentication, |
| 339 | NULL}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 340 | {"publickey", |
| 341 | userauth_pubkey, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 342 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 343 | &options.pubkey_authentication, |
| 344 | NULL}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 345 | {"keyboard-interactive", |
| 346 | userauth_kbdint, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 347 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 348 | &options.kbd_interactive_authentication, |
| 349 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 350 | {"password", |
| 351 | userauth_passwd, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 352 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 353 | &options.password_authentication, |
| 354 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 355 | {"none", |
| 356 | userauth_none, |
| 357 | NULL, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 358 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | NULL}, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 360 | {NULL, NULL, NULL, NULL, NULL} |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | void |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 364 | ssh_userauth2(struct ssh *ssh, const char *local_user, |
| 365 | const char *server_user, char *host, Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 366 | { |
| 367 | Authctxt authctxt; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 368 | int r; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 369 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 370 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 371 | options.kbd_interactive_authentication = 1; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 372 | if (options.preferred_authentications == NULL) |
| 373 | options.preferred_authentications = authmethods_get(); |
| 374 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 375 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 376 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 377 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 378 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 379 | authctxt.host = host; |
| 380 | authctxt.service = "ssh-connection"; /* service name */ |
| 381 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 382 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 383 | authctxt.authlist = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 384 | authctxt.methoddata = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 385 | authctxt.sensitive = sensitive; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 386 | authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 387 | authctxt.info_req_seen = 0; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 388 | authctxt.agent_fd = -1; |
djm@openbsd.org | aaed635 | 2018-11-16 02:46:20 +0000 | [diff] [blame] | 389 | pubkey_prepare(&authctxt); |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 390 | if (authctxt.method == NULL) { |
| 391 | fatal("%s: internal error: cannot send userauth none request", |
| 392 | __func__); |
| 393 | } |
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 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 458 | struct ssh *ssh = active_state; /* XXX */ |
| 459 | |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 460 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 461 | authctxt->method->cleanup(authctxt); |
| 462 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 463 | free(authctxt->methoddata); |
| 464 | authctxt->methoddata = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 465 | if (authlist == NULL) { |
| 466 | authlist = authctxt->authlist; |
| 467 | } else { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 468 | free(authctxt->authlist); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 469 | authctxt->authlist = authlist; |
| 470 | } |
| 471 | for (;;) { |
| 472 | Authmethod *method = authmethod_get(authlist); |
| 473 | if (method == NULL) |
dtucker@openbsd.org | 4626e39 | 2017-06-14 00:31:38 +0000 | [diff] [blame] | 474 | fatal("%s@%s: Permission denied (%s).", |
| 475 | authctxt->server_user, authctxt->host, authlist); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 476 | authctxt->method = method; |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 477 | |
| 478 | /* reset the per method handler */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 479 | ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN, |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 480 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); |
| 481 | |
| 482 | /* and try new method */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 483 | if (method->userauth(authctxt) != 0) { |
| 484 | debug2("we sent a %s packet, wait for reply", method->name); |
| 485 | break; |
| 486 | } else { |
| 487 | debug2("we did not send a packet, disable method"); |
| 488 | method->enabled = NULL; |
| 489 | } |
| 490 | } |
| 491 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 492 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 493 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 494 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 495 | input_userauth_error(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 496 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 497 | fatal("input_userauth_error: bad message during authentication: " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 498 | "type %d", type); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 499 | return 0; |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 500 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 501 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 502 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 503 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 504 | input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 505 | { |
djm@openbsd.org | 65c6c6b | 2016-07-17 04:20:16 +0000 | [diff] [blame] | 506 | char *msg, *lang; |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 507 | u_int len; |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 508 | |
djm@openbsd.org | 65c6c6b | 2016-07-17 04:20:16 +0000 | [diff] [blame] | 509 | debug3("%s", __func__); |
| 510 | msg = packet_get_string(&len); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 511 | lang = packet_get_string(NULL); |
djm@openbsd.org | 65c6c6b | 2016-07-17 04:20:16 +0000 | [diff] [blame] | 512 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) |
| 513 | fmprintf(stderr, "%s", msg); |
| 514 | free(msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 515 | free(lang); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 516 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 517 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 518 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 519 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 520 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 521 | input_userauth_success(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 522 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 523 | Authctxt *authctxt = ssh->authctxt; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 524 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 525 | if (authctxt == NULL) |
| 526 | fatal("input_userauth_success: no authentication context"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 527 | free(authctxt->authlist); |
| 528 | authctxt->authlist = NULL; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 529 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 530 | authctxt->method->cleanup(authctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 531 | free(authctxt->methoddata); |
| 532 | authctxt->methoddata = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 533 | authctxt->success = 1; /* break out */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 534 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 535 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 536 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 537 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 538 | 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] | 539 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 540 | Authctxt *authctxt = ssh->authctxt; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 541 | |
| 542 | if (authctxt == NULL) |
| 543 | fatal("%s: no authentication context", __func__); |
| 544 | |
| 545 | fatal("Unexpected authentication success during %s.", |
| 546 | authctxt->method->name); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 547 | return 0; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 548 | } |
| 549 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 550 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 551 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 552 | input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 553 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 554 | Authctxt *authctxt = ssh->authctxt; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 555 | char *authlist = NULL; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 556 | u_char partial; |
| 557 | int r; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 558 | |
| 559 | if (authctxt == NULL) |
| 560 | fatal("input_userauth_failure: no authentication context"); |
| 561 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 562 | if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 || |
| 563 | (r = sshpkt_get_u8(ssh, &partial)) != 0 || |
| 564 | (r = sshpkt_get_end(ssh)) != 0) |
| 565 | goto out; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 566 | |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 567 | if (partial != 0) { |
dtucker@openbsd.org | 83cbca6 | 2016-07-22 05:46:11 +0000 | [diff] [blame] | 568 | verbose("Authenticated with partial success."); |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 569 | /* reset state */ |
djm@openbsd.org | b9844a4 | 2016-12-04 23:54:02 +0000 | [diff] [blame] | 570 | pubkey_reset(authctxt); |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 571 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 572 | debug("Authentications that can continue: %s", authlist); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 573 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 574 | userauth(authctxt, authlist); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 575 | authlist = NULL; |
| 576 | out: |
| 577 | free(authlist); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 578 | return 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 579 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 580 | |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Format an identity for logging including filename, key type, fingerprint |
| 583 | * and location (agent, etc.). Caller must free. |
| 584 | */ |
| 585 | static char * |
| 586 | format_identity(Identity *id) |
| 587 | { |
| 588 | char *fp = NULL, *ret = NULL; |
| 589 | |
| 590 | if (id->key != NULL) { |
| 591 | fp = sshkey_fingerprint(id->key, options.fingerprint_hash, |
| 592 | SSH_FP_DEFAULT); |
| 593 | } |
| 594 | xasprintf(&ret, "%s %s%s%s%s%s%s", |
| 595 | id->filename, |
| 596 | id->key ? sshkey_type(id->key) : "", id->key ? " " : "", |
| 597 | fp ? fp : "", |
| 598 | id->userprovided ? " explicit" : "", |
| 599 | (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "", |
| 600 | id->agent_fd != -1 ? " agent" : ""); |
| 601 | free(fp); |
| 602 | return ret; |
| 603 | } |
| 604 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 605 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 606 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 607 | 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] | 608 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 609 | Authctxt *authctxt = ssh->authctxt; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 610 | struct sshkey *key = NULL; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 611 | Identity *id = NULL; |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 612 | int pktype, found = 0, sent = 0; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 613 | size_t blen; |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 614 | char *pkalg = NULL, *fp = NULL, *ident = NULL; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 615 | u_char *pkblob = NULL; |
| 616 | int r; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 617 | |
| 618 | if (authctxt == NULL) |
| 619 | fatal("input_userauth_pk_ok: no authentication context"); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 620 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 621 | if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || |
| 622 | (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || |
| 623 | (r = sshpkt_get_end(ssh)) != 0) |
| 624 | goto done; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 625 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 626 | if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) { |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 627 | debug("%s: server sent unknown pkalg %s", __func__, pkalg); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 628 | goto done; |
| 629 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 630 | if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 631 | debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 632 | goto done; |
| 633 | } |
| 634 | if (key->type != pktype) { |
| 635 | error("input_userauth_pk_ok: type mismatch " |
| 636 | "for decoded key (received %d, expected %d)", |
| 637 | key->type, pktype); |
| 638 | goto done; |
| 639 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 640 | |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 641 | /* |
| 642 | * search keys in the reverse order, because last candidate has been |
| 643 | * moved to the end of the queue. this also avoids confusion by |
| 644 | * duplicate keys |
| 645 | */ |
Damien Miller | 0b51a52 | 2004-04-20 20:07:19 +1000 | [diff] [blame] | 646 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 647 | if (sshkey_equal(key, id->key)) { |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 648 | found = 1; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | } |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 652 | if (!found || id == NULL) { |
| 653 | fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 654 | SSH_FP_DEFAULT); |
| 655 | error("%s: server replied with unknown key: %s %s", __func__, |
| 656 | sshkey_type(key), fp == NULL ? "<ERROR>" : fp); |
| 657 | goto done; |
| 658 | } |
| 659 | ident = format_identity(id); |
| 660 | debug("Server accepts key: %s", ident); |
| 661 | sent = sign_and_send_pubkey(ssh, authctxt, id); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 662 | r = 0; |
| 663 | done: |
| 664 | sshkey_free(key); |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 665 | free(ident); |
| 666 | free(fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 667 | free(pkalg); |
| 668 | free(pkblob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 669 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 670 | /* try another method if we did not send a packet */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 671 | if (r == 0 && sent == 0) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 672 | userauth(authctxt, NULL); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 673 | return r; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 674 | } |
| 675 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 676 | #ifdef GSSAPI |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 677 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 678 | userauth_gssapi(Authctxt *authctxt) |
| 679 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 680 | struct ssh *ssh = active_state; /* XXX */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 681 | Gssctxt *gssctxt = NULL; |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 682 | static gss_OID_set gss_supported = NULL; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 683 | static u_int mech = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 684 | OM_uint32 min; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 685 | int r, ok = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 686 | |
| 687 | /* Try one GSSAPI method at a time, rather than sending them all at |
| 688 | * once. */ |
| 689 | |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 690 | if (gss_supported == NULL) |
| 691 | gss_indicate_mechs(&min, &gss_supported); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 692 | |
| 693 | /* Check to see if the mechanism is usable before we offer it */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 694 | while (mech < gss_supported->count && !ok) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 695 | /* My DER encoding requires length<128 */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 696 | if (gss_supported->elements[mech].length < 128 && |
djm@openbsd.org | 84623e0 | 2018-06-26 02:02:36 +0000 | [diff] [blame] | 697 | ssh_gssapi_check_mechanism(&gssctxt, |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 698 | &gss_supported->elements[mech], authctxt->host)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 699 | ok = 1; /* Mechanism works */ |
| 700 | } else { |
| 701 | mech++; |
| 702 | } |
| 703 | } |
| 704 | |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 705 | if (!ok) |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 706 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 707 | |
| 708 | authctxt->methoddata=(void *)gssctxt; |
| 709 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 710 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 711 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 712 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 713 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 714 | (r = sshpkt_put_u32(ssh, 1)) != 0 || |
| 715 | (r = sshpkt_put_u32(ssh, |
| 716 | (gss_supported->elements[mech].length) + 2)) != 0 || |
| 717 | (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 || |
| 718 | (r = sshpkt_put_u8(ssh, |
| 719 | gss_supported->elements[mech].length)) != 0 || |
| 720 | (r = sshpkt_put(ssh, |
| 721 | gss_supported->elements[mech].elements, |
| 722 | gss_supported->elements[mech].length)) != 0 || |
| 723 | (r = sshpkt_send(ssh)) != 0) |
| 724 | fatal("%s: %s", __func__, ssh_err(r)); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 725 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 726 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); |
| 727 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); |
| 728 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); |
| 729 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 730 | |
| 731 | mech++; /* Move along to next candidate */ |
| 732 | |
| 733 | return 1; |
| 734 | } |
| 735 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 736 | static OM_uint32 |
djm@openbsd.org | 39896b7 | 2017-05-31 05:08:46 +0000 | [diff] [blame] | 737 | process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok) |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 738 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 739 | Authctxt *authctxt = ssh->authctxt; |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 740 | Gssctxt *gssctxt = authctxt->methoddata; |
| 741 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
Damien Miller | da9984f | 2005-08-31 19:46:26 +1000 | [diff] [blame] | 742 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
| 743 | gss_buffer_desc gssbuf; |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 744 | OM_uint32 status, ms, flags; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 745 | int r; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 746 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 747 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 748 | recv_tok, &send_tok, &flags); |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 749 | |
| 750 | if (send_tok.length > 0) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 751 | u_char type = GSS_ERROR(status) ? |
| 752 | SSH2_MSG_USERAUTH_GSSAPI_ERRTOK : |
| 753 | SSH2_MSG_USERAUTH_GSSAPI_TOKEN; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 754 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 755 | if ((r = sshpkt_start(ssh, type)) != 0 || |
| 756 | (r = sshpkt_put_string(ssh, send_tok.value, |
| 757 | send_tok.length)) != 0 || |
| 758 | (r = sshpkt_send(ssh)) != 0) |
| 759 | fatal("%s: %s", __func__, ssh_err(r)); |
| 760 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 761 | gss_release_buffer(&ms, &send_tok); |
| 762 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 763 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 764 | if (status == GSS_S_COMPLETE) { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 765 | /* send either complete or MIC, depending on mechanism */ |
| 766 | if (!(flags & GSS_C_INTEG_FLAG)) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 767 | if ((r = sshpkt_start(ssh, |
| 768 | SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 || |
| 769 | (r = sshpkt_send(ssh)) != 0) |
| 770 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 771 | } else { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 772 | struct sshbuf *b; |
| 773 | |
| 774 | if ((b = sshbuf_new()) == NULL) |
| 775 | fatal("%s: sshbuf_new failed", __func__); |
| 776 | ssh_gssapi_buildmic(b, authctxt->server_user, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 777 | authctxt->service, "gssapi-with-mic"); |
| 778 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 779 | if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL) |
| 780 | fatal("%s: sshbuf_mutable_ptr failed", __func__); |
| 781 | gssbuf.length = sshbuf_len(b); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 782 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 783 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 784 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 785 | if (!GSS_ERROR(status)) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 786 | if ((r = sshpkt_start(ssh, |
| 787 | SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 || |
| 788 | (r = sshpkt_put_string(ssh, mic.value, |
| 789 | mic.length)) != 0 || |
| 790 | (r = sshpkt_send(ssh)) != 0) |
| 791 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 792 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 793 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 794 | sshbuf_free(b); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 795 | gss_release_buffer(&ms, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 796 | } |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 797 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 798 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 799 | return status; |
| 800 | } |
| 801 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 802 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 803 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 804 | input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 805 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 806 | Authctxt *authctxt = ssh->authctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 807 | Gssctxt *gssctxt; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 808 | size_t oidlen; |
| 809 | u_char *oidv = NULL; |
| 810 | int r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 811 | |
| 812 | if (authctxt == NULL) |
| 813 | fatal("input_gssapi_response: no authentication context"); |
| 814 | gssctxt = authctxt->methoddata; |
| 815 | |
| 816 | /* Setup our OID */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 817 | if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0) |
| 818 | goto done; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 819 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 820 | if (oidlen <= 2 || |
| 821 | oidv[0] != SSH_GSS_OIDTYPE || |
| 822 | oidv[1] != oidlen - 2) { |
| 823 | debug("Badly encoded mechanism OID received"); |
| 824 | userauth(authctxt, NULL); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 825 | goto ok; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 826 | } |
| 827 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 828 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
| 829 | fatal("Server returned different OID than expected"); |
| 830 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 831 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 832 | goto done; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 833 | |
djm@openbsd.org | 39896b7 | 2017-05-31 05:08:46 +0000 | [diff] [blame] | 834 | if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 835 | /* Start again with next method on list */ |
| 836 | debug("Trying to start again"); |
| 837 | userauth(authctxt, NULL); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 838 | goto ok; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 839 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 840 | ok: |
| 841 | r = 0; |
| 842 | done: |
| 843 | free(oidv); |
| 844 | return r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 845 | } |
| 846 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 847 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 848 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 849 | input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 850 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 851 | Authctxt *authctxt = ssh->authctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 852 | gss_buffer_desc recv_tok; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 853 | u_char *p = NULL; |
| 854 | size_t len; |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 855 | OM_uint32 status; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 856 | int r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 857 | |
| 858 | if (authctxt == NULL) |
| 859 | fatal("input_gssapi_response: no authentication context"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 860 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 861 | if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || |
| 862 | (r = sshpkt_get_end(ssh)) != 0) |
| 863 | goto out; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 864 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 865 | recv_tok.value = p; |
| 866 | recv_tok.length = len; |
djm@openbsd.org | 39896b7 | 2017-05-31 05:08:46 +0000 | [diff] [blame] | 867 | status = process_gssapi_token(ssh, &recv_tok); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 868 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 869 | /* Start again with the next method in the list */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 870 | if (GSS_ERROR(status)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 871 | userauth(authctxt, NULL); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 872 | /* ok */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 873 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 874 | r = 0; |
| 875 | out: |
| 876 | free(p); |
| 877 | return r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 878 | } |
| 879 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 880 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 881 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 882 | input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 883 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 884 | Authctxt *authctxt = ssh->authctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 885 | Gssctxt *gssctxt; |
| 886 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 887 | gss_buffer_desc recv_tok; |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 888 | OM_uint32 ms; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 889 | u_char *p = NULL; |
| 890 | size_t len; |
| 891 | int r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 892 | |
| 893 | if (authctxt == NULL) |
| 894 | fatal("input_gssapi_response: no authentication context"); |
| 895 | gssctxt = authctxt->methoddata; |
| 896 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 897 | if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || |
| 898 | (r = sshpkt_get_end(ssh)) != 0) { |
| 899 | free(p); |
| 900 | return r; |
| 901 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 902 | |
| 903 | /* Stick it into GSSAPI and see what it says */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 904 | recv_tok.value = p; |
| 905 | recv_tok.length = len; |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 906 | (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 907 | &recv_tok, &send_tok, NULL); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 908 | free(p); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 909 | gss_release_buffer(&ms, &send_tok); |
| 910 | |
| 911 | /* Server will be returning a failed packet after this one */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 912 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 913 | } |
| 914 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 915 | /* ARGSUSED */ |
djm@openbsd.org | 12b5f50 | 2015-01-20 07:56:44 +0000 | [diff] [blame] | 916 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 917 | input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 918 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 919 | char *msg = NULL; |
| 920 | char *lang = NULL; |
| 921 | int r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 922 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 923 | if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */ |
| 924 | (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */ |
| 925 | (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || |
| 926 | (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) |
| 927 | goto out; |
| 928 | r = sshpkt_get_end(ssh); |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 929 | debug("Server GSSAPI Error:\n%s", msg); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 930 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 931 | free(msg); |
| 932 | free(lang); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 933 | return r; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 934 | } |
| 935 | #endif /* GSSAPI */ |
| 936 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 937 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 938 | userauth_none(Authctxt *authctxt) |
| 939 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 940 | struct ssh *ssh = active_state; /* XXX */ |
| 941 | int r; |
| 942 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 943 | /* initial userauth request */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 944 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 945 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 946 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 947 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 948 | (r = sshpkt_send(ssh)) != 0) |
| 949 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 950 | return 1; |
| 951 | } |
| 952 | |
| 953 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 954 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 955 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 956 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 957 | static int attempt = 0; |
djm@openbsd.org | 91b1919 | 2018-11-28 06:00:38 +0000 | [diff] [blame] | 958 | char *password, *prompt = NULL; |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 959 | const char *host = options.host_key_alias ? options.host_key_alias : |
| 960 | authctxt->host; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 961 | int r; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 962 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 963 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 964 | return 0; |
| 965 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 966 | if (attempt != 1) |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 967 | error("Permission denied, please try again."); |
| 968 | |
djm@openbsd.org | 91b1919 | 2018-11-28 06:00:38 +0000 | [diff] [blame] | 969 | xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 970 | password = read_passphrase(prompt, 0); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 971 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 972 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 973 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 974 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 975 | (r = sshpkt_put_u8(ssh, 0)) != 0 || |
| 976 | (r = sshpkt_put_cstring(ssh, password)) != 0 || |
| 977 | (r = sshpkt_add_padding(ssh, 64)) != 0 || |
| 978 | (r = sshpkt_send(ssh)) != 0) |
| 979 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 980 | |
djm@openbsd.org | 91b1919 | 2018-11-28 06:00:38 +0000 | [diff] [blame] | 981 | free(prompt); |
| 982 | if (password != NULL) |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 983 | freezero(password, strlen(password)); |
| 984 | |
| 985 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 986 | &input_userauth_passwd_changereq); |
| 987 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 988 | return 1; |
| 989 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 990 | |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 991 | /* |
| 992 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
| 993 | */ |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 994 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 995 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 996 | 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] | 997 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 998 | Authctxt *authctxt = ssh->authctxt; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 999 | char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL; |
dtucker@openbsd.org | a54eb27 | 2017-08-27 00:38:41 +0000 | [diff] [blame] | 1000 | char prompt[256]; |
djm@openbsd.org | 3e032a9 | 2017-01-30 00:32:03 +0000 | [diff] [blame] | 1001 | const char *host; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1002 | int r; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1003 | |
| 1004 | debug2("input_userauth_passwd_changereq"); |
| 1005 | |
| 1006 | if (authctxt == NULL) |
| 1007 | fatal("input_userauth_passwd_changereq: " |
| 1008 | "no authentication context"); |
djm@openbsd.org | 3e032a9 | 2017-01-30 00:32:03 +0000 | [diff] [blame] | 1009 | host = options.host_key_alias ? options.host_key_alias : authctxt->host; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1010 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1011 | if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 || |
| 1012 | (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) |
| 1013 | goto out; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1014 | if (strlen(info) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1015 | logit("%s", info); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1016 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1017 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 1018 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 1019 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 1020 | (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */ |
| 1021 | goto out; |
| 1022 | |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 1023 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1024 | "Enter %.30s@%.128s's old password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 1025 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1026 | password = read_passphrase(prompt, 0); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1027 | if ((r = sshpkt_put_cstring(ssh, password)) != 0) |
| 1028 | goto out; |
| 1029 | |
| 1030 | freezero(password, strlen(password)); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1031 | password = NULL; |
| 1032 | while (password == NULL) { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 1033 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1034 | "Enter %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 1035 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1036 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
| 1037 | if (password == NULL) { |
| 1038 | /* bail out */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1039 | r = 0; |
| 1040 | goto out; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1041 | } |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 1042 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1043 | "Retype %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 1044 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1045 | retype = read_passphrase(prompt, 0); |
| 1046 | if (strcmp(password, retype) != 0) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1047 | freezero(password, strlen(password)); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1048 | logit("Mismatch; try again, EOF to quit."); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1049 | password = NULL; |
| 1050 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1051 | freezero(retype, strlen(retype)); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1052 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1053 | if ((r = sshpkt_put_cstring(ssh, password)) != 0 || |
| 1054 | (r = sshpkt_add_padding(ssh, 64)) != 0 || |
| 1055 | (r = sshpkt_send(ssh)) != 0) |
| 1056 | goto out; |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 1057 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1058 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1059 | &input_userauth_passwd_changereq); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1060 | r = 0; |
| 1061 | out: |
| 1062 | if (password) |
| 1063 | freezero(password, strlen(password)); |
| 1064 | free(info); |
| 1065 | free(lang); |
| 1066 | return r; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 1067 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1068 | |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1069 | /* |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1070 | * Select an algorithm for publickey signatures. |
| 1071 | * Returns algorithm (caller must free) or NULL if no mutual algorithm found. |
| 1072 | * |
| 1073 | * Call with ssh==NULL to ignore server-sig-algs extension list and |
| 1074 | * only attempt with the key's base signature type. |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1075 | */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1076 | static char * |
| 1077 | key_sig_algorithm(struct ssh *ssh, const struct sshkey *key) |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1078 | { |
djm@openbsd.org | b4d4eda | 2018-07-03 13:20:25 +0000 | [diff] [blame] | 1079 | char *allowed, *oallowed, *cp, *tmp, *alg = NULL; |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1080 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | * The signature algorithm will only differ from the key algorithm |
| 1083 | * for RSA keys/certs and when the server advertises support for |
| 1084 | * newer (SHA2) algorithms. |
| 1085 | */ |
| 1086 | if (ssh == NULL || ssh->kex->server_sig_algs == NULL || |
djm@openbsd.org | 1a4a9cf | 2018-10-11 03:48:04 +0000 | [diff] [blame] | 1087 | (key->type != KEY_RSA && key->type != KEY_RSA_CERT) || |
| 1088 | (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1089 | /* Filter base key signature alg against our configuration */ |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 1090 | return match_list(sshkey_ssh_name(key), |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1091 | options.pubkey_key_types, NULL); |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1092 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* |
| 1095 | * For RSA keys/certs, since these might have a different sig type: |
| 1096 | * find the first entry in PubkeyAcceptedKeyTypes of the right type |
| 1097 | * that also appears in the supported signature algorithms list from |
| 1098 | * the server. |
| 1099 | */ |
| 1100 | oallowed = allowed = xstrdup(options.pubkey_key_types); |
| 1101 | while ((cp = strsep(&allowed, ",")) != NULL) { |
| 1102 | if (sshkey_type_from_name(cp) != key->type) |
| 1103 | continue; |
djm@openbsd.org | b4d4eda | 2018-07-03 13:20:25 +0000 | [diff] [blame] | 1104 | tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL); |
| 1105 | if (tmp != NULL) |
| 1106 | alg = xstrdup(cp); |
| 1107 | free(tmp); |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1108 | if (alg != NULL) |
| 1109 | break; |
| 1110 | } |
| 1111 | free(oallowed); |
| 1112 | return alg; |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1115 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1116 | identity_sign(struct identity *id, u_char **sigp, size_t *lenp, |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1117 | const u_char *data, size_t datalen, u_int compat, const char *alg) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1118 | { |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1119 | struct sshkey *prv; |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1120 | int r; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1121 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1122 | /* The agent supports this key. */ |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1123 | if (id->key != NULL && id->agent_fd != -1) { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1124 | return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, |
| 1125 | data, datalen, alg, compat); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * We have already loaded the private key or the private key is |
| 1130 | * stored in external hardware. |
| 1131 | */ |
| 1132 | if (id->key != NULL && |
| 1133 | (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) { |
| 1134 | if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen, |
| 1135 | alg, compat)) != 0) |
| 1136 | return r; |
| 1137 | /* |
| 1138 | * PKCS#11 tokens may not support all signature algorithms, |
| 1139 | * so check what we get back. |
| 1140 | */ |
| 1141 | if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1142 | return r; |
| 1143 | return 0; |
| 1144 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1145 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1146 | /* Load the private key from the file. */ |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1147 | if ((prv = load_identity_file(id)) == NULL) |
djm@openbsd.org | 0d1451a | 2016-02-23 01:34:14 +0000 | [diff] [blame] | 1148 | return SSH_ERR_KEY_NOT_FOUND; |
djm@openbsd.org | c4972d0 | 2017-08-11 04:47:12 +0000 | [diff] [blame] | 1149 | if (id->key != NULL && !sshkey_equal_public(prv, id->key)) { |
| 1150 | error("%s: private key %s contents do not match public", |
| 1151 | __func__, id->filename); |
| 1152 | return SSH_ERR_KEY_NOT_FOUND; |
| 1153 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1154 | r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1155 | sshkey_free(prv); |
djm@openbsd.org | 2607438 | 2018-03-03 03:01:50 +0000 | [diff] [blame] | 1156 | return r; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | static int |
djm@openbsd.org | 1e24552 | 2017-03-11 23:40:26 +0000 | [diff] [blame] | 1160 | id_filename_matches(Identity *id, Identity *private_id) |
| 1161 | { |
| 1162 | const char *suffixes[] = { ".pub", "-cert.pub", NULL }; |
| 1163 | size_t len = strlen(id->filename), plen = strlen(private_id->filename); |
| 1164 | size_t i, slen; |
| 1165 | |
| 1166 | if (strcmp(id->filename, private_id->filename) == 0) |
| 1167 | return 1; |
| 1168 | for (i = 0; suffixes[i]; i++) { |
| 1169 | slen = strlen(suffixes[i]); |
| 1170 | if (len > slen && plen == len - slen && |
| 1171 | strcmp(id->filename + (len - slen), suffixes[i]) == 0 && |
| 1172 | memcmp(id->filename, private_id->filename, plen) == 0) |
| 1173 | return 1; |
| 1174 | } |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static int |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1179 | sign_and_send_pubkey(struct ssh *ssh, Authctxt *authctxt, Identity *id) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1180 | { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1181 | struct sshbuf *b = NULL; |
| 1182 | Identity *private_id, *sign_id = NULL; |
| 1183 | u_char *signature = NULL; |
| 1184 | size_t slen = 0, skip = 0; |
| 1185 | int r, fallback_sigtype, sent = 0; |
| 1186 | char *alg = NULL, *fp = NULL; |
| 1187 | const char *loc = ""; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1188 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 1189 | if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash, |
| 1190 | SSH_FP_DEFAULT)) == NULL) |
| 1191 | return 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1192 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1193 | debug3("%s: %s %s", __func__, sshkey_type(id->key), fp); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1194 | |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1195 | /* |
| 1196 | * If the key is an certificate, try to find a matching private key |
| 1197 | * and use it to complete the signature. |
djm@openbsd.org | 9ee692f | 2016-04-28 14:30:21 +0000 | [diff] [blame] | 1198 | * If no such private key exists, fall back to trying the certificate |
| 1199 | * key itself in case it has a private half already loaded. |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1200 | * This will try to set sign_id to the private key that will perform |
| 1201 | * the signature. |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1202 | */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1203 | if (sshkey_is_cert(id->key)) { |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1204 | TAILQ_FOREACH(private_id, &authctxt->keys, next) { |
| 1205 | if (sshkey_equal_public(id->key, private_id->key) && |
| 1206 | id->key->type != private_id->key->type) { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1207 | sign_id = private_id; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | } |
djm@openbsd.org | 1e24552 | 2017-03-11 23:40:26 +0000 | [diff] [blame] | 1211 | /* |
| 1212 | * Exact key matches are preferred, but also allow |
| 1213 | * filename matches for non-PKCS#11/agent keys that |
| 1214 | * didn't load public keys. This supports the case |
| 1215 | * of keeping just a private key file and public |
| 1216 | * certificate on disk. |
| 1217 | */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1218 | if (sign_id == NULL && |
| 1219 | !id->isprivate && id->agent_fd == -1 && |
djm@openbsd.org | 1e24552 | 2017-03-11 23:40:26 +0000 | [diff] [blame] | 1220 | (id->key->flags & SSHKEY_FLAG_EXT) == 0) { |
| 1221 | TAILQ_FOREACH(private_id, &authctxt->keys, next) { |
| 1222 | if (private_id->key == NULL && |
| 1223 | id_filename_matches(id, private_id)) { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1224 | sign_id = private_id; |
djm@openbsd.org | 1e24552 | 2017-03-11 23:40:26 +0000 | [diff] [blame] | 1225 | break; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1229 | if (sign_id != NULL) { |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1230 | debug2("%s: using private key \"%s\"%s for " |
| 1231 | "certificate", __func__, id->filename, |
| 1232 | id->agent_fd != -1 ? " from agent" : ""); |
| 1233 | } else { |
djm@openbsd.org | c38905b | 2016-03-14 16:20:54 +0000 | [diff] [blame] | 1234 | debug("%s: no separate private key for certificate " |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1235 | "\"%s\"", __func__, id->filename); |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1239 | /* |
| 1240 | * If the above didn't select another identity to do the signing |
| 1241 | * then default to the one we started with. |
| 1242 | */ |
| 1243 | if (sign_id == NULL) |
| 1244 | sign_id = id; |
| 1245 | |
| 1246 | /* assemble and sign data */ |
| 1247 | for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) { |
| 1248 | free(alg); |
| 1249 | slen = 0; |
| 1250 | signature = NULL; |
| 1251 | if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh, |
| 1252 | id->key)) == NULL) { |
| 1253 | error("%s: no mutual signature supported", __func__); |
| 1254 | goto out; |
| 1255 | } |
| 1256 | debug3("%s: signing using %s", __func__, alg); |
| 1257 | |
| 1258 | sshbuf_free(b); |
| 1259 | if ((b = sshbuf_new()) == NULL) |
| 1260 | fatal("%s: sshbuf_new failed", __func__); |
| 1261 | if (datafellows & SSH_OLD_SESSIONID) { |
| 1262 | if ((r = sshbuf_put(b, session_id2, |
| 1263 | session_id2_len)) != 0) { |
| 1264 | fatal("%s: sshbuf_put: %s", |
| 1265 | __func__, ssh_err(r)); |
| 1266 | } |
| 1267 | } else { |
| 1268 | if ((r = sshbuf_put_string(b, session_id2, |
| 1269 | session_id2_len)) != 0) { |
| 1270 | fatal("%s: sshbuf_put_string: %s", |
| 1271 | __func__, ssh_err(r)); |
| 1272 | } |
| 1273 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1274 | skip = sshbuf_len(b); |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1275 | if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1276 | (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || |
| 1277 | (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || |
| 1278 | (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || |
| 1279 | (r = sshbuf_put_u8(b, 1)) != 0 || |
| 1280 | (r = sshbuf_put_cstring(b, alg)) != 0 || |
| 1281 | (r = sshkey_puts(id->key, b)) != 0) { |
| 1282 | fatal("%s: assemble signed data: %s", |
| 1283 | __func__, ssh_err(r)); |
| 1284 | } |
| 1285 | |
| 1286 | /* generate signature */ |
| 1287 | r = identity_sign(sign_id, &signature, &slen, |
| 1288 | sshbuf_ptr(b), sshbuf_len(b), datafellows, alg); |
| 1289 | if (r == 0) |
| 1290 | break; |
| 1291 | else if (r == SSH_ERR_KEY_NOT_FOUND) |
| 1292 | goto out; /* soft failure */ |
| 1293 | else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED && |
| 1294 | !fallback_sigtype) { |
| 1295 | if (sign_id->agent_fd != -1) |
| 1296 | loc = "agent "; |
| 1297 | else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0) |
| 1298 | loc = "token "; |
| 1299 | logit("%skey %s %s returned incorrect signature type", |
| 1300 | loc, sshkey_type(id->key), fp); |
| 1301 | continue; |
| 1302 | } |
| 1303 | error("%s: signing failed: %s", __func__, ssh_err(r)); |
| 1304 | goto out; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1305 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1306 | if (slen == 0 || signature == NULL) /* shouldn't happen */ |
| 1307 | fatal("%s: no signature", __func__); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1308 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1309 | /* append signature */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1310 | if ((r = sshbuf_put_string(b, signature, slen)) != 0) |
| 1311 | fatal("%s: append signature: %s", __func__, ssh_err(r)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1312 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1313 | #ifdef DEBUG_PK |
| 1314 | sshbuf_dump(b, stderr); |
| 1315 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1316 | /* skip session id and packet type */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1317 | if ((r = sshbuf_consume(b, skip + 1)) != 0) |
| 1318 | fatal("%s: consume: %s", __func__, ssh_err(r)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1319 | |
| 1320 | /* put remaining data from buffer into packet */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1321 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1322 | (r = sshpkt_putb(ssh, b)) != 0 || |
| 1323 | (r = sshpkt_send(ssh)) != 0) |
| 1324 | fatal("%s: enqueue request: %s", __func__, ssh_err(r)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1325 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1326 | /* success */ |
| 1327 | sent = 1; |
| 1328 | |
| 1329 | out: |
| 1330 | free(fp); |
| 1331 | free(alg); |
| 1332 | sshbuf_free(b); |
| 1333 | freezero(signature, slen); |
| 1334 | return sent; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1335 | } |
| 1336 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1337 | static int |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1338 | send_pubkey_test(struct ssh *ssh, Authctxt *authctxt, Identity *id) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1339 | { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1340 | u_char *blob = NULL; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1341 | char *alg = NULL; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1342 | size_t bloblen; |
| 1343 | u_int have_sig = 0; |
| 1344 | int sent = 0, r; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1345 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1346 | if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) { |
| 1347 | debug("%s: no mutual signature algorithm", __func__); |
| 1348 | goto out; |
| 1349 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1350 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1351 | if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1352 | /* we cannot handle this key */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1353 | debug3("%s: cannot handle key", __func__); |
| 1354 | goto out; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1355 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1356 | /* register callback for USERAUTH_PK_OK message */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1357 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1358 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1359 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1360 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 1361 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 1362 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 1363 | (r = sshpkt_put_u8(ssh, have_sig)) != 0 || |
| 1364 | (r = sshpkt_put_cstring(ssh, alg)) != 0 || |
| 1365 | (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 || |
| 1366 | (r = sshpkt_send(ssh)) != 0) |
| 1367 | fatal("%s: %s", __func__, ssh_err(r)); |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1368 | sent = 1; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1369 | |
| 1370 | out: |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1371 | free(alg); |
| 1372 | free(blob); |
| 1373 | return sent; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1376 | static struct sshkey * |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1377 | load_identity_file(Identity *id) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1378 | { |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1379 | struct sshkey *private = NULL; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1380 | char prompt[300], *passphrase, *comment; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1381 | int r, perm_ok = 0, quit = 0, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1382 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1383 | |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1384 | if (stat(id->filename, &st) < 0) { |
| 1385 | (id->userprovided ? logit : debug3)("no such identity: %s: %s", |
| 1386 | id->filename, strerror(errno)); |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1387 | return NULL; |
| 1388 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1389 | snprintf(prompt, sizeof prompt, |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1390 | "Enter passphrase for key '%.100s': ", id->filename); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1391 | for (i = 0; i <= options.number_of_password_prompts; i++) { |
| 1392 | if (i == 0) |
| 1393 | passphrase = ""; |
| 1394 | else { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1395 | passphrase = read_passphrase(prompt, 0); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1396 | if (*passphrase == '\0') { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1397 | debug2("no passphrase given, try next key"); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1398 | free(passphrase); |
| 1399 | break; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1400 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1401 | } |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1402 | switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename, |
| 1403 | passphrase, &private, &comment, &perm_ok))) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1404 | case 0: |
| 1405 | break; |
| 1406 | case SSH_ERR_KEY_WRONG_PASSPHRASE: |
| 1407 | if (options.batch_mode) { |
| 1408 | quit = 1; |
| 1409 | break; |
| 1410 | } |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1411 | if (i != 0) |
| 1412 | debug2("bad passphrase given, try again..."); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | case SSH_ERR_SYSTEM_ERROR: |
| 1415 | if (errno == ENOENT) { |
| 1416 | debug2("Load key \"%s\": %s", |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1417 | id->filename, ssh_err(r)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1418 | quit = 1; |
| 1419 | break; |
| 1420 | } |
| 1421 | /* FALLTHROUGH */ |
| 1422 | default: |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1423 | error("Load key \"%s\": %s", id->filename, ssh_err(r)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1424 | quit = 1; |
| 1425 | break; |
| 1426 | } |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1427 | if (!quit && private != NULL && id->agent_fd == -1 && |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1428 | !(id->key && id->isprivate)) |
| 1429 | maybe_add_key_to_agent(id->filename, private, comment, |
| 1430 | passphrase); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1431 | if (i > 0) |
| 1432 | freezero(passphrase, strlen(passphrase)); |
mmcc@openbsd.org | d59ce08 | 2015-12-10 17:08:40 +0000 | [diff] [blame] | 1433 | free(comment); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1434 | if (private != NULL || quit) |
| 1435 | break; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1436 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1437 | return private; |
| 1438 | } |
| 1439 | |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1440 | static int |
| 1441 | key_type_allowed_by_config(struct sshkey *key) |
| 1442 | { |
| 1443 | if (match_pattern_list(sshkey_ssh_name(key), |
| 1444 | options.pubkey_key_types, 0) == 1) |
| 1445 | return 1; |
| 1446 | |
| 1447 | /* RSA keys/certs might be allowed by alternate signature types */ |
| 1448 | switch (key->type) { |
| 1449 | case KEY_RSA: |
| 1450 | if (match_pattern_list("rsa-sha2-512", |
| 1451 | options.pubkey_key_types, 0) == 1) |
| 1452 | return 1; |
| 1453 | if (match_pattern_list("rsa-sha2-256", |
| 1454 | options.pubkey_key_types, 0) == 1) |
| 1455 | return 1; |
| 1456 | break; |
| 1457 | case KEY_RSA_CERT: |
| 1458 | if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com", |
| 1459 | options.pubkey_key_types, 0) == 1) |
| 1460 | return 1; |
| 1461 | if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com", |
| 1462 | options.pubkey_key_types, 0) == 1) |
| 1463 | return 1; |
| 1464 | break; |
| 1465 | } |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1470 | /* |
| 1471 | * try keys in the following order: |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1472 | * 1. certificates listed in the config file |
| 1473 | * 2. other input certificates |
| 1474 | * 3. agent keys that are found in the config file |
| 1475 | * 4. other agent keys |
| 1476 | * 5. keys that are only listed in the config file |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1477 | */ |
| 1478 | static void |
| 1479 | pubkey_prepare(Authctxt *authctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1480 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1481 | struct identity *id, *id2, *tmp; |
| 1482 | struct idlist agent, files, *preferred; |
| 1483 | struct sshkey *key; |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1484 | int agent_fd = -1, i, r, found; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1485 | size_t j; |
| 1486 | struct ssh_identitylist *idlist; |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 1487 | char *ident; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1488 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1489 | TAILQ_INIT(&agent); /* keys from the agent */ |
| 1490 | TAILQ_INIT(&files); /* keys from the config file */ |
| 1491 | preferred = &authctxt->keys; |
| 1492 | TAILQ_INIT(preferred); /* preferred order of keys */ |
| 1493 | |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1494 | /* list of keys stored in the filesystem and PKCS#11 */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1495 | for (i = 0; i < options.num_identity_files; i++) { |
| 1496 | key = options.identity_keys[i]; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1497 | if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) |
| 1498 | continue; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1499 | options.identity_keys[i] = NULL; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1500 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1501 | id->agent_fd = -1; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1502 | id->key = key; |
| 1503 | id->filename = xstrdup(options.identity_files[i]); |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1504 | id->userprovided = options.identity_file_userprovided[i]; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1505 | TAILQ_INSERT_TAIL(&files, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1506 | } |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1507 | /* list of certificates specified by user */ |
| 1508 | for (i = 0; i < options.num_certificate_files; i++) { |
| 1509 | key = options.certificates[i]; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1510 | if (!sshkey_is_cert(key) || key->cert == NULL || |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1511 | key->cert->type != SSH2_CERT_TYPE_USER) |
| 1512 | continue; |
| 1513 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 6064a8b | 2015-12-04 00:24:55 +0000 | [diff] [blame] | 1514 | id->agent_fd = -1; |
djm@openbsd.org | 4e44a79 | 2015-09-24 06:15:11 +0000 | [diff] [blame] | 1515 | id->key = key; |
| 1516 | id->filename = xstrdup(options.certificate_files[i]); |
| 1517 | id->userprovided = options.certificate_file_userprovided[i]; |
| 1518 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1519 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1520 | /* list of keys supported by the agent */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1521 | if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { |
| 1522 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1523 | debug("%s: ssh_get_authentication_socket: %s", |
| 1524 | __func__, ssh_err(r)); |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 1525 | } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1526 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 1527 | debug("%s: ssh_fetch_identitylist: %s", |
| 1528 | __func__, ssh_err(r)); |
markus@openbsd.org | fc77ccd | 2016-01-14 22:56:56 +0000 | [diff] [blame] | 1529 | close(agent_fd); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1530 | } else { |
| 1531 | for (j = 0; j < idlist->nkeys; j++) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1532 | found = 0; |
| 1533 | TAILQ_FOREACH(id, &files, next) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1534 | /* |
| 1535 | * agent keys from the config file are |
| 1536 | * preferred |
| 1537 | */ |
| 1538 | if (sshkey_equal(idlist->keys[j], id->key)) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1539 | TAILQ_REMOVE(&files, id, next); |
| 1540 | TAILQ_INSERT_TAIL(preferred, id, next); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1541 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1542 | found = 1; |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1546 | if (!found && !options.identities_only) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1547 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1548 | /* XXX "steals" key/comment from idlist */ |
| 1549 | id->key = idlist->keys[j]; |
| 1550 | id->filename = idlist->comments[j]; |
| 1551 | idlist->keys[j] = NULL; |
| 1552 | idlist->comments[j] = NULL; |
| 1553 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1554 | TAILQ_INSERT_TAIL(&agent, id, next); |
| 1555 | } |
| 1556 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1557 | ssh_free_identitylist(idlist); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1558 | /* append remaining agent keys */ |
| 1559 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
| 1560 | TAILQ_REMOVE(&agent, id, next); |
| 1561 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1562 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1563 | authctxt->agent_fd = agent_fd; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1564 | } |
djm@openbsd.org | 82f24c3 | 2016-05-23 23:30:50 +0000 | [diff] [blame] | 1565 | /* Prefer PKCS11 keys that are explicitly listed */ |
| 1566 | TAILQ_FOREACH_SAFE(id, &files, next, tmp) { |
| 1567 | if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0) |
| 1568 | continue; |
| 1569 | found = 0; |
| 1570 | TAILQ_FOREACH(id2, &files, next) { |
| 1571 | if (id2->key == NULL || |
| 1572 | (id2->key->flags & SSHKEY_FLAG_EXT) == 0) |
| 1573 | continue; |
| 1574 | if (sshkey_equal(id->key, id2->key)) { |
| 1575 | TAILQ_REMOVE(&files, id, next); |
| 1576 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1577 | found = 1; |
| 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | /* If IdentitiesOnly set and key not found then don't use it */ |
| 1582 | if (!found && options.identities_only) { |
| 1583 | TAILQ_REMOVE(&files, id, next); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1584 | freezero(id, sizeof(*id)); |
djm@openbsd.org | 82f24c3 | 2016-05-23 23:30:50 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1587 | /* append remaining keys from the config file */ |
| 1588 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
| 1589 | TAILQ_REMOVE(&files, id, next); |
| 1590 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1591 | } |
djm@openbsd.org | e679c09 | 2015-10-13 16:15:21 +0000 | [diff] [blame] | 1592 | /* finally, filter by PubkeyAcceptedKeyTypes */ |
| 1593 | TAILQ_FOREACH_SAFE(id, preferred, next, id2) { |
djm@openbsd.org | d78b75d | 2018-07-03 13:07:58 +0000 | [diff] [blame] | 1594 | if (id->key != NULL && !key_type_allowed_by_config(id->key)) { |
djm@openbsd.org | e679c09 | 2015-10-13 16:15:21 +0000 | [diff] [blame] | 1595 | debug("Skipping %s key %s - " |
| 1596 | "not in PubkeyAcceptedKeyTypes", |
| 1597 | sshkey_ssh_name(id->key), id->filename); |
| 1598 | TAILQ_REMOVE(preferred, id, next); |
| 1599 | sshkey_free(id->key); |
| 1600 | free(id->filename); |
| 1601 | memset(id, 0, sizeof(*id)); |
| 1602 | continue; |
| 1603 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1604 | } |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 1605 | /* List the keys we plan on using */ |
| 1606 | TAILQ_FOREACH_SAFE(id, preferred, next, id2) { |
| 1607 | ident = format_identity(id); |
| 1608 | debug("Will attempt key: %s", ident); |
| 1609 | free(ident); |
| 1610 | } |
| 1611 | debug2("%s: done", __func__); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | static void |
| 1615 | pubkey_cleanup(Authctxt *authctxt) |
| 1616 | { |
| 1617 | Identity *id; |
| 1618 | |
djm@openbsd.org | aaed635 | 2018-11-16 02:46:20 +0000 | [diff] [blame] | 1619 | if (authctxt->agent_fd != -1) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1620 | ssh_close_authentication_socket(authctxt->agent_fd); |
djm@openbsd.org | aaed635 | 2018-11-16 02:46:20 +0000 | [diff] [blame] | 1621 | authctxt->agent_fd = -1; |
| 1622 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1623 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
| 1624 | id = TAILQ_FIRST(&authctxt->keys)) { |
| 1625 | TAILQ_REMOVE(&authctxt->keys, id, next); |
mmcc@openbsd.org | 89540b6 | 2015-12-11 02:31:47 +0000 | [diff] [blame] | 1626 | sshkey_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1627 | free(id->filename); |
| 1628 | free(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1629 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1630 | } |
| 1631 | |
djm@openbsd.org | b9844a4 | 2016-12-04 23:54:02 +0000 | [diff] [blame] | 1632 | static void |
| 1633 | pubkey_reset(Authctxt *authctxt) |
| 1634 | { |
| 1635 | Identity *id; |
| 1636 | |
| 1637 | TAILQ_FOREACH(id, &authctxt->keys, next) |
| 1638 | id->tried = 0; |
| 1639 | } |
| 1640 | |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1641 | static int |
| 1642 | try_identity(Identity *id) |
| 1643 | { |
| 1644 | if (!id->key) |
| 1645 | return (0); |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 1646 | if (sshkey_type_plain(id->key->type) == KEY_RSA && |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1647 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 1648 | debug("Skipped %s key %s for RSA/MD5 server", |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 1649 | sshkey_type(id->key), id->filename); |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1650 | return (0); |
| 1651 | } |
djm@openbsd.org | 873d3e7 | 2017-04-30 23:18:44 +0000 | [diff] [blame] | 1652 | return 1; |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1655 | int |
| 1656 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1657 | { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1658 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1659 | Identity *id; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1660 | int sent = 0; |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 1661 | char *ident; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1662 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1663 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
| 1664 | if (id->tried++) |
| 1665 | return (0); |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 1666 | /* move key to the end of the queue */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1667 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1668 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); |
| 1669 | /* |
| 1670 | * send a test message if we have the public key. for |
| 1671 | * encrypted keys we cannot do this and have to load the |
| 1672 | * private key instead |
| 1673 | */ |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1674 | if (id->key != NULL) { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1675 | if (try_identity(id)) { |
djm@openbsd.org | beb9e52 | 2018-09-14 05:26:27 +0000 | [diff] [blame] | 1676 | ident = format_identity(id); |
| 1677 | debug("Offering public key: %s", ident); |
| 1678 | free(ident); |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1679 | sent = send_pubkey_test(ssh, authctxt, id); |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1680 | } |
| 1681 | } else { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1682 | debug("Trying private key: %s", id->filename); |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 1683 | id->key = load_identity_file(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1684 | if (id->key != NULL) { |
markus@openbsd.org | 3a1638d | 2015-07-10 06:21:53 +0000 | [diff] [blame] | 1685 | if (try_identity(id)) { |
| 1686 | id->isprivate = 1; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1687 | sent = sign_and_send_pubkey(ssh, |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1688 | authctxt, id); |
| 1689 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1690 | sshkey_free(id->key); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1691 | id->key = NULL; |
djm@openbsd.org | b9844a4 | 2016-12-04 23:54:02 +0000 | [diff] [blame] | 1692 | id->isprivate = 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1693 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1694 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1695 | if (sent) |
| 1696 | return (sent); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1697 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1698 | return (0); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1699 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1700 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1701 | /* |
| 1702 | * Send userauth request message specifying keyboard-interactive method. |
| 1703 | */ |
| 1704 | int |
| 1705 | userauth_kbdint(Authctxt *authctxt) |
| 1706 | { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1707 | struct ssh *ssh = active_state; /* XXX */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1708 | static int attempt = 0; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1709 | int r; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1710 | |
| 1711 | if (attempt++ >= options.number_of_password_prompts) |
| 1712 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1713 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 1714 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 1715 | debug3("userauth_kbdint: disable: no info_req_seen"); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1716 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1717 | return 0; |
| 1718 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1719 | |
| 1720 | debug2("userauth_kbdint"); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1721 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 1722 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 1723 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 1724 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
| 1725 | (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */ |
| 1726 | (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ? |
| 1727 | options.kbd_interactive_devices : "")) != 0 || |
| 1728 | (r = sshpkt_send(ssh)) != 0) |
| 1729 | fatal("%s: %s", __func__, ssh_err(r)); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1730 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1731 | ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1732 | return 1; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1736 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1737 | */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 1738 | int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 1739 | 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] | 1740 | { |
markus@openbsd.org | 94583be | 2017-05-30 14:19:15 +0000 | [diff] [blame] | 1741 | Authctxt *authctxt = ssh->authctxt; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1742 | char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL; |
| 1743 | char *response = NULL; |
| 1744 | u_char echo = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1745 | u_int num_prompts, i; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1746 | int r; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1747 | |
| 1748 | debug2("input_userauth_info_req"); |
| 1749 | |
| 1750 | if (authctxt == NULL) |
| 1751 | fatal("input_userauth_info_req: no authentication context"); |
| 1752 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1753 | authctxt->info_req_seen = 1; |
| 1754 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1755 | if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 || |
| 1756 | (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 || |
| 1757 | (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) |
| 1758 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1759 | if (strlen(name) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1760 | logit("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1761 | if (strlen(inst) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1762 | logit("%s", inst); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1763 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1764 | if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0) |
| 1765 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1766 | /* |
| 1767 | * Begin to build info response packet based on prompts requested. |
| 1768 | * We commit to providing the correct number of responses, so if |
| 1769 | * further on we run into a problem that prevents this, we have to |
| 1770 | * be sure and clean this up and send a correct error response. |
| 1771 | */ |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1772 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 || |
| 1773 | (r = sshpkt_put_u32(ssh, num_prompts)) != 0) |
| 1774 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1775 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1776 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1777 | for (i = 0; i < num_prompts; i++) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1778 | if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 || |
| 1779 | (r = sshpkt_get_u8(ssh, &echo)) != 0) |
| 1780 | goto out; |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1781 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1782 | if ((r = sshpkt_put_cstring(ssh, response)) != 0) |
| 1783 | goto out; |
| 1784 | freezero(response, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1785 | free(prompt); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1786 | response = prompt = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1787 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 1788 | /* done with parsing incoming message. */ |
| 1789 | if ((r = sshpkt_get_end(ssh)) != 0 || |
| 1790 | (r = sshpkt_add_padding(ssh, 64)) != 0) |
| 1791 | goto out; |
| 1792 | r = sshpkt_send(ssh); |
| 1793 | out: |
| 1794 | if (response) |
| 1795 | freezero(response, strlen(response)); |
| 1796 | free(prompt); |
| 1797 | free(name); |
| 1798 | free(inst); |
| 1799 | free(lang); |
| 1800 | return r; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1801 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1802 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1803 | static int |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1804 | ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp, |
| 1805 | const u_char *data, size_t datalen) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1806 | { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1807 | struct sshbuf *b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1808 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1809 | pid_t pid; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1810 | int i, r, to[2], from[2], status, sock = packet_get_connection_in(); |
| 1811 | u_char rversion = 0, version = 2; |
| 1812 | void (*osigchld)(int); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1813 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1814 | *sigp = NULL; |
| 1815 | *lenp = 0; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1816 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1817 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1818 | error("%s: not installed: %s", __func__, strerror(errno)); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1819 | return -1; |
| 1820 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1821 | if (fflush(stdout) != 0) { |
| 1822 | error("%s: fflush: %s", __func__, strerror(errno)); |
| 1823 | return -1; |
| 1824 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1825 | if (pipe(to) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1826 | error("%s: pipe: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1827 | return -1; |
| 1828 | } |
| 1829 | if (pipe(from) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1830 | error("%s: pipe: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1831 | return -1; |
| 1832 | } |
| 1833 | if ((pid = fork()) < 0) { |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1834 | error("%s: fork: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1835 | return -1; |
| 1836 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1837 | osigchld = signal(SIGCHLD, SIG_DFL); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1838 | if (pid == 0) { |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 1839 | /* keep the socket on exec */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1840 | fcntl(sock, F_SETFD, 0); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1841 | close(from[0]); |
| 1842 | if (dup2(from[1], STDOUT_FILENO) < 0) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1843 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1844 | close(to[1]); |
| 1845 | if (dup2(to[0], STDIN_FILENO) < 0) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1846 | fatal("%s: dup2: %s", __func__, strerror(errno)); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1847 | close(from[1]); |
| 1848 | close(to[0]); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1849 | /* Close everything but stdio and the socket */ |
| 1850 | for (i = STDERR_FILENO + 1; i < sock; i++) |
| 1851 | close(i); |
| 1852 | closefrom(sock + 1); |
| 1853 | debug3("%s: [child] pid=%ld, exec %s", |
| 1854 | __func__, (long)getpid(), _PATH_SSH_KEY_SIGN); |
mmcc@openbsd.org | 94141b7 | 2015-12-11 00:20:04 +0000 | [diff] [blame] | 1855 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1856 | fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1857 | strerror(errno)); |
| 1858 | } |
| 1859 | close(from[1]); |
| 1860 | close(to[0]); |
| 1861 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1862 | if ((b = sshbuf_new()) == NULL) |
| 1863 | fatal("%s: sshbuf_new failed", __func__); |
| 1864 | /* send # of sock, data to be signed */ |
djm@openbsd.org | 326e2fa | 2017-01-30 00:32:28 +0000 | [diff] [blame] | 1865 | if ((r = sshbuf_put_u32(b, sock)) != 0 || |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1866 | (r = sshbuf_put_string(b, data, datalen)) != 0) |
| 1867 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1868 | if (ssh_msg_send(to[1], version, b) == -1) |
| 1869 | fatal("%s: couldn't send request", __func__); |
| 1870 | sshbuf_reset(b); |
| 1871 | r = ssh_msg_recv(from[0], b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1872 | close(from[0]); |
| 1873 | close(to[1]); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1874 | if (r < 0) { |
| 1875 | error("%s: no reply", __func__); |
| 1876 | goto fail; |
| 1877 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1878 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1879 | errno = 0; |
| 1880 | while (waitpid(pid, &status, 0) < 0) { |
| 1881 | if (errno != EINTR) { |
| 1882 | error("%s: waitpid %ld: %s", |
| 1883 | __func__, (long)pid, strerror(errno)); |
| 1884 | goto fail; |
| 1885 | } |
| 1886 | } |
| 1887 | if (!WIFEXITED(status)) { |
| 1888 | error("%s: exited abnormally", __func__); |
| 1889 | goto fail; |
| 1890 | } |
| 1891 | if (WEXITSTATUS(status) != 0) { |
| 1892 | error("%s: exited with status %d", |
| 1893 | __func__, WEXITSTATUS(status)); |
| 1894 | goto fail; |
| 1895 | } |
| 1896 | if ((r = sshbuf_get_u8(b, &rversion)) != 0) { |
| 1897 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1898 | goto fail; |
| 1899 | } |
| 1900 | if (rversion != version) { |
| 1901 | error("%s: bad version", __func__); |
| 1902 | goto fail; |
| 1903 | } |
| 1904 | if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { |
| 1905 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 1906 | fail: |
| 1907 | signal(SIGCHLD, osigchld); |
| 1908 | sshbuf_free(b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1909 | return -1; |
| 1910 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1911 | signal(SIGCHLD, osigchld); |
| 1912 | sshbuf_free(b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1913 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1914 | return 0; |
| 1915 | } |
| 1916 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1917 | int |
| 1918 | userauth_hostbased(Authctxt *authctxt) |
| 1919 | { |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 1920 | struct ssh *ssh = active_state; /* XXX */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1921 | struct sshkey *private = NULL; |
| 1922 | struct sshbuf *b = NULL; |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1923 | u_char *sig = NULL, *keyblob = NULL; |
| 1924 | char *fp = NULL, *chost = NULL, *lname = NULL; |
| 1925 | size_t siglen = 0, keylen = 0; |
| 1926 | int i, r, success = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1927 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1928 | if (authctxt->ktypes == NULL) { |
| 1929 | authctxt->oktypes = xstrdup(options.hostbased_key_types); |
| 1930 | authctxt->ktypes = authctxt->oktypes; |
| 1931 | } |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 1932 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1933 | /* |
| 1934 | * Work through each listed type pattern in HostbasedKeyTypes, |
| 1935 | * trying each hostkey that matches the type in turn. |
| 1936 | */ |
| 1937 | for (;;) { |
| 1938 | if (authctxt->active_ktype == NULL) |
| 1939 | authctxt->active_ktype = strsep(&authctxt->ktypes, ","); |
| 1940 | if (authctxt->active_ktype == NULL || |
| 1941 | *authctxt->active_ktype == '\0') |
| 1942 | break; |
| 1943 | debug3("%s: trying key type %s", __func__, |
| 1944 | authctxt->active_ktype); |
| 1945 | |
| 1946 | /* check for a useful key */ |
| 1947 | private = NULL; |
| 1948 | for (i = 0; i < authctxt->sensitive->nkeys; i++) { |
| 1949 | if (authctxt->sensitive->keys[i] == NULL || |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1950 | authctxt->sensitive->keys[i]->type == KEY_UNSPEC) |
| 1951 | continue; |
| 1952 | if (match_pattern_list( |
| 1953 | sshkey_ssh_name(authctxt->sensitive->keys[i]), |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 1954 | authctxt->active_ktype, 0) != 1) |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1955 | continue; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1956 | /* we take and free the key */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1957 | private = authctxt->sensitive->keys[i]; |
| 1958 | authctxt->sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1961 | /* Found one */ |
| 1962 | if (private != NULL) |
| 1963 | break; |
| 1964 | /* No more keys of this type; advance */ |
| 1965 | authctxt->active_ktype = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1966 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1967 | if (private == NULL) { |
| 1968 | free(authctxt->oktypes); |
| 1969 | authctxt->oktypes = authctxt->ktypes = NULL; |
| 1970 | authctxt->active_ktype = NULL; |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1971 | debug("No more client hostkeys for hostbased authentication."); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1972 | goto out; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1973 | } |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1974 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1975 | if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, |
| 1976 | SSH_FP_DEFAULT)) == NULL) { |
| 1977 | error("%s: sshkey_fingerprint failed", __func__); |
| 1978 | goto out; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1979 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1980 | debug("%s: trying hostkey %s %s", |
| 1981 | __func__, sshkey_ssh_name(private), fp); |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1982 | |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1983 | /* figure out a name for the client host */ |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1984 | if ((lname = get_local_name(packet_get_connection_in())) == NULL) { |
| 1985 | error("%s: cannot get local ipaddr/name", __func__); |
| 1986 | goto out; |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1987 | } |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1988 | |
| 1989 | /* XXX sshbuf_put_stringf? */ |
| 1990 | xasprintf(&chost, "%s.", lname); |
| 1991 | debug2("%s: chost %s", __func__, chost); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1992 | |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 1993 | /* construct data */ |
| 1994 | if ((b = sshbuf_new()) == NULL) { |
| 1995 | error("%s: sshbuf_new failed", __func__); |
| 1996 | goto out; |
| 1997 | } |
| 1998 | if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { |
| 1999 | error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); |
| 2000 | goto out; |
| 2001 | } |
| 2002 | if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || |
| 2003 | (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 2004 | (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 2005 | (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2006 | (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 2007 | (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 || |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2008 | (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || |
| 2009 | (r = sshbuf_put_cstring(b, chost)) != 0 || |
| 2010 | (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { |
| 2011 | error("%s: buffer error: %s", __func__, ssh_err(r)); |
| 2012 | goto out; |
| 2013 | } |
| 2014 | |
| 2015 | #ifdef DEBUG_PK |
| 2016 | sshbuf_dump(b, stderr); |
| 2017 | #endif |
dtucker@openbsd.org | 26efc2f | 2018-07-16 11:05:41 +0000 | [diff] [blame] | 2018 | r = ssh_keysign(private, &sig, &siglen, |
| 2019 | sshbuf_ptr(b), sshbuf_len(b)); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2020 | if (r != 0) { |
| 2021 | error("sign using hostkey %s %s failed", |
| 2022 | sshkey_ssh_name(private), fp); |
| 2023 | goto out; |
| 2024 | } |
| 2025 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 2026 | (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || |
| 2027 | (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || |
| 2028 | (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 2029 | (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 || |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2030 | (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || |
| 2031 | (r = sshpkt_put_cstring(ssh, chost)) != 0 || |
| 2032 | (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || |
| 2033 | (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || |
| 2034 | (r = sshpkt_send(ssh)) != 0) { |
| 2035 | error("%s: packet error: %s", __func__, ssh_err(r)); |
| 2036 | goto out; |
| 2037 | } |
| 2038 | success = 1; |
| 2039 | |
| 2040 | out: |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2041 | if (sig != NULL) |
| 2042 | freezero(sig, siglen); |
djm@openbsd.org | 46347ed | 2015-01-30 11:43:14 +0000 | [diff] [blame] | 2043 | free(keyblob); |
| 2044 | free(lname); |
| 2045 | free(fp); |
| 2046 | free(chost); |
| 2047 | sshkey_free(private); |
| 2048 | sshbuf_free(b); |
| 2049 | |
| 2050 | return success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2053 | /* find auth method */ |
| 2054 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2055 | /* |
| 2056 | * given auth method name, if configurable options permit this method fill |
| 2057 | * in auth_ident field and return true, otherwise return false. |
| 2058 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2059 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2060 | authmethod_is_enabled(Authmethod *method) |
| 2061 | { |
| 2062 | if (method == NULL) |
| 2063 | return 0; |
| 2064 | /* return false if options indicate this method is disabled */ |
| 2065 | if (method->enabled == NULL || *method->enabled == 0) |
| 2066 | return 0; |
| 2067 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 2068 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 2069 | return 0; |
| 2070 | return 1; |
| 2071 | } |
| 2072 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2073 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2074 | authmethod_lookup(const char *name) |
| 2075 | { |
| 2076 | Authmethod *method = NULL; |
| 2077 | if (name != NULL) |
| 2078 | for (method = authmethods; method->name != NULL; method++) |
| 2079 | if (strcmp(name, method->name) == 0) |
| 2080 | return method; |
| 2081 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2085 | /* XXX internal state */ |
| 2086 | static Authmethod *current = NULL; |
| 2087 | static char *supported = NULL; |
| 2088 | static char *preferred = NULL; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 2089 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2090 | /* |
| 2091 | * Given the authentication method list sent by the server, return the |
| 2092 | * 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] | 2093 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 2094 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 2095 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2096 | authmethod_get(char *authlist) |
| 2097 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2098 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 2099 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 2100 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2101 | /* Use a suitable default if we're passed a nil list. */ |
| 2102 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2103 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2104 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2105 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 2106 | debug3("start over, passed a different list %s", authlist); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2107 | free(supported); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2108 | supported = xstrdup(authlist); |
| 2109 | preferred = options.preferred_authentications; |
| 2110 | debug3("preferred %s", preferred); |
| 2111 | current = NULL; |
| 2112 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 2113 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2114 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2115 | for (;;) { |
| 2116 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 2117 | debug("No more authentication methods to try."); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2118 | current = NULL; |
| 2119 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 2120 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2121 | preferred += next; |
| 2122 | debug3("authmethod_lookup %s", name); |
| 2123 | debug3("remaining preferred: %s", preferred); |
| 2124 | if ((current = authmethod_lookup(name)) != NULL && |
| 2125 | authmethod_is_enabled(current)) { |
| 2126 | debug3("authmethod_is_enabled %s", name); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 2127 | debug("Next authentication method: %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2128 | free(name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2129 | return current; |
| 2130 | } |
Darren Tucker | e52a260 | 2013-06-06 08:22:05 +1000 | [diff] [blame] | 2131 | free(name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2132 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2133 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2134 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 2135 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2136 | authmethods_get(void) |
| 2137 | { |
| 2138 | Authmethod *method = NULL; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2139 | struct sshbuf *b; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 2140 | char *list; |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2141 | int r; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2142 | |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2143 | if ((b = sshbuf_new()) == NULL) |
| 2144 | fatal("%s: sshbuf_new failed", __func__); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2145 | for (method = authmethods; method->name != NULL; method++) { |
| 2146 | if (authmethod_is_enabled(method)) { |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2147 | if ((r = sshbuf_putf(b, "%s%s", |
| 2148 | sshbuf_len(b) ? "," : "", method->name)) != 0) |
| 2149 | fatal("%s: buffer error: %s", |
| 2150 | __func__, ssh_err(r)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 2151 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 2152 | } |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2153 | if ((list = sshbuf_dup_string(b)) == NULL) |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 2154 | fatal("%s: sshbuf_dup_string failed", __func__); |
markus@openbsd.org | cecee2d | 2018-07-09 21:03:30 +0000 | [diff] [blame] | 2155 | sshbuf_free(b); |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 2156 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2157 | } |