]> andersk Git - openssh.git/blame - kexdhc.c
- deraadt@cvs.openbsd.org 2006/08/03 03:34:42
[openssh.git] / kexdhc.c
CommitLineData
31652869 1/* $OpenBSD: kexdhc.c,v 1.9 2006/08/03 03:34:42 deraadt Exp $ */
98a58eda 2/*
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"
98a58eda 27
31652869 28#include <sys/types.h>
29
cf851879 30#include <stdio.h>
00146caa 31#include <string.h>
31652869 32#include <signal.h>
00146caa 33
98a58eda 34#include "xmalloc.h"
31652869 35#include "buffer.h"
98a58eda 36#include "key.h"
31652869 37#include "cipher.h"
98a58eda 38#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;
47e5dc72 52 u_int klen, kout, slen, sbloblen, hashlen;
98a58eda 53
54 /* generate and send 'e', client DH public key */
41e5bd9a 55 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 }
98a58eda 65 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
18c60a0b 93 /* DH parameter f, server public DH key */
98a58eda 94 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 */
47e5dc72 125 kex_dh_hash(
98a58eda 126 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,
47e5dc72 133 shared_secret,
134 &hash, &hashlen
98a58eda 135 );
136 xfree(server_host_key_blob);
137 BN_clear_free(dh_server_pub);
138 DH_free(dh);
139
47e5dc72 140 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
98a58eda 141 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) {
47e5dc72 147 kex->session_id_len = hashlen;
98a58eda 148 kex->session_id = xmalloc(kex->session_id_len);
149 memcpy(kex->session_id, hash, kex->session_id_len);
150 }
151
47e5dc72 152 kex_derive_keys(kex, hash, hashlen, shared_secret);
98a58eda 153 BN_clear_free(shared_secret);
154 kex_finish(kex);
155}
This page took 0.133279 seconds and 5 git commands to generate.