Fixed the build error for gcc-14
[platform/upstream/openssh.git] / kexgexs.c
1 /* $OpenBSD: kexgexs.c,v 1.32 2018/02/07 02:06:51 jsing Exp $ */
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"
28
29 #ifdef WITH_OPENSSL
30
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <signal.h>
36
37 #include <openssl/dh.h>
38
39 #include "sshkey.h"
40 #include "cipher.h"
41 #include "digest.h"
42 #include "kex.h"
43 #include "log.h"
44 #include "packet.h"
45 #include "dh.h"
46 #include "ssh2.h"
47 #include "compat.h"
48 #ifdef GSSAPI
49 #include "ssh-gss.h"
50 #endif
51 #include "monitor_wrap.h"
52 #include "dispatch.h"
53 #include "ssherr.h"
54 #include "sshbuf.h"
55 #include "misc.h"
56
57 static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
58 static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
59
60 int
61 kexgex_server(struct ssh *ssh)
62 {
63         ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
64             &input_kex_dh_gex_request);
65         debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
66         return 0;
67 }
68
69 static int
70 input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
71 {
72         struct kex *kex = ssh->kex;
73         int r;
74         u_int min = 0, max = 0, nbits = 0;
75
76         debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
77         if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
78             (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
79             (r = sshpkt_get_u32(ssh, &max)) != 0 ||
80             (r = sshpkt_get_end(ssh)) != 0)
81                 goto out;
82         kex->nbits = nbits;
83         kex->min = min;
84         kex->max = max;
85         min = MAXIMUM(DH_GRP_MIN, min);
86         max = MINIMUM(DH_GRP_MAX, max);
87         nbits = MAXIMUM(DH_GRP_MIN, nbits);
88         nbits = MINIMUM(DH_GRP_MAX, nbits);
89
90         if (kex->max < kex->min || kex->nbits < kex->min ||
91             kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
92                 r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
93                 goto out;
94         }
95
96         /* Contact privileged parent */
97         kex->dh = PRIVSEP(choose_dh(min, nbits, max));
98         if (kex->dh == NULL) {
99                 sshpkt_disconnect(ssh, "no matching DH grp found");
100                 r = SSH_ERR_ALLOC_FAIL;
101                 goto out;
102         }
103         debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
104         {
105         const BIGNUM *p, *g;
106         DH_get0_pqg(kex->dh, &p, NULL, &g);
107         if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
108             (r = sshpkt_put_bignum2(ssh, p)) != 0 ||
109             (r = sshpkt_put_bignum2(ssh, g)) != 0 ||
110             (r = sshpkt_send(ssh)) != 0) {
111                 goto out;
112         }
113         }
114
115         /* Compute our exchange value in parallel with the client */
116         if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
117                 goto out;
118
119         debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
120         ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
121         r = 0;
122  out:
123         return r;
124 }
125
126 static int
127 input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
128 {
129         struct kex *kex = ssh->kex;
130         BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
131         struct sshkey *server_host_public, *server_host_private;
132         u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
133         u_char hash[SSH_DIGEST_MAX_LENGTH];
134         size_t sbloblen, slen;
135         size_t klen = 0, hashlen;
136         int kout, r;
137
138         if (kex->load_host_public_key == NULL ||
139             kex->load_host_private_key == NULL) {
140                 r = SSH_ERR_INVALID_ARGUMENT;
141                 goto out;
142         }
143         server_host_public = kex->load_host_public_key(kex->hostkey_type,
144             kex->hostkey_nid, ssh);
145         server_host_private = kex->load_host_private_key(kex->hostkey_type,
146             kex->hostkey_nid, ssh);
147         if (server_host_public == NULL) {
148                 r = SSH_ERR_NO_HOSTKEY_LOADED;
149                 goto out;
150         }
151
152         /* key, cert */
153         if ((dh_client_pub = BN_new()) == NULL) {
154                 r = SSH_ERR_ALLOC_FAIL;
155                 goto out;
156         }
157         if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
158             (r = sshpkt_get_end(ssh)) != 0)
159                 goto out;
160
161 #ifdef DEBUG_KEXDH
162         fprintf(stderr, "dh_client_pub= ");
163         BN_print_fp(stderr, dh_client_pub);
164         fprintf(stderr, "\n");
165         debug("bits %d", BN_num_bits(dh_client_pub));
166 #endif
167
168 #ifdef DEBUG_KEXDH
169         DHparams_print_fp(stderr, kex->dh);
170         fprintf(stderr, "pub= ");
171         BN_print_fp(stderr, kex->dh->pub_key);
172         fprintf(stderr, "\n");
173 #endif
174         if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
175                 sshpkt_disconnect(ssh, "bad client public DH value");
176                 r = SSH_ERR_MESSAGE_INCOMPLETE;
177                 goto out;
178         }
179
180         klen = DH_size(kex->dh);
181         if ((kbuf = malloc(klen)) == NULL ||
182             (shared_secret = BN_new()) == NULL) {
183                 r = SSH_ERR_ALLOC_FAIL;
184                 goto out;
185         }
186         if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
187             BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
188                 r = SSH_ERR_LIBCRYPTO_ERROR;
189                 goto out;
190         }
191 #ifdef DEBUG_KEXDH
192         dump_digest("shared secret", kbuf, kout);
193 #endif
194         if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
195             &sbloblen)) != 0)
196                 goto out;
197         /* calc H */
198         hashlen = sizeof(hash);
199         {
200         const BIGNUM *p, *g, *pub_key;
201         DH_get0_pqg(kex->dh, &p, NULL, &g);
202         DH_get0_key(kex->dh, &pub_key, NULL);
203         if ((r = kexgex_hash(
204             kex->hash_alg,
205             kex->client_version_string,
206             kex->server_version_string,
207             sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
208             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
209             server_host_key_blob, sbloblen,
210             kex->min, kex->nbits, kex->max,
211             p, g,
212             dh_client_pub,
213             pub_key,
214             shared_secret,
215             hash, &hashlen)) != 0) {
216                 goto out;
217         }
218         }
219
220         /* save session id := H */
221         if (kex->session_id == NULL) {
222                 kex->session_id_len = hashlen;
223                 kex->session_id = malloc(kex->session_id_len);
224                 if (kex->session_id == NULL) {
225                         r = SSH_ERR_ALLOC_FAIL;
226                         goto out;
227                 }
228                 memcpy(kex->session_id, hash, kex->session_id_len);
229         }
230
231         /* sign H */
232         if ((r = kex->sign(server_host_private, server_host_public, &signature,
233              &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
234                 goto out;
235
236         /* destroy_sensitive_data(); */
237
238         /* send server hostkey, DH pubkey 'f' and singed H */
239         {
240         const BIGNUM *pub_key;
241         DH_get0_key(kex->dh, &pub_key, NULL);
242         if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
243             (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
244             (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
245             (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
246             (r = sshpkt_send(ssh)) != 0) {
247                 goto out;
248         }
249         }
250
251         if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
252                 r = kex_send_newkeys(ssh);
253  out:
254         DH_free(kex->dh);
255         kex->dh = NULL;
256         BN_clear_free(dh_client_pub);
257         if (kbuf) {
258                 explicit_bzero(kbuf, klen);
259                 free(kbuf);
260         }
261         BN_clear_free(shared_secret);
262         free(server_host_key_blob);
263         free(signature);
264         return r;
265 }
266 #endif /* WITH_OPENSSL */