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