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