]> andersk Git - openssh.git/blame - kexdhc.c
- djm@cvs.openbsd.org 2006/03/25 01:13:23
[openssh.git] / kexdhc.c
CommitLineData
98a58eda 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"
98a58eda 26
27#include "xmalloc.h"
28#include "key.h"
29#include "kex.h"
30#include "log.h"
31#include "packet.h"
32#include "dh.h"
33#include "ssh2.h"
34
35void
36kexdh_client(Kex *kex)
37{
38 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
39 DH *dh;
40 Key *server_host_key;
41 u_char *server_host_key_blob = NULL, *signature = NULL;
42 u_char *kbuf, *hash;
47e5dc72 43 u_int klen, kout, slen, sbloblen, hashlen;
98a58eda 44
45 /* generate and send 'e', client DH public key */
41e5bd9a 46 switch (kex->kex_type) {
47 case KEX_DH_GRP1_SHA1:
48 dh = dh_new_group1();
49 break;
50 case KEX_DH_GRP14_SHA1:
51 dh = dh_new_group14();
52 break;
53 default:
54 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
55 }
98a58eda 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 */
47e5dc72 116 kex_dh_hash(
98a58eda 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,
47e5dc72 124 shared_secret,
125 &hash, &hashlen
98a58eda 126 );
127 xfree(server_host_key_blob);
128 BN_clear_free(dh_server_pub);
129 DH_free(dh);
130
47e5dc72 131 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
98a58eda 132 fatal("key_verify failed for server_host_key");
133 key_free(server_host_key);
134 xfree(signature);
135
136 /* save session id */
137 if (kex->session_id == NULL) {
47e5dc72 138 kex->session_id_len = hashlen;
98a58eda 139 kex->session_id = xmalloc(kex->session_id_len);
140 memcpy(kex->session_id, hash, kex->session_id_len);
141 }
142
47e5dc72 143 kex_derive_keys(kex, hash, hashlen, shared_secret);
98a58eda 144 BN_clear_free(shared_secret);
145 kex_finish(kex);
146}
This page took 0.140857 seconds and 5 git commands to generate.