blob: 19d079bd3983d9626b8b6c7b2117628af3dd34fe [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 Lindstromb9be60a2001-03-11 01:49:19 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.53 2001/03/10 17:51:04 markus 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"
Damien Miller62cee002000-09-23 17:15:56 +110049#include "dispatch.h"
Damien Millerad833b32000-08-23 10:46:23 +100050#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000054#include "match.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055
Damien Miller874d77b2000-10-14 16:23:11 +110056void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
57void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
58
Damien Millereba71ba2000-04-29 23:57:08 +100059/* import */
60extern char *client_version_string;
61extern char *server_version_string;
62extern Options options;
63
64/*
65 * SSH2 key exchange
66 */
67
Ben Lindstrom46c16222000-12-22 01:43:59 +000068u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100069int session_id2_len = 0;
70
71void
Damien Miller874d77b2000-10-14 16:23:11 +110072ssh_kex2(char *host, struct sockaddr *hostaddr)
73{
74 int i, plen;
75 Kex *kex;
76 Buffer *client_kexinit, *server_kexinit;
77 char *sprop[PROPOSAL_MAX];
78
Damien Millere39cacc2000-11-29 12:18:44 +110079 if (options.ciphers == (char *)-1) {
80 log("No valid ciphers for protocol version 2 given, using defaults.");
81 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110082 }
83 if (options.ciphers != NULL) {
84 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
85 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
86 }
87 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000088 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110089 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
90 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000091 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +110092 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
93 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000094 if (options.macs != NULL) {
95 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
96 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
97 }
Damien Miller874d77b2000-10-14 16:23:11 +110098
99 /* buffers with raw kexinit messages */
100 server_kexinit = xmalloc(sizeof(*server_kexinit));
101 buffer_init(server_kexinit);
102 client_kexinit = kex_init(myproposal);
103
104 /* algorithm negotiation */
105 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
106 kex = kex_choose_conf(myproposal, sprop, 0);
107 for (i = 0; i < PROPOSAL_MAX; i++)
108 xfree(sprop[i]);
109
110 /* server authentication and session key agreement */
111 switch(kex->kex_type) {
112 case DH_GRP1_SHA1:
113 ssh_dh1_client(kex, host, hostaddr,
114 client_kexinit, server_kexinit);
115 break;
116 case DH_GEX_SHA1:
117 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
118 server_kexinit);
119 break;
120 default:
121 fatal("Unsupported key exchange %d", kex->kex_type);
122 }
123
124 buffer_free(client_kexinit);
125 buffer_free(server_kexinit);
126 xfree(client_kexinit);
127 xfree(server_kexinit);
128
129 debug("Wait SSH2_MSG_NEWKEYS.");
130 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
131 packet_done();
132 debug("GOT SSH2_MSG_NEWKEYS.");
133
134 debug("send SSH2_MSG_NEWKEYS.");
135 packet_start(SSH2_MSG_NEWKEYS);
136 packet_send();
137 packet_write_wait();
138 debug("done: send SSH2_MSG_NEWKEYS.");
139
140#ifdef DEBUG_KEXDH
141 /* send 1st encrypted/maced/compressed message */
142 packet_start(SSH2_MSG_IGNORE);
143 packet_put_cstring("markus");
144 packet_send();
145 packet_write_wait();
146#endif
147 debug("done: KEX2.");
148}
149
150/* diffie-hellman-group1-sha1 */
151
152void
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
Damien Miller874d77b2000-10-14 16:23:11 +1100154 Buffer *client_kexinit, Buffer *server_kexinit)
Damien Millereba71ba2000-04-29 23:57:08 +1000155{
Damien Miller62cee002000-09-23 17:15:56 +1100156#ifdef DEBUG_KEXDH
157 int i;
158#endif
Damien Millerb1715dc2000-05-30 13:44:51 +1000159 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000160 u_int klen, kout;
Damien Millereba71ba2000-04-29 23:57:08 +1000161 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000162 u_int slen;
Damien Millereba71ba2000-04-29 23:57:08 +1000163 char *server_host_key_blob = NULL;
164 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000165 u_int sbloblen;
Damien Millereba71ba2000-04-29 23:57:08 +1000166 DH *dh;
167 BIGNUM *dh_server_pub = 0;
168 BIGNUM *shared_secret = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000169 u_char *kbuf;
170 u_char *hash;
Damien Millereba71ba2000-04-29 23:57:08 +1000171
Damien Millereba71ba2000-04-29 23:57:08 +1000172 debug("Sending SSH2_MSG_KEXDH_INIT.");
Damien Millereba71ba2000-04-29 23:57:08 +1000173 /* generate and send 'e', client DH public key */
174 dh = dh_new_group1();
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000175 dh_gen_key(dh, kex->we_need * 8);
Damien Millereba71ba2000-04-29 23:57:08 +1000176 packet_start(SSH2_MSG_KEXDH_INIT);
177 packet_put_bignum2(dh->pub_key);
178 packet_send();
179 packet_write_wait();
180
181#ifdef DEBUG_KEXDH
182 fprintf(stderr, "\np= ");
Damien Miller62cee002000-09-23 17:15:56 +1100183 BN_print_fp(stderr, dh->p);
Damien Millereba71ba2000-04-29 23:57:08 +1000184 fprintf(stderr, "\ng= ");
Damien Miller62cee002000-09-23 17:15:56 +1100185 BN_print_fp(stderr, dh->g);
Damien Millereba71ba2000-04-29 23:57:08 +1000186 fprintf(stderr, "\npub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100187 BN_print_fp(stderr, dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000188 fprintf(stderr, "\n");
189 DHparams_print_fp(stderr, dh);
190#endif
191
192 debug("Wait SSH2_MSG_KEXDH_REPLY.");
193
Damien Millerb1715dc2000-05-30 13:44:51 +1000194 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
Damien Millereba71ba2000-04-29 23:57:08 +1000195
196 debug("Got SSH2_MSG_KEXDH_REPLY.");
197
198 /* key, cert */
199 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000201 if (server_host_key == NULL)
202 fatal("cannot decode server_host_key_blob");
203
204 check_host_key(host, hostaddr, server_host_key,
Damien Miller874d77b2000-10-14 16:23:11 +1100205 options.user_hostfile2, options.system_hostfile2);
Damien Millereba71ba2000-04-29 23:57:08 +1000206
207 /* DH paramter f, server public DH key */
208 dh_server_pub = BN_new();
209 if (dh_server_pub == NULL)
210 fatal("dh_server_pub == NULL");
211 packet_get_bignum2(dh_server_pub, &dlen);
212
213#ifdef DEBUG_KEXDH
214 fprintf(stderr, "\ndh_server_pub= ");
Damien Miller62cee002000-09-23 17:15:56 +1100215 BN_print_fp(stderr, dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000216 fprintf(stderr, "\n");
217 debug("bits %d", BN_num_bits(dh_server_pub));
218#endif
219
220 /* signed H */
221 signature = packet_get_string(&slen);
222 packet_done();
223
224 if (!dh_pub_is_valid(dh, dh_server_pub))
225 packet_disconnect("bad server public DH value");
226
227 klen = DH_size(dh);
228 kbuf = xmalloc(klen);
229 kout = DH_compute_key(kbuf, dh_server_pub, dh);
230#ifdef DEBUG_KEXDH
231 debug("shared secret: len %d/%d", klen, kout);
232 fprintf(stderr, "shared secret == ");
233 for (i = 0; i< kout; i++)
234 fprintf(stderr, "%02x", (kbuf[i])&0xff);
235 fprintf(stderr, "\n");
236#endif
237 shared_secret = BN_new();
238
239 BN_bin2bn(kbuf, kout, shared_secret);
240 memset(kbuf, 0, klen);
241 xfree(kbuf);
242
243 /* calc and verify H */
244 hash = kex_hash(
245 client_version_string,
246 server_version_string,
247 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
248 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
249 server_host_key_blob, sbloblen,
250 dh->pub_key,
251 dh_server_pub,
252 shared_secret
253 );
254 xfree(server_host_key_blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000255 DH_free(dh);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000256 BN_free(dh_server_pub);
Damien Millereba71ba2000-04-29 23:57:08 +1000257#ifdef DEBUG_KEXDH
258 fprintf(stderr, "hash == ");
259 for (i = 0; i< 20; i++)
260 fprintf(stderr, "%02x", (hash[i])&0xff);
261 fprintf(stderr, "\n");
262#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000263 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100264 fatal("key_verify failed for server_host_key");
Damien Millereba71ba2000-04-29 23:57:08 +1000265 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000266 xfree(signature);
Damien Millereba71ba2000-04-29 23:57:08 +1000267
268 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000269 BN_clear_free(shared_secret);
Damien Millereba71ba2000-04-29 23:57:08 +1000270 packet_set_kex(kex);
271
Damien Millereba71ba2000-04-29 23:57:08 +1000272 /* save session id */
273 session_id2_len = 20;
274 session_id2 = xmalloc(session_id2_len);
275 memcpy(session_id2, hash, session_id2_len);
Damien Millerb1715dc2000-05-30 13:44:51 +1000276}
277
Damien Miller874d77b2000-10-14 16:23:11 +1100278/* diffie-hellman-group-exchange-sha1 */
279
280/*
281 * Estimates the group order for a Diffie-Hellman group that has an
282 * attack complexity approximately the same as O(2**bits). Estimate
283 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
284 */
285
286int
287dh_estimate(int bits)
Damien Millerb1715dc2000-05-30 13:44:51 +1000288{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000289
Damien Miller874d77b2000-10-14 16:23:11 +1100290 if (bits < 64)
291 return (512); /* O(2**63) */
292 if (bits < 128)
293 return (1024); /* O(2**86) */
294 if (bits < 192)
295 return (2048); /* O(2**116) */
296 return (4096); /* O(2**156) */
297}
Damien Millerb1715dc2000-05-30 13:44:51 +1000298
Damien Miller874d77b2000-10-14 16:23:11 +1100299void
300ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
301 Buffer *client_kexinit, Buffer *server_kexinit)
302{
303#ifdef DEBUG_KEXDH
304 int i;
305#endif
306 int plen, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000307 u_int klen, kout;
Damien Miller874d77b2000-10-14 16:23:11 +1100308 char *signature = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000309 u_int slen, nbits;
Damien Miller874d77b2000-10-14 16:23:11 +1100310 char *server_host_key_blob = NULL;
311 Key *server_host_key;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000312 u_int sbloblen;
Damien Miller874d77b2000-10-14 16:23:11 +1100313 DH *dh;
314 BIGNUM *dh_server_pub = 0;
315 BIGNUM *shared_secret = 0;
316 BIGNUM *p = 0, *g = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000317 u_char *kbuf;
318 u_char *hash;
Damien Millerb1715dc2000-05-30 13:44:51 +1000319
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000320 nbits = dh_estimate(kex->we_need * 8);
Damien Millerb1715dc2000-05-30 13:44:51 +1000321
Damien Miller874d77b2000-10-14 16:23:11 +1100322 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
323 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
324 packet_put_int(nbits);
Damien Millereba71ba2000-04-29 23:57:08 +1000325 packet_send();
326 packet_write_wait();
Damien Millereba71ba2000-04-29 23:57:08 +1000327
328#ifdef DEBUG_KEXDH
Damien Miller874d77b2000-10-14 16:23:11 +1100329 fprintf(stderr, "\nnbits = %d", nbits);
330#endif
331
332 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
333
334 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
335
336 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
337
338 if ((p = BN_new()) == NULL)
339 fatal("BN_new");
340 packet_get_bignum2(p, &dlen);
341 if ((g = BN_new()) == NULL)
342 fatal("BN_new");
343 packet_get_bignum2(g, &dlen);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000344 dh = dh_new_group(g, p);
Damien Miller874d77b2000-10-14 16:23:11 +1100345
Ben Lindstrom4c4f05e2001-03-06 01:09:20 +0000346 dh_gen_key(dh, kex->we_need * 8);
Kevin Steves6b875862000-12-15 23:31:01 +0000347
Damien Miller874d77b2000-10-14 16:23:11 +1100348#ifdef DEBUG_KEXDH
349 fprintf(stderr, "\np= ");
350 BN_print_fp(stderr, dh->p);
351 fprintf(stderr, "\ng= ");
352 BN_print_fp(stderr, dh->g);
353 fprintf(stderr, "\npub= ");
354 BN_print_fp(stderr, dh->pub_key);
355 fprintf(stderr, "\n");
356 DHparams_print_fp(stderr, dh);
357#endif
358
359 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
360 /* generate and send 'e', client DH public key */
361 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
362 packet_put_bignum2(dh->pub_key);
Damien Millereba71ba2000-04-29 23:57:08 +1000363 packet_send();
364 packet_write_wait();
Damien Miller874d77b2000-10-14 16:23:11 +1100365
366 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
367
368 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
369
370 debug("Got SSH2_MSG_KEXDH_REPLY.");
371
372 /* key, cert */
373 server_host_key_blob = packet_get_string(&sbloblen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
Damien Miller874d77b2000-10-14 16:23:11 +1100375 if (server_host_key == NULL)
376 fatal("cannot decode server_host_key_blob");
377
378 check_host_key(host, hostaddr, server_host_key,
379 options.user_hostfile2, options.system_hostfile2);
380
381 /* DH paramter f, server public DH key */
382 dh_server_pub = BN_new();
383 if (dh_server_pub == NULL)
384 fatal("dh_server_pub == NULL");
385 packet_get_bignum2(dh_server_pub, &dlen);
386
387#ifdef DEBUG_KEXDH
388 fprintf(stderr, "\ndh_server_pub= ");
389 BN_print_fp(stderr, dh_server_pub);
390 fprintf(stderr, "\n");
391 debug("bits %d", BN_num_bits(dh_server_pub));
Damien Millereba71ba2000-04-29 23:57:08 +1000392#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100393
394 /* signed H */
395 signature = packet_get_string(&slen);
396 packet_done();
397
398 if (!dh_pub_is_valid(dh, dh_server_pub))
399 packet_disconnect("bad server public DH value");
400
401 klen = DH_size(dh);
402 kbuf = xmalloc(klen);
403 kout = DH_compute_key(kbuf, dh_server_pub, dh);
404#ifdef DEBUG_KEXDH
405 debug("shared secret: len %d/%d", klen, kout);
406 fprintf(stderr, "shared secret == ");
407 for (i = 0; i< kout; i++)
408 fprintf(stderr, "%02x", (kbuf[i])&0xff);
409 fprintf(stderr, "\n");
410#endif
411 shared_secret = BN_new();
412
413 BN_bin2bn(kbuf, kout, shared_secret);
414 memset(kbuf, 0, klen);
415 xfree(kbuf);
416
417 /* calc and verify H */
418 hash = kex_hash_gex(
419 client_version_string,
420 server_version_string,
421 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
422 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
423 server_host_key_blob, sbloblen,
Kevin Stevesef4eea92001-02-05 12:42:17 +0000424 nbits, dh->p, dh->g,
Damien Miller874d77b2000-10-14 16:23:11 +1100425 dh->pub_key,
426 dh_server_pub,
427 shared_secret
428 );
429 xfree(server_host_key_blob);
430 DH_free(dh);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000431 BN_free(dh_server_pub);
Damien Miller874d77b2000-10-14 16:23:11 +1100432#ifdef DEBUG_KEXDH
433 fprintf(stderr, "hash == ");
434 for (i = 0; i< 20; i++)
435 fprintf(stderr, "%02x", (hash[i])&0xff);
436 fprintf(stderr, "\n");
437#endif
Ben Lindstrom46c16222000-12-22 01:43:59 +0000438 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100439 fatal("key_verify failed for server_host_key");
Damien Miller874d77b2000-10-14 16:23:11 +1100440 key_free(server_host_key);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000441 xfree(signature);
Damien Miller874d77b2000-10-14 16:23:11 +1100442
443 kex_derive_keys(kex, hash, shared_secret);
Ben Lindstromb1985f72001-01-23 00:19:15 +0000444 BN_clear_free(shared_secret);
Damien Miller874d77b2000-10-14 16:23:11 +1100445 packet_set_kex(kex);
446
447 /* save session id */
448 session_id2_len = 20;
449 session_id2 = xmalloc(session_id2_len);
450 memcpy(session_id2, hash, session_id2_len);
Damien Millereba71ba2000-04-29 23:57:08 +1000451}
Damien Millerb1715dc2000-05-30 13:44:51 +1000452
Damien Millereba71ba2000-04-29 23:57:08 +1000453/*
454 * Authenticate user
455 */
Damien Miller62cee002000-09-23 17:15:56 +1100456
457typedef struct Authctxt Authctxt;
458typedef struct Authmethod Authmethod;
459
460typedef int sign_cb_fn(
461 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000462 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100463
464struct Authctxt {
465 const char *server_user;
466 const char *host;
467 const char *service;
468 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100469 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100470 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000471 char *authlist;
472 Key *last_key;
473 sign_cb_fn *last_key_sign;
474 int last_key_hint;
Damien Miller62cee002000-09-23 17:15:56 +1100475};
476struct Authmethod {
477 char *name; /* string to compare against server's list */
478 int (*userauth)(Authctxt *authctxt);
479 int *enabled; /* flag in option struct that enables method */
480 int *batch_flag; /* flag in option struct that disables method */
481};
482
483void input_userauth_success(int type, int plen, void *ctxt);
484void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000485void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100486void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100487void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000488void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100489
490int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100491int userauth_pubkey(Authctxt *authctxt);
492int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100493int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100494
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000495void userauth(Authctxt *authctxt, char *authlist);
496
497int
498sign_and_send_pubkey(Authctxt *authctxt, Key *k,
499 sign_cb_fn *sign_callback);
500void clear_auth_state(Authctxt *authctxt);
501
Damien Miller874d77b2000-10-14 16:23:11 +1100502Authmethod *authmethod_get(char *authlist);
503Authmethod *authmethod_lookup(const char *name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000504char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100505
506Authmethod authmethods[] = {
507 {"publickey",
508 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100510 NULL},
511 {"password",
512 userauth_passwd,
513 &options.password_authentication,
514 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100515 {"keyboard-interactive",
516 userauth_kbdint,
517 &options.kbd_interactive_authentication,
518 &options.batch_mode},
519 {"none",
520 userauth_none,
521 NULL,
522 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100523 {NULL, NULL, NULL, NULL}
524};
525
526void
527ssh_userauth2(const char *server_user, char *host)
528{
529 Authctxt authctxt;
530 int type;
531 int plen;
532
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000533 if (options.challenge_reponse_authentication)
534 options.kbd_interactive_authentication = 1;
535
Damien Miller62cee002000-09-23 17:15:56 +1100536 debug("send SSH2_MSG_SERVICE_REQUEST");
537 packet_start(SSH2_MSG_SERVICE_REQUEST);
538 packet_put_cstring("ssh-userauth");
539 packet_send();
540 packet_write_wait();
541 type = packet_read(&plen);
542 if (type != SSH2_MSG_SERVICE_ACCEPT) {
543 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
544 }
545 if (packet_remaining() > 0) {
546 char *reply = packet_get_string(&plen);
547 debug("service_accept: %s", reply);
548 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100549 } else {
550 debug("buggy server: service_accept w/o service");
551 }
552 packet_done();
553 debug("got SSH2_MSG_SERVICE_ACCEPT");
554
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000555 if (options.preferred_authentications == NULL)
556 options.preferred_authentications = authmethods_get();
557
Damien Miller62cee002000-09-23 17:15:56 +1100558 /* setup authentication context */
559 authctxt.agent = ssh_get_authentication_connection();
560 authctxt.server_user = server_user;
561 authctxt.host = host;
562 authctxt.service = "ssh-connection"; /* service name */
563 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100564 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000565 authctxt.authlist = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100566 if (authctxt.method == NULL)
567 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100568
569 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100570 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100571
572 dispatch_init(&input_userauth_error);
573 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
574 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000575 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100576 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
577
578 if (authctxt.agent != NULL)
579 ssh_close_authentication_connection(authctxt.agent);
580
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000581 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100582}
583void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000584userauth(Authctxt *authctxt, char *authlist)
585{
586 if (authlist == NULL) {
587 authlist = authctxt->authlist;
588 } else {
589 if (authctxt->authlist)
590 xfree(authctxt->authlist);
591 authctxt->authlist = authlist;
592 }
593 for (;;) {
594 Authmethod *method = authmethod_get(authlist);
595 if (method == NULL)
596 fatal("Permission denied (%s).", authlist);
597 authctxt->method = method;
598 if (method->userauth(authctxt) != 0) {
599 debug2("we sent a %s packet, wait for reply", method->name);
600 break;
601 } else {
602 debug2("we did not send a packet, disable method");
603 method->enabled = NULL;
604 }
605 }
606}
607void
Damien Miller62cee002000-09-23 17:15:56 +1100608input_userauth_error(int type, int plen, void *ctxt)
609{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000610 fatal("input_userauth_error: bad message during authentication: "
611 "type %d", type);
612}
613void
614input_userauth_banner(int type, int plen, void *ctxt)
615{
616 char *msg, *lang;
617 debug3("input_userauth_banner");
618 msg = packet_get_string(NULL);
619 lang = packet_get_string(NULL);
620 fprintf(stderr, "%s", msg);
621 xfree(msg);
622 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100623}
624void
625input_userauth_success(int type, int plen, void *ctxt)
626{
627 Authctxt *authctxt = ctxt;
628 if (authctxt == NULL)
629 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000630 if (authctxt->authlist)
631 xfree(authctxt->authlist);
632 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100633 authctxt->success = 1; /* break out */
634}
635void
636input_userauth_failure(int type, int plen, void *ctxt)
637{
Damien Miller62cee002000-09-23 17:15:56 +1100638 Authctxt *authctxt = ctxt;
639 char *authlist = NULL;
640 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100641
642 if (authctxt == NULL)
643 fatal("input_userauth_failure: no authentication context");
644
Damien Miller874d77b2000-10-14 16:23:11 +1100645 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100646 partial = packet_get_char();
647 packet_done();
648
649 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000650 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100651 debug("authentications that can continue: %s", authlist);
652
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000653 clear_auth_state(authctxt);
654 userauth(authctxt, authlist);
655}
656void
657input_userauth_pk_ok(int type, int plen, void *ctxt)
658{
659 Authctxt *authctxt = ctxt;
660 Key *key = NULL;
661 Buffer b;
662 int alen, blen, pktype, sent = 0;
663 char *pkalg, *pkblob;
664
665 if (authctxt == NULL)
666 fatal("input_userauth_pk_ok: no authentication context");
667 if (datafellows & SSH_BUG_PKOK) {
668 /* this is similar to SSH_BUG_PKAUTH */
669 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
670 pkblob = packet_get_string(&blen);
671 buffer_init(&b);
672 buffer_append(&b, pkblob, blen);
673 pkalg = buffer_get_string(&b, &alen);
674 buffer_free(&b);
675 } else {
676 pkalg = packet_get_string(&alen);
677 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000678 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000679 packet_done();
680
681 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
682 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
683
684 do {
685 if (authctxt->last_key == NULL ||
686 authctxt->last_key_sign == NULL) {
687 debug("no last key or no sign cb");
688 break;
689 }
690 debug2("last_key %s", key_fingerprint(authctxt->last_key));
691 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
692 debug("unknown pkalg %s", pkalg);
693 break;
694 }
695 if ((key = key_from_blob(pkblob, blen)) == NULL) {
696 debug("no key from blob. pkalg %s", pkalg);
697 break;
698 }
699 debug2("input_userauth_pk_ok: fp %s", key_fingerprint(key));
700 if (!key_equal(key, authctxt->last_key)) {
701 debug("key != last_key");
702 break;
703 }
704 sent = sign_and_send_pubkey(authctxt, key,
705 authctxt->last_key_sign);
706 } while(0);
707
708 if (key != NULL)
709 key_free(key);
710 xfree(pkalg);
711 xfree(pkblob);
712
713 /* unregister */
714 clear_auth_state(authctxt);
715 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
716
717 /* try another method if we did not send a packet*/
718 if (sent == 0)
719 userauth(authctxt, NULL);
720
Damien Miller62cee002000-09-23 17:15:56 +1100721}
722
Damien Millereba71ba2000-04-29 23:57:08 +1000723int
Damien Miller874d77b2000-10-14 16:23:11 +1100724userauth_none(Authctxt *authctxt)
725{
726 /* initial userauth request */
727 packet_start(SSH2_MSG_USERAUTH_REQUEST);
728 packet_put_cstring(authctxt->server_user);
729 packet_put_cstring(authctxt->service);
730 packet_put_cstring(authctxt->method->name);
731 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100732 return 1;
733}
734
735int
Damien Miller62cee002000-09-23 17:15:56 +1100736userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000737{
Damien Millere247cc42000-05-07 12:03:14 +1000738 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000739 char prompt[80];
740 char *password;
741
Damien Millerd3a18572000-06-07 19:55:44 +1000742 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000743 return 0;
744
Damien Millerd3a18572000-06-07 19:55:44 +1000745 if(attempt != 1)
746 error("Permission denied, please try again.");
747
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000748 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100749 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000750 password = read_passphrase(prompt, 0);
751 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100752 packet_put_cstring(authctxt->server_user);
753 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100754 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000755 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000756 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000757 memset(password, 0, strlen(password));
758 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000759 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000760 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000761 return 1;
762}
763
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000764void
765clear_auth_state(Authctxt *authctxt)
766{
767 /* XXX clear authentication state */
768 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
769 debug3("clear_auth_state: key_free %p", authctxt->last_key);
770 key_free(authctxt->last_key);
771 }
772 authctxt->last_key = NULL;
773 authctxt->last_key_hint = -2;
774 authctxt->last_key_sign = NULL;
775}
776
Damien Millerad833b32000-08-23 10:46:23 +1000777int
Damien Miller62cee002000-09-23 17:15:56 +1100778sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000779{
780 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000781 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000782 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000783 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000784 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100785 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000786
Ben Lindstromd121f612000-12-03 17:00:47 +0000787 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000788
Damien Miller0bc1bd82000-11-13 22:57:25 +1100789 if (key_to_blob(k, &blob, &bloblen) == 0) {
790 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000791 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792 return 0;
793 }
Damien Millereba71ba2000-04-29 23:57:08 +1000794 /* data to be signed */
795 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100796 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000797 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000798 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100799 } else {
800 buffer_put_string(&b, session_id2, session_id2_len);
801 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000802 }
Damien Millereba71ba2000-04-29 23:57:08 +1000803 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100804 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000805 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000806 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000807 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100808 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000809 if (datafellows & SSH_BUG_PKAUTH) {
810 buffer_put_char(&b, have_sig);
811 } else {
812 buffer_put_cstring(&b, authctxt->method->name);
813 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000814 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000815 }
Damien Millereba71ba2000-04-29 23:57:08 +1000816 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000817
818 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000819 ret = (*sign_callback)(authctxt, k, &signature, &slen,
820 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000821 if (ret == -1) {
822 xfree(blob);
823 buffer_free(&b);
824 return 0;
825 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100826#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000827 buffer_dump(&b);
828#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000829 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000830 buffer_clear(&b);
831 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000832 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000833 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100834 buffer_put_cstring(&b, authctxt->server_user);
835 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100836 buffer_put_cstring(&b, authctxt->method->name);
837 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000838 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000839 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000840 buffer_put_string(&b, blob, bloblen);
841 }
842 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000843
Damien Millereba71ba2000-04-29 23:57:08 +1000844 /* append signature */
845 buffer_put_string(&b, signature, slen);
846 xfree(signature);
847
848 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000849 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100850 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000851 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000852
853 /* put remaining data from buffer into packet */
854 packet_start(SSH2_MSG_USERAUTH_REQUEST);
855 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
856 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000857 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000858
859 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000860}
861
862int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000863send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
864 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000865{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000866 u_char *blob;
867 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000868
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000869 debug3("send_pubkey_test");
870
871 if (key_to_blob(k, &blob, &bloblen) == 0) {
872 /* we cannot handle this key */
873 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000874 return 0;
875 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000876 /* register callback for USERAUTH_PK_OK message */
877 authctxt->last_key_sign = sign_callback;
878 authctxt->last_key_hint = hint;
879 authctxt->last_key = k;
880 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000881
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000882 packet_start(SSH2_MSG_USERAUTH_REQUEST);
883 packet_put_cstring(authctxt->server_user);
884 packet_put_cstring(authctxt->service);
885 packet_put_cstring(authctxt->method->name);
886 packet_put_char(have_sig);
887 if (!(datafellows & SSH_BUG_PKAUTH))
888 packet_put_cstring(key_ssh_name(k));
889 packet_put_string(blob, bloblen);
890 xfree(blob);
891 packet_send();
892 return 1;
893}
894
895Key *
896load_identity_file(char *filename)
897{
898 Key *private;
899 char prompt[300], *passphrase;
900 int success = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000901 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000902
Ben Lindstrom329782e2001-03-10 17:08:59 +0000903 if (stat(filename, &st) < 0) {
904 debug3("no such identity: %s", filename);
905 return NULL;
906 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000907 private = key_new(KEY_UNSPEC);
908 if (!load_private_key(filename, "", private, NULL)) {
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000909 if (options.batch_mode) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000910 key_free(private);
911 return NULL;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000912 }
Damien Miller994cf142000-07-21 10:19:44 +1000913 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100914 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100915 for (i = 0; i < options.number_of_password_prompts; i++) {
916 passphrase = read_passphrase(prompt, 0);
917 if (strcmp(passphrase, "") != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000918 success = load_private_key(filename,
919 passphrase, private, NULL);
920 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100921 } else {
922 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000923 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100924 }
925 memset(passphrase, 0, strlen(passphrase));
926 xfree(passphrase);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000927 if (success || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100928 break;
929 debug2("bad passphrase given, try again...");
930 }
Damien Miller994cf142000-07-21 10:19:44 +1000931 if (!success) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000932 key_free(private);
933 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000934 }
935 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000936 return private;
937}
938
939int
940identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
941 u_char *data, int datalen)
942{
943 Key *private;
944 int idx, ret;
945
946 idx = authctxt->last_key_hint;
947 if (idx < 0)
948 return -1;
949 private = load_identity_file(options.identity_files[idx]);
950 if (private == NULL)
951 return -1;
952 ret = key_sign(private, sigp, lenp, data, datalen);
953 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000954 return ret;
955}
956
Ben Lindstrom46c16222000-12-22 01:43:59 +0000957int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
958 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000959{
Damien Miller62cee002000-09-23 17:15:56 +1100960 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000961}
962
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000963int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
964 u_char *data, int datalen)
965{
966 return key_sign(key, sigp, lenp, data, datalen);
967}
968
Damien Millerad833b32000-08-23 10:46:23 +1000969int
Damien Miller62cee002000-09-23 17:15:56 +1100970userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000971{
972 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100973 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000974 char *comment;
975 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000976
977 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100978 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
979 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100980 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000981 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100982 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100983 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100984 debug2("userauth_pubkey_agent: no more keys");
985 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000986 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100987 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000988 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
989 if (ret == 0)
990 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100991 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100992 if (ret == 0)
993 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000994 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000995}
996
Damien Miller62cee002000-09-23 17:15:56 +1100997int
998userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000999{
Damien Miller62cee002000-09-23 17:15:56 +11001000 static int idx = 0;
1001 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001002 Key *key;
1003 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +10001004
Damien Miller0bc1bd82000-11-13 22:57:25 +11001005 if (authctxt->agent != NULL) {
1006 do {
1007 sent = userauth_pubkey_agent(authctxt);
1008 } while(!sent && authctxt->agent->howmany > 0);
1009 }
1010 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001011 key = options.identity_keys[idx];
1012 filename = options.identity_files[idx];
1013 if (key == NULL) {
1014 debug("try privkey: %s", filename);
1015 key = load_identity_file(filename);
1016 if (key != NULL) {
1017 sent = sign_and_send_pubkey(authctxt, key,
1018 key_sign_cb);
1019 key_free(key);
1020 }
1021 } else if (key->type != KEY_RSA1) {
1022 debug("try pubkey: %s", filename);
1023 sent = send_pubkey_test(authctxt, key,
1024 identity_sign_cb, idx);
1025 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001026 idx++;
1027 }
Damien Miller62cee002000-09-23 17:15:56 +11001028 return sent;
1029}
Damien Millereba71ba2000-04-29 23:57:08 +10001030
Damien Miller874d77b2000-10-14 16:23:11 +11001031/*
1032 * Send userauth request message specifying keyboard-interactive method.
1033 */
1034int
1035userauth_kbdint(Authctxt *authctxt)
1036{
1037 static int attempt = 0;
1038
1039 if (attempt++ >= options.number_of_password_prompts)
1040 return 0;
1041
1042 debug2("userauth_kbdint");
1043 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1044 packet_put_cstring(authctxt->server_user);
1045 packet_put_cstring(authctxt->service);
1046 packet_put_cstring(authctxt->method->name);
1047 packet_put_cstring(""); /* lang */
1048 packet_put_cstring(options.kbd_interactive_devices ?
1049 options.kbd_interactive_devices : "");
1050 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001051
1052 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1053 return 1;
1054}
1055
1056/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001057 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001058 */
1059void
1060input_userauth_info_req(int type, int plen, void *ctxt)
1061{
1062 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001063 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001064 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001065 int echo = 0;
1066
1067 debug2("input_userauth_info_req");
1068
1069 if (authctxt == NULL)
1070 fatal("input_userauth_info_req: no authentication context");
1071
1072 name = packet_get_string(NULL);
1073 inst = packet_get_string(NULL);
1074 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001075 if (strlen(name) > 0)
1076 cli_mesg(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001077 if (strlen(inst) > 0)
1078 cli_mesg(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001079 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001080 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001081 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001082
1083 num_prompts = packet_get_int();
1084 /*
1085 * Begin to build info response packet based on prompts requested.
1086 * We commit to providing the correct number of responses, so if
1087 * further on we run into a problem that prevents this, we have to
1088 * be sure and clean this up and send a correct error response.
1089 */
1090 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1091 packet_put_int(num_prompts);
1092
1093 for (i = 0; i < num_prompts; i++) {
1094 prompt = packet_get_string(NULL);
1095 echo = packet_get_char();
1096
1097 response = cli_prompt(prompt, echo);
1098
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001099 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001100 memset(response, 0, strlen(response));
1101 xfree(response);
1102 xfree(prompt);
1103 }
1104 packet_done(); /* done with parsing incoming message. */
1105
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001106 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001107 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001108}
Damien Miller62cee002000-09-23 17:15:56 +11001109
1110/* find auth method */
1111
Damien Miller62cee002000-09-23 17:15:56 +11001112/*
1113 * given auth method name, if configurable options permit this method fill
1114 * in auth_ident field and return true, otherwise return false.
1115 */
1116int
1117authmethod_is_enabled(Authmethod *method)
1118{
1119 if (method == NULL)
1120 return 0;
1121 /* return false if options indicate this method is disabled */
1122 if (method->enabled == NULL || *method->enabled == 0)
1123 return 0;
1124 /* return false if batch mode is enabled but method needs interactive mode */
1125 if (method->batch_flag != NULL && *method->batch_flag != 0)
1126 return 0;
1127 return 1;
1128}
1129
1130Authmethod *
1131authmethod_lookup(const char *name)
1132{
1133 Authmethod *method = NULL;
1134 if (name != NULL)
1135 for (method = authmethods; method->name != NULL; method++)
1136 if (strcmp(name, method->name) == 0)
1137 return method;
1138 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1139 return NULL;
1140}
1141
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001142/* XXX internal state */
1143static Authmethod *current = NULL;
1144static char *supported = NULL;
1145static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +11001146/*
1147 * Given the authentication method list sent by the server, return the
1148 * next method we should try. If the server initially sends a nil list,
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001149 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001150 */
Damien Miller62cee002000-09-23 17:15:56 +11001151Authmethod *
1152authmethod_get(char *authlist)
1153{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001154
1155 char *name = NULL;
1156 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001157
Damien Miller62cee002000-09-23 17:15:56 +11001158 /* Use a suitable default if we're passed a nil list. */
1159 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001160 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001161
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001162 if (supported == NULL || strcmp(authlist, supported) != 0) {
1163 debug3("start over, passed a different list %s", authlist);
1164 if (supported != NULL)
1165 xfree(supported);
1166 supported = xstrdup(authlist);
1167 preferred = options.preferred_authentications;
1168 debug3("preferred %s", preferred);
1169 current = NULL;
1170 } else if (current != NULL && authmethod_is_enabled(current))
1171 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001172
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001173 for (;;) {
1174 if ((name = match_list(preferred, supported, &next)) == NULL) {
1175 debug("no more auth methods to try");
1176 current = NULL;
1177 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001178 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001179 preferred += next;
1180 debug3("authmethod_lookup %s", name);
1181 debug3("remaining preferred: %s", preferred);
1182 if ((current = authmethod_lookup(name)) != NULL &&
1183 authmethod_is_enabled(current)) {
1184 debug3("authmethod_is_enabled %s", name);
1185 debug("next auth method to try is %s", name);
1186 return current;
1187 }
Damien Millereba71ba2000-04-29 23:57:08 +10001188 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001189}
Damien Miller62cee002000-09-23 17:15:56 +11001190
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001191
1192#define DELIM ","
1193char *
1194authmethods_get(void)
1195{
1196 Authmethod *method = NULL;
1197 char buf[1024];
1198
1199 buf[0] = '\0';
1200 for (method = authmethods; method->name != NULL; method++) {
1201 if (authmethod_is_enabled(method)) {
1202 if (buf[0] != '\0')
1203 strlcat(buf, DELIM, sizeof buf);
1204 strlcat(buf, method->name, sizeof buf);
1205 }
Damien Miller62cee002000-09-23 17:15:56 +11001206 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001207 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +10001208}