markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: sshconnect2.c,v 1.218 2015/01/19 20:07:45 markus Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 4 | * Copyright (c) 2008 Damien Miller. All rights reserved. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | 2eb6340 | 2006-03-15 11:09:42 +1100 | [diff] [blame] | 28 | |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 29 | #include <sys/types.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/socket.h> |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 31 | #include <sys/wait.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 32 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 33 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 34 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 35 | #include <fcntl.h> |
Darren Tucker | f520ea1 | 2007-05-20 15:11:33 +1000 | [diff] [blame] | 36 | #include <netdb.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
| 38 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 39 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 40 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 41 | #include <string.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 42 | #include <unistd.h> |
Damien Miller | 63b4bcd | 2013-03-20 12:55:14 +1100 | [diff] [blame] | 43 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 44 | #include <vis.h> |
Damien Miller | 2e28d86 | 2008-07-17 19:15:43 +1000 | [diff] [blame] | 45 | #endif |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 46 | |
Damien Miller | 9c61769 | 2003-05-14 14:31:11 +1000 | [diff] [blame] | 47 | #include "openbsd-compat/sys-queue.h" |
| 48 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "buffer.h" |
| 53 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 55 | #include "cipher.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | #include "kex.h" |
| 58 | #include "myproposal.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 59 | #include "sshconnect.h" |
| 60 | #include "authfile.h" |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 61 | #include "dh.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 62 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 63 | #include "log.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 64 | #include "misc.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 65 | #include "readconf.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 66 | #include "match.h" |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 67 | #include "dispatch.h" |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 68 | #include "canohost.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 69 | #include "msg.h" |
| 70 | #include "pathnames.h" |
Damien Miller | 6b4069a | 2006-06-13 13:05:15 +1000 | [diff] [blame] | 71 | #include "uidswap.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 72 | #include "hostfile.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 73 | #include "ssherr.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 74 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 75 | #ifdef GSSAPI |
| 76 | #include "ssh-gss.h" |
| 77 | #endif |
| 78 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 79 | /* import */ |
| 80 | extern char *client_version_string; |
| 81 | extern char *server_version_string; |
| 82 | extern Options options; |
| 83 | |
| 84 | /* |
| 85 | * SSH2 key exchange |
| 86 | */ |
| 87 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 88 | u_char *session_id2 = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 89 | u_int session_id2_len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 90 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 91 | char *xxx_host; |
| 92 | struct sockaddr *xxx_hostaddr; |
| 93 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 94 | static int |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 95 | verify_host_key_callback(Key *hostkey) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 96 | { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 97 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 98 | fatal("Host key verification failed."); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 102 | static char * |
| 103 | order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) |
| 104 | { |
| 105 | char *oavail, *avail, *first, *last, *alg, *hostname, *ret; |
| 106 | size_t maxlen; |
| 107 | struct hostkeys *hostkeys; |
| 108 | int ktype; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 109 | u_int i; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 110 | |
| 111 | /* Find all hostkeys for this hostname */ |
| 112 | get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); |
| 113 | hostkeys = init_hostkeys(); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 114 | for (i = 0; i < options.num_user_hostfiles; i++) |
| 115 | load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); |
| 116 | for (i = 0; i < options.num_system_hostfiles; i++) |
| 117 | load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 118 | |
| 119 | oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG); |
| 120 | maxlen = strlen(avail) + 1; |
| 121 | first = xmalloc(maxlen); |
| 122 | last = xmalloc(maxlen); |
| 123 | *first = *last = '\0'; |
| 124 | |
| 125 | #define ALG_APPEND(to, from) \ |
| 126 | do { \ |
| 127 | if (*to != '\0') \ |
| 128 | strlcat(to, ",", maxlen); \ |
| 129 | strlcat(to, from, maxlen); \ |
| 130 | } while (0) |
| 131 | |
| 132 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 133 | if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 134 | fatal("%s: unknown alg %s", __func__, alg); |
| 135 | if (lookup_key_in_hostkeys_by_type(hostkeys, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 136 | sshkey_type_plain(ktype), NULL)) |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 137 | ALG_APPEND(first, alg); |
| 138 | else |
| 139 | ALG_APPEND(last, alg); |
| 140 | } |
| 141 | #undef ALG_APPEND |
djm@openbsd.org | 35d6022 | 2015-01-18 13:33:34 +0000 | [diff] [blame] | 142 | xasprintf(&ret, "%s%s%s", first, |
| 143 | (*first == '\0' || *last == '\0') ? "" : ",", last); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 144 | if (*first != '\0') |
| 145 | debug3("%s: prefer hostkeyalgs: %s", __func__, first); |
| 146 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 147 | free(first); |
| 148 | free(last); |
| 149 | free(hostname); |
| 150 | free(oavail); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 151 | free_hostkeys(hostkeys); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 156 | void |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 157 | ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 158 | { |
Damien Miller | 9235a03 | 2014-04-20 13:17:20 +1000 | [diff] [blame] | 159 | char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 160 | Kex *kex; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 161 | |
| 162 | xxx_host = host; |
| 163 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 164 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 165 | if (options.ciphers == (char *)-1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 166 | logit("No valid ciphers for protocol version 2 given, using defaults."); |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 167 | options.ciphers = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 168 | } |
| 169 | if (options.ciphers != NULL) { |
| 170 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 171 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 172 | } |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 173 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 174 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); |
| 175 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
| 176 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 177 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 178 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 179 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 180 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 181 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 182 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 183 | } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 184 | if (options.macs != NULL) { |
| 185 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 186 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 187 | } |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 188 | if (options.hostkeyalgorithms != NULL) |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 189 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 190 | compat_pkalg_proposal(options.hostkeyalgorithms); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 191 | else { |
| 192 | /* Prefer algorithms that we already have keys for */ |
| 193 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 194 | compat_pkalg_proposal( |
| 195 | order_hostkeyalgs(host, hostaddr, port)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 196 | } |
Damien Miller | d5f62bf | 2010-09-24 22:11:14 +1000 | [diff] [blame] | 197 | if (options.kex_algorithms != NULL) |
| 198 | myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms; |
Damien Miller | 9395b28 | 2014-04-20 13:25:30 +1000 | [diff] [blame] | 199 | myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal( |
| 200 | myproposal[PROPOSAL_KEX_ALGS]); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 201 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 202 | if (options.rekey_limit || options.rekey_interval) |
| 203 | packet_set_rekey_limits((u_int32_t)options.rekey_limit, |
| 204 | (time_t)options.rekey_interval); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 205 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 206 | /* start key exchange */ |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 207 | kex = kex_setup(myproposal); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 208 | active_state->kex = kex; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 209 | #ifdef WITH_OPENSSL |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 210 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 211 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 212 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 213 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 214 | kex->kex[KEX_ECDH_SHA2] = kexecdh_client; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 215 | #endif |
Damien Miller | 1e12426 | 2013-11-04 08:26:52 +1100 | [diff] [blame] | 216 | kex->kex[KEX_C25519_SHA256] = kexc25519_client; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 217 | kex->client_version_string=client_version_string; |
| 218 | kex->server_version_string=server_version_string; |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 219 | kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 220 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 221 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 222 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 223 | if (options.use_roaming && !kex->roaming) { |
| 224 | debug("Roaming not allowed by server"); |
| 225 | options.use_roaming = 0; |
| 226 | } |
| 227 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 228 | session_id2 = kex->session_id; |
| 229 | session_id2_len = kex->session_id_len; |
| 230 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 231 | #ifdef DEBUG_KEXDH |
| 232 | /* send 1st encrypted/maced/compressed message */ |
| 233 | packet_start(SSH2_MSG_IGNORE); |
| 234 | packet_put_cstring("markus"); |
| 235 | packet_send(); |
| 236 | packet_write_wait(); |
| 237 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 238 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 239 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 240 | /* |
| 241 | * Authenticate user |
| 242 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 243 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 244 | typedef struct cauthctxt Authctxt; |
| 245 | typedef struct cauthmethod Authmethod; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 246 | typedef struct identity Identity; |
| 247 | typedef struct idlist Idlist; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 248 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 249 | struct identity { |
| 250 | TAILQ_ENTRY(identity) next; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 251 | int agent_fd; /* >=0 if agent supports key */ |
| 252 | struct sshkey *key; /* public/private key */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 253 | char *filename; /* comment for agent-only keys */ |
| 254 | int tried; |
| 255 | int isprivate; /* key points to the private key */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 256 | int userprovided; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 257 | }; |
| 258 | TAILQ_HEAD(idlist, identity); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 259 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 260 | struct cauthctxt { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 261 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 262 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 263 | const char *host; |
| 264 | const char *service; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 265 | struct cauthmethod *method; |
Damien Miller | 79442c0 | 2010-05-10 11:55:38 +1000 | [diff] [blame] | 266 | sig_atomic_t success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 267 | char *authlist; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 268 | int attempt; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 269 | /* pubkey */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 270 | struct idlist keys; |
| 271 | int agent_fd; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 272 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 273 | Sensitive *sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 274 | /* kbd-interactive */ |
| 275 | int info_req_seen; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 276 | /* generic */ |
| 277 | void *methoddata; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 278 | }; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 279 | |
| 280 | struct cauthmethod { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 281 | char *name; /* string to compare against server's list */ |
| 282 | int (*userauth)(Authctxt *authctxt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 283 | void (*cleanup)(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 284 | int *enabled; /* flag in option struct that enables method */ |
| 285 | int *batch_flag; /* flag in option struct that disables method */ |
| 286 | }; |
| 287 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 288 | int input_userauth_success(int, u_int32_t, void *); |
| 289 | int input_userauth_success_unexpected(int, u_int32_t, void *); |
| 290 | int input_userauth_failure(int, u_int32_t, void *); |
| 291 | int input_userauth_banner(int, u_int32_t, void *); |
| 292 | int input_userauth_error(int, u_int32_t, void *); |
| 293 | int input_userauth_info_req(int, u_int32_t, void *); |
| 294 | int input_userauth_pk_ok(int, u_int32_t, void *); |
| 295 | int input_userauth_passwd_changereq(int, u_int32_t, void *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 296 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 297 | int userauth_none(Authctxt *); |
| 298 | int userauth_pubkey(Authctxt *); |
| 299 | int userauth_passwd(Authctxt *); |
| 300 | int userauth_kbdint(Authctxt *); |
| 301 | int userauth_hostbased(Authctxt *); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 302 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 303 | #ifdef GSSAPI |
| 304 | int userauth_gssapi(Authctxt *authctxt); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 305 | int input_gssapi_response(int type, u_int32_t, void *); |
| 306 | int input_gssapi_token(int type, u_int32_t, void *); |
| 307 | int input_gssapi_hash(int type, u_int32_t, void *); |
| 308 | int input_gssapi_error(int, u_int32_t, void *); |
| 309 | int input_gssapi_errtok(int, u_int32_t, void *); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 310 | #endif |
| 311 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 312 | void userauth(Authctxt *, char *); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 313 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 314 | static int sign_and_send_pubkey(Authctxt *, Identity *); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 315 | static void pubkey_prepare(Authctxt *); |
| 316 | static void pubkey_cleanup(Authctxt *); |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 317 | static Key *load_identity_file(char *, int); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 318 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 319 | static Authmethod *authmethod_get(char *authlist); |
| 320 | static Authmethod *authmethod_lookup(const char *name); |
| 321 | static char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 322 | |
| 323 | Authmethod authmethods[] = { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 324 | #ifdef GSSAPI |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 325 | {"gssapi-with-mic", |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 326 | userauth_gssapi, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 327 | NULL, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 328 | &options.gss_authentication, |
| 329 | NULL}, |
| 330 | #endif |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 331 | {"hostbased", |
| 332 | userauth_hostbased, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 333 | NULL, |
Ben Lindstrom | 1bfe291 | 2001-06-05 19:37:25 +0000 | [diff] [blame] | 334 | &options.hostbased_authentication, |
| 335 | NULL}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 336 | {"publickey", |
| 337 | userauth_pubkey, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 338 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 339 | &options.pubkey_authentication, |
| 340 | NULL}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 341 | {"keyboard-interactive", |
| 342 | userauth_kbdint, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 343 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 344 | &options.kbd_interactive_authentication, |
| 345 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 346 | {"password", |
| 347 | userauth_passwd, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 348 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 349 | &options.password_authentication, |
| 350 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 351 | {"none", |
| 352 | userauth_none, |
| 353 | NULL, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 354 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 355 | NULL}, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 356 | {NULL, NULL, NULL, NULL, NULL} |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 360 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 361 | Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 362 | { |
| 363 | Authctxt authctxt; |
| 364 | int type; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 365 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 366 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 367 | options.kbd_interactive_authentication = 1; |
| 368 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 369 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 370 | packet_put_cstring("ssh-userauth"); |
| 371 | packet_send(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 372 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 373 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 374 | type = packet_read(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 375 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
| 376 | fatal("Server denied authentication request: %d", type); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 377 | if (packet_remaining() > 0) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 378 | char *reply = packet_get_string(NULL); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 379 | debug2("service_accept: %s", reply); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 380 | free(reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 381 | } else { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 382 | debug2("buggy server: service_accept w/o service"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 383 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 384 | packet_check_eom(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 385 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 386 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 387 | if (options.preferred_authentications == NULL) |
| 388 | options.preferred_authentications = authmethods_get(); |
| 389 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 390 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 391 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 392 | pubkey_prepare(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 393 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 394 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 395 | authctxt.host = host; |
| 396 | authctxt.service = "ssh-connection"; /* service name */ |
| 397 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 398 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 399 | authctxt.authlist = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 400 | authctxt.methoddata = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 401 | authctxt.sensitive = sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 402 | authctxt.info_req_seen = 0; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 403 | authctxt.agent_fd = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 404 | if (authctxt.method == NULL) |
| 405 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 406 | |
| 407 | /* initial userauth request */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 408 | userauth_none(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 409 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 410 | dispatch_init(&input_userauth_error); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 411 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 412 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 413 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 414 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
| 415 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 416 | pubkey_cleanup(&authctxt); |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 417 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); |
| 418 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 419 | debug("Authentication succeeded (%s).", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 420 | } |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 421 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 422 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 423 | userauth(Authctxt *authctxt, char *authlist) |
| 424 | { |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 425 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 426 | authctxt->method->cleanup(authctxt); |
| 427 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 428 | free(authctxt->methoddata); |
| 429 | authctxt->methoddata = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 430 | if (authlist == NULL) { |
| 431 | authlist = authctxt->authlist; |
| 432 | } else { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 433 | free(authctxt->authlist); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 434 | authctxt->authlist = authlist; |
| 435 | } |
| 436 | for (;;) { |
| 437 | Authmethod *method = authmethod_get(authlist); |
| 438 | if (method == NULL) |
| 439 | fatal("Permission denied (%s).", authlist); |
| 440 | authctxt->method = method; |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 441 | |
| 442 | /* reset the per method handler */ |
| 443 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, |
| 444 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); |
| 445 | |
| 446 | /* and try new method */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 447 | if (method->userauth(authctxt) != 0) { |
| 448 | debug2("we sent a %s packet, wait for reply", method->name); |
| 449 | break; |
| 450 | } else { |
| 451 | debug2("we did not send a packet, disable method"); |
| 452 | method->enabled = NULL; |
| 453 | } |
| 454 | } |
| 455 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 456 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 457 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 458 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 459 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 460 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 461 | fatal("input_userauth_error: bad message during authentication: " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 462 | "type %d", type); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 463 | return 0; |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 464 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 465 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 466 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 467 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 468 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 469 | { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 470 | char *msg, *raw, *lang; |
| 471 | u_int len; |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 472 | |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 473 | debug3("input_userauth_banner"); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 474 | raw = packet_get_string(&len); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 475 | lang = packet_get_string(NULL); |
Damien Miller | c674d58 | 2008-11-03 19:16:57 +1100 | [diff] [blame] | 476 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 477 | if (len > 65536) |
| 478 | len = 65536; |
Damien Miller | c4d1b36 | 2008-11-03 19:22:09 +1100 | [diff] [blame] | 479 | msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ |
Darren Tucker | 0c348f5 | 2010-01-08 18:58:05 +1100 | [diff] [blame] | 480 | strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH); |
Darren Tucker | 046dff2 | 2003-10-08 17:32:02 +1000 | [diff] [blame] | 481 | fprintf(stderr, "%s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 482 | free(msg); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 483 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 484 | free(raw); |
| 485 | free(lang); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 486 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 487 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 488 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 489 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 490 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 491 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 492 | { |
| 493 | Authctxt *authctxt = ctxt; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 494 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 495 | if (authctxt == NULL) |
| 496 | fatal("input_userauth_success: no authentication context"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 497 | free(authctxt->authlist); |
| 498 | authctxt->authlist = NULL; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 499 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 500 | authctxt->method->cleanup(authctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 501 | free(authctxt->methoddata); |
| 502 | authctxt->methoddata = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 503 | authctxt->success = 1; /* break out */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 504 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 505 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 506 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 507 | int |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 508 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) |
| 509 | { |
| 510 | Authctxt *authctxt = ctxt; |
| 511 | |
| 512 | if (authctxt == NULL) |
| 513 | fatal("%s: no authentication context", __func__); |
| 514 | |
| 515 | fatal("Unexpected authentication success during %s.", |
| 516 | authctxt->method->name); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 517 | return 0; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 518 | } |
| 519 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 520 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 521 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 522 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 523 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 524 | Authctxt *authctxt = ctxt; |
| 525 | char *authlist = NULL; |
| 526 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 527 | |
| 528 | if (authctxt == NULL) |
| 529 | fatal("input_userauth_failure: no authentication context"); |
| 530 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 531 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 532 | partial = packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 533 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 534 | |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 535 | if (partial != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 536 | logit("Authenticated with partial success."); |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 537 | /* reset state */ |
| 538 | pubkey_cleanup(authctxt); |
| 539 | pubkey_prepare(authctxt); |
| 540 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 541 | debug("Authentications that can continue: %s", authlist); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 542 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 543 | userauth(authctxt, authlist); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 544 | return 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 545 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 546 | |
| 547 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 548 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 549 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 550 | { |
| 551 | Authctxt *authctxt = ctxt; |
| 552 | Key *key = NULL; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 553 | Identity *id = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 554 | Buffer b; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 555 | int pktype, sent = 0; |
| 556 | u_int alen, blen; |
| 557 | char *pkalg, *fp; |
| 558 | u_char *pkblob; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 559 | |
| 560 | if (authctxt == NULL) |
| 561 | fatal("input_userauth_pk_ok: no authentication context"); |
| 562 | if (datafellows & SSH_BUG_PKOK) { |
| 563 | /* this is similar to SSH_BUG_PKAUTH */ |
| 564 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 565 | pkblob = packet_get_string(&blen); |
| 566 | buffer_init(&b); |
| 567 | buffer_append(&b, pkblob, blen); |
| 568 | pkalg = buffer_get_string(&b, &alen); |
| 569 | buffer_free(&b); |
| 570 | } else { |
| 571 | pkalg = packet_get_string(&alen); |
| 572 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 573 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 574 | packet_check_eom(); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 575 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 576 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 577 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 578 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
| 579 | debug("unknown pkalg %s", pkalg); |
| 580 | goto done; |
| 581 | } |
| 582 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 583 | debug("no key from blob. pkalg %s", pkalg); |
| 584 | goto done; |
| 585 | } |
| 586 | if (key->type != pktype) { |
| 587 | error("input_userauth_pk_ok: type mismatch " |
| 588 | "for decoded key (received %d, expected %d)", |
| 589 | key->type, pktype); |
| 590 | goto done; |
| 591 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 592 | fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 593 | debug2("input_userauth_pk_ok: fp %s", fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 594 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 595 | |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 596 | /* |
| 597 | * search keys in the reverse order, because last candidate has been |
| 598 | * moved to the end of the queue. this also avoids confusion by |
| 599 | * duplicate keys |
| 600 | */ |
Damien Miller | 0b51a52 | 2004-04-20 20:07:19 +1000 | [diff] [blame] | 601 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 602 | if (key_equal(key, id->key)) { |
| 603 | sent = sign_and_send_pubkey(authctxt, id); |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | done: |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 608 | if (key != NULL) |
| 609 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 610 | free(pkalg); |
| 611 | free(pkblob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 612 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 613 | /* try another method if we did not send a packet */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 614 | if (sent == 0) |
| 615 | userauth(authctxt, NULL); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 616 | return 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 617 | } |
| 618 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 619 | #ifdef GSSAPI |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 620 | int |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 621 | userauth_gssapi(Authctxt *authctxt) |
| 622 | { |
| 623 | Gssctxt *gssctxt = NULL; |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 624 | static gss_OID_set gss_supported = NULL; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 625 | static u_int mech = 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 626 | OM_uint32 min; |
| 627 | int ok = 0; |
| 628 | |
| 629 | /* Try one GSSAPI method at a time, rather than sending them all at |
| 630 | * once. */ |
| 631 | |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 632 | if (gss_supported == NULL) |
| 633 | gss_indicate_mechs(&min, &gss_supported); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 634 | |
| 635 | /* Check to see if the mechanism is usable before we offer it */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 636 | while (mech < gss_supported->count && !ok) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 637 | /* My DER encoding requires length<128 */ |
Darren Tucker | 56afe14 | 2003-11-03 20:06:14 +1100 | [diff] [blame] | 638 | if (gss_supported->elements[mech].length < 128 && |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 639 | ssh_gssapi_check_mechanism(&gssctxt, |
| 640 | &gss_supported->elements[mech], authctxt->host)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 641 | ok = 1; /* Mechanism works */ |
| 642 | } else { |
| 643 | mech++; |
| 644 | } |
| 645 | } |
| 646 | |
Damien Miller | a1cb9f3 | 2006-08-19 00:33:34 +1000 | [diff] [blame] | 647 | if (!ok) |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 648 | return 0; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 649 | |
| 650 | authctxt->methoddata=(void *)gssctxt; |
| 651 | |
| 652 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 653 | packet_put_cstring(authctxt->server_user); |
| 654 | packet_put_cstring(authctxt->service); |
| 655 | packet_put_cstring(authctxt->method->name); |
| 656 | |
| 657 | packet_put_int(1); |
| 658 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 659 | packet_put_int((gss_supported->elements[mech].length) + 2); |
| 660 | packet_put_char(SSH_GSS_OIDTYPE); |
| 661 | packet_put_char(gss_supported->elements[mech].length); |
| 662 | packet_put_raw(gss_supported->elements[mech].elements, |
| 663 | gss_supported->elements[mech].length); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 664 | |
| 665 | packet_send(); |
| 666 | |
| 667 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); |
| 668 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); |
| 669 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); |
| 670 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); |
| 671 | |
| 672 | mech++; /* Move along to next candidate */ |
| 673 | |
| 674 | return 1; |
| 675 | } |
| 676 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 677 | static OM_uint32 |
| 678 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) |
| 679 | { |
| 680 | Authctxt *authctxt = ctxt; |
| 681 | Gssctxt *gssctxt = authctxt->methoddata; |
| 682 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
Damien Miller | da9984f | 2005-08-31 19:46:26 +1000 | [diff] [blame] | 683 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
| 684 | gss_buffer_desc gssbuf; |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 685 | OM_uint32 status, ms, flags; |
| 686 | Buffer b; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 687 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 688 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 689 | recv_tok, &send_tok, &flags); |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 690 | |
| 691 | if (send_tok.length > 0) { |
| 692 | if (GSS_ERROR(status)) |
| 693 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); |
| 694 | else |
| 695 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 696 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 697 | packet_put_string(send_tok.value, send_tok.length); |
| 698 | packet_send(); |
| 699 | gss_release_buffer(&ms, &send_tok); |
| 700 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 701 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 702 | if (status == GSS_S_COMPLETE) { |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 703 | /* send either complete or MIC, depending on mechanism */ |
| 704 | if (!(flags & GSS_C_INTEG_FLAG)) { |
| 705 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); |
| 706 | packet_send(); |
| 707 | } else { |
| 708 | ssh_gssapi_buildmic(&b, authctxt->server_user, |
| 709 | authctxt->service, "gssapi-with-mic"); |
| 710 | |
| 711 | gssbuf.value = buffer_ptr(&b); |
| 712 | gssbuf.length = buffer_len(&b); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 713 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 714 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 715 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 716 | if (!GSS_ERROR(status)) { |
| 717 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); |
| 718 | packet_put_string(mic.value, mic.length); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 719 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 720 | packet_send(); |
| 721 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 722 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 723 | buffer_free(&b); |
| 724 | gss_release_buffer(&ms, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 725 | } |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 726 | } |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 727 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 728 | return status; |
| 729 | } |
| 730 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 731 | /* ARGSUSED */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 732 | void |
| 733 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) |
| 734 | { |
| 735 | Authctxt *authctxt = ctxt; |
| 736 | Gssctxt *gssctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 737 | int oidlen; |
| 738 | char *oidv; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 739 | |
| 740 | if (authctxt == NULL) |
| 741 | fatal("input_gssapi_response: no authentication context"); |
| 742 | gssctxt = authctxt->methoddata; |
| 743 | |
| 744 | /* Setup our OID */ |
| 745 | oidv = packet_get_string(&oidlen); |
| 746 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 747 | if (oidlen <= 2 || |
| 748 | oidv[0] != SSH_GSS_OIDTYPE || |
| 749 | oidv[1] != oidlen - 2) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 750 | free(oidv); |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 751 | debug("Badly encoded mechanism OID received"); |
| 752 | userauth(authctxt, NULL); |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 753 | return; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 754 | } |
| 755 | |
Darren Tucker | 655a5e0 | 2003-11-03 20:09:03 +1100 | [diff] [blame] | 756 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
| 757 | fatal("Server returned different OID than expected"); |
| 758 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 759 | packet_check_eom(); |
| 760 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 761 | free(oidv); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 762 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 763 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 764 | /* Start again with next method on list */ |
| 765 | debug("Trying to start again"); |
| 766 | userauth(authctxt, NULL); |
| 767 | return; |
| 768 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 769 | } |
| 770 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 771 | /* ARGSUSED */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 772 | void |
| 773 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) |
| 774 | { |
| 775 | Authctxt *authctxt = ctxt; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 776 | gss_buffer_desc recv_tok; |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 777 | OM_uint32 status; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 778 | u_int slen; |
| 779 | |
| 780 | if (authctxt == NULL) |
| 781 | fatal("input_gssapi_response: no authentication context"); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 782 | |
| 783 | recv_tok.value = packet_get_string(&slen); |
| 784 | recv_tok.length = slen; /* safe typecast */ |
| 785 | |
| 786 | packet_check_eom(); |
| 787 | |
Damien Miller | 91c6aa4 | 2003-11-17 21:20:18 +1100 | [diff] [blame] | 788 | status = process_gssapi_token(ctxt, &recv_tok); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 789 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 790 | free(recv_tok.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 791 | |
| 792 | if (GSS_ERROR(status)) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 793 | /* Start again with the next method in the list */ |
| 794 | userauth(authctxt, NULL); |
| 795 | return; |
| 796 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 797 | } |
| 798 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 799 | /* ARGSUSED */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 800 | void |
| 801 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) |
| 802 | { |
| 803 | Authctxt *authctxt = ctxt; |
| 804 | Gssctxt *gssctxt; |
| 805 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; |
| 806 | gss_buffer_desc recv_tok; |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 807 | OM_uint32 ms; |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 808 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 809 | |
| 810 | if (authctxt == NULL) |
| 811 | fatal("input_gssapi_response: no authentication context"); |
| 812 | gssctxt = authctxt->methoddata; |
| 813 | |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 814 | recv_tok.value = packet_get_string(&len); |
| 815 | recv_tok.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 816 | |
| 817 | packet_check_eom(); |
| 818 | |
| 819 | /* Stick it into GSSAPI and see what it says */ |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 820 | (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 821 | &recv_tok, &send_tok, NULL); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 822 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 823 | free(recv_tok.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 824 | gss_release_buffer(&ms, &send_tok); |
| 825 | |
| 826 | /* Server will be returning a failed packet after this one */ |
| 827 | } |
| 828 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 829 | /* ARGSUSED */ |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 830 | void |
| 831 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) |
| 832 | { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 833 | char *msg; |
| 834 | char *lang; |
| 835 | |
Damien Miller | d677ad1 | 2013-04-23 15:18:51 +1000 | [diff] [blame] | 836 | /* maj */(void)packet_get_int(); |
| 837 | /* min */(void)packet_get_int(); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 838 | msg=packet_get_string(NULL); |
| 839 | lang=packet_get_string(NULL); |
| 840 | |
| 841 | packet_check_eom(); |
| 842 | |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 843 | debug("Server GSSAPI Error:\n%s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 844 | free(msg); |
| 845 | free(lang); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 846 | } |
| 847 | #endif /* GSSAPI */ |
| 848 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 849 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 850 | userauth_none(Authctxt *authctxt) |
| 851 | { |
| 852 | /* initial userauth request */ |
| 853 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 854 | packet_put_cstring(authctxt->server_user); |
| 855 | packet_put_cstring(authctxt->service); |
| 856 | packet_put_cstring(authctxt->method->name); |
| 857 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 862 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 863 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 864 | static int attempt = 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 865 | char prompt[150]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 866 | char *password; |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 867 | const char *host = options.host_key_alias ? options.host_key_alias : |
| 868 | authctxt->host; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 869 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 870 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 871 | return 0; |
| 872 | |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 873 | if (attempt != 1) |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 874 | error("Permission denied, please try again."); |
| 875 | |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 876 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 877 | authctxt->server_user, host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 878 | password = read_passphrase(prompt, 0); |
| 879 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 880 | packet_put_cstring(authctxt->server_user); |
| 881 | packet_put_cstring(authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 882 | packet_put_cstring(authctxt->method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 883 | packet_put_char(0); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 884 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 885 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 886 | free(password); |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 887 | packet_add_padding(64); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 888 | packet_send(); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 889 | |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 890 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 891 | &input_userauth_passwd_changereq); |
| 892 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 893 | return 1; |
| 894 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 895 | |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 896 | /* |
| 897 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST |
| 898 | */ |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 899 | /* ARGSUSED */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 900 | int |
Tim Rice | c854962 | 2002-03-31 12:49:38 -0800 | [diff] [blame] | 901 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 902 | { |
| 903 | Authctxt *authctxt = ctxt; |
| 904 | char *info, *lang, *password = NULL, *retype = NULL; |
| 905 | char prompt[150]; |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 906 | const char *host = options.host_key_alias ? options.host_key_alias : |
| 907 | authctxt->host; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 908 | |
| 909 | debug2("input_userauth_passwd_changereq"); |
| 910 | |
| 911 | if (authctxt == NULL) |
| 912 | fatal("input_userauth_passwd_changereq: " |
| 913 | "no authentication context"); |
| 914 | |
| 915 | info = packet_get_string(NULL); |
| 916 | lang = packet_get_string(NULL); |
| 917 | if (strlen(info) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 918 | logit("%s", info); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 919 | free(info); |
| 920 | free(lang); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 921 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 922 | packet_put_cstring(authctxt->server_user); |
| 923 | packet_put_cstring(authctxt->service); |
| 924 | packet_put_cstring(authctxt->method->name); |
| 925 | packet_put_char(1); /* additional info */ |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 926 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 927 | "Enter %.30s@%.128s's old password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 928 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 929 | password = read_passphrase(prompt, 0); |
| 930 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 931 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 932 | free(password); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 933 | password = NULL; |
| 934 | while (password == NULL) { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 935 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 936 | "Enter %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 937 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 938 | password = read_passphrase(prompt, RP_ALLOW_EOF); |
| 939 | if (password == NULL) { |
| 940 | /* bail out */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 941 | return 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 942 | } |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 943 | snprintf(prompt, sizeof(prompt), |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 944 | "Retype %.30s@%.128s's new password: ", |
Darren Tucker | ab79169 | 2010-01-08 18:48:02 +1100 | [diff] [blame] | 945 | authctxt->server_user, host); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 946 | retype = read_passphrase(prompt, 0); |
| 947 | if (strcmp(password, retype) != 0) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 948 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 949 | free(password); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 950 | logit("Mismatch; try again, EOF to quit."); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 951 | password = NULL; |
| 952 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 953 | explicit_bzero(retype, strlen(retype)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 954 | free(retype); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 955 | } |
| 956 | packet_put_cstring(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 957 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 958 | free(password); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 959 | packet_add_padding(64); |
| 960 | packet_send(); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 961 | |
| 962 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 963 | &input_userauth_passwd_changereq); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 964 | return 0; |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 965 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 966 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 967 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 968 | identity_sign(struct identity *id, u_char **sigp, size_t *lenp, |
| 969 | const u_char *data, size_t datalen, u_int compat) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 970 | { |
| 971 | Key *prv; |
| 972 | int ret; |
| 973 | |
| 974 | /* the agent supports this key */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 975 | if (id->agent_fd) |
| 976 | return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, |
| 977 | data, datalen, compat); |
| 978 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 979 | /* |
| 980 | * we have already loaded the private key or |
| 981 | * the private key is stored in external hardware |
| 982 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 983 | if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 984 | return (sshkey_sign(id->key, sigp, lenp, data, datalen, |
| 985 | compat)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 986 | /* load the private key from the file */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 987 | if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 988 | return (-1); /* XXX return decent error code */ |
| 989 | ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat); |
| 990 | sshkey_free(prv); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 991 | return (ret); |
| 992 | } |
| 993 | |
| 994 | static int |
| 995 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 996 | { |
| 997 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 998 | u_char *blob, *signature; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 999 | u_int bloblen; |
| 1000 | size_t slen; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 1001 | u_int skip = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1002 | int ret = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1003 | int have_sig = 1; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1004 | char *fp; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1005 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1006 | fp = key_fingerprint(id->key, options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1007 | debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1008 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1009 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1010 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1011 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1012 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1015 | /* data to be signed */ |
| 1016 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1017 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1018 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1019 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1020 | } else { |
| 1021 | buffer_put_string(&b, session_id2, session_id2_len); |
| 1022 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1023 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1024 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1025 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1026 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1027 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1028 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1029 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1030 | if (datafellows & SSH_BUG_PKAUTH) { |
| 1031 | buffer_put_char(&b, have_sig); |
| 1032 | } else { |
| 1033 | buffer_put_cstring(&b, authctxt->method->name); |
| 1034 | buffer_put_char(&b, have_sig); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1035 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1036 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1037 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1038 | |
| 1039 | /* generate signature */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1040 | ret = identity_sign(id, &signature, &slen, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1041 | buffer_ptr(&b), buffer_len(&b), datafellows); |
| 1042 | if (ret != 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1043 | free(blob); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1044 | buffer_free(&b); |
| 1045 | return 0; |
| 1046 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1047 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1048 | buffer_dump(&b); |
| 1049 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1050 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1051 | buffer_clear(&b); |
| 1052 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1053 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1054 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1055 | buffer_put_cstring(&b, authctxt->server_user); |
| 1056 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1057 | buffer_put_cstring(&b, authctxt->method->name); |
| 1058 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1059 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1060 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1061 | buffer_put_string(&b, blob, bloblen); |
| 1062 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1063 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1064 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1065 | /* append signature */ |
| 1066 | buffer_put_string(&b, signature, slen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1067 | free(signature); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1068 | |
| 1069 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1070 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1071 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1072 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1073 | |
| 1074 | /* put remaining data from buffer into packet */ |
| 1075 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1076 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 1077 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1078 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1079 | |
| 1080 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1081 | } |
| 1082 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1083 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1084 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1085 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1086 | u_char *blob; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1087 | u_int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1088 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1089 | debug3("send_pubkey_test"); |
| 1090 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1091 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1092 | /* we cannot handle this key */ |
| 1093 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1096 | /* register callback for USERAUTH_PK_OK message */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1097 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1098 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1099 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1100 | packet_put_cstring(authctxt->server_user); |
| 1101 | packet_put_cstring(authctxt->service); |
| 1102 | packet_put_cstring(authctxt->method->name); |
| 1103 | packet_put_char(have_sig); |
| 1104 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1105 | packet_put_cstring(key_ssh_name(id->key)); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1106 | packet_put_string(blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1107 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1108 | packet_send(); |
| 1109 | return 1; |
| 1110 | } |
| 1111 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1112 | static Key * |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1113 | load_identity_file(char *filename, int userprovided) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1114 | { |
| 1115 | Key *private; |
| 1116 | char prompt[300], *passphrase; |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1117 | int r, perm_ok = 0, quit = 0, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1118 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1119 | |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1120 | if (stat(filename, &st) < 0) { |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1121 | (userprovided ? logit : debug3)("no such identity: %s: %s", |
| 1122 | filename, strerror(errno)); |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1123 | return NULL; |
| 1124 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1125 | snprintf(prompt, sizeof prompt, |
| 1126 | "Enter passphrase for key '%.100s': ", filename); |
| 1127 | for (i = 0; i <= options.number_of_password_prompts; i++) { |
| 1128 | if (i == 0) |
| 1129 | passphrase = ""; |
| 1130 | else { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1131 | passphrase = read_passphrase(prompt, 0); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1132 | if (*passphrase == '\0') { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1133 | debug2("no passphrase given, try next key"); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1134 | free(passphrase); |
| 1135 | break; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1136 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1137 | } |
| 1138 | switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename, |
| 1139 | passphrase, &private, NULL, &perm_ok))) { |
| 1140 | case 0: |
| 1141 | break; |
| 1142 | case SSH_ERR_KEY_WRONG_PASSPHRASE: |
| 1143 | if (options.batch_mode) { |
| 1144 | quit = 1; |
| 1145 | break; |
| 1146 | } |
djm@openbsd.org | f14564c | 2015-01-15 11:04:36 +0000 | [diff] [blame] | 1147 | if (i != 0) |
| 1148 | debug2("bad passphrase given, try again..."); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | case SSH_ERR_SYSTEM_ERROR: |
| 1151 | if (errno == ENOENT) { |
| 1152 | debug2("Load key \"%s\": %s", |
| 1153 | filename, ssh_err(r)); |
| 1154 | quit = 1; |
| 1155 | break; |
| 1156 | } |
| 1157 | /* FALLTHROUGH */ |
| 1158 | default: |
| 1159 | error("Load key \"%s\": %s", filename, ssh_err(r)); |
| 1160 | quit = 1; |
| 1161 | break; |
| 1162 | } |
| 1163 | if (i > 0) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1164 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1165 | free(passphrase); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1166 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1167 | if (private != NULL || quit) |
| 1168 | break; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1169 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1170 | return private; |
| 1171 | } |
| 1172 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1173 | /* |
| 1174 | * try keys in the following order: |
| 1175 | * 1. agent keys that are found in the config file |
| 1176 | * 2. other agent keys |
| 1177 | * 3. keys that are only listed in the config file |
| 1178 | */ |
| 1179 | static void |
| 1180 | pubkey_prepare(Authctxt *authctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1181 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1182 | struct identity *id, *id2, *tmp; |
| 1183 | struct idlist agent, files, *preferred; |
| 1184 | struct sshkey *key; |
| 1185 | int agent_fd, i, r, found; |
| 1186 | size_t j; |
| 1187 | struct ssh_identitylist *idlist; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1188 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1189 | TAILQ_INIT(&agent); /* keys from the agent */ |
| 1190 | TAILQ_INIT(&files); /* keys from the config file */ |
| 1191 | preferred = &authctxt->keys; |
| 1192 | TAILQ_INIT(preferred); /* preferred order of keys */ |
| 1193 | |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1194 | /* list of keys stored in the filesystem and PKCS#11 */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1195 | for (i = 0; i < options.num_identity_files; i++) { |
| 1196 | key = options.identity_keys[i]; |
| 1197 | if (key && key->type == KEY_RSA1) |
| 1198 | continue; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1199 | if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) |
| 1200 | continue; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1201 | options.identity_keys[i] = NULL; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1202 | id = xcalloc(1, sizeof(*id)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1203 | id->key = key; |
| 1204 | id->filename = xstrdup(options.identity_files[i]); |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1205 | id->userprovided = options.identity_file_userprovided[i]; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1206 | TAILQ_INSERT_TAIL(&files, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1207 | } |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1208 | /* Prefer PKCS11 keys that are explicitly listed */ |
| 1209 | TAILQ_FOREACH_SAFE(id, &files, next, tmp) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1210 | if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0) |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1211 | continue; |
| 1212 | found = 0; |
| 1213 | TAILQ_FOREACH(id2, &files, next) { |
| 1214 | if (id2->key == NULL || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1215 | (id2->key->flags & SSHKEY_FLAG_EXT) == 0) |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1216 | continue; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1217 | if (sshkey_equal(id->key, id2->key)) { |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1218 | TAILQ_REMOVE(&files, id, next); |
| 1219 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1220 | found = 1; |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | /* If IdentitiesOnly set and key not found then don't use it */ |
| 1225 | if (!found && options.identities_only) { |
| 1226 | TAILQ_REMOVE(&files, id, next); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 1227 | explicit_bzero(id, sizeof(*id)); |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1228 | free(id); |
| 1229 | } |
| 1230 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1231 | /* list of keys supported by the agent */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1232 | if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { |
| 1233 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 1234 | debug("%s: ssh_get_authentication_socket: %s", |
| 1235 | __func__, ssh_err(r)); |
| 1236 | } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) { |
| 1237 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 1238 | debug("%s: ssh_fetch_identitylist: %s", |
| 1239 | __func__, ssh_err(r)); |
| 1240 | } else { |
| 1241 | for (j = 0; j < idlist->nkeys; j++) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1242 | found = 0; |
| 1243 | TAILQ_FOREACH(id, &files, next) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1244 | /* |
| 1245 | * agent keys from the config file are |
| 1246 | * preferred |
| 1247 | */ |
| 1248 | if (sshkey_equal(idlist->keys[j], id->key)) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1249 | TAILQ_REMOVE(&files, id, next); |
| 1250 | TAILQ_INSERT_TAIL(preferred, id, next); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1251 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1252 | found = 1; |
| 1253 | break; |
| 1254 | } |
| 1255 | } |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1256 | if (!found && !options.identities_only) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1257 | id = xcalloc(1, sizeof(*id)); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1258 | /* XXX "steals" key/comment from idlist */ |
| 1259 | id->key = idlist->keys[j]; |
| 1260 | id->filename = idlist->comments[j]; |
| 1261 | idlist->keys[j] = NULL; |
| 1262 | idlist->comments[j] = NULL; |
| 1263 | id->agent_fd = agent_fd; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1264 | TAILQ_INSERT_TAIL(&agent, id, next); |
| 1265 | } |
| 1266 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1267 | ssh_free_identitylist(idlist); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1268 | /* append remaining agent keys */ |
| 1269 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
| 1270 | TAILQ_REMOVE(&agent, id, next); |
| 1271 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1272 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1273 | authctxt->agent_fd = agent_fd; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1274 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1275 | /* append remaining keys from the config file */ |
| 1276 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
| 1277 | TAILQ_REMOVE(&files, id, next); |
| 1278 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1279 | } |
| 1280 | TAILQ_FOREACH(id, preferred, next) { |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1281 | debug2("key: %s (%p),%s", id->filename, id->key, |
| 1282 | id->userprovided ? " explicit" : ""); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | static void |
| 1287 | pubkey_cleanup(Authctxt *authctxt) |
| 1288 | { |
| 1289 | Identity *id; |
| 1290 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1291 | if (authctxt->agent_fd != -1) |
| 1292 | ssh_close_authentication_socket(authctxt->agent_fd); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1293 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
| 1294 | id = TAILQ_FIRST(&authctxt->keys)) { |
| 1295 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1296 | if (id->key) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 1297 | sshkey_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1298 | free(id->filename); |
| 1299 | free(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1300 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1301 | } |
| 1302 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1303 | int |
| 1304 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1305 | { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1306 | Identity *id; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1307 | int sent = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1308 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1309 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
| 1310 | if (id->tried++) |
| 1311 | return (0); |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 1312 | /* move key to the end of the queue */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1313 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1314 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); |
| 1315 | /* |
| 1316 | * send a test message if we have the public key. for |
| 1317 | * encrypted keys we cannot do this and have to load the |
| 1318 | * private key instead |
| 1319 | */ |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1320 | if (id->key != NULL) { |
| 1321 | if (key_type_plain(id->key->type) == KEY_RSA && |
| 1322 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 1323 | debug("Skipped %s key %s for RSA/MD5 server", |
| 1324 | key_type(id->key), id->filename); |
| 1325 | } else if (id->key->type != KEY_RSA1) { |
| 1326 | debug("Offering %s public key: %s", |
| 1327 | key_type(id->key), id->filename); |
| 1328 | sent = send_pubkey_test(authctxt, id); |
| 1329 | } |
| 1330 | } else { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1331 | debug("Trying private key: %s", id->filename); |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1332 | id->key = load_identity_file(id->filename, |
| 1333 | id->userprovided); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1334 | if (id->key != NULL) { |
| 1335 | id->isprivate = 1; |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 1336 | if (key_type_plain(id->key->type) == KEY_RSA && |
| 1337 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 1338 | debug("Skipped %s key %s for RSA/MD5 " |
| 1339 | "server", key_type(id->key), |
| 1340 | id->filename); |
| 1341 | } else { |
| 1342 | sent = sign_and_send_pubkey( |
| 1343 | authctxt, id); |
| 1344 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1345 | key_free(id->key); |
| 1346 | id->key = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1347 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1348 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1349 | if (sent) |
| 1350 | return (sent); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1351 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1352 | return (0); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1353 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1354 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1355 | /* |
| 1356 | * Send userauth request message specifying keyboard-interactive method. |
| 1357 | */ |
| 1358 | int |
| 1359 | userauth_kbdint(Authctxt *authctxt) |
| 1360 | { |
| 1361 | static int attempt = 0; |
| 1362 | |
| 1363 | if (attempt++ >= options.number_of_password_prompts) |
| 1364 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1365 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 1366 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 1367 | debug3("userauth_kbdint: disable: no info_req_seen"); |
| 1368 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
| 1369 | return 0; |
| 1370 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1371 | |
| 1372 | debug2("userauth_kbdint"); |
| 1373 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1374 | packet_put_cstring(authctxt->server_user); |
| 1375 | packet_put_cstring(authctxt->service); |
| 1376 | packet_put_cstring(authctxt->method->name); |
| 1377 | packet_put_cstring(""); /* lang */ |
| 1378 | packet_put_cstring(options.kbd_interactive_devices ? |
| 1379 | options.kbd_interactive_devices : ""); |
| 1380 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1381 | |
| 1382 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | |
| 1386 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1387 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1388 | */ |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 1389 | int |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1390 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1391 | { |
| 1392 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1393 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1394 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1395 | int echo = 0; |
| 1396 | |
| 1397 | debug2("input_userauth_info_req"); |
| 1398 | |
| 1399 | if (authctxt == NULL) |
| 1400 | fatal("input_userauth_info_req: no authentication context"); |
| 1401 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1402 | authctxt->info_req_seen = 1; |
| 1403 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1404 | name = packet_get_string(NULL); |
| 1405 | inst = packet_get_string(NULL); |
| 1406 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1407 | if (strlen(name) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1408 | logit("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1409 | if (strlen(inst) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1410 | logit("%s", inst); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1411 | free(name); |
| 1412 | free(inst); |
| 1413 | free(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1414 | |
| 1415 | num_prompts = packet_get_int(); |
| 1416 | /* |
| 1417 | * Begin to build info response packet based on prompts requested. |
| 1418 | * We commit to providing the correct number of responses, so if |
| 1419 | * further on we run into a problem that prevents this, we have to |
| 1420 | * be sure and clean this up and send a correct error response. |
| 1421 | */ |
| 1422 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 1423 | packet_put_int(num_prompts); |
| 1424 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1425 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1426 | for (i = 0; i < num_prompts; i++) { |
| 1427 | prompt = packet_get_string(NULL); |
| 1428 | echo = packet_get_char(); |
| 1429 | |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1430 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1431 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1432 | packet_put_cstring(response); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1433 | explicit_bzero(response, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1434 | free(response); |
| 1435 | free(prompt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1436 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1437 | packet_check_eom(); /* done with parsing incoming message. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1438 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1439 | packet_add_padding(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1440 | packet_send(); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame^] | 1441 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1442 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1443 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1444 | static int |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1445 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1446 | u_char *data, u_int datalen) |
| 1447 | { |
| 1448 | Buffer b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1449 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1450 | pid_t pid; |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1451 | int to[2], from[2], status, version = 2; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1452 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1453 | debug2("ssh_keysign called"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1454 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1455 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1456 | error("ssh_keysign: not installed: %s", strerror(errno)); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1457 | return -1; |
| 1458 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1459 | if (fflush(stdout) != 0) |
| 1460 | error("ssh_keysign: fflush: %s", strerror(errno)); |
| 1461 | if (pipe(to) < 0) { |
| 1462 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | if (pipe(from) < 0) { |
| 1466 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1467 | return -1; |
| 1468 | } |
| 1469 | if ((pid = fork()) < 0) { |
| 1470 | error("ssh_keysign: fork: %s", strerror(errno)); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | if (pid == 0) { |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 1474 | /* keep the socket on exec */ |
| 1475 | fcntl(packet_get_connection_in(), F_SETFD, 0); |
Damien Miller | 2e5fe88 | 2006-06-13 13:10:00 +1000 | [diff] [blame] | 1476 | permanently_drop_suid(getuid()); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1477 | close(from[0]); |
| 1478 | if (dup2(from[1], STDOUT_FILENO) < 0) |
| 1479 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
| 1480 | close(to[1]); |
| 1481 | if (dup2(to[0], STDIN_FILENO) < 0) |
| 1482 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1483 | close(from[1]); |
| 1484 | close(to[0]); |
Ben Lindstrom | 4887da2 | 2002-06-06 20:05:57 +0000 | [diff] [blame] | 1485 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1486 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, |
| 1487 | strerror(errno)); |
| 1488 | } |
| 1489 | close(from[1]); |
| 1490 | close(to[0]); |
| 1491 | |
| 1492 | buffer_init(&b); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1493 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1494 | buffer_put_string(&b, data, datalen); |
Damien Miller | 51bf11f | 2003-11-17 21:20:47 +1100 | [diff] [blame] | 1495 | if (ssh_msg_send(to[1], version, &b) == -1) |
| 1496 | fatal("ssh_keysign: couldn't send request"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1497 | |
Damien Miller | 901119b | 2002-10-04 11:10:04 +1000 | [diff] [blame] | 1498 | if (ssh_msg_recv(from[0], &b) < 0) { |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1499 | error("ssh_keysign: no reply"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1500 | buffer_free(&b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1503 | close(from[0]); |
| 1504 | close(to[1]); |
| 1505 | |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1506 | while (waitpid(pid, &status, 0) < 0) |
| 1507 | if (errno != EINTR) |
| 1508 | break; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1509 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1510 | if (buffer_get_char(&b) != version) { |
| 1511 | error("ssh_keysign: bad version"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1512 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1513 | return -1; |
| 1514 | } |
| 1515 | *sigp = buffer_get_string(&b, lenp); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1516 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1517 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1518 | return 0; |
| 1519 | } |
| 1520 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1521 | int |
| 1522 | userauth_hostbased(Authctxt *authctxt) |
| 1523 | { |
| 1524 | Key *private = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1525 | Sensitive *sensitive = authctxt->sensitive; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1526 | Buffer b; |
| 1527 | u_char *signature, *blob; |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1528 | char *chost, *pkalg, *p; |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1529 | const char *service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1530 | u_int blen, slen; |
Darren Tucker | 5246df4 | 2010-01-08 18:50:46 +1100 | [diff] [blame] | 1531 | int ok, i, found = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1532 | |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 1533 | /* XXX provide some way to allow user to specify key types attempted */ |
| 1534 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1535 | /* check for a useful key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1536 | for (i = 0; i < sensitive->nkeys; i++) { |
| 1537 | private = sensitive->keys[i]; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1538 | if (private && private->type != KEY_RSA1) { |
| 1539 | found = 1; |
| 1540 | /* we take and free the key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1541 | sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | if (!found) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1546 | debug("No more client hostkeys for hostbased authentication."); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1547 | return 0; |
| 1548 | } |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1549 | |
| 1550 | debug("%s: trying hostkey type %s", __func__, key_type(private)); |
| 1551 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1552 | if (key_to_blob(private, &blob, &blen) == 0) { |
| 1553 | key_free(private); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
djm@openbsd.org | 17bf3d8 | 2014-12-11 05:13:28 +0000 | [diff] [blame] | 1556 | |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1557 | /* figure out a name for the client host */ |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1558 | p = get_local_name(packet_get_connection_in()); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1559 | if (p == NULL) { |
| 1560 | error("userauth_hostbased: cannot get local ipaddr/name"); |
| 1561 | key_free(private); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1562 | free(blob); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1565 | xasprintf(&chost, "%s.", p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1566 | debug2("userauth_hostbased: chost %s", chost); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1567 | free(p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1568 | |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1569 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 1570 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1571 | pkalg = xstrdup(key_ssh_name(private)); |
| 1572 | buffer_init(&b); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1573 | /* construct data */ |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1574 | buffer_put_string(&b, session_id2, session_id2_len); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1575 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 1576 | buffer_put_cstring(&b, authctxt->server_user); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1577 | buffer_put_cstring(&b, service); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1578 | buffer_put_cstring(&b, authctxt->method->name); |
| 1579 | buffer_put_cstring(&b, pkalg); |
| 1580 | buffer_put_string(&b, blob, blen); |
| 1581 | buffer_put_cstring(&b, chost); |
| 1582 | buffer_put_cstring(&b, authctxt->local_user); |
| 1583 | #ifdef DEBUG_PK |
| 1584 | buffer_dump(&b); |
| 1585 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1586 | if (sensitive->external_keysign) |
| 1587 | ok = ssh_keysign(private, &signature, &slen, |
| 1588 | buffer_ptr(&b), buffer_len(&b)); |
| 1589 | else |
| 1590 | ok = key_sign(private, &signature, &slen, |
| 1591 | buffer_ptr(&b), buffer_len(&b)); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1592 | key_free(private); |
| 1593 | buffer_free(&b); |
| 1594 | if (ok != 0) { |
| 1595 | error("key_sign failed"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1596 | free(chost); |
| 1597 | free(pkalg); |
| 1598 | free(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1602 | packet_put_cstring(authctxt->server_user); |
| 1603 | packet_put_cstring(authctxt->service); |
| 1604 | packet_put_cstring(authctxt->method->name); |
| 1605 | packet_put_cstring(pkalg); |
| 1606 | packet_put_string(blob, blen); |
| 1607 | packet_put_cstring(chost); |
| 1608 | packet_put_cstring(authctxt->local_user); |
| 1609 | packet_put_string(signature, slen); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1610 | explicit_bzero(signature, slen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1611 | free(signature); |
| 1612 | free(chost); |
| 1613 | free(pkalg); |
| 1614 | free(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1615 | |
| 1616 | packet_send(); |
| 1617 | return 1; |
| 1618 | } |
| 1619 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1620 | /* find auth method */ |
| 1621 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1622 | /* |
| 1623 | * given auth method name, if configurable options permit this method fill |
| 1624 | * in auth_ident field and return true, otherwise return false. |
| 1625 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1626 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1627 | authmethod_is_enabled(Authmethod *method) |
| 1628 | { |
| 1629 | if (method == NULL) |
| 1630 | return 0; |
| 1631 | /* return false if options indicate this method is disabled */ |
| 1632 | if (method->enabled == NULL || *method->enabled == 0) |
| 1633 | return 0; |
| 1634 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1635 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1636 | return 0; |
| 1637 | return 1; |
| 1638 | } |
| 1639 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1640 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1641 | authmethod_lookup(const char *name) |
| 1642 | { |
| 1643 | Authmethod *method = NULL; |
| 1644 | if (name != NULL) |
| 1645 | for (method = authmethods; method->name != NULL; method++) |
| 1646 | if (strcmp(name, method->name) == 0) |
| 1647 | return method; |
| 1648 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1652 | /* XXX internal state */ |
| 1653 | static Authmethod *current = NULL; |
| 1654 | static char *supported = NULL; |
| 1655 | static char *preferred = NULL; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1656 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1657 | /* |
| 1658 | * Given the authentication method list sent by the server, return the |
| 1659 | * 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] | 1660 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1661 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1662 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1663 | authmethod_get(char *authlist) |
| 1664 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1665 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1666 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1667 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1668 | /* Use a suitable default if we're passed a nil list. */ |
| 1669 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1670 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1671 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1672 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1673 | debug3("start over, passed a different list %s", authlist); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1674 | free(supported); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1675 | supported = xstrdup(authlist); |
| 1676 | preferred = options.preferred_authentications; |
| 1677 | debug3("preferred %s", preferred); |
| 1678 | current = NULL; |
| 1679 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1680 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1681 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1682 | for (;;) { |
| 1683 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1684 | debug("No more authentication methods to try."); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1685 | current = NULL; |
| 1686 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1687 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1688 | preferred += next; |
| 1689 | debug3("authmethod_lookup %s", name); |
| 1690 | debug3("remaining preferred: %s", preferred); |
| 1691 | if ((current = authmethod_lookup(name)) != NULL && |
| 1692 | authmethod_is_enabled(current)) { |
| 1693 | debug3("authmethod_is_enabled %s", name); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1694 | debug("Next authentication method: %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1695 | free(name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1696 | return current; |
| 1697 | } |
Darren Tucker | e52a260 | 2013-06-06 08:22:05 +1000 | [diff] [blame] | 1698 | free(name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1699 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1700 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1701 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 1702 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1703 | authmethods_get(void) |
| 1704 | { |
| 1705 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1706 | Buffer b; |
| 1707 | char *list; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1708 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1709 | buffer_init(&b); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1710 | for (method = authmethods; method->name != NULL; method++) { |
| 1711 | if (authmethod_is_enabled(method)) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1712 | if (buffer_len(&b) > 0) |
| 1713 | buffer_append(&b, ",", 1); |
| 1714 | buffer_append(&b, method->name, strlen(method->name)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1715 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1716 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1717 | buffer_append(&b, "\0", 1); |
| 1718 | list = xstrdup(buffer_ptr(&b)); |
| 1719 | buffer_free(&b); |
| 1720 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1721 | } |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1722 | |