1 /*
2 * Copyright (c) 2001 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 *
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"
26 RCSID("$OpenBSD: kexdh.c,v 1.18 2002/03/18 17:50:31 provos Exp $");
27
28 #pragma ident "%Z%%M% %I% %E% SMI"
29
30 #include <openssl/opensslconf.h>
31 #include <openssl/crypto.h>
32 #include <openssl/bn.h>
33
34 #include "xmalloc.h"
35 #include "buffer.h"
36 #include "bufaux.h"
37 #include "key.h"
38 #include "kex.h"
39 #include "log.h"
40 #include "packet.h"
41 #include "dh.h"
42 #include "ssh2.h"
43
44 void
45 kexdh_client(Kex *kex)
46 {
47 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
48 DH *dh;
49 Key *server_host_key;
50 u_char *server_host_key_blob = NULL, *signature = NULL;
51 u_char *kbuf, *hash;
52 u_int klen, kout, slen, sbloblen;
53
54 /* generate and send 'e', client DH public key */
55 dh = dh_new_group1();
56 dh_gen_key(dh, kex->we_need * 8);
57 packet_start(SSH2_MSG_KEXDH_INIT);
58 packet_put_bignum2(dh->pub_key);
59 packet_send();
60
61 debug("sending SSH2_MSG_KEXDH_INIT");
62 #ifdef DEBUG_KEXDH
63 DHparams_print_fp(stderr, dh);
64 fprintf(stderr, "pub= ");
65 BN_print_fp(stderr, dh->pub_key);
66 fprintf(stderr, "\n");
67 #endif
68
69 debug("expecting SSH2_MSG_KEXDH_REPLY");
70 packet_read_expect(SSH2_MSG_KEXDH_REPLY);
71
72 /* key, cert */
73 server_host_key_blob = packet_get_string(&sbloblen);
74 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
75 if (server_host_key == NULL)
76 fatal("cannot decode server_host_key_blob");
77 if (server_host_key->type != kex->hostkey_type)
78 fatal("type mismatch for decoded server_host_key_blob");
79 if (kex->verify_host_key == NULL)
80 fatal("cannot verify server_host_key");
81 if (kex->verify_host_key(server_host_key) == -1)
82 fatal("server_host_key verification failed");
83
84 /* DH paramter f, server public DH key */
85 if ((dh_server_pub = BN_new()) == NULL)
86 fatal("dh_server_pub == NULL");
87 packet_get_bignum2(dh_server_pub);
88
89 #ifdef DEBUG_KEXDH
90 fprintf(stderr, "dh_server_pub= ");
91 BN_print_fp(stderr, dh_server_pub);
92 fprintf(stderr, "\n");
93 debug("bits %d", BN_num_bits(dh_server_pub));
94 #endif
95
96 /* signed H */
97 signature = packet_get_string(&slen);
98 packet_check_eom();
99
100 if (!dh_pub_is_valid(dh, dh_server_pub))
101 packet_disconnect("bad server public DH value");
102
103 klen = DH_size(dh);
104 kbuf = xmalloc(klen);
105 kout = DH_compute_key(kbuf, dh_server_pub, dh);
106 #ifdef DEBUG_KEXDH
107 dump_digest("shared secret", kbuf, kout);
108 #endif
109 if ((shared_secret = BN_new()) == NULL)
110 fatal("kexdh_client: BN_new failed");
111 BN_bin2bn(kbuf, kout, shared_secret);
112 memset(kbuf, 0, klen);
113 xfree(kbuf);
114
115 /* calc and verify H */
116 hash = kex_dh_hash(
117 kex->client_version_string,
118 kex->server_version_string,
119 buffer_ptr(&kex->my), buffer_len(&kex->my),
120 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
121 server_host_key_blob, sbloblen,
122 dh->pub_key,
123 dh_server_pub,
124 shared_secret
125 );
126 xfree(server_host_key_blob);
127 BN_clear_free(dh_server_pub);
128 DH_free(dh);
129
130 if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
131 fatal("key_verify failed for server_host_key");
132 key_free(server_host_key);
133 xfree(signature);
134
135 /* save session id */
136 if (kex->session_id == NULL) {
137 kex->session_id_len = 20;
138 kex->session_id = xmalloc(kex->session_id_len);
139 memcpy(kex->session_id, hash, kex->session_id_len);
140 }
141
142 kex_derive_keys(kex, hash, shared_secret);
143 BN_clear_free(shared_secret);
144 kex_finish(kex);
145 }