]> andersk Git - openssh.git/blob - sshconnect1.c
- (dtucker) OpenBSD CVS Sync
[openssh.git] / sshconnect1.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Code to connect to a remote host, and to perform the client side of the
6  * login (authentication) dialog.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14
15 #include "includes.h"
16 RCSID("$OpenBSD: sshconnect1.c,v 1.54 2003/07/22 13:35:22 markus Exp $");
17
18 #include <openssl/bn.h>
19 #include <openssl/md5.h>
20
21 #ifdef KRB5
22 #include <krb5.h>
23 #ifndef HEIMDAL
24 #define krb5_get_err_text(context,code) error_message(code)
25 #endif /* !HEIMDAL */
26 #endif
27
28 #include "ssh.h"
29 #include "ssh1.h"
30 #include "xmalloc.h"
31 #include "rsa.h"
32 #include "buffer.h"
33 #include "packet.h"
34 #include "mpaux.h"
35 #include "uidswap.h"
36 #include "log.h"
37 #include "readconf.h"
38 #include "key.h"
39 #include "authfd.h"
40 #include "sshconnect.h"
41 #include "authfile.h"
42 #include "readpass.h"
43 #include "cipher.h"
44 #include "canohost.h"
45 #include "auth.h"
46
47 /* Session id for the current session. */
48 u_char session_id[16];
49 u_int supported_authentications = 0;
50
51 extern Options options;
52 extern char *__progname;
53
54 /*
55  * Checks if the user has an authentication agent, and if so, tries to
56  * authenticate using the agent.
57  */
58 static int
59 try_agent_authentication(void)
60 {
61         int type;
62         char *comment;
63         AuthenticationConnection *auth;
64         u_char response[16];
65         u_int i;
66         Key *key;
67         BIGNUM *challenge;
68
69         /* Get connection to the agent. */
70         auth = ssh_get_authentication_connection();
71         if (!auth)
72                 return 0;
73
74         if ((challenge = BN_new()) == NULL)
75                 fatal("try_agent_authentication: BN_new failed");
76         /* Loop through identities served by the agent. */
77         for (key = ssh_get_first_identity(auth, &comment, 1);
78             key != NULL;
79             key = ssh_get_next_identity(auth, &comment, 1)) {
80
81                 /* Try this identity. */
82                 debug("Trying RSA authentication via agent with '%.100s'", comment);
83                 xfree(comment);
84
85                 /* Tell the server that we are willing to authenticate using this key. */
86                 packet_start(SSH_CMSG_AUTH_RSA);
87                 packet_put_bignum(key->rsa->n);
88                 packet_send();
89                 packet_write_wait();
90
91                 /* Wait for server's response. */
92                 type = packet_read();
93
94                 /* The server sends failure if it doesn\'t like our key or
95                    does not support RSA authentication. */
96                 if (type == SSH_SMSG_FAILURE) {
97                         debug("Server refused our key.");
98                         key_free(key);
99                         continue;
100                 }
101                 /* Otherwise it should have sent a challenge. */
102                 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
103                         packet_disconnect("Protocol error during RSA authentication: %d",
104                                           type);
105
106                 packet_get_bignum(challenge);
107                 packet_check_eom();
108
109                 debug("Received RSA challenge from server.");
110
111                 /* Ask the agent to decrypt the challenge. */
112                 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
113                         /*
114                          * The agent failed to authenticate this identifier
115                          * although it advertised it supports this.  Just
116                          * return a wrong value.
117                          */
118                         logit("Authentication agent failed to decrypt challenge.");
119                         memset(response, 0, sizeof(response));
120                 }
121                 key_free(key);
122                 debug("Sending response to RSA challenge.");
123
124                 /* Send the decrypted challenge back to the server. */
125                 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
126                 for (i = 0; i < 16; i++)
127                         packet_put_char(response[i]);
128                 packet_send();
129                 packet_write_wait();
130
131                 /* Wait for response from the server. */
132                 type = packet_read();
133
134                 /* The server returns success if it accepted the authentication. */
135                 if (type == SSH_SMSG_SUCCESS) {
136                         ssh_close_authentication_connection(auth);
137                         BN_clear_free(challenge);
138                         debug("RSA authentication accepted by server.");
139                         return 1;
140                 }
141                 /* Otherwise it should return failure. */
142                 if (type != SSH_SMSG_FAILURE)
143                         packet_disconnect("Protocol error waiting RSA auth response: %d",
144                                           type);
145         }
146         ssh_close_authentication_connection(auth);
147         BN_clear_free(challenge);
148         debug("RSA authentication using agent refused.");
149         return 0;
150 }
151
152 /*
153  * Computes the proper response to a RSA challenge, and sends the response to
154  * the server.
155  */
156 static void
157 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
158 {
159         u_char buf[32], response[16];
160         MD5_CTX md;
161         int i, len;
162
163         /* Decrypt the challenge using the private key. */
164         /* XXX think about Bleichenbacher, too */
165         if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
166                 packet_disconnect(
167                     "respond_to_rsa_challenge: rsa_private_decrypt failed");
168
169         /* Compute the response. */
170         /* The response is MD5 of decrypted challenge plus session id. */
171         len = BN_num_bytes(challenge);
172         if (len <= 0 || len > sizeof(buf))
173                 packet_disconnect(
174                     "respond_to_rsa_challenge: bad challenge length %d", len);
175
176         memset(buf, 0, sizeof(buf));
177         BN_bn2bin(challenge, buf + sizeof(buf) - len);
178         MD5_Init(&md);
179         MD5_Update(&md, buf, 32);
180         MD5_Update(&md, session_id, 16);
181         MD5_Final(response, &md);
182
183         debug("Sending response to host key RSA challenge.");
184
185         /* Send the response back to the server. */
186         packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
187         for (i = 0; i < 16; i++)
188                 packet_put_char(response[i]);
189         packet_send();
190         packet_write_wait();
191
192         memset(buf, 0, sizeof(buf));
193         memset(response, 0, sizeof(response));
194         memset(&md, 0, sizeof(md));
195 }
196
197 /*
198  * Checks if the user has authentication file, and if so, tries to authenticate
199  * the user using it.
200  */
201 static int
202 try_rsa_authentication(int idx)
203 {
204         BIGNUM *challenge;
205         Key *public, *private;
206         char buf[300], *passphrase, *comment, *authfile;
207         int i, type, quit;
208
209         public = options.identity_keys[idx];
210         authfile = options.identity_files[idx];
211         comment = xstrdup(authfile);
212
213         debug("Trying RSA authentication with key '%.100s'", comment);
214
215         /* Tell the server that we are willing to authenticate using this key. */
216         packet_start(SSH_CMSG_AUTH_RSA);
217         packet_put_bignum(public->rsa->n);
218         packet_send();
219         packet_write_wait();
220
221         /* Wait for server's response. */
222         type = packet_read();
223
224         /*
225          * The server responds with failure if it doesn\'t like our key or
226          * doesn\'t support RSA authentication.
227          */
228         if (type == SSH_SMSG_FAILURE) {
229                 debug("Server refused our key.");
230                 xfree(comment);
231                 return 0;
232         }
233         /* Otherwise, the server should respond with a challenge. */
234         if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
235                 packet_disconnect("Protocol error during RSA authentication: %d", type);
236
237         /* Get the challenge from the packet. */
238         if ((challenge = BN_new()) == NULL)
239                 fatal("try_rsa_authentication: BN_new failed");
240         packet_get_bignum(challenge);
241         packet_check_eom();
242
243         debug("Received RSA challenge from server.");
244
245         /*
246          * If the key is not stored in external hardware, we have to
247          * load the private key.  Try first with empty passphrase; if it
248          * fails, ask for a passphrase.
249          */
250         if (public->flags & KEY_FLAG_EXT)
251                 private = public;
252         else
253                 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
254         if (private == NULL && !options.batch_mode) {
255                 snprintf(buf, sizeof(buf),
256                     "Enter passphrase for RSA key '%.100s': ", comment);
257                 for (i = 0; i < options.number_of_password_prompts; i++) {
258                         passphrase = read_passphrase(buf, 0);
259                         if (strcmp(passphrase, "") != 0) {
260                                 private = key_load_private_type(KEY_RSA1,
261                                     authfile, passphrase, NULL);
262                                 quit = 0;
263                         } else {
264                                 debug2("no passphrase given, try next key");
265                                 quit = 1;
266                         }
267                         memset(passphrase, 0, strlen(passphrase));
268                         xfree(passphrase);
269                         if (private != NULL || quit)
270                                 break;
271                         debug2("bad passphrase given, try again...");
272                 }
273         }
274         /* We no longer need the comment. */
275         xfree(comment);
276
277         if (private == NULL) {
278                 if (!options.batch_mode)
279                         error("Bad passphrase.");
280
281                 /* Send a dummy response packet to avoid protocol error. */
282                 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
283                 for (i = 0; i < 16; i++)
284                         packet_put_char(0);
285                 packet_send();
286                 packet_write_wait();
287
288                 /* Expect the server to reject it... */
289                 packet_read_expect(SSH_SMSG_FAILURE);
290                 BN_clear_free(challenge);
291                 return 0;
292         }
293
294         /* Compute and send a response to the challenge. */
295         respond_to_rsa_challenge(challenge, private->rsa);
296
297         /* Destroy the private key unless it in external hardware. */
298         if (!(private->flags & KEY_FLAG_EXT))
299                 key_free(private);
300
301         /* We no longer need the challenge. */
302         BN_clear_free(challenge);
303
304         /* Wait for response from the server. */
305         type = packet_read();
306         if (type == SSH_SMSG_SUCCESS) {
307                 debug("RSA authentication accepted by server.");
308                 return 1;
309         }
310         if (type != SSH_SMSG_FAILURE)
311                 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
312         debug("RSA authentication refused.");
313         return 0;
314 }
315
316 /*
317  * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
318  * authentication and RSA host authentication.
319  */
320 static int
321 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
322 {
323         int type;
324         BIGNUM *challenge;
325
326         debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
327
328         /* Tell the server that we are willing to authenticate using this key. */
329         packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
330         packet_put_cstring(local_user);
331         packet_put_int(BN_num_bits(host_key->rsa->n));
332         packet_put_bignum(host_key->rsa->e);
333         packet_put_bignum(host_key->rsa->n);
334         packet_send();
335         packet_write_wait();
336
337         /* Wait for server's response. */
338         type = packet_read();
339
340         /* The server responds with failure if it doesn't admit our
341            .rhosts authentication or doesn't know our host key. */
342         if (type == SSH_SMSG_FAILURE) {
343                 debug("Server refused our rhosts authentication or host key.");
344                 return 0;
345         }
346         /* Otherwise, the server should respond with a challenge. */
347         if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
348                 packet_disconnect("Protocol error during RSA authentication: %d", type);
349
350         /* Get the challenge from the packet. */
351         if ((challenge = BN_new()) == NULL)
352                 fatal("try_rhosts_rsa_authentication: BN_new failed");
353         packet_get_bignum(challenge);
354         packet_check_eom();
355
356         debug("Received RSA challenge for host key from server.");
357
358         /* Compute a response to the challenge. */
359         respond_to_rsa_challenge(challenge, host_key->rsa);
360
361         /* We no longer need the challenge. */
362         BN_clear_free(challenge);
363
364         /* Wait for response from the server. */
365         type = packet_read();
366         if (type == SSH_SMSG_SUCCESS) {
367                 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
368                 return 1;
369         }
370         if (type != SSH_SMSG_FAILURE)
371                 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
372         debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
373         return 0;
374 }
375
376 #ifdef KRB5
377 static int
378 try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
379 {
380         krb5_error_code problem;
381         const char *tkfile;
382         struct stat buf;
383         krb5_ccache ccache = NULL;
384         const char *remotehost;
385         krb5_data ap;
386         int type;
387         krb5_ap_rep_enc_part *reply = NULL;
388         int ret;
389
390         memset(&ap, 0, sizeof(ap));
391
392         problem = krb5_init_context(context);
393         if (problem) {
394                 debug("Kerberos v5: krb5_init_context failed");
395                 ret = 0;
396                 goto out;
397         }
398         
399         problem = krb5_auth_con_init(*context, auth_context);
400         if (problem) {
401                 debug("Kerberos v5: krb5_auth_con_init failed");
402                 ret = 0;
403                 goto out;
404         }
405
406 #ifndef HEIMDAL
407         problem = krb5_auth_con_setflags(*context, *auth_context,
408                                          KRB5_AUTH_CONTEXT_RET_TIME);
409         if (problem) {
410                 debug("Keberos v5: krb5_auth_con_setflags failed");
411                 ret = 0;
412                 goto out;
413         }
414 #endif
415
416         tkfile = krb5_cc_default_name(*context);
417         if (strncmp(tkfile, "FILE:", 5) == 0)
418                 tkfile += 5;
419
420         if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
421                 debug("Kerberos v5: could not get default ccache (permission denied).");
422                 ret = 0;
423                 goto out;
424         }
425
426         problem = krb5_cc_default(*context, &ccache);
427         if (problem) {
428                 debug("Kerberos v5: krb5_cc_default failed: %s",
429                     krb5_get_err_text(*context, problem));
430                 ret = 0;
431                 goto out;
432         }
433
434         remotehost = get_canonical_hostname(1);
435
436         problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
437             "host", remotehost, NULL, ccache, &ap);
438         if (problem) {
439                 debug("Kerberos v5: krb5_mk_req failed: %s",
440                     krb5_get_err_text(*context, problem));
441                 ret = 0;
442                 goto out;
443         }
444
445         packet_start(SSH_CMSG_AUTH_KERBEROS);
446         packet_put_string((char *) ap.data, ap.length);
447         packet_send();
448         packet_write_wait();
449
450         xfree(ap.data);
451         ap.length = 0;
452
453         type = packet_read();
454         switch (type) {
455         case SSH_SMSG_FAILURE:
456                 /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
457                 debug("Kerberos v5 authentication failed.");
458                 ret = 0;
459                 break;
460
461         case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
462                 /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
463                 debug("Kerberos v5 authentication accepted.");
464
465                 /* Get server's response. */
466                 ap.data = packet_get_string((unsigned int *) &ap.length);
467                 packet_check_eom();
468                 /* XXX je to dobre? */
469
470                 problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
471                 if (problem) {
472                         ret = 0;
473                 }
474                 ret = 1;
475                 break;
476
477         default:
478                 packet_disconnect("Protocol error on Kerberos v5 response: %d",
479                     type);
480                 ret = 0;
481                 break;
482
483         }
484
485  out:
486         if (ccache != NULL)
487                 krb5_cc_close(*context, ccache);
488         if (reply != NULL)
489                 krb5_free_ap_rep_enc_part(*context, reply);
490         if (ap.length > 0)
491 #ifdef HEIMDAL
492                 krb5_data_free(&ap);
493 #else
494                 krb5_free_data_contents(*context, &ap);
495 #endif
496
497         return (ret);
498 }
499
500 static void
501 send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
502 {
503         int fd, type;
504         krb5_error_code problem;
505         krb5_data outbuf;
506         krb5_ccache ccache = NULL;
507         krb5_creds creds;
508 #ifdef HEIMDAL
509         krb5_kdc_flags flags;
510 #else
511         int forwardable;
512 #endif
513         const char *remotehost;
514
515         memset(&creds, 0, sizeof(creds));
516         memset(&outbuf, 0, sizeof(outbuf));
517
518         fd = packet_get_connection_in();
519
520 #ifdef HEIMDAL
521         problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
522 #else
523         problem = krb5_auth_con_genaddrs(context, auth_context, fd,
524                         KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
525                         KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
526 #endif
527         if (problem)
528                 goto out;
529
530         problem = krb5_cc_default(context, &ccache);
531         if (problem)
532                 goto out;
533
534         problem = krb5_cc_get_principal(context, ccache, &creds.client);
535         if (problem)
536                 goto out;
537
538         remotehost = get_canonical_hostname(1);
539         
540 #ifdef HEIMDAL
541         problem = krb5_build_principal(context, &creds.server,
542             strlen(creds.client->realm), creds.client->realm,
543             "krbtgt", creds.client->realm, NULL);
544 #else
545         problem = krb5_build_principal(context, &creds.server,
546             creds.client->realm.length, creds.client->realm.data,
547             "host", remotehost, NULL);
548 #endif
549         if (problem)
550                 goto out;
551
552         creds.times.endtime = 0;
553
554 #ifdef HEIMDAL
555         flags.i = 0;
556         flags.b.forwarded = 1;
557         flags.b.forwardable = krb5_config_get_bool(context,  NULL,
558             "libdefaults", "forwardable", NULL);
559         problem = krb5_get_forwarded_creds(context, auth_context,
560             ccache, flags.i, remotehost, &creds, &outbuf);
561 #else
562         forwardable = 1;
563         problem = krb5_fwd_tgt_creds(context, auth_context, remotehost,
564             creds.client, creds.server, ccache, forwardable, &outbuf);
565 #endif
566
567         if (problem)
568                 goto out;
569
570         packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
571         packet_put_string((char *)outbuf.data, outbuf.length);
572         packet_send();
573         packet_write_wait();
574
575         type = packet_read();
576
577         if (type == SSH_SMSG_SUCCESS) {
578                 char *pname;
579
580                 krb5_unparse_name(context, creds.client, &pname);
581                 debug("Kerberos v5 TGT forwarded (%s).", pname);
582                 xfree(pname);
583         } else
584                 debug("Kerberos v5 TGT forwarding failed.");
585
586         return;
587
588  out:
589         if (problem)
590                 debug("Kerberos v5 TGT forwarding failed: %s",
591                     krb5_get_err_text(context, problem));
592         if (creds.client)
593                 krb5_free_principal(context, creds.client);
594         if (creds.server)
595                 krb5_free_principal(context, creds.server);
596         if (ccache)
597                 krb5_cc_close(context, ccache);
598         if (outbuf.data)
599                 xfree(outbuf.data);
600 }
601 #endif /* KRB5 */
602
603 /*
604  * Tries to authenticate with any string-based challenge/response system.
605  * Note that the client code is not tied to s/key or TIS.
606  */
607 static int
608 try_challenge_response_authentication(void)
609 {
610         int type, i;
611         u_int clen;
612         char prompt[1024];
613         char *challenge, *response;
614
615         debug("Doing challenge response authentication.");
616
617         for (i = 0; i < options.number_of_password_prompts; i++) {
618                 /* request a challenge */
619                 packet_start(SSH_CMSG_AUTH_TIS);
620                 packet_send();
621                 packet_write_wait();
622
623                 type = packet_read();
624                 if (type != SSH_SMSG_FAILURE &&
625                     type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
626                         packet_disconnect("Protocol error: got %d in response "
627                             "to SSH_CMSG_AUTH_TIS", type);
628                 }
629                 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
630                         debug("No challenge.");
631                         return 0;
632                 }
633                 challenge = packet_get_string(&clen);
634                 packet_check_eom();
635                 snprintf(prompt, sizeof prompt, "%s%s", challenge,
636                     strchr(challenge, '\n') ? "" : "\nResponse: ");
637                 xfree(challenge);
638                 if (i != 0)
639                         error("Permission denied, please try again.");
640                 if (options.cipher == SSH_CIPHER_NONE)
641                         logit("WARNING: Encryption is disabled! "
642                             "Response will be transmitted in clear text.");
643                 response = read_passphrase(prompt, 0);
644                 if (strcmp(response, "") == 0) {
645                         xfree(response);
646                         break;
647                 }
648                 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
649                 ssh_put_password(response);
650                 memset(response, 0, strlen(response));
651                 xfree(response);
652                 packet_send();
653                 packet_write_wait();
654                 type = packet_read();
655                 if (type == SSH_SMSG_SUCCESS)
656                         return 1;
657                 if (type != SSH_SMSG_FAILURE)
658                         packet_disconnect("Protocol error: got %d in response "
659                             "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
660         }
661         /* failure */
662         return 0;
663 }
664
665 /*
666  * Tries to authenticate with plain passwd authentication.
667  */
668 static int
669 try_password_authentication(char *prompt)
670 {
671         int type, i;
672         char *password;
673
674         debug("Doing password authentication.");
675         if (options.cipher == SSH_CIPHER_NONE)
676                 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
677         for (i = 0; i < options.number_of_password_prompts; i++) {
678                 if (i != 0)
679                         error("Permission denied, please try again.");
680                 password = read_passphrase(prompt, 0);
681                 packet_start(SSH_CMSG_AUTH_PASSWORD);
682                 ssh_put_password(password);
683                 memset(password, 0, strlen(password));
684                 xfree(password);
685                 packet_send();
686                 packet_write_wait();
687
688                 type = packet_read();
689                 if (type == SSH_SMSG_SUCCESS)
690                         return 1;
691                 if (type != SSH_SMSG_FAILURE)
692                         packet_disconnect("Protocol error: got %d in response to passwd auth", type);
693         }
694         /* failure */
695         return 0;
696 }
697
698 /*
699  * SSH1 key exchange
700  */
701 void
702 ssh_kex(char *host, struct sockaddr *hostaddr)
703 {
704         int i;
705         BIGNUM *key;
706         Key *host_key, *server_key;
707         int bits, rbits;
708         int ssh_cipher_default = SSH_CIPHER_3DES;
709         u_char session_key[SSH_SESSION_KEY_LENGTH];
710         u_char cookie[8];
711         u_int supported_ciphers;
712         u_int server_flags, client_flags;
713         u_int32_t rand = 0;
714
715         debug("Waiting for server public key.");
716
717         /* Wait for a public key packet from the server. */
718         packet_read_expect(SSH_SMSG_PUBLIC_KEY);
719
720         /* Get cookie from the packet. */
721         for (i = 0; i < 8; i++)
722                 cookie[i] = packet_get_char();
723
724         /* Get the public key. */
725         server_key = key_new(KEY_RSA1);
726         bits = packet_get_int();
727         packet_get_bignum(server_key->rsa->e);
728         packet_get_bignum(server_key->rsa->n);
729
730         rbits = BN_num_bits(server_key->rsa->n);
731         if (bits != rbits) {
732                 logit("Warning: Server lies about size of server public key: "
733                     "actual size is %d bits vs. announced %d.", rbits, bits);
734                 logit("Warning: This may be due to an old implementation of ssh.");
735         }
736         /* Get the host key. */
737         host_key = key_new(KEY_RSA1);
738         bits = packet_get_int();
739         packet_get_bignum(host_key->rsa->e);
740         packet_get_bignum(host_key->rsa->n);
741
742         rbits = BN_num_bits(host_key->rsa->n);
743         if (bits != rbits) {
744                 logit("Warning: Server lies about size of server host key: "
745                     "actual size is %d bits vs. announced %d.", rbits, bits);
746                 logit("Warning: This may be due to an old implementation of ssh.");
747         }
748
749         /* Get protocol flags. */
750         server_flags = packet_get_int();
751         packet_set_protocol_flags(server_flags);
752
753         supported_ciphers = packet_get_int();
754         supported_authentications = packet_get_int();
755         packet_check_eom();
756
757         debug("Received server public key (%d bits) and host key (%d bits).",
758             BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
759
760         if (verify_host_key(host, hostaddr, host_key) == -1)
761                 fatal("Host key verification failed.");
762
763         client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
764
765         compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
766
767         /* Generate a session key. */
768         arc4random_stir();
769
770         /*
771          * Generate an encryption key for the session.   The key is a 256 bit
772          * random number, interpreted as a 32-byte key, with the least
773          * significant 8 bits being the first byte of the key.
774          */
775         for (i = 0; i < 32; i++) {
776                 if (i % 4 == 0)
777                         rand = arc4random();
778                 session_key[i] = rand & 0xff;
779                 rand >>= 8;
780         }
781
782         /*
783          * According to the protocol spec, the first byte of the session key
784          * is the highest byte of the integer.  The session key is xored with
785          * the first 16 bytes of the session id.
786          */
787         if ((key = BN_new()) == NULL)
788                 fatal("respond_to_rsa_challenge: BN_new failed");
789         BN_set_word(key, 0);
790         for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
791                 BN_lshift(key, key, 8);
792                 if (i < 16)
793                         BN_add_word(key, session_key[i] ^ session_id[i]);
794                 else
795                         BN_add_word(key, session_key[i]);
796         }
797
798         /*
799          * Encrypt the integer using the public key and host key of the
800          * server (key with smaller modulus first).
801          */
802         if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
803                 /* Public key has smaller modulus. */
804                 if (BN_num_bits(host_key->rsa->n) <
805                     BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
806                         fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
807                             "SSH_KEY_BITS_RESERVED %d",
808                             BN_num_bits(host_key->rsa->n),
809                             BN_num_bits(server_key->rsa->n),
810                             SSH_KEY_BITS_RESERVED);
811                 }
812                 rsa_public_encrypt(key, key, server_key->rsa);
813                 rsa_public_encrypt(key, key, host_key->rsa);
814         } else {
815                 /* Host key has smaller modulus (or they are equal). */
816                 if (BN_num_bits(server_key->rsa->n) <
817                     BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
818                         fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
819                             "SSH_KEY_BITS_RESERVED %d",
820                             BN_num_bits(server_key->rsa->n),
821                             BN_num_bits(host_key->rsa->n),
822                             SSH_KEY_BITS_RESERVED);
823                 }
824                 rsa_public_encrypt(key, key, host_key->rsa);
825                 rsa_public_encrypt(key, key, server_key->rsa);
826         }
827
828         /* Destroy the public keys since we no longer need them. */
829         key_free(server_key);
830         key_free(host_key);
831
832         if (options.cipher == SSH_CIPHER_NOT_SET) {
833                 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
834                         options.cipher = ssh_cipher_default;
835         } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
836             !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
837                 logit("No valid SSH1 cipher, using %.100s instead.",
838                     cipher_name(ssh_cipher_default));
839                 options.cipher = ssh_cipher_default;
840         }
841         /* Check that the selected cipher is supported. */
842         if (!(supported_ciphers & (1 << options.cipher)))
843                 fatal("Selected cipher type %.100s not supported by server.",
844                     cipher_name(options.cipher));
845
846         debug("Encryption type: %.100s", cipher_name(options.cipher));
847
848         /* Send the encrypted session key to the server. */
849         packet_start(SSH_CMSG_SESSION_KEY);
850         packet_put_char(options.cipher);
851
852         /* Send the cookie back to the server. */
853         for (i = 0; i < 8; i++)
854                 packet_put_char(cookie[i]);
855
856         /* Send and destroy the encrypted encryption key integer. */
857         packet_put_bignum(key);
858         BN_clear_free(key);
859
860         /* Send protocol flags. */
861         packet_put_int(client_flags);
862
863         /* Send the packet now. */
864         packet_send();
865         packet_write_wait();
866
867         debug("Sent encrypted session key.");
868
869         /* Set the encryption key. */
870         packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
871
872         /* We will no longer need the session key here.  Destroy any extra copies. */
873         memset(session_key, 0, sizeof(session_key));
874
875         /*
876          * Expect a success message from the server.  Note that this message
877          * will be received in encrypted form.
878          */
879         packet_read_expect(SSH_SMSG_SUCCESS);
880
881         debug("Received encrypted confirmation.");
882 }
883
884 /*
885  * Authenticate user
886  */
887 void
888 ssh_userauth1(const char *local_user, const char *server_user, char *host,
889     Sensitive *sensitive)
890 {
891 #ifdef KRB5
892         krb5_context context = NULL;
893         krb5_auth_context auth_context = NULL;
894 #endif
895         int i, type;
896
897         if (supported_authentications == 0)
898                 fatal("ssh_userauth1: server supports no auth methods");
899
900         /* Send the name of the user to log in as on the server. */
901         packet_start(SSH_CMSG_USER);
902         packet_put_cstring(server_user);
903         packet_send();
904         packet_write_wait();
905
906         /*
907          * The server should respond with success if no authentication is
908          * needed (the user has no password).  Otherwise the server responds
909          * with failure.
910          */
911         type = packet_read();
912
913         /* check whether the connection was accepted without authentication. */
914         if (type == SSH_SMSG_SUCCESS)
915                 goto success;
916         if (type != SSH_SMSG_FAILURE)
917                 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
918
919 #ifdef KRB5
920         if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
921             options.kerberos_authentication) {
922                 debug("Trying Kerberos v5 authentication.");
923
924                 if (try_krb5_authentication(&context, &auth_context)) {
925                         type = packet_read();
926                         if (type == SSH_SMSG_SUCCESS)
927                                 goto success;
928                         if (type != SSH_SMSG_FAILURE)
929                                 packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
930                 }
931         }
932 #endif /* KRB5 */
933
934         /*
935          * Use rhosts authentication if running in privileged socket and we
936          * do not wish to remain anonymous.
937          */
938         if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
939             options.rhosts_authentication) {
940                 debug("Trying rhosts authentication.");
941                 packet_start(SSH_CMSG_AUTH_RHOSTS);
942                 packet_put_cstring(local_user);
943                 packet_send();
944                 packet_write_wait();
945
946                 /* The server should respond with success or failure. */
947                 type = packet_read();
948                 if (type == SSH_SMSG_SUCCESS)
949                         goto success;
950                 if (type != SSH_SMSG_FAILURE)
951                         packet_disconnect("Protocol error: got %d in response to rhosts auth",
952                                           type);
953         }
954         /*
955          * Try .rhosts or /etc/hosts.equiv authentication with RSA host
956          * authentication.
957          */
958         if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
959             options.rhosts_rsa_authentication) {
960                 for (i = 0; i < sensitive->nkeys; i++) {
961                         if (sensitive->keys[i] != NULL &&
962                             sensitive->keys[i]->type == KEY_RSA1 &&
963                             try_rhosts_rsa_authentication(local_user,
964                             sensitive->keys[i]))
965                                 goto success;
966                 }
967         }
968         /* Try RSA authentication if the server supports it. */
969         if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
970             options.rsa_authentication) {
971                 /*
972                  * Try RSA authentication using the authentication agent. The
973                  * agent is tried first because no passphrase is needed for
974                  * it, whereas identity files may require passphrases.
975                  */
976                 if (try_agent_authentication())
977                         goto success;
978
979                 /* Try RSA authentication for each identity. */
980                 for (i = 0; i < options.num_identity_files; i++)
981                         if (options.identity_keys[i] != NULL &&
982                             options.identity_keys[i]->type == KEY_RSA1 &&
983                             try_rsa_authentication(i))
984                                 goto success;
985         }
986         /* Try challenge response authentication if the server supports it. */
987         if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
988             options.challenge_response_authentication && !options.batch_mode) {
989                 if (try_challenge_response_authentication())
990                         goto success;
991         }
992         /* Try password authentication if the server supports it. */
993         if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
994             options.password_authentication && !options.batch_mode) {
995                 char prompt[80];
996
997                 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
998                     server_user, host);
999                 if (try_password_authentication(prompt))
1000                         goto success;
1001         }
1002         /* All authentication methods have failed.  Exit with an error message. */
1003         fatal("Permission denied.");
1004         /* NOTREACHED */
1005
1006  success:
1007 #ifdef KRB5
1008         /* Try Kerberos v5 TGT passing. */
1009         if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
1010             options.kerberos_tgt_passing && context && auth_context) {
1011                 if (options.cipher == SSH_CIPHER_NONE)
1012                         logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
1013                 send_krb5_tgt(context, auth_context);
1014         }
1015         if (auth_context)
1016                 krb5_auth_con_free(context, auth_context);
1017         if (context)
1018                 krb5_free_context(context);
1019 #endif
1020         return; /* need statement after label */
1021 }
This page took 0.127822 seconds and 5 git commands to generate.