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