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