]>
Commit | Line | Data |
---|---|---|
a306f2dd | 1 | /* |
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. | |
12 | * 3. All advertising materials mentioning features or use of this software | |
13 | * must display the following acknowledgement: | |
14 | * This product includes software developed by Markus Friedl. | |
15 | * 4. The name of the author may not be used to endorse or promote products | |
16 | * derived from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #include "includes.h" | |
74fc9186 | 31 | RCSID("$OpenBSD: sshconnect2.c,v 1.15 2000/06/21 16:46:10 markus Exp $"); |
a306f2dd | 32 | |
33 | #include <openssl/bn.h> | |
34 | #include <openssl/rsa.h> | |
35 | #include <openssl/dsa.h> | |
36 | #include <openssl/md5.h> | |
37 | #include <openssl/dh.h> | |
38 | #include <openssl/hmac.h> | |
39 | ||
40 | #include "ssh.h" | |
41 | #include "xmalloc.h" | |
42 | #include "rsa.h" | |
43 | #include "buffer.h" | |
44 | #include "packet.h" | |
45 | #include "cipher.h" | |
46 | #include "uidswap.h" | |
47 | #include "compat.h" | |
48 | #include "readconf.h" | |
49 | #include "bufaux.h" | |
50 | #include "ssh2.h" | |
51 | #include "kex.h" | |
52 | #include "myproposal.h" | |
53 | #include "key.h" | |
54 | #include "dsa.h" | |
55 | #include "sshconnect.h" | |
56 | #include "authfile.h" | |
57 | ||
58 | /* import */ | |
59 | extern char *client_version_string; | |
60 | extern char *server_version_string; | |
61 | extern Options options; | |
62 | ||
63 | /* | |
64 | * SSH2 key exchange | |
65 | */ | |
66 | ||
67 | unsigned char *session_id2 = NULL; | |
68 | int session_id2_len = 0; | |
69 | ||
70 | void | |
71276795 | 71 | ssh_kex_dh(Kex *kex, char *host, struct sockaddr *hostaddr, |
72 | Buffer *client_kexinit, Buffer *server_kexinit) | |
a306f2dd | 73 | { |
71276795 | 74 | int plen, dlen; |
a306f2dd | 75 | unsigned int klen, kout; |
a306f2dd | 76 | char *signature = NULL; |
77 | unsigned int slen; | |
78 | char *server_host_key_blob = NULL; | |
79 | Key *server_host_key; | |
80 | unsigned int sbloblen; | |
81 | DH *dh; | |
82 | BIGNUM *dh_server_pub = 0; | |
83 | BIGNUM *shared_secret = 0; | |
a306f2dd | 84 | unsigned char *kbuf; |
85 | unsigned char *hash; | |
86 | ||
a306f2dd | 87 | debug("Sending SSH2_MSG_KEXDH_INIT."); |
a306f2dd | 88 | /* generate and send 'e', client DH public key */ |
89 | dh = dh_new_group1(); | |
90 | packet_start(SSH2_MSG_KEXDH_INIT); | |
91 | packet_put_bignum2(dh->pub_key); | |
92 | packet_send(); | |
93 | packet_write_wait(); | |
94 | ||
95 | #ifdef DEBUG_KEXDH | |
96 | fprintf(stderr, "\np= "); | |
97 | bignum_print(dh->p); | |
98 | fprintf(stderr, "\ng= "); | |
99 | bignum_print(dh->g); | |
100 | fprintf(stderr, "\npub= "); | |
101 | bignum_print(dh->pub_key); | |
102 | fprintf(stderr, "\n"); | |
103 | DHparams_print_fp(stderr, dh); | |
104 | #endif | |
105 | ||
106 | debug("Wait SSH2_MSG_KEXDH_REPLY."); | |
107 | ||
71276795 | 108 | packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY); |
a306f2dd | 109 | |
110 | debug("Got SSH2_MSG_KEXDH_REPLY."); | |
111 | ||
112 | /* key, cert */ | |
113 | server_host_key_blob = packet_get_string(&sbloblen); | |
114 | server_host_key = dsa_key_from_blob(server_host_key_blob, sbloblen); | |
115 | if (server_host_key == NULL) | |
116 | fatal("cannot decode server_host_key_blob"); | |
117 | ||
118 | check_host_key(host, hostaddr, server_host_key, | |
119 | options.user_hostfile2, options.system_hostfile2); | |
120 | ||
121 | /* DH paramter f, server public DH key */ | |
122 | dh_server_pub = BN_new(); | |
123 | if (dh_server_pub == NULL) | |
124 | fatal("dh_server_pub == NULL"); | |
125 | packet_get_bignum2(dh_server_pub, &dlen); | |
126 | ||
127 | #ifdef DEBUG_KEXDH | |
128 | fprintf(stderr, "\ndh_server_pub= "); | |
129 | bignum_print(dh_server_pub); | |
130 | fprintf(stderr, "\n"); | |
131 | debug("bits %d", BN_num_bits(dh_server_pub)); | |
132 | #endif | |
133 | ||
134 | /* signed H */ | |
135 | signature = packet_get_string(&slen); | |
136 | packet_done(); | |
137 | ||
138 | if (!dh_pub_is_valid(dh, dh_server_pub)) | |
139 | packet_disconnect("bad server public DH value"); | |
140 | ||
141 | klen = DH_size(dh); | |
142 | kbuf = xmalloc(klen); | |
143 | kout = DH_compute_key(kbuf, dh_server_pub, dh); | |
144 | #ifdef DEBUG_KEXDH | |
145 | debug("shared secret: len %d/%d", klen, kout); | |
146 | fprintf(stderr, "shared secret == "); | |
147 | for (i = 0; i< kout; i++) | |
148 | fprintf(stderr, "%02x", (kbuf[i])&0xff); | |
149 | fprintf(stderr, "\n"); | |
150 | #endif | |
151 | shared_secret = BN_new(); | |
152 | ||
153 | BN_bin2bn(kbuf, kout, shared_secret); | |
154 | memset(kbuf, 0, klen); | |
155 | xfree(kbuf); | |
156 | ||
157 | /* calc and verify H */ | |
158 | hash = kex_hash( | |
159 | client_version_string, | |
160 | server_version_string, | |
161 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), | |
162 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), | |
163 | server_host_key_blob, sbloblen, | |
164 | dh->pub_key, | |
165 | dh_server_pub, | |
166 | shared_secret | |
167 | ); | |
168 | xfree(server_host_key_blob); | |
71276795 | 169 | DH_free(dh); |
a306f2dd | 170 | #ifdef DEBUG_KEXDH |
171 | fprintf(stderr, "hash == "); | |
172 | for (i = 0; i< 20; i++) | |
173 | fprintf(stderr, "%02x", (hash[i])&0xff); | |
174 | fprintf(stderr, "\n"); | |
175 | #endif | |
176 | if (dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20) != 1) | |
177 | fatal("dsa_verify failed for server_host_key"); | |
178 | key_free(server_host_key); | |
179 | ||
180 | kex_derive_keys(kex, hash, shared_secret); | |
181 | packet_set_kex(kex); | |
182 | ||
a306f2dd | 183 | /* save session id */ |
184 | session_id2_len = 20; | |
185 | session_id2 = xmalloc(session_id2_len); | |
186 | memcpy(session_id2, hash, session_id2_len); | |
71276795 | 187 | } |
188 | ||
189 | void | |
190 | ssh_kex2(char *host, struct sockaddr *hostaddr) | |
191 | { | |
192 | int i, plen; | |
193 | Kex *kex; | |
194 | Buffer *client_kexinit, *server_kexinit; | |
195 | char *sprop[PROPOSAL_MAX]; | |
196 | ||
197 | if (options.ciphers != NULL) { | |
198 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
199 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
200 | } else if (options.cipher == SSH_CIPHER_3DES) { | |
201 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
202 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
203 | (char *) cipher_name(SSH_CIPHER_3DES_CBC); | |
204 | } else if (options.cipher == SSH_CIPHER_BLOWFISH) { | |
205 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
206 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
207 | (char *) cipher_name(SSH_CIPHER_BLOWFISH_CBC); | |
208 | } | |
209 | if (options.compression) { | |
210 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib"; | |
211 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; | |
212 | } else { | |
213 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none"; | |
214 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; | |
215 | } | |
216 | ||
217 | /* buffers with raw kexinit messages */ | |
218 | server_kexinit = xmalloc(sizeof(*server_kexinit)); | |
219 | buffer_init(server_kexinit); | |
220 | client_kexinit = kex_init(myproposal); | |
221 | ||
222 | /* algorithm negotiation */ | |
223 | kex_exchange_kexinit(client_kexinit, server_kexinit, sprop); | |
224 | kex = kex_choose_conf(myproposal, sprop, 0); | |
225 | for (i = 0; i < PROPOSAL_MAX; i++) | |
226 | xfree(sprop[i]); | |
227 | ||
228 | /* server authentication and session key agreement */ | |
229 | ssh_kex_dh(kex, host, hostaddr, client_kexinit, server_kexinit); | |
230 | ||
231 | buffer_free(client_kexinit); | |
232 | buffer_free(server_kexinit); | |
233 | xfree(client_kexinit); | |
234 | xfree(server_kexinit); | |
a306f2dd | 235 | |
236 | debug("Wait SSH2_MSG_NEWKEYS."); | |
71276795 | 237 | packet_read_expect(&plen, SSH2_MSG_NEWKEYS); |
a306f2dd | 238 | packet_done(); |
239 | debug("GOT SSH2_MSG_NEWKEYS."); | |
240 | ||
241 | debug("send SSH2_MSG_NEWKEYS."); | |
242 | packet_start(SSH2_MSG_NEWKEYS); | |
243 | packet_send(); | |
244 | packet_write_wait(); | |
245 | debug("done: send SSH2_MSG_NEWKEYS."); | |
246 | ||
247 | #ifdef DEBUG_KEXDH | |
248 | /* send 1st encrypted/maced/compressed message */ | |
249 | packet_start(SSH2_MSG_IGNORE); | |
250 | packet_put_cstring("markus"); | |
251 | packet_send(); | |
252 | packet_write_wait(); | |
253 | #endif | |
254 | debug("done: KEX2."); | |
255 | } | |
71276795 | 256 | |
a306f2dd | 257 | /* |
258 | * Authenticate user | |
259 | */ | |
260 | int | |
261 | ssh2_try_passwd(const char *server_user, const char *host, const char *service) | |
262 | { | |
1d1ffb87 | 263 | static int attempt = 0; |
a306f2dd | 264 | char prompt[80]; |
265 | char *password; | |
266 | ||
fa649821 | 267 | if (attempt++ >= options.number_of_password_prompts) |
1d1ffb87 | 268 | return 0; |
269 | ||
fa649821 | 270 | if(attempt != 1) |
271 | error("Permission denied, please try again."); | |
272 | ||
a306f2dd | 273 | snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ", |
274 | server_user, host); | |
275 | password = read_passphrase(prompt, 0); | |
276 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
277 | packet_put_cstring(server_user); | |
278 | packet_put_cstring(service); | |
279 | packet_put_cstring("password"); | |
280 | packet_put_char(0); | |
281 | packet_put_cstring(password); | |
282 | memset(password, 0, strlen(password)); | |
283 | xfree(password); | |
284 | packet_send(); | |
285 | packet_write_wait(); | |
286 | return 1; | |
287 | } | |
288 | ||
289 | int | |
290 | ssh2_try_pubkey(char *filename, | |
291 | const char *server_user, const char *host, const char *service) | |
292 | { | |
293 | Buffer b; | |
294 | Key *k; | |
295 | unsigned char *blob, *signature; | |
296 | int bloblen, slen; | |
0fbe8c74 | 297 | struct stat st; |
74fc9186 | 298 | int skip = 0; |
a306f2dd | 299 | |
0fbe8c74 | 300 | if (stat(filename, &st) != 0) { |
301 | debug("key does not exist: %s", filename); | |
302 | return 0; | |
303 | } | |
a306f2dd | 304 | debug("try pubkey: %s", filename); |
305 | ||
306 | k = key_new(KEY_DSA); | |
307 | if (!load_private_key(filename, "", k, NULL)) { | |
308 | int success = 0; | |
309 | char *passphrase; | |
310 | char prompt[300]; | |
0fbe8c74 | 311 | snprintf(prompt, sizeof prompt, |
a306f2dd | 312 | "Enter passphrase for DSA key '%.100s': ", |
0fbe8c74 | 313 | filename); |
a306f2dd | 314 | passphrase = read_passphrase(prompt, 0); |
315 | success = load_private_key(filename, passphrase, k, NULL); | |
316 | memset(passphrase, 0, strlen(passphrase)); | |
317 | xfree(passphrase); | |
74fc9186 | 318 | if (!success) { |
319 | key_free(k); | |
a306f2dd | 320 | return 0; |
74fc9186 | 321 | } |
a306f2dd | 322 | } |
323 | dsa_make_key_blob(k, &blob, &bloblen); | |
324 | ||
325 | /* data to be signed */ | |
326 | buffer_init(&b); | |
74fc9186 | 327 | if (datafellows & SSH_COMPAT_SESSIONID_ENCODING) { |
328 | buffer_put_string(&b, session_id2, session_id2_len); | |
329 | skip = buffer_len(&b); | |
330 | } else { | |
331 | buffer_append(&b, session_id2, session_id2_len); | |
332 | skip = session_id2_len; | |
333 | } | |
a306f2dd | 334 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
335 | buffer_put_cstring(&b, server_user); | |
d0c832f3 | 336 | buffer_put_cstring(&b, |
337 | datafellows & SSH_BUG_PUBKEYAUTH ? | |
338 | "ssh-userauth" : | |
339 | service); | |
a306f2dd | 340 | buffer_put_cstring(&b, "publickey"); |
341 | buffer_put_char(&b, 1); | |
342 | buffer_put_cstring(&b, KEX_DSS); | |
343 | buffer_put_string(&b, blob, bloblen); | |
a306f2dd | 344 | |
345 | /* generate signature */ | |
346 | dsa_sign(k, &signature, &slen, buffer_ptr(&b), buffer_len(&b)); | |
347 | key_free(k); | |
348 | #ifdef DEBUG_DSS | |
349 | buffer_dump(&b); | |
350 | #endif | |
d0c832f3 | 351 | if (datafellows & SSH_BUG_PUBKEYAUTH) { |
d0c832f3 | 352 | buffer_clear(&b); |
353 | buffer_append(&b, session_id2, session_id2_len); | |
354 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
355 | buffer_put_cstring(&b, server_user); | |
356 | buffer_put_cstring(&b, service); | |
357 | buffer_put_cstring(&b, "publickey"); | |
358 | buffer_put_char(&b, 1); | |
359 | buffer_put_cstring(&b, KEX_DSS); | |
360 | buffer_put_string(&b, blob, bloblen); | |
361 | } | |
362 | xfree(blob); | |
a306f2dd | 363 | /* append signature */ |
364 | buffer_put_string(&b, signature, slen); | |
365 | xfree(signature); | |
366 | ||
367 | /* skip session id and packet type */ | |
74fc9186 | 368 | if (buffer_len(&b) < skip + 1) |
a306f2dd | 369 | fatal("ssh2_try_pubkey: internal error"); |
74fc9186 | 370 | buffer_consume(&b, skip + 1); |
a306f2dd | 371 | |
372 | /* put remaining data from buffer into packet */ | |
373 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
374 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
375 | buffer_free(&b); | |
376 | ||
377 | /* send */ | |
378 | packet_send(); | |
379 | packet_write_wait(); | |
380 | return 1; | |
381 | } | |
382 | ||
383 | void | |
384 | ssh_userauth2(const char *server_user, char *host) | |
385 | { | |
386 | int type; | |
387 | int plen; | |
388 | int sent; | |
389 | unsigned int dlen; | |
390 | int partial; | |
391 | int i = 0; | |
392 | char *auths; | |
393 | char *service = "ssh-connection"; /* service name */ | |
394 | ||
395 | debug("send SSH2_MSG_SERVICE_REQUEST"); | |
396 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
397 | packet_put_cstring("ssh-userauth"); | |
398 | packet_send(); | |
399 | packet_write_wait(); | |
400 | ||
401 | type = packet_read(&plen); | |
402 | if (type != SSH2_MSG_SERVICE_ACCEPT) { | |
403 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); | |
404 | } | |
405 | if (packet_remaining() > 0) { | |
406 | char *reply = packet_get_string(&plen); | |
407 | debug("service_accept: %s", reply); | |
408 | xfree(reply); | |
409 | } else { | |
410 | /* payload empty for ssh-2.0.13 ?? */ | |
f6cde515 | 411 | debug("buggy server: service_accept w/o service"); |
a306f2dd | 412 | } |
413 | packet_done(); | |
414 | debug("got SSH2_MSG_SERVICE_ACCEPT"); | |
415 | ||
416 | /* INITIAL request for auth */ | |
417 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
418 | packet_put_cstring(server_user); | |
419 | packet_put_cstring(service); | |
420 | packet_put_cstring("none"); | |
421 | packet_send(); | |
422 | packet_write_wait(); | |
423 | ||
424 | for (;;) { | |
425 | sent = 0; | |
426 | type = packet_read(&plen); | |
427 | if (type == SSH2_MSG_USERAUTH_SUCCESS) | |
428 | break; | |
429 | if (type != SSH2_MSG_USERAUTH_FAILURE) | |
430 | fatal("access denied: %d", type); | |
431 | /* SSH2_MSG_USERAUTH_FAILURE means: try again */ | |
432 | auths = packet_get_string(&dlen); | |
433 | debug("authentications that can continue: %s", auths); | |
434 | partial = packet_get_char(); | |
435 | packet_done(); | |
436 | if (partial) | |
437 | debug("partial success"); | |
1d1ffb87 | 438 | if (options.dsa_authentication && |
a306f2dd | 439 | strstr(auths, "publickey") != NULL) { |
440 | while (i < options.num_identity_files2) { | |
441 | sent = ssh2_try_pubkey( | |
442 | options.identity_files2[i++], | |
443 | server_user, host, service); | |
444 | if (sent) | |
445 | break; | |
446 | } | |
447 | } | |
448 | if (!sent) { | |
449 | if (options.password_authentication && | |
450 | !options.batch_mode && | |
451 | strstr(auths, "password") != NULL) { | |
452 | sent = ssh2_try_passwd(server_user, host, service); | |
453 | } | |
454 | } | |
455 | if (!sent) | |
456 | fatal("Permission denied (%s).", auths); | |
457 | xfree(auths); | |
458 | } | |
459 | packet_done(); | |
460 | debug("ssh-userauth2 successfull"); | |
461 | } |