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