Annotation of src/usr.bin/ssh/sshd.c, Revision 1.184
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.184 ! stevesk 43: RCSID("$OpenBSD: sshd.c,v 1.183 2001/03/28 21:59:41 provos 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: }
1.180 markus 638: SSLeay_add_all_algorithms();
1.64 markus 639:
1.75 markus 640: /*
641: * Force logging to stderr until we have loaded the private host
642: * key (unless started from inetd)
643: */
1.138 markus 644: log_init(__progname,
1.152 markus 645: options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
1.75 markus 646: options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
1.158 markus 647: !inetd_flag);
1.75 markus 648:
1.64 markus 649: /* Read server configuration options from the configuration file. */
650: read_server_config(&options, config_file_name);
651:
652: /* Fill in default values for those options not explicitly set. */
653: fill_default_server_options(&options);
654:
655: /* Check that there are no remaining arguments. */
656: if (optind < ac) {
657: fprintf(stderr, "Extra argument %s.\n", av[optind]);
658: exit(1);
659: }
660:
661: debug("sshd version %.100s", SSH_VERSION);
662:
1.134 markus 663: /* load private host keys */
664: sensitive_data.host_keys = xmalloc(options.num_host_key_files*sizeof(Key*));
1.141 markus 665: for(i = 0; i < options.num_host_key_files; i++)
666: sensitive_data.host_keys[i] = NULL;
1.134 markus 667: sensitive_data.server_key = NULL;
668: sensitive_data.ssh1_host_key = NULL;
669: sensitive_data.have_ssh1_key = 0;
670: sensitive_data.have_ssh2_key = 0;
671:
672: for(i = 0; i < options.num_host_key_files; i++) {
1.179 markus 673: key = key_load_private(options.host_key_files[i], "", NULL);
674: sensitive_data.host_keys[i] = key;
1.134 markus 675: if (key == NULL) {
676: error("Could not load host key: %.200s: %.100s",
677: options.host_key_files[i], strerror(errno));
1.179 markus 678: sensitive_data.host_keys[i] = NULL;
1.134 markus 679: continue;
680: }
681: switch(key->type){
682: case KEY_RSA1:
683: sensitive_data.ssh1_host_key = key;
684: sensitive_data.have_ssh1_key = 1;
685: break;
686: case KEY_RSA:
687: case KEY_DSA:
688: sensitive_data.have_ssh2_key = 1;
689: break;
690: }
1.179 markus 691: debug("private host key: #%d type %d %s", i, key->type,
692: key_type(key));
1.134 markus 693: }
694: if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
695: log("Disabling protocol version 1. Could not load host key");
1.108 markus 696: options.protocol &= ~SSH_PROTO_1;
697: }
1.134 markus 698: if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
699: log("Disabling protocol version 2. Could not load host key");
700: options.protocol &= ~SSH_PROTO_2;
1.108 markus 701: }
1.162 stevesk 702: if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.172 millert 703: log("sshd: no hostkeys available -- exiting.");
1.64 markus 704: exit(1);
705: }
706:
1.108 markus 707: /* Check certain values for sanity. */
708: if (options.protocol & SSH_PROTO_1) {
709: if (options.server_key_bits < 512 ||
710: options.server_key_bits > 32768) {
711: fprintf(stderr, "Bad server key size.\n");
712: exit(1);
713: }
714: /*
715: * Check that server and host key lengths differ sufficiently. This
716: * is necessary to make double encryption work with rsaref. Oh, I
717: * hate software patents. I dont know if this can go? Niels
718: */
719: if (options.server_key_bits >
1.134 markus 720: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) - SSH_KEY_BITS_RESERVED &&
1.108 markus 721: options.server_key_bits <
1.134 markus 722: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1.108 markus 723: options.server_key_bits =
1.134 markus 724: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED;
1.108 markus 725: debug("Forcing server key to %d bits to make it differ from host key.",
726: options.server_key_bits);
727: }
728: }
729:
730: /* Initialize the log (it is reinitialized below in case we forked). */
1.64 markus 731: if (debug_flag && !inetd_flag)
732: log_stderr = 1;
1.138 markus 733: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 734:
1.108 markus 735: /*
736: * If not in debugging mode, and not started from inetd, disconnect
737: * from the controlling terminal, and fork. The original process
738: * exits.
739: */
1.135 markus 740: if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.1 deraadt 741: #ifdef TIOCNOTTY
1.64 markus 742: int fd;
1.1 deraadt 743: #endif /* TIOCNOTTY */
1.64 markus 744: if (daemon(0, 0) < 0)
745: fatal("daemon() failed: %.200s", strerror(errno));
746:
747: /* Disconnect from the controlling tty. */
1.1 deraadt 748: #ifdef TIOCNOTTY
1.165 itojun 749: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64 markus 750: if (fd >= 0) {
751: (void) ioctl(fd, TIOCNOTTY, NULL);
752: close(fd);
753: }
754: #endif /* TIOCNOTTY */
755: }
756: /* Reinitialize the log (because of the fork above). */
1.138 markus 757: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 758:
759: /* Initialize the random number generator. */
760: arc4random_stir();
761:
762: /* Chdir to the root directory so that the current disk can be
763: unmounted if desired. */
764: chdir("/");
1.178 markus 765:
766: /* ignore SIGPIPE */
767: signal(SIGPIPE, SIG_IGN);
1.64 markus 768:
769: /* Start listening for a socket, unless started from inetd. */
770: if (inetd_flag) {
771: int s1, s2;
772: s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
773: s2 = dup(s1);
774: sock_in = dup(0);
775: sock_out = dup(1);
1.123 djm 776: startup_pipe = -1;
1.108 markus 777: /*
778: * We intentionally do not close the descriptors 0, 1, and 2
779: * as our code for setting the descriptors won\'t work if
780: * ttyfd happens to be one of those.
781: */
1.64 markus 782: debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
1.134 markus 783: if (options.protocol & SSH_PROTO_1)
1.174 deraadt 784: generate_ephemeral_server_key();
1.64 markus 785: } else {
1.75 markus 786: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
787: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
788: continue;
789: if (num_listen_socks >= MAX_LISTEN_SOCKS)
790: fatal("Too many listen sockets. "
791: "Enlarge MAX_LISTEN_SOCKS");
792: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
793: ntop, sizeof(ntop), strport, sizeof(strport),
794: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
795: error("getnameinfo failed");
796: continue;
797: }
798: /* Create socket for listening. */
799: listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
800: if (listen_sock < 0) {
801: /* kernel may not support ipv6 */
802: verbose("socket: %.100s", strerror(errno));
803: continue;
804: }
805: if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
806: error("listen_sock O_NONBLOCK: %s", strerror(errno));
807: close(listen_sock);
808: continue;
809: }
810: /*
811: * Set socket options. We try to make the port
812: * reusable and have it close as fast as possible
813: * without waiting in unnecessary wait states on
814: * close.
815: */
816: setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
817: (void *) &on, sizeof(on));
818: linger.l_onoff = 1;
819: linger.l_linger = 5;
820: setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
821: (void *) &linger, sizeof(linger));
822:
823: debug("Bind to port %s on %s.", strport, ntop);
824:
825: /* Bind the socket to the desired port. */
826: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
827: error("Bind to port %s on %s failed: %.200s.",
828: strport, ntop, strerror(errno));
829: close(listen_sock);
830: continue;
831: }
832: listen_socks[num_listen_socks] = listen_sock;
833: num_listen_socks++;
834:
835: /* Start listening on the port. */
836: log("Server listening on %s port %s.", ntop, strport);
837: if (listen(listen_sock, 5) < 0)
838: fatal("listen: %.100s", strerror(errno));
839:
1.64 markus 840: }
1.75 markus 841: freeaddrinfo(options.listen_addrs);
842:
843: if (!num_listen_socks)
844: fatal("Cannot bind any address.");
845:
1.64 markus 846: if (!debug_flag) {
1.66 markus 847: /*
1.136 todd 848: * Record our pid in /var/run/sshd.pid to make it
849: * easier to kill the correct sshd. We don't want to
850: * do this before the bind above because the bind will
1.66 markus 851: * fail if there already is a daemon, and this will
852: * overwrite any old pid in the file.
853: */
1.112 markus 854: f = fopen(options.pid_file, "w");
1.64 markus 855: if (f) {
1.140 markus 856: fprintf(f, "%u\n", (u_int) getpid());
1.64 markus 857: fclose(f);
858: }
859: }
1.151 markus 860: if (options.protocol & SSH_PROTO_1)
1.174 deraadt 861: generate_ephemeral_server_key();
1.64 markus 862:
863: /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
864: signal(SIGHUP, sighup_handler);
1.120 markus 865:
1.64 markus 866: signal(SIGTERM, sigterm_handler);
867: signal(SIGQUIT, sigterm_handler);
868:
869: /* Arrange SIGCHLD to be caught. */
870: signal(SIGCHLD, main_sigchld_handler);
871:
1.75 markus 872: /* setup fd set for listen */
1.120 markus 873: fdset = NULL;
1.75 markus 874: maxfd = 0;
875: for (i = 0; i < num_listen_socks; i++)
876: if (listen_socks[i] > maxfd)
877: maxfd = listen_socks[i];
1.120 markus 878: /* pipes connected to unauthenticated childs */
879: startup_pipes = xmalloc(options.max_startups * sizeof(int));
880: for (i = 0; i < options.max_startups; i++)
881: startup_pipes[i] = -1;
1.75 markus 882:
1.66 markus 883: /*
884: * Stay listening for connections until the system crashes or
885: * the daemon is killed with a signal.
886: */
1.64 markus 887: for (;;) {
888: if (received_sighup)
889: sighup_restart();
1.120 markus 890: if (fdset != NULL)
891: xfree(fdset);
1.148 markus 892: fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
1.120 markus 893: fdset = (fd_set *)xmalloc(fdsetsz);
1.75 markus 894: memset(fdset, 0, fdsetsz);
1.120 markus 895:
1.75 markus 896: for (i = 0; i < num_listen_socks; i++)
897: FD_SET(listen_socks[i], fdset);
1.120 markus 898: for (i = 0; i < options.max_startups; i++)
899: if (startup_pipes[i] != -1)
900: FD_SET(startup_pipes[i], fdset);
901:
902: /* Wait in select until there is a connection. */
1.151 markus 903: ret = select(maxfd+1, fdset, NULL, NULL, NULL);
904: if (ret < 0 && errno != EINTR)
905: error("select: %.100s", strerror(errno));
906: if (key_used && key_do_regen) {
1.174 deraadt 907: generate_ephemeral_server_key();
1.151 markus 908: key_used = 0;
909: key_do_regen = 0;
910: }
911: if (ret < 0)
1.75 markus 912: continue;
1.151 markus 913:
1.120 markus 914: for (i = 0; i < options.max_startups; i++)
915: if (startup_pipes[i] != -1 &&
916: FD_ISSET(startup_pipes[i], fdset)) {
917: /*
918: * the read end of the pipe is ready
919: * if the child has closed the pipe
1.143 markus 920: * after successful authentication
1.120 markus 921: * or if the child has died
922: */
923: close(startup_pipes[i]);
924: startup_pipes[i] = -1;
925: startups--;
926: }
1.75 markus 927: for (i = 0; i < num_listen_socks; i++) {
928: if (!FD_ISSET(listen_socks[i], fdset))
1.70 provos 929: continue;
1.120 markus 930: fromlen = sizeof(from);
931: newsock = accept(listen_socks[i], (struct sockaddr *)&from,
932: &fromlen);
933: if (newsock < 0) {
934: if (errno != EINTR && errno != EWOULDBLOCK)
935: error("accept: %.100s", strerror(errno));
936: continue;
937: }
938: if (fcntl(newsock, F_SETFL, 0) < 0) {
939: error("newsock del O_NONBLOCK: %s", strerror(errno));
940: continue;
941: }
1.124 markus 942: if (drop_connection(startups) == 1) {
943: debug("drop connection #%d", startups);
1.120 markus 944: close(newsock);
945: continue;
946: }
947: if (pipe(startup_p) == -1) {
948: close(newsock);
949: continue;
950: }
951:
952: for (j = 0; j < options.max_startups; j++)
953: if (startup_pipes[j] == -1) {
954: startup_pipes[j] = startup_p[0];
955: if (maxfd < startup_p[0])
956: maxfd = startup_p[0];
957: startups++;
958: break;
959: }
1.161 stevesk 960:
1.66 markus 961: /*
1.120 markus 962: * Got connection. Fork a child to handle it, unless
963: * we are in debugging mode.
1.66 markus 964: */
1.120 markus 965: if (debug_flag) {
1.66 markus 966: /*
1.120 markus 967: * In debugging mode. Close the listening
968: * socket, and start processing the
969: * connection without forking.
1.66 markus 970: */
1.120 markus 971: debug("Server will not fork when running in debugging mode.");
1.75 markus 972: close_listen_socks();
1.64 markus 973: sock_in = newsock;
974: sock_out = newsock;
1.122 deraadt 975: startup_pipe = -1;
1.120 markus 976: pid = getpid();
1.64 markus 977: break;
1.120 markus 978: } else {
979: /*
980: * Normal production daemon. Fork, and have
981: * the child process the connection. The
982: * parent continues listening.
983: */
984: if ((pid = fork()) == 0) {
985: /*
986: * Child. Close the listening and max_startup
987: * sockets. Start using the accepted socket.
988: * Reinitialize logging (since our pid has
989: * changed). We break out of the loop to handle
990: * the connection.
991: */
992: startup_pipe = startup_p[1];
993: for (j = 0; j < options.max_startups; j++)
994: if (startup_pipes[j] != -1)
995: close(startup_pipes[j]);
996: close_listen_socks();
997: sock_in = newsock;
998: sock_out = newsock;
1.138 markus 999: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.120 markus 1000: break;
1001: }
1.64 markus 1002: }
1003:
1.120 markus 1004: /* Parent. Stay in the loop. */
1005: if (pid < 0)
1006: error("fork: %.100s", strerror(errno));
1007: else
1008: debug("Forked child %d.", pid);
1009:
1010: close(startup_p[1]);
1.1 deraadt 1011:
1.120 markus 1012: /* Mark that the key has been used (it was "given" to the child). */
1.151 markus 1013: if ((options.protocol & SSH_PROTO_1) &&
1014: key_used == 0) {
1015: /* Schedule server key regeneration alarm. */
1016: signal(SIGALRM, key_regeneration_alarm);
1017: alarm(options.key_regeneration_time);
1018: key_used = 1;
1019: }
1.1 deraadt 1020:
1.120 markus 1021: arc4random_stir();
1.1 deraadt 1022:
1.120 markus 1023: /* Close the new socket (the child is now taking care of it). */
1024: close(newsock);
1025: }
1.75 markus 1026: /* child process check (or debug mode) */
1027: if (num_listen_socks < 0)
1028: break;
1.64 markus 1029: }
1.1 deraadt 1030: }
1031:
1.64 markus 1032: /* This is the child processing a new connection. */
1033:
1.66 markus 1034: /*
1035: * Disable the key regeneration alarm. We will not regenerate the
1036: * key since we are no longer in a position to give it to anyone. We
1037: * will not restart on SIGHUP since it no longer makes sense.
1038: */
1.64 markus 1039: alarm(0);
1040: signal(SIGALRM, SIG_DFL);
1041: signal(SIGHUP, SIG_DFL);
1042: signal(SIGTERM, SIG_DFL);
1043: signal(SIGQUIT, SIG_DFL);
1044: signal(SIGCHLD, SIG_DFL);
1045:
1.66 markus 1046: /*
1047: * Set socket options for the connection. We want the socket to
1048: * close as fast as possible without waiting for anything. If the
1049: * connection is not a socket, these will do nothing.
1050: */
1051: /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
1.64 markus 1052: linger.l_onoff = 1;
1053: linger.l_linger = 5;
1054: setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
1.150 markus 1055:
1056: /* Set keepalives if requested. */
1057: if (options.keepalives &&
1058: setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
1059: sizeof(on)) < 0)
1060: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.64 markus 1061:
1.66 markus 1062: /*
1063: * Register our connection. This turns encryption off because we do
1064: * not have a key.
1065: */
1.64 markus 1066: packet_set_connection(sock_in, sock_out);
1.1 deraadt 1067:
1.64 markus 1068: remote_port = get_remote_port();
1069: remote_ip = get_remote_ipaddr();
1.52 markus 1070:
1.64 markus 1071: /* Check whether logins are denied from this host. */
1.37 dugsong 1072: #ifdef LIBWRAP
1.75 markus 1073: /* XXX LIBWRAP noes not know about IPv6 */
1.64 markus 1074: {
1075: struct request_info req;
1.37 dugsong 1076:
1.138 markus 1077: request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, NULL);
1.64 markus 1078: fromhost(&req);
1.37 dugsong 1079:
1.64 markus 1080: if (!hosts_access(&req)) {
1.182 markus 1081: refuse(&req);
1.64 markus 1082: close(sock_in);
1083: close(sock_out);
1084: }
1.75 markus 1085: /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
1.64 markus 1086: }
1.75 markus 1087: #endif /* LIBWRAP */
1.64 markus 1088: /* Log the connection. */
1089: verbose("Connection from %.500s port %d", remote_ip, remote_port);
1.1 deraadt 1090:
1.66 markus 1091: /*
1092: * We don\'t want to listen forever unless the other side
1093: * successfully authenticates itself. So we set up an alarm which is
1094: * cleared after successful authentication. A limit of zero
1095: * indicates no limit. Note that we don\'t set the alarm in debugging
1096: * mode; it is just annoying to have the server exit just when you
1097: * are about to discover the bug.
1098: */
1.64 markus 1099: signal(SIGALRM, grace_alarm_handler);
1100: if (!debug_flag)
1101: alarm(options.login_grace_time);
1102:
1.96 markus 1103: sshd_exchange_identification(sock_in, sock_out);
1.66 markus 1104: /*
1.137 markus 1105: * Check that the connection comes from a privileged port.
1106: * Rhosts-Authentication only makes sense from priviledged
1.66 markus 1107: * programs. Of course, if the intruder has root access on his local
1108: * machine, he can connect from any port. So do not use these
1109: * authentication methods from machines that you do not trust.
1110: */
1.64 markus 1111: if (remote_port >= IPPORT_RESERVED ||
1112: remote_port < IPPORT_RESERVED / 2) {
1.137 markus 1113: debug("Rhosts Authentication disabled, "
1.133 markus 1114: "originating port not trusted.");
1.64 markus 1115: options.rhosts_authentication = 0;
1116: }
1.76 markus 1117: #ifdef KRB4
1118: if (!packet_connection_is_ipv4() &&
1119: options.kerberos_authentication) {
1120: debug("Kerberos Authentication disabled, only available for IPv4.");
1121: options.kerberos_authentication = 0;
1122: }
1123: #endif /* KRB4 */
1.164 markus 1124: #ifdef AFS
1125: /* If machine has AFS, set process authentication group. */
1126: if (k_hasafs()) {
1127: k_setpag();
1128: k_unlog();
1129: }
1130: #endif /* AFS */
1.76 markus 1131:
1.64 markus 1132: packet_set_nonblocking();
1.1 deraadt 1133:
1.77 markus 1134: /* perform the key exchange */
1135: /* authenticate user and start session */
1.98 markus 1136: if (compat20) {
1137: do_ssh2_kex();
1138: do_authentication2();
1139: } else {
1140: do_ssh1_kex();
1141: do_authentication();
1142: }
1.1 deraadt 1143:
1144: #ifdef KRB4
1.64 markus 1145: /* Cleanup user's ticket cache file. */
1146: if (options.kerberos_ticket_cleanup)
1147: (void) dest_tkt();
1.1 deraadt 1148: #endif /* KRB4 */
1149:
1.64 markus 1150: /* The connection has been terminated. */
1151: verbose("Closing connection to %.100s", remote_ip);
1152: packet_close();
1153: exit(0);
1.1 deraadt 1154: }
1155:
1.65 deraadt 1156: /*
1.77 markus 1157: * SSH1 key exchange
1.65 deraadt 1158: */
1.52 markus 1159: void
1.142 markus 1160: do_ssh1_kex(void)
1.1 deraadt 1161: {
1.64 markus 1162: int i, len;
1.77 markus 1163: int plen, slen;
1.159 markus 1164: int rsafail = 0;
1.64 markus 1165: BIGNUM *session_key_int;
1.140 markus 1166: u_char session_key[SSH_SESSION_KEY_LENGTH];
1167: u_char cookie[8];
1168: u_int cipher_type, auth_mask, protocol_flags;
1.64 markus 1169: u_int32_t rand = 0;
1170:
1.66 markus 1171: /*
1172: * Generate check bytes that the client must send back in the user
1173: * packet in order for it to be accepted; this is used to defy ip
1174: * spoofing attacks. Note that this only works against somebody
1175: * doing IP spoofing from a remote machine; any machine on the local
1176: * network can still see outgoing packets and catch the random
1177: * cookie. This only affects rhosts authentication, and this is one
1178: * of the reasons why it is inherently insecure.
1179: */
1.64 markus 1180: for (i = 0; i < 8; i++) {
1181: if (i % 4 == 0)
1182: rand = arc4random();
1.77 markus 1183: cookie[i] = rand & 0xff;
1.64 markus 1184: rand >>= 8;
1185: }
1186:
1.66 markus 1187: /*
1188: * Send our public key. We include in the packet 64 bits of random
1189: * data that must be matched in the reply in order to prevent IP
1190: * spoofing.
1191: */
1.64 markus 1192: packet_start(SSH_SMSG_PUBLIC_KEY);
1193: for (i = 0; i < 8; i++)
1.77 markus 1194: packet_put_char(cookie[i]);
1.64 markus 1195:
1196: /* Store our public server RSA key. */
1.134 markus 1197: packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1198: packet_put_bignum(sensitive_data.server_key->rsa->e);
1199: packet_put_bignum(sensitive_data.server_key->rsa->n);
1.64 markus 1200:
1201: /* Store our public host RSA key. */
1.134 markus 1202: packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1203: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1204: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
1.64 markus 1205:
1206: /* Put protocol flags. */
1207: packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1208:
1209: /* Declare which ciphers we support. */
1.131 markus 1210: packet_put_int(cipher_mask_ssh1(0));
1.64 markus 1211:
1212: /* Declare supported authentication types. */
1213: auth_mask = 0;
1214: if (options.rhosts_authentication)
1215: auth_mask |= 1 << SSH_AUTH_RHOSTS;
1216: if (options.rhosts_rsa_authentication)
1217: auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1218: if (options.rsa_authentication)
1219: auth_mask |= 1 << SSH_AUTH_RSA;
1.1 deraadt 1220: #ifdef KRB4
1.64 markus 1221: if (options.kerberos_authentication)
1222: auth_mask |= 1 << SSH_AUTH_KERBEROS;
1.1 deraadt 1223: #endif
1.5 dugsong 1224: #ifdef AFS
1.64 markus 1225: if (options.kerberos_tgt_passing)
1226: auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1227: if (options.afs_token_passing)
1228: auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
1.1 deraadt 1229: #endif
1.157 markus 1230: if (options.challenge_reponse_authentication == 1)
1.64 markus 1231: auth_mask |= 1 << SSH_AUTH_TIS;
1232: if (options.password_authentication)
1233: auth_mask |= 1 << SSH_AUTH_PASSWORD;
1234: packet_put_int(auth_mask);
1235:
1236: /* Send the packet and wait for it to be sent. */
1237: packet_send();
1238: packet_write_wait();
1239:
1.134 markus 1240: debug("Sent %d bit server key and %d bit host key.",
1241: BN_num_bits(sensitive_data.server_key->rsa->n),
1242: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1.64 markus 1243:
1244: /* Read clients reply (cipher type and session key). */
1245: packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
1246:
1.69 markus 1247: /* Get cipher type and check whether we accept this. */
1.64 markus 1248: cipher_type = packet_get_char();
1.69 markus 1249:
1.131 markus 1250: if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
1.69 markus 1251: packet_disconnect("Warning: client selects unsupported cipher.");
1.64 markus 1252:
1253: /* Get check bytes from the packet. These must match those we
1254: sent earlier with the public key packet. */
1255: for (i = 0; i < 8; i++)
1.77 markus 1256: if (cookie[i] != packet_get_char())
1.64 markus 1257: packet_disconnect("IP Spoofing check bytes do not match.");
1258:
1259: debug("Encryption type: %.200s", cipher_name(cipher_type));
1260:
1261: /* Get the encrypted integer. */
1262: session_key_int = BN_new();
1263: packet_get_bignum(session_key_int, &slen);
1264:
1265: protocol_flags = packet_get_int();
1266: packet_set_protocol_flags(protocol_flags);
1267:
1268: packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
1269:
1.66 markus 1270: /*
1271: * Decrypt it using our private server key and private host key (key
1272: * with larger modulus first).
1273: */
1.134 markus 1274: if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
1.159 markus 1275: /* Server key has bigger modulus. */
1.134 markus 1276: if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1277: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1278: fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1279: get_remote_ipaddr(),
1280: BN_num_bits(sensitive_data.server_key->rsa->n),
1281: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1282: SSH_KEY_BITS_RESERVED);
1.64 markus 1283: }
1.159 markus 1284: if (rsa_private_decrypt(session_key_int, session_key_int,
1285: sensitive_data.server_key->rsa) <= 0)
1286: rsafail++;
1287: if (rsa_private_decrypt(session_key_int, session_key_int,
1288: sensitive_data.ssh1_host_key->rsa) <= 0)
1289: rsafail++;
1.64 markus 1290: } else {
1291: /* Host key has bigger modulus (or they are equal). */
1.134 markus 1292: if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1293: BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1294: fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1295: get_remote_ipaddr(),
1296: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1297: BN_num_bits(sensitive_data.server_key->rsa->n),
1298: SSH_KEY_BITS_RESERVED);
1.64 markus 1299: }
1.159 markus 1300: if (rsa_private_decrypt(session_key_int, session_key_int,
1301: sensitive_data.ssh1_host_key->rsa) < 0)
1302: rsafail++;
1303: if (rsa_private_decrypt(session_key_int, session_key_int,
1304: sensitive_data.server_key->rsa) < 0)
1305: rsafail++;
1.64 markus 1306: }
1.66 markus 1307: /*
1308: * Extract session key from the decrypted integer. The key is in the
1309: * least significant 256 bits of the integer; the first byte of the
1310: * key is in the highest bits.
1311: */
1.159 markus 1312: if (!rsafail) {
1313: BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1314: len = BN_num_bytes(session_key_int);
1315: if (len < 0 || len > sizeof(session_key)) {
1316: error("do_connection: bad session key len from %s: "
1.165 itojun 1317: "session_key_int %d > sizeof(session_key) %lu",
1318: get_remote_ipaddr(), len, (u_long)sizeof(session_key));
1.159 markus 1319: rsafail++;
1320: } else {
1321: memset(session_key, 0, sizeof(session_key));
1322: BN_bn2bin(session_key_int,
1323: session_key + sizeof(session_key) - len);
1.169 markus 1324:
1325: compute_session_id(session_id, cookie,
1326: sensitive_data.ssh1_host_key->rsa->n,
1327: sensitive_data.server_key->rsa->n);
1328: /*
1329: * Xor the first 16 bytes of the session key with the
1330: * session id.
1331: */
1332: for (i = 0; i < 16; i++)
1333: session_key[i] ^= session_id[i];
1.159 markus 1334: }
1335: }
1336: if (rsafail) {
1.169 markus 1337: int bytes = BN_num_bytes(session_key_int);
1338: char *buf = xmalloc(bytes);
1339: MD5_CTX md;
1340:
1.159 markus 1341: log("do_connection: generating a fake encryption key");
1.169 markus 1342: BN_bn2bin(session_key_int, buf);
1343: MD5_Init(&md);
1344: MD5_Update(&md, buf, bytes);
1345: MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1346: MD5_Final(session_key, &md);
1347: MD5_Init(&md);
1348: MD5_Update(&md, session_key, 16);
1349: MD5_Update(&md, buf, bytes);
1350: MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1351: MD5_Final(session_key + 16, &md);
1352: memset(buf, 0, bytes);
1353: xfree(buf);
1.170 markus 1354: for (i = 0; i < 16; i++)
1355: session_id[i] = session_key[i] ^ session_key[i + 16];
1.159 markus 1356: }
1.169 markus 1357: /* Destroy the private and public keys. They will no longer be needed. */
1358: destroy_sensitive_data();
1359:
1.77 markus 1360: /* Destroy the decrypted integer. It is no longer needed. */
1361: BN_clear_free(session_key_int);
1.64 markus 1362:
1363: /* Set the session key. From this on all communications will be encrypted. */
1364: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
1365:
1366: /* Destroy our copy of the session key. It is no longer needed. */
1367: memset(session_key, 0, sizeof(session_key));
1368:
1369: debug("Received session key; encryption turned on.");
1370:
1371: /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1372: packet_start(SSH_SMSG_SUCCESS);
1373: packet_send();
1374: packet_write_wait();
1.98 markus 1375: }
1376:
1377: /*
1378: * SSH2 key exchange: diffie-hellman-group1-sha1
1379: */
1380: void
1.142 markus 1381: do_ssh2_kex(void)
1.98 markus 1382: {
1383: Buffer *server_kexinit;
1384: Buffer *client_kexinit;
1.129 provos 1385: int payload_len;
1.98 markus 1386: int i;
1387: Kex *kex;
1388: char *cprop[PROPOSAL_MAX];
1389:
1390: /* KEXINIT */
1.102 markus 1391:
1392: if (options.ciphers != NULL) {
1.105 markus 1393: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102 markus 1394: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1.166 markus 1395: }
1.184 ! stevesk 1396: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
! 1397: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
! 1398: myproposal[PROPOSAL_ENC_ALGS_STOC] =
! 1399: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
! 1400:
1.166 markus 1401: if (options.macs != NULL) {
1402: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1403: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.102 markus 1404: }
1.134 markus 1405: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1406:
1.118 markus 1407: server_kexinit = kex_init(myproposal);
1.98 markus 1408: client_kexinit = xmalloc(sizeof(*client_kexinit));
1409: buffer_init(client_kexinit);
1410:
1.118 markus 1411: /* algorithm negotiation */
1412: kex_exchange_kexinit(server_kexinit, client_kexinit, cprop);
1413: kex = kex_choose_conf(cprop, myproposal, 1);
1414: for (i = 0; i < PROPOSAL_MAX; i++)
1415: xfree(cprop[i]);
1.98 markus 1416:
1.129 provos 1417: switch (kex->kex_type) {
1418: case DH_GRP1_SHA1:
1419: ssh_dh1_server(kex, client_kexinit, server_kexinit);
1420: break;
1421: case DH_GEX_SHA1:
1422: ssh_dhgex_server(kex, client_kexinit, server_kexinit);
1423: break;
1424: default:
1425: fatal("Unsupported key exchange %d", kex->kex_type);
1426: }
1427:
1428: debug("send SSH2_MSG_NEWKEYS.");
1429: packet_start(SSH2_MSG_NEWKEYS);
1430: packet_send();
1431: packet_write_wait();
1432: debug("done: send SSH2_MSG_NEWKEYS.");
1433:
1434: debug("Wait SSH2_MSG_NEWKEYS.");
1435: packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS);
1436: debug("GOT SSH2_MSG_NEWKEYS.");
1437:
1438: #ifdef DEBUG_KEXDH
1439: /* send 1st encrypted/maced/compressed message */
1440: packet_start(SSH2_MSG_IGNORE);
1441: packet_put_cstring("markus");
1442: packet_send();
1443: packet_write_wait();
1444: #endif
1445:
1446: debug("done: KEX2.");
1447: }
1448:
1449: /*
1450: * SSH2 key exchange
1451: */
1452:
1453: /* diffie-hellman-group1-sha1 */
1454:
1455: void
1456: ssh_dh1_server(Kex *kex, Buffer *client_kexinit, Buffer *server_kexinit)
1457: {
1.130 markus 1458: #ifdef DEBUG_KEXDH
1459: int i;
1460: #endif
1.129 provos 1461: int payload_len, dlen;
1462: int slen;
1.140 markus 1463: u_char *signature = NULL;
1464: u_char *server_host_key_blob = NULL;
1465: u_int sbloblen;
1466: u_int klen, kout;
1467: u_char *kbuf;
1468: u_char *hash;
1.129 provos 1469: BIGNUM *shared_secret = 0;
1470: DH *dh;
1471: BIGNUM *dh_client_pub = 0;
1.134 markus 1472: Key *hostkey;
1473:
1474: hostkey = get_hostkey_by_type(kex->hostkey_type);
1475: if (hostkey == NULL)
1476: fatal("Unsupported hostkey type %d", kex->hostkey_type);
1.129 provos 1477:
1.98 markus 1478: /* KEXDH */
1.139 provos 1479: /* generate DH key */
1480: dh = dh_new_group1(); /* XXX depends on 'kex' */
1.173 markus 1481: dh_gen_key(dh, kex->we_need * 8);
1.139 provos 1482:
1.98 markus 1483: debug("Wait SSH2_MSG_KEXDH_INIT.");
1484: packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT);
1485:
1486: /* key, cert */
1487: dh_client_pub = BN_new();
1488: if (dh_client_pub == NULL)
1489: fatal("dh_client_pub == NULL");
1490: packet_get_bignum2(dh_client_pub, &dlen);
1491:
1492: #ifdef DEBUG_KEXDH
1493: fprintf(stderr, "\ndh_client_pub= ");
1.128 markus 1494: BN_print_fp(stderr, dh_client_pub);
1.98 markus 1495: fprintf(stderr, "\n");
1496: debug("bits %d", BN_num_bits(dh_client_pub));
1497: #endif
1498:
1499: #ifdef DEBUG_KEXDH
1500: fprintf(stderr, "\np= ");
1.128 markus 1501: BN_print_fp(stderr, dh->p);
1.98 markus 1502: fprintf(stderr, "\ng= ");
1.128 markus 1503: bn_print(dh->g);
1.98 markus 1504: fprintf(stderr, "\npub= ");
1.128 markus 1505: BN_print_fp(stderr, dh->pub_key);
1.98 markus 1506: fprintf(stderr, "\n");
1.161 stevesk 1507: DHparams_print_fp(stderr, dh);
1.98 markus 1508: #endif
1.101 markus 1509: if (!dh_pub_is_valid(dh, dh_client_pub))
1510: packet_disconnect("bad client public DH value");
1.98 markus 1511:
1512: klen = DH_size(dh);
1513: kbuf = xmalloc(klen);
1514: kout = DH_compute_key(kbuf, dh_client_pub, dh);
1515:
1516: #ifdef DEBUG_KEXDH
1517: debug("shared secret: len %d/%d", klen, kout);
1518: fprintf(stderr, "shared secret == ");
1519: for (i = 0; i< kout; i++)
1520: fprintf(stderr, "%02x", (kbuf[i])&0xff);
1521: fprintf(stderr, "\n");
1522: #endif
1523: shared_secret = BN_new();
1524:
1525: BN_bin2bn(kbuf, kout, shared_secret);
1526: memset(kbuf, 0, klen);
1527: xfree(kbuf);
1528:
1.111 markus 1529: /* XXX precompute? */
1.134 markus 1530: key_to_blob(hostkey, &server_host_key_blob, &sbloblen);
1.98 markus 1531:
1532: /* calc H */ /* XXX depends on 'kex' */
1533: hash = kex_hash(
1534: client_version_string,
1535: server_version_string,
1536: buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1537: buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1538: (char *)server_host_key_blob, sbloblen,
1539: dh_client_pub,
1540: dh->pub_key,
1541: shared_secret
1542: );
1543: buffer_free(client_kexinit);
1544: buffer_free(server_kexinit);
1545: xfree(client_kexinit);
1546: xfree(server_kexinit);
1.156 stevesk 1547: BN_free(dh_client_pub);
1.98 markus 1548: #ifdef DEBUG_KEXDH
1.105 markus 1549: fprintf(stderr, "hash == ");
1550: for (i = 0; i< 20; i++)
1551: fprintf(stderr, "%02x", (hash[i])&0xff);
1552: fprintf(stderr, "\n");
1.98 markus 1553: #endif
1.108 markus 1554: /* save session id := H */
1555: /* XXX hashlen depends on KEX */
1556: session_id2_len = 20;
1557: session_id2 = xmalloc(session_id2_len);
1558: memcpy(session_id2, hash, session_id2_len);
1559:
1.98 markus 1560: /* sign H */
1.108 markus 1561: /* XXX hashlen depends on KEX */
1.134 markus 1562: key_sign(hostkey, &signature, &slen, hash, 20);
1.108 markus 1563:
1564: destroy_sensitive_data();
1.98 markus 1565:
1566: /* send server hostkey, DH pubkey 'f' and singed H */
1567: packet_start(SSH2_MSG_KEXDH_REPLY);
1568: packet_put_string((char *)server_host_key_blob, sbloblen);
1.114 markus 1569: packet_put_bignum2(dh->pub_key); /* f */
1.98 markus 1570: packet_put_string((char *)signature, slen);
1571: packet_send();
1.106 markus 1572: xfree(signature);
1.111 markus 1573: xfree(server_host_key_blob);
1.98 markus 1574: packet_write_wait();
1575:
1576: kex_derive_keys(kex, hash, shared_secret);
1.156 stevesk 1577: BN_clear_free(shared_secret);
1.98 markus 1578: packet_set_kex(kex);
1579:
1580: /* have keys, free DH */
1581: DH_free(dh);
1.129 provos 1582: }
1583:
1584: /* diffie-hellman-group-exchange-sha1 */
1.98 markus 1585:
1.129 provos 1586: void
1587: ssh_dhgex_server(Kex *kex, Buffer *client_kexinit, Buffer *server_kexinit)
1588: {
1.130 markus 1589: #ifdef DEBUG_KEXDH
1590: int i;
1591: #endif
1.129 provos 1592: int payload_len, dlen;
1.181 provos 1593: int slen, nbits, type, min, max;
1.140 markus 1594: u_char *signature = NULL;
1595: u_char *server_host_key_blob = NULL;
1596: u_int sbloblen;
1597: u_int klen, kout;
1598: u_char *kbuf;
1599: u_char *hash;
1.129 provos 1600: BIGNUM *shared_secret = 0;
1601: DH *dh;
1602: BIGNUM *dh_client_pub = 0;
1.134 markus 1603: Key *hostkey;
1604:
1605: hostkey = get_hostkey_by_type(kex->hostkey_type);
1606: if (hostkey == NULL)
1607: fatal("Unsupported hostkey type %d", kex->hostkey_type);
1.129 provos 1608:
1609: /* KEXDHGEX */
1610: debug("Wait SSH2_MSG_KEX_DH_GEX_REQUEST.");
1.181 provos 1611: type = packet_read(&payload_len);
1612: if (type != SSH2_MSG_KEX_DH_GEX_REQUEST_OLD &&
1613: type != SSH2_MSG_KEX_DH_GEX_REQUEST)
1614: packet_disconnect("Protocol error: expected type %d or %d, got %d",
1615: SSH2_MSG_KEX_DH_GEX_REQUEST_OLD,
1616: SSH2_MSG_KEX_DH_GEX_REQUEST,
1617: type);
1618: if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
1619: nbits = packet_get_int();
1620: min = DH_GRP_MIN;
1621: max = DH_GRP_MAX;
1622: } else {
1623: min = packet_get_int();
1624: nbits = packet_get_int();
1625: max = packet_get_int();
1626:
1627: min = MAX(DH_GRP_MIN, min);
1628: max = MIN(DH_GRP_MAX, max);
1629: }
1630:
1631: if (max < min || nbits < min || max < nbits)
1632: fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
1633: min, nbits, max);
1634:
1635: dh = choose_dh(min, nbits, max);
1636: if (dh == NULL)
1637: packet_disconnect("Protocol error: no matching DH grp found");
1.129 provos 1638:
1639: debug("Sending SSH2_MSG_KEX_DH_GEX_GROUP.");
1640: packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
1641: packet_put_bignum2(dh->p);
1642: packet_put_bignum2(dh->g);
1.98 markus 1643: packet_send();
1644: packet_write_wait();
1.139 provos 1645:
1646: /* Compute our exchange value in parallel with the client */
1647:
1.173 markus 1648: dh_gen_key(dh, kex->we_need * 8);
1.98 markus 1649:
1.129 provos 1650: debug("Wait SSH2_MSG_KEX_DH_GEX_INIT.");
1651: packet_read_expect(&payload_len, SSH2_MSG_KEX_DH_GEX_INIT);
1652:
1653: /* key, cert */
1654: dh_client_pub = BN_new();
1655: if (dh_client_pub == NULL)
1656: fatal("dh_client_pub == NULL");
1657: packet_get_bignum2(dh_client_pub, &dlen);
1658:
1659: #ifdef DEBUG_KEXDH
1660: fprintf(stderr, "\ndh_client_pub= ");
1661: BN_print_fp(stderr, dh_client_pub);
1662: fprintf(stderr, "\n");
1663: debug("bits %d", BN_num_bits(dh_client_pub));
1664: #endif
1665:
1666: #ifdef DEBUG_KEXDH
1667: fprintf(stderr, "\np= ");
1668: BN_print_fp(stderr, dh->p);
1669: fprintf(stderr, "\ng= ");
1670: bn_print(dh->g);
1671: fprintf(stderr, "\npub= ");
1672: BN_print_fp(stderr, dh->pub_key);
1673: fprintf(stderr, "\n");
1.161 stevesk 1674: DHparams_print_fp(stderr, dh);
1.129 provos 1675: #endif
1676: if (!dh_pub_is_valid(dh, dh_client_pub))
1677: packet_disconnect("bad client public DH value");
1678:
1679: klen = DH_size(dh);
1680: kbuf = xmalloc(klen);
1681: kout = DH_compute_key(kbuf, dh_client_pub, dh);
1682:
1683: #ifdef DEBUG_KEXDH
1684: debug("shared secret: len %d/%d", klen, kout);
1685: fprintf(stderr, "shared secret == ");
1686: for (i = 0; i< kout; i++)
1687: fprintf(stderr, "%02x", (kbuf[i])&0xff);
1688: fprintf(stderr, "\n");
1689: #endif
1690: shared_secret = BN_new();
1691:
1692: BN_bin2bn(kbuf, kout, shared_secret);
1693: memset(kbuf, 0, klen);
1694: xfree(kbuf);
1695:
1696: /* XXX precompute? */
1.134 markus 1697: key_to_blob(hostkey, &server_host_key_blob, &sbloblen);
1.98 markus 1698:
1.183 provos 1699: if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD) {
1700: /* These values are not included in the hash */
1701: min = -1;
1702: max = -1;
1703: }
1704:
1.129 provos 1705: /* calc H */ /* XXX depends on 'kex' */
1706: hash = kex_hash_gex(
1707: client_version_string,
1708: server_version_string,
1709: buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1710: buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1711: (char *)server_host_key_blob, sbloblen,
1.183 provos 1712: min, nbits, max,
1713: dh->p, dh->g,
1.129 provos 1714: dh_client_pub,
1715: dh->pub_key,
1716: shared_secret
1717: );
1718: buffer_free(client_kexinit);
1719: buffer_free(server_kexinit);
1720: xfree(client_kexinit);
1721: xfree(server_kexinit);
1.156 stevesk 1722: BN_free(dh_client_pub);
1.100 markus 1723: #ifdef DEBUG_KEXDH
1.129 provos 1724: fprintf(stderr, "hash == ");
1725: for (i = 0; i< 20; i++)
1726: fprintf(stderr, "%02x", (hash[i])&0xff);
1727: fprintf(stderr, "\n");
1728: #endif
1729: /* save session id := H */
1730: /* XXX hashlen depends on KEX */
1731: session_id2_len = 20;
1732: session_id2 = xmalloc(session_id2_len);
1733: memcpy(session_id2, hash, session_id2_len);
1734:
1735: /* sign H */
1736: /* XXX hashlen depends on KEX */
1.134 markus 1737: key_sign(hostkey, &signature, &slen, hash, 20);
1.129 provos 1738:
1739: destroy_sensitive_data();
1740:
1741: /* send server hostkey, DH pubkey 'f' and singed H */
1742: packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
1743: packet_put_string((char *)server_host_key_blob, sbloblen);
1744: packet_put_bignum2(dh->pub_key); /* f */
1745: packet_put_string((char *)signature, slen);
1.98 markus 1746: packet_send();
1.129 provos 1747: xfree(signature);
1748: xfree(server_host_key_blob);
1.98 markus 1749: packet_write_wait();
1.129 provos 1750:
1751: kex_derive_keys(kex, hash, shared_secret);
1.156 stevesk 1752: BN_clear_free(shared_secret);
1.129 provos 1753: packet_set_kex(kex);
1754:
1755: /* have keys, free DH */
1756: DH_free(dh);
1.1 deraadt 1757: }