blob: da8c8229ca0ef6f01fb90bbd65429c62684ef0dd [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.57 2001/03/27 17:46:49 provos Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100029#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "uidswap.h"
40#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110048#include "cli.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000049#include "dh.h"
Damien Miller62cee002000-09-23 17:15:56 +110050#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100051#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
53#include "readconf.h"
54#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000055#include "match.h"
Damien Millereba71ba2000-04-29 23:57:08 +100056
Damien Miller874d77b2000-10-14 16:23:11 +110057void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
58void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
59
Damien Millereba71ba2000-04-29 23:57:08 +100060/* import */
61extern char *client_version_string;
62extern char *server_version_string;
63extern Options options;
64
65/*
66 * SSH2 key exchange
67 */
68
Ben Lindstrom46c16222000-12-22 01:43:59 +000069u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100070int session_id2_len = 0;
71
72void
Damien Miller874d77b2000-10-14 16:23:11 +110073ssh_kex2(char *host, struct sockaddr *hostaddr)
74{
75 int i, plen;
76 Kex *kex;
77 Buffer *client_kexinit, *server_kexinit;
78 char *sprop[PROPOSAL_MAX];
79
Damien Millere39cacc2000-11-29 12:18:44 +110080 if (options.ciphers == (char *)-1) {
81 log("No valid ciphers for protocol version 2 given, using defaults.");
82 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110083 }
84 if (options.ciphers != NULL) {
85 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
86 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
87 }
88 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000089 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110090 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
91 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000092 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110093 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
94 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000095 if (options.macs != NULL) {
96 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
97 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
98 }
Damien Miller874d77b2000-10-14 16:23:11 +110099
Ben Lindstromc8530c72001-03-24 00:35:19 +0000100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
102
Damien Miller874d77b2000-10-14 16:23:11 +1100103 /* buffers with raw kexinit messages */
104 server_kexinit = xmalloc(sizeof(*server_kexinit));
105 buffer_init(server_kexinit);
106 client_kexinit = kex_init(myproposal);
107
108 /* algorithm negotiation */
109 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
110 kex = kex_choose_conf(myproposal, sprop, 0);
111 for (i = 0; i < PROPOSAL_MAX; i++)
112 xfree(sprop[i]);
113
114 /* server authentication and session key agreement */
115 switch(kex->kex_type) {
116 case DH_GRP1_SHA1:
117 ssh_dh1_client(kex, host, hostaddr,
118 client_kexinit, server_kexinit);
119 break;
120 case DH_GEX_SHA1:
121 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
122 server_kexinit);
123 break;
124 default:
125 fatal("Unsupported key exchange %d", kex->kex_type);
126 }
127
128 buffer_free(client_kexinit);
129 buffer_free(server_kexinit);
130 xfree(client_kexinit);
131 xfree(server_kexinit);
132
133 debug("Wait SSH2_MSG_NEWKEYS.");
134 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
135 packet_done();
136 debug("GOT SSH2_MSG_NEWKEYS.");
137
138 debug("send SSH2_MSG_NEWKEYS.");
139 packet_start(SSH2_MSG_NEWKEYS);
140 packet_send();
141 packet_write_wait();
142 debug("done: send SSH2_MSG_NEWKEYS.");
143
144#ifdef DEBUG_KEXDH
145 /* send 1st encrypted/maced/compressed message */
146 packet_start(SSH2_MSG_IGNORE);
147 packet_put_cstring("markus");
148 packet_send();
149 packet_write_wait();
150#endif
151 debug("done: KEX2.");
152}
153
154/* diffie-hellman-group1-sha1 */
155
156void
Kevin Stevesef4eea92001-02-05 12:42:17 +0000157ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
Damien Miller874d77b2000-10-14 16:23:11 +1100158 Buffer *client_kexinit, Buffer *server_kexinit)
Damien Millereba71ba2000-04-29 23:57:08 +1000159{
Damien Miller62cee002000-09-23 17:15:56 +1100160#ifdef DEBUG_KEXDH
161 int i;
162#endif
Damien Millerb1715dc2000-05-30 13:44:51 +1000163 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000164 u_int klen, kout;
Damien Millereba71ba2000-04-29 23:57:08 +1000165 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000166 u_int slen;
Damien Millereba71ba2000-04-29 23:57:08 +1000167 char *server_host_key_blob = NULL;
168 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000169 u_int sbloblen;
Damien Millereba71ba2000-04-29 23:57:08 +1000170 DH *dh;
171 BIGNUM *dh_server_pub = 0;
172 BIGNUM *shared_secret = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000173 u_char *kbuf;
174 u_char *hash;
Damien Millereba71ba2000-04-29 23:57:08 +1000175
Damien Millereba71ba2000-04-29 23:57:08 +1000176 debug("Sending SSH2_MSG_KEXDH_INIT.");
Damien Millereba71ba2000-04-29 23:57:08 +1000177 /* generate and send 'e', client DH public key */
178 dh = dh_new_group1();
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000179 dh_gen_key(dh, kex->we_need * 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000180 packet_start(SSH2_MSG_KEXDH_INIT);
181 packet_put_bignum2(dh->pub_key);
182 packet_send();
183 packet_write_wait();
184
185#ifdef DEBUG_KEXDH
186 fprintf(stderr, "\np= ");
Damien Miller62cee002000-09-23 17:15:56 +1100187 BN_print_fp(stderr, dh->p);
Damien Millereba71ba2000-04-29 23:57:08 +1000188 fprintf(stderr, "\ng= ");
Damien Miller62cee002000-09-23 17:15:56 +1100189 BN_print_fp(stderr, dh->g);
Damien Millereba71ba2000-04-29 23:57:08 +1000190 fprintf(stderr, "\npub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100191 BN_print_fp(stderr, dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000192 fprintf(stderr, "\n");
193 DHparams_print_fp(stderr, dh);
194#endif
195
196 debug("Wait SSH2_MSG_KEXDH_REPLY.");
197
Damien Millerb1715dc2000-05-30 13:44:51 +1000198 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
Damien Millereba71ba2000-04-29 23:57:08 +1000199
200 debug("Got SSH2_MSG_KEXDH_REPLY.");
201
202 /* key, cert */
203 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100204 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000205 if (server_host_key == NULL)
206 fatal("cannot decode server_host_key_blob");
207
208 check_host_key(host, hostaddr, server_host_key,
Damien Miller874d77b2000-10-14 16:23:11 +1100209 options.user_hostfile2, options.system_hostfile2);
Damien Millereba71ba2000-04-29 23:57:08 +1000210
211 /* DH paramter f, server public DH key */
212 dh_server_pub = BN_new();
213 if (dh_server_pub == NULL)
214 fatal("dh_server_pub == NULL");
215 packet_get_bignum2(dh_server_pub, &dlen);
216
217#ifdef DEBUG_KEXDH
218 fprintf(stderr, "\ndh_server_pub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100219 BN_print_fp(stderr, dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000220 fprintf(stderr, "\n");
221 debug("bits %d", BN_num_bits(dh_server_pub));
222#endif
223
224 /* signed H */
225 signature = packet_get_string(&slen);
226 packet_done();
227
228 if (!dh_pub_is_valid(dh, dh_server_pub))
229 packet_disconnect("bad server public DH value");
230
231 klen = DH_size(dh);
232 kbuf = xmalloc(klen);
233 kout = DH_compute_key(kbuf, dh_server_pub, dh);
234#ifdef DEBUG_KEXDH
235 debug("shared secret: len %d/%d", klen, kout);
236 fprintf(stderr, "shared secret == ");
237 for (i = 0; i< kout; i++)
238 fprintf(stderr, "%02x", (kbuf[i])&0xff);
239 fprintf(stderr, "\n");
240#endif
241 shared_secret = BN_new();
242
243 BN_bin2bn(kbuf, kout, shared_secret);
244 memset(kbuf, 0, klen);
245 xfree(kbuf);
246
247 /* calc and verify H */
248 hash = kex_hash(
249 client_version_string,
250 server_version_string,
251 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
252 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
253 server_host_key_blob, sbloblen,
254 dh->pub_key,
255 dh_server_pub,
256 shared_secret
257 );
258 xfree(server_host_key_blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000259 DH_free(dh);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000260 BN_free(dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000261#ifdef DEBUG_KEXDH
262 fprintf(stderr, "hash == ");
263 for (i = 0; i< 20; i++)
264 fprintf(stderr, "%02x", (hash[i])&0xff);
265 fprintf(stderr, "\n");
266#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000267 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100268 fatal("key_verify failed for server_host_key");
Damien Millereba71ba2000-04-29 23:57:08 +1000269 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000270 xfree(signature);
Damien Millereba71ba2000-04-29 23:57:08 +1000271
272 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000273 BN_clear_free(shared_secret);
Damien Millereba71ba2000-04-29 23:57:08 +1000274 packet_set_kex(kex);
275
Damien Millereba71ba2000-04-29 23:57:08 +1000276 /* save session id */
277 session_id2_len = 20;
278 session_id2 = xmalloc(session_id2_len);
279 memcpy(session_id2, hash, session_id2_len);
Damien Millerb1715dc2000-05-30 13:44:51 +1000280}
281
Damien Miller874d77b2000-10-14 16:23:11 +1100282/* diffie-hellman-group-exchange-sha1 */
283
284/*
285 * Estimates the group order for a Diffie-Hellman group that has an
286 * attack complexity approximately the same as O(2**bits). Estimate
287 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
288 */
289
290int
291dh_estimate(int bits)
Damien Millerb1715dc2000-05-30 13:44:51 +1000292{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000293
Damien Miller874d77b2000-10-14 16:23:11 +1100294 if (bits < 64)
295 return (512); /* O(2**63) */
296 if (bits < 128)
297 return (1024); /* O(2**86) */
298 if (bits < 192)
299 return (2048); /* O(2**116) */
300 return (4096); /* O(2**156) */
301}
Damien Millerb1715dc2000-05-30 13:44:51 +1000302
Damien Miller874d77b2000-10-14 16:23:11 +1100303void
304ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
305 Buffer *client_kexinit, Buffer *server_kexinit)
306{
307#ifdef DEBUG_KEXDH
308 int i;
309#endif
310 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000311 u_int klen, kout;
Damien Miller874d77b2000-10-14 16:23:11 +1100312 char *signature = NULL;
Ben Lindstromdf221392001-03-29 00:36:16 +0000313 u_int slen, nbits, min, max;
Damien Miller874d77b2000-10-14 16:23:11 +1100314 char *server_host_key_blob = NULL;
315 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000316 u_int sbloblen;
Damien Miller874d77b2000-10-14 16:23:11 +1100317 DH *dh;
318 BIGNUM *dh_server_pub = 0;
319 BIGNUM *shared_secret = 0;
320 BIGNUM *p = 0, *g = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000321 u_char *kbuf;
322 u_char *hash;
Damien Millerb1715dc2000-05-30 13:44:51 +1000323
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000324 nbits = dh_estimate(kex->we_need * 8);
Damien Millerb1715dc2000-05-30 13:44:51 +1000325
Ben Lindstromdf221392001-03-29 00:36:16 +0000326 if (datafellows & SSH_OLD_DHGEX) {
327 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST_OLD.");
328
329 /* Old GEX request */
330 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
331 packet_put_int(nbits);
332 min = DH_GRP_MIN;
333 max = DH_GRP_MAX;
334 } else {
335 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
336
337 /* New GEX request */
338 min = DH_GRP_MIN;
339 max = MIN(DH_GRP_MAX, nbits * 1.25);
340
341 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
342 packet_put_int(min);
343 packet_put_int(nbits);
344 packet_put_int(max);
345 }
Damien Millereba71ba2000-04-29 23:57:08 +1000346 packet_send();
347 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000348
349#ifdef DEBUG_KEXDH
Ben Lindstromdf221392001-03-29 00:36:16 +0000350 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d", min, nbits, max);
Damien Miller874d77b2000-10-14 16:23:11 +1100351#endif
352
353 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
354
355 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
356
357 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
358
359 if ((p = BN_new()) == NULL)
360 fatal("BN_new");
361 packet_get_bignum2(p, &dlen);
362 if ((g = BN_new()) == NULL)
363 fatal("BN_new");
364 packet_get_bignum2(g, &dlen);
Ben Lindstromdf221392001-03-29 00:36:16 +0000365
366 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
367 fatal("DH_GEX group out of range: %d !< %d !< %d",
368 min, BN_num_bits(p), max);
369
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000370 dh = dh_new_group(g, p);
Damien Miller874d77b2000-10-14 16:23:11 +1100371
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000372 dh_gen_key(dh, kex->we_need * 8);
Kevin Steves6b875862000-12-15 23:31:01 +0000373
Damien Miller874d77b2000-10-14 16:23:11 +1100374#ifdef DEBUG_KEXDH
375 fprintf(stderr, "\np= ");
376 BN_print_fp(stderr, dh->p);
377 fprintf(stderr, "\ng= ");
378 BN_print_fp(stderr, dh->g);
379 fprintf(stderr, "\npub= ");
380 BN_print_fp(stderr, dh->pub_key);
381 fprintf(stderr, "\n");
382 DHparams_print_fp(stderr, dh);
383#endif
384
385 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
386 /* generate and send 'e', client DH public key */
387 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
388 packet_put_bignum2(dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000389 packet_send();
390 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100391
392 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
393
394 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
395
396 debug("Got SSH2_MSG_KEXDH_REPLY.");
397
398 /* key, cert */
399 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100400 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Miller874d77b2000-10-14 16:23:11 +1100401 if (server_host_key == NULL)
402 fatal("cannot decode server_host_key_blob");
403
404 check_host_key(host, hostaddr, server_host_key,
405 options.user_hostfile2, options.system_hostfile2);
406
407 /* DH paramter f, server public DH key */
408 dh_server_pub = BN_new();
409 if (dh_server_pub == NULL)
410 fatal("dh_server_pub == NULL");
411 packet_get_bignum2(dh_server_pub, &dlen);
412
413#ifdef DEBUG_KEXDH
414 fprintf(stderr, "\ndh_server_pub= ");
415 BN_print_fp(stderr, dh_server_pub);
416 fprintf(stderr, "\n");
417 debug("bits %d", BN_num_bits(dh_server_pub));
Damien Millereba71ba2000-04-29 23:57:08 +1000418#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100419
420 /* signed H */
421 signature = packet_get_string(&slen);
422 packet_done();
423
424 if (!dh_pub_is_valid(dh, dh_server_pub))
425 packet_disconnect("bad server public DH value");
426
427 klen = DH_size(dh);
428 kbuf = xmalloc(klen);
429 kout = DH_compute_key(kbuf, dh_server_pub, dh);
430#ifdef DEBUG_KEXDH
431 debug("shared secret: len %d/%d", klen, kout);
432 fprintf(stderr, "shared secret == ");
433 for (i = 0; i< kout; i++)
434 fprintf(stderr, "%02x", (kbuf[i])&0xff);
435 fprintf(stderr, "\n");
436#endif
437 shared_secret = BN_new();
438
439 BN_bin2bn(kbuf, kout, shared_secret);
440 memset(kbuf, 0, klen);
441 xfree(kbuf);
442
443 /* calc and verify H */
444 hash = kex_hash_gex(
445 client_version_string,
446 server_version_string,
447 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
448 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
449 server_host_key_blob, sbloblen,
Kevin Stevesef4eea92001-02-05 12:42:17 +0000450 nbits, dh->p, dh->g,
Damien Miller874d77b2000-10-14 16:23:11 +1100451 dh->pub_key,
452 dh_server_pub,
453 shared_secret
454 );
455 xfree(server_host_key_blob);
456 DH_free(dh);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000457 BN_free(dh_server_pub);
Damien Miller874d77b2000-10-14 16:23:11 +1100458#ifdef DEBUG_KEXDH
459 fprintf(stderr, "hash == ");
460 for (i = 0; i< 20; i++)
461 fprintf(stderr, "%02x", (hash[i])&0xff);
462 fprintf(stderr, "\n");
463#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000464 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 fatal("key_verify failed for server_host_key");
Damien Miller874d77b2000-10-14 16:23:11 +1100466 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000467 xfree(signature);
Damien Miller874d77b2000-10-14 16:23:11 +1100468
469 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000470 BN_clear_free(shared_secret);
Damien Miller874d77b2000-10-14 16:23:11 +1100471 packet_set_kex(kex);
472
473 /* save session id */
474 session_id2_len = 20;
475 session_id2 = xmalloc(session_id2_len);
476 memcpy(session_id2, hash, session_id2_len);
Damien Millereba71ba2000-04-29 23:57:08 +1000477}
Damien Millerb1715dc2000-05-30 13:44:51 +1000478
Damien Millereba71ba2000-04-29 23:57:08 +1000479/*
480 * Authenticate user
481 */
Damien Miller62cee002000-09-23 17:15:56 +1100482
483typedef struct Authctxt Authctxt;
484typedef struct Authmethod Authmethod;
485
486typedef int sign_cb_fn(
487 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000488 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100489
490struct Authctxt {
491 const char *server_user;
492 const char *host;
493 const char *service;
494 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100495 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100496 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000497 char *authlist;
498 Key *last_key;
499 sign_cb_fn *last_key_sign;
500 int last_key_hint;
Damien Miller62cee002000-09-23 17:15:56 +1100501};
502struct Authmethod {
503 char *name; /* string to compare against server's list */
504 int (*userauth)(Authctxt *authctxt);
505 int *enabled; /* flag in option struct that enables method */
506 int *batch_flag; /* flag in option struct that disables method */
507};
508
509void input_userauth_success(int type, int plen, void *ctxt);
510void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000511void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100512void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100513void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000514void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100515
516int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100517int userauth_pubkey(Authctxt *authctxt);
518int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100519int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100520
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000521void userauth(Authctxt *authctxt, char *authlist);
522
523int
524sign_and_send_pubkey(Authctxt *authctxt, Key *k,
525 sign_cb_fn *sign_callback);
526void clear_auth_state(Authctxt *authctxt);
527
Damien Miller874d77b2000-10-14 16:23:11 +1100528Authmethod *authmethod_get(char *authlist);
529Authmethod *authmethod_lookup(const char *name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000530char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100531
532Authmethod authmethods[] = {
533 {"publickey",
534 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100535 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100536 NULL},
537 {"password",
538 userauth_passwd,
539 &options.password_authentication,
540 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100541 {"keyboard-interactive",
542 userauth_kbdint,
543 &options.kbd_interactive_authentication,
544 &options.batch_mode},
545 {"none",
546 userauth_none,
547 NULL,
548 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100549 {NULL, NULL, NULL, NULL}
550};
551
552void
553ssh_userauth2(const char *server_user, char *host)
554{
555 Authctxt authctxt;
556 int type;
557 int plen;
558
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000559 if (options.challenge_reponse_authentication)
560 options.kbd_interactive_authentication = 1;
561
Damien Miller62cee002000-09-23 17:15:56 +1100562 debug("send SSH2_MSG_SERVICE_REQUEST");
563 packet_start(SSH2_MSG_SERVICE_REQUEST);
564 packet_put_cstring("ssh-userauth");
565 packet_send();
566 packet_write_wait();
567 type = packet_read(&plen);
568 if (type != SSH2_MSG_SERVICE_ACCEPT) {
569 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
570 }
571 if (packet_remaining() > 0) {
572 char *reply = packet_get_string(&plen);
573 debug("service_accept: %s", reply);
574 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100575 } else {
576 debug("buggy server: service_accept w/o service");
577 }
578 packet_done();
579 debug("got SSH2_MSG_SERVICE_ACCEPT");
580
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000581 if (options.preferred_authentications == NULL)
582 options.preferred_authentications = authmethods_get();
583
Damien Miller62cee002000-09-23 17:15:56 +1100584 /* setup authentication context */
585 authctxt.agent = ssh_get_authentication_connection();
586 authctxt.server_user = server_user;
587 authctxt.host = host;
588 authctxt.service = "ssh-connection"; /* service name */
589 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100590 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000591 authctxt.authlist = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100592 if (authctxt.method == NULL)
593 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100594
595 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100596 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100597
598 dispatch_init(&input_userauth_error);
599 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
600 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000601 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100602 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
603
604 if (authctxt.agent != NULL)
605 ssh_close_authentication_connection(authctxt.agent);
606
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000607 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100608}
609void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610userauth(Authctxt *authctxt, char *authlist)
611{
612 if (authlist == NULL) {
613 authlist = authctxt->authlist;
614 } else {
615 if (authctxt->authlist)
616 xfree(authctxt->authlist);
617 authctxt->authlist = authlist;
618 }
619 for (;;) {
620 Authmethod *method = authmethod_get(authlist);
621 if (method == NULL)
622 fatal("Permission denied (%s).", authlist);
623 authctxt->method = method;
624 if (method->userauth(authctxt) != 0) {
625 debug2("we sent a %s packet, wait for reply", method->name);
626 break;
627 } else {
628 debug2("we did not send a packet, disable method");
629 method->enabled = NULL;
630 }
631 }
632}
633void
Damien Miller62cee002000-09-23 17:15:56 +1100634input_userauth_error(int type, int plen, void *ctxt)
635{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000636 fatal("input_userauth_error: bad message during authentication: "
637 "type %d", type);
638}
639void
640input_userauth_banner(int type, int plen, void *ctxt)
641{
642 char *msg, *lang;
643 debug3("input_userauth_banner");
644 msg = packet_get_string(NULL);
645 lang = packet_get_string(NULL);
646 fprintf(stderr, "%s", msg);
647 xfree(msg);
648 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100649}
650void
651input_userauth_success(int type, int plen, void *ctxt)
652{
653 Authctxt *authctxt = ctxt;
654 if (authctxt == NULL)
655 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000656 if (authctxt->authlist)
657 xfree(authctxt->authlist);
658 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100659 authctxt->success = 1; /* break out */
660}
661void
662input_userauth_failure(int type, int plen, void *ctxt)
663{
Damien Miller62cee002000-09-23 17:15:56 +1100664 Authctxt *authctxt = ctxt;
665 char *authlist = NULL;
666 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100667
668 if (authctxt == NULL)
669 fatal("input_userauth_failure: no authentication context");
670
Damien Miller874d77b2000-10-14 16:23:11 +1100671 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100672 partial = packet_get_char();
673 packet_done();
674
675 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000676 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100677 debug("authentications that can continue: %s", authlist);
678
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000679 clear_auth_state(authctxt);
680 userauth(authctxt, authlist);
681}
682void
683input_userauth_pk_ok(int type, int plen, void *ctxt)
684{
685 Authctxt *authctxt = ctxt;
686 Key *key = NULL;
687 Buffer b;
688 int alen, blen, pktype, sent = 0;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000689 char *pkalg, *pkblob, *fp;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000690
691 if (authctxt == NULL)
692 fatal("input_userauth_pk_ok: no authentication context");
693 if (datafellows & SSH_BUG_PKOK) {
694 /* this is similar to SSH_BUG_PKAUTH */
695 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
696 pkblob = packet_get_string(&blen);
697 buffer_init(&b);
698 buffer_append(&b, pkblob, blen);
699 pkalg = buffer_get_string(&b, &alen);
700 buffer_free(&b);
701 } else {
702 pkalg = packet_get_string(&alen);
703 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000704 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000705 packet_done();
706
707 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
708 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
709
710 do {
711 if (authctxt->last_key == NULL ||
712 authctxt->last_key_sign == NULL) {
713 debug("no last key or no sign cb");
714 break;
715 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000716 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
717 debug("unknown pkalg %s", pkalg);
718 break;
719 }
720 if ((key = key_from_blob(pkblob, blen)) == NULL) {
721 debug("no key from blob. pkalg %s", pkalg);
722 break;
723 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000724 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
725 debug2("input_userauth_pk_ok: fp %s", fp);
726 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000727 if (!key_equal(key, authctxt->last_key)) {
728 debug("key != last_key");
729 break;
730 }
731 sent = sign_and_send_pubkey(authctxt, key,
732 authctxt->last_key_sign);
733 } while(0);
734
735 if (key != NULL)
736 key_free(key);
737 xfree(pkalg);
738 xfree(pkblob);
739
740 /* unregister */
741 clear_auth_state(authctxt);
742 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
743
744 /* try another method if we did not send a packet*/
745 if (sent == 0)
746 userauth(authctxt, NULL);
747
Damien Miller62cee002000-09-23 17:15:56 +1100748}
749
Damien Millereba71ba2000-04-29 23:57:08 +1000750int
Damien Miller874d77b2000-10-14 16:23:11 +1100751userauth_none(Authctxt *authctxt)
752{
753 /* initial userauth request */
754 packet_start(SSH2_MSG_USERAUTH_REQUEST);
755 packet_put_cstring(authctxt->server_user);
756 packet_put_cstring(authctxt->service);
757 packet_put_cstring(authctxt->method->name);
758 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100759 return 1;
760}
761
762int
Damien Miller62cee002000-09-23 17:15:56 +1100763userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000764{
Damien Millere247cc42000-05-07 12:03:14 +1000765 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000766 char prompt[80];
767 char *password;
768
Damien Millerd3a18572000-06-07 19:55:44 +1000769 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000770 return 0;
771
Damien Millerd3a18572000-06-07 19:55:44 +1000772 if(attempt != 1)
773 error("Permission denied, please try again.");
774
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000775 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100776 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000777 password = read_passphrase(prompt, 0);
778 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100779 packet_put_cstring(authctxt->server_user);
780 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100781 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000782 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000783 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000784 memset(password, 0, strlen(password));
785 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000786 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000787 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000788 return 1;
789}
790
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000791void
792clear_auth_state(Authctxt *authctxt)
793{
794 /* XXX clear authentication state */
795 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
796 debug3("clear_auth_state: key_free %p", authctxt->last_key);
797 key_free(authctxt->last_key);
798 }
799 authctxt->last_key = NULL;
800 authctxt->last_key_hint = -2;
801 authctxt->last_key_sign = NULL;
802}
803
Damien Millerad833b32000-08-23 10:46:23 +1000804int
Damien Miller62cee002000-09-23 17:15:56 +1100805sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000806{
807 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000808 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000809 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000810 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000811 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100812 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000813
Ben Lindstromd121f612000-12-03 17:00:47 +0000814 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000815
Damien Miller0bc1bd82000-11-13 22:57:25 +1100816 if (key_to_blob(k, &blob, &bloblen) == 0) {
817 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000818 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 return 0;
820 }
Damien Millereba71ba2000-04-29 23:57:08 +1000821 /* data to be signed */
822 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100823 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000824 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000825 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100826 } else {
827 buffer_put_string(&b, session_id2, session_id2_len);
828 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000829 }
Damien Millereba71ba2000-04-29 23:57:08 +1000830 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100831 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000832 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000833 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000834 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100835 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000836 if (datafellows & SSH_BUG_PKAUTH) {
837 buffer_put_char(&b, have_sig);
838 } else {
839 buffer_put_cstring(&b, authctxt->method->name);
840 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000841 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000842 }
Damien Millereba71ba2000-04-29 23:57:08 +1000843 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000844
845 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000846 ret = (*sign_callback)(authctxt, k, &signature, &slen,
847 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000848 if (ret == -1) {
849 xfree(blob);
850 buffer_free(&b);
851 return 0;
852 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100853#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000854 buffer_dump(&b);
855#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000856 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000857 buffer_clear(&b);
858 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000859 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000860 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100861 buffer_put_cstring(&b, authctxt->server_user);
862 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100863 buffer_put_cstring(&b, authctxt->method->name);
864 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000865 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000866 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000867 buffer_put_string(&b, blob, bloblen);
868 }
869 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000870
Damien Millereba71ba2000-04-29 23:57:08 +1000871 /* append signature */
872 buffer_put_string(&b, signature, slen);
873 xfree(signature);
874
875 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000876 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100877 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000878 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000879
880 /* put remaining data from buffer into packet */
881 packet_start(SSH2_MSG_USERAUTH_REQUEST);
882 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
883 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000884 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000885
886 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000887}
888
889int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000890send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
891 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000892{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000893 u_char *blob;
894 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000895
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000896 debug3("send_pubkey_test");
897
898 if (key_to_blob(k, &blob, &bloblen) == 0) {
899 /* we cannot handle this key */
900 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000901 return 0;
902 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000903 /* register callback for USERAUTH_PK_OK message */
904 authctxt->last_key_sign = sign_callback;
905 authctxt->last_key_hint = hint;
906 authctxt->last_key = k;
907 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000908
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000909 packet_start(SSH2_MSG_USERAUTH_REQUEST);
910 packet_put_cstring(authctxt->server_user);
911 packet_put_cstring(authctxt->service);
912 packet_put_cstring(authctxt->method->name);
913 packet_put_char(have_sig);
914 if (!(datafellows & SSH_BUG_PKAUTH))
915 packet_put_cstring(key_ssh_name(k));
916 packet_put_string(blob, bloblen);
917 xfree(blob);
918 packet_send();
919 return 1;
920}
921
922Key *
923load_identity_file(char *filename)
924{
925 Key *private;
926 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000927 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000928 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000929
Ben Lindstrom329782e2001-03-10 17:08:59 +0000930 if (stat(filename, &st) < 0) {
931 debug3("no such identity: %s", filename);
932 return NULL;
933 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000934 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
935 if (private == NULL) {
936 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000937 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000938 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100939 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100940 for (i = 0; i < options.number_of_password_prompts; i++) {
941 passphrase = read_passphrase(prompt, 0);
942 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000943 private = key_load_private_type(KEY_UNSPEC, filename,
944 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000945 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100946 } else {
947 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000948 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100949 }
950 memset(passphrase, 0, strlen(passphrase));
951 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000952 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100953 break;
954 debug2("bad passphrase given, try again...");
955 }
Damien Miller994cf142000-07-21 10:19:44 +1000956 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000957 return private;
958}
959
960int
961identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
962 u_char *data, int datalen)
963{
964 Key *private;
965 int idx, ret;
966
967 idx = authctxt->last_key_hint;
968 if (idx < 0)
969 return -1;
970 private = load_identity_file(options.identity_files[idx]);
971 if (private == NULL)
972 return -1;
973 ret = key_sign(private, sigp, lenp, data, datalen);
974 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000975 return ret;
976}
977
Ben Lindstrom46c16222000-12-22 01:43:59 +0000978int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
979 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000980{
Damien Miller62cee002000-09-23 17:15:56 +1100981 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000982}
983
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000984int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
985 u_char *data, int datalen)
986{
987 return key_sign(key, sigp, lenp, data, datalen);
988}
989
Damien Millerad833b32000-08-23 10:46:23 +1000990int
Damien Miller62cee002000-09-23 17:15:56 +1100991userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000992{
993 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100994 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000995 char *comment;
996 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000997
998 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100999 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1000 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +11001001 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +10001002 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001003 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +11001004 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001005 debug2("userauth_pubkey_agent: no more keys");
1006 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001007 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001008 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001009 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1010 if (ret == 0)
1011 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +11001012 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001013 if (ret == 0)
1014 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +10001015 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +10001016}
1017
Damien Miller62cee002000-09-23 17:15:56 +11001018int
1019userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001020{
Damien Miller62cee002000-09-23 17:15:56 +11001021 static int idx = 0;
1022 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001023 Key *key;
1024 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +10001025
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 if (authctxt->agent != NULL) {
1027 do {
1028 sent = userauth_pubkey_agent(authctxt);
1029 } while(!sent && authctxt->agent->howmany > 0);
1030 }
1031 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001032 key = options.identity_keys[idx];
1033 filename = options.identity_files[idx];
1034 if (key == NULL) {
1035 debug("try privkey: %s", filename);
1036 key = load_identity_file(filename);
1037 if (key != NULL) {
1038 sent = sign_and_send_pubkey(authctxt, key,
1039 key_sign_cb);
1040 key_free(key);
1041 }
1042 } else if (key->type != KEY_RSA1) {
1043 debug("try pubkey: %s", filename);
1044 sent = send_pubkey_test(authctxt, key,
1045 identity_sign_cb, idx);
1046 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001047 idx++;
1048 }
Damien Miller62cee002000-09-23 17:15:56 +11001049 return sent;
1050}
Damien Millereba71ba2000-04-29 23:57:08 +10001051
Damien Miller874d77b2000-10-14 16:23:11 +11001052/*
1053 * Send userauth request message specifying keyboard-interactive method.
1054 */
1055int
1056userauth_kbdint(Authctxt *authctxt)
1057{
1058 static int attempt = 0;
1059
1060 if (attempt++ >= options.number_of_password_prompts)
1061 return 0;
1062
1063 debug2("userauth_kbdint");
1064 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1065 packet_put_cstring(authctxt->server_user);
1066 packet_put_cstring(authctxt->service);
1067 packet_put_cstring(authctxt->method->name);
1068 packet_put_cstring(""); /* lang */
1069 packet_put_cstring(options.kbd_interactive_devices ?
1070 options.kbd_interactive_devices : "");
1071 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001072
1073 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1074 return 1;
1075}
1076
1077/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001078 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001079 */
1080void
1081input_userauth_info_req(int type, int plen, void *ctxt)
1082{
1083 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001084 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001085 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001086 int echo = 0;
1087
1088 debug2("input_userauth_info_req");
1089
1090 if (authctxt == NULL)
1091 fatal("input_userauth_info_req: no authentication context");
1092
1093 name = packet_get_string(NULL);
1094 inst = packet_get_string(NULL);
1095 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001096 if (strlen(name) > 0)
1097 cli_mesg(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001098 if (strlen(inst) > 0)
1099 cli_mesg(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001100 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001101 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001102 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001103
1104 num_prompts = packet_get_int();
1105 /*
1106 * Begin to build info response packet based on prompts requested.
1107 * We commit to providing the correct number of responses, so if
1108 * further on we run into a problem that prevents this, we have to
1109 * be sure and clean this up and send a correct error response.
1110 */
1111 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1112 packet_put_int(num_prompts);
1113
1114 for (i = 0; i < num_prompts; i++) {
1115 prompt = packet_get_string(NULL);
1116 echo = packet_get_char();
1117
1118 response = cli_prompt(prompt, echo);
1119
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001120 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001121 memset(response, 0, strlen(response));
1122 xfree(response);
1123 xfree(prompt);
1124 }
1125 packet_done(); /* done with parsing incoming message. */
1126
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001127 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001128 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001129}
Damien Miller62cee002000-09-23 17:15:56 +11001130
1131/* find auth method */
1132
Damien Miller62cee002000-09-23 17:15:56 +11001133/*
1134 * given auth method name, if configurable options permit this method fill
1135 * in auth_ident field and return true, otherwise return false.
1136 */
1137int
1138authmethod_is_enabled(Authmethod *method)
1139{
1140 if (method == NULL)
1141 return 0;
1142 /* return false if options indicate this method is disabled */
1143 if (method->enabled == NULL || *method->enabled == 0)
1144 return 0;
1145 /* return false if batch mode is enabled but method needs interactive mode */
1146 if (method->batch_flag != NULL && *method->batch_flag != 0)
1147 return 0;
1148 return 1;
1149}
1150
1151Authmethod *
1152authmethod_lookup(const char *name)
1153{
1154 Authmethod *method = NULL;
1155 if (name != NULL)
1156 for (method = authmethods; method->name != NULL; method++)
1157 if (strcmp(name, method->name) == 0)
1158 return method;
1159 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1160 return NULL;
1161}
1162
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001163/* XXX internal state */
1164static Authmethod *current = NULL;
1165static char *supported = NULL;
1166static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001167/*
1168 * Given the authentication method list sent by the server, return the
1169 * next method we should try. If the server initially sends a nil list,
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001170 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001171 */
Damien Miller62cee002000-09-23 17:15:56 +11001172Authmethod *
1173authmethod_get(char *authlist)
1174{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001175
1176 char *name = NULL;
1177 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001178
Damien Miller62cee002000-09-23 17:15:56 +11001179 /* Use a suitable default if we're passed a nil list. */
1180 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001181 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001182
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001183 if (supported == NULL || strcmp(authlist, supported) != 0) {
1184 debug3("start over, passed a different list %s", authlist);
1185 if (supported != NULL)
1186 xfree(supported);
1187 supported = xstrdup(authlist);
1188 preferred = options.preferred_authentications;
1189 debug3("preferred %s", preferred);
1190 current = NULL;
1191 } else if (current != NULL && authmethod_is_enabled(current))
1192 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001193
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001194 for (;;) {
1195 if ((name = match_list(preferred, supported, &next)) == NULL) {
1196 debug("no more auth methods to try");
1197 current = NULL;
1198 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001199 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001200 preferred += next;
1201 debug3("authmethod_lookup %s", name);
1202 debug3("remaining preferred: %s", preferred);
1203 if ((current = authmethod_lookup(name)) != NULL &&
1204 authmethod_is_enabled(current)) {
1205 debug3("authmethod_is_enabled %s", name);
1206 debug("next auth method to try is %s", name);
1207 return current;
1208 }
Damien Millereba71ba2000-04-29 23:57:08 +10001209 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001210}
Damien Miller62cee002000-09-23 17:15:56 +11001211
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001212
1213#define DELIM ","
1214char *
1215authmethods_get(void)
1216{
1217 Authmethod *method = NULL;
1218 char buf[1024];
1219
1220 buf[0] = '\0';
1221 for (method = authmethods; method->name != NULL; method++) {
1222 if (authmethod_is_enabled(method)) {
1223 if (buf[0] != '\0')
1224 strlcat(buf, DELIM, sizeof buf);
1225 strlcat(buf, method->name, sizeof buf);
1226 }
Damien Miller62cee002000-09-23 17:15:56 +11001227 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001228 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +10001229}