]> andersk Git - gssapi-openssh.git/blame - openssh/kexgexc.c
Import of OpenSSH 4.4p1
[gssapi-openssh.git] / openssh / kexgexc.c
CommitLineData
9108f8d9 1/* $OpenBSD: kexgexc.c,v 1.9 2006/08/03 03:34:42 deraadt Exp $ */
6a9b3198 2/*
3 * Copyright (c) 2000 Niels Provos. All rights reserved.
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
9108f8d9 28
29#include <sys/types.h>
30
31#include <stdarg.h>
32#include <stdio.h>
33#include <string.h>
34#include <signal.h>
6a9b3198 35
36#include "xmalloc.h"
9108f8d9 37#include "buffer.h"
6a9b3198 38#include "key.h"
9108f8d9 39#include "cipher.h"
6a9b3198 40#include "kex.h"
41#include "log.h"
42#include "packet.h"
43#include "dh.h"
44#include "ssh2.h"
45#include "compat.h"
46
47void
48kexgex_client(Kex *kex)
49{
50 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
51 BIGNUM *p = NULL, *g = NULL;
52 Key *server_host_key;
53 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
2c06c99b 54 u_int klen, kout, slen, sbloblen, hashlen;
6a9b3198 55 int min, max, nbits;
56 DH *dh;
57
58 nbits = dh_estimate(kex->we_need * 8);
59
60 if (datafellows & SSH_OLD_DHGEX) {
6a9b3198 61 /* Old GEX request */
62 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
63 packet_put_int(nbits);
64 min = DH_GRP_MIN;
65 max = DH_GRP_MAX;
9cb1827b 66
cdd66111 67 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
68 } else {
6a9b3198 69 /* New GEX request */
70 min = DH_GRP_MIN;
71 max = DH_GRP_MAX;
72 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
73 packet_put_int(min);
74 packet_put_int(nbits);
75 packet_put_int(max);
cdd66111 76
77 debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
78 min, nbits, max);
6a9b3198 79 }
80#ifdef DEBUG_KEXDH
81 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
82 min, nbits, max);
83#endif
84 packet_send();
85
86 debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
87 packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
88
89 if ((p = BN_new()) == NULL)
90 fatal("BN_new");
91 packet_get_bignum2(p);
92 if ((g = BN_new()) == NULL)
93 fatal("BN_new");
94 packet_get_bignum2(g);
95 packet_check_eom();
96
97 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
98 fatal("DH_GEX group out of range: %d !< %d !< %d",
99 min, BN_num_bits(p), max);
100
101 dh = dh_new_group(g, p);
102 dh_gen_key(dh, kex->we_need * 8);
103
104#ifdef DEBUG_KEXDH
105 DHparams_print_fp(stderr, dh);
106 fprintf(stderr, "pub= ");
107 BN_print_fp(stderr, dh->pub_key);
108 fprintf(stderr, "\n");
109#endif
110
111 debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
112 /* generate and send 'e', client DH public key */
113 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
114 packet_put_bignum2(dh->pub_key);
115 packet_send();
116
117 debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
118 packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
119
120 /* key, cert */
121 server_host_key_blob = packet_get_string(&sbloblen);
122 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
123 if (server_host_key == NULL)
124 fatal("cannot decode server_host_key_blob");
125 if (server_host_key->type != kex->hostkey_type)
126 fatal("type mismatch for decoded server_host_key_blob");
127 if (kex->verify_host_key == NULL)
128 fatal("cannot verify server_host_key");
129 if (kex->verify_host_key(server_host_key) == -1)
130 fatal("server_host_key verification failed");
131
9108f8d9 132 /* DH parameter f, server public DH key */
6a9b3198 133 if ((dh_server_pub = BN_new()) == NULL)
134 fatal("dh_server_pub == NULL");
135 packet_get_bignum2(dh_server_pub);
136
137#ifdef DEBUG_KEXDH
138 fprintf(stderr, "dh_server_pub= ");
139 BN_print_fp(stderr, dh_server_pub);
140 fprintf(stderr, "\n");
141 debug("bits %d", BN_num_bits(dh_server_pub));
142#endif
143
144 /* signed H */
145 signature = packet_get_string(&slen);
146 packet_check_eom();
147
148 if (!dh_pub_is_valid(dh, dh_server_pub))
149 packet_disconnect("bad server public DH value");
150
151 klen = DH_size(dh);
152 kbuf = xmalloc(klen);
153 kout = DH_compute_key(kbuf, dh_server_pub, dh);
154#ifdef DEBUG_KEXDH
155 dump_digest("shared secret", kbuf, kout);
156#endif
157 if ((shared_secret = BN_new()) == NULL)
158 fatal("kexgex_client: BN_new failed");
159 BN_bin2bn(kbuf, kout, shared_secret);
160 memset(kbuf, 0, klen);
161 xfree(kbuf);
162
163 if (datafellows & SSH_OLD_DHGEX)
164 min = max = -1;
165
166 /* calc and verify H */
2c06c99b 167 kexgex_hash(
168 kex->evp_md,
6a9b3198 169 kex->client_version_string,
170 kex->server_version_string,
171 buffer_ptr(&kex->my), buffer_len(&kex->my),
172 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
173 server_host_key_blob, sbloblen,
174 min, nbits, max,
175 dh->p, dh->g,
176 dh->pub_key,
177 dh_server_pub,
2c06c99b 178 shared_secret,
179 &hash, &hashlen
6a9b3198 180 );
2c06c99b 181
6a9b3198 182 /* have keys, free DH */
183 DH_free(dh);
184 xfree(server_host_key_blob);
185 BN_clear_free(dh_server_pub);
186
2c06c99b 187 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
6a9b3198 188 fatal("key_verify failed for server_host_key");
189 key_free(server_host_key);
190 xfree(signature);
191
192 /* save session id */
193 if (kex->session_id == NULL) {
2c06c99b 194 kex->session_id_len = hashlen;
6a9b3198 195 kex->session_id = xmalloc(kex->session_id_len);
196 memcpy(kex->session_id, hash, kex->session_id_len);
197 }
2c06c99b 198 kex_derive_keys(kex, hash, hashlen, shared_secret);
6a9b3198 199 BN_clear_free(shared_secret);
200
201 kex_finish(kex);
202}
This page took 0.836346 seconds and 5 git commands to generate.