1*535af610SEd Maste /* $OpenBSD: kexgexs.c,v 1.46 2023/03/29 01:07:48 dtucker Exp $ */
2d0c8c0bcSDag-Erling Smørgrav /*
3d0c8c0bcSDag-Erling Smørgrav * Copyright (c) 2000 Niels Provos. All rights reserved.
4d0c8c0bcSDag-Erling Smørgrav * Copyright (c) 2001 Markus Friedl. All rights reserved.
5d0c8c0bcSDag-Erling Smørgrav *
6d0c8c0bcSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
7d0c8c0bcSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
8d0c8c0bcSDag-Erling Smørgrav * are met:
9d0c8c0bcSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
10d0c8c0bcSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
11d0c8c0bcSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
12d0c8c0bcSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
13d0c8c0bcSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
14d0c8c0bcSDag-Erling Smørgrav *
15d0c8c0bcSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16d0c8c0bcSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d0c8c0bcSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d0c8c0bcSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19d0c8c0bcSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d0c8c0bcSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d0c8c0bcSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d0c8c0bcSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d0c8c0bcSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d0c8c0bcSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d0c8c0bcSDag-Erling Smørgrav */
26d0c8c0bcSDag-Erling Smørgrav
27d0c8c0bcSDag-Erling Smørgrav #include "includes.h"
28761efaa7SDag-Erling Smørgrav
29bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
30bc5531deSDag-Erling Smørgrav
31761efaa7SDag-Erling Smørgrav
32761efaa7SDag-Erling Smørgrav #include <stdarg.h>
33761efaa7SDag-Erling Smørgrav #include <stdio.h>
34761efaa7SDag-Erling Smørgrav #include <string.h>
35761efaa7SDag-Erling Smørgrav #include <signal.h>
36d0c8c0bcSDag-Erling Smørgrav
374a421b63SDag-Erling Smørgrav #include <openssl/dh.h>
384a421b63SDag-Erling Smørgrav
392a01feabSEd Maste #include "openbsd-compat/openssl-compat.h"
402a01feabSEd Maste
41bc5531deSDag-Erling Smørgrav #include "sshkey.h"
42761efaa7SDag-Erling Smørgrav #include "cipher.h"
43bc5531deSDag-Erling Smørgrav #include "digest.h"
44d0c8c0bcSDag-Erling Smørgrav #include "kex.h"
45d0c8c0bcSDag-Erling Smørgrav #include "log.h"
46d0c8c0bcSDag-Erling Smørgrav #include "packet.h"
47d0c8c0bcSDag-Erling Smørgrav #include "dh.h"
48d0c8c0bcSDag-Erling Smørgrav #include "ssh2.h"
49761efaa7SDag-Erling Smørgrav #ifdef GSSAPI
50761efaa7SDag-Erling Smørgrav #include "ssh-gss.h"
51761efaa7SDag-Erling Smørgrav #endif
52d0c8c0bcSDag-Erling Smørgrav #include "monitor_wrap.h"
53bc5531deSDag-Erling Smørgrav #include "dispatch.h"
54bc5531deSDag-Erling Smørgrav #include "ssherr.h"
55bc5531deSDag-Erling Smørgrav #include "sshbuf.h"
56ca86bcf2SDag-Erling Smørgrav #include "misc.h"
57d0c8c0bcSDag-Erling Smørgrav
584f52dfbbSDag-Erling Smørgrav static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
594f52dfbbSDag-Erling Smørgrav static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
60bc5531deSDag-Erling Smørgrav
61bc5531deSDag-Erling Smørgrav int
kexgex_server(struct ssh * ssh)62bc5531deSDag-Erling Smørgrav kexgex_server(struct ssh *ssh)
63d0c8c0bcSDag-Erling Smørgrav {
64bc5531deSDag-Erling Smørgrav ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
65bc5531deSDag-Erling Smørgrav &input_kex_dh_gex_request);
66bc5531deSDag-Erling Smørgrav debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
67bc5531deSDag-Erling Smørgrav return 0;
68bc5531deSDag-Erling Smørgrav }
69d0c8c0bcSDag-Erling Smørgrav
70bc5531deSDag-Erling Smørgrav static int
input_kex_dh_gex_request(int type,u_int32_t seq,struct ssh * ssh)714f52dfbbSDag-Erling Smørgrav input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
72bc5531deSDag-Erling Smørgrav {
73bc5531deSDag-Erling Smørgrav struct kex *kex = ssh->kex;
74bc5531deSDag-Erling Smørgrav int r;
75bc5531deSDag-Erling Smørgrav u_int min = 0, max = 0, nbits = 0;
762a01feabSEd Maste const BIGNUM *dh_p, *dh_g;
77d0c8c0bcSDag-Erling Smørgrav
78d0c8c0bcSDag-Erling Smørgrav debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
7919261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
8019261079SEd Maste
81bc5531deSDag-Erling Smørgrav if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
82bc5531deSDag-Erling Smørgrav (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
83bc5531deSDag-Erling Smørgrav (r = sshpkt_get_u32(ssh, &max)) != 0 ||
84bc5531deSDag-Erling Smørgrav (r = sshpkt_get_end(ssh)) != 0)
85bc5531deSDag-Erling Smørgrav goto out;
86bc5531deSDag-Erling Smørgrav kex->nbits = nbits;
87bc5531deSDag-Erling Smørgrav kex->min = min;
88bc5531deSDag-Erling Smørgrav kex->max = max;
89ca86bcf2SDag-Erling Smørgrav min = MAXIMUM(DH_GRP_MIN, min);
90ca86bcf2SDag-Erling Smørgrav max = MINIMUM(DH_GRP_MAX, max);
91ca86bcf2SDag-Erling Smørgrav nbits = MAXIMUM(DH_GRP_MIN, nbits);
92ca86bcf2SDag-Erling Smørgrav nbits = MINIMUM(DH_GRP_MAX, nbits);
93d0c8c0bcSDag-Erling Smørgrav
94bc5531deSDag-Erling Smørgrav if (kex->max < kex->min || kex->nbits < kex->min ||
95076ad2f8SDag-Erling Smørgrav kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
96bc5531deSDag-Erling Smørgrav r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
97bc5531deSDag-Erling Smørgrav goto out;
98bc5531deSDag-Erling Smørgrav }
99d0c8c0bcSDag-Erling Smørgrav
100d0c8c0bcSDag-Erling Smørgrav /* Contact privileged parent */
101bc5531deSDag-Erling Smørgrav kex->dh = PRIVSEP(choose_dh(min, nbits, max));
102bc5531deSDag-Erling Smørgrav if (kex->dh == NULL) {
103*535af610SEd Maste (void)sshpkt_disconnect(ssh, "no matching DH grp found");
104bc5531deSDag-Erling Smørgrav r = SSH_ERR_ALLOC_FAIL;
105bc5531deSDag-Erling Smørgrav goto out;
106bc5531deSDag-Erling Smørgrav }
107d0c8c0bcSDag-Erling Smørgrav debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
1082a01feabSEd Maste DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
109bc5531deSDag-Erling Smørgrav if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
1102a01feabSEd Maste (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
1112a01feabSEd Maste (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
112bc5531deSDag-Erling Smørgrav (r = sshpkt_send(ssh)) != 0)
113bc5531deSDag-Erling Smørgrav goto out;
114d0c8c0bcSDag-Erling Smørgrav
115d0c8c0bcSDag-Erling Smørgrav /* Compute our exchange value in parallel with the client */
116bc5531deSDag-Erling Smørgrav if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
117bc5531deSDag-Erling Smørgrav goto out;
118bc5531deSDag-Erling Smørgrav
119d0c8c0bcSDag-Erling Smørgrav debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
120bc5531deSDag-Erling Smørgrav ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
121bc5531deSDag-Erling Smørgrav r = 0;
122bc5531deSDag-Erling Smørgrav out:
123bc5531deSDag-Erling Smørgrav return r;
124bc5531deSDag-Erling Smørgrav }
125bc5531deSDag-Erling Smørgrav
126bc5531deSDag-Erling Smørgrav static int
input_kex_dh_gex_init(int type,u_int32_t seq,struct ssh * ssh)1274f52dfbbSDag-Erling Smørgrav input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
128bc5531deSDag-Erling Smørgrav {
129bc5531deSDag-Erling Smørgrav struct kex *kex = ssh->kex;
13019261079SEd Maste BIGNUM *dh_client_pub = NULL;
1312a01feabSEd Maste const BIGNUM *pub_key, *dh_p, *dh_g;
13219261079SEd Maste struct sshbuf *shared_secret = NULL;
13319261079SEd Maste struct sshbuf *server_host_key_blob = NULL;
134bc5531deSDag-Erling Smørgrav struct sshkey *server_host_public, *server_host_private;
13519261079SEd Maste u_char *signature = NULL;
136bc5531deSDag-Erling Smørgrav u_char hash[SSH_DIGEST_MAX_LENGTH];
13719261079SEd Maste size_t slen, hashlen;
13819261079SEd Maste int r;
139bc5531deSDag-Erling Smørgrav
14019261079SEd Maste debug("SSH2_MSG_KEX_DH_GEX_INIT received");
14119261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
14219261079SEd Maste
14319261079SEd Maste if ((r = kex_load_hostkey(ssh, &server_host_private,
14419261079SEd Maste &server_host_public)) != 0)
145bc5531deSDag-Erling Smørgrav goto out;
146d0c8c0bcSDag-Erling Smørgrav
147d0c8c0bcSDag-Erling Smørgrav /* key, cert */
14819261079SEd Maste if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
149bc5531deSDag-Erling Smørgrav (r = sshpkt_get_end(ssh)) != 0)
150bc5531deSDag-Erling Smørgrav goto out;
15119261079SEd Maste if ((shared_secret = sshbuf_new()) == NULL) {
152bc5531deSDag-Erling Smørgrav r = SSH_ERR_ALLOC_FAIL;
153bc5531deSDag-Erling Smørgrav goto out;
154bc5531deSDag-Erling Smørgrav }
15519261079SEd Maste if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
15619261079SEd Maste goto out;
15719261079SEd Maste if ((server_host_key_blob = sshbuf_new()) == NULL) {
15819261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
159bc5531deSDag-Erling Smørgrav goto out;
160bc5531deSDag-Erling Smørgrav }
16119261079SEd Maste if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
162bc5531deSDag-Erling Smørgrav goto out;
16319261079SEd Maste
164021d409fSDag-Erling Smørgrav /* calc H */
16519261079SEd Maste DH_get0_key(kex->dh, &pub_key, NULL);
16619261079SEd Maste DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
167bc5531deSDag-Erling Smørgrav hashlen = sizeof(hash);
168bc5531deSDag-Erling Smørgrav if ((r = kexgex_hash(
169f7167e0eSDag-Erling Smørgrav kex->hash_alg,
17019261079SEd Maste kex->client_version,
17119261079SEd Maste kex->server_version,
17219261079SEd Maste kex->peer,
17319261079SEd Maste kex->my,
17419261079SEd Maste server_host_key_blob,
175bc5531deSDag-Erling Smørgrav kex->min, kex->nbits, kex->max,
1762a01feabSEd Maste dh_p, dh_g,
177d0c8c0bcSDag-Erling Smørgrav dh_client_pub,
1782a01feabSEd Maste pub_key,
17919261079SEd Maste sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
180bc5531deSDag-Erling Smørgrav hash, &hashlen)) != 0)
181bc5531deSDag-Erling Smørgrav goto out;
182d0c8c0bcSDag-Erling Smørgrav
183d0c8c0bcSDag-Erling Smørgrav /* sign H */
18419261079SEd Maste if ((r = kex->sign(ssh, server_host_private, server_host_public,
18519261079SEd Maste &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
186bc5531deSDag-Erling Smørgrav goto out;
187d0c8c0bcSDag-Erling Smørgrav
188190cef3dSDag-Erling Smørgrav /* send server hostkey, DH pubkey 'f' and signed H */
189bc5531deSDag-Erling Smørgrav if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
19019261079SEd Maste (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
1912a01feabSEd Maste (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
192bc5531deSDag-Erling Smørgrav (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
193bc5531deSDag-Erling Smørgrav (r = sshpkt_send(ssh)) != 0)
194bc5531deSDag-Erling Smørgrav goto out;
195d0c8c0bcSDag-Erling Smørgrav
1961323ec57SEd Maste if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
1971323ec57SEd Maste (r = kex_send_newkeys(ssh)) != 0)
1981323ec57SEd Maste goto out;
1991323ec57SEd Maste
2001323ec57SEd Maste /* retain copy of hostkey used at initial KEX */
2011323ec57SEd Maste if (kex->initial_hostkey == NULL &&
2021323ec57SEd Maste (r = sshkey_from_private(server_host_public,
2031323ec57SEd Maste &kex->initial_hostkey)) != 0)
2041323ec57SEd Maste goto out;
2051323ec57SEd Maste /* success */
206bc5531deSDag-Erling Smørgrav out:
2072f513db7SEd Maste explicit_bzero(hash, sizeof(hash));
208bc5531deSDag-Erling Smørgrav DH_free(kex->dh);
209bc5531deSDag-Erling Smørgrav kex->dh = NULL;
210bc5531deSDag-Erling Smørgrav BN_clear_free(dh_client_pub);
21119261079SEd Maste sshbuf_free(shared_secret);
21219261079SEd Maste sshbuf_free(server_host_key_blob);
213bc5531deSDag-Erling Smørgrav free(signature);
214bc5531deSDag-Erling Smørgrav return r;
215bc5531deSDag-Erling Smørgrav }
216bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
217