blob: bf875ae07b1024a4593d9920db8e4de34bad004d [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: kexdhc.c,v 1.9 2006/08/03 03:34:42 deraadt Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110027
Damien Millerd7834352006-08-05 12:39:39 +100028#include <sys/types.h>
29
Damien Millera7a73ee2006-08-05 11:37:59 +100030#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033
Damien Miller8e7fb332003-02-24 12:03:03 +110034#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100035#include "buffer.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110036#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100037#include "cipher.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110038#include "kex.h"
39#include "log.h"
40#include "packet.h"
41#include "dh.h"
42#include "ssh2.h"
43
44void
45kexdh_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;
Damien Miller19bb3a52005-11-05 15:19:35 +110052 u_int klen, kout, slen, sbloblen, hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +110053
54 /* generate and send 'e', client DH public key */
Damien Millerf675fc42004-06-15 10:30:09 +100055 switch (kex->kex_type) {
56 case KEX_DH_GRP1_SHA1:
57 dh = dh_new_group1();
58 break;
59 case KEX_DH_GRP14_SHA1:
60 dh = dh_new_group14();
61 break;
62 default:
63 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
64 }
Damien Miller8e7fb332003-02-24 12:03:03 +110065 dh_gen_key(dh, kex->we_need * 8);
66 packet_start(SSH2_MSG_KEXDH_INIT);
67 packet_put_bignum2(dh->pub_key);
68 packet_send();
69
70 debug("sending SSH2_MSG_KEXDH_INIT");
71#ifdef DEBUG_KEXDH
72 DHparams_print_fp(stderr, dh);
73 fprintf(stderr, "pub= ");
74 BN_print_fp(stderr, dh->pub_key);
75 fprintf(stderr, "\n");
76#endif
77
78 debug("expecting SSH2_MSG_KEXDH_REPLY");
79 packet_read_expect(SSH2_MSG_KEXDH_REPLY);
80
81 /* key, cert */
82 server_host_key_blob = packet_get_string(&sbloblen);
83 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
84 if (server_host_key == NULL)
85 fatal("cannot decode server_host_key_blob");
86 if (server_host_key->type != kex->hostkey_type)
87 fatal("type mismatch for decoded server_host_key_blob");
88 if (kex->verify_host_key == NULL)
89 fatal("cannot verify server_host_key");
90 if (kex->verify_host_key(server_host_key) == -1)
91 fatal("server_host_key verification failed");
92
Damien Millerad6b14d2006-06-13 13:00:41 +100093 /* DH parameter f, server public DH key */
Damien Miller8e7fb332003-02-24 12:03:03 +110094 if ((dh_server_pub = BN_new()) == NULL)
95 fatal("dh_server_pub == NULL");
96 packet_get_bignum2(dh_server_pub);
97
98#ifdef DEBUG_KEXDH
99 fprintf(stderr, "dh_server_pub= ");
100 BN_print_fp(stderr, dh_server_pub);
101 fprintf(stderr, "\n");
102 debug("bits %d", BN_num_bits(dh_server_pub));
103#endif
104
105 /* signed H */
106 signature = packet_get_string(&slen);
107 packet_check_eom();
108
109 if (!dh_pub_is_valid(dh, dh_server_pub))
110 packet_disconnect("bad server public DH value");
111
112 klen = DH_size(dh);
113 kbuf = xmalloc(klen);
114 kout = DH_compute_key(kbuf, dh_server_pub, dh);
115#ifdef DEBUG_KEXDH
116 dump_digest("shared secret", kbuf, kout);
117#endif
118 if ((shared_secret = BN_new()) == NULL)
119 fatal("kexdh_client: BN_new failed");
120 BN_bin2bn(kbuf, kout, shared_secret);
121 memset(kbuf, 0, klen);
122 xfree(kbuf);
123
124 /* calc and verify H */
Damien Miller19bb3a52005-11-05 15:19:35 +1100125 kex_dh_hash(
Damien Miller8e7fb332003-02-24 12:03:03 +1100126 kex->client_version_string,
127 kex->server_version_string,
128 buffer_ptr(&kex->my), buffer_len(&kex->my),
129 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
130 server_host_key_blob, sbloblen,
131 dh->pub_key,
132 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100133 shared_secret,
134 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100135 );
136 xfree(server_host_key_blob);
137 BN_clear_free(dh_server_pub);
138 DH_free(dh);
139
Damien Miller19bb3a52005-11-05 15:19:35 +1100140 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
Damien Miller8e7fb332003-02-24 12:03:03 +1100141 fatal("key_verify failed for server_host_key");
142 key_free(server_host_key);
143 xfree(signature);
144
145 /* save session id */
146 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100147 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100148 kex->session_id = xmalloc(kex->session_id_len);
149 memcpy(kex->session_id, hash, kex->session_id_len);
150 }
151
Damien Miller19bb3a52005-11-05 15:19:35 +1100152 kex_derive_keys(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100153 BN_clear_free(shared_secret);
154 kex_finish(kex);
155}