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