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