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