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