- (dtucker) [contrib/aix/buildbff.sh] Fix creation of ssh_prng_cmds.default
[openssh-git.git] / kexgexs.c
blobf4156af96362b119de18d046c3ac30aa1abda6cf
1 /* $OpenBSD: kexgexs.c,v 1.13 2010/02/26 20:29:54 djm Exp $ */
2 /*
3 * Copyright (c) 2000 Niels Provos. All rights reserved.
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
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.
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.
27 #include "includes.h"
29 #include <sys/param.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <signal.h>
36 #include "xmalloc.h"
37 #include "buffer.h"
38 #include "key.h"
39 #include "cipher.h"
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 #ifdef GSSAPI
47 #include "ssh-gss.h"
48 #endif
49 #include "monitor_wrap.h"
51 void
52 kexgex_server(Kex *kex)
54 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
55 Key *server_host_public, *server_host_private;
56 DH *dh;
57 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
58 u_int sbloblen, klen, slen, hashlen;
59 int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
60 int type, kout;
62 if (kex->load_host_public_key == NULL ||
63 kex->load_host_private_key == NULL)
64 fatal("Cannot load hostkey");
65 server_host_public = kex->load_host_public_key(kex->hostkey_type);
66 if (server_host_public == NULL)
67 fatal("Unsupported hostkey type %d", kex->hostkey_type);
68 server_host_private = kex->load_host_private_key(kex->hostkey_type);
69 if (server_host_private == NULL)
70 fatal("Missing private key for hostkey type %d",
71 kex->hostkey_type);
74 type = packet_read();
75 switch (type) {
76 case SSH2_MSG_KEX_DH_GEX_REQUEST:
77 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
78 omin = min = packet_get_int();
79 onbits = nbits = packet_get_int();
80 omax = max = packet_get_int();
81 min = MAX(DH_GRP_MIN, min);
82 max = MIN(DH_GRP_MAX, max);
83 nbits = MAX(DH_GRP_MIN, nbits);
84 nbits = MIN(DH_GRP_MAX, nbits);
85 break;
86 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
87 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
88 onbits = nbits = packet_get_int();
89 /* unused for old GEX */
90 omin = min = DH_GRP_MIN;
91 omax = max = DH_GRP_MAX;
92 break;
93 default:
94 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
96 packet_check_eom();
98 if (omax < omin || onbits < omin || omax < onbits)
99 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
100 omin, onbits, omax);
102 /* Contact privileged parent */
103 dh = PRIVSEP(choose_dh(min, nbits, max));
104 if (dh == NULL)
105 packet_disconnect("Protocol error: no matching DH grp found");
107 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
108 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
109 packet_put_bignum2(dh->p);
110 packet_put_bignum2(dh->g);
111 packet_send();
113 /* flush */
114 packet_write_wait();
116 /* Compute our exchange value in parallel with the client */
117 dh_gen_key(dh, kex->we_need * 8);
119 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
120 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
122 /* key, cert */
123 if ((dh_client_pub = BN_new()) == NULL)
124 fatal("dh_client_pub == NULL");
125 packet_get_bignum2(dh_client_pub);
126 packet_check_eom();
128 #ifdef DEBUG_KEXDH
129 fprintf(stderr, "dh_client_pub= ");
130 BN_print_fp(stderr, dh_client_pub);
131 fprintf(stderr, "\n");
132 debug("bits %d", BN_num_bits(dh_client_pub));
133 #endif
135 #ifdef DEBUG_KEXDH
136 DHparams_print_fp(stderr, dh);
137 fprintf(stderr, "pub= ");
138 BN_print_fp(stderr, dh->pub_key);
139 fprintf(stderr, "\n");
140 #endif
141 if (!dh_pub_is_valid(dh, dh_client_pub))
142 packet_disconnect("bad client public DH value");
144 klen = DH_size(dh);
145 kbuf = xmalloc(klen);
146 if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
147 fatal("DH_compute_key: failed");
148 #ifdef DEBUG_KEXDH
149 dump_digest("shared secret", kbuf, kout);
150 #endif
151 if ((shared_secret = BN_new()) == NULL)
152 fatal("kexgex_server: BN_new failed");
153 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
154 fatal("kexgex_server: BN_bin2bn failed");
155 memset(kbuf, 0, klen);
156 xfree(kbuf);
158 key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
160 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
161 omin = min = omax = max = -1;
163 /* calc H */
164 kexgex_hash(
165 kex->evp_md,
166 kex->client_version_string,
167 kex->server_version_string,
168 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
169 buffer_ptr(&kex->my), buffer_len(&kex->my),
170 server_host_key_blob, sbloblen,
171 omin, onbits, omax,
172 dh->p, dh->g,
173 dh_client_pub,
174 dh->pub_key,
175 shared_secret,
176 &hash, &hashlen
178 BN_clear_free(dh_client_pub);
180 /* save session id := H */
181 if (kex->session_id == NULL) {
182 kex->session_id_len = hashlen;
183 kex->session_id = xmalloc(kex->session_id_len);
184 memcpy(kex->session_id, hash, kex->session_id_len);
187 /* sign H */
188 if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash,
189 hashlen)) < 0)
190 fatal("kexgex_server: key_sign failed");
192 /* destroy_sensitive_data(); */
194 /* send server hostkey, DH pubkey 'f' and singed H */
195 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
196 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
197 packet_put_string(server_host_key_blob, sbloblen);
198 packet_put_bignum2(dh->pub_key); /* f */
199 packet_put_string(signature, slen);
200 packet_send();
202 xfree(signature);
203 xfree(server_host_key_blob);
204 /* have keys, free DH */
205 DH_free(dh);
207 kex_derive_keys(kex, hash, hashlen, shared_secret);
208 BN_clear_free(shared_secret);
210 kex_finish(kex);