]> andersk Git - openssh.git/blame - sshd.c
- (djm) OpenBSD CVS Sync
[openssh.git] / sshd.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
bcbf86ec 5 * This program is the ssh daemon. It listens for connections from clients,
6 * and performs authentication, executes use commands or shell, and forwards
5260325f 7 * information to/from the application to the user client over an encrypted
bcbf86ec 8 * connection. This can also handle forwarding of X11, TCP/IP, and
9 * authentication agent connections.
e78a59f5 10 *
bcbf86ec 11 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 *
17 * SSH2 implementation:
18 *
19 * Copyright (c) 2000 Markus Friedl. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5260325f 40 */
8efc0c15 41
42#include "includes.h"
4047d868 43RCSID("$OpenBSD: sshd.c,v 1.183 2001/03/28 21:59:41 provos Exp $");
8efc0c15 44
42f11eb2 45#include <openssl/dh.h>
46#include <openssl/bn.h>
47#include <openssl/hmac.h>
48
49#include "ssh.h"
50#include "ssh1.h"
51#include "ssh2.h"
8efc0c15 52#include "xmalloc.h"
53#include "rsa.h"
1729c161 54#include "sshpty.h"
8efc0c15 55#include "packet.h"
8efc0c15 56#include "mpaux.h"
42f11eb2 57#include "log.h"
8efc0c15 58#include "servconf.h"
59#include "uidswap.h"
60#include "compat.h"
7368a6c8 61#include "buffer.h"
42f11eb2 62#include "cipher.h"
e78a59f5 63#include "kex.h"
7368a6c8 64#include "key.h"
94ec8c6b 65#include "dh.h"
e78a59f5 66#include "myproposal.h"
a306f2dd 67#include "authfile.h"
42f11eb2 68#include "pathnames.h"
69#include "atomicio.h"
70#include "canohost.h"
71#include "auth.h"
72#include "misc.h"
8efc0c15 73
74#ifdef LIBWRAP
75#include <tcpd.h>
76#include <syslog.h>
77int allow_severity = LOG_INFO;
78int deny_severity = LOG_WARNING;
79#endif /* LIBWRAP */
80
81#ifndef O_NOCTTY
82#define O_NOCTTY 0
83#endif
84
260d427b 85#ifdef HAVE___PROGNAME
86extern char *__progname;
87#else
88char *__progname;
89#endif
90
8efc0c15 91/* Server configuration options. */
92ServerOptions options;
93
94/* Name of the server configuration file. */
42f11eb2 95char *config_file_name = _PATH_SERVER_CONFIG_FILE;
8efc0c15 96
6ae2364d 97/*
48e671d5 98 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
99 * Default value is AF_UNSPEC means both IPv4 and IPv6.
100 */
59e76f33 101#ifdef IPV4_DEFAULT
102int IPv4or6 = AF_INET;
103#else
48e671d5 104int IPv4or6 = AF_UNSPEC;
59e76f33 105#endif
48e671d5 106
5260325f 107/*
108 * Debug mode flag. This can be set on the command line. If debug
109 * mode is enabled, extra debugging output will be sent to the system
110 * log, the daemon will not go to background, and will exit after processing
111 * the first connection.
112 */
8efc0c15 113int debug_flag = 0;
114
115/* Flag indicating that the daemon is being started from inetd. */
116int inetd_flag = 0;
117
0b6fbf03 118/* Flag indicating that sshd should not detach and become a daemon. */
119int no_daemon_flag = 0;
120
6a17f9c2 121/* debug goes to stderr unless inetd_flag is set */
122int log_stderr = 0;
123
8efc0c15 124/* Saved arguments to main(). */
125char **saved_argv;
4d33e531 126int saved_argc;
8efc0c15 127
aa3378df 128/*
48e671d5 129 * The sockets that the server is listening; this is used in the SIGHUP
130 * signal handler.
aa3378df 131 */
48e671d5 132#define MAX_LISTEN_SOCKS 16
133int listen_socks[MAX_LISTEN_SOCKS];
134int num_listen_socks = 0;
8efc0c15 135
aa3378df 136/*
137 * the client's version string, passed by sshd2 in compat mode. if != NULL,
138 * sshd will skip the version-number exchange
139 */
5260325f 140char *client_version_string = NULL;
7368a6c8 141char *server_version_string = NULL;
8efc0c15 142
aa3378df 143/*
144 * Any really sensitive data in the application is contained in this
145 * structure. The idea is that this structure could be locked into memory so
146 * that the pages do not get written into swap. However, there are some
147 * problems. The private key contains BIGNUMs, and we do not (in principle)
148 * have access to the internals of them, and locking just the structure is
149 * not very useful. Currently, memory locking is not implemented.
150 */
5260325f 151struct {
cb7bd922 152 Key *server_key; /* ephemeral server key */
fa08c86b 153 Key *ssh1_host_key; /* ssh1 host key */
154 Key **host_keys; /* all private host keys */
155 int have_ssh1_key;
156 int have_ssh2_key;
00be5382 157 u_char ssh1_cookie[SSH_SESSION_KEY_LENGTH];
8efc0c15 158} sensitive_data;
159
aa3378df 160/*
59c97189 161 * Flag indicating whether the RSA server key needs to be regenerated.
162 * Is set in the SIGALRM handler and cleared when the key is regenerated.
aa3378df 163 */
59c97189 164int key_do_regen = 0;
8efc0c15 165
166/* This is set to true when SIGHUP is received. */
167int received_sighup = 0;
168
7368a6c8 169/* session identifier, used by RSA-auth */
1e3b8b07 170u_char session_id[16];
e7c0f9d5 171
a306f2dd 172/* same for ssh2 */
1e3b8b07 173u_char *session_id2 = NULL;
a306f2dd 174int session_id2_len = 0;
175
c345cf9d 176/* record remote hostname or ip */
1e3b8b07 177u_int utmp_len = MAXHOSTNAMELEN;
c345cf9d 178
7368a6c8 179/* Prototypes for various functions defined later in this file. */
1e3b8b07 180void do_ssh1_kex(void);
181void do_ssh2_kex(void);
c8d54615 182
94ec8c6b 183void ssh_dh1_server(Kex *, Buffer *_kexinit, Buffer *);
184void ssh_dhgex_server(Kex *, Buffer *_kexinit, Buffer *);
185
48e671d5 186/*
187 * Close all listening sockets
188 */
189void
190close_listen_socks(void)
191{
192 int i;
193 for (i = 0; i < num_listen_socks; i++)
194 close(listen_socks[i]);
195 num_listen_socks = -1;
196}
197
5260325f 198/*
199 * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
200 * the effect is to reread the configuration file (and to regenerate
201 * the server key).
202 */
6ae2364d 203void
5260325f 204sighup_handler(int sig)
8efc0c15 205{
5260325f 206 received_sighup = 1;
207 signal(SIGHUP, sighup_handler);
8efc0c15 208}
209
5260325f 210/*
211 * Called from the main program after receiving SIGHUP.
212 * Restarts the server.
213 */
6ae2364d 214void
5ca51e19 215sighup_restart(void)
8efc0c15 216{
5260325f 217 log("Received SIGHUP; restarting.");
48e671d5 218 close_listen_socks();
5260325f 219 execv(saved_argv[0], saved_argv);
1fe6a48f 220 log("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], strerror(errno));
5260325f 221 exit(1);
8efc0c15 222}
223
5260325f 224/*
225 * Generic signal handler for terminating signals in the master daemon.
226 * These close the listen socket; not closing it seems to cause "Address
227 * already in use" problems on some machines, which is inconvenient.
228 */
6ae2364d 229void
5260325f 230sigterm_handler(int sig)
8efc0c15 231{
5260325f 232 log("Received signal %d; terminating.", sig);
48e671d5 233 close_listen_socks();
0fbe8c74 234 unlink(options.pid_file);
5260325f 235 exit(255);
8efc0c15 236}
237
5260325f 238/*
239 * SIGCHLD handler. This is called whenever a child dies. This will then
240 * reap any zombies left by exited c.
241 */
6ae2364d 242void
5260325f 243main_sigchld_handler(int sig)
8efc0c15 244{
5260325f 245 int save_errno = errno;
246 int status;
5ad13cd7 247
5260325f 248 while (waitpid(-1, &status, WNOHANG) > 0)
249 ;
5ad13cd7 250
5260325f 251 signal(SIGCHLD, main_sigchld_handler);
252 errno = save_errno;
8efc0c15 253}
254
5260325f 255/*
256 * Signal handler for the alarm after the login grace period has expired.
257 */
6ae2364d 258void
5260325f 259grace_alarm_handler(int sig)
8efc0c15 260{
5260325f 261 /* Close the connection. */
262 packet_close();
8efc0c15 263
5260325f 264 /* Log error and exit. */
265 fatal("Timeout before authentication for %s.", get_remote_ipaddr());
266}
8efc0c15 267
5260325f 268/*
269 * Signal handler for the key regeneration alarm. Note that this
270 * alarm only occurs in the daemon waiting for connections, and it does not
271 * do anything with the private key or random state before forking.
272 * Thus there should be no concurrency control/asynchronous execution
273 * problems.
274 */
6ae2364d 275void
cb7bd922 276generate_ephemeral_server_key(void)
fa08c86b 277{
00be5382 278 u_int32_t rand = 0;
279 int i;
280
fa08c86b 281 log("Generating %s%d bit RSA key.", sensitive_data.server_key ? "new " : "",
282 options.server_key_bits);
283 if (sensitive_data.server_key != NULL)
284 key_free(sensitive_data.server_key);
285 sensitive_data.server_key = key_generate(KEY_RSA1, options.server_key_bits);
fa08c86b 286 log("RSA key generation complete.");
00be5382 287
288 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
289 if (i % 4 == 0)
290 rand = arc4random();
291 sensitive_data.ssh1_cookie[i] = rand & 0xff;
292 rand >>= 8;
293 }
294 arc4random_stir();
fa08c86b 295}
f546c780 296
fa08c86b 297void
5260325f 298key_regeneration_alarm(int sig)
299{
300 int save_errno = errno;
59c97189 301 signal(SIGALRM, SIG_DFL);
5260325f 302 errno = save_errno;
59c97189 303 key_do_regen = 1;
5260325f 304}
8efc0c15 305
7368a6c8 306void
307sshd_exchange_identification(int sock_in, int sock_out)
308{
a8be9f80 309 int i, mismatch;
7368a6c8 310 int remote_major, remote_minor;
a8be9f80 311 int major, minor;
7368a6c8 312 char *s;
313 char buf[256]; /* Must not be larger than remote_version. */
314 char remote_version[256]; /* Must be at least as big as buf. */
315
a8be9f80 316 if ((options.protocol & SSH_PROTO_1) &&
317 (options.protocol & SSH_PROTO_2)) {
318 major = PROTOCOL_MAJOR_1;
319 minor = 99;
320 } else if (options.protocol & SSH_PROTO_2) {
321 major = PROTOCOL_MAJOR_2;
322 minor = PROTOCOL_MINOR_2;
323 } else {
324 major = PROTOCOL_MAJOR_1;
325 minor = PROTOCOL_MINOR_1;
326 }
327 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
7368a6c8 328 server_version_string = xstrdup(buf);
329
330 if (client_version_string == NULL) {
331 /* Send our protocol version identification. */
332 if (atomicio(write, sock_out, server_version_string, strlen(server_version_string))
333 != strlen(server_version_string)) {
334 log("Could not write ident string to %s.", get_remote_ipaddr());
335 fatal_cleanup();
336 }
337
2ce1adf9 338 /* Read other side's version identification. */
339 memset(buf, 0, sizeof(buf));
7368a6c8 340 for (i = 0; i < sizeof(buf) - 1; i++) {
e5a0294f 341 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
c2b544a5 342 log("Did not receive identification string from %s.",
343 get_remote_ipaddr());
7368a6c8 344 fatal_cleanup();
345 }
346 if (buf[i] == '\r') {
8a169574 347 buf[i] = 0;
94ec8c6b 348 /* Kludge for F-Secure Macintosh < 1.0.2 */
349 if (i == 12 &&
350 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
351 break;
7368a6c8 352 continue;
7368a6c8 353 }
354 if (buf[i] == '\n') {
8a169574 355 buf[i] = 0;
7368a6c8 356 break;
357 }
358 }
359 buf[sizeof(buf) - 1] = 0;
360 client_version_string = xstrdup(buf);
361 }
362
363 /*
364 * Check that the versions match. In future this might accept
365 * several versions and set appropriate flags to handle them.
366 */
367 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
368 &remote_major, &remote_minor, remote_version) != 3) {
6ae2364d 369 s = "Protocol mismatch.\n";
7368a6c8 370 (void) atomicio(write, sock_out, s, strlen(s));
371 close(sock_in);
372 close(sock_out);
373 log("Bad protocol version identification '%.100s' from %s",
374 client_version_string, get_remote_ipaddr());
375 fatal_cleanup();
376 }
377 debug("Client protocol version %d.%d; client software version %.100s",
378 remote_major, remote_minor, remote_version);
379
e78a59f5 380 compat_datafellows(remote_version);
381
3a1c54d4 382 if (datafellows & SSH_BUG_SCANNER) {
383 log("scanned from %s with %s. Don't panic.",
384 get_remote_ipaddr(), client_version_string);
385 fatal_cleanup();
386 }
387
a8be9f80 388 mismatch = 0;
7368a6c8 389 switch(remote_major) {
390 case 1:
a306f2dd 391 if (remote_minor == 99) {
392 if (options.protocol & SSH_PROTO_2)
393 enable_compat20();
394 else
395 mismatch = 1;
396 break;
397 }
a8be9f80 398 if (!(options.protocol & SSH_PROTO_1)) {
399 mismatch = 1;
400 break;
401 }
7368a6c8 402 if (remote_minor < 3) {
089fbbd2 403 packet_disconnect("Your ssh version is too old and "
7368a6c8 404 "is no longer supported. Please install a newer version.");
405 } else if (remote_minor == 3) {
406 /* note that this disables agent-forwarding */
407 enable_compat13();
408 }
a8be9f80 409 break;
e78a59f5 410 case 2:
a8be9f80 411 if (options.protocol & SSH_PROTO_2) {
e78a59f5 412 enable_compat20();
413 break;
414 }
415 /* FALLTHROUGH */
6ae2364d 416 default:
a8be9f80 417 mismatch = 1;
418 break;
419 }
420 chop(server_version_string);
a8be9f80 421 debug("Local version string %.200s", server_version_string);
422
423 if (mismatch) {
7368a6c8 424 s = "Protocol major versions differ.\n";
425 (void) atomicio(write, sock_out, s, strlen(s));
426 close(sock_in);
427 close(sock_out);
a8be9f80 428 log("Protocol major versions differ for %s: %.200s vs. %.200s",
429 get_remote_ipaddr(),
430 server_version_string, client_version_string);
7368a6c8 431 fatal_cleanup();
7368a6c8 432 }
a306f2dd 433 if (compat20)
434 packet_set_ssh2_format();
435}
436
437
fa08c86b 438/* Destroy the host and server keys. They will no longer be needed. */
a306f2dd 439void
440destroy_sensitive_data(void)
441{
fa08c86b 442 int i;
443
444 if (sensitive_data.server_key) {
445 key_free(sensitive_data.server_key);
446 sensitive_data.server_key = NULL;
447 }
2b87da3b 448 for(i = 0; i < options.num_host_key_files; i++) {
fa08c86b 449 if (sensitive_data.host_keys[i]) {
450 key_free(sensitive_data.host_keys[i]);
451 sensitive_data.host_keys[i] = NULL;
452 }
453 }
454 sensitive_data.ssh1_host_key = NULL;
00be5382 455 memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
fa08c86b 456}
fa08c86b 457
458char *
459list_hostkey_types(void)
460{
461 static char buf[1024];
462 int i;
463 buf[0] = '\0';
464 for(i = 0; i < options.num_host_key_files; i++) {
465 Key *key = sensitive_data.host_keys[i];
466 if (key == NULL)
467 continue;
468 switch(key->type) {
469 case KEY_RSA:
470 case KEY_DSA:
471 strlcat(buf, key_ssh_name(key), sizeof buf);
472 strlcat(buf, ",", sizeof buf);
473 break;
474 }
475 }
476 i = strlen(buf);
477 if (i > 0 && buf[i-1] == ',')
478 buf[i-1] = '\0';
479 debug("list_hostkey_types: %s", buf);
480 return buf;
481}
482
483Key *
484get_hostkey_by_type(int type)
485{
486 int i;
487 for(i = 0; i < options.num_host_key_files; i++) {
488 Key *key = sensitive_data.host_keys[i];
489 if (key != NULL && key->type == type)
490 return key;
491 }
492 return NULL;
7368a6c8 493}
494
c345cf9d 495/*
496 * returns 1 if connection should be dropped, 0 otherwise.
497 * dropping starts at connection #max_startups_begin with a probability
498 * of (max_startups_rate/100). the probability increases linearly until
499 * all connections are dropped for startups > max_startups
500 */
501int
502drop_connection(int startups)
503{
504 double p, r;
505
506 if (startups < options.max_startups_begin)
507 return 0;
508 if (startups >= options.max_startups)
509 return 1;
510 if (options.max_startups_rate == 100)
511 return 1;
512
513 p = 100 - options.max_startups_rate;
514 p *= startups - options.max_startups_begin;
515 p /= (double) (options.max_startups - options.max_startups_begin);
516 p += options.max_startups_rate;
517 p /= 100.0;
518 r = arc4random() / (double) UINT_MAX;
519
520 debug("drop_connection: p %g, r %g", p, r);
521 return (r < p) ? 1 : 0;
522}
523
089fbbd2 524int *startup_pipes = NULL; /* options.max_startup sized array of fd ints */
525int startup_pipe; /* in child */
526
5260325f 527/*
528 * Main program for the daemon.
529 */
8efc0c15 530int
531main(int ac, char **av)
532{
5260325f 533 extern char *optarg;
534 extern int optind;
089fbbd2 535 int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1;
9da5c3c9 536 pid_t pid;
48e671d5 537 socklen_t fromlen;
48e671d5 538 fd_set *fdset;
539 struct sockaddr_storage from;
5260325f 540 const char *remote_ip;
541 int remote_port;
5260325f 542 FILE *f;
543 struct linger linger;
48e671d5 544 struct addrinfo *ai;
545 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
546 int listen_sock, maxfd;
089fbbd2 547 int startup_p[2];
548 int startups = 0;
aeaa3d9e 549 Key *key;
59c97189 550 int ret, key_used = 0;
5260325f 551
260d427b 552 __progname = get_progname(av[0]);
264dce47 553 init_rng();
554
1fe6a48f 555 /* Save argv. */
4d33e531 556 saved_argc = ac;
5260325f 557 saved_argv = av;
5260325f 558
559 /* Initialize configuration options to their default values. */
560 initialize_server_options(&options);
561
562 /* Parse command-line arguments. */
b5c334cc 563 while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:dDiqQ46")) != -1) {
5260325f 564 switch (opt) {
48e671d5 565 case '4':
566 IPv4or6 = AF_INET;
567 break;
568 case '6':
569 IPv4or6 = AF_INET6;
570 break;
5260325f 571 case 'f':
572 config_file_name = optarg;
573 break;
574 case 'd':
bcbf86ec 575 if (0 == debug_flag) {
576 debug_flag = 1;
577 options.log_level = SYSLOG_LEVEL_DEBUG1;
578 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
579 options.log_level++;
580 } else {
581 fprintf(stderr, "Too high debugging level.\n");
582 exit(1);
583 }
5260325f 584 break;
0b6fbf03 585 case 'D':
586 no_daemon_flag = 1;
587 break;
5260325f 588 case 'i':
589 inetd_flag = 1;
590 break;
591 case 'Q':
9bd5b720 592 /* ignored */
5260325f 593 break;
594 case 'q':
595 options.log_level = SYSLOG_LEVEL_QUIET;
596 break;
597 case 'b':
598 options.server_key_bits = atoi(optarg);
599 break;
600 case 'p':
48e671d5 601 options.ports_from_cmdline = 1;
bcbf86ec 602 if (options.num_ports >= MAX_PORTS) {
603 fprintf(stderr, "too many ports.\n");
604 exit(1);
605 }
48e671d5 606 options.ports[options.num_ports++] = atoi(optarg);
5260325f 607 break;
608 case 'g':
609 options.login_grace_time = atoi(optarg);
610 break;
611 case 'k':
612 options.key_regeneration_time = atoi(optarg);
613 break;
614 case 'h':
fa08c86b 615 if (options.num_host_key_files >= MAX_HOSTKEYS) {
616 fprintf(stderr, "too many host keys.\n");
617 exit(1);
618 }
619 options.host_key_files[options.num_host_key_files++] = optarg;
5260325f 620 break;
621 case 'V':
622 client_version_string = optarg;
623 /* only makes sense with inetd_flag, i.e. no listen() */
624 inetd_flag = 1;
625 break;
c345cf9d 626 case 'u':
627 utmp_len = atoi(optarg);
628 break;
5260325f 629 case '?':
630 default:
631 fprintf(stderr, "sshd version %s\n", SSH_VERSION);
1fe6a48f 632 fprintf(stderr, "Usage: %s [options]\n", __progname);
5260325f 633 fprintf(stderr, "Options:\n");
42f11eb2 634 fprintf(stderr, " -f file Configuration file (default %s)\n", _PATH_SERVER_CONFIG_FILE);
bcbf86ec 635 fprintf(stderr, " -d Debugging mode (multiple -d means more debugging)\n");
5260325f 636 fprintf(stderr, " -i Started from inetd\n");
8abcdba4 637 fprintf(stderr, " -D Do not fork into daemon mode\n");
5260325f 638 fprintf(stderr, " -q Quiet (no logging)\n");
639 fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
640 fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
9616313f 641 fprintf(stderr, " -g seconds Grace period for authentication (default: 600)\n");
5260325f 642 fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
643 fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
42f11eb2 644 _PATH_HOST_KEY_FILE);
c345cf9d 645 fprintf(stderr, " -u len Maximum hostname length for utmp recording\n");
48e671d5 646 fprintf(stderr, " -4 Use IPv4 only\n");
647 fprintf(stderr, " -6 Use IPv6 only\n");
5260325f 648 exit(1);
649 }
650 }
1a92bd7e 651 SSLeay_add_all_algorithms();
5260325f 652
48e671d5 653 /*
654 * Force logging to stderr until we have loaded the private host
655 * key (unless started from inetd)
656 */
1fe6a48f 657 log_init(__progname,
59c97189 658 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
48e671d5 659 options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
9bd5b720 660 !inetd_flag);
48e671d5 661
e339aa53 662 seed_rng();
663
5260325f 664 /* Read server configuration options from the configuration file. */
665 read_server_config(&options, config_file_name);
666
667 /* Fill in default values for those options not explicitly set. */
668 fill_default_server_options(&options);
669
5260325f 670 /* Check that there are no remaining arguments. */
671 if (optind < ac) {
672 fprintf(stderr, "Extra argument %s.\n", av[optind]);
673 exit(1);
8efc0c15 674 }
5260325f 675
676 debug("sshd version %.100s", SSH_VERSION);
677
fa08c86b 678 /* load private host keys */
679 sensitive_data.host_keys = xmalloc(options.num_host_key_files*sizeof(Key*));
1e3b8b07 680 for(i = 0; i < options.num_host_key_files; i++)
681 sensitive_data.host_keys[i] = NULL;
fa08c86b 682 sensitive_data.server_key = NULL;
683 sensitive_data.ssh1_host_key = NULL;
684 sensitive_data.have_ssh1_key = 0;
685 sensitive_data.have_ssh2_key = 0;
a306f2dd 686
fa08c86b 687 for(i = 0; i < options.num_host_key_files; i++) {
aeaa3d9e 688 key = key_load_private(options.host_key_files[i], "", NULL);
689 sensitive_data.host_keys[i] = key;
fa08c86b 690 if (key == NULL) {
a306f2dd 691 error("Could not load host key: %.200s: %.100s",
fa08c86b 692 options.host_key_files[i], strerror(errno));
aeaa3d9e 693 sensitive_data.host_keys[i] = NULL;
fa08c86b 694 continue;
a306f2dd 695 }
fa08c86b 696 switch(key->type){
697 case KEY_RSA1:
698 sensitive_data.ssh1_host_key = key;
699 sensitive_data.have_ssh1_key = 1;
700 break;
701 case KEY_RSA:
702 case KEY_DSA:
703 sensitive_data.have_ssh2_key = 1;
704 break;
a306f2dd 705 }
aeaa3d9e 706 debug("private host key: #%d type %d %s", i, key->type,
707 key_type(key));
fa08c86b 708 }
709 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
710 log("Disabling protocol version 1. Could not load host key");
711 options.protocol &= ~SSH_PROTO_1;
712 }
713 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
714 log("Disabling protocol version 2. Could not load host key");
715 options.protocol &= ~SSH_PROTO_2;
a306f2dd 716 }
6a416424 717 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
54b974dc 718 log("sshd: no hostkeys available -- exiting.");
5260325f 719 exit(1);
720 }
5260325f 721
a306f2dd 722 /* Check certain values for sanity. */
723 if (options.protocol & SSH_PROTO_1) {
724 if (options.server_key_bits < 512 ||
725 options.server_key_bits > 32768) {
726 fprintf(stderr, "Bad server key size.\n");
727 exit(1);
728 }
729 /*
730 * Check that server and host key lengths differ sufficiently. This
731 * is necessary to make double encryption work with rsaref. Oh, I
732 * hate software patents. I dont know if this can go? Niels
733 */
734 if (options.server_key_bits >
fa08c86b 735 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) - SSH_KEY_BITS_RESERVED &&
a306f2dd 736 options.server_key_bits <
fa08c86b 737 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
a306f2dd 738 options.server_key_bits =
fa08c86b 739 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED;
a306f2dd 740 debug("Forcing server key to %d bits to make it differ from host key.",
741 options.server_key_bits);
742 }
743 }
744
77bb0bca 745#ifdef HAVE_SCO_PROTECTED_PW
746 (void) set_auth_parameters(ac, av);
747#endif
748
a306f2dd 749 /* Initialize the log (it is reinitialized below in case we forked). */
5260325f 750 if (debug_flag && !inetd_flag)
751 log_stderr = 1;
1fe6a48f 752 log_init(__progname, options.log_level, options.log_facility, log_stderr);
5260325f 753
a306f2dd 754 /*
755 * If not in debugging mode, and not started from inetd, disconnect
756 * from the controlling terminal, and fork. The original process
757 * exits.
758 */
0b6fbf03 759 if (!(debug_flag || inetd_flag || no_daemon_flag)) {
8efc0c15 760#ifdef TIOCNOTTY
5260325f 761 int fd;
8efc0c15 762#endif /* TIOCNOTTY */
5260325f 763 if (daemon(0, 0) < 0)
764 fatal("daemon() failed: %.200s", strerror(errno));
765
766 /* Disconnect from the controlling tty. */
8efc0c15 767#ifdef TIOCNOTTY
5ca51e19 768 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325f 769 if (fd >= 0) {
770 (void) ioctl(fd, TIOCNOTTY, NULL);
771 close(fd);
772 }
8efc0c15 773#endif /* TIOCNOTTY */
8efc0c15 774 }
5260325f 775 /* Reinitialize the log (because of the fork above). */
1fe6a48f 776 log_init(__progname, options.log_level, options.log_facility, log_stderr);
5260325f 777
5260325f 778 /* Initialize the random number generator. */
779 arc4random_stir();
780
781 /* Chdir to the root directory so that the current disk can be
782 unmounted if desired. */
783 chdir("/");
1d3c30db 784
785 /* ignore SIGPIPE */
786 signal(SIGPIPE, SIG_IGN);
5260325f 787
5260325f 788 /* Start listening for a socket, unless started from inetd. */
789 if (inetd_flag) {
790 int s1, s2;
791 s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
792 s2 = dup(s1);
793 sock_in = dup(0);
794 sock_out = dup(1);
4c8722d9 795 startup_pipe = -1;
a306f2dd 796 /*
797 * We intentionally do not close the descriptors 0, 1, and 2
798 * as our code for setting the descriptors won\'t work if
799 * ttyfd happens to be one of those.
800 */
5260325f 801 debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
fa08c86b 802 if (options.protocol & SSH_PROTO_1)
cb7bd922 803 generate_ephemeral_server_key();
5260325f 804 } else {
48e671d5 805 for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
806 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
807 continue;
808 if (num_listen_socks >= MAX_LISTEN_SOCKS)
809 fatal("Too many listen sockets. "
810 "Enlarge MAX_LISTEN_SOCKS");
811 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
812 ntop, sizeof(ntop), strport, sizeof(strport),
813 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
814 error("getnameinfo failed");
815 continue;
816 }
817 /* Create socket for listening. */
818 listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
819 if (listen_sock < 0) {
820 /* kernel may not support ipv6 */
821 verbose("socket: %.100s", strerror(errno));
822 continue;
823 }
824 if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
825 error("listen_sock O_NONBLOCK: %s", strerror(errno));
826 close(listen_sock);
827 continue;
828 }
829 /*
830 * Set socket options. We try to make the port
831 * reusable and have it close as fast as possible
832 * without waiting in unnecessary wait states on
833 * close.
834 */
835 setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
836 (void *) &on, sizeof(on));
837 linger.l_onoff = 1;
838 linger.l_linger = 5;
839 setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
840 (void *) &linger, sizeof(linger));
841
842 debug("Bind to port %s on %s.", strport, ntop);
843
844 /* Bind the socket to the desired port. */
32ced054 845 if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
846 if (!ai->ai_next)
847 error("Bind to port %s on %s failed: %.200s.",
848 strport, ntop, strerror(errno));
48e671d5 849 close(listen_sock);
850 continue;
851 }
852 listen_socks[num_listen_socks] = listen_sock;
853 num_listen_socks++;
854
855 /* Start listening on the port. */
856 log("Server listening on %s port %s.", ntop, strport);
857 if (listen(listen_sock, 5) < 0)
858 fatal("listen: %.100s", strerror(errno));
859
5260325f 860 }
48e671d5 861 freeaddrinfo(options.listen_addrs);
862
863 if (!num_listen_socks)
864 fatal("Cannot bind any address.");
865
5260325f 866 if (!debug_flag) {
aa3378df 867 /*
97fb6912 868 * Record our pid in /var/run/sshd.pid to make it
869 * easier to kill the correct sshd. We don't want to
870 * do this before the bind above because the bind will
aa3378df 871 * fail if there already is a daemon, and this will
872 * overwrite any old pid in the file.
873 */
3c62e7eb 874 f = fopen(options.pid_file, "wb");
5260325f 875 if (f) {
1e3b8b07 876 fprintf(f, "%u\n", (u_int) getpid());
5260325f 877 fclose(f);
878 }
8efc0c15 879 }
59c97189 880 if (options.protocol & SSH_PROTO_1)
cb7bd922 881 generate_ephemeral_server_key();
5260325f 882
5260325f 883 /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
884 signal(SIGHUP, sighup_handler);
089fbbd2 885
5260325f 886 signal(SIGTERM, sigterm_handler);
887 signal(SIGQUIT, sigterm_handler);
888
889 /* Arrange SIGCHLD to be caught. */
890 signal(SIGCHLD, main_sigchld_handler);
891
48e671d5 892 /* setup fd set for listen */
089fbbd2 893 fdset = NULL;
48e671d5 894 maxfd = 0;
895 for (i = 0; i < num_listen_socks; i++)
896 if (listen_socks[i] > maxfd)
897 maxfd = listen_socks[i];
089fbbd2 898 /* pipes connected to unauthenticated childs */
899 startup_pipes = xmalloc(options.max_startups * sizeof(int));
900 for (i = 0; i < options.max_startups; i++)
901 startup_pipes[i] = -1;
48e671d5 902
aa3378df 903 /*
904 * Stay listening for connections until the system crashes or
905 * the daemon is killed with a signal.
906 */
5260325f 907 for (;;) {
908 if (received_sighup)
909 sighup_restart();
089fbbd2 910 if (fdset != NULL)
911 xfree(fdset);
b5c334cc 912 fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
089fbbd2 913 fdset = (fd_set *)xmalloc(fdsetsz);
48e671d5 914 memset(fdset, 0, fdsetsz);
089fbbd2 915
48e671d5 916 for (i = 0; i < num_listen_socks; i++)
917 FD_SET(listen_socks[i], fdset);
089fbbd2 918 for (i = 0; i < options.max_startups; i++)
919 if (startup_pipes[i] != -1)
920 FD_SET(startup_pipes[i], fdset);
921
922 /* Wait in select until there is a connection. */
59c97189 923 ret = select(maxfd+1, fdset, NULL, NULL, NULL);
924 if (ret < 0 && errno != EINTR)
925 error("select: %.100s", strerror(errno));
926 if (key_used && key_do_regen) {
cb7bd922 927 generate_ephemeral_server_key();
59c97189 928 key_used = 0;
929 key_do_regen = 0;
48e671d5 930 }
59c97189 931 if (ret < 0)
932 continue;
933
089fbbd2 934 for (i = 0; i < options.max_startups; i++)
935 if (startup_pipes[i] != -1 &&
936 FD_ISSET(startup_pipes[i], fdset)) {
937 /*
938 * the read end of the pipe is ready
939 * if the child has closed the pipe
8abcdba4 940 * after successful authentication
089fbbd2 941 * or if the child has died
942 */
943 close(startup_pipes[i]);
944 startup_pipes[i] = -1;
945 startups--;
946 }
48e671d5 947 for (i = 0; i < num_listen_socks; i++) {
948 if (!FD_ISSET(listen_socks[i], fdset))
5260325f 949 continue;
089fbbd2 950 fromlen = sizeof(from);
951 newsock = accept(listen_socks[i], (struct sockaddr *)&from,
952 &fromlen);
953 if (newsock < 0) {
954 if (errno != EINTR && errno != EWOULDBLOCK)
955 error("accept: %.100s", strerror(errno));
956 continue;
957 }
958 if (fcntl(newsock, F_SETFL, 0) < 0) {
959 error("newsock del O_NONBLOCK: %s", strerror(errno));
960 continue;
961 }
c345cf9d 962 if (drop_connection(startups) == 1) {
963 debug("drop connection #%d", startups);
089fbbd2 964 close(newsock);
965 continue;
966 }
967 if (pipe(startup_p) == -1) {
968 close(newsock);
969 continue;
970 }
971
972 for (j = 0; j < options.max_startups; j++)
973 if (startup_pipes[j] == -1) {
974 startup_pipes[j] = startup_p[0];
975 if (maxfd < startup_p[0])
976 maxfd = startup_p[0];
977 startups++;
978 break;
979 }
2b87da3b 980
aa3378df 981 /*
089fbbd2 982 * Got connection. Fork a child to handle it, unless
983 * we are in debugging mode.
aa3378df 984 */
089fbbd2 985 if (debug_flag) {
aa3378df 986 /*
089fbbd2 987 * In debugging mode. Close the listening
988 * socket, and start processing the
989 * connection without forking.
aa3378df 990 */
089fbbd2 991 debug("Server will not fork when running in debugging mode.");
48e671d5 992 close_listen_socks();
5260325f 993 sock_in = newsock;
994 sock_out = newsock;
5540ea9b 995 startup_pipe = -1;
3f7a7e4a 996 pid = getpid();
5260325f 997 break;
089fbbd2 998 } else {
999 /*
1000 * Normal production daemon. Fork, and have
1001 * the child process the connection. The
1002 * parent continues listening.
1003 */
1004 if ((pid = fork()) == 0) {
1005 /*
1006 * Child. Close the listening and max_startup
1007 * sockets. Start using the accepted socket.
1008 * Reinitialize logging (since our pid has
1009 * changed). We break out of the loop to handle
1010 * the connection.
1011 */
1012 startup_pipe = startup_p[1];
1013 for (j = 0; j < options.max_startups; j++)
1014 if (startup_pipes[j] != -1)
1015 close(startup_pipes[j]);
1016 close_listen_socks();
1017 sock_in = newsock;
1018 sock_out = newsock;
1fe6a48f 1019 log_init(__progname, options.log_level, options.log_facility, log_stderr);
089fbbd2 1020 break;
1021 }
5260325f 1022 }
5260325f 1023
089fbbd2 1024 /* Parent. Stay in the loop. */
1025 if (pid < 0)
1026 error("fork: %.100s", strerror(errno));
1027 else
1028 debug("Forked child %d.", pid);
5260325f 1029
089fbbd2 1030 close(startup_p[1]);
5260325f 1031
089fbbd2 1032 /* Mark that the key has been used (it was "given" to the child). */
59c97189 1033 if ((options.protocol & SSH_PROTO_1) &&
1034 key_used == 0) {
1035 /* Schedule server key regeneration alarm. */
1036 signal(SIGALRM, key_regeneration_alarm);
1037 alarm(options.key_regeneration_time);
1038 key_used = 1;
1039 }
5260325f 1040
089fbbd2 1041 arc4random_stir();
1042
1043 /* Close the new socket (the child is now taking care of it). */
1044 close(newsock);
1045 }
48e671d5 1046 /* child process check (or debug mode) */
1047 if (num_listen_socks < 0)
1048 break;
5260325f 1049 }
1050 }
8efc0c15 1051
5260325f 1052 /* This is the child processing a new connection. */
1053
aa3378df 1054 /*
1055 * Disable the key regeneration alarm. We will not regenerate the
1056 * key since we are no longer in a position to give it to anyone. We
1057 * will not restart on SIGHUP since it no longer makes sense.
1058 */
5260325f 1059 alarm(0);
1060 signal(SIGALRM, SIG_DFL);
1061 signal(SIGHUP, SIG_DFL);
1062 signal(SIGTERM, SIG_DFL);
1063 signal(SIGQUIT, SIG_DFL);
1064 signal(SIGCHLD, SIG_DFL);
9aaf9be4 1065 signal(SIGINT, SIG_DFL);
5260325f 1066
aa3378df 1067 /*
1068 * Set socket options for the connection. We want the socket to
1069 * close as fast as possible without waiting for anything. If the
1070 * connection is not a socket, these will do nothing.
1071 */
1072 /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
5260325f 1073 linger.l_onoff = 1;
1074 linger.l_linger = 5;
1075 setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
1076
b5c334cc 1077 /* Set keepalives if requested. */
1078 if (options.keepalives &&
1079 setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
1080 sizeof(on)) < 0)
1081 error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1082
aa3378df 1083 /*
1084 * Register our connection. This turns encryption off because we do
1085 * not have a key.
1086 */
5260325f 1087 packet_set_connection(sock_in, sock_out);
1088
1089 remote_port = get_remote_port();
1090 remote_ip = get_remote_ipaddr();
1091
1092 /* Check whether logins are denied from this host. */
1093#ifdef LIBWRAP
48e671d5 1094 /* XXX LIBWRAP noes not know about IPv6 */
5260325f 1095 {
1096 struct request_info req;
8efc0c15 1097
1fe6a48f 1098 request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, NULL);
5260325f 1099 fromhost(&req);
8efc0c15 1100
5260325f 1101 if (!hosts_access(&req)) {
563834bb 1102 refuse(&req);
5260325f 1103 close(sock_in);
1104 close(sock_out);
5260325f 1105 }
48e671d5 1106/*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
8efc0c15 1107 }
48e671d5 1108#endif /* LIBWRAP */
5260325f 1109 /* Log the connection. */
1110 verbose("Connection from %.500s port %d", remote_ip, remote_port);
8efc0c15 1111
aa3378df 1112 /*
1113 * We don\'t want to listen forever unless the other side
1114 * successfully authenticates itself. So we set up an alarm which is
1115 * cleared after successful authentication. A limit of zero
1116 * indicates no limit. Note that we don\'t set the alarm in debugging
1117 * mode; it is just annoying to have the server exit just when you
1118 * are about to discover the bug.
1119 */
5260325f 1120 signal(SIGALRM, grace_alarm_handler);
1121 if (!debug_flag)
1122 alarm(options.login_grace_time);
1123
7368a6c8 1124 sshd_exchange_identification(sock_in, sock_out);
aa3378df 1125 /*
6ffc9c88 1126 * Check that the connection comes from a privileged port.
1127 * Rhosts-Authentication only makes sense from priviledged
aa3378df 1128 * programs. Of course, if the intruder has root access on his local
1129 * machine, he can connect from any port. So do not use these
1130 * authentication methods from machines that you do not trust.
1131 */
5260325f 1132 if (remote_port >= IPPORT_RESERVED ||
1133 remote_port < IPPORT_RESERVED / 2) {
6ffc9c88 1134 debug("Rhosts Authentication disabled, "
fa08c86b 1135 "originating port not trusted.");
5260325f 1136 options.rhosts_authentication = 0;
5260325f 1137 }
48e671d5 1138#ifdef KRB4
1139 if (!packet_connection_is_ipv4() &&
1140 options.kerberos_authentication) {
1141 debug("Kerberos Authentication disabled, only available for IPv4.");
1142 options.kerberos_authentication = 0;
1143 }
1144#endif /* KRB4 */
abf1f107 1145#ifdef AFS
1146 /* If machine has AFS, set process authentication group. */
1147 if (k_hasafs()) {
1148 k_setpag();
1149 k_unlog();
1150 }
1151#endif /* AFS */
48e671d5 1152
5260325f 1153 packet_set_nonblocking();
1154
7b2ea3a1 1155 /* perform the key exchange */
7b2ea3a1 1156 /* authenticate user and start session */
e78a59f5 1157 if (compat20) {
1158 do_ssh2_kex();
1159 do_authentication2();
1160 } else {
1161 do_ssh1_kex();
1162 do_authentication();
1163 }
8efc0c15 1164
1165#ifdef KRB4
5260325f 1166 /* Cleanup user's ticket cache file. */
1167 if (options.kerberos_ticket_cleanup)
1168 (void) dest_tkt();
8efc0c15 1169#endif /* KRB4 */
1170
5260325f 1171 /* The connection has been terminated. */
1172 verbose("Closing connection to %.100s", remote_ip);
8efc0c15 1173
d94aa2ae 1174#ifdef USE_PAM
a5c9cd31 1175 finish_pam();
d94aa2ae 1176#endif /* USE_PAM */
8efc0c15 1177
5260325f 1178 packet_close();
1179 exit(0);
1180}
8efc0c15 1181
5260325f 1182/*
7b2ea3a1 1183 * SSH1 key exchange
5260325f 1184 */
e7c0f9d5 1185void
1e3b8b07 1186do_ssh1_kex(void)
8efc0c15 1187{
5260325f 1188 int i, len;
7b2ea3a1 1189 int plen, slen;
46aa2d1f 1190 int rsafail = 0;
5260325f 1191 BIGNUM *session_key_int;
1e3b8b07 1192 u_char session_key[SSH_SESSION_KEY_LENGTH];
1193 u_char cookie[8];
1194 u_int cipher_type, auth_mask, protocol_flags;
5260325f 1195 u_int32_t rand = 0;
1196
aa3378df 1197 /*
1198 * Generate check bytes that the client must send back in the user
1199 * packet in order for it to be accepted; this is used to defy ip
1200 * spoofing attacks. Note that this only works against somebody
1201 * doing IP spoofing from a remote machine; any machine on the local
1202 * network can still see outgoing packets and catch the random
1203 * cookie. This only affects rhosts authentication, and this is one
1204 * of the reasons why it is inherently insecure.
1205 */
5260325f 1206 for (i = 0; i < 8; i++) {
1207 if (i % 4 == 0)
1208 rand = arc4random();
7b2ea3a1 1209 cookie[i] = rand & 0xff;
5260325f 1210 rand >>= 8;
1211 }
1212
aa3378df 1213 /*
1214 * Send our public key. We include in the packet 64 bits of random
1215 * data that must be matched in the reply in order to prevent IP
1216 * spoofing.
1217 */
5260325f 1218 packet_start(SSH_SMSG_PUBLIC_KEY);
1219 for (i = 0; i < 8; i++)
7b2ea3a1 1220 packet_put_char(cookie[i]);
5260325f 1221
1222 /* Store our public server RSA key. */
fa08c86b 1223 packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1224 packet_put_bignum(sensitive_data.server_key->rsa->e);
1225 packet_put_bignum(sensitive_data.server_key->rsa->n);
5260325f 1226
1227 /* Store our public host RSA key. */
fa08c86b 1228 packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1229 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1230 packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
5260325f 1231
1232 /* Put protocol flags. */
1233 packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1234
1235 /* Declare which ciphers we support. */
94ec8c6b 1236 packet_put_int(cipher_mask_ssh1(0));
5260325f 1237
1238 /* Declare supported authentication types. */
1239 auth_mask = 0;
1240 if (options.rhosts_authentication)
1241 auth_mask |= 1 << SSH_AUTH_RHOSTS;
1242 if (options.rhosts_rsa_authentication)
1243 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1244 if (options.rsa_authentication)
1245 auth_mask |= 1 << SSH_AUTH_RSA;
8efc0c15 1246#ifdef KRB4
5260325f 1247 if (options.kerberos_authentication)
1248 auth_mask |= 1 << SSH_AUTH_KERBEROS;
8efc0c15 1249#endif
1250#ifdef AFS
5260325f 1251 if (options.kerberos_tgt_passing)
1252 auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1253 if (options.afs_token_passing)
1254 auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
8efc0c15 1255#endif
d464095c 1256 if (options.challenge_reponse_authentication == 1)
5260325f 1257 auth_mask |= 1 << SSH_AUTH_TIS;
5260325f 1258 if (options.password_authentication)
1259 auth_mask |= 1 << SSH_AUTH_PASSWORD;
1260 packet_put_int(auth_mask);
1261
1262 /* Send the packet and wait for it to be sent. */
1263 packet_send();
1264 packet_write_wait();
1265
fa08c86b 1266 debug("Sent %d bit server key and %d bit host key.",
1267 BN_num_bits(sensitive_data.server_key->rsa->n),
1268 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
5260325f 1269
1270 /* Read clients reply (cipher type and session key). */
1271 packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
1272
2d86a6cc 1273 /* Get cipher type and check whether we accept this. */
5260325f 1274 cipher_type = packet_get_char();
1275
94ec8c6b 1276 if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
2d86a6cc 1277 packet_disconnect("Warning: client selects unsupported cipher.");
1278
5260325f 1279 /* Get check bytes from the packet. These must match those we
1280 sent earlier with the public key packet. */
1281 for (i = 0; i < 8; i++)
7b2ea3a1 1282 if (cookie[i] != packet_get_char())
5260325f 1283 packet_disconnect("IP Spoofing check bytes do not match.");
1284
1285 debug("Encryption type: %.200s", cipher_name(cipher_type));
1286
1287 /* Get the encrypted integer. */
1288 session_key_int = BN_new();
1289 packet_get_bignum(session_key_int, &slen);
1290
5260325f 1291 protocol_flags = packet_get_int();
1292 packet_set_protocol_flags(protocol_flags);
1293
1294 packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
1295
aa3378df 1296 /*
1297 * Decrypt it using our private server key and private host key (key
1298 * with larger modulus first).
1299 */
fa08c86b 1300 if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
46aa2d1f 1301 /* Server key has bigger modulus. */
fa08c86b 1302 if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1303 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1304 fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1305 get_remote_ipaddr(),
1306 BN_num_bits(sensitive_data.server_key->rsa->n),
1307 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1308 SSH_KEY_BITS_RESERVED);
5260325f 1309 }
46aa2d1f 1310 if (rsa_private_decrypt(session_key_int, session_key_int,
1311 sensitive_data.server_key->rsa) <= 0)
1312 rsafail++;
1313 if (rsa_private_decrypt(session_key_int, session_key_int,
1314 sensitive_data.ssh1_host_key->rsa) <= 0)
1315 rsafail++;
5260325f 1316 } else {
1317 /* Host key has bigger modulus (or they are equal). */
fa08c86b 1318 if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1319 BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1320 fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1321 get_remote_ipaddr(),
1322 BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1323 BN_num_bits(sensitive_data.server_key->rsa->n),
1324 SSH_KEY_BITS_RESERVED);
5260325f 1325 }
46aa2d1f 1326 if (rsa_private_decrypt(session_key_int, session_key_int,
1327 sensitive_data.ssh1_host_key->rsa) < 0)
1328 rsafail++;
1329 if (rsa_private_decrypt(session_key_int, session_key_int,
1330 sensitive_data.server_key->rsa) < 0)
1331 rsafail++;
5260325f 1332 }
aa3378df 1333 /*
1334 * Extract session key from the decrypted integer. The key is in the
1335 * least significant 256 bits of the integer; the first byte of the
1336 * key is in the highest bits.
1337 */
46aa2d1f 1338 if (!rsafail) {
1339 BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1340 len = BN_num_bytes(session_key_int);
1341 if (len < 0 || len > sizeof(session_key)) {
1342 error("do_connection: bad session key len from %s: "
5ca51e19 1343 "session_key_int %d > sizeof(session_key) %lu",
1344 get_remote_ipaddr(), len, (u_long)sizeof(session_key));
46aa2d1f 1345 rsafail++;
1346 } else {
1347 memset(session_key, 0, sizeof(session_key));
1348 BN_bn2bin(session_key_int,
1349 session_key + sizeof(session_key) - len);
00be5382 1350
1351 compute_session_id(session_id, cookie,
1352 sensitive_data.ssh1_host_key->rsa->n,
1353 sensitive_data.server_key->rsa->n);
1354 /*
1355 * Xor the first 16 bytes of the session key with the
1356 * session id.
1357 */
1358 for (i = 0; i < 16; i++)
1359 session_key[i] ^= session_id[i];
46aa2d1f 1360 }
1361 }
1362 if (rsafail) {
00be5382 1363 int bytes = BN_num_bytes(session_key_int);
1364 char *buf = xmalloc(bytes);
1365 MD5_CTX md;
1366
46aa2d1f 1367 log("do_connection: generating a fake encryption key");
00be5382 1368 BN_bn2bin(session_key_int, buf);
1369 MD5_Init(&md);
1370 MD5_Update(&md, buf, bytes);
1371 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1372 MD5_Final(session_key, &md);
1373 MD5_Init(&md);
1374 MD5_Update(&md, session_key, 16);
1375 MD5_Update(&md, buf, bytes);
1376 MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1377 MD5_Final(session_key + 16, &md);
1378 memset(buf, 0, bytes);
1379 xfree(buf);
0572fe75 1380 for (i = 0; i < 16; i++)
1381 session_id[i] = session_key[i] ^ session_key[i + 16];
46aa2d1f 1382 }
00be5382 1383 /* Destroy the private and public keys. They will no longer be needed. */
1384 destroy_sensitive_data();
1385
7b2ea3a1 1386 /* Destroy the decrypted integer. It is no longer needed. */
1387 BN_clear_free(session_key_int);
1388
5260325f 1389 /* Set the session key. From this on all communications will be encrypted. */
1390 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
1391
1392 /* Destroy our copy of the session key. It is no longer needed. */
1393 memset(session_key, 0, sizeof(session_key));
1394
1395 debug("Received session key; encryption turned on.");
1396
1397 /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1398 packet_start(SSH_SMSG_SUCCESS);
1399 packet_send();
1400 packet_write_wait();
5260325f 1401}
e78a59f5 1402
1403/*
1404 * SSH2 key exchange: diffie-hellman-group1-sha1
1405 */
1406void
1e3b8b07 1407do_ssh2_kex(void)
e78a59f5 1408{
1409 Buffer *server_kexinit;
1410 Buffer *client_kexinit;
94ec8c6b 1411 int payload_len;
e78a59f5 1412 int i;
e78a59f5 1413 Kex *kex;
e78a59f5 1414 char *cprop[PROPOSAL_MAX];
e78a59f5 1415
1416/* KEXINIT */
1417
a8be9f80 1418 if (options.ciphers != NULL) {
6ae2364d 1419 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
a8be9f80 1420 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1421 }
b2552997 1422 if (options.macs != NULL) {
1423 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1424 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1425 }
fa08c86b 1426 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1427
80cd07ae 1428 myproposal[PROPOSAL_ENC_ALGS_STOC] =
1429 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1430
71276795 1431 server_kexinit = kex_init(myproposal);
e78a59f5 1432 client_kexinit = xmalloc(sizeof(*client_kexinit));
1433 buffer_init(client_kexinit);
e78a59f5 1434
71276795 1435 /* algorithm negotiation */
1436 kex_exchange_kexinit(server_kexinit, client_kexinit, cprop);
1437 kex = kex_choose_conf(cprop, myproposal, 1);
1438 for (i = 0; i < PROPOSAL_MAX; i++)
1439 xfree(cprop[i]);
e78a59f5 1440
94ec8c6b 1441 switch (kex->kex_type) {
1442 case DH_GRP1_SHA1:
1443 ssh_dh1_server(kex, client_kexinit, server_kexinit);
1444 break;
1445 case DH_GEX_SHA1:
1446 ssh_dhgex_server(kex, client_kexinit, server_kexinit);
1447 break;
1448 default:
1449 fatal("Unsupported key exchange %d", kex->kex_type);
1450 }
1451
1452 debug("send SSH2_MSG_NEWKEYS.");
1453 packet_start(SSH2_MSG_NEWKEYS);
1454 packet_send();
1455 packet_write_wait();
1456 debug("done: send SSH2_MSG_NEWKEYS.");
1457
1458 debug("Wait SSH2_MSG_NEWKEYS.");
1459 packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS);
1460 debug("GOT SSH2_MSG_NEWKEYS.");
1461
1462#ifdef DEBUG_KEXDH
1463 /* send 1st encrypted/maced/compressed message */
1464 packet_start(SSH2_MSG_IGNORE);
1465 packet_put_cstring("markus");
1466 packet_send();
1467 packet_write_wait();
1468#endif
1469
1470 debug("done: KEX2.");
1471}
1472
1473/*
1474 * SSH2 key exchange
1475 */
1476
1477/* diffie-hellman-group1-sha1 */
1478
1479void
1480ssh_dh1_server(Kex *kex, Buffer *client_kexinit, Buffer *server_kexinit)
1481{
1482#ifdef DEBUG_KEXDH
1483 int i;
1484#endif
1485 int payload_len, dlen;
1486 int slen;
1e3b8b07 1487 u_char *signature = NULL;
1488 u_char *server_host_key_blob = NULL;
1489 u_int sbloblen;
1490 u_int klen, kout;
1491 u_char *kbuf;
1492 u_char *hash;
94ec8c6b 1493 BIGNUM *shared_secret = 0;
1494 DH *dh;
1495 BIGNUM *dh_client_pub = 0;
fa08c86b 1496 Key *hostkey;
1497
1498 hostkey = get_hostkey_by_type(kex->hostkey_type);
1499 if (hostkey == NULL)
1500 fatal("Unsupported hostkey type %d", kex->hostkey_type);
e78a59f5 1501
94ec8c6b 1502/* KEXDH */
3e1caa83 1503 /* generate DH key */
1504 dh = dh_new_group1(); /* XXX depends on 'kex' */
7de5b06b 1505 dh_gen_key(dh, kex->we_need * 8);
3e1caa83 1506
e78a59f5 1507 debug("Wait SSH2_MSG_KEXDH_INIT.");
1508 packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT);
1509
1510 /* key, cert */
1511 dh_client_pub = BN_new();
1512 if (dh_client_pub == NULL)
1513 fatal("dh_client_pub == NULL");
1514 packet_get_bignum2(dh_client_pub, &dlen);
1515
1516#ifdef DEBUG_KEXDH
1517 fprintf(stderr, "\ndh_client_pub= ");
188adeb2 1518 BN_print_fp(stderr, dh_client_pub);
e78a59f5 1519 fprintf(stderr, "\n");
1520 debug("bits %d", BN_num_bits(dh_client_pub));
1521#endif
1522
e78a59f5 1523#ifdef DEBUG_KEXDH
1524 fprintf(stderr, "\np= ");
188adeb2 1525 BN_print_fp(stderr, dh->p);
e78a59f5 1526 fprintf(stderr, "\ng= ");
188adeb2 1527 bn_print(dh->g);
e78a59f5 1528 fprintf(stderr, "\npub= ");
188adeb2 1529 BN_print_fp(stderr, dh->pub_key);
e78a59f5 1530 fprintf(stderr, "\n");
2b87da3b 1531 DHparams_print_fp(stderr, dh);
e78a59f5 1532#endif
a8be9f80 1533 if (!dh_pub_is_valid(dh, dh_client_pub))
1534 packet_disconnect("bad client public DH value");
e78a59f5 1535
1536 klen = DH_size(dh);
1537 kbuf = xmalloc(klen);
1538 kout = DH_compute_key(kbuf, dh_client_pub, dh);
1539
1540#ifdef DEBUG_KEXDH
1541 debug("shared secret: len %d/%d", klen, kout);
1542 fprintf(stderr, "shared secret == ");
1543 for (i = 0; i< kout; i++)
1544 fprintf(stderr, "%02x", (kbuf[i])&0xff);
1545 fprintf(stderr, "\n");
1546#endif
1547 shared_secret = BN_new();
1548
1549 BN_bin2bn(kbuf, kout, shared_secret);
1550 memset(kbuf, 0, klen);
1551 xfree(kbuf);
1552
a306f2dd 1553 /* XXX precompute? */
fa08c86b 1554 key_to_blob(hostkey, &server_host_key_blob, &sbloblen);
e78a59f5 1555
1556 /* calc H */ /* XXX depends on 'kex' */
1557 hash = kex_hash(
1558 client_version_string,
1559 server_version_string,
1560 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1561 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1562 (char *)server_host_key_blob, sbloblen,
1563 dh_client_pub,
1564 dh->pub_key,
1565 shared_secret
1566 );
1567 buffer_free(client_kexinit);
1568 buffer_free(server_kexinit);
1569 xfree(client_kexinit);
1570 xfree(server_kexinit);
53a24016 1571 BN_free(dh_client_pub);
e78a59f5 1572#ifdef DEBUG_KEXDH
6ae2364d 1573 fprintf(stderr, "hash == ");
1574 for (i = 0; i< 20; i++)
1575 fprintf(stderr, "%02x", (hash[i])&0xff);
1576 fprintf(stderr, "\n");
e78a59f5 1577#endif
a306f2dd 1578 /* save session id := H */
1579 /* XXX hashlen depends on KEX */
1580 session_id2_len = 20;
1581 session_id2 = xmalloc(session_id2_len);
1582 memcpy(session_id2, hash, session_id2_len);
1583
e78a59f5 1584 /* sign H */
a306f2dd 1585 /* XXX hashlen depends on KEX */
fa08c86b 1586 key_sign(hostkey, &signature, &slen, hash, 20);
a306f2dd 1587
1588 destroy_sensitive_data();
e78a59f5 1589
1590 /* send server hostkey, DH pubkey 'f' and singed H */
1591 packet_start(SSH2_MSG_KEXDH_REPLY);
1592 packet_put_string((char *)server_host_key_blob, sbloblen);
1d1ffb87 1593 packet_put_bignum2(dh->pub_key); /* f */
e78a59f5 1594 packet_put_string((char *)signature, slen);
1595 packet_send();
d6f24e45 1596 xfree(signature);
a306f2dd 1597 xfree(server_host_key_blob);
e78a59f5 1598 packet_write_wait();
1599
1600 kex_derive_keys(kex, hash, shared_secret);
53a24016 1601 BN_clear_free(shared_secret);
e78a59f5 1602 packet_set_kex(kex);
1603
1604 /* have keys, free DH */
1605 DH_free(dh);
94ec8c6b 1606}
e78a59f5 1607
94ec8c6b 1608/* diffie-hellman-group-exchange-sha1 */
1609
1610void
1611ssh_dhgex_server(Kex *kex, Buffer *client_kexinit, Buffer *server_kexinit)
1612{
1613#ifdef DEBUG_KEXDH
1614 int i;
1615#endif
1616 int payload_len, dlen;
db1cd2f3 1617 int slen, nbits, type, min, max;
1e3b8b07 1618 u_char *signature = NULL;
1619 u_char *server_host_key_blob = NULL;
1620 u_int sbloblen;
1621 u_int klen, kout;
1622 u_char *kbuf;
1623 u_char *hash;
94ec8c6b 1624 BIGNUM *shared_secret = 0;
1625 DH *dh;
1626 BIGNUM *dh_client_pub = 0;
fa08c86b 1627 Key *hostkey;
1628
1629 hostkey = get_hostkey_by_type(kex->hostkey_type);
1630 if (hostkey == NULL)
1631 fatal("Unsupported hostkey type %d", kex->hostkey_type);
94ec8c6b 1632
1633/* KEXDHGEX */
1634 debug("Wait SSH2_MSG_KEX_DH_GEX_REQUEST.");
db1cd2f3 1635 type = packet_read(&payload_len);
1636 if (type != SSH2_MSG_KEX_DH_GEX_REQUEST_OLD &&
1637 type != SSH2_MSG_KEX_DH_GEX_REQUEST)
1638 packet_disconnect("Protocol error: expected type %d or %d, got %d",
1639 SSH2_MSG_KEX_DH_GEX_REQUEST_OLD,
1640 SSH2_MSG_KEX_DH_GEX_REQUEST,
1641 type);
1642 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
1643 nbits = packet_get_int();
1644 min = DH_GRP_MIN;
1645 max = DH_GRP_MAX;
1646 } else {
1647 min = packet_get_int();
1648 nbits = packet_get_int();
1649 max = packet_get_int();
1650
1651 min = MAX(DH_GRP_MIN, min);
1652 max = MIN(DH_GRP_MAX, max);
1653 }
1654
1655 if (max < min || nbits < min || max < nbits)
1656 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
1657 min, nbits, max);
1658
1659 dh = choose_dh(min, nbits, max);
1660 if (dh == NULL)
1661 packet_disconnect("Protocol error: no matching DH grp found");
94ec8c6b 1662
1663 debug("Sending SSH2_MSG_KEX_DH_GEX_GROUP.");
1664 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
1665 packet_put_bignum2(dh->p);
1666 packet_put_bignum2(dh->g);
e78a59f5 1667 packet_send();
1668 packet_write_wait();
e78a59f5 1669
3e1caa83 1670 /* Compute our exchange value in parallel with the client */
1671
7de5b06b 1672 dh_gen_key(dh, kex->we_need * 8);
3e1caa83 1673
94ec8c6b 1674 debug("Wait SSH2_MSG_KEX_DH_GEX_INIT.");
1675 packet_read_expect(&payload_len, SSH2_MSG_KEX_DH_GEX_INIT);
1676
1677 /* key, cert */
1678 dh_client_pub = BN_new();
1679 if (dh_client_pub == NULL)
1680 fatal("dh_client_pub == NULL");
1681 packet_get_bignum2(dh_client_pub, &dlen);
e78a59f5 1682
a8be9f80 1683#ifdef DEBUG_KEXDH
94ec8c6b 1684 fprintf(stderr, "\ndh_client_pub= ");
1685 BN_print_fp(stderr, dh_client_pub);
1686 fprintf(stderr, "\n");
1687 debug("bits %d", BN_num_bits(dh_client_pub));
1688#endif
1689
1690#ifdef DEBUG_KEXDH
1691 fprintf(stderr, "\np= ");
1692 BN_print_fp(stderr, dh->p);
1693 fprintf(stderr, "\ng= ");
1694 bn_print(dh->g);
1695 fprintf(stderr, "\npub= ");
1696 BN_print_fp(stderr, dh->pub_key);
1697 fprintf(stderr, "\n");
2b87da3b 1698 DHparams_print_fp(stderr, dh);
94ec8c6b 1699#endif
1700 if (!dh_pub_is_valid(dh, dh_client_pub))
1701 packet_disconnect("bad client public DH value");
1702
1703 klen = DH_size(dh);
1704 kbuf = xmalloc(klen);
1705 kout = DH_compute_key(kbuf, dh_client_pub, dh);
1706
1707#ifdef DEBUG_KEXDH
1708 debug("shared secret: len %d/%d", klen, kout);
1709 fprintf(stderr, "shared secret == ");
1710 for (i = 0; i< kout; i++)
1711 fprintf(stderr, "%02x", (kbuf[i])&0xff);
1712 fprintf(stderr, "\n");
1713#endif
1714 shared_secret = BN_new();
1715
1716 BN_bin2bn(kbuf, kout, shared_secret);
1717 memset(kbuf, 0, klen);
1718 xfree(kbuf);
1719
1720 /* XXX precompute? */
fa08c86b 1721 key_to_blob(hostkey, &server_host_key_blob, &sbloblen);
94ec8c6b 1722
4047d868 1723 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
1724 /* These values are not included in the hash */
1725 min = -1;
1726 max = -1;
1727 }
1728
94ec8c6b 1729 /* calc H */ /* XXX depends on 'kex' */
1730 hash = kex_hash_gex(
1731 client_version_string,
1732 server_version_string,
1733 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1734 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1735 (char *)server_host_key_blob, sbloblen,
4047d868 1736 min, nbits, max,
1737 dh->p, dh->g,
94ec8c6b 1738 dh_client_pub,
1739 dh->pub_key,
1740 shared_secret
1741 );
1742 buffer_free(client_kexinit);
1743 buffer_free(server_kexinit);
1744 xfree(client_kexinit);
1745 xfree(server_kexinit);
53a24016 1746 BN_free(dh_client_pub);
94ec8c6b 1747#ifdef DEBUG_KEXDH
1748 fprintf(stderr, "hash == ");
1749 for (i = 0; i< 20; i++)
1750 fprintf(stderr, "%02x", (hash[i])&0xff);
1751 fprintf(stderr, "\n");
1752#endif
1753 /* save session id := H */
1754 /* XXX hashlen depends on KEX */
1755 session_id2_len = 20;
1756 session_id2 = xmalloc(session_id2_len);
1757 memcpy(session_id2, hash, session_id2_len);
1758
1759 /* sign H */
1760 /* XXX hashlen depends on KEX */
fa08c86b 1761 key_sign(hostkey, &signature, &slen, hash, 20);
94ec8c6b 1762
1763 destroy_sensitive_data();
1764
1765 /* send server hostkey, DH pubkey 'f' and singed H */
1766 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
1767 packet_put_string((char *)server_host_key_blob, sbloblen);
1768 packet_put_bignum2(dh->pub_key); /* f */
1769 packet_put_string((char *)signature, slen);
e78a59f5 1770 packet_send();
94ec8c6b 1771 xfree(signature);
1772 xfree(server_host_key_blob);
e78a59f5 1773 packet_write_wait();
94ec8c6b 1774
1775 kex_derive_keys(kex, hash, shared_secret);
53a24016 1776 BN_clear_free(shared_secret);
94ec8c6b 1777 packet_set_kex(kex);
1778
1779 /* have keys, free DH */
1780 DH_free(dh);
e78a59f5 1781}
This page took 0.47093 seconds and 5 git commands to generate.