2 * Copyright (c) 2001 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.
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.
26 RCSID("$OpenBSD: kexdhc.c,v 1.1 2003/02/16 17:09:57 markus Exp $");
37 kexdh_client(Kex
*kex
)
39 BIGNUM
*dh_server_pub
= NULL
, *shared_secret
= NULL
;
42 u_char
*server_host_key_blob
= NULL
, *signature
= NULL
;
44 u_int klen
, kout
, slen
, sbloblen
;
46 /* generate and send 'e', client DH public key */
48 dh_gen_key(dh
, kex
->we_need
* 8);
49 packet_start(SSH2_MSG_KEXDH_INIT
);
50 packet_put_bignum2(dh
->pub_key
);
53 debug("sending SSH2_MSG_KEXDH_INIT");
55 DHparams_print_fp(stderr
, dh
);
56 fprintf(stderr
, "pub= ");
57 BN_print_fp(stderr
, dh
->pub_key
);
58 fprintf(stderr
, "\n");
61 debug("expecting SSH2_MSG_KEXDH_REPLY");
62 packet_read_expect(SSH2_MSG_KEXDH_REPLY
);
65 server_host_key_blob
= packet_get_string(&sbloblen
);
66 server_host_key
= key_from_blob(server_host_key_blob
, sbloblen
);
67 if (server_host_key
== NULL
)
68 fatal("cannot decode server_host_key_blob");
69 if (server_host_key
->type
!= kex
->hostkey_type
)
70 fatal("type mismatch for decoded server_host_key_blob");
71 if (kex
->verify_host_key
== NULL
)
72 fatal("cannot verify server_host_key");
73 if (kex
->verify_host_key(server_host_key
) == -1)
74 fatal("server_host_key verification failed");
76 /* DH paramter f, server public DH key */
77 if ((dh_server_pub
= BN_new()) == NULL
)
78 fatal("dh_server_pub == NULL");
79 packet_get_bignum2(dh_server_pub
);
82 fprintf(stderr
, "dh_server_pub= ");
83 BN_print_fp(stderr
, dh_server_pub
);
84 fprintf(stderr
, "\n");
85 debug("bits %d", BN_num_bits(dh_server_pub
));
89 signature
= packet_get_string(&slen
);
92 if (!dh_pub_is_valid(dh
, dh_server_pub
))
93 packet_disconnect("bad server public DH value");
97 kout
= DH_compute_key(kbuf
, dh_server_pub
, dh
);
99 dump_digest("shared secret", kbuf
, kout
);
101 if ((shared_secret
= BN_new()) == NULL
)
102 fatal("kexdh_client: BN_new failed");
103 BN_bin2bn(kbuf
, kout
, shared_secret
);
104 memset(kbuf
, 0, klen
);
107 /* calc and verify H */
109 kex
->client_version_string
,
110 kex
->server_version_string
,
111 buffer_ptr(&kex
->my
), buffer_len(&kex
->my
),
112 buffer_ptr(&kex
->peer
), buffer_len(&kex
->peer
),
113 server_host_key_blob
, sbloblen
,
118 xfree(server_host_key_blob
);
119 BN_clear_free(dh_server_pub
);
122 if (key_verify(server_host_key
, signature
, slen
, hash
, 20) != 1)
123 fatal("key_verify failed for server_host_key");
124 key_free(server_host_key
);
127 /* save session id */
128 if (kex
->session_id
== NULL
) {
129 kex
->session_id_len
= 20;
130 kex
->session_id
= xmalloc(kex
->session_id_len
);
131 memcpy(kex
->session_id
, hash
, kex
->session_id_len
);
134 kex_derive_keys(kex
, hash
, shared_secret
);
135 BN_clear_free(shared_secret
);