Annotation of src/usr.bin/ssh/sshd.c, Revision 1.110
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
5: * Created: Fri Mar 17 17:09:28 1995 ylo
6: * This program is the ssh daemon. It listens for connections from clients, and
7: * performs authentication, executes use commands or shell, and forwards
8: * information to/from the application to the user client over an encrypted
9: * connection. This can also handle forwarding of X11, TCP/IP, and authentication
10: * agent connections.
1.98 markus 11: *
12: * SSH2 implementation,
13: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.65 deraadt 14: */
1.1 deraadt 15:
16: #include "includes.h"
1.110 ! markus 17: RCSID("$OpenBSD: sshd.c,v 1.109 2000/04/26 22:15:59 markus Exp $");
1.1 deraadt 18:
19: #include "xmalloc.h"
20: #include "rsa.h"
21: #include "ssh.h"
22: #include "pty.h"
23: #include "packet.h"
24: #include "cipher.h"
25: #include "mpaux.h"
26: #include "servconf.h"
27: #include "uidswap.h"
1.33 markus 28: #include "compat.h"
1.96 markus 29: #include "buffer.h"
30:
1.98 markus 31: #include "ssh2.h"
1.104 markus 32: #include <openssl/dh.h>
33: #include <openssl/bn.h>
34: #include <openssl/hmac.h>
1.98 markus 35: #include "kex.h"
1.104 markus 36: #include <openssl/dsa.h>
37: #include <openssl/rsa.h>
1.96 markus 38: #include "key.h"
1.98 markus 39: #include "dsa.h"
1.96 markus 40:
41: #include "auth.h"
1.98 markus 42: #include "myproposal.h"
1.108 markus 43: #include "authfile.h"
1.1 deraadt 44:
45: #ifdef LIBWRAP
46: #include <tcpd.h>
47: #include <syslog.h>
48: int allow_severity = LOG_INFO;
49: int deny_severity = LOG_WARNING;
50: #endif /* LIBWRAP */
51:
52: #ifndef O_NOCTTY
53: #define O_NOCTTY 0
54: #endif
55:
56: /* Server configuration options. */
57: ServerOptions options;
58:
59: /* Name of the server configuration file. */
60: char *config_file_name = SERVER_CONFIG_FILE;
61:
1.105 markus 62: /*
1.75 markus 63: * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
64: * Default value is AF_UNSPEC means both IPv4 and IPv6.
65: */
66: int IPv4or6 = AF_UNSPEC;
67:
1.65 deraadt 68: /*
69: * Debug mode flag. This can be set on the command line. If debug
70: * mode is enabled, extra debugging output will be sent to the system
71: * log, the daemon will not go to background, and will exit after processing
72: * the first connection.
73: */
1.1 deraadt 74: int debug_flag = 0;
75:
76: /* Flag indicating that the daemon is being started from inetd. */
77: int inetd_flag = 0;
78:
1.47 markus 79: /* debug goes to stderr unless inetd_flag is set */
80: int log_stderr = 0;
81:
1.1 deraadt 82: /* argv[0] without path. */
83: char *av0;
84:
85: /* Saved arguments to main(). */
86: char **saved_argv;
87:
1.66 markus 88: /*
1.75 markus 89: * The sockets that the server is listening; this is used in the SIGHUP
90: * signal handler.
1.66 markus 91: */
1.75 markus 92: #define MAX_LISTEN_SOCKS 16
93: int listen_socks[MAX_LISTEN_SOCKS];
94: int num_listen_socks = 0;
1.1 deraadt 95:
1.66 markus 96: /*
97: * the client's version string, passed by sshd2 in compat mode. if != NULL,
98: * sshd will skip the version-number exchange
99: */
1.61 markus 100: char *client_version_string = NULL;
1.96 markus 101: char *server_version_string = NULL;
1.1 deraadt 102:
1.66 markus 103: /*
104: * Any really sensitive data in the application is contained in this
105: * structure. The idea is that this structure could be locked into memory so
106: * that the pages do not get written into swap. However, there are some
107: * problems. The private key contains BIGNUMs, and we do not (in principle)
108: * have access to the internals of them, and locking just the structure is
109: * not very useful. Currently, memory locking is not implemented.
110: */
1.64 markus 111: struct {
1.108 markus 112: RSA *private_key; /* Private part of empheral server key. */
1.64 markus 113: RSA *host_key; /* Private part of host key. */
1.108 markus 114: Key *dsa_host_key; /* Private DSA host key. */
1.1 deraadt 115: } sensitive_data;
116:
1.66 markus 117: /*
118: * Flag indicating whether the current session key has been used. This flag
119: * is set whenever the key is used, and cleared when the key is regenerated.
120: */
1.1 deraadt 121: int key_used = 0;
122:
123: /* This is set to true when SIGHUP is received. */
124: int received_sighup = 0;
125:
126: /* Public side of the server key. This value is regenerated regularly with
127: the private key. */
1.2 provos 128: RSA *public_key;
1.1 deraadt 129:
1.96 markus 130: /* session identifier, used by RSA-auth */
131: unsigned char session_id[16];
132:
1.108 markus 133: /* same for ssh2 */
134: unsigned char *session_id2 = NULL;
135: int session_id2_len = 0;
136:
1.1 deraadt 137: /* Prototypes for various functions defined later in this file. */
1.96 markus 138: void do_ssh1_kex();
1.98 markus 139: void do_ssh2_kex();
1.87 markus 140:
141: /*
1.75 markus 142: * Close all listening sockets
143: */
144: void
145: close_listen_socks(void)
146: {
147: int i;
148: for (i = 0; i < num_listen_socks; i++)
149: close(listen_socks[i]);
150: num_listen_socks = -1;
151: }
152:
153: /*
1.65 deraadt 154: * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP;
155: * the effect is to reread the configuration file (and to regenerate
156: * the server key).
157: */
1.105 markus 158: void
1.64 markus 159: sighup_handler(int sig)
1.1 deraadt 160: {
1.64 markus 161: received_sighup = 1;
162: signal(SIGHUP, sighup_handler);
1.1 deraadt 163: }
164:
1.65 deraadt 165: /*
166: * Called from the main program after receiving SIGHUP.
167: * Restarts the server.
168: */
1.105 markus 169: void
1.64 markus 170: sighup_restart()
1.1 deraadt 171: {
1.64 markus 172: log("Received SIGHUP; restarting.");
1.75 markus 173: close_listen_socks();
1.64 markus 174: execv(saved_argv[0], saved_argv);
175: log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno));
176: exit(1);
1.1 deraadt 177: }
178:
1.65 deraadt 179: /*
180: * Generic signal handler for terminating signals in the master daemon.
181: * These close the listen socket; not closing it seems to cause "Address
182: * already in use" problems on some machines, which is inconvenient.
183: */
1.105 markus 184: void
1.64 markus 185: sigterm_handler(int sig)
1.1 deraadt 186: {
1.64 markus 187: log("Received signal %d; terminating.", sig);
1.75 markus 188: close_listen_socks();
1.64 markus 189: exit(255);
1.1 deraadt 190: }
191:
1.65 deraadt 192: /*
193: * SIGCHLD handler. This is called whenever a child dies. This will then
194: * reap any zombies left by exited c.
195: */
1.105 markus 196: void
1.64 markus 197: main_sigchld_handler(int sig)
1.1 deraadt 198: {
1.64 markus 199: int save_errno = errno;
200: int status;
1.60 deraadt 201:
1.64 markus 202: while (waitpid(-1, &status, WNOHANG) > 0)
203: ;
1.60 deraadt 204:
1.64 markus 205: signal(SIGCHLD, main_sigchld_handler);
206: errno = save_errno;
1.1 deraadt 207: }
208:
1.65 deraadt 209: /*
210: * Signal handler for the alarm after the login grace period has expired.
211: */
1.105 markus 212: void
1.64 markus 213: grace_alarm_handler(int sig)
1.1 deraadt 214: {
1.64 markus 215: /* Close the connection. */
216: packet_close();
217:
218: /* Log error and exit. */
219: fatal("Timeout before authentication for %s.", get_remote_ipaddr());
1.62 markus 220: }
221:
1.65 deraadt 222: /*
223: * Signal handler for the key regeneration alarm. Note that this
224: * alarm only occurs in the daemon waiting for connections, and it does not
225: * do anything with the private key or random state before forking.
226: * Thus there should be no concurrency control/asynchronous execution
227: * problems.
228: */
1.108 markus 229: /* XXX do we really want this work to be done in a signal handler ? -m */
1.105 markus 230: void
1.64 markus 231: key_regeneration_alarm(int sig)
1.1 deraadt 232: {
1.64 markus 233: int save_errno = errno;
1.18 deraadt 234:
1.64 markus 235: /* Check if we should generate a new key. */
236: if (key_used) {
237: /* This should really be done in the background. */
238: log("Generating new %d bit RSA key.", options.server_key_bits);
239:
240: if (sensitive_data.private_key != NULL)
241: RSA_free(sensitive_data.private_key);
242: sensitive_data.private_key = RSA_new();
243:
244: if (public_key != NULL)
245: RSA_free(public_key);
246: public_key = RSA_new();
247:
248: rsa_generate_key(sensitive_data.private_key, public_key,
249: options.server_key_bits);
250: arc4random_stir();
251: key_used = 0;
252: log("RSA key generation complete.");
253: }
254: /* Reschedule the alarm. */
255: signal(SIGALRM, key_regeneration_alarm);
256: alarm(options.key_regeneration_time);
257: errno = save_errno;
1.1 deraadt 258: }
259:
1.98 markus 260: char *
261: chop(char *s)
262: {
1.105 markus 263: char *t = s;
264: while (*t) {
265: if(*t == '\n' || *t == '\r') {
266: *t = '\0';
267: return s;
268: }
269: t++;
270: }
271: return s;
1.98 markus 272:
273: }
274:
1.96 markus 275: void
276: sshd_exchange_identification(int sock_in, int sock_out)
277: {
1.102 markus 278: int i, mismatch;
1.96 markus 279: int remote_major, remote_minor;
1.102 markus 280: int major, minor;
1.96 markus 281: char *s;
282: char buf[256]; /* Must not be larger than remote_version. */
283: char remote_version[256]; /* Must be at least as big as buf. */
284:
1.103 markus 285: if ((options.protocol & SSH_PROTO_1) &&
286: (options.protocol & SSH_PROTO_2)) {
1.102 markus 287: major = PROTOCOL_MAJOR_1;
288: minor = 99;
289: } else if (options.protocol & SSH_PROTO_2) {
290: major = PROTOCOL_MAJOR_2;
291: minor = PROTOCOL_MINOR_2;
292: } else {
293: major = PROTOCOL_MAJOR_1;
294: minor = PROTOCOL_MINOR_1;
295: }
296: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
1.96 markus 297: server_version_string = xstrdup(buf);
298:
299: if (client_version_string == NULL) {
300: /* Send our protocol version identification. */
301: if (atomicio(write, sock_out, server_version_string, strlen(server_version_string))
302: != strlen(server_version_string)) {
303: log("Could not write ident string to %s.", get_remote_ipaddr());
304: fatal_cleanup();
305: }
306:
307: /* Read other side\'s version identification. */
308: for (i = 0; i < sizeof(buf) - 1; i++) {
309: if (read(sock_in, &buf[i], 1) != 1) {
310: log("Did not receive ident string from %s.", get_remote_ipaddr());
311: fatal_cleanup();
312: }
313: if (buf[i] == '\r') {
314: buf[i] = '\n';
315: buf[i + 1] = 0;
316: continue;
317: }
318: if (buf[i] == '\n') {
319: /* buf[i] == '\n' */
320: buf[i + 1] = 0;
321: break;
322: }
323: }
324: buf[sizeof(buf) - 1] = 0;
325: client_version_string = xstrdup(buf);
326: }
327:
328: /*
329: * Check that the versions match. In future this might accept
330: * several versions and set appropriate flags to handle them.
331: */
332: if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
333: &remote_major, &remote_minor, remote_version) != 3) {
1.105 markus 334: s = "Protocol mismatch.\n";
1.96 markus 335: (void) atomicio(write, sock_out, s, strlen(s));
336: close(sock_in);
337: close(sock_out);
338: log("Bad protocol version identification '%.100s' from %s",
339: client_version_string, get_remote_ipaddr());
340: fatal_cleanup();
341: }
342: debug("Client protocol version %d.%d; client software version %.100s",
343: remote_major, remote_minor, remote_version);
344:
1.98 markus 345: compat_datafellows(remote_version);
346:
1.102 markus 347: mismatch = 0;
1.96 markus 348: switch(remote_major) {
349: case 1:
1.108 markus 350: if (remote_minor == 99) {
351: if (options.protocol & SSH_PROTO_2)
352: enable_compat20();
353: else
354: mismatch = 1;
355: break;
356: }
1.102 markus 357: if (!(options.protocol & SSH_PROTO_1)) {
358: mismatch = 1;
359: break;
360: }
1.96 markus 361: if (remote_minor < 3) {
362: packet_disconnect("Your ssh version is too old and"
363: "is no longer supported. Please install a newer version.");
364: } else if (remote_minor == 3) {
365: /* note that this disables agent-forwarding */
366: enable_compat13();
367: }
1.102 markus 368: break;
1.98 markus 369: case 2:
1.102 markus 370: if (options.protocol & SSH_PROTO_2) {
1.98 markus 371: enable_compat20();
372: break;
373: }
1.99 markus 374: /* FALLTHROUGH */
1.105 markus 375: default:
1.102 markus 376: mismatch = 1;
377: break;
378: }
379: chop(server_version_string);
380: chop(client_version_string);
381: debug("Local version string %.200s", server_version_string);
382:
383: if (mismatch) {
1.96 markus 384: s = "Protocol major versions differ.\n";
385: (void) atomicio(write, sock_out, s, strlen(s));
386: close(sock_in);
387: close(sock_out);
1.102 markus 388: log("Protocol major versions differ for %s: %.200s vs. %.200s",
389: get_remote_ipaddr(),
390: server_version_string, client_version_string);
1.96 markus 391: fatal_cleanup();
392: }
1.108 markus 393: if (compat20)
394: packet_set_ssh2_format();
395: }
396:
397:
398: void
399: destroy_sensitive_data(void)
400: {
401: /* Destroy the private and public keys. They will no longer be needed. */
402: RSA_free(public_key);
403: RSA_free(sensitive_data.private_key);
404: RSA_free(sensitive_data.host_key);
405: if (sensitive_data.dsa_host_key != NULL)
406: key_free(sensitive_data.dsa_host_key);
1.96 markus 407: }
408:
1.65 deraadt 409: /*
410: * Main program for the daemon.
411: */
1.2 provos 412: int
413: main(int ac, char **av)
1.1 deraadt 414: {
1.64 markus 415: extern char *optarg;
416: extern int optind;
1.107 deraadt 417: int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, on = 1;
418: pid_t pid;
1.75 markus 419: socklen_t fromlen;
1.110 ! markus 420: int silent = 0;
1.75 markus 421: fd_set *fdset;
422: struct sockaddr_storage from;
1.64 markus 423: const char *remote_ip;
424: int remote_port;
425: FILE *f;
426: struct linger linger;
1.75 markus 427: struct addrinfo *ai;
428: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
429: int listen_sock, maxfd;
1.64 markus 430:
431: /* Save argv[0]. */
432: saved_argv = av;
433: if (strchr(av[0], '/'))
434: av0 = strrchr(av[0], '/') + 1;
435: else
436: av0 = av[0];
437:
438: /* Initialize configuration options to their default values. */
439: initialize_server_options(&options);
440:
441: /* Parse command-line arguments. */
1.102 markus 442: while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) {
1.64 markus 443: switch (opt) {
1.75 markus 444: case '4':
445: IPv4or6 = AF_INET;
446: break;
447: case '6':
448: IPv4or6 = AF_INET6;
449: break;
1.64 markus 450: case 'f':
451: config_file_name = optarg;
452: break;
453: case 'd':
454: debug_flag = 1;
455: options.log_level = SYSLOG_LEVEL_DEBUG;
456: break;
457: case 'i':
458: inetd_flag = 1;
459: break;
460: case 'Q':
1.110 ! markus 461: silent = 1;
1.64 markus 462: break;
463: case 'q':
464: options.log_level = SYSLOG_LEVEL_QUIET;
465: break;
466: case 'b':
467: options.server_key_bits = atoi(optarg);
468: break;
469: case 'p':
1.75 markus 470: options.ports_from_cmdline = 1;
471: if (options.num_ports >= MAX_PORTS)
472: fatal("too many ports.\n");
473: options.ports[options.num_ports++] = atoi(optarg);
1.64 markus 474: break;
475: case 'g':
476: options.login_grace_time = atoi(optarg);
477: break;
478: case 'k':
479: options.key_regeneration_time = atoi(optarg);
480: break;
481: case 'h':
482: options.host_key_file = optarg;
483: break;
484: case 'V':
485: client_version_string = optarg;
486: /* only makes sense with inetd_flag, i.e. no listen() */
487: inetd_flag = 1;
488: break;
489: case '?':
490: default:
491: fprintf(stderr, "sshd version %s\n", SSH_VERSION);
492: fprintf(stderr, "Usage: %s [options]\n", av0);
493: fprintf(stderr, "Options:\n");
1.66 markus 494: fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE);
1.64 markus 495: fprintf(stderr, " -d Debugging mode\n");
496: fprintf(stderr, " -i Started from inetd\n");
497: fprintf(stderr, " -q Quiet (no logging)\n");
498: fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
499: fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
500: fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n");
501: fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
502: fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
1.75 markus 503: HOST_KEY_FILE);
504: fprintf(stderr, " -4 Use IPv4 only\n");
505: fprintf(stderr, " -6 Use IPv6 only\n");
1.64 markus 506: exit(1);
507: }
508: }
509:
1.75 markus 510: /*
511: * Force logging to stderr until we have loaded the private host
512: * key (unless started from inetd)
513: */
514: log_init(av0,
515: options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
516: options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
1.110 ! markus 517: !silent && !inetd_flag);
1.75 markus 518:
1.64 markus 519: /* Read server configuration options from the configuration file. */
520: read_server_config(&options, config_file_name);
521:
522: /* Fill in default values for those options not explicitly set. */
523: fill_default_server_options(&options);
524:
525: /* Check that there are no remaining arguments. */
526: if (optind < ac) {
527: fprintf(stderr, "Extra argument %s.\n", av[optind]);
528: exit(1);
529: }
530:
531: debug("sshd version %.100s", SSH_VERSION);
532:
1.108 markus 533: sensitive_data.dsa_host_key = NULL;
534: sensitive_data.host_key = NULL;
535:
536: /* check if RSA support exists */
537: if ((options.protocol & SSH_PROTO_1) &&
538: rsa_alive() == 0) {
539: log("no RSA support in libssl and libcrypto. See ssl(8)");
540: log("Disabling protocol version 1");
541: options.protocol &= ~SSH_PROTO_1;
542: }
543: /* Load the RSA/DSA host key. It must have empty passphrase. */
544: if (options.protocol & SSH_PROTO_1) {
545: Key k;
546: sensitive_data.host_key = RSA_new();
547: k.type = KEY_RSA;
548: k.rsa = sensitive_data.host_key;
549: errno = 0;
1.109 markus 550: if (!load_private_key(options.host_key_file, "", &k, NULL)) {
1.108 markus 551: error("Could not load host key: %.200s: %.100s",
552: options.host_key_file, strerror(errno));
553: log("Disabling protocol version 1");
554: options.protocol &= ~SSH_PROTO_1;
555: }
556: k.rsa = NULL;
557: }
558: if (options.protocol & SSH_PROTO_2) {
559: sensitive_data.dsa_host_key = key_new(KEY_DSA);
560: if (!load_private_key(options.dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) {
561: error("Could not load DSA host key: %.200s", options.dsa_key_file);
562: log("Disabling protocol version 2");
563: options.protocol &= ~SSH_PROTO_2;
564: }
565: }
566: if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
1.110 ! markus 567: if (silent == 0)
! 568: fprintf(stderr, "sshd: no hostkeys available -- exiting.\n");
1.108 markus 569: log("sshd: no hostkeys available -- exiting.\n");
1.64 markus 570: exit(1);
571: }
572:
1.108 markus 573: /* Check certain values for sanity. */
574: if (options.protocol & SSH_PROTO_1) {
575: if (options.server_key_bits < 512 ||
576: options.server_key_bits > 32768) {
577: fprintf(stderr, "Bad server key size.\n");
578: exit(1);
579: }
580: /*
581: * Check that server and host key lengths differ sufficiently. This
582: * is necessary to make double encryption work with rsaref. Oh, I
583: * hate software patents. I dont know if this can go? Niels
584: */
585: if (options.server_key_bits >
586: BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED &&
587: options.server_key_bits <
588: BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
589: options.server_key_bits =
590: BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED;
591: debug("Forcing server key to %d bits to make it differ from host key.",
592: options.server_key_bits);
593: }
594: }
595:
596: /* Initialize the log (it is reinitialized below in case we forked). */
1.64 markus 597: if (debug_flag && !inetd_flag)
598: log_stderr = 1;
599: log_init(av0, options.log_level, options.log_facility, log_stderr);
600:
1.108 markus 601: /*
602: * If not in debugging mode, and not started from inetd, disconnect
603: * from the controlling terminal, and fork. The original process
604: * exits.
605: */
1.64 markus 606: if (!debug_flag && !inetd_flag) {
1.1 deraadt 607: #ifdef TIOCNOTTY
1.64 markus 608: int fd;
1.1 deraadt 609: #endif /* TIOCNOTTY */
1.64 markus 610: if (daemon(0, 0) < 0)
611: fatal("daemon() failed: %.200s", strerror(errno));
612:
613: /* Disconnect from the controlling tty. */
1.1 deraadt 614: #ifdef TIOCNOTTY
1.64 markus 615: fd = open("/dev/tty", O_RDWR | O_NOCTTY);
616: if (fd >= 0) {
617: (void) ioctl(fd, TIOCNOTTY, NULL);
618: close(fd);
619: }
620: #endif /* TIOCNOTTY */
621: }
622: /* Reinitialize the log (because of the fork above). */
623: log_init(av0, options.log_level, options.log_facility, log_stderr);
624:
625: /* Do not display messages to stdout in RSA code. */
626: rsa_set_verbose(0);
627:
628: /* Initialize the random number generator. */
629: arc4random_stir();
630:
631: /* Chdir to the root directory so that the current disk can be
632: unmounted if desired. */
633: chdir("/");
634:
635: /* Start listening for a socket, unless started from inetd. */
636: if (inetd_flag) {
637: int s1, s2;
638: s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
639: s2 = dup(s1);
640: sock_in = dup(0);
641: sock_out = dup(1);
1.108 markus 642: /*
643: * We intentionally do not close the descriptors 0, 1, and 2
644: * as our code for setting the descriptors won\'t work if
645: * ttyfd happens to be one of those.
646: */
1.64 markus 647: debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
648:
1.108 markus 649: if (options.protocol & SSH_PROTO_1) {
650: public_key = RSA_new();
651: sensitive_data.private_key = RSA_new();
652: log("Generating %d bit RSA key.", options.server_key_bits);
653: rsa_generate_key(sensitive_data.private_key, public_key,
654: options.server_key_bits);
655: arc4random_stir();
656: log("RSA key generation complete.");
657: }
1.64 markus 658: } else {
1.75 markus 659: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
660: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
661: continue;
662: if (num_listen_socks >= MAX_LISTEN_SOCKS)
663: fatal("Too many listen sockets. "
664: "Enlarge MAX_LISTEN_SOCKS");
665: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
666: ntop, sizeof(ntop), strport, sizeof(strport),
667: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
668: error("getnameinfo failed");
669: continue;
670: }
671: /* Create socket for listening. */
672: listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
673: if (listen_sock < 0) {
674: /* kernel may not support ipv6 */
675: verbose("socket: %.100s", strerror(errno));
676: continue;
677: }
678: if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
679: error("listen_sock O_NONBLOCK: %s", strerror(errno));
680: close(listen_sock);
681: continue;
682: }
683: /*
684: * Set socket options. We try to make the port
685: * reusable and have it close as fast as possible
686: * without waiting in unnecessary wait states on
687: * close.
688: */
689: setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
690: (void *) &on, sizeof(on));
691: linger.l_onoff = 1;
692: linger.l_linger = 5;
693: setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
694: (void *) &linger, sizeof(linger));
695:
696: debug("Bind to port %s on %s.", strport, ntop);
697:
698: /* Bind the socket to the desired port. */
699: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
700: error("Bind to port %s on %s failed: %.200s.",
701: strport, ntop, strerror(errno));
702: close(listen_sock);
703: continue;
704: }
705: listen_socks[num_listen_socks] = listen_sock;
706: num_listen_socks++;
707:
708: /* Start listening on the port. */
709: log("Server listening on %s port %s.", ntop, strport);
710: if (listen(listen_sock, 5) < 0)
711: fatal("listen: %.100s", strerror(errno));
712:
1.64 markus 713: }
1.75 markus 714: freeaddrinfo(options.listen_addrs);
715:
716: if (!num_listen_socks)
717: fatal("Cannot bind any address.");
718:
1.64 markus 719: if (!debug_flag) {
1.66 markus 720: /*
721: * Record our pid in /etc/sshd_pid to make it easier
722: * to kill the correct sshd. We don\'t want to do
723: * this before the bind above because the bind will
724: * fail if there already is a daemon, and this will
725: * overwrite any old pid in the file.
726: */
1.64 markus 727: f = fopen(SSH_DAEMON_PID_FILE, "w");
728: if (f) {
729: fprintf(f, "%u\n", (unsigned int) getpid());
730: fclose(f);
731: }
732: }
1.108 markus 733: if (options.protocol & SSH_PROTO_1) {
734: public_key = RSA_new();
735: sensitive_data.private_key = RSA_new();
736:
737: log("Generating %d bit RSA key.", options.server_key_bits);
738: rsa_generate_key(sensitive_data.private_key, public_key,
739: options.server_key_bits);
740: arc4random_stir();
741: log("RSA key generation complete.");
1.64 markus 742:
1.108 markus 743: /* Schedule server key regeneration alarm. */
744: signal(SIGALRM, key_regeneration_alarm);
745: alarm(options.key_regeneration_time);
746: }
1.64 markus 747:
748: /* Arrange to restart on SIGHUP. The handler needs listen_sock. */
749: signal(SIGHUP, sighup_handler);
750: signal(SIGTERM, sigterm_handler);
751: signal(SIGQUIT, sigterm_handler);
752:
753: /* Arrange SIGCHLD to be caught. */
754: signal(SIGCHLD, main_sigchld_handler);
755:
1.75 markus 756: /* setup fd set for listen */
757: maxfd = 0;
758: for (i = 0; i < num_listen_socks; i++)
759: if (listen_socks[i] > maxfd)
760: maxfd = listen_socks[i];
1.105 markus 761: fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
762: fdset = (fd_set *)xmalloc(fdsetsz);
1.75 markus 763:
1.66 markus 764: /*
765: * Stay listening for connections until the system crashes or
766: * the daemon is killed with a signal.
767: */
1.64 markus 768: for (;;) {
769: if (received_sighup)
770: sighup_restart();
1.75 markus 771: /* Wait in select until there is a connection. */
772: memset(fdset, 0, fdsetsz);
773: for (i = 0; i < num_listen_socks; i++)
774: FD_SET(listen_socks[i], fdset);
775: if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) {
776: if (errno != EINTR)
777: error("select: %.100s", strerror(errno));
778: continue;
779: }
780: for (i = 0; i < num_listen_socks; i++) {
781: if (!FD_ISSET(listen_socks[i], fdset))
1.70 provos 782: continue;
1.75 markus 783: fromlen = sizeof(from);
784: newsock = accept(listen_socks[i], (struct sockaddr *)&from,
785: &fromlen);
786: if (newsock < 0) {
787: if (errno != EINTR && errno != EWOULDBLOCK)
788: error("accept: %.100s", strerror(errno));
789: continue;
1.70 provos 790: }
1.75 markus 791: if (fcntl(newsock, F_SETFL, 0) < 0) {
792: error("newsock del O_NONBLOCK: %s", strerror(errno));
1.64 markus 793: continue;
794: }
1.66 markus 795: /*
796: * Got connection. Fork a child to handle it, unless
797: * we are in debugging mode.
798: */
1.64 markus 799: if (debug_flag) {
1.66 markus 800: /*
801: * In debugging mode. Close the listening
802: * socket, and start processing the
803: * connection without forking.
804: */
1.64 markus 805: debug("Server will not fork when running in debugging mode.");
1.75 markus 806: close_listen_socks();
1.64 markus 807: sock_in = newsock;
808: sock_out = newsock;
809: pid = getpid();
810: break;
811: } else {
1.66 markus 812: /*
813: * Normal production daemon. Fork, and have
814: * the child process the connection. The
815: * parent continues listening.
816: */
1.64 markus 817: if ((pid = fork()) == 0) {
1.66 markus 818: /*
819: * Child. Close the listening socket, and start using the
820: * accepted socket. Reinitialize logging (since our pid has
821: * changed). We break out of the loop to handle the connection.
822: */
1.75 markus 823: close_listen_socks();
1.64 markus 824: sock_in = newsock;
825: sock_out = newsock;
826: log_init(av0, options.log_level, options.log_facility, log_stderr);
827: break;
828: }
829: }
830:
831: /* Parent. Stay in the loop. */
832: if (pid < 0)
833: error("fork: %.100s", strerror(errno));
834: else
835: debug("Forked child %d.", pid);
1.1 deraadt 836:
1.64 markus 837: /* Mark that the key has been used (it was "given" to the child). */
838: key_used = 1;
1.1 deraadt 839:
1.64 markus 840: arc4random_stir();
1.1 deraadt 841:
1.64 markus 842: /* Close the new socket (the child is now taking care of it). */
843: close(newsock);
1.75 markus 844: } /* for (i = 0; i < num_listen_socks; i++) */
845: /* child process check (or debug mode) */
846: if (num_listen_socks < 0)
847: break;
1.64 markus 848: }
1.1 deraadt 849: }
850:
1.64 markus 851: /* This is the child processing a new connection. */
852:
1.66 markus 853: /*
854: * Disable the key regeneration alarm. We will not regenerate the
855: * key since we are no longer in a position to give it to anyone. We
856: * will not restart on SIGHUP since it no longer makes sense.
857: */
1.64 markus 858: alarm(0);
859: signal(SIGALRM, SIG_DFL);
860: signal(SIGHUP, SIG_DFL);
861: signal(SIGTERM, SIG_DFL);
862: signal(SIGQUIT, SIG_DFL);
863: signal(SIGCHLD, SIG_DFL);
864:
1.66 markus 865: /*
866: * Set socket options for the connection. We want the socket to
867: * close as fast as possible without waiting for anything. If the
868: * connection is not a socket, these will do nothing.
869: */
870: /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
1.64 markus 871: linger.l_onoff = 1;
872: linger.l_linger = 5;
873: setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
874:
1.66 markus 875: /*
876: * Register our connection. This turns encryption off because we do
877: * not have a key.
878: */
1.64 markus 879: packet_set_connection(sock_in, sock_out);
1.1 deraadt 880:
1.64 markus 881: remote_port = get_remote_port();
882: remote_ip = get_remote_ipaddr();
1.52 markus 883:
1.64 markus 884: /* Check whether logins are denied from this host. */
1.37 dugsong 885: #ifdef LIBWRAP
1.75 markus 886: /* XXX LIBWRAP noes not know about IPv6 */
1.64 markus 887: {
888: struct request_info req;
1.37 dugsong 889:
1.64 markus 890: request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL);
891: fromhost(&req);
1.37 dugsong 892:
1.64 markus 893: if (!hosts_access(&req)) {
894: close(sock_in);
895: close(sock_out);
896: refuse(&req);
897: }
1.75 markus 898: /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */
1.64 markus 899: }
1.75 markus 900: #endif /* LIBWRAP */
1.64 markus 901: /* Log the connection. */
902: verbose("Connection from %.500s port %d", remote_ip, remote_port);
1.1 deraadt 903:
1.66 markus 904: /*
905: * We don\'t want to listen forever unless the other side
906: * successfully authenticates itself. So we set up an alarm which is
907: * cleared after successful authentication. A limit of zero
908: * indicates no limit. Note that we don\'t set the alarm in debugging
909: * mode; it is just annoying to have the server exit just when you
910: * are about to discover the bug.
911: */
1.64 markus 912: signal(SIGALRM, grace_alarm_handler);
913: if (!debug_flag)
914: alarm(options.login_grace_time);
915:
1.96 markus 916: sshd_exchange_identification(sock_in, sock_out);
1.66 markus 917: /*
918: * Check that the connection comes from a privileged port. Rhosts-
919: * and Rhosts-RSA-Authentication only make sense from priviledged
920: * programs. Of course, if the intruder has root access on his local
921: * machine, he can connect from any port. So do not use these
922: * authentication methods from machines that you do not trust.
923: */
1.64 markus 924: if (remote_port >= IPPORT_RESERVED ||
925: remote_port < IPPORT_RESERVED / 2) {
926: options.rhosts_authentication = 0;
927: options.rhosts_rsa_authentication = 0;
928: }
1.76 markus 929: #ifdef KRB4
930: if (!packet_connection_is_ipv4() &&
931: options.kerberos_authentication) {
932: debug("Kerberos Authentication disabled, only available for IPv4.");
933: options.kerberos_authentication = 0;
934: }
935: #endif /* KRB4 */
936:
1.64 markus 937: packet_set_nonblocking();
1.1 deraadt 938:
1.77 markus 939: /* perform the key exchange */
940: /* authenticate user and start session */
1.98 markus 941: if (compat20) {
942: do_ssh2_kex();
943: do_authentication2();
944: } else {
945: do_ssh1_kex();
946: do_authentication();
947: }
1.1 deraadt 948:
949: #ifdef KRB4
1.64 markus 950: /* Cleanup user's ticket cache file. */
951: if (options.kerberos_ticket_cleanup)
952: (void) dest_tkt();
1.1 deraadt 953: #endif /* KRB4 */
954:
1.64 markus 955: /* The connection has been terminated. */
956: verbose("Closing connection to %.100s", remote_ip);
957: packet_close();
958: exit(0);
1.1 deraadt 959: }
960:
1.65 deraadt 961: /*
1.77 markus 962: * SSH1 key exchange
1.65 deraadt 963: */
1.52 markus 964: void
1.96 markus 965: do_ssh1_kex()
1.1 deraadt 966: {
1.64 markus 967: int i, len;
1.77 markus 968: int plen, slen;
1.64 markus 969: BIGNUM *session_key_int;
970: unsigned char session_key[SSH_SESSION_KEY_LENGTH];
1.77 markus 971: unsigned char cookie[8];
1.64 markus 972: unsigned int cipher_type, auth_mask, protocol_flags;
973: u_int32_t rand = 0;
974:
1.66 markus 975: /*
976: * Generate check bytes that the client must send back in the user
977: * packet in order for it to be accepted; this is used to defy ip
978: * spoofing attacks. Note that this only works against somebody
979: * doing IP spoofing from a remote machine; any machine on the local
980: * network can still see outgoing packets and catch the random
981: * cookie. This only affects rhosts authentication, and this is one
982: * of the reasons why it is inherently insecure.
983: */
1.64 markus 984: for (i = 0; i < 8; i++) {
985: if (i % 4 == 0)
986: rand = arc4random();
1.77 markus 987: cookie[i] = rand & 0xff;
1.64 markus 988: rand >>= 8;
989: }
990:
1.66 markus 991: /*
992: * Send our public key. We include in the packet 64 bits of random
993: * data that must be matched in the reply in order to prevent IP
994: * spoofing.
995: */
1.64 markus 996: packet_start(SSH_SMSG_PUBLIC_KEY);
997: for (i = 0; i < 8; i++)
1.77 markus 998: packet_put_char(cookie[i]);
1.64 markus 999:
1000: /* Store our public server RSA key. */
1001: packet_put_int(BN_num_bits(public_key->n));
1002: packet_put_bignum(public_key->e);
1003: packet_put_bignum(public_key->n);
1004:
1005: /* Store our public host RSA key. */
1006: packet_put_int(BN_num_bits(sensitive_data.host_key->n));
1007: packet_put_bignum(sensitive_data.host_key->e);
1008: packet_put_bignum(sensitive_data.host_key->n);
1009:
1010: /* Put protocol flags. */
1011: packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1012:
1013: /* Declare which ciphers we support. */
1.97 markus 1014: packet_put_int(cipher_mask1());
1.64 markus 1015:
1016: /* Declare supported authentication types. */
1017: auth_mask = 0;
1018: if (options.rhosts_authentication)
1019: auth_mask |= 1 << SSH_AUTH_RHOSTS;
1020: if (options.rhosts_rsa_authentication)
1021: auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1022: if (options.rsa_authentication)
1023: auth_mask |= 1 << SSH_AUTH_RSA;
1.1 deraadt 1024: #ifdef KRB4
1.64 markus 1025: if (options.kerberos_authentication)
1026: auth_mask |= 1 << SSH_AUTH_KERBEROS;
1.1 deraadt 1027: #endif
1.5 dugsong 1028: #ifdef AFS
1.64 markus 1029: if (options.kerberos_tgt_passing)
1030: auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1031: if (options.afs_token_passing)
1032: auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
1.1 deraadt 1033: #endif
1.63 markus 1034: #ifdef SKEY
1.64 markus 1035: if (options.skey_authentication == 1)
1036: auth_mask |= 1 << SSH_AUTH_TIS;
1.63 markus 1037: #endif
1.64 markus 1038: if (options.password_authentication)
1039: auth_mask |= 1 << SSH_AUTH_PASSWORD;
1040: packet_put_int(auth_mask);
1041:
1042: /* Send the packet and wait for it to be sent. */
1043: packet_send();
1044: packet_write_wait();
1045:
1046: debug("Sent %d bit public key and %d bit host key.",
1047: BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n));
1048:
1049: /* Read clients reply (cipher type and session key). */
1050: packet_read_expect(&plen, SSH_CMSG_SESSION_KEY);
1051:
1.69 markus 1052: /* Get cipher type and check whether we accept this. */
1.64 markus 1053: cipher_type = packet_get_char();
1.69 markus 1054:
1.105 markus 1055: if (!(cipher_mask() & (1 << cipher_type)))
1.69 markus 1056: packet_disconnect("Warning: client selects unsupported cipher.");
1.64 markus 1057:
1058: /* Get check bytes from the packet. These must match those we
1059: sent earlier with the public key packet. */
1060: for (i = 0; i < 8; i++)
1.77 markus 1061: if (cookie[i] != packet_get_char())
1.64 markus 1062: packet_disconnect("IP Spoofing check bytes do not match.");
1063:
1064: debug("Encryption type: %.200s", cipher_name(cipher_type));
1065:
1066: /* Get the encrypted integer. */
1067: session_key_int = BN_new();
1068: packet_get_bignum(session_key_int, &slen);
1069:
1070: protocol_flags = packet_get_int();
1071: packet_set_protocol_flags(protocol_flags);
1072:
1073: packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY);
1074:
1.66 markus 1075: /*
1076: * Decrypt it using our private server key and private host key (key
1077: * with larger modulus first).
1078: */
1.64 markus 1079: if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) {
1080: /* Private key has bigger modulus. */
1081: if (BN_num_bits(sensitive_data.private_key->n) <
1082: BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) {
1083: fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1084: get_remote_ipaddr(),
1085: BN_num_bits(sensitive_data.private_key->n),
1086: BN_num_bits(sensitive_data.host_key->n),
1087: SSH_KEY_BITS_RESERVED);
1088: }
1089: rsa_private_decrypt(session_key_int, session_key_int,
1090: sensitive_data.private_key);
1091: rsa_private_decrypt(session_key_int, session_key_int,
1092: sensitive_data.host_key);
1093: } else {
1094: /* Host key has bigger modulus (or they are equal). */
1095: if (BN_num_bits(sensitive_data.host_key->n) <
1096: BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) {
1097: fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d",
1098: get_remote_ipaddr(),
1099: BN_num_bits(sensitive_data.host_key->n),
1100: BN_num_bits(sensitive_data.private_key->n),
1101: SSH_KEY_BITS_RESERVED);
1102: }
1103: rsa_private_decrypt(session_key_int, session_key_int,
1104: sensitive_data.host_key);
1105: rsa_private_decrypt(session_key_int, session_key_int,
1106: sensitive_data.private_key);
1107: }
1108:
1.77 markus 1109: compute_session_id(session_id, cookie,
1.64 markus 1110: sensitive_data.host_key->n,
1111: sensitive_data.private_key->n);
1112:
1.77 markus 1113: /* Destroy the private and public keys. They will no longer be needed. */
1.108 markus 1114: destroy_sensitive_data();
1.77 markus 1115:
1.66 markus 1116: /*
1117: * Extract session key from the decrypted integer. The key is in the
1118: * least significant 256 bits of the integer; the first byte of the
1119: * key is in the highest bits.
1120: */
1.64 markus 1121: BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1122: len = BN_num_bytes(session_key_int);
1123: if (len < 0 || len > sizeof(session_key))
1124: fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d",
1125: get_remote_ipaddr(),
1126: len, sizeof(session_key));
1127: memset(session_key, 0, sizeof(session_key));
1128: BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len);
1129:
1.77 markus 1130: /* Destroy the decrypted integer. It is no longer needed. */
1131: BN_clear_free(session_key_int);
1132:
1.64 markus 1133: /* Xor the first 16 bytes of the session key with the session id. */
1134: for (i = 0; i < 16; i++)
1135: session_key[i] ^= session_id[i];
1136:
1137: /* Set the session key. From this on all communications will be encrypted. */
1138: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
1139:
1140: /* Destroy our copy of the session key. It is no longer needed. */
1141: memset(session_key, 0, sizeof(session_key));
1142:
1143: debug("Received session key; encryption turned on.");
1144:
1145: /* Send an acknowledgement packet. Note that this packet is sent encrypted. */
1146: packet_start(SSH_SMSG_SUCCESS);
1147: packet_send();
1148: packet_write_wait();
1.98 markus 1149: }
1150:
1151: /*
1152: * SSH2 key exchange: diffie-hellman-group1-sha1
1153: */
1154: void
1155: do_ssh2_kex()
1156: {
1157: Buffer *server_kexinit;
1158: Buffer *client_kexinit;
1159: int payload_len, dlen;
1160: int slen;
1161: unsigned int klen, kout;
1162: char *ptr;
1163: unsigned char *signature = NULL;
1164: unsigned char *server_host_key_blob = NULL;
1165: unsigned int sbloblen;
1166: DH *dh;
1167: BIGNUM *dh_client_pub = 0;
1168: BIGNUM *shared_secret = 0;
1169: int i;
1170: unsigned char *kbuf;
1171: unsigned char *hash;
1172: Kex *kex;
1173: char *cprop[PROPOSAL_MAX];
1174: char *sprop[PROPOSAL_MAX];
1175:
1176: /* KEXINIT */
1.102 markus 1177:
1178: if (options.ciphers != NULL) {
1.105 markus 1179: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102 markus 1180: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1181: }
1.98 markus 1182:
1183: debug("Sending KEX init.");
1184:
1185: for (i = 0; i < PROPOSAL_MAX; i++)
1186: sprop[i] = xstrdup(myproposal[i]);
1187: server_kexinit = kex_init(sprop);
1188: packet_start(SSH2_MSG_KEXINIT);
1189: packet_put_raw(buffer_ptr(server_kexinit), buffer_len(server_kexinit));
1190: packet_send();
1191: packet_write_wait();
1192:
1193: debug("done");
1194:
1195: packet_read_expect(&payload_len, SSH2_MSG_KEXINIT);
1196:
1197: /*
1198: * save raw KEXINIT payload in buffer. this is used during
1199: * computation of the session_id and the session keys.
1200: */
1201: client_kexinit = xmalloc(sizeof(*client_kexinit));
1202: buffer_init(client_kexinit);
1203: ptr = packet_get_raw(&payload_len);
1204: buffer_append(client_kexinit, ptr, payload_len);
1205:
1206: /* skip cookie */
1207: for (i = 0; i < 16; i++)
1208: (void) packet_get_char();
1209: /* save kex init proposal strings */
1210: for (i = 0; i < PROPOSAL_MAX; i++) {
1211: cprop[i] = packet_get_string(NULL);
1212: debug("got kexinit string: %s", cprop[i]);
1213: }
1214:
1215: i = (int) packet_get_char();
1216: debug("first kex follow == %d", i);
1217: i = packet_get_int();
1218: debug("reserved == %d", i);
1219:
1220: debug("done read kexinit");
1221: kex = kex_choose_conf(cprop, sprop, 1);
1222:
1223: /* KEXDH */
1224:
1225: debug("Wait SSH2_MSG_KEXDH_INIT.");
1226: packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT);
1227:
1228: /* key, cert */
1229: dh_client_pub = BN_new();
1230: if (dh_client_pub == NULL)
1231: fatal("dh_client_pub == NULL");
1232: packet_get_bignum2(dh_client_pub, &dlen);
1233:
1234: #ifdef DEBUG_KEXDH
1235: fprintf(stderr, "\ndh_client_pub= ");
1236: bignum_print(dh_client_pub);
1237: fprintf(stderr, "\n");
1238: debug("bits %d", BN_num_bits(dh_client_pub));
1239: #endif
1240:
1241: /* generate DH key */
1.101 markus 1242: dh = dh_new_group1(); /* XXX depends on 'kex' */
1.98 markus 1243:
1244: #ifdef DEBUG_KEXDH
1245: fprintf(stderr, "\np= ");
1246: bignum_print(dh->p);
1247: fprintf(stderr, "\ng= ");
1248: bignum_print(dh->g);
1249: fprintf(stderr, "\npub= ");
1250: bignum_print(dh->pub_key);
1251: fprintf(stderr, "\n");
1252: #endif
1.101 markus 1253: if (!dh_pub_is_valid(dh, dh_client_pub))
1254: packet_disconnect("bad client public DH value");
1.98 markus 1255:
1256: klen = DH_size(dh);
1257: kbuf = xmalloc(klen);
1258: kout = DH_compute_key(kbuf, dh_client_pub, dh);
1259:
1260: #ifdef DEBUG_KEXDH
1261: debug("shared secret: len %d/%d", klen, kout);
1262: fprintf(stderr, "shared secret == ");
1263: for (i = 0; i< kout; i++)
1264: fprintf(stderr, "%02x", (kbuf[i])&0xff);
1265: fprintf(stderr, "\n");
1266: #endif
1267: shared_secret = BN_new();
1268:
1269: BN_bin2bn(kbuf, kout, shared_secret);
1270: memset(kbuf, 0, klen);
1271: xfree(kbuf);
1272:
1.108 markus 1273: dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen);
1.98 markus 1274:
1275: /* calc H */ /* XXX depends on 'kex' */
1276: hash = kex_hash(
1277: client_version_string,
1278: server_version_string,
1279: buffer_ptr(client_kexinit), buffer_len(client_kexinit),
1280: buffer_ptr(server_kexinit), buffer_len(server_kexinit),
1281: (char *)server_host_key_blob, sbloblen,
1282: dh_client_pub,
1283: dh->pub_key,
1284: shared_secret
1285: );
1286: buffer_free(client_kexinit);
1287: buffer_free(server_kexinit);
1288: xfree(client_kexinit);
1289: xfree(server_kexinit);
1290: #ifdef DEBUG_KEXDH
1.105 markus 1291: fprintf(stderr, "hash == ");
1292: for (i = 0; i< 20; i++)
1293: fprintf(stderr, "%02x", (hash[i])&0xff);
1294: fprintf(stderr, "\n");
1.98 markus 1295: #endif
1.108 markus 1296: /* save session id := H */
1297: /* XXX hashlen depends on KEX */
1298: session_id2_len = 20;
1299: session_id2 = xmalloc(session_id2_len);
1300: memcpy(session_id2, hash, session_id2_len);
1301:
1.98 markus 1302: /* sign H */
1.108 markus 1303: /* XXX hashlen depends on KEX */
1304: dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20);
1305:
1306: destroy_sensitive_data();
1.98 markus 1307:
1308: /* send server hostkey, DH pubkey 'f' and singed H */
1309: packet_start(SSH2_MSG_KEXDH_REPLY);
1310: packet_put_string((char *)server_host_key_blob, sbloblen);
1311: packet_put_bignum2(dh->pub_key); // f
1312: packet_put_string((char *)signature, slen);
1313: packet_send();
1.106 markus 1314: xfree(signature);
1.98 markus 1315: packet_write_wait();
1316:
1317: kex_derive_keys(kex, hash, shared_secret);
1318: packet_set_kex(kex);
1319:
1320: /* have keys, free DH */
1321: DH_free(dh);
1322:
1323: debug("send SSH2_MSG_NEWKEYS.");
1324: packet_start(SSH2_MSG_NEWKEYS);
1325: packet_send();
1326: packet_write_wait();
1327: debug("done: send SSH2_MSG_NEWKEYS.");
1328:
1329: debug("Wait SSH2_MSG_NEWKEYS.");
1330: packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS);
1331: debug("GOT SSH2_MSG_NEWKEYS.");
1332:
1.100 markus 1333: #ifdef DEBUG_KEXDH
1.98 markus 1334: /* send 1st encrypted/maced/compressed message */
1335: packet_start(SSH2_MSG_IGNORE);
1336: packet_put_cstring("markus");
1337: packet_send();
1338: packet_write_wait();
1.100 markus 1339: #endif
1.98 markus 1340: debug("done: KEX2.");
1.1 deraadt 1341: }