]>
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" | |
0fbe8c74 | 17 | RCSID("$OpenBSD: sshd.c,v 1.113 2000/05/01 20:34:51 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 | |
e78a59f5 | 265 | char * |
266 | chop(char *s) | |
267 | { | |
6ae2364d | 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; | |
e78a59f5 | 277 | |
278 | } | |
279 | ||
7368a6c8 | 280 | void |
281 | sshd_exchange_identification(int sock_in, int sock_out) | |
282 | { | |
a8be9f80 | 283 | int i, mismatch; |
7368a6c8 | 284 | int remote_major, remote_minor; |
a8be9f80 | 285 | int major, minor; |
7368a6c8 | 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 | ||
a8be9f80 | 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); | |
7368a6c8 | 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; | |
7368a6c8 | 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) { | |
6ae2364d | 339 | s = "Protocol mismatch.\n"; |
7368a6c8 | 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 | ||
e78a59f5 | 350 | compat_datafellows(remote_version); |
351 | ||
a8be9f80 | 352 | mismatch = 0; |
7368a6c8 | 353 | switch(remote_major) { |
354 | case 1: | |
a306f2dd | 355 | if (remote_minor == 99) { |
356 | if (options.protocol & SSH_PROTO_2) | |
357 | enable_compat20(); | |
358 | else | |
359 | mismatch = 1; | |
360 | break; | |
361 | } | |
a8be9f80 | 362 | if (!(options.protocol & SSH_PROTO_1)) { |
363 | mismatch = 1; | |
364 | break; | |
365 | } | |
7368a6c8 | 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 | } | |
a8be9f80 | 373 | break; |
e78a59f5 | 374 | case 2: |
a8be9f80 | 375 | if (options.protocol & SSH_PROTO_2) { |
e78a59f5 | 376 | enable_compat20(); |
377 | break; | |
378 | } | |
379 | /* FALLTHROUGH */ | |
6ae2364d | 380 | default: |
a8be9f80 | 381 | mismatch = 1; |
382 | break; | |
383 | } | |
384 | chop(server_version_string); | |
385 | chop(client_version_string); | |
386 | debug("Local version string %.200s", server_version_string); | |
387 | ||
388 | if (mismatch) { | |
7368a6c8 | 389 | s = "Protocol major versions differ.\n"; |
390 | (void) atomicio(write, sock_out, s, strlen(s)); | |
391 | close(sock_in); | |
392 | close(sock_out); | |
a8be9f80 | 393 | log("Protocol major versions differ for %s: %.200s vs. %.200s", |
394 | get_remote_ipaddr(), | |
395 | server_version_string, client_version_string); | |
7368a6c8 | 396 | fatal_cleanup(); |
7368a6c8 | 397 | } |
a306f2dd | 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); | |
7368a6c8 | 412 | } |
413 | ||
5260325f | 414 | /* |
415 | * Main program for the daemon. | |
416 | */ | |
8efc0c15 | 417 | int |
418 | main(int ac, char **av) | |
419 | { | |
5260325f | 420 | extern char *optarg; |
421 | extern int optind; | |
9da5c3c9 | 422 | int opt, sock_in = 0, sock_out = 0, newsock, i, fdsetsz, on = 1; |
423 | pid_t pid; | |
48e671d5 | 424 | socklen_t fromlen; |
a306f2dd | 425 | int silent = 0; |
48e671d5 | 426 | fd_set *fdset; |
427 | struct sockaddr_storage from; | |
5260325f | 428 | const char *remote_ip; |
429 | int remote_port; | |
5260325f | 430 | FILE *f; |
431 | struct linger linger; | |
48e671d5 | 432 | struct addrinfo *ai; |
433 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | |
434 | int listen_sock, maxfd; | |
5260325f | 435 | |
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]; | |
442 | ||
443 | /* Initialize configuration options to their default values. */ | |
444 | initialize_server_options(&options); | |
445 | ||
446 | /* Parse command-line arguments. */ | |
a8be9f80 | 447 | while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:diqQ46")) != EOF) { |
5260325f | 448 | switch (opt) { |
48e671d5 | 449 | case '4': |
450 | IPv4or6 = AF_INET; | |
451 | break; | |
452 | case '6': | |
453 | IPv4or6 = AF_INET6; | |
454 | break; | |
5260325f | 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': | |
a306f2dd | 466 | silent = 1; |
5260325f | 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': | |
48e671d5 | 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); | |
5260325f | 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"); | |
aa3378df | 499 | fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE); |
5260325f | 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", | |
48e671d5 | 508 | HOST_KEY_FILE); |
509 | fprintf(stderr, " -4 Use IPv4 only\n"); | |
510 | fprintf(stderr, " -6 Use IPv6 only\n"); | |
5260325f | 511 | exit(1); |
512 | } | |
513 | } | |
514 | ||
48e671d5 | 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, | |
a306f2dd | 522 | !silent && !inetd_flag); |
48e671d5 | 523 | |
5260325f | 524 | /* Read server configuration options from the configuration file. */ |
525 | read_server_config(&options, config_file_name); | |
526 | ||
527 | /* Fill in default values for those options not explicitly set. */ | |
528 | fill_default_server_options(&options); | |
529 | ||
5260325f | 530 | /* Check that there are no remaining arguments. */ |
531 | if (optind < ac) { | |
532 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | |
533 | exit(1); | |
8efc0c15 | 534 | } |
5260325f | 535 | |
536 | debug("sshd version %.100s", SSH_VERSION); | |
537 | ||
a306f2dd | 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); | |
565 | if (!load_private_key(options.dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) { | |
566 | error("Could not load DSA host key: %.200s", options.dsa_key_file); | |
567 | log("Disabling protocol version 2"); | |
568 | options.protocol &= ~SSH_PROTO_2; | |
569 | } | |
570 | } | |
571 | if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) { | |
572 | if (silent == 0) | |
573 | fprintf(stderr, "sshd: no hostkeys available -- exiting.\n"); | |
574 | log("sshd: no hostkeys available -- exiting.\n"); | |
5260325f | 575 | exit(1); |
576 | } | |
5260325f | 577 | |
a306f2dd | 578 | /* Check certain values for sanity. */ |
579 | if (options.protocol & SSH_PROTO_1) { | |
580 | if (options.server_key_bits < 512 || | |
581 | options.server_key_bits > 32768) { | |
582 | fprintf(stderr, "Bad server key size.\n"); | |
583 | exit(1); | |
584 | } | |
585 | /* | |
586 | * Check that server and host key lengths differ sufficiently. This | |
587 | * is necessary to make double encryption work with rsaref. Oh, I | |
588 | * hate software patents. I dont know if this can go? Niels | |
589 | */ | |
590 | if (options.server_key_bits > | |
591 | BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED && | |
592 | options.server_key_bits < | |
593 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
594 | options.server_key_bits = | |
595 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED; | |
596 | debug("Forcing server key to %d bits to make it differ from host key.", | |
597 | options.server_key_bits); | |
598 | } | |
599 | } | |
600 | ||
601 | /* Initialize the log (it is reinitialized below in case we forked). */ | |
5260325f | 602 | if (debug_flag && !inetd_flag) |
603 | log_stderr = 1; | |
604 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
605 | ||
a306f2dd | 606 | /* |
607 | * If not in debugging mode, and not started from inetd, disconnect | |
608 | * from the controlling terminal, and fork. The original process | |
609 | * exits. | |
610 | */ | |
5260325f | 611 | if (!debug_flag && !inetd_flag) { |
8efc0c15 | 612 | #ifdef TIOCNOTTY |
5260325f | 613 | int fd; |
8efc0c15 | 614 | #endif /* TIOCNOTTY */ |
5260325f | 615 | if (daemon(0, 0) < 0) |
616 | fatal("daemon() failed: %.200s", strerror(errno)); | |
617 | ||
618 | /* Disconnect from the controlling tty. */ | |
8efc0c15 | 619 | #ifdef TIOCNOTTY |
5260325f | 620 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
621 | if (fd >= 0) { | |
622 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
623 | close(fd); | |
624 | } | |
8efc0c15 | 625 | #endif /* TIOCNOTTY */ |
8efc0c15 | 626 | } |
5260325f | 627 | /* Reinitialize the log (because of the fork above). */ |
628 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
629 | ||
5260325f | 630 | /* Do not display messages to stdout in RSA code. */ |
631 | rsa_set_verbose(0); | |
632 | ||
633 | /* Initialize the random number generator. */ | |
634 | arc4random_stir(); | |
635 | ||
636 | /* Chdir to the root directory so that the current disk can be | |
637 | unmounted if desired. */ | |
638 | chdir("/"); | |
639 | ||
5260325f | 640 | /* Start listening for a socket, unless started from inetd. */ |
641 | if (inetd_flag) { | |
642 | int s1, s2; | |
643 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ | |
644 | s2 = dup(s1); | |
645 | sock_in = dup(0); | |
646 | sock_out = dup(1); | |
a306f2dd | 647 | /* |
648 | * We intentionally do not close the descriptors 0, 1, and 2 | |
649 | * as our code for setting the descriptors won\'t work if | |
650 | * ttyfd happens to be one of those. | |
651 | */ | |
5260325f | 652 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); |
653 | ||
a306f2dd | 654 | if (options.protocol & SSH_PROTO_1) { |
655 | public_key = RSA_new(); | |
656 | sensitive_data.private_key = RSA_new(); | |
657 | log("Generating %d bit RSA key.", options.server_key_bits); | |
658 | rsa_generate_key(sensitive_data.private_key, public_key, | |
659 | options.server_key_bits); | |
660 | arc4random_stir(); | |
661 | log("RSA key generation complete."); | |
662 | } | |
5260325f | 663 | } else { |
48e671d5 | 664 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { |
665 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | |
666 | continue; | |
667 | if (num_listen_socks >= MAX_LISTEN_SOCKS) | |
668 | fatal("Too many listen sockets. " | |
669 | "Enlarge MAX_LISTEN_SOCKS"); | |
670 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
671 | ntop, sizeof(ntop), strport, sizeof(strport), | |
672 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | |
673 | error("getnameinfo failed"); | |
674 | continue; | |
675 | } | |
676 | /* Create socket for listening. */ | |
677 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); | |
678 | if (listen_sock < 0) { | |
679 | /* kernel may not support ipv6 */ | |
680 | verbose("socket: %.100s", strerror(errno)); | |
681 | continue; | |
682 | } | |
683 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { | |
684 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); | |
685 | close(listen_sock); | |
686 | continue; | |
687 | } | |
688 | /* | |
689 | * Set socket options. We try to make the port | |
690 | * reusable and have it close as fast as possible | |
691 | * without waiting in unnecessary wait states on | |
692 | * close. | |
693 | */ | |
694 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, | |
695 | (void *) &on, sizeof(on)); | |
696 | linger.l_onoff = 1; | |
697 | linger.l_linger = 5; | |
698 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, | |
699 | (void *) &linger, sizeof(linger)); | |
700 | ||
701 | debug("Bind to port %s on %s.", strport, ntop); | |
702 | ||
703 | /* Bind the socket to the desired port. */ | |
16218745 | 704 | if ((bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) && |
705 | (!ai->ai_next)) { | |
48e671d5 | 706 | error("Bind to port %s on %s failed: %.200s.", |
707 | strport, ntop, strerror(errno)); | |
708 | close(listen_sock); | |
709 | continue; | |
710 | } | |
711 | listen_socks[num_listen_socks] = listen_sock; | |
712 | num_listen_socks++; | |
713 | ||
714 | /* Start listening on the port. */ | |
715 | log("Server listening on %s port %s.", ntop, strport); | |
716 | if (listen(listen_sock, 5) < 0) | |
717 | fatal("listen: %.100s", strerror(errno)); | |
718 | ||
5260325f | 719 | } |
48e671d5 | 720 | freeaddrinfo(options.listen_addrs); |
721 | ||
722 | if (!num_listen_socks) | |
723 | fatal("Cannot bind any address."); | |
724 | ||
5260325f | 725 | if (!debug_flag) { |
aa3378df | 726 | /* |
727 | * Record our pid in /etc/sshd_pid to make it easier | |
728 | * to kill the correct sshd. We don\'t want to do | |
729 | * this before the bind above because the bind will | |
730 | * fail if there already is a daemon, and this will | |
731 | * overwrite any old pid in the file. | |
732 | */ | |
0fbe8c74 | 733 | f = fopen(options.pid_file, "w"); |
5260325f | 734 | if (f) { |
735 | fprintf(f, "%u\n", (unsigned int) getpid()); | |
736 | fclose(f); | |
737 | } | |
8efc0c15 | 738 | } |
a306f2dd | 739 | if (options.protocol & SSH_PROTO_1) { |
740 | public_key = RSA_new(); | |
741 | sensitive_data.private_key = RSA_new(); | |
8efc0c15 | 742 | |
a306f2dd | 743 | log("Generating %d bit RSA key.", options.server_key_bits); |
744 | rsa_generate_key(sensitive_data.private_key, public_key, | |
745 | options.server_key_bits); | |
746 | arc4random_stir(); | |
747 | log("RSA key generation complete."); | |
5260325f | 748 | |
a306f2dd | 749 | /* Schedule server key regeneration alarm. */ |
750 | signal(SIGALRM, key_regeneration_alarm); | |
751 | alarm(options.key_regeneration_time); | |
752 | } | |
5260325f | 753 | |
754 | /* Arrange to restart on SIGHUP. The handler needs listen_sock. */ | |
755 | signal(SIGHUP, sighup_handler); | |
756 | signal(SIGTERM, sigterm_handler); | |
757 | signal(SIGQUIT, sigterm_handler); | |
758 | ||
759 | /* Arrange SIGCHLD to be caught. */ | |
760 | signal(SIGCHLD, main_sigchld_handler); | |
761 | ||
48e671d5 | 762 | /* setup fd set for listen */ |
763 | maxfd = 0; | |
764 | for (i = 0; i < num_listen_socks; i++) | |
765 | if (listen_socks[i] > maxfd) | |
766 | maxfd = listen_socks[i]; | |
6ae2364d | 767 | fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask); |
768 | fdset = (fd_set *)xmalloc(fdsetsz); | |
48e671d5 | 769 | |
aa3378df | 770 | /* |
771 | * Stay listening for connections until the system crashes or | |
772 | * the daemon is killed with a signal. | |
773 | */ | |
5260325f | 774 | for (;;) { |
775 | if (received_sighup) | |
776 | sighup_restart(); | |
48e671d5 | 777 | /* Wait in select until there is a connection. */ |
778 | memset(fdset, 0, fdsetsz); | |
779 | for (i = 0; i < num_listen_socks; i++) | |
780 | FD_SET(listen_socks[i], fdset); | |
781 | if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) { | |
782 | if (errno != EINTR) | |
783 | error("select: %.100s", strerror(errno)); | |
2d86a6cc | 784 | continue; |
48e671d5 | 785 | } |
786 | for (i = 0; i < num_listen_socks; i++) { | |
787 | if (!FD_ISSET(listen_socks[i], fdset)) | |
5260325f | 788 | continue; |
48e671d5 | 789 | fromlen = sizeof(from); |
790 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, | |
791 | &fromlen); | |
792 | if (newsock < 0) { | |
793 | if (errno != EINTR && errno != EWOULDBLOCK) | |
794 | error("accept: %.100s", strerror(errno)); | |
795 | continue; | |
796 | } | |
797 | if (fcntl(newsock, F_SETFL, 0) < 0) { | |
798 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | |
5260325f | 799 | continue; |
800 | } | |
aa3378df | 801 | /* |
802 | * Got connection. Fork a child to handle it, unless | |
803 | * we are in debugging mode. | |
804 | */ | |
5260325f | 805 | if (debug_flag) { |
aa3378df | 806 | /* |
807 | * In debugging mode. Close the listening | |
808 | * socket, and start processing the | |
809 | * connection without forking. | |
810 | */ | |
5260325f | 811 | debug("Server will not fork when running in debugging mode."); |
48e671d5 | 812 | close_listen_socks(); |
5260325f | 813 | sock_in = newsock; |
814 | sock_out = newsock; | |
815 | pid = getpid(); | |
816 | break; | |
817 | } else { | |
aa3378df | 818 | /* |
819 | * Normal production daemon. Fork, and have | |
820 | * the child process the connection. The | |
821 | * parent continues listening. | |
822 | */ | |
5260325f | 823 | if ((pid = fork()) == 0) { |
aa3378df | 824 | /* |
825 | * Child. Close the listening socket, and start using the | |
826 | * accepted socket. Reinitialize logging (since our pid has | |
827 | * changed). We break out of the loop to handle the connection. | |
828 | */ | |
48e671d5 | 829 | close_listen_socks(); |
5260325f | 830 | sock_in = newsock; |
831 | sock_out = newsock; | |
832 | log_init(av0, options.log_level, options.log_facility, log_stderr); | |
833 | break; | |
834 | } | |
835 | } | |
836 | ||
837 | /* Parent. Stay in the loop. */ | |
838 | if (pid < 0) | |
839 | error("fork: %.100s", strerror(errno)); | |
840 | else | |
841 | debug("Forked child %d.", pid); | |
842 | ||
843 | /* Mark that the key has been used (it was "given" to the child). */ | |
844 | key_used = 1; | |
845 | ||
846 | arc4random_stir(); | |
847 | ||
848 | /* Close the new socket (the child is now taking care of it). */ | |
849 | close(newsock); | |
48e671d5 | 850 | } /* for (i = 0; i < num_listen_socks; i++) */ |
851 | /* child process check (or debug mode) */ | |
852 | if (num_listen_socks < 0) | |
853 | break; | |
5260325f | 854 | } |
855 | } | |
8efc0c15 | 856 | |
5260325f | 857 | /* This is the child processing a new connection. */ |
858 | ||
aa3378df | 859 | /* |
860 | * Disable the key regeneration alarm. We will not regenerate the | |
861 | * key since we are no longer in a position to give it to anyone. We | |
862 | * will not restart on SIGHUP since it no longer makes sense. | |
863 | */ | |
5260325f | 864 | alarm(0); |
865 | signal(SIGALRM, SIG_DFL); | |
866 | signal(SIGHUP, SIG_DFL); | |
867 | signal(SIGTERM, SIG_DFL); | |
868 | signal(SIGQUIT, SIG_DFL); | |
869 | signal(SIGCHLD, SIG_DFL); | |
870 | ||
aa3378df | 871 | /* |
872 | * Set socket options for the connection. We want the socket to | |
873 | * close as fast as possible without waiting for anything. If the | |
874 | * connection is not a socket, these will do nothing. | |
875 | */ | |
876 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | |
5260325f | 877 | linger.l_onoff = 1; |
878 | linger.l_linger = 5; | |
879 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | |
880 | ||
aa3378df | 881 | /* |
882 | * Register our connection. This turns encryption off because we do | |
883 | * not have a key. | |
884 | */ | |
5260325f | 885 | packet_set_connection(sock_in, sock_out); |
886 | ||
887 | remote_port = get_remote_port(); | |
888 | remote_ip = get_remote_ipaddr(); | |
889 | ||
890 | /* Check whether logins are denied from this host. */ | |
891 | #ifdef LIBWRAP | |
48e671d5 | 892 | /* XXX LIBWRAP noes not know about IPv6 */ |
5260325f | 893 | { |
894 | struct request_info req; | |
8efc0c15 | 895 | |
5260325f | 896 | request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL); |
897 | fromhost(&req); | |
8efc0c15 | 898 | |
5260325f | 899 | if (!hosts_access(&req)) { |
900 | close(sock_in); | |
901 | close(sock_out); | |
902 | refuse(&req); | |
903 | } | |
48e671d5 | 904 | /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */ |
8efc0c15 | 905 | } |
48e671d5 | 906 | #endif /* LIBWRAP */ |
5260325f | 907 | /* Log the connection. */ |
908 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | |
8efc0c15 | 909 | |
aa3378df | 910 | /* |
911 | * We don\'t want to listen forever unless the other side | |
912 | * successfully authenticates itself. So we set up an alarm which is | |
913 | * cleared after successful authentication. A limit of zero | |
914 | * indicates no limit. Note that we don\'t set the alarm in debugging | |
915 | * mode; it is just annoying to have the server exit just when you | |
916 | * are about to discover the bug. | |
917 | */ | |
5260325f | 918 | signal(SIGALRM, grace_alarm_handler); |
919 | if (!debug_flag) | |
920 | alarm(options.login_grace_time); | |
921 | ||
7368a6c8 | 922 | sshd_exchange_identification(sock_in, sock_out); |
aa3378df | 923 | /* |
924 | * Check that the connection comes from a privileged port. Rhosts- | |
925 | * and Rhosts-RSA-Authentication only make sense from priviledged | |
926 | * programs. Of course, if the intruder has root access on his local | |
927 | * machine, he can connect from any port. So do not use these | |
928 | * authentication methods from machines that you do not trust. | |
929 | */ | |
5260325f | 930 | if (remote_port >= IPPORT_RESERVED || |
931 | remote_port < IPPORT_RESERVED / 2) { | |
932 | options.rhosts_authentication = 0; | |
933 | options.rhosts_rsa_authentication = 0; | |
934 | } | |
48e671d5 | 935 | #ifdef KRB4 |
936 | if (!packet_connection_is_ipv4() && | |
937 | options.kerberos_authentication) { | |
938 | debug("Kerberos Authentication disabled, only available for IPv4."); | |
939 | options.kerberos_authentication = 0; | |
940 | } | |
941 | #endif /* KRB4 */ | |
942 | ||
5260325f | 943 | packet_set_nonblocking(); |
944 | ||
7b2ea3a1 | 945 | /* perform the key exchange */ |
7b2ea3a1 | 946 | /* authenticate user and start session */ |
e78a59f5 | 947 | if (compat20) { |
948 | do_ssh2_kex(); | |
949 | do_authentication2(); | |
950 | } else { | |
951 | do_ssh1_kex(); | |
952 | do_authentication(); | |
953 | } | |
8efc0c15 | 954 | |
955 | #ifdef KRB4 | |
5260325f | 956 | /* Cleanup user's ticket cache file. */ |
957 | if (options.kerberos_ticket_cleanup) | |
958 | (void) dest_tkt(); | |
8efc0c15 | 959 | #endif /* KRB4 */ |
960 | ||
5260325f | 961 | /* The connection has been terminated. */ |
962 | verbose("Closing connection to %.100s", remote_ip); | |
8efc0c15 | 963 | |
d94aa2ae | 964 | #ifdef USE_PAM |
a5c9cd31 | 965 | finish_pam(); |
d94aa2ae | 966 | #endif /* USE_PAM */ |
8efc0c15 | 967 | |
5260325f | 968 | packet_close(); |
969 | exit(0); | |
970 | } | |
8efc0c15 | 971 | |
5260325f | 972 | /* |
7b2ea3a1 | 973 | * SSH1 key exchange |
5260325f | 974 | */ |
e7c0f9d5 | 975 | void |
7368a6c8 | 976 | do_ssh1_kex() |
8efc0c15 | 977 | { |
5260325f | 978 | int i, len; |
7b2ea3a1 | 979 | int plen, slen; |
5260325f | 980 | BIGNUM *session_key_int; |
981 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; | |
7b2ea3a1 | 982 | unsigned char cookie[8]; |
5260325f | 983 | unsigned int cipher_type, auth_mask, protocol_flags; |
5260325f | 984 | u_int32_t rand = 0; |
985 | ||
aa3378df | 986 | /* |
987 | * Generate check bytes that the client must send back in the user | |
988 | * packet in order for it to be accepted; this is used to defy ip | |
989 | * spoofing attacks. Note that this only works against somebody | |
990 | * doing IP spoofing from a remote machine; any machine on the local | |
991 | * network can still see outgoing packets and catch the random | |
992 | * cookie. This only affects rhosts authentication, and this is one | |
993 | * of the reasons why it is inherently insecure. | |
994 | */ | |
5260325f | 995 | for (i = 0; i < 8; i++) { |
996 | if (i % 4 == 0) | |
997 | rand = arc4random(); | |
7b2ea3a1 | 998 | cookie[i] = rand & 0xff; |
5260325f | 999 | rand >>= 8; |
1000 | } | |
1001 | ||
aa3378df | 1002 | /* |
1003 | * Send our public key. We include in the packet 64 bits of random | |
1004 | * data that must be matched in the reply in order to prevent IP | |
1005 | * spoofing. | |
1006 | */ | |
5260325f | 1007 | packet_start(SSH_SMSG_PUBLIC_KEY); |
1008 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1009 | packet_put_char(cookie[i]); |
5260325f | 1010 | |
1011 | /* Store our public server RSA key. */ | |
1012 | packet_put_int(BN_num_bits(public_key->n)); | |
1013 | packet_put_bignum(public_key->e); | |
1014 | packet_put_bignum(public_key->n); | |
1015 | ||
1016 | /* Store our public host RSA key. */ | |
1017 | packet_put_int(BN_num_bits(sensitive_data.host_key->n)); | |
1018 | packet_put_bignum(sensitive_data.host_key->e); | |
1019 | packet_put_bignum(sensitive_data.host_key->n); | |
1020 | ||
1021 | /* Put protocol flags. */ | |
1022 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | |
1023 | ||
1024 | /* Declare which ciphers we support. */ | |
8ce64345 | 1025 | packet_put_int(cipher_mask1()); |
5260325f | 1026 | |
1027 | /* Declare supported authentication types. */ | |
1028 | auth_mask = 0; | |
1029 | if (options.rhosts_authentication) | |
1030 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | |
1031 | if (options.rhosts_rsa_authentication) | |
1032 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | |
1033 | if (options.rsa_authentication) | |
1034 | auth_mask |= 1 << SSH_AUTH_RSA; | |
8efc0c15 | 1035 | #ifdef KRB4 |
5260325f | 1036 | if (options.kerberos_authentication) |
1037 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | |
8efc0c15 | 1038 | #endif |
1039 | #ifdef AFS | |
5260325f | 1040 | if (options.kerberos_tgt_passing) |
1041 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | |
1042 | if (options.afs_token_passing) | |
1043 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | |
8efc0c15 | 1044 | #endif |
5260325f | 1045 | #ifdef SKEY |
1046 | if (options.skey_authentication == 1) | |
1047 | auth_mask |= 1 << SSH_AUTH_TIS; | |
1048 | #endif | |
1049 | if (options.password_authentication) | |
1050 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | |
1051 | packet_put_int(auth_mask); | |
1052 | ||
1053 | /* Send the packet and wait for it to be sent. */ | |
1054 | packet_send(); | |
1055 | packet_write_wait(); | |
1056 | ||
1057 | debug("Sent %d bit public key and %d bit host key.", | |
1058 | BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n)); | |
1059 | ||
1060 | /* Read clients reply (cipher type and session key). */ | |
1061 | packet_read_expect(&plen, SSH_CMSG_SESSION_KEY); | |
1062 | ||
2d86a6cc | 1063 | /* Get cipher type and check whether we accept this. */ |
5260325f | 1064 | cipher_type = packet_get_char(); |
1065 | ||
6ae2364d | 1066 | if (!(cipher_mask() & (1 << cipher_type))) |
2d86a6cc | 1067 | packet_disconnect("Warning: client selects unsupported cipher."); |
1068 | ||
5260325f | 1069 | /* Get check bytes from the packet. These must match those we |
1070 | sent earlier with the public key packet. */ | |
1071 | for (i = 0; i < 8; i++) | |
7b2ea3a1 | 1072 | if (cookie[i] != packet_get_char()) |
5260325f | 1073 | packet_disconnect("IP Spoofing check bytes do not match."); |
1074 | ||
1075 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | |
1076 | ||
1077 | /* Get the encrypted integer. */ | |
1078 | session_key_int = BN_new(); | |
1079 | packet_get_bignum(session_key_int, &slen); | |
1080 | ||
5260325f | 1081 | protocol_flags = packet_get_int(); |
1082 | packet_set_protocol_flags(protocol_flags); | |
1083 | ||
1084 | packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY); | |
1085 | ||
aa3378df | 1086 | /* |
1087 | * Decrypt it using our private server key and private host key (key | |
1088 | * with larger modulus first). | |
1089 | */ | |
5260325f | 1090 | if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) { |
1091 | /* Private key has bigger modulus. */ | |
1092 | if (BN_num_bits(sensitive_data.private_key->n) < | |
1093 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | |
1094 | fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | |
1095 | get_remote_ipaddr(), | |
1096 | BN_num_bits(sensitive_data.private_key->n), | |
1097 | BN_num_bits(sensitive_data.host_key->n), | |
1098 | SSH_KEY_BITS_RESERVED); | |
1099 | } | |
1100 | rsa_private_decrypt(session_key_int, session_key_int, | |
1101 | sensitive_data.private_key); | |
1102 | rsa_private_decrypt(session_key_int, session_key_int, | |
1103 | sensitive_data.host_key); | |
1104 | } else { | |
1105 | /* Host key has bigger modulus (or they are equal). */ | |
1106 | if (BN_num_bits(sensitive_data.host_key->n) < | |
1107 | BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) { | |
1108 | fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d", | |
1109 | get_remote_ipaddr(), | |
1110 | BN_num_bits(sensitive_data.host_key->n), | |
1111 | BN_num_bits(sensitive_data.private_key->n), | |
1112 | SSH_KEY_BITS_RESERVED); | |
1113 | } | |
1114 | rsa_private_decrypt(session_key_int, session_key_int, | |
1115 | sensitive_data.host_key); | |
1116 | rsa_private_decrypt(session_key_int, session_key_int, | |
1117 | sensitive_data.private_key); | |
1118 | } | |
1119 | ||
7b2ea3a1 | 1120 | compute_session_id(session_id, cookie, |
5260325f | 1121 | sensitive_data.host_key->n, |
1122 | sensitive_data.private_key->n); | |
1123 | ||
7b2ea3a1 | 1124 | /* Destroy the private and public keys. They will no longer be needed. */ |
a306f2dd | 1125 | destroy_sensitive_data(); |
7b2ea3a1 | 1126 | |
aa3378df | 1127 | /* |
1128 | * Extract session key from the decrypted integer. The key is in the | |
1129 | * least significant 256 bits of the integer; the first byte of the | |
1130 | * key is in the highest bits. | |
1131 | */ | |
5260325f | 1132 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); |
1133 | len = BN_num_bytes(session_key_int); | |
1134 | if (len < 0 || len > sizeof(session_key)) | |
1135 | fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d", | |
1136 | get_remote_ipaddr(), | |
1137 | len, sizeof(session_key)); | |
1138 | memset(session_key, 0, sizeof(session_key)); | |
1139 | BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len); | |
1140 | ||
7b2ea3a1 | 1141 | /* Destroy the decrypted integer. It is no longer needed. */ |
1142 | BN_clear_free(session_key_int); | |
1143 | ||
5260325f | 1144 | /* Xor the first 16 bytes of the session key with the session id. */ |
1145 | for (i = 0; i < 16; i++) | |
1146 | session_key[i] ^= session_id[i]; | |
1147 | ||
5260325f | 1148 | /* Set the session key. From this on all communications will be encrypted. */ |
1149 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | |
1150 | ||
1151 | /* Destroy our copy of the session key. It is no longer needed. */ | |
1152 | memset(session_key, 0, sizeof(session_key)); | |
1153 | ||
1154 | debug("Received session key; encryption turned on."); | |
1155 | ||
1156 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ | |
1157 | packet_start(SSH_SMSG_SUCCESS); | |
1158 | packet_send(); | |
1159 | packet_write_wait(); | |
5260325f | 1160 | } |
e78a59f5 | 1161 | |
1162 | /* | |
1163 | * SSH2 key exchange: diffie-hellman-group1-sha1 | |
1164 | */ | |
1165 | void | |
1166 | do_ssh2_kex() | |
1167 | { | |
1168 | Buffer *server_kexinit; | |
1169 | Buffer *client_kexinit; | |
1170 | int payload_len, dlen; | |
1171 | int slen; | |
1172 | unsigned int klen, kout; | |
1173 | char *ptr; | |
1174 | unsigned char *signature = NULL; | |
1175 | unsigned char *server_host_key_blob = NULL; | |
1176 | unsigned int sbloblen; | |
1177 | DH *dh; | |
1178 | BIGNUM *dh_client_pub = 0; | |
1179 | BIGNUM *shared_secret = 0; | |
1180 | int i; | |
1181 | unsigned char *kbuf; | |
1182 | unsigned char *hash; | |
1183 | Kex *kex; | |
e78a59f5 | 1184 | char *cprop[PROPOSAL_MAX]; |
1185 | char *sprop[PROPOSAL_MAX]; | |
1186 | ||
1187 | /* KEXINIT */ | |
1188 | ||
a8be9f80 | 1189 | if (options.ciphers != NULL) { |
6ae2364d | 1190 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
a8be9f80 | 1191 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
1192 | } | |
1193 | ||
e78a59f5 | 1194 | debug("Sending KEX init."); |
1195 | ||
1196 | for (i = 0; i < PROPOSAL_MAX; i++) | |
1197 | sprop[i] = xstrdup(myproposal[i]); | |
1198 | server_kexinit = kex_init(sprop); | |
1199 | packet_start(SSH2_MSG_KEXINIT); | |
1200 | packet_put_raw(buffer_ptr(server_kexinit), buffer_len(server_kexinit)); | |
1201 | packet_send(); | |
1202 | packet_write_wait(); | |
1203 | ||
1204 | debug("done"); | |
1205 | ||
1206 | packet_read_expect(&payload_len, SSH2_MSG_KEXINIT); | |
1207 | ||
1208 | /* | |
1209 | * save raw KEXINIT payload in buffer. this is used during | |
1210 | * computation of the session_id and the session keys. | |
1211 | */ | |
1212 | client_kexinit = xmalloc(sizeof(*client_kexinit)); | |
1213 | buffer_init(client_kexinit); | |
1214 | ptr = packet_get_raw(&payload_len); | |
1215 | buffer_append(client_kexinit, ptr, payload_len); | |
1216 | ||
1217 | /* skip cookie */ | |
1218 | for (i = 0; i < 16; i++) | |
1219 | (void) packet_get_char(); | |
1220 | /* save kex init proposal strings */ | |
1221 | for (i = 0; i < PROPOSAL_MAX; i++) { | |
1222 | cprop[i] = packet_get_string(NULL); | |
1223 | debug("got kexinit string: %s", cprop[i]); | |
1224 | } | |
1225 | ||
1226 | i = (int) packet_get_char(); | |
1227 | debug("first kex follow == %d", i); | |
1228 | i = packet_get_int(); | |
1229 | debug("reserved == %d", i); | |
1230 | ||
1231 | debug("done read kexinit"); | |
1232 | kex = kex_choose_conf(cprop, sprop, 1); | |
1233 | ||
1234 | /* KEXDH */ | |
1235 | ||
1236 | debug("Wait SSH2_MSG_KEXDH_INIT."); | |
1237 | packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT); | |
1238 | ||
1239 | /* key, cert */ | |
1240 | dh_client_pub = BN_new(); | |
1241 | if (dh_client_pub == NULL) | |
1242 | fatal("dh_client_pub == NULL"); | |
1243 | packet_get_bignum2(dh_client_pub, &dlen); | |
1244 | ||
1245 | #ifdef DEBUG_KEXDH | |
1246 | fprintf(stderr, "\ndh_client_pub= "); | |
1247 | bignum_print(dh_client_pub); | |
1248 | fprintf(stderr, "\n"); | |
1249 | debug("bits %d", BN_num_bits(dh_client_pub)); | |
1250 | #endif | |
1251 | ||
1252 | /* generate DH key */ | |
a8be9f80 | 1253 | dh = dh_new_group1(); /* XXX depends on 'kex' */ |
e78a59f5 | 1254 | |
1255 | #ifdef DEBUG_KEXDH | |
1256 | fprintf(stderr, "\np= "); | |
1257 | bignum_print(dh->p); | |
1258 | fprintf(stderr, "\ng= "); | |
1259 | bignum_print(dh->g); | |
1260 | fprintf(stderr, "\npub= "); | |
1261 | bignum_print(dh->pub_key); | |
1262 | fprintf(stderr, "\n"); | |
1263 | #endif | |
a8be9f80 | 1264 | if (!dh_pub_is_valid(dh, dh_client_pub)) |
1265 | packet_disconnect("bad client public DH value"); | |
e78a59f5 | 1266 | |
1267 | klen = DH_size(dh); | |
1268 | kbuf = xmalloc(klen); | |
1269 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
1270 | ||
1271 | #ifdef DEBUG_KEXDH | |
1272 | debug("shared secret: len %d/%d", klen, kout); | |
1273 | fprintf(stderr, "shared secret == "); | |
1274 | for (i = 0; i< kout; i++) | |
1275 | fprintf(stderr, "%02x", (kbuf[i])&0xff); | |
1276 | fprintf(stderr, "\n"); | |
1277 | #endif | |
1278 | shared_secret = BN_new(); | |
1279 | ||
1280 | BN_bin2bn(kbuf, kout, shared_secret); | |
1281 | memset(kbuf, 0, klen); | |
1282 | xfree(kbuf); | |
1283 | ||
a306f2dd | 1284 | /* XXX precompute? */ |
1285 | dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen); | |
e78a59f5 | 1286 | |
1287 | /* calc H */ /* XXX depends on 'kex' */ | |
1288 | hash = kex_hash( | |
1289 | client_version_string, | |
1290 | server_version_string, | |
1291 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), | |
1292 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), | |
1293 | (char *)server_host_key_blob, sbloblen, | |
1294 | dh_client_pub, | |
1295 | dh->pub_key, | |
1296 | shared_secret | |
1297 | ); | |
1298 | buffer_free(client_kexinit); | |
1299 | buffer_free(server_kexinit); | |
1300 | xfree(client_kexinit); | |
1301 | xfree(server_kexinit); | |
1302 | #ifdef DEBUG_KEXDH | |
6ae2364d | 1303 | fprintf(stderr, "hash == "); |
1304 | for (i = 0; i< 20; i++) | |
1305 | fprintf(stderr, "%02x", (hash[i])&0xff); | |
1306 | fprintf(stderr, "\n"); | |
e78a59f5 | 1307 | #endif |
a306f2dd | 1308 | /* save session id := H */ |
1309 | /* XXX hashlen depends on KEX */ | |
1310 | session_id2_len = 20; | |
1311 | session_id2 = xmalloc(session_id2_len); | |
1312 | memcpy(session_id2, hash, session_id2_len); | |
1313 | ||
e78a59f5 | 1314 | /* sign H */ |
a306f2dd | 1315 | /* XXX hashlen depends on KEX */ |
1316 | dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20); | |
1317 | ||
1318 | destroy_sensitive_data(); | |
e78a59f5 | 1319 | |
1320 | /* send server hostkey, DH pubkey 'f' and singed H */ | |
1321 | packet_start(SSH2_MSG_KEXDH_REPLY); | |
1322 | packet_put_string((char *)server_host_key_blob, sbloblen); | |
1323 | packet_put_bignum2(dh->pub_key); // f | |
1324 | packet_put_string((char *)signature, slen); | |
1325 | packet_send(); | |
d6f24e45 | 1326 | xfree(signature); |
a306f2dd | 1327 | xfree(server_host_key_blob); |
e78a59f5 | 1328 | packet_write_wait(); |
1329 | ||
1330 | kex_derive_keys(kex, hash, shared_secret); | |
1331 | packet_set_kex(kex); | |
1332 | ||
1333 | /* have keys, free DH */ | |
1334 | DH_free(dh); | |
1335 | ||
1336 | debug("send SSH2_MSG_NEWKEYS."); | |
1337 | packet_start(SSH2_MSG_NEWKEYS); | |
1338 | packet_send(); | |
1339 | packet_write_wait(); | |
1340 | debug("done: send SSH2_MSG_NEWKEYS."); | |
1341 | ||
1342 | debug("Wait SSH2_MSG_NEWKEYS."); | |
1343 | packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS); | |
1344 | debug("GOT SSH2_MSG_NEWKEYS."); | |
1345 | ||
a8be9f80 | 1346 | #ifdef DEBUG_KEXDH |
e78a59f5 | 1347 | /* send 1st encrypted/maced/compressed message */ |
1348 | packet_start(SSH2_MSG_IGNORE); | |
1349 | packet_put_cstring("markus"); | |
1350 | packet_send(); | |
1351 | packet_write_wait(); | |
a8be9f80 | 1352 | #endif |
e78a59f5 | 1353 | debug("done: KEX2."); |
1354 | } |