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