Fixed the build error for gcc-14
[platform/upstream/openssh.git] / kexdhs.c
1 /* $OpenBSD: kexdhs.c,v 1.26 2018/02/07 02:06:51 jsing Exp $ */
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"
27
28 #ifdef WITH_OPENSSL
29
30 #include <sys/types.h>
31
32 #include <stdarg.h>
33 #include <string.h>
34 #include <signal.h>
35
36 #include <openssl/dh.h>
37
38 #include "sshkey.h"
39 #include "cipher.h"
40 #include "digest.h"
41 #include "kex.h"
42 #include "log.h"
43 #include "packet.h"
44 #include "dh.h"
45 #include "ssh2.h"
46
47 #include "dispatch.h"
48 #include "compat.h"
49 #include "ssherr.h"
50 #include "sshbuf.h"
51
52 static int input_kex_dh_init(int, u_int32_t, struct ssh *);
53
54 int
55 kexdh_server(struct ssh *ssh)
56 {
57         struct kex *kex = ssh->kex;
58         int r;
59
60         /* generate server DH public key */
61         switch (kex->kex_type) {
62         case KEX_DH_GRP1_SHA1:
63                 kex->dh = dh_new_group1();
64                 break;
65         case KEX_DH_GRP14_SHA1:
66         case KEX_DH_GRP14_SHA256:
67                 kex->dh = dh_new_group14();
68                 break;
69         case KEX_DH_GRP16_SHA512:
70                 kex->dh = dh_new_group16();
71                 break;
72         case KEX_DH_GRP18_SHA512:
73                 kex->dh = dh_new_group18();
74                 break;
75         default:
76                 r = SSH_ERR_INVALID_ARGUMENT;
77                 goto out;
78         }
79         if (kex->dh == NULL) {
80                 r = SSH_ERR_ALLOC_FAIL;
81                 goto out;
82         }
83         if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
84                 goto out;
85
86         debug("expecting SSH2_MSG_KEXDH_INIT");
87         ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
88         r = 0;
89  out:
90         return r;
91 }
92
93 int
94 input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
95 {
96         struct kex *kex = ssh->kex;
97         BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
98         struct sshkey *server_host_public, *server_host_private;
99         u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
100         u_char hash[SSH_DIGEST_MAX_LENGTH];
101         size_t sbloblen, slen;
102         size_t klen = 0, hashlen;
103         int kout, r;
104
105         if (kex->load_host_public_key == NULL ||
106             kex->load_host_private_key == NULL) {
107                 r = SSH_ERR_INVALID_ARGUMENT;
108                 goto out;
109         }
110         server_host_public = kex->load_host_public_key(kex->hostkey_type,
111             kex->hostkey_nid, ssh);
112         server_host_private = kex->load_host_private_key(kex->hostkey_type,
113             kex->hostkey_nid, ssh);
114         if (server_host_public == NULL) {
115                 r = SSH_ERR_NO_HOSTKEY_LOADED;
116                 goto out;
117         }
118
119         /* key, cert */
120         if ((dh_client_pub = BN_new()) == NULL) {
121                 r = SSH_ERR_ALLOC_FAIL;
122                 goto out;
123         }
124         if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
125             (r = sshpkt_get_end(ssh)) != 0)
126                 goto out;
127
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
134
135 #ifdef DEBUG_KEXDH
136         DHparams_print_fp(stderr, kex->dh);
137         fprintf(stderr, "pub= ");
138         BN_print_fp(stderr, kex->dh->pub_key);
139         fprintf(stderr, "\n");
140 #endif
141         if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
142                 sshpkt_disconnect(ssh, "bad client public DH value");
143                 r = SSH_ERR_MESSAGE_INCOMPLETE;
144                 goto out;
145         }
146
147         klen = DH_size(kex->dh);
148         if ((kbuf = malloc(klen)) == NULL ||
149             (shared_secret = BN_new()) == NULL) {
150                 r = SSH_ERR_ALLOC_FAIL;
151                 goto out;
152         }
153         if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
154             BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
155                 r = SSH_ERR_LIBCRYPTO_ERROR;
156                 goto out;
157         }
158 #ifdef DEBUG_KEXDH
159         dump_digest("shared secret", kbuf, kout);
160 #endif
161         if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
162             &sbloblen)) != 0)
163                 goto out;
164         /* calc H */
165         hashlen = sizeof(hash);
166         {
167         const BIGNUM *pub_key;
168         DH_get0_key(kex->dh, &pub_key, NULL);
169         if ((r = kex_dh_hash(
170             kex->hash_alg,
171             kex->client_version_string,
172             kex->server_version_string,
173             sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
174             sshbuf_ptr(kex->my), sshbuf_len(kex->my),
175             server_host_key_blob, sbloblen,
176             dh_client_pub,
177             pub_key,
178             shared_secret,
179             hash, &hashlen)) != 0) {
180                 goto out;
181         }
182         }
183
184         /* save session id := H */
185         if (kex->session_id == NULL) {
186                 kex->session_id_len = hashlen;
187                 kex->session_id = malloc(kex->session_id_len);
188                 if (kex->session_id == NULL) {
189                         r = SSH_ERR_ALLOC_FAIL;
190                         goto out;
191                 }
192                 memcpy(kex->session_id, hash, kex->session_id_len);
193         }
194
195         /* sign H */
196         if ((r = kex->sign(server_host_private, server_host_public, &signature,
197              &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
198                 goto out;
199
200         /* destroy_sensitive_data(); */
201
202         /* send server hostkey, DH pubkey 'f' and singed H */
203         {
204         const BIGNUM *pub_key;
205         DH_get0_key(kex->dh, &pub_key, NULL);
206         if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
207             (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
208             (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||      /* f */
209             (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
210             (r = sshpkt_send(ssh)) != 0) {
211                 goto out;
212         }
213         }
214
215         if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
216                 r = kex_send_newkeys(ssh);
217  out:
218         explicit_bzero(hash, sizeof(hash));
219         DH_free(kex->dh);
220         kex->dh = NULL;
221         BN_clear_free(dh_client_pub);
222         if (kbuf) {
223                 explicit_bzero(kbuf, klen);
224                 free(kbuf);
225         }
226         BN_clear_free(shared_secret);
227         free(server_host_key_blob);
228         free(signature);
229         return r;
230 }
231 #endif /* WITH_OPENSSL */