blob: dc00b4cd04d7a895579387b6d27cf89bf1b8955f [file] [log] [blame]
dtucker@openbsd.org66be4fe2017-03-10 03:53:11 +00001/* $OpenBSD: sshconnect1.c,v 1.80 2017/03/10 03:53:11 dtucker Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Millereba71ba2000-04-29 23:57:08 +10006 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Millereba71ba2000-04-29 23:57:08 +100014 */
15
16#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100017
Damien Miller76c04802015-01-13 19:38:18 +110018#ifdef WITH_SSH1
19
Damien Millerd7834352006-08-05 12:39:39 +100020#include <sys/types.h>
21#include <sys/socket.h>
22
Damien Millereba71ba2000-04-29 23:57:08 +100023#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100024
djm@openbsd.org141efe42015-01-14 20:05:27 +000025#include <errno.h>
Damien Millerded319c2006-09-01 15:38:36 +100026#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100027#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100028#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100029#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <signal.h>
31#include <pwd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032
Damien Millerd7834352006-08-05 12:39:39 +100033#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh.h"
35#include "ssh1.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "buffer.h"
38#include "packet.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "key.h"
40#include "cipher.h"
Darren Tuckere14e0052004-05-13 16:30:44 +100041#include "kex.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100044#include "misc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "readconf.h"
Damien Miller994cf142000-07-21 10:19:44 +100046#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "sshconnect.h"
48#include "authfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "hostfile.h"
Ben Lindstromec95ed92001-07-04 04:21:14 +000051#include "auth.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110052#include "digest.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000053#include "ssherr.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054
55/* Session id for the current session. */
Ben Lindstrom46c16222000-12-22 01:43:59 +000056u_char session_id[16];
57u_int supported_authentications = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100058
59extern Options options;
60extern char *__progname;
61
62/*
63 * Checks if the user has an authentication agent, and if so, tries to
64 * authenticate using the agent.
65 */
Ben Lindstrombba81212001-06-25 05:01:22 +000066static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000067try_agent_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +100068{
djm@openbsd.org141efe42015-01-14 20:05:27 +000069 int r, type, agent_fd, ret = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +000070 u_char response[16];
djm@openbsd.org141efe42015-01-14 20:05:27 +000071 size_t i;
Damien Millerad833b32000-08-23 10:46:23 +100072 BIGNUM *challenge;
djm@openbsd.org141efe42015-01-14 20:05:27 +000073 struct ssh_identitylist *idlist = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100074
75 /* Get connection to the agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +000076 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
77 if (r != SSH_ERR_AGENT_NOT_PRESENT)
78 debug("%s: ssh_get_authentication_socket: %s",
79 __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +100080 return 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +000081 }
Damien Millereba71ba2000-04-29 23:57:08 +100082
Damien Millerda755162002-01-22 23:09:22 +110083 if ((challenge = BN_new()) == NULL)
84 fatal("try_agent_authentication: BN_new failed");
Damien Millereba71ba2000-04-29 23:57:08 +100085
djm@openbsd.org141efe42015-01-14 20:05:27 +000086 /* Loop through identities served by the agent. */
87 if ((r = ssh_fetch_identitylist(agent_fd, 1, &idlist)) != 0) {
88 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
89 debug("%s: ssh_fetch_identitylist: %s",
90 __func__, ssh_err(r));
91 goto out;
92 }
93 for (i = 0; i < idlist->nkeys; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +100094 /* Try this identity. */
djm@openbsd.org141efe42015-01-14 20:05:27 +000095 debug("Trying RSA authentication via agent with '%.100s'",
96 idlist->comments[i]);
Damien Millereba71ba2000-04-29 23:57:08 +100097
98 /* Tell the server that we are willing to authenticate using this key. */
99 packet_start(SSH_CMSG_AUTH_RSA);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000100 packet_put_bignum(idlist->keys[i]->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000101 packet_send();
102 packet_write_wait();
103
104 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100105 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000106
Damien Miller788f2122005-11-05 15:14:59 +1100107 /* The server sends failure if it doesn't like our key or
Damien Millereba71ba2000-04-29 23:57:08 +1000108 does not support RSA authentication. */
109 if (type == SSH_SMSG_FAILURE) {
110 debug("Server refused our key.");
111 continue;
112 }
113 /* Otherwise it should have sent a challenge. */
114 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
115 packet_disconnect("Protocol error during RSA authentication: %d",
116 type);
117
Damien Millerd432ccf2002-01-22 23:14:44 +1100118 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100119 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000120
121 debug("Received RSA challenge from server.");
122
123 /* Ask the agent to decrypt the challenge. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000124 if ((r = ssh_decrypt_challenge(agent_fd, idlist->keys[i],
125 challenge, session_id, response)) != 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000126 /*
127 * The agent failed to authenticate this identifier
128 * although it advertised it supports this. Just
129 * return a wrong value.
130 */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000131 logit("Authentication agent failed to decrypt "
132 "challenge: %s", ssh_err(r));
Damien Millera5103f42014-02-04 11:20:14 +1100133 explicit_bzero(response, sizeof(response));
Damien Millereba71ba2000-04-29 23:57:08 +1000134 }
135 debug("Sending response to RSA challenge.");
136
137 /* Send the decrypted challenge back to the server. */
138 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
139 for (i = 0; i < 16; i++)
140 packet_put_char(response[i]);
141 packet_send();
142 packet_write_wait();
143
144 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100145 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000146
djm@openbsd.org141efe42015-01-14 20:05:27 +0000147 /*
148 * The server returns success if it accepted the
149 * authentication.
150 */
Damien Millereba71ba2000-04-29 23:57:08 +1000151 if (type == SSH_SMSG_SUCCESS) {
Damien Millerad833b32000-08-23 10:46:23 +1000152 debug("RSA authentication accepted by server.");
djm@openbsd.org141efe42015-01-14 20:05:27 +0000153 ret = 1;
154 break;
155 } else if (type != SSH_SMSG_FAILURE)
156 packet_disconnect("Protocol error waiting RSA auth "
157 "response: %d", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000158 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000159 if (ret != 1)
160 debug("RSA authentication using agent refused.");
161 out:
162 ssh_free_identitylist(idlist);
163 ssh_close_authentication_socket(agent_fd);
Damien Millereba71ba2000-04-29 23:57:08 +1000164 BN_clear_free(challenge);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000165 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000166}
167
168/*
169 * Computes the proper response to a RSA challenge, and sends the response to
170 * the server.
171 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000172static void
Damien Millereba71ba2000-04-29 23:57:08 +1000173respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
174{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000175 u_char buf[32], response[16];
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100176 struct ssh_digest_ctx *md;
Damien Millereba71ba2000-04-29 23:57:08 +1000177 int i, len;
178
179 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100180 /* XXX think about Bleichenbacher, too */
Damien Miller86687062014-07-02 15:28:02 +1000181 if (rsa_private_decrypt(challenge, challenge, prv) != 0)
Damien Miller7650bc62001-01-30 09:27:26 +1100182 packet_disconnect(
183 "respond_to_rsa_challenge: rsa_private_decrypt failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000184
185 /* Compute the response. */
186 /* The response is MD5 of decrypted challenge plus session id. */
187 len = BN_num_bytes(challenge);
Damien Millereccb9de2005-06-17 12:59:34 +1000188 if (len <= 0 || (u_int)len > sizeof(buf))
Damien Miller7650bc62001-01-30 09:27:26 +1100189 packet_disconnect(
190 "respond_to_rsa_challenge: bad challenge length %d", len);
Damien Millereba71ba2000-04-29 23:57:08 +1000191
192 memset(buf, 0, sizeof(buf));
193 BN_bn2bin(challenge, buf + sizeof(buf) - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100194 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
195 ssh_digest_update(md, buf, 32) < 0 ||
196 ssh_digest_update(md, session_id, 16) < 0 ||
197 ssh_digest_final(md, response, sizeof(response)) < 0)
198 fatal("%s: md5 failed", __func__);
199 ssh_digest_free(md);
Damien Millereba71ba2000-04-29 23:57:08 +1000200
201 debug("Sending response to host key RSA challenge.");
202
203 /* Send the response back to the server. */
204 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
205 for (i = 0; i < 16; i++)
206 packet_put_char(response[i]);
207 packet_send();
208 packet_write_wait();
209
Damien Millera5103f42014-02-04 11:20:14 +1100210 explicit_bzero(buf, sizeof(buf));
211 explicit_bzero(response, sizeof(response));
212 explicit_bzero(&md, sizeof(md));
Damien Millereba71ba2000-04-29 23:57:08 +1000213}
214
215/*
216 * Checks if the user has authentication file, and if so, tries to authenticate
217 * the user using it.
218 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static int
Ben Lindstromc5b68002001-07-04 04:52:03 +0000220try_rsa_authentication(int idx)
Damien Millereba71ba2000-04-29 23:57:08 +1000221{
222 BIGNUM *challenge;
Ben Lindstrom05209452001-06-25 05:16:02 +0000223 Key *public, *private;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000224 char buf[300], *passphrase = NULL, *comment, *authfile;
Darren Tucker232b76f2006-05-06 17:41:51 +1000225 int i, perm_ok = 1, type, quit;
Damien Millereba71ba2000-04-29 23:57:08 +1000226
Ben Lindstromc5b68002001-07-04 04:52:03 +0000227 public = options.identity_keys[idx];
228 authfile = options.identity_files[idx];
229 comment = xstrdup(authfile);
230
Damien Millereba71ba2000-04-29 23:57:08 +1000231 debug("Trying RSA authentication with key '%.100s'", comment);
232
233 /* Tell the server that we are willing to authenticate using this key. */
234 packet_start(SSH_CMSG_AUTH_RSA);
235 packet_put_bignum(public->rsa->n);
236 packet_send();
237 packet_write_wait();
238
Damien Millereba71ba2000-04-29 23:57:08 +1000239 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100240 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000241
242 /*
Damien Miller788f2122005-11-05 15:14:59 +1100243 * The server responds with failure if it doesn't like our key or
244 * doesn't support RSA authentication.
Damien Millereba71ba2000-04-29 23:57:08 +1000245 */
246 if (type == SSH_SMSG_FAILURE) {
247 debug("Server refused our key.");
Darren Tuckera627d422013-06-02 07:31:17 +1000248 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000249 return 0;
250 }
251 /* Otherwise, the server should respond with a challenge. */
252 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
253 packet_disconnect("Protocol error during RSA authentication: %d", type);
254
255 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100256 if ((challenge = BN_new()) == NULL)
257 fatal("try_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100258 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100259 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000260
261 debug("Received RSA challenge from server.");
262
Damien Millereba71ba2000-04-29 23:57:08 +1000263 /*
Ben Lindstromc5b68002001-07-04 04:52:03 +0000264 * If the key is not stored in external hardware, we have to
265 * load the private key. Try first with empty passphrase; if it
Damien Millereba71ba2000-04-29 23:57:08 +1000266 * fails, ask for a passphrase.
267 */
Damien Miller86687062014-07-02 15:28:02 +1000268 if (public->flags & SSHKEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000269 private = public;
270 else
Darren Tucker232b76f2006-05-06 17:41:51 +1000271 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
272 &perm_ok);
273 if (private == NULL && !options.batch_mode && perm_ok) {
Ben Lindstrom05209452001-06-25 05:16:02 +0000274 snprintf(buf, sizeof(buf),
275 "Enter passphrase for RSA key '%.100s': ", comment);
276 for (i = 0; i < options.number_of_password_prompts; i++) {
Damien Millereba71ba2000-04-29 23:57:08 +1000277 passphrase = read_passphrase(buf, 0);
Ben Lindstrom05209452001-06-25 05:16:02 +0000278 if (strcmp(passphrase, "") != 0) {
279 private = key_load_private_type(KEY_RSA1,
Darren Tucker232b76f2006-05-06 17:41:51 +1000280 authfile, passphrase, NULL, NULL);
Ben Lindstrom05209452001-06-25 05:16:02 +0000281 quit = 0;
282 } else {
283 debug2("no passphrase given, try next key");
284 quit = 1;
285 }
Ben Lindstrom05209452001-06-25 05:16:02 +0000286 if (private != NULL || quit)
287 break;
288 debug2("bad passphrase given, try again...");
Damien Millereba71ba2000-04-29 23:57:08 +1000289 }
Damien Millereba71ba2000-04-29 23:57:08 +1000290 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000291
292 if (private != NULL)
293 maybe_add_key_to_agent(authfile, private, comment, passphrase);
294
295 if (passphrase != NULL) {
296 explicit_bzero(passphrase, strlen(passphrase));
297 free(passphrase);
298 }
299
Damien Millereba71ba2000-04-29 23:57:08 +1000300 /* We no longer need the comment. */
Darren Tuckera627d422013-06-02 07:31:17 +1000301 free(comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000302
Ben Lindstrom05209452001-06-25 05:16:02 +0000303 if (private == NULL) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000304 if (!options.batch_mode && perm_ok)
Ben Lindstrom05209452001-06-25 05:16:02 +0000305 error("Bad passphrase.");
306
307 /* Send a dummy response packet to avoid protocol error. */
308 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
309 for (i = 0; i < 16; i++)
310 packet_put_char(0);
311 packet_send();
312 packet_write_wait();
313
314 /* Expect the server to reject it... */
Damien Millerdff50992002-01-22 23:16:32 +1100315 packet_read_expect(SSH_SMSG_FAILURE);
Ben Lindstrom05209452001-06-25 05:16:02 +0000316 BN_clear_free(challenge);
317 return 0;
318 }
319
Damien Millereba71ba2000-04-29 23:57:08 +1000320 /* Compute and send a response to the challenge. */
321 respond_to_rsa_challenge(challenge, private->rsa);
322
Ben Lindstromc5b68002001-07-04 04:52:03 +0000323 /* Destroy the private key unless it in external hardware. */
Damien Miller86687062014-07-02 15:28:02 +1000324 if (!(private->flags & SSHKEY_FLAG_EXT))
Ben Lindstromc5b68002001-07-04 04:52:03 +0000325 key_free(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000326
327 /* We no longer need the challenge. */
328 BN_clear_free(challenge);
329
330 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100331 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000332 if (type == SSH_SMSG_SUCCESS) {
333 debug("RSA authentication accepted by server.");
334 return 1;
335 }
336 if (type != SSH_SMSG_FAILURE)
337 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
338 debug("RSA authentication refused.");
339 return 0;
340}
341
342/*
343 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
344 * authentication and RSA host authentication.
345 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static int
Ben Lindstromd0fca422001-03-26 13:44:06 +0000347try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
Damien Millereba71ba2000-04-29 23:57:08 +1000348{
349 int type;
350 BIGNUM *challenge;
Damien Millereba71ba2000-04-29 23:57:08 +1000351
352 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
353
354 /* Tell the server that we are willing to authenticate using this key. */
355 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000356 packet_put_cstring(local_user);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000357 packet_put_int(BN_num_bits(host_key->rsa->n));
358 packet_put_bignum(host_key->rsa->e);
359 packet_put_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000360 packet_send();
361 packet_write_wait();
362
363 /* Wait for server's response. */
Damien Millerdff50992002-01-22 23:16:32 +1100364 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000365
366 /* The server responds with failure if it doesn't admit our
367 .rhosts authentication or doesn't know our host key. */
368 if (type == SSH_SMSG_FAILURE) {
369 debug("Server refused our rhosts authentication or host key.");
370 return 0;
371 }
372 /* Otherwise, the server should respond with a challenge. */
373 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
374 packet_disconnect("Protocol error during RSA authentication: %d", type);
375
376 /* Get the challenge from the packet. */
Damien Millerda755162002-01-22 23:09:22 +1100377 if ((challenge = BN_new()) == NULL)
378 fatal("try_rhosts_rsa_authentication: BN_new failed");
Damien Millerd432ccf2002-01-22 23:14:44 +1100379 packet_get_bignum(challenge);
Damien Miller48b03fc2002-01-22 23:11:40 +1100380 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000381
382 debug("Received RSA challenge for host key from server.");
383
384 /* Compute a response to the challenge. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000385 respond_to_rsa_challenge(challenge, host_key->rsa);
Damien Millereba71ba2000-04-29 23:57:08 +1000386
387 /* We no longer need the challenge. */
388 BN_clear_free(challenge);
389
390 /* Wait for response from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100391 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000392 if (type == SSH_SMSG_SUCCESS) {
393 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
394 return 1;
395 }
396 if (type != SSH_SMSG_FAILURE)
397 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
398 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
399 return 0;
400}
401
Damien Millereba71ba2000-04-29 23:57:08 +1000402/*
403 * Tries to authenticate with any string-based challenge/response system.
404 * Note that the client code is not tied to s/key or TIS.
405 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000406static int
Ben Lindstrom551ea372001-06-05 18:56:16 +0000407try_challenge_response_authentication(void)
Damien Millereba71ba2000-04-29 23:57:08 +1000408{
409 int type, i;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000410 u_int clen;
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000411 char prompt[1024];
Damien Millereba71ba2000-04-29 23:57:08 +1000412 char *challenge, *response;
Ben Lindstrombdfb4df2001-10-03 17:12:43 +0000413
414 debug("Doing challenge response authentication.");
415
Damien Millereba71ba2000-04-29 23:57:08 +1000416 for (i = 0; i < options.number_of_password_prompts; i++) {
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000417 /* request a challenge */
418 packet_start(SSH_CMSG_AUTH_TIS);
419 packet_send();
420 packet_write_wait();
421
Damien Millerdff50992002-01-22 23:16:32 +1100422 type = packet_read();
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000423 if (type != SSH_SMSG_FAILURE &&
424 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
425 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000426 "to SSH_CMSG_AUTH_TIS", type);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000427 }
428 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000429 debug("No challenge.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000430 return 0;
431 }
432 challenge = packet_get_string(&clen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100433 packet_check_eom();
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000434 snprintf(prompt, sizeof prompt, "%s%s", challenge,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100435 strchr(challenge, '\n') ? "" : "\nResponse: ");
Darren Tuckera627d422013-06-02 07:31:17 +1000436 free(challenge);
Damien Millereba71ba2000-04-29 23:57:08 +1000437 if (i != 0)
438 error("Permission denied, please try again.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000439 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000440 logit("WARNING: Encryption is disabled! "
Damien Millerf61c0152002-04-23 20:56:02 +1000441 "Response will be transmitted in clear text.");
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000442 response = read_passphrase(prompt, 0);
443 if (strcmp(response, "") == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000444 free(response);
Ben Lindstroma65c78a2000-12-10 22:57:30 +0000445 break;
446 }
Damien Millereba71ba2000-04-29 23:57:08 +1000447 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
Damien Miller79438cc2001-02-16 12:34:57 +1100448 ssh_put_password(response);
Damien Millera5103f42014-02-04 11:20:14 +1100449 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +1000450 free(response);
Damien Millereba71ba2000-04-29 23:57:08 +1000451 packet_send();
452 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100453 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000454 if (type == SSH_SMSG_SUCCESS)
455 return 1;
456 if (type != SSH_SMSG_FAILURE)
457 packet_disconnect("Protocol error: got %d in response "
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000458 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
Damien Millereba71ba2000-04-29 23:57:08 +1000459 }
460 /* failure */
461 return 0;
462}
463
464/*
465 * Tries to authenticate with plain passwd authentication.
466 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000467static int
Damien Millereba71ba2000-04-29 23:57:08 +1000468try_password_authentication(char *prompt)
469{
Damien Millerdff50992002-01-22 23:16:32 +1100470 int type, i;
Damien Millereba71ba2000-04-29 23:57:08 +1000471 char *password;
472
473 debug("Doing password authentication.");
474 if (options.cipher == SSH_CIPHER_NONE)
Damien Miller996acd22003-04-09 20:59:48 +1000475 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
Damien Millereba71ba2000-04-29 23:57:08 +1000476 for (i = 0; i < options.number_of_password_prompts; i++) {
477 if (i != 0)
478 error("Permission denied, please try again.");
479 password = read_passphrase(prompt, 0);
480 packet_start(SSH_CMSG_AUTH_PASSWORD);
Damien Miller79438cc2001-02-16 12:34:57 +1100481 ssh_put_password(password);
Damien Millera5103f42014-02-04 11:20:14 +1100482 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000483 free(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000484 packet_send();
485 packet_write_wait();
486
Damien Millerdff50992002-01-22 23:16:32 +1100487 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000488 if (type == SSH_SMSG_SUCCESS)
489 return 1;
490 if (type != SSH_SMSG_FAILURE)
491 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
492 }
493 /* failure */
494 return 0;
495}
496
497/*
498 * SSH1 key exchange
499 */
500void
501ssh_kex(char *host, struct sockaddr *hostaddr)
502{
503 int i;
504 BIGNUM *key;
Damien Millerda755162002-01-22 23:09:22 +1100505 Key *host_key, *server_key;
Damien Millereba71ba2000-04-29 23:57:08 +1000506 int bits, rbits;
507 int ssh_cipher_default = SSH_CIPHER_3DES;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000508 u_char session_key[SSH_SESSION_KEY_LENGTH];
509 u_char cookie[8];
510 u_int supported_ciphers;
511 u_int server_flags, client_flags;
Damien Millereba71ba2000-04-29 23:57:08 +1000512
513 debug("Waiting for server public key.");
514
515 /* Wait for a public key packet from the server. */
Damien Millerdff50992002-01-22 23:16:32 +1100516 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
Damien Millereba71ba2000-04-29 23:57:08 +1000517
518 /* Get cookie from the packet. */
519 for (i = 0; i < 8; i++)
520 cookie[i] = packet_get_char();
521
522 /* Get the public key. */
dtucker@openbsd.org66be4fe2017-03-10 03:53:11 +0000523 if ((server_key = key_new(KEY_RSA1)) == NULL)
524 fatal("%s: key_new(KEY_RSA1) failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100525 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100526 packet_get_bignum(server_key->rsa->e);
527 packet_get_bignum(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000528
Damien Millerda755162002-01-22 23:09:22 +1100529 rbits = BN_num_bits(server_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000530 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000531 logit("Warning: Server lies about size of server public key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000532 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000533 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000534 }
535 /* Get the host key. */
dtucker@openbsd.org66be4fe2017-03-10 03:53:11 +0000536 if ((host_key = key_new(KEY_RSA1)) == NULL)
537 fatal("%s: key_new(KEY_RSA1) failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100538 bits = packet_get_int();
Damien Millerd432ccf2002-01-22 23:14:44 +1100539 packet_get_bignum(host_key->rsa->e);
540 packet_get_bignum(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000541
Damien Millerda755162002-01-22 23:09:22 +1100542 rbits = BN_num_bits(host_key->rsa->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000543 if (bits != rbits) {
Damien Miller996acd22003-04-09 20:59:48 +1000544 logit("Warning: Server lies about size of server host key: "
Damien Millereba71ba2000-04-29 23:57:08 +1000545 "actual size is %d bits vs. announced %d.", rbits, bits);
Damien Miller996acd22003-04-09 20:59:48 +1000546 logit("Warning: This may be due to an old implementation of ssh.");
Damien Millereba71ba2000-04-29 23:57:08 +1000547 }
548
549 /* Get protocol flags. */
550 server_flags = packet_get_int();
551 packet_set_protocol_flags(server_flags);
552
553 supported_ciphers = packet_get_int();
554 supported_authentications = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +1100555 packet_check_eom();
Damien Millereba71ba2000-04-29 23:57:08 +1000556
557 debug("Received server public key (%d bits) and host key (%d bits).",
Damien Millerda755162002-01-22 23:09:22 +1100558 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
Damien Millereba71ba2000-04-29 23:57:08 +1000559
Damien Millerda755162002-01-22 23:09:22 +1100560 if (verify_host_key(host, hostaddr, host_key) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000561 fatal("Host key verification failed.");
Damien Millereba71ba2000-04-29 23:57:08 +1000562
563 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
564
Darren Tuckere14e0052004-05-13 16:30:44 +1000565 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
Damien Millereba71ba2000-04-29 23:57:08 +1000566
Damien Millereba71ba2000-04-29 23:57:08 +1000567 /*
568 * Generate an encryption key for the session. The key is a 256 bit
569 * random number, interpreted as a 32-byte key, with the least
570 * significant 8 bits being the first byte of the key.
571 */
natano@openbsd.org49271082016-09-19 07:52:42 +0000572 arc4random_buf(session_key, sizeof(session_key));
Damien Millereba71ba2000-04-29 23:57:08 +1000573
574 /*
575 * According to the protocol spec, the first byte of the session key
576 * is the highest byte of the integer. The session key is xored with
577 * the first 16 bytes of the session id.
578 */
Damien Millerda755162002-01-22 23:09:22 +1100579 if ((key = BN_new()) == NULL)
Darren Tucker0bc85572006-11-07 23:14:41 +1100580 fatal("ssh_kex: BN_new failed");
581 if (BN_set_word(key, 0) == 0)
582 fatal("ssh_kex: BN_set_word failed");
Damien Millereba71ba2000-04-29 23:57:08 +1000583 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
Darren Tucker0bc85572006-11-07 23:14:41 +1100584 if (BN_lshift(key, key, 8) == 0)
585 fatal("ssh_kex: BN_lshift failed");
586 if (i < 16) {
587 if (BN_add_word(key, session_key[i] ^ session_id[i])
588 == 0)
589 fatal("ssh_kex: BN_add_word failed");
590 } else {
591 if (BN_add_word(key, session_key[i]) == 0)
592 fatal("ssh_kex: BN_add_word failed");
593 }
Damien Millereba71ba2000-04-29 23:57:08 +1000594 }
595
596 /*
597 * Encrypt the integer using the public key and host key of the
598 * server (key with smaller modulus first).
599 */
Damien Millerda755162002-01-22 23:09:22 +1100600 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
Damien Millereba71ba2000-04-29 23:57:08 +1000601 /* Public key has smaller modulus. */
Damien Millerda755162002-01-22 23:09:22 +1100602 if (BN_num_bits(host_key->rsa->n) <
603 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
604 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100606 BN_num_bits(host_key->rsa->n),
607 BN_num_bits(server_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100608 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000609 }
Damien Miller86687062014-07-02 15:28:02 +1000610 if (rsa_public_encrypt(key, key, server_key->rsa) != 0 ||
611 rsa_public_encrypt(key, key, host_key->rsa) != 0)
612 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000613 } else {
614 /* Host key has smaller modulus (or they are equal). */
Damien Millerda755162002-01-22 23:09:22 +1100615 if (BN_num_bits(server_key->rsa->n) <
616 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
617 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618 "SSH_KEY_BITS_RESERVED %d",
Damien Millerda755162002-01-22 23:09:22 +1100619 BN_num_bits(server_key->rsa->n),
620 BN_num_bits(host_key->rsa->n),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100621 SSH_KEY_BITS_RESERVED);
Damien Millereba71ba2000-04-29 23:57:08 +1000622 }
Damien Miller86687062014-07-02 15:28:02 +1000623 if (rsa_public_encrypt(key, key, host_key->rsa) != 0 ||
624 rsa_public_encrypt(key, key, server_key->rsa) != 0)
625 fatal("%s: rsa_public_encrypt failed", __func__);
Damien Millereba71ba2000-04-29 23:57:08 +1000626 }
627
628 /* Destroy the public keys since we no longer need them. */
Damien Millerda755162002-01-22 23:09:22 +1100629 key_free(server_key);
630 key_free(host_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000631
Damien Millere39cacc2000-11-29 12:18:44 +1100632 if (options.cipher == SSH_CIPHER_NOT_SET) {
633 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
634 options.cipher = ssh_cipher_default;
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000635 } else if (options.cipher == SSH_CIPHER_INVALID ||
Damien Millere39cacc2000-11-29 12:18:44 +1100636 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
Damien Miller996acd22003-04-09 20:59:48 +1000637 logit("No valid SSH1 cipher, using %.100s instead.",
Damien Miller874d77b2000-10-14 16:23:11 +1100638 cipher_name(ssh_cipher_default));
639 options.cipher = ssh_cipher_default;
Damien Millereba71ba2000-04-29 23:57:08 +1000640 }
641 /* Check that the selected cipher is supported. */
642 if (!(supported_ciphers & (1 << options.cipher)))
643 fatal("Selected cipher type %.100s not supported by server.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100644 cipher_name(options.cipher));
Damien Millereba71ba2000-04-29 23:57:08 +1000645
646 debug("Encryption type: %.100s", cipher_name(options.cipher));
647
648 /* Send the encrypted session key to the server. */
649 packet_start(SSH_CMSG_SESSION_KEY);
650 packet_put_char(options.cipher);
651
652 /* Send the cookie back to the server. */
653 for (i = 0; i < 8; i++)
654 packet_put_char(cookie[i]);
655
656 /* Send and destroy the encrypted encryption key integer. */
657 packet_put_bignum(key);
658 BN_clear_free(key);
659
660 /* Send protocol flags. */
661 packet_put_int(client_flags);
662
663 /* Send the packet now. */
664 packet_send();
665 packet_write_wait();
666
667 debug("Sent encrypted session key.");
668
669 /* Set the encryption key. */
670 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
671
Damien Millera5103f42014-02-04 11:20:14 +1100672 /*
673 * We will no longer need the session key here.
674 * Destroy any extra copies.
675 */
676 explicit_bzero(session_key, sizeof(session_key));
Damien Millereba71ba2000-04-29 23:57:08 +1000677
678 /*
679 * Expect a success message from the server. Note that this message
680 * will be received in encrypted form.
681 */
Damien Millerdff50992002-01-22 23:16:32 +1100682 packet_read_expect(SSH_SMSG_SUCCESS);
Damien Millereba71ba2000-04-29 23:57:08 +1000683
684 debug("Received encrypted confirmation.");
685}
686
687/*
688 * Authenticate user
689 */
690void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000691ssh_userauth1(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000692 Sensitive *sensitive)
Damien Millereba71ba2000-04-29 23:57:08 +1000693{
694 int i, type;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100695
Damien Millereba71ba2000-04-29 23:57:08 +1000696 if (supported_authentications == 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000697 fatal("ssh_userauth1: server supports no auth methods");
Damien Millereba71ba2000-04-29 23:57:08 +1000698
699 /* Send the name of the user to log in as on the server. */
700 packet_start(SSH_CMSG_USER);
Ben Lindstrom664408d2001-06-09 01:42:01 +0000701 packet_put_cstring(server_user);
Damien Millereba71ba2000-04-29 23:57:08 +1000702 packet_send();
703 packet_write_wait();
704
705 /*
706 * The server should respond with success if no authentication is
707 * needed (the user has no password). Otherwise the server responds
708 * with failure.
709 */
Damien Millerdff50992002-01-22 23:16:32 +1100710 type = packet_read();
Damien Millereba71ba2000-04-29 23:57:08 +1000711
712 /* check whether the connection was accepted without authentication. */
713 if (type == SSH_SMSG_SUCCESS)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000714 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000715 if (type != SSH_SMSG_FAILURE)
Ben Lindstromec95ed92001-07-04 04:21:14 +0000716 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100717
Damien Millereba71ba2000-04-29 23:57:08 +1000718 /*
Damien Millereba71ba2000-04-29 23:57:08 +1000719 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
720 * authentication.
721 */
722 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000723 options.rhosts_rsa_authentication) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000724 for (i = 0; i < sensitive->nkeys; i++) {
725 if (sensitive->keys[i] != NULL &&
726 sensitive->keys[i]->type == KEY_RSA1 &&
727 try_rhosts_rsa_authentication(local_user,
728 sensitive->keys[i]))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000729 goto success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000730 }
Damien Millereba71ba2000-04-29 23:57:08 +1000731 }
732 /* Try RSA authentication if the server supports it. */
733 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
734 options.rsa_authentication) {
735 /*
736 * Try RSA authentication using the authentication agent. The
737 * agent is tried first because no passphrase is needed for
738 * it, whereas identity files may require passphrases.
739 */
740 if (try_agent_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000741 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000742
743 /* Try RSA authentication for each identity. */
744 for (i = 0; i < options.num_identity_files; i++)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000745 if (options.identity_keys[i] != NULL &&
746 options.identity_keys[i]->type == KEY_RSA1 &&
Ben Lindstromc5b68002001-07-04 04:52:03 +0000747 try_rsa_authentication(i))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000748 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000749 }
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000750 /* Try challenge response authentication if the server supports it. */
Damien Millereba71ba2000-04-29 23:57:08 +1000751 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
Ben Lindstrom551ea372001-06-05 18:56:16 +0000752 options.challenge_response_authentication && !options.batch_mode) {
753 if (try_challenge_response_authentication())
Ben Lindstromec95ed92001-07-04 04:21:14 +0000754 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000755 }
756 /* Try password authentication if the server supports it. */
757 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
758 options.password_authentication && !options.batch_mode) {
759 char prompt[80];
760
Ben Lindstrome0557162001-02-11 00:00:24 +0000761 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Millereba71ba2000-04-29 23:57:08 +1000762 server_user, host);
763 if (try_password_authentication(prompt))
Ben Lindstromec95ed92001-07-04 04:21:14 +0000764 goto success;
Damien Millereba71ba2000-04-29 23:57:08 +1000765 }
766 /* All authentication methods have failed. Exit with an error message. */
767 fatal("Permission denied.");
768 /* NOTREACHED */
Ben Lindstromec95ed92001-07-04 04:21:14 +0000769
770 success:
Damien Miller40eb1d82001-07-14 12:16:59 +1000771 return; /* need statement after label */
Damien Millereba71ba2000-04-29 23:57:08 +1000772}
Damien Miller76c04802015-01-13 19:38:18 +1100773
774#endif /* WITH_SSH1 */