Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: sshconnect2.c,v 1.197 2013/05/17 00:13:14 djm Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 4 | * Copyright (c) 2008 Damien Miller. All rights reserved. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | 2eb6340 | 2006-03-15 11:09:42 +1100 | [diff] [blame] | 28 | |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 29 | #include <sys/types.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <sys/socket.h> |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 31 | #include <sys/wait.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 32 | #include <sys/stat.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 33 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 34 | #include <errno.h> |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 35 | #include <fcntl.h> |
Darren Tucker | f520ea1 | 2007-05-20 15:11:33 +1000 | [diff] [blame] | 36 | #include <netdb.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
| 38 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 39 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 40 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 41 | #include <string.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 42 | #include <unistd.h> |
Damien Miller | 63b4bcd | 2013-03-20 12:55:14 +1100 | [diff] [blame] | 43 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 44 | #include <vis.h> |
Damien Miller | 2e28d86 | 2008-07-17 19:15:43 +1000 | [diff] [blame] | 45 | #endif |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 46 | |
Damien Miller | 9c61769 | 2003-05-14 14:31:11 +1000 | [diff] [blame] | 47 | #include "openbsd-compat/sys-queue.h" |
| 48 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 50 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 52 | #include "buffer.h" |
| 53 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 55 | #include "cipher.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | #include "kex.h" |
| 58 | #include "myproposal.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 59 | #include "sshconnect.h" |
| 60 | #include "authfile.h" |
Ben Lindstrom | df22139 | 2001-03-29 00:36:16 +0000 | [diff] [blame] | 61 | #include "dh.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 62 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 63 | #include "log.h" |
| 64 | #include "readconf.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 65 | #include "misc.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" |
Damien Miller | cee8523 | 2009-03-06 00:58:22 +1100 | [diff] [blame] | 73 | #include "schnorr.h" |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 74 | #include "jpake.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 75 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 76 | #ifdef GSSAPI |
| 77 | #include "ssh-gss.h" |
| 78 | #endif |
| 79 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 80 | /* import */ |
| 81 | extern char *client_version_string; |
| 82 | extern char *server_version_string; |
| 83 | extern Options options; |
| 84 | |
| 85 | /* |
| 86 | * SSH2 key exchange |
| 87 | */ |
| 88 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 89 | u_char *session_id2 = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 90 | u_int session_id2_len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 91 | |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 92 | char *xxx_host; |
| 93 | struct sockaddr *xxx_hostaddr; |
| 94 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 95 | Kex *xxx_kex = NULL; |
| 96 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 97 | static int |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 98 | verify_host_key_callback(Key *hostkey) |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 99 | { |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 100 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 101 | fatal("Host key verification failed."); |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 105 | static char * |
| 106 | order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) |
| 107 | { |
| 108 | char *oavail, *avail, *first, *last, *alg, *hostname, *ret; |
| 109 | size_t maxlen; |
| 110 | struct hostkeys *hostkeys; |
| 111 | int ktype; |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 112 | u_int i; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 113 | |
| 114 | /* Find all hostkeys for this hostname */ |
| 115 | get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); |
| 116 | hostkeys = init_hostkeys(); |
Damien Miller | 295ee63 | 2011-05-29 21:42:31 +1000 | [diff] [blame] | 117 | for (i = 0; i < options.num_user_hostfiles; i++) |
| 118 | load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); |
| 119 | for (i = 0; i < options.num_system_hostfiles; i++) |
| 120 | load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 121 | |
| 122 | oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG); |
| 123 | maxlen = strlen(avail) + 1; |
| 124 | first = xmalloc(maxlen); |
| 125 | last = xmalloc(maxlen); |
| 126 | *first = *last = '\0'; |
| 127 | |
| 128 | #define ALG_APPEND(to, from) \ |
| 129 | do { \ |
| 130 | if (*to != '\0') \ |
| 131 | strlcat(to, ",", maxlen); \ |
| 132 | strlcat(to, from, maxlen); \ |
| 133 | } while (0) |
| 134 | |
| 135 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { |
| 136 | if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC) |
| 137 | fatal("%s: unknown alg %s", __func__, alg); |
| 138 | if (lookup_key_in_hostkeys_by_type(hostkeys, |
| 139 | key_type_plain(ktype), NULL)) |
| 140 | ALG_APPEND(first, alg); |
| 141 | else |
| 142 | ALG_APPEND(last, alg); |
| 143 | } |
| 144 | #undef ALG_APPEND |
| 145 | xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last); |
| 146 | if (*first != '\0') |
| 147 | debug3("%s: prefer hostkeyalgs: %s", __func__, first); |
| 148 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 149 | free(first); |
| 150 | free(last); |
| 151 | free(hostname); |
| 152 | free(oavail); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 153 | free_hostkeys(hostkeys); |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 158 | void |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 159 | ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 160 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 161 | Kex *kex; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 162 | |
| 163 | xxx_host = host; |
| 164 | xxx_hostaddr = hostaddr; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 165 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 166 | if (options.ciphers == (char *)-1) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 167 | logit("No valid ciphers for protocol version 2 given, using defaults."); |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 168 | options.ciphers = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 169 | } |
| 170 | if (options.ciphers != NULL) { |
| 171 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 172 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 173 | } |
Damien Miller | a0ff466 | 2001-03-30 10:49:35 +1000 | [diff] [blame] | 174 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 175 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); |
| 176 | myproposal[PROPOSAL_ENC_ALGS_STOC] = |
| 177 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 178 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 179 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 180 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 181 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 182 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 9786e6e | 2005-07-26 21:54:56 +1000 | [diff] [blame] | 183 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 184 | } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 185 | if (options.macs != NULL) { |
| 186 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 187 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 188 | } |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 189 | if (options.hostkeyalgorithms != NULL) |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 190 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
Ben Lindstrom | 982dbbc | 2001-04-17 18:11:36 +0000 | [diff] [blame] | 191 | options.hostkeyalgorithms; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 192 | else { |
| 193 | /* Prefer algorithms that we already have keys for */ |
| 194 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
| 195 | order_hostkeyalgs(host, hostaddr, port); |
| 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 | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 199 | |
Darren Tucker | c53c2af | 2013-05-16 20:28:16 +1000 | [diff] [blame] | 200 | if (options.rekey_limit || options.rekey_interval) |
| 201 | packet_set_rekey_limits((u_int32_t)options.rekey_limit, |
| 202 | (time_t)options.rekey_interval); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 203 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 204 | /* start key exchange */ |
Ben Lindstrom | 238abf6 | 2001-04-04 17:52:53 +0000 | [diff] [blame] | 205 | kex = kex_setup(myproposal); |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 206 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 207 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
Damien Miller | 8e7fb33 | 2003-02-24 12:03:03 +1100 | [diff] [blame] | 208 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 209 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 210 | kex->kex[KEX_ECDH_SHA2] = kexecdh_client; |
Ben Lindstrom | 20d7c7b | 2001-04-04 01:56:17 +0000 | [diff] [blame] | 211 | kex->client_version_string=client_version_string; |
| 212 | kex->server_version_string=server_version_string; |
Ben Lindstrom | d6481ea | 2001-06-25 04:37:41 +0000 | [diff] [blame] | 213 | kex->verify_host_key=&verify_host_key_callback; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | |
Ben Lindstrom | f28f634 | 2001-04-04 02:03:04 +0000 | [diff] [blame] | 215 | xxx_kex = kex; |
| 216 | |
Ben Lindstrom | be2cc43 | 2001-04-04 23:46:07 +0000 | [diff] [blame] | 217 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 218 | |
Darren Tucker | 36331b5 | 2010-01-08 16:50:41 +1100 | [diff] [blame] | 219 | if (options.use_roaming && !kex->roaming) { |
| 220 | debug("Roaming not allowed by server"); |
| 221 | options.use_roaming = 0; |
| 222 | } |
| 223 | |
Ben Lindstrom | 2d90e00 | 2001-04-04 02:00:54 +0000 | [diff] [blame] | 224 | session_id2 = kex->session_id; |
| 225 | session_id2_len = kex->session_id_len; |
| 226 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 227 | #ifdef DEBUG_KEXDH |
| 228 | /* send 1st encrypted/maced/compressed message */ |
| 229 | packet_start(SSH2_MSG_IGNORE); |
| 230 | packet_put_cstring("markus"); |
| 231 | packet_send(); |
| 232 | packet_write_wait(); |
| 233 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 234 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 235 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 236 | /* |
| 237 | * Authenticate user |
| 238 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 239 | |
| 240 | typedef struct Authctxt Authctxt; |
| 241 | typedef struct Authmethod Authmethod; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 242 | typedef struct identity Identity; |
| 243 | typedef struct idlist Idlist; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 244 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 245 | struct identity { |
| 246 | TAILQ_ENTRY(identity) next; |
| 247 | AuthenticationConnection *ac; /* set if agent supports key */ |
| 248 | Key *key; /* public/private key */ |
| 249 | char *filename; /* comment for agent-only keys */ |
| 250 | int tried; |
| 251 | int isprivate; /* key points to the private key */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 252 | int userprovided; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 253 | }; |
| 254 | TAILQ_HEAD(idlist, identity); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 255 | |
| 256 | struct Authctxt { |
| 257 | const char *server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 258 | const char *local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 259 | const char *host; |
| 260 | const char *service; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 261 | Authmethod *method; |
Damien Miller | 79442c0 | 2010-05-10 11:55:38 +1000 | [diff] [blame] | 262 | sig_atomic_t success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 263 | char *authlist; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 264 | /* pubkey */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 265 | Idlist keys; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 266 | AuthenticationConnection *agent; |
| 267 | /* hostbased */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 268 | Sensitive *sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 269 | /* kbd-interactive */ |
| 270 | int info_req_seen; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 271 | /* generic */ |
| 272 | void *methoddata; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 273 | }; |
| 274 | struct Authmethod { |
| 275 | char *name; /* string to compare against server's list */ |
| 276 | int (*userauth)(Authctxt *authctxt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 277 | void (*cleanup)(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 278 | int *enabled; /* flag in option struct that enables method */ |
| 279 | int *batch_flag; /* flag in option struct that disables method */ |
| 280 | }; |
| 281 | |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 282 | void input_userauth_success(int, u_int32_t, void *); |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 283 | void input_userauth_success_unexpected(int, u_int32_t, void *); |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 284 | void input_userauth_failure(int, u_int32_t, void *); |
| 285 | void input_userauth_banner(int, u_int32_t, void *); |
| 286 | void input_userauth_error(int, u_int32_t, void *); |
| 287 | void input_userauth_info_req(int, u_int32_t, void *); |
| 288 | void input_userauth_pk_ok(int, u_int32_t, void *); |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 289 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 290 | void input_userauth_jpake_server_step1(int, u_int32_t, void *); |
| 291 | void input_userauth_jpake_server_step2(int, u_int32_t, void *); |
| 292 | void input_userauth_jpake_server_confirm(int, u_int32_t, void *); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 293 | |
Ben Lindstrom | 3c36bb2 | 2001-12-06 17:55:26 +0000 | [diff] [blame] | 294 | int userauth_none(Authctxt *); |
| 295 | int userauth_pubkey(Authctxt *); |
| 296 | int userauth_passwd(Authctxt *); |
| 297 | int userauth_kbdint(Authctxt *); |
| 298 | int userauth_hostbased(Authctxt *); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 299 | int userauth_jpake(Authctxt *); |
| 300 | |
| 301 | void userauth_jpake_cleanup(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); |
| 305 | void input_gssapi_response(int type, u_int32_t, void *); |
| 306 | void input_gssapi_token(int type, u_int32_t, void *); |
| 307 | void input_gssapi_hash(int type, u_int32_t, void *); |
| 308 | void input_gssapi_error(int, u_int32_t, void *); |
| 309 | void input_gssapi_errtok(int, u_int32_t, void *); |
| 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 | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 341 | #ifdef JPAKE |
| 342 | {"jpake-01@openssh.com", |
| 343 | userauth_jpake, |
| 344 | userauth_jpake_cleanup, |
| 345 | &options.zero_knowledge_password_authentication, |
| 346 | &options.batch_mode}, |
| 347 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 348 | {"keyboard-interactive", |
| 349 | userauth_kbdint, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 350 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 351 | &options.kbd_interactive_authentication, |
| 352 | &options.batch_mode}, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 353 | {"password", |
| 354 | userauth_passwd, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 355 | NULL, |
Ben Lindstrom | 45350e8 | 2001-08-06 20:57:11 +0000 | [diff] [blame] | 356 | &options.password_authentication, |
| 357 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 358 | {"none", |
| 359 | userauth_none, |
| 360 | NULL, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 361 | NULL, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 362 | NULL}, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 363 | {NULL, NULL, NULL, NULL, NULL} |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 367 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 368 | Sensitive *sensitive) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 369 | { |
| 370 | Authctxt authctxt; |
| 371 | int type; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 372 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 373 | if (options.challenge_response_authentication) |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 374 | options.kbd_interactive_authentication = 1; |
| 375 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 376 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 377 | packet_put_cstring("ssh-userauth"); |
| 378 | packet_send(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 379 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 380 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 381 | type = packet_read(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 382 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
| 383 | fatal("Server denied authentication request: %d", type); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 384 | if (packet_remaining() > 0) { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 385 | char *reply = packet_get_string(NULL); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 386 | debug2("service_accept: %s", reply); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 387 | free(reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 388 | } else { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 389 | debug2("buggy server: service_accept w/o service"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 390 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 391 | packet_check_eom(); |
Ben Lindstrom | 064496f | 2002-12-23 02:04:22 +0000 | [diff] [blame] | 392 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 393 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 394 | if (options.preferred_authentications == NULL) |
| 395 | options.preferred_authentications = authmethods_get(); |
| 396 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 397 | /* setup authentication context */ |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 398 | memset(&authctxt, 0, sizeof(authctxt)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 399 | pubkey_prepare(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 400 | authctxt.server_user = server_user; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 401 | authctxt.local_user = local_user; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 402 | authctxt.host = host; |
| 403 | authctxt.service = "ssh-connection"; /* service name */ |
| 404 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 405 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 406 | authctxt.authlist = NULL; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 407 | authctxt.methoddata = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 408 | authctxt.sensitive = sensitive; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 409 | authctxt.info_req_seen = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 410 | if (authctxt.method == NULL) |
| 411 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 412 | |
| 413 | /* initial userauth request */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 414 | userauth_none(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 415 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 416 | dispatch_init(&input_userauth_error); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 417 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 418 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 419 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 420 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
| 421 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 422 | pubkey_cleanup(&authctxt); |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 423 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); |
| 424 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 425 | debug("Authentication succeeded (%s).", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 426 | } |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 427 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 428 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 429 | userauth(Authctxt *authctxt, char *authlist) |
| 430 | { |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 431 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 432 | authctxt->method->cleanup(authctxt); |
| 433 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 434 | free(authctxt->methoddata); |
| 435 | authctxt->methoddata = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 436 | if (authlist == NULL) { |
| 437 | authlist = authctxt->authlist; |
| 438 | } else { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 439 | free(authctxt->authlist); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 440 | authctxt->authlist = authlist; |
| 441 | } |
| 442 | for (;;) { |
| 443 | Authmethod *method = authmethod_get(authlist); |
| 444 | if (method == NULL) |
| 445 | fatal("Permission denied (%s).", authlist); |
| 446 | authctxt->method = method; |
Damien Miller | f842fcb | 2003-05-15 12:01:28 +1000 | [diff] [blame] | 447 | |
| 448 | /* reset the per method handler */ |
| 449 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, |
| 450 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); |
| 451 | |
| 452 | /* and try new method */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 453 | if (method->userauth(authctxt) != 0) { |
| 454 | debug2("we sent a %s packet, wait for reply", method->name); |
| 455 | break; |
| 456 | } else { |
| 457 | debug2("we did not send a packet, disable method"); |
| 458 | method->enabled = NULL; |
| 459 | } |
| 460 | } |
| 461 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 462 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 463 | /* ARGSUSED */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 464 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 465 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 466 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 467 | fatal("input_userauth_error: bad message during authentication: " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 468 | "type %d", type); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 469 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 470 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 471 | /* ARGSUSED */ |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 472 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 473 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 474 | { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 475 | char *msg, *raw, *lang; |
| 476 | u_int len; |
Darren Tucker | 7964482 | 2003-10-08 17:37:58 +1000 | [diff] [blame] | 477 | |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 478 | debug3("input_userauth_banner"); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 479 | raw = packet_get_string(&len); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 480 | lang = packet_get_string(NULL); |
Damien Miller | c674d58 | 2008-11-03 19:16:57 +1100 | [diff] [blame] | 481 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 482 | if (len > 65536) |
| 483 | len = 65536; |
Damien Miller | c4d1b36 | 2008-11-03 19:22:09 +1100 | [diff] [blame] | 484 | msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ |
Darren Tucker | 0c348f5 | 2010-01-08 18:58:05 +1100 | [diff] [blame] | 485 | strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH); |
Darren Tucker | 046dff2 | 2003-10-08 17:32:02 +1000 | [diff] [blame] | 486 | fprintf(stderr, "%s", msg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 487 | free(msg); |
Damien Miller | 7ba0ca7 | 2008-07-17 18:57:06 +1000 | [diff] [blame] | 488 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 489 | free(raw); |
| 490 | free(lang); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 491 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 492 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 493 | /* ARGSUSED */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 494 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 495 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 496 | { |
| 497 | Authctxt *authctxt = ctxt; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 498 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 499 | if (authctxt == NULL) |
| 500 | fatal("input_userauth_success: no authentication context"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 501 | free(authctxt->authlist); |
| 502 | authctxt->authlist = NULL; |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 503 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
| 504 | authctxt->method->cleanup(authctxt); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 505 | free(authctxt->methoddata); |
| 506 | authctxt->methoddata = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 507 | authctxt->success = 1; /* break out */ |
| 508 | } |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 509 | |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 510 | void |
| 511 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) |
| 512 | { |
| 513 | Authctxt *authctxt = ctxt; |
| 514 | |
| 515 | if (authctxt == NULL) |
| 516 | fatal("%s: no authentication context", __func__); |
| 517 | |
| 518 | fatal("Unexpected authentication success during %s.", |
| 519 | authctxt->method->name); |
| 520 | } |
| 521 | |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 522 | /* ARGSUSED */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 523 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 524 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 525 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 526 | Authctxt *authctxt = ctxt; |
| 527 | char *authlist = NULL; |
| 528 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 529 | |
| 530 | if (authctxt == NULL) |
| 531 | fatal("input_userauth_failure: no authentication context"); |
| 532 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 533 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 534 | partial = packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 535 | packet_check_eom(); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 536 | |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 537 | if (partial != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 538 | logit("Authenticated with partial success."); |
Damien Miller | 62e9c4f | 2013-04-23 15:15:49 +1000 | [diff] [blame] | 539 | /* reset state */ |
| 540 | pubkey_cleanup(authctxt); |
| 541 | pubkey_prepare(authctxt); |
| 542 | } |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 543 | debug("Authentications that can continue: %s", authlist); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 544 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 545 | userauth(authctxt, authlist); |
| 546 | } |
Damien Miller | f7475d7 | 2008-11-03 19:26:18 +1100 | [diff] [blame] | 547 | |
| 548 | /* ARGSUSED */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 549 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 550 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 551 | { |
| 552 | Authctxt *authctxt = ctxt; |
| 553 | Key *key = NULL; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 554 | Identity *id = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 555 | Buffer b; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 556 | int pktype, sent = 0; |
| 557 | u_int alen, blen; |
| 558 | char *pkalg, *fp; |
| 559 | u_char *pkblob; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 560 | |
| 561 | if (authctxt == NULL) |
| 562 | fatal("input_userauth_pk_ok: no authentication context"); |
| 563 | if (datafellows & SSH_BUG_PKOK) { |
| 564 | /* this is similar to SSH_BUG_PKAUTH */ |
| 565 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 566 | pkblob = packet_get_string(&blen); |
| 567 | buffer_init(&b); |
| 568 | buffer_append(&b, pkblob, blen); |
| 569 | pkalg = buffer_get_string(&b, &alen); |
| 570 | buffer_free(&b); |
| 571 | } else { |
| 572 | pkalg = packet_get_string(&alen); |
| 573 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 574 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 575 | packet_check_eom(); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 576 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 577 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 578 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 579 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
| 580 | debug("unknown pkalg %s", pkalg); |
| 581 | goto done; |
| 582 | } |
| 583 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 584 | debug("no key from blob. pkalg %s", pkalg); |
| 585 | goto done; |
| 586 | } |
| 587 | if (key->type != pktype) { |
| 588 | error("input_userauth_pk_ok: type mismatch " |
| 589 | "for decoded key (received %d, expected %d)", |
| 590 | key->type, pktype); |
| 591 | goto done; |
| 592 | } |
| 593 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 594 | debug2("input_userauth_pk_ok: fp %s", fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 595 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 596 | |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 597 | /* |
| 598 | * search keys in the reverse order, because last candidate has been |
| 599 | * moved to the end of the queue. this also avoids confusion by |
| 600 | * duplicate keys |
| 601 | */ |
Damien Miller | 0b51a52 | 2004-04-20 20:07:19 +1000 | [diff] [blame] | 602 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 603 | if (key_equal(key, id->key)) { |
| 604 | sent = sign_and_send_pubkey(authctxt, id); |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | done: |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 609 | if (key != NULL) |
| 610 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 611 | free(pkalg); |
| 612 | free(pkblob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 613 | |
Ben Lindstrom | a962c2f | 2002-07-04 00:14:17 +0000 | [diff] [blame] | 614 | /* try another method if we did not send a packet */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 615 | if (sent == 0) |
| 616 | userauth(authctxt, NULL); |
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 | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 885 | memset(password, 0, 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 */ |
Ben Lindstrom | 38a69e6 | 2002-03-27 17:28:46 +0000 | [diff] [blame] | 900 | void |
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); |
| 931 | memset(password, 0, 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 */ |
| 941 | return; |
| 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) { |
| 948 | memset(password, 0, 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 | } |
| 953 | memset(retype, 0, 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); |
| 957 | memset(password, 0, 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); |
| 964 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 965 | |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 966 | #ifdef JPAKE |
| 967 | static char * |
| 968 | pw_encrypt(const char *password, const char *crypt_scheme, const char *salt) |
| 969 | { |
| 970 | /* OpenBSD crypt(3) handles all of these */ |
| 971 | if (strcmp(crypt_scheme, "crypt") == 0 || |
| 972 | strcmp(crypt_scheme, "bcrypt") == 0 || |
| 973 | strcmp(crypt_scheme, "md5crypt") == 0 || |
| 974 | strcmp(crypt_scheme, "crypt-extended") == 0) |
| 975 | return xstrdup(crypt(password, salt)); |
| 976 | error("%s: unsupported password encryption scheme \"%.100s\"", |
| 977 | __func__, crypt_scheme); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
| 981 | static BIGNUM * |
| 982 | jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme, |
| 983 | const char *salt) |
| 984 | { |
| 985 | char prompt[256], *password, *crypted; |
| 986 | u_char *secret; |
| 987 | u_int secret_len; |
| 988 | BIGNUM *ret; |
| 989 | |
| 990 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ", |
| 991 | authctxt->server_user, authctxt->host); |
| 992 | password = read_passphrase(prompt, 0); |
| 993 | |
| 994 | if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) { |
| 995 | logit("Disabling %s authentication", authctxt->method->name); |
| 996 | authctxt->method->enabled = NULL; |
| 997 | /* Continue with an empty password to fail gracefully */ |
| 998 | crypted = xstrdup(""); |
| 999 | } |
| 1000 | |
| 1001 | #ifdef JPAKE_DEBUG |
| 1002 | debug3("%s: salt = %s", __func__, salt); |
| 1003 | debug3("%s: scheme = %s", __func__, crypt_scheme); |
| 1004 | debug3("%s: crypted = %s", __func__, crypted); |
| 1005 | #endif |
| 1006 | |
| 1007 | if (hash_buffer(crypted, strlen(crypted), EVP_sha256(), |
| 1008 | &secret, &secret_len) != 0) |
| 1009 | fatal("%s: hash_buffer", __func__); |
| 1010 | |
| 1011 | bzero(password, strlen(password)); |
| 1012 | bzero(crypted, strlen(crypted)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1013 | free(password); |
| 1014 | free(crypted); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1015 | |
| 1016 | if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL) |
| 1017 | fatal("%s: BN_bin2bn (secret)", __func__); |
| 1018 | bzero(secret, secret_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1019 | free(secret); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1020 | |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | /* ARGSUSED */ |
| 1025 | void |
| 1026 | input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt) |
| 1027 | { |
| 1028 | Authctxt *authctxt = ctxt; |
| 1029 | struct jpake_ctx *pctx = authctxt->methoddata; |
| 1030 | u_char *x3_proof, *x4_proof, *x2_s_proof; |
| 1031 | u_int x3_proof_len, x4_proof_len, x2_s_proof_len; |
| 1032 | char *crypt_scheme, *salt; |
| 1033 | |
| 1034 | /* Disable this message */ |
| 1035 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL); |
| 1036 | |
| 1037 | if ((pctx->g_x3 = BN_new()) == NULL || |
| 1038 | (pctx->g_x4 = BN_new()) == NULL) |
| 1039 | fatal("%s: BN_new", __func__); |
| 1040 | |
| 1041 | /* Fetch step 1 values */ |
| 1042 | crypt_scheme = packet_get_string(NULL); |
| 1043 | salt = packet_get_string(NULL); |
| 1044 | pctx->server_id = packet_get_string(&pctx->server_id_len); |
| 1045 | packet_get_bignum2(pctx->g_x3); |
| 1046 | packet_get_bignum2(pctx->g_x4); |
| 1047 | x3_proof = packet_get_string(&x3_proof_len); |
| 1048 | x4_proof = packet_get_string(&x4_proof_len); |
| 1049 | packet_check_eom(); |
| 1050 | |
| 1051 | JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__)); |
| 1052 | |
| 1053 | /* Obtain password and derive secret */ |
| 1054 | pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt); |
| 1055 | bzero(crypt_scheme, strlen(crypt_scheme)); |
| 1056 | bzero(salt, strlen(salt)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1057 | free(crypt_scheme); |
| 1058 | free(salt); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1059 | JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__)); |
| 1060 | |
| 1061 | /* Calculate step 2 values */ |
| 1062 | jpake_step2(pctx->grp, pctx->s, pctx->g_x1, |
| 1063 | pctx->g_x3, pctx->g_x4, pctx->x2, |
| 1064 | pctx->server_id, pctx->server_id_len, |
| 1065 | pctx->client_id, pctx->client_id_len, |
| 1066 | x3_proof, x3_proof_len, |
| 1067 | x4_proof, x4_proof_len, |
| 1068 | &pctx->a, |
| 1069 | &x2_s_proof, &x2_s_proof_len); |
| 1070 | |
| 1071 | bzero(x3_proof, x3_proof_len); |
| 1072 | bzero(x4_proof, x4_proof_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1073 | free(x3_proof); |
| 1074 | free(x4_proof); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1075 | |
| 1076 | JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__)); |
| 1077 | |
| 1078 | /* Send values for step 2 */ |
| 1079 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2); |
| 1080 | packet_put_bignum2(pctx->a); |
| 1081 | packet_put_string(x2_s_proof, x2_s_proof_len); |
| 1082 | packet_send(); |
| 1083 | |
| 1084 | bzero(x2_s_proof, x2_s_proof_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1085 | free(x2_s_proof); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1086 | |
| 1087 | /* Expect step 2 packet from peer */ |
| 1088 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, |
| 1089 | input_userauth_jpake_server_step2); |
| 1090 | } |
| 1091 | |
| 1092 | /* ARGSUSED */ |
| 1093 | void |
| 1094 | input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt) |
| 1095 | { |
| 1096 | Authctxt *authctxt = ctxt; |
| 1097 | struct jpake_ctx *pctx = authctxt->methoddata; |
| 1098 | u_char *x4_s_proof; |
| 1099 | u_int x4_s_proof_len; |
| 1100 | |
| 1101 | /* Disable this message */ |
| 1102 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL); |
| 1103 | |
| 1104 | if ((pctx->b = BN_new()) == NULL) |
| 1105 | fatal("%s: BN_new", __func__); |
| 1106 | |
| 1107 | /* Fetch step 2 values */ |
| 1108 | packet_get_bignum2(pctx->b); |
| 1109 | x4_s_proof = packet_get_string(&x4_s_proof_len); |
| 1110 | packet_check_eom(); |
| 1111 | |
| 1112 | JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__)); |
| 1113 | |
| 1114 | /* Derive shared key and calculate confirmation hash */ |
| 1115 | jpake_key_confirm(pctx->grp, pctx->s, pctx->b, |
| 1116 | pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4, |
| 1117 | pctx->client_id, pctx->client_id_len, |
| 1118 | pctx->server_id, pctx->server_id_len, |
| 1119 | session_id2, session_id2_len, |
| 1120 | x4_s_proof, x4_s_proof_len, |
| 1121 | &pctx->k, |
| 1122 | &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len); |
| 1123 | |
| 1124 | bzero(x4_s_proof, x4_s_proof_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1125 | free(x4_s_proof); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1126 | |
| 1127 | JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__)); |
| 1128 | |
| 1129 | /* Send key confirmation proof */ |
| 1130 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM); |
| 1131 | packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len); |
| 1132 | packet_send(); |
| 1133 | |
| 1134 | /* Expect confirmation from peer */ |
| 1135 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, |
| 1136 | input_userauth_jpake_server_confirm); |
| 1137 | } |
| 1138 | |
| 1139 | /* ARGSUSED */ |
| 1140 | void |
| 1141 | input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt) |
| 1142 | { |
| 1143 | Authctxt *authctxt = ctxt; |
| 1144 | struct jpake_ctx *pctx = authctxt->methoddata; |
| 1145 | |
| 1146 | /* Disable this message */ |
| 1147 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL); |
| 1148 | |
| 1149 | pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len); |
| 1150 | packet_check_eom(); |
| 1151 | |
| 1152 | JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__)); |
| 1153 | |
| 1154 | /* Verify expected confirmation hash */ |
| 1155 | if (jpake_check_confirm(pctx->k, |
| 1156 | pctx->server_id, pctx->server_id_len, |
| 1157 | session_id2, session_id2_len, |
| 1158 | pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1) |
| 1159 | debug("%s: %s success", __func__, authctxt->method->name); |
| 1160 | else { |
| 1161 | debug("%s: confirmation mismatch", __func__); |
| 1162 | /* XXX stash this so if auth succeeds then we can warn/kill */ |
| 1163 | } |
| 1164 | |
| 1165 | userauth_jpake_cleanup(authctxt); |
| 1166 | } |
| 1167 | #endif /* JPAKE */ |
| 1168 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1169 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1170 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, |
| 1171 | u_char *data, u_int datalen) |
| 1172 | { |
| 1173 | Key *prv; |
| 1174 | int ret; |
| 1175 | |
| 1176 | /* the agent supports this key */ |
| 1177 | if (id->ac) |
| 1178 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, |
| 1179 | data, datalen)); |
| 1180 | /* |
| 1181 | * we have already loaded the private key or |
| 1182 | * the private key is stored in external hardware |
| 1183 | */ |
| 1184 | if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) |
| 1185 | return (key_sign(id->key, sigp, lenp, data, datalen)); |
| 1186 | /* load the private key from the file */ |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1187 | if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1188 | return (-1); |
| 1189 | ret = key_sign(prv, sigp, lenp, data, datalen); |
| 1190 | key_free(prv); |
| 1191 | return (ret); |
| 1192 | } |
| 1193 | |
| 1194 | static int |
| 1195 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1196 | { |
| 1197 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1198 | u_char *blob, *signature; |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 1199 | u_int bloblen, slen; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 1200 | u_int skip = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1201 | int ret = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1202 | int have_sig = 1; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1203 | char *fp; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1204 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1205 | fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); |
| 1206 | debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1207 | free(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1208 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1209 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1210 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1211 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1214 | /* data to be signed */ |
| 1215 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1216 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1217 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1218 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 1219 | } else { |
| 1220 | buffer_put_string(&b, session_id2, session_id2_len); |
| 1221 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1222 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1223 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1224 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1225 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1226 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1227 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1228 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1229 | if (datafellows & SSH_BUG_PKAUTH) { |
| 1230 | buffer_put_char(&b, have_sig); |
| 1231 | } else { |
| 1232 | buffer_put_cstring(&b, authctxt->method->name); |
| 1233 | buffer_put_char(&b, have_sig); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1234 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1235 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1236 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1237 | |
| 1238 | /* generate signature */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1239 | ret = identity_sign(id, &signature, &slen, |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1240 | buffer_ptr(&b), buffer_len(&b)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1241 | if (ret == -1) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1242 | free(blob); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1243 | buffer_free(&b); |
| 1244 | return 0; |
| 1245 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1246 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1247 | buffer_dump(&b); |
| 1248 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1249 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1250 | buffer_clear(&b); |
| 1251 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1252 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1253 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1254 | buffer_put_cstring(&b, authctxt->server_user); |
| 1255 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1256 | buffer_put_cstring(&b, authctxt->method->name); |
| 1257 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 1258 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1259 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 1260 | buffer_put_string(&b, blob, bloblen); |
| 1261 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1262 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1263 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1264 | /* append signature */ |
| 1265 | buffer_put_string(&b, signature, slen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1266 | free(signature); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1267 | |
| 1268 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1269 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1270 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 1271 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1272 | |
| 1273 | /* put remaining data from buffer into packet */ |
| 1274 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1275 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 1276 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1277 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1278 | |
| 1279 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1280 | } |
| 1281 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1282 | static int |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1283 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1284 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1285 | u_char *blob; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1286 | u_int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1287 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1288 | debug3("send_pubkey_test"); |
| 1289 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1290 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1291 | /* we cannot handle this key */ |
| 1292 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1295 | /* register callback for USERAUTH_PK_OK message */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1296 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1297 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1298 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1299 | packet_put_cstring(authctxt->server_user); |
| 1300 | packet_put_cstring(authctxt->service); |
| 1301 | packet_put_cstring(authctxt->method->name); |
| 1302 | packet_put_char(have_sig); |
| 1303 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1304 | packet_put_cstring(key_ssh_name(id->key)); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1305 | packet_put_string(blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1306 | free(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1307 | packet_send(); |
| 1308 | return 1; |
| 1309 | } |
| 1310 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1311 | static Key * |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1312 | load_identity_file(char *filename, int userprovided) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1313 | { |
| 1314 | Key *private; |
| 1315 | char prompt[300], *passphrase; |
Darren Tucker | d4c86b1 | 2010-01-12 19:41:22 +1100 | [diff] [blame] | 1316 | int perm_ok = 0, quit, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1317 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1318 | |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1319 | if (stat(filename, &st) < 0) { |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1320 | (userprovided ? logit : debug3)("no such identity: %s: %s", |
| 1321 | filename, strerror(errno)); |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 1322 | return NULL; |
| 1323 | } |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 1324 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); |
| 1325 | if (!perm_ok) |
| 1326 | return NULL; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1327 | if (private == NULL) { |
| 1328 | if (options.batch_mode) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1329 | return NULL; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1330 | snprintf(prompt, sizeof prompt, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1331 | "Enter passphrase for key '%.100s': ", filename); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1332 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 1333 | passphrase = read_passphrase(prompt, 0); |
| 1334 | if (strcmp(passphrase, "") != 0) { |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 1335 | private = key_load_private_type(KEY_UNSPEC, |
| 1336 | filename, passphrase, NULL, NULL); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1337 | quit = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1338 | } else { |
| 1339 | debug2("no passphrase given, try next key"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1340 | quit = 1; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1341 | } |
| 1342 | memset(passphrase, 0, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1343 | free(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1344 | if (private != NULL || quit) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1345 | break; |
| 1346 | debug2("bad passphrase given, try again..."); |
| 1347 | } |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 1348 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1349 | return private; |
| 1350 | } |
| 1351 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1352 | /* |
| 1353 | * try keys in the following order: |
| 1354 | * 1. agent keys that are found in the config file |
| 1355 | * 2. other agent keys |
| 1356 | * 3. keys that are only listed in the config file |
| 1357 | */ |
| 1358 | static void |
| 1359 | pubkey_prepare(Authctxt *authctxt) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1360 | { |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1361 | Identity *id, *id2, *tmp; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1362 | Idlist agent, files, *preferred; |
| 1363 | Key *key; |
| 1364 | AuthenticationConnection *ac; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1365 | char *comment; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1366 | int i, found; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1367 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1368 | TAILQ_INIT(&agent); /* keys from the agent */ |
| 1369 | TAILQ_INIT(&files); /* keys from the config file */ |
| 1370 | preferred = &authctxt->keys; |
| 1371 | TAILQ_INIT(preferred); /* preferred order of keys */ |
| 1372 | |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1373 | /* list of keys stored in the filesystem and PKCS#11 */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1374 | for (i = 0; i < options.num_identity_files; i++) { |
| 1375 | key = options.identity_keys[i]; |
| 1376 | if (key && key->type == KEY_RSA1) |
| 1377 | continue; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1378 | if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER) |
| 1379 | continue; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1380 | options.identity_keys[i] = NULL; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1381 | id = xcalloc(1, sizeof(*id)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1382 | id->key = key; |
| 1383 | id->filename = xstrdup(options.identity_files[i]); |
Darren Tucker | 1910478 | 2013-04-05 11:13:08 +1100 | [diff] [blame] | 1384 | id->userprovided = options.identity_file_userprovided[i]; |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1385 | TAILQ_INSERT_TAIL(&files, id, next); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 1386 | } |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1387 | /* Prefer PKCS11 keys that are explicitly listed */ |
| 1388 | TAILQ_FOREACH_SAFE(id, &files, next, tmp) { |
| 1389 | if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0) |
| 1390 | continue; |
| 1391 | found = 0; |
| 1392 | TAILQ_FOREACH(id2, &files, next) { |
| 1393 | if (id2->key == NULL || |
| 1394 | (id2->key->flags & KEY_FLAG_EXT) != 0) |
| 1395 | continue; |
| 1396 | if (key_equal(id->key, id2->key)) { |
| 1397 | TAILQ_REMOVE(&files, id, next); |
| 1398 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1399 | found = 1; |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | /* If IdentitiesOnly set and key not found then don't use it */ |
| 1404 | if (!found && options.identities_only) { |
| 1405 | TAILQ_REMOVE(&files, id, next); |
Darren Tucker | 54da6be | 2013-05-16 20:25:04 +1000 | [diff] [blame] | 1406 | bzero(id, sizeof(*id)); |
Damien Miller | cb6b68b | 2012-12-03 09:49:52 +1100 | [diff] [blame] | 1407 | free(id); |
| 1408 | } |
| 1409 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1410 | /* list of keys supported by the agent */ |
| 1411 | if ((ac = ssh_get_authentication_connection())) { |
| 1412 | for (key = ssh_get_first_identity(ac, &comment, 2); |
| 1413 | key != NULL; |
| 1414 | key = ssh_get_next_identity(ac, &comment, 2)) { |
| 1415 | found = 0; |
| 1416 | TAILQ_FOREACH(id, &files, next) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1417 | /* agent keys from the config file are preferred */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1418 | if (key_equal(key, id->key)) { |
| 1419 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1420 | free(comment); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1421 | TAILQ_REMOVE(&files, id, next); |
| 1422 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1423 | id->ac = ac; |
| 1424 | found = 1; |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
Damien Miller | bd394c3 | 2004-03-08 23:12:36 +1100 | [diff] [blame] | 1428 | if (!found && !options.identities_only) { |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1429 | id = xcalloc(1, sizeof(*id)); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1430 | id->key = key; |
| 1431 | id->filename = comment; |
| 1432 | id->ac = ac; |
| 1433 | TAILQ_INSERT_TAIL(&agent, id, next); |
| 1434 | } |
| 1435 | } |
| 1436 | /* append remaining agent keys */ |
| 1437 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { |
| 1438 | TAILQ_REMOVE(&agent, id, next); |
| 1439 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1440 | } |
| 1441 | authctxt->agent = ac; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1442 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1443 | /* append remaining keys from the config file */ |
| 1444 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { |
| 1445 | TAILQ_REMOVE(&files, id, next); |
| 1446 | TAILQ_INSERT_TAIL(preferred, id, next); |
| 1447 | } |
| 1448 | TAILQ_FOREACH(id, preferred, next) { |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1449 | debug2("key: %s (%p),%s", id->filename, id->key, |
| 1450 | id->userprovided ? " explicit" : ""); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | static void |
| 1455 | pubkey_cleanup(Authctxt *authctxt) |
| 1456 | { |
| 1457 | Identity *id; |
| 1458 | |
| 1459 | if (authctxt->agent != NULL) |
| 1460 | ssh_close_authentication_connection(authctxt->agent); |
| 1461 | for (id = TAILQ_FIRST(&authctxt->keys); id; |
| 1462 | id = TAILQ_FIRST(&authctxt->keys)) { |
| 1463 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1464 | if (id->key) |
| 1465 | key_free(id->key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1466 | free(id->filename); |
| 1467 | free(id); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1468 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1469 | } |
| 1470 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1471 | int |
| 1472 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1473 | { |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1474 | Identity *id; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1475 | int sent = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1476 | |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1477 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
| 1478 | if (id->tried++) |
| 1479 | return (0); |
Darren Tucker | d05b601 | 2003-10-15 15:55:59 +1000 | [diff] [blame] | 1480 | /* move key to the end of the queue */ |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1481 | TAILQ_REMOVE(&authctxt->keys, id, next); |
| 1482 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); |
| 1483 | /* |
| 1484 | * send a test message if we have the public key. for |
| 1485 | * encrypted keys we cannot do this and have to load the |
| 1486 | * private key instead |
| 1487 | */ |
| 1488 | if (id->key && id->key->type != KEY_RSA1) { |
Damien Miller | 8868065 | 2010-04-16 15:53:43 +1000 | [diff] [blame] | 1489 | debug("Offering %s public key: %s", key_type(id->key), |
| 1490 | id->filename); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1491 | sent = send_pubkey_test(authctxt, id); |
| 1492 | } else if (id->key == NULL) { |
| 1493 | debug("Trying private key: %s", id->filename); |
Damien Miller | 5ceddc3 | 2013-02-15 12:18:32 +1100 | [diff] [blame] | 1494 | id->key = load_identity_file(id->filename, |
| 1495 | id->userprovided); |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1496 | if (id->key != NULL) { |
| 1497 | id->isprivate = 1; |
| 1498 | sent = sign_and_send_pubkey(authctxt, id); |
| 1499 | key_free(id->key); |
| 1500 | id->key = NULL; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1501 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1502 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1503 | if (sent) |
| 1504 | return (sent); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1505 | } |
Damien Miller | 280ecfb | 2003-05-14 13:46:00 +1000 | [diff] [blame] | 1506 | return (0); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1507 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1508 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1509 | /* |
| 1510 | * Send userauth request message specifying keyboard-interactive method. |
| 1511 | */ |
| 1512 | int |
| 1513 | userauth_kbdint(Authctxt *authctxt) |
| 1514 | { |
| 1515 | static int attempt = 0; |
| 1516 | |
| 1517 | if (attempt++ >= options.number_of_password_prompts) |
| 1518 | return 0; |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1519 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ |
| 1520 | if (attempt > 1 && !authctxt->info_req_seen) { |
| 1521 | debug3("userauth_kbdint: disable: no info_req_seen"); |
| 1522 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); |
| 1523 | return 0; |
| 1524 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1525 | |
| 1526 | debug2("userauth_kbdint"); |
| 1527 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1528 | packet_put_cstring(authctxt->server_user); |
| 1529 | packet_put_cstring(authctxt->service); |
| 1530 | packet_put_cstring(authctxt->method->name); |
| 1531 | packet_put_cstring(""); /* lang */ |
| 1532 | packet_put_cstring(options.kbd_interactive_devices ? |
| 1533 | options.kbd_interactive_devices : ""); |
| 1534 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1535 | |
| 1536 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 1537 | return 1; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1541 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1542 | */ |
| 1543 | void |
Damien Miller | 630d6f4 | 2002-01-22 23:17:30 +1100 | [diff] [blame] | 1544 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1545 | { |
| 1546 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1547 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1548 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1549 | int echo = 0; |
| 1550 | |
| 1551 | debug2("input_userauth_info_req"); |
| 1552 | |
| 1553 | if (authctxt == NULL) |
| 1554 | fatal("input_userauth_info_req: no authentication context"); |
| 1555 | |
Ben Lindstrom | 7d19996 | 2001-09-12 18:29:00 +0000 | [diff] [blame] | 1556 | authctxt->info_req_seen = 1; |
| 1557 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1558 | name = packet_get_string(NULL); |
| 1559 | inst = packet_get_string(NULL); |
| 1560 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1561 | if (strlen(name) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1562 | logit("%s", name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1563 | if (strlen(inst) > 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1564 | logit("%s", inst); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1565 | free(name); |
| 1566 | free(inst); |
| 1567 | free(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1568 | |
| 1569 | num_prompts = packet_get_int(); |
| 1570 | /* |
| 1571 | * Begin to build info response packet based on prompts requested. |
| 1572 | * We commit to providing the correct number of responses, so if |
| 1573 | * further on we run into a problem that prevents this, we have to |
| 1574 | * be sure and clean this up and send a correct error response. |
| 1575 | */ |
| 1576 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 1577 | packet_put_int(num_prompts); |
| 1578 | |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 1579 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1580 | for (i = 0; i < num_prompts; i++) { |
| 1581 | prompt = packet_get_string(NULL); |
| 1582 | echo = packet_get_char(); |
| 1583 | |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1584 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1585 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1586 | packet_put_cstring(response); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1587 | memset(response, 0, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1588 | free(response); |
| 1589 | free(prompt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1590 | } |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 1591 | packet_check_eom(); /* done with parsing incoming message. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1592 | |
Damien Miller | 9f64390 | 2001-11-12 11:02:52 +1100 | [diff] [blame] | 1593 | packet_add_padding(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1594 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1595 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1596 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1597 | static int |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1598 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1599 | u_char *data, u_int datalen) |
| 1600 | { |
| 1601 | Buffer b; |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1602 | struct stat st; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1603 | pid_t pid; |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1604 | int to[2], from[2], status, version = 2; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1605 | |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1606 | debug2("ssh_keysign called"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1607 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1608 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1609 | error("ssh_keysign: not installed: %s", strerror(errno)); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1610 | return -1; |
| 1611 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1612 | if (fflush(stdout) != 0) |
| 1613 | error("ssh_keysign: fflush: %s", strerror(errno)); |
| 1614 | if (pipe(to) < 0) { |
| 1615 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | if (pipe(from) < 0) { |
| 1619 | error("ssh_keysign: pipe: %s", strerror(errno)); |
| 1620 | return -1; |
| 1621 | } |
| 1622 | if ((pid = fork()) < 0) { |
| 1623 | error("ssh_keysign: fork: %s", strerror(errno)); |
| 1624 | return -1; |
| 1625 | } |
| 1626 | if (pid == 0) { |
Darren Tucker | 6e7fe1c | 2010-01-08 17:07:22 +1100 | [diff] [blame] | 1627 | /* keep the socket on exec */ |
| 1628 | fcntl(packet_get_connection_in(), F_SETFD, 0); |
Damien Miller | 2e5fe88 | 2006-06-13 13:10:00 +1000 | [diff] [blame] | 1629 | permanently_drop_suid(getuid()); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1630 | close(from[0]); |
| 1631 | if (dup2(from[1], STDOUT_FILENO) < 0) |
| 1632 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
| 1633 | close(to[1]); |
| 1634 | if (dup2(to[0], STDIN_FILENO) < 0) |
| 1635 | fatal("ssh_keysign: dup2: %s", strerror(errno)); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1636 | close(from[1]); |
| 1637 | close(to[0]); |
Ben Lindstrom | 4887da2 | 2002-06-06 20:05:57 +0000 | [diff] [blame] | 1638 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1639 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, |
| 1640 | strerror(errno)); |
| 1641 | } |
| 1642 | close(from[1]); |
| 1643 | close(to[0]); |
| 1644 | |
| 1645 | buffer_init(&b); |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1646 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1647 | buffer_put_string(&b, data, datalen); |
Damien Miller | 51bf11f | 2003-11-17 21:20:47 +1100 | [diff] [blame] | 1648 | if (ssh_msg_send(to[1], version, &b) == -1) |
| 1649 | fatal("ssh_keysign: couldn't send request"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1650 | |
Damien Miller | 901119b | 2002-10-04 11:10:04 +1000 | [diff] [blame] | 1651 | if (ssh_msg_recv(from[0], &b) < 0) { |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1652 | error("ssh_keysign: no reply"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1653 | buffer_free(&b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1654 | return -1; |
| 1655 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1656 | close(from[0]); |
| 1657 | close(to[1]); |
| 1658 | |
Ben Lindstrom | cec2ea8 | 2002-06-06 20:51:04 +0000 | [diff] [blame] | 1659 | while (waitpid(pid, &status, 0) < 0) |
| 1660 | if (errno != EINTR) |
| 1661 | break; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1662 | |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1663 | if (buffer_get_char(&b) != version) { |
| 1664 | error("ssh_keysign: bad version"); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1665 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1666 | return -1; |
| 1667 | } |
| 1668 | *sigp = buffer_get_string(&b, lenp); |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 1669 | buffer_free(&b); |
Ben Lindstrom | 5206b95 | 2002-06-06 19:59:29 +0000 | [diff] [blame] | 1670 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1674 | int |
| 1675 | userauth_hostbased(Authctxt *authctxt) |
| 1676 | { |
| 1677 | Key *private = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1678 | Sensitive *sensitive = authctxt->sensitive; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1679 | Buffer b; |
| 1680 | u_char *signature, *blob; |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1681 | char *chost, *pkalg, *p; |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1682 | const char *service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1683 | u_int blen, slen; |
Darren Tucker | 5246df4 | 2010-01-08 18:50:46 +1100 | [diff] [blame] | 1684 | int ok, i, found = 0; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1685 | |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1686 | /* check for a useful key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1687 | for (i = 0; i < sensitive->nkeys; i++) { |
| 1688 | private = sensitive->keys[i]; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1689 | if (private && private->type != KEY_RSA1) { |
| 1690 | found = 1; |
| 1691 | /* we take and free the key */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1692 | sensitive->keys[i] = NULL; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | if (!found) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1697 | debug("No more client hostkeys for hostbased authentication."); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | if (key_to_blob(private, &blob, &blen) == 0) { |
| 1701 | key_free(private); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1702 | return 0; |
| 1703 | } |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1704 | /* figure out a name for the client host */ |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 1705 | p = get_local_name(packet_get_connection_in()); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1706 | if (p == NULL) { |
| 1707 | error("userauth_hostbased: cannot get local ipaddr/name"); |
| 1708 | key_free(private); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1709 | free(blob); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1710 | return 0; |
| 1711 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1712 | xasprintf(&chost, "%s.", p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1713 | debug2("userauth_hostbased: chost %s", chost); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1714 | free(p); |
Damien Miller | 91c1847 | 2001-11-12 11:02:03 +1100 | [diff] [blame] | 1715 | |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1716 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
| 1717 | authctxt->service; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1718 | pkalg = xstrdup(key_ssh_name(private)); |
| 1719 | buffer_init(&b); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1720 | /* construct data */ |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1721 | buffer_put_string(&b, session_id2, session_id2_len); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1722 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 1723 | buffer_put_cstring(&b, authctxt->server_user); |
Ben Lindstrom | 671388f | 2001-04-19 20:40:45 +0000 | [diff] [blame] | 1724 | buffer_put_cstring(&b, service); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1725 | buffer_put_cstring(&b, authctxt->method->name); |
| 1726 | buffer_put_cstring(&b, pkalg); |
| 1727 | buffer_put_string(&b, blob, blen); |
| 1728 | buffer_put_cstring(&b, chost); |
| 1729 | buffer_put_cstring(&b, authctxt->local_user); |
| 1730 | #ifdef DEBUG_PK |
| 1731 | buffer_dump(&b); |
| 1732 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 1733 | if (sensitive->external_keysign) |
| 1734 | ok = ssh_keysign(private, &signature, &slen, |
| 1735 | buffer_ptr(&b), buffer_len(&b)); |
| 1736 | else |
| 1737 | ok = key_sign(private, &signature, &slen, |
| 1738 | buffer_ptr(&b), buffer_len(&b)); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1739 | key_free(private); |
| 1740 | buffer_free(&b); |
| 1741 | if (ok != 0) { |
| 1742 | error("key_sign failed"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1743 | free(chost); |
| 1744 | free(pkalg); |
| 1745 | free(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1746 | return 0; |
| 1747 | } |
| 1748 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1749 | packet_put_cstring(authctxt->server_user); |
| 1750 | packet_put_cstring(authctxt->service); |
| 1751 | packet_put_cstring(authctxt->method->name); |
| 1752 | packet_put_cstring(pkalg); |
| 1753 | packet_put_string(blob, blen); |
| 1754 | packet_put_cstring(chost); |
| 1755 | packet_put_cstring(authctxt->local_user); |
| 1756 | packet_put_string(signature, slen); |
| 1757 | memset(signature, 's', slen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1758 | free(signature); |
| 1759 | free(chost); |
| 1760 | free(pkalg); |
| 1761 | free(blob); |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 1762 | |
| 1763 | packet_send(); |
| 1764 | return 1; |
| 1765 | } |
| 1766 | |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1767 | #ifdef JPAKE |
| 1768 | int |
| 1769 | userauth_jpake(Authctxt *authctxt) |
| 1770 | { |
| 1771 | struct jpake_ctx *pctx; |
| 1772 | u_char *x1_proof, *x2_proof; |
| 1773 | u_int x1_proof_len, x2_proof_len; |
| 1774 | static int attempt = 0; /* XXX share with userauth_password's? */ |
| 1775 | |
| 1776 | if (attempt++ >= options.number_of_password_prompts) |
| 1777 | return 0; |
| 1778 | if (attempt != 1) |
| 1779 | error("Permission denied, please try again."); |
| 1780 | |
| 1781 | if (authctxt->methoddata != NULL) |
| 1782 | fatal("%s: authctxt->methoddata already set (%p)", |
| 1783 | __func__, authctxt->methoddata); |
| 1784 | |
| 1785 | authctxt->methoddata = pctx = jpake_new(); |
| 1786 | |
| 1787 | /* |
| 1788 | * Send request immediately, to get the protocol going while |
| 1789 | * we do the initial computations. |
| 1790 | */ |
| 1791 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1792 | packet_put_cstring(authctxt->server_user); |
| 1793 | packet_put_cstring(authctxt->service); |
| 1794 | packet_put_cstring(authctxt->method->name); |
| 1795 | packet_send(); |
| 1796 | packet_write_wait(); |
| 1797 | |
| 1798 | jpake_step1(pctx->grp, |
| 1799 | &pctx->client_id, &pctx->client_id_len, |
| 1800 | &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2, |
| 1801 | &x1_proof, &x1_proof_len, |
| 1802 | &x2_proof, &x2_proof_len); |
| 1803 | |
| 1804 | JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__)); |
| 1805 | |
| 1806 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1); |
| 1807 | packet_put_string(pctx->client_id, pctx->client_id_len); |
| 1808 | packet_put_bignum2(pctx->g_x1); |
| 1809 | packet_put_bignum2(pctx->g_x2); |
| 1810 | packet_put_string(x1_proof, x1_proof_len); |
| 1811 | packet_put_string(x2_proof, x2_proof_len); |
| 1812 | packet_send(); |
| 1813 | |
| 1814 | bzero(x1_proof, x1_proof_len); |
| 1815 | bzero(x2_proof, x2_proof_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1816 | free(x1_proof); |
| 1817 | free(x2_proof); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1818 | |
| 1819 | /* Expect step 1 packet from peer */ |
| 1820 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, |
| 1821 | input_userauth_jpake_server_step1); |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 1822 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, |
| 1823 | &input_userauth_success_unexpected); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1824 | |
| 1825 | return 1; |
| 1826 | } |
| 1827 | |
| 1828 | void |
| 1829 | userauth_jpake_cleanup(Authctxt *authctxt) |
| 1830 | { |
| 1831 | debug3("%s: clean up", __func__); |
| 1832 | if (authctxt->methoddata != NULL) { |
| 1833 | jpake_free(authctxt->methoddata); |
| 1834 | authctxt->methoddata = NULL; |
| 1835 | } |
Darren Tucker | 2f29a8c | 2009-10-24 11:47:58 +1100 | [diff] [blame] | 1836 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1837 | } |
| 1838 | #endif /* JPAKE */ |
| 1839 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1840 | /* find auth method */ |
| 1841 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1842 | /* |
| 1843 | * given auth method name, if configurable options permit this method fill |
| 1844 | * in auth_ident field and return true, otherwise return false. |
| 1845 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1846 | static int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1847 | authmethod_is_enabled(Authmethod *method) |
| 1848 | { |
| 1849 | if (method == NULL) |
| 1850 | return 0; |
| 1851 | /* return false if options indicate this method is disabled */ |
| 1852 | if (method->enabled == NULL || *method->enabled == 0) |
| 1853 | return 0; |
| 1854 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1855 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1856 | return 0; |
| 1857 | return 1; |
| 1858 | } |
| 1859 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1860 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1861 | authmethod_lookup(const char *name) |
| 1862 | { |
| 1863 | Authmethod *method = NULL; |
| 1864 | if (name != NULL) |
| 1865 | for (method = authmethods; method->name != NULL; method++) |
| 1866 | if (strcmp(name, method->name) == 0) |
| 1867 | return method; |
| 1868 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1869 | return NULL; |
| 1870 | } |
| 1871 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1872 | /* XXX internal state */ |
| 1873 | static Authmethod *current = NULL; |
| 1874 | static char *supported = NULL; |
| 1875 | static char *preferred = NULL; |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 1876 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1877 | /* |
| 1878 | * Given the authentication method list sent by the server, return the |
| 1879 | * 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] | 1880 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1881 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1882 | static Authmethod * |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1883 | authmethod_get(char *authlist) |
| 1884 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1885 | char *name = NULL; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 1886 | u_int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1887 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1888 | /* Use a suitable default if we're passed a nil list. */ |
| 1889 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1890 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1891 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1892 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1893 | debug3("start over, passed a different list %s", authlist); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1894 | free(supported); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1895 | supported = xstrdup(authlist); |
| 1896 | preferred = options.preferred_authentications; |
| 1897 | debug3("preferred %s", preferred); |
| 1898 | current = NULL; |
| 1899 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1900 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1901 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1902 | for (;;) { |
| 1903 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1904 | debug("No more authentication methods to try."); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1905 | current = NULL; |
| 1906 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1907 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1908 | preferred += next; |
| 1909 | debug3("authmethod_lookup %s", name); |
| 1910 | debug3("remaining preferred: %s", preferred); |
| 1911 | if ((current = authmethod_lookup(name)) != NULL && |
| 1912 | authmethod_is_enabled(current)) { |
| 1913 | debug3("authmethod_is_enabled %s", name); |
Ben Lindstrom | 1d568f9 | 2002-12-23 02:44:36 +0000 | [diff] [blame] | 1914 | debug("Next authentication method: %s", name); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame^] | 1915 | free(name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1916 | return current; |
| 1917 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1918 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1919 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1920 | |
Ben Lindstrom | 7907382 | 2001-07-04 03:42:30 +0000 | [diff] [blame] | 1921 | static char * |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1922 | authmethods_get(void) |
| 1923 | { |
| 1924 | Authmethod *method = NULL; |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1925 | Buffer b; |
| 1926 | char *list; |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1927 | |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1928 | buffer_init(&b); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1929 | for (method = authmethods; method->name != NULL; method++) { |
| 1930 | if (authmethod_is_enabled(method)) { |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1931 | if (buffer_len(&b) > 0) |
| 1932 | buffer_append(&b, ",", 1); |
| 1933 | buffer_append(&b, method->name, strlen(method->name)); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1934 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1935 | } |
Damien Miller | 0e3b872 | 2002-01-22 23:26:38 +1100 | [diff] [blame] | 1936 | buffer_append(&b, "\0", 1); |
| 1937 | list = xstrdup(buffer_ptr(&b)); |
| 1938 | buffer_free(&b); |
| 1939 | return list; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1940 | } |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1941 | |