1 /* $NetBSD: kexgen.c,v 1.10 2026/07/29 08:24:09 rin Exp $ */ 2 /* $OpenBSD: kexgen.c,v 1.12 2026/03/03 09:57:25 dtucker Exp $ */ 3 4 /* 5 * Copyright (c) 2019 Markus Friedl. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include "includes.h" 28 __RCSID("$NetBSD: kexgen.c,v 1.10 2026/07/29 08:24:09 rin Exp $"); 29 30 #include <sys/types.h> 31 32 #include <stdarg.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <signal.h> 36 37 #include "sshkey.h" 38 #include "kex.h" 39 #include "log.h" 40 #include "packet.h" 41 #include "ssh2.h" 42 #include "sshbuf.h" 43 #include "digest.h" 44 #include "ssherr.h" 45 46 static int input_kex_gen_init(int, uint32_t, struct ssh *); 47 static int input_kex_gen_reply(int type, uint32_t seq, struct ssh *ssh); 48 49 static int 50 kex_gen_hash( 51 int hash_alg, 52 const struct sshbuf *client_version, 53 const struct sshbuf *server_version, 54 const struct sshbuf *client_kexinit, 55 const struct sshbuf *server_kexinit, 56 const struct sshbuf *server_host_key_blob, 57 const struct sshbuf *client_pub, 58 const struct sshbuf *server_pub, 59 const struct sshbuf *shared_secret, 60 u_char *hash, size_t *hashlen) 61 { 62 struct sshbuf *b; 63 int r; 64 65 if (*hashlen < ssh_digest_bytes(hash_alg)) 66 return SSH_ERR_INVALID_ARGUMENT; 67 if ((b = sshbuf_new()) == NULL) 68 return SSH_ERR_ALLOC_FAIL; 69 if ((r = sshbuf_put_stringb(b, client_version)) != 0 || 70 (r = sshbuf_put_stringb(b, server_version)) != 0 || 71 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ 72 (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 || 73 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 74 (r = sshbuf_putb(b, client_kexinit)) != 0 || 75 (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 || 76 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 77 (r = sshbuf_putb(b, server_kexinit)) != 0 || 78 (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 || 79 (r = sshbuf_put_stringb(b, client_pub)) != 0 || 80 (r = sshbuf_put_stringb(b, server_pub)) != 0 || 81 (r = sshbuf_putb(b, shared_secret)) != 0) { 82 sshbuf_free(b); 83 return r; 84 } 85 #ifdef DEBUG_KEX 86 sshbuf_dump(b, stderr); 87 #endif 88 if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) { 89 sshbuf_free(b); 90 return SSH_ERR_LIBCRYPTO_ERROR; 91 } 92 sshbuf_free(b); 93 *hashlen = ssh_digest_bytes(hash_alg); 94 #ifdef DEBUG_KEX 95 dump_digest("hash", hash, *hashlen); 96 #endif 97 return 0; 98 } 99 100 int 101 kex_gen_client(struct ssh *ssh) 102 { 103 struct kex *kex = ssh->kex; 104 int r; 105 106 switch (kex->kex_type) { 107 #ifdef WITH_OPENSSL 108 case KEX_DH_GRP1_SHA1: 109 case KEX_DH_GRP14_SHA1: 110 case KEX_DH_GRP14_SHA256: 111 case KEX_DH_GRP16_SHA512: 112 case KEX_DH_GRP18_SHA512: 113 r = kex_dh_keypair(kex); 114 break; 115 case KEX_ECDH_SHA2: 116 r = kex_ecdh_keypair(kex); 117 break; 118 #endif /* WITH_OPENSSL */ 119 case KEX_C25519_SHA256: 120 r = kex_c25519_keypair(kex); 121 break; 122 case KEX_KEM_SNTRUP761X25519_SHA512: 123 r = kex_kem_sntrup761x25519_keypair(kex); 124 break; 125 case KEX_KEM_MLKEM768X25519_SHA256: 126 r = kex_kem_mlkem768x25519_keypair(kex); 127 break; 128 default: 129 r = SSH_ERR_INVALID_ARGUMENT; 130 break; 131 } 132 if (r != 0) 133 return r; 134 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 || 135 (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 || 136 (r = sshpkt_send(ssh)) != 0) 137 return r; 138 debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); 139 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply); 140 return 0; 141 } 142 143 static int 144 input_kex_gen_reply(int type, uint32_t seq, struct ssh *ssh) 145 { 146 struct kex *kex = ssh->kex; 147 struct sshkey *server_host_key = NULL; 148 struct sshbuf *shared_secret = NULL; 149 struct sshbuf *server_blob = NULL; 150 struct sshbuf *tmp = NULL, *server_host_key_blob = NULL; 151 u_char *signature = NULL; 152 u_char hash[SSH_DIGEST_MAX_LENGTH]; 153 size_t slen, hashlen; 154 int r; 155 156 debug("SSH2_MSG_KEX_ECDH_REPLY received"); 157 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &kex_protocol_error); 158 159 /* hostkey */ 160 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0) 161 goto out; 162 /* sshkey_fromb() consumes its buffer, so make a copy */ 163 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) { 164 r = SSH_ERR_ALLOC_FAIL; 165 goto out; 166 } 167 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0) 168 goto out; 169 if ((r = kex_verify_host_key(ssh, server_host_key)) != 0) 170 goto out; 171 172 /* Q_S, server public key */ 173 /* signed H */ 174 if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 || 175 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || 176 (r = sshpkt_get_end(ssh)) != 0) 177 goto out; 178 179 /* compute shared secret */ 180 switch (kex->kex_type) { 181 #ifdef WITH_OPENSSL 182 case KEX_DH_GRP1_SHA1: 183 case KEX_DH_GRP14_SHA1: 184 case KEX_DH_GRP14_SHA256: 185 case KEX_DH_GRP16_SHA512: 186 case KEX_DH_GRP18_SHA512: 187 r = kex_dh_dec(kex, server_blob, &shared_secret); 188 break; 189 case KEX_ECDH_SHA2: 190 r = kex_ecdh_dec(kex, server_blob, &shared_secret); 191 break; 192 #endif /* WITH_OPENSSL */ 193 case KEX_C25519_SHA256: 194 r = kex_c25519_dec(kex, server_blob, &shared_secret); 195 break; 196 case KEX_KEM_SNTRUP761X25519_SHA512: 197 r = kex_kem_sntrup761x25519_dec(kex, server_blob, 198 &shared_secret); 199 break; 200 case KEX_KEM_MLKEM768X25519_SHA256: 201 r = kex_kem_mlkem768x25519_dec(kex, server_blob, 202 &shared_secret); 203 break; 204 default: 205 r = SSH_ERR_INVALID_ARGUMENT; 206 break; 207 } 208 if (r !=0 ) 209 goto out; 210 211 /* calc and verify H */ 212 hashlen = sizeof(hash); 213 if ((r = kex_gen_hash( 214 kex->hash_alg, 215 kex->client_version, 216 kex->server_version, 217 kex->my, 218 kex->peer, 219 server_host_key_blob, 220 kex->client_pub, 221 server_blob, 222 shared_secret, 223 hash, &hashlen)) != 0) 224 goto out; 225 226 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, 227 kex->hostkey_alg, ssh->compat, NULL)) != 0) 228 goto out; 229 230 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 231 (r = kex_send_newkeys(ssh)) != 0) 232 goto out; 233 234 /* save initial signature and hostkey */ 235 if ((kex->flags & KEX_INITIAL) != 0) { 236 if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) { 237 r = SSH_ERR_INTERNAL_ERROR; 238 goto out; 239 } 240 if ((kex->initial_sig = sshbuf_new()) == NULL) { 241 r = SSH_ERR_ALLOC_FAIL; 242 goto out; 243 } 244 if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0) 245 goto out; 246 kex->initial_hostkey = server_host_key; 247 server_host_key = NULL; 248 } 249 /* success */ 250 out: 251 explicit_bzero(hash, sizeof(hash)); 252 explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key)); 253 explicit_bzero(kex->sntrup761_client_key, 254 sizeof(kex->sntrup761_client_key)); 255 explicit_bzero(kex->mlkem768_client_key, 256 sizeof(kex->mlkem768_client_key)); 257 sshbuf_free(server_host_key_blob); 258 free(signature); 259 sshbuf_free(tmp); 260 sshkey_free(server_host_key); 261 sshbuf_free(server_blob); 262 sshbuf_free(shared_secret); 263 sshbuf_free(kex->client_pub); 264 kex->client_pub = NULL; 265 return r; 266 } 267 268 int 269 kex_gen_server(struct ssh *ssh) 270 { 271 debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 272 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init); 273 return 0; 274 } 275 276 static int 277 input_kex_gen_init(int type, uint32_t seq, struct ssh *ssh) 278 { 279 struct kex *kex = ssh->kex; 280 struct sshkey *server_host_private, *server_host_public; 281 struct sshbuf *shared_secret = NULL; 282 struct sshbuf *server_pubkey = NULL; 283 struct sshbuf *client_pubkey = NULL; 284 struct sshbuf *server_host_key_blob = NULL; 285 u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH]; 286 size_t slen, hashlen; 287 int r; 288 289 debug("SSH2_MSG_KEX_ECDH_INIT received"); 290 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &kex_protocol_error); 291 292 if ((r = kex_load_hostkey(ssh, &server_host_private, 293 &server_host_public)) != 0) 294 goto out; 295 296 if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 || 297 (r = sshpkt_get_end(ssh)) != 0) 298 goto out; 299 300 /* compute shared secret */ 301 switch (kex->kex_type) { 302 #ifdef WITH_OPENSSL 303 case KEX_DH_GRP1_SHA1: 304 case KEX_DH_GRP14_SHA1: 305 case KEX_DH_GRP14_SHA256: 306 case KEX_DH_GRP16_SHA512: 307 case KEX_DH_GRP18_SHA512: 308 r = kex_dh_enc(kex, client_pubkey, &server_pubkey, 309 &shared_secret); 310 break; 311 case KEX_ECDH_SHA2: 312 r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey, 313 &shared_secret); 314 break; 315 #endif /* WITH_OPENSSL */ 316 case KEX_C25519_SHA256: 317 r = kex_c25519_enc(kex, client_pubkey, &server_pubkey, 318 &shared_secret); 319 break; 320 case KEX_KEM_SNTRUP761X25519_SHA512: 321 r = kex_kem_sntrup761x25519_enc(kex, client_pubkey, 322 &server_pubkey, &shared_secret); 323 break; 324 case KEX_KEM_MLKEM768X25519_SHA256: 325 r = kex_kem_mlkem768x25519_enc(kex, client_pubkey, 326 &server_pubkey, &shared_secret); 327 break; 328 default: 329 r = SSH_ERR_INVALID_ARGUMENT; 330 break; 331 } 332 if (r !=0 ) 333 goto out; 334 335 /* calc H */ 336 if ((server_host_key_blob = sshbuf_new()) == NULL) { 337 r = SSH_ERR_ALLOC_FAIL; 338 goto out; 339 } 340 if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0) 341 goto out; 342 hashlen = sizeof(hash); 343 if ((r = kex_gen_hash( 344 kex->hash_alg, 345 kex->client_version, 346 kex->server_version, 347 kex->peer, 348 kex->my, 349 server_host_key_blob, 350 client_pubkey, 351 server_pubkey, 352 shared_secret, 353 hash, &hashlen)) != 0) 354 goto out; 355 356 /* sign H */ 357 if ((r = kex->sign(ssh, server_host_private, server_host_public, 358 &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0) 359 goto out; 360 361 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 362 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 || 363 (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 || 364 (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 || 365 (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 366 (r = sshpkt_send(ssh)) != 0) 367 goto out; 368 369 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 370 (r = kex_send_newkeys(ssh)) != 0) 371 goto out; 372 /* retain copy of hostkey used at initial KEX */ 373 if (kex->initial_hostkey == NULL && 374 (r = sshkey_from_private(server_host_public, 375 &kex->initial_hostkey)) != 0) 376 goto out; 377 /* success */ 378 out: 379 explicit_bzero(hash, sizeof(hash)); 380 sshbuf_free(server_host_key_blob); 381 free(signature); 382 sshbuf_free(shared_secret); 383 sshbuf_free(client_pubkey); 384 sshbuf_free(server_pubkey); 385 return r; 386 } 387