2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 RCSID("$OpenBSD: sshconnect2.c,v 1.11 2000/05/25 20:45:20 markus Exp $");
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>
52 #include "myproposal.h"
55 #include "sshconnect.h"
59 extern char *client_version_string;
60 extern char *server_version_string;
61 extern Options options;
67 unsigned char *session_id2 = NULL;
68 int session_id2_len = 0;
71 ssh_kex_dh(Kex *kex, char *host, struct sockaddr *hostaddr,
72 Buffer *client_kexinit, Buffer *server_kexinit)
76 unsigned int klen, kout;
77 char *signature = NULL;
79 char *server_host_key_blob = NULL;
81 unsigned int sbloblen;
83 BIGNUM *dh_server_pub = 0;
84 BIGNUM *shared_secret = 0;
88 debug("Sending SSH2_MSG_KEXDH_INIT.");
89 /* generate and send 'e', client DH public key */
91 packet_start(SSH2_MSG_KEXDH_INIT);
92 packet_put_bignum2(dh->pub_key);
97 fprintf(stderr, "\np= ");
99 fprintf(stderr, "\ng= ");
101 fprintf(stderr, "\npub= ");
102 bignum_print(dh->pub_key);
103 fprintf(stderr, "\n");
104 DHparams_print_fp(stderr, dh);
107 debug("Wait SSH2_MSG_KEXDH_REPLY.");
109 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
111 debug("Got SSH2_MSG_KEXDH_REPLY.");
114 server_host_key_blob = packet_get_string(&sbloblen);
115 server_host_key = dsa_key_from_blob(server_host_key_blob, sbloblen);
116 if (server_host_key == NULL)
117 fatal("cannot decode server_host_key_blob");
119 check_host_key(host, hostaddr, server_host_key,
120 options.user_hostfile2, options.system_hostfile2);
122 /* DH paramter f, server public DH key */
123 dh_server_pub = BN_new();
124 if (dh_server_pub == NULL)
125 fatal("dh_server_pub == NULL");
126 packet_get_bignum2(dh_server_pub, &dlen);
129 fprintf(stderr, "\ndh_server_pub= ");
130 bignum_print(dh_server_pub);
131 fprintf(stderr, "\n");
132 debug("bits %d", BN_num_bits(dh_server_pub));
136 signature = packet_get_string(&slen);
139 if (!dh_pub_is_valid(dh, dh_server_pub))
140 packet_disconnect("bad server public DH value");
143 kbuf = xmalloc(klen);
144 kout = DH_compute_key(kbuf, dh_server_pub, dh);
146 debug("shared secret: len %d/%d", klen, kout);
147 fprintf(stderr, "shared secret == ");
148 for (i = 0; i< kout; i++)
149 fprintf(stderr, "%02x", (kbuf[i])&0xff);
150 fprintf(stderr, "\n");
152 shared_secret = BN_new();
154 BN_bin2bn(kbuf, kout, shared_secret);
155 memset(kbuf, 0, klen);
158 /* calc and verify H */
160 client_version_string,
161 server_version_string,
162 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
163 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
164 server_host_key_blob, sbloblen,
169 xfree(server_host_key_blob);
172 fprintf(stderr, "hash == ");
173 for (i = 0; i< 20; i++)
174 fprintf(stderr, "%02x", (hash[i])&0xff);
175 fprintf(stderr, "\n");
177 if (dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20) != 1)
178 fatal("dsa_verify failed for server_host_key");
179 key_free(server_host_key);
181 kex_derive_keys(kex, hash, shared_secret);
184 /* save session id */
185 session_id2_len = 20;
186 session_id2 = xmalloc(session_id2_len);
187 memcpy(session_id2, hash, session_id2_len);
191 ssh_kex2(char *host, struct sockaddr *hostaddr)
195 Buffer *client_kexinit, *server_kexinit;
196 char *sprop[PROPOSAL_MAX];
198 if (options.ciphers != NULL) {
199 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
200 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
201 } else if (options.cipher == SSH_CIPHER_3DES) {
202 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
203 myproposal[PROPOSAL_ENC_ALGS_STOC] =
204 (char *) cipher_name(SSH_CIPHER_3DES_CBC);
205 } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
206 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
207 myproposal[PROPOSAL_ENC_ALGS_STOC] =
208 (char *) cipher_name(SSH_CIPHER_BLOWFISH_CBC);
210 if (options.compression) {
211 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
212 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
214 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
215 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
218 /* buffers with raw kexinit messages */
219 server_kexinit = xmalloc(sizeof(*server_kexinit));
220 buffer_init(server_kexinit);
221 client_kexinit = kex_init(myproposal);
223 /* algorithm negotiation */
224 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
225 kex = kex_choose_conf(myproposal, sprop, 0);
226 for (i = 0; i < PROPOSAL_MAX; i++)
229 /* server authentication and session key agreement */
230 ssh_kex_dh(kex, host, hostaddr, client_kexinit, server_kexinit);
232 buffer_free(client_kexinit);
233 buffer_free(server_kexinit);
234 xfree(client_kexinit);
235 xfree(server_kexinit);
237 debug("Wait SSH2_MSG_NEWKEYS.");
238 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
240 debug("GOT SSH2_MSG_NEWKEYS.");
242 debug("send SSH2_MSG_NEWKEYS.");
243 packet_start(SSH2_MSG_NEWKEYS);
246 debug("done: send SSH2_MSG_NEWKEYS.");
249 /* send 1st encrypted/maced/compressed message */
250 packet_start(SSH2_MSG_IGNORE);
251 packet_put_cstring("markus");
255 debug("done: KEX2.");
262 ssh2_try_passwd(const char *server_user, const char *host, const char *service)
264 static int attempt = 0;
268 if (attempt++ > options.number_of_password_prompts)
271 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
273 password = read_passphrase(prompt, 0);
274 packet_start(SSH2_MSG_USERAUTH_REQUEST);
275 packet_put_cstring(server_user);
276 packet_put_cstring(service);
277 packet_put_cstring("password");
279 packet_put_cstring(password);
280 memset(password, 0, strlen(password));
288 ssh2_try_pubkey(char *filename,
289 const char *server_user, const char *host, const char *service)
293 unsigned char *blob, *signature;
297 if (stat(filename, &st) != 0) {
298 debug("key does not exist: %s", filename);
301 debug("try pubkey: %s", filename);
303 k = key_new(KEY_DSA);
304 if (!load_private_key(filename, "", k, NULL)) {
308 snprintf(prompt, sizeof prompt,
309 "Enter passphrase for DSA key '%.100s': ",
311 passphrase = read_passphrase(prompt, 0);
312 success = load_private_key(filename, passphrase, k, NULL);
313 memset(passphrase, 0, strlen(passphrase));
318 dsa_make_key_blob(k, &blob, &bloblen);
320 /* data to be signed */
322 buffer_append(&b, session_id2, session_id2_len);
323 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
324 buffer_put_cstring(&b, server_user);
325 buffer_put_cstring(&b,
326 datafellows & SSH_BUG_PUBKEYAUTH ?
329 buffer_put_cstring(&b, "publickey");
330 buffer_put_char(&b, 1);
331 buffer_put_cstring(&b, KEX_DSS);
332 buffer_put_string(&b, blob, bloblen);
334 /* generate signature */
335 dsa_sign(k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
340 if (datafellows & SSH_BUG_PUBKEYAUTH) {
341 /* e.g. ssh-2.0.13: data-to-be-signed != data-on-the-wire */
343 buffer_append(&b, session_id2, session_id2_len);
344 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
345 buffer_put_cstring(&b, server_user);
346 buffer_put_cstring(&b, service);
347 buffer_put_cstring(&b, "publickey");
348 buffer_put_char(&b, 1);
349 buffer_put_cstring(&b, KEX_DSS);
350 buffer_put_string(&b, blob, bloblen);
353 /* append signature */
354 buffer_put_string(&b, signature, slen);
357 /* skip session id and packet type */
358 if (buffer_len(&b) < session_id2_len + 1)
359 fatal("ssh2_try_pubkey: internal error");
360 buffer_consume(&b, session_id2_len + 1);
362 /* put remaining data from buffer into packet */
363 packet_start(SSH2_MSG_USERAUTH_REQUEST);
364 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
374 ssh_userauth2(const char *server_user, char *host)
383 char *service = "ssh-connection"; /* service name */
385 debug("send SSH2_MSG_SERVICE_REQUEST");
386 packet_start(SSH2_MSG_SERVICE_REQUEST);
387 packet_put_cstring("ssh-userauth");
391 type = packet_read(&plen);
392 if (type != SSH2_MSG_SERVICE_ACCEPT) {
393 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
395 if (packet_remaining() > 0) {
396 char *reply = packet_get_string(&plen);
397 debug("service_accept: %s", reply);
400 /* payload empty for ssh-2.0.13 ?? */
401 debug("buggy server: service_accept w/o service");
404 debug("got SSH2_MSG_SERVICE_ACCEPT");
406 /* INITIAL request for auth */
407 packet_start(SSH2_MSG_USERAUTH_REQUEST);
408 packet_put_cstring(server_user);
409 packet_put_cstring(service);
410 packet_put_cstring("none");
416 type = packet_read(&plen);
417 if (type == SSH2_MSG_USERAUTH_SUCCESS)
419 if (type != SSH2_MSG_USERAUTH_FAILURE)
420 fatal("access denied: %d", type);
421 /* SSH2_MSG_USERAUTH_FAILURE means: try again */
422 auths = packet_get_string(&dlen);
423 debug("authentications that can continue: %s", auths);
424 partial = packet_get_char();
427 debug("partial success");
428 if (options.dsa_authentication &&
429 strstr(auths, "publickey") != NULL) {
430 while (i < options.num_identity_files2) {
431 sent = ssh2_try_pubkey(
432 options.identity_files2[i++],
433 server_user, host, service);
439 if (options.password_authentication &&
440 !options.batch_mode &&
441 strstr(auths, "password") != NULL) {
442 sent = ssh2_try_passwd(server_user, host, service);
446 fatal("Permission denied (%s).", auths);
450 debug("ssh-userauth2 successfull");