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