1 /* $OpenBSD: kexgen.c,v 1.10 2024/09/09 02:39:57 djm Exp $ */ 2 /* 3 * Copyright (c) 2019 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 #include <sys/types.h> 29 30 #include <stdarg.h> 31 #include <stdio.h> 32 #include <string.h> 33 #include <signal.h> 34 35 #include "sshkey.h" 36 #include "kex.h" 37 #include "log.h" 38 #include "packet.h" 39 #include "ssh2.h" 40 #include "sshbuf.h" 41 #include "digest.h" 42 #include "ssherr.h" 43 44 static int input_kex_gen_init(int, u_int32_t, struct ssh *); 45 static int input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh); 46 47 static int 48 kex_gen_hash( 49 int hash_alg, 50 const struct sshbuf *client_version, 51 const struct sshbuf *server_version, 52 const struct sshbuf *client_kexinit, 53 const struct sshbuf *server_kexinit, 54 const struct sshbuf *server_host_key_blob, 55 const struct sshbuf *client_pub, 56 const struct sshbuf *server_pub, 57 const struct sshbuf *shared_secret, 58 u_char *hash, size_t *hashlen) 59 { 60 struct sshbuf *b; 61 int r; 62 63 if (*hashlen < ssh_digest_bytes(hash_alg)) 64 return SSH_ERR_INVALID_ARGUMENT; 65 if ((b = sshbuf_new()) == NULL) 66 return SSH_ERR_ALLOC_FAIL; 67 if ((r = sshbuf_put_stringb(b, client_version)) != 0 || 68 (r = sshbuf_put_stringb(b, server_version)) != 0 || 69 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ 70 (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 || 71 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 72 (r = sshbuf_putb(b, client_kexinit)) != 0 || 73 (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 || 74 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 || 75 (r = sshbuf_putb(b, server_kexinit)) != 0 || 76 (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 || 77 (r = sshbuf_put_stringb(b, client_pub)) != 0 || 78 (r = sshbuf_put_stringb(b, server_pub)) != 0 || 79 (r = sshbuf_putb(b, shared_secret)) != 0) { 80 sshbuf_free(b); 81 return r; 82 } 83 #ifdef DEBUG_KEX 84 sshbuf_dump(b, stderr); 85 #endif 86 if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) { 87 sshbuf_free(b); 88 return SSH_ERR_LIBCRYPTO_ERROR; 89 } 90 sshbuf_free(b); 91 *hashlen = ssh_digest_bytes(hash_alg); 92 #ifdef DEBUG_KEX 93 dump_digest("hash", hash, *hashlen); 94 #endif 95 return 0; 96 } 97 98 int 99 kex_gen_client(struct ssh *ssh) 100 { 101 struct kex *kex = ssh->kex; 102 int r; 103 104 switch (kex->kex_type) { 105 #ifdef WITH_OPENSSL 106 case KEX_DH_GRP1_SHA1: 107 case KEX_DH_GRP14_SHA1: 108 case KEX_DH_GRP14_SHA256: 109 case KEX_DH_GRP16_SHA512: 110 case KEX_DH_GRP18_SHA512: 111 r = kex_dh_keypair(kex); 112 break; 113 case KEX_ECDH_SHA2: 114 r = kex_ecdh_keypair(kex); 115 break; 116 #endif 117 case KEX_C25519_SHA256: 118 r = kex_c25519_keypair(kex); 119 break; 120 case KEX_KEM_SNTRUP761X25519_SHA512: 121 r = kex_kem_sntrup761x25519_keypair(kex); 122 break; 123 case KEX_KEM_MLKEM768X25519_SHA256: 124 r = kex_kem_mlkem768x25519_keypair(kex); 125 break; 126 default: 127 r = SSH_ERR_INVALID_ARGUMENT; 128 break; 129 } 130 if (r != 0) 131 return r; 132 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 || 133 (r = sshpkt_put_stringb(ssh, kex->client_pub)) != 0 || 134 (r = sshpkt_send(ssh)) != 0) 135 return r; 136 debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); 137 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_gen_reply); 138 return 0; 139 } 140 141 static int 142 input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh) 143 { 144 struct kex *kex = ssh->kex; 145 struct sshkey *server_host_key = NULL; 146 struct sshbuf *shared_secret = NULL; 147 struct sshbuf *server_blob = NULL; 148 struct sshbuf *tmp = NULL, *server_host_key_blob = NULL; 149 u_char *signature = NULL; 150 u_char hash[SSH_DIGEST_MAX_LENGTH]; 151 size_t slen, hashlen; 152 int r; 153 154 debug("SSH2_MSG_KEX_ECDH_REPLY received"); 155 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &kex_protocol_error); 156 157 /* hostkey */ 158 if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0) 159 goto out; 160 /* sshkey_fromb() consumes its buffer, so make a copy */ 161 if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) { 162 r = SSH_ERR_ALLOC_FAIL; 163 goto out; 164 } 165 if ((r = sshkey_fromb(tmp, &server_host_key)) != 0) 166 goto out; 167 if ((r = kex_verify_host_key(ssh, server_host_key)) != 0) 168 goto out; 169 170 /* Q_S, server public key */ 171 /* signed H */ 172 if ((r = sshpkt_getb_froms(ssh, &server_blob)) != 0 || 173 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || 174 (r = sshpkt_get_end(ssh)) != 0) 175 goto out; 176 177 /* compute shared secret */ 178 switch (kex->kex_type) { 179 #ifdef WITH_OPENSSL 180 case KEX_DH_GRP1_SHA1: 181 case KEX_DH_GRP14_SHA1: 182 case KEX_DH_GRP14_SHA256: 183 case KEX_DH_GRP16_SHA512: 184 case KEX_DH_GRP18_SHA512: 185 r = kex_dh_dec(kex, server_blob, &shared_secret); 186 break; 187 case KEX_ECDH_SHA2: 188 r = kex_ecdh_dec(kex, server_blob, &shared_secret); 189 break; 190 #endif 191 case KEX_C25519_SHA256: 192 r = kex_c25519_dec(kex, server_blob, &shared_secret); 193 break; 194 case KEX_KEM_SNTRUP761X25519_SHA512: 195 r = kex_kem_sntrup761x25519_dec(kex, server_blob, 196 &shared_secret); 197 break; 198 case KEX_KEM_MLKEM768X25519_SHA256: 199 r = kex_kem_mlkem768x25519_dec(kex, server_blob, 200 &shared_secret); 201 break; 202 default: 203 r = SSH_ERR_INVALID_ARGUMENT; 204 break; 205 } 206 if (r !=0 ) 207 goto out; 208 209 /* calc and verify H */ 210 hashlen = sizeof(hash); 211 if ((r = kex_gen_hash( 212 kex->hash_alg, 213 kex->client_version, 214 kex->server_version, 215 kex->my, 216 kex->peer, 217 server_host_key_blob, 218 kex->client_pub, 219 server_blob, 220 shared_secret, 221 hash, &hashlen)) != 0) 222 goto out; 223 224 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, 225 kex->hostkey_alg, ssh->compat, NULL)) != 0) 226 goto out; 227 228 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 229 (r = kex_send_newkeys(ssh)) != 0) 230 goto out; 231 232 /* save initial signature and hostkey */ 233 if ((kex->flags & KEX_INITIAL) != 0) { 234 if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) { 235 r = SSH_ERR_INTERNAL_ERROR; 236 goto out; 237 } 238 if ((kex->initial_sig = sshbuf_new()) == NULL) { 239 r = SSH_ERR_ALLOC_FAIL; 240 goto out; 241 } 242 if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0) 243 goto out; 244 kex->initial_hostkey = server_host_key; 245 server_host_key = NULL; 246 } 247 /* success */ 248 out: 249 explicit_bzero(hash, sizeof(hash)); 250 explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key)); 251 explicit_bzero(kex->sntrup761_client_key, 252 sizeof(kex->sntrup761_client_key)); 253 explicit_bzero(kex->mlkem768_client_key, 254 sizeof(kex->mlkem768_client_key)); 255 sshbuf_free(server_host_key_blob); 256 free(signature); 257 sshbuf_free(tmp); 258 sshkey_free(server_host_key); 259 sshbuf_free(server_blob); 260 sshbuf_free(shared_secret); 261 sshbuf_free(kex->client_pub); 262 kex->client_pub = NULL; 263 return r; 264 } 265 266 int 267 kex_gen_server(struct ssh *ssh) 268 { 269 debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 270 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_gen_init); 271 return 0; 272 } 273 274 static int 275 input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh) 276 { 277 struct kex *kex = ssh->kex; 278 struct sshkey *server_host_private, *server_host_public; 279 struct sshbuf *shared_secret = NULL; 280 struct sshbuf *server_pubkey = NULL; 281 struct sshbuf *client_pubkey = NULL; 282 struct sshbuf *server_host_key_blob = NULL; 283 u_char *signature = NULL, hash[SSH_DIGEST_MAX_LENGTH]; 284 size_t slen, hashlen; 285 int r; 286 287 debug("SSH2_MSG_KEX_ECDH_INIT received"); 288 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &kex_protocol_error); 289 290 if ((r = kex_load_hostkey(ssh, &server_host_private, 291 &server_host_public)) != 0) 292 goto out; 293 294 if ((r = sshpkt_getb_froms(ssh, &client_pubkey)) != 0 || 295 (r = sshpkt_get_end(ssh)) != 0) 296 goto out; 297 298 /* compute shared secret */ 299 switch (kex->kex_type) { 300 #ifdef WITH_OPENSSL 301 case KEX_DH_GRP1_SHA1: 302 case KEX_DH_GRP14_SHA1: 303 case KEX_DH_GRP14_SHA256: 304 case KEX_DH_GRP16_SHA512: 305 case KEX_DH_GRP18_SHA512: 306 r = kex_dh_enc(kex, client_pubkey, &server_pubkey, 307 &shared_secret); 308 break; 309 case KEX_ECDH_SHA2: 310 r = kex_ecdh_enc(kex, client_pubkey, &server_pubkey, 311 &shared_secret); 312 break; 313 #endif 314 case KEX_C25519_SHA256: 315 r = kex_c25519_enc(kex, client_pubkey, &server_pubkey, 316 &shared_secret); 317 break; 318 case KEX_KEM_SNTRUP761X25519_SHA512: 319 r = kex_kem_sntrup761x25519_enc(kex, client_pubkey, 320 &server_pubkey, &shared_secret); 321 break; 322 case KEX_KEM_MLKEM768X25519_SHA256: 323 r = kex_kem_mlkem768x25519_enc(kex, client_pubkey, 324 &server_pubkey, &shared_secret); 325 break; 326 default: 327 r = SSH_ERR_INVALID_ARGUMENT; 328 break; 329 } 330 if (r !=0 ) 331 goto out; 332 333 /* calc H */ 334 if ((server_host_key_blob = sshbuf_new()) == NULL) { 335 r = SSH_ERR_ALLOC_FAIL; 336 goto out; 337 } 338 if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0) 339 goto out; 340 hashlen = sizeof(hash); 341 if ((r = kex_gen_hash( 342 kex->hash_alg, 343 kex->client_version, 344 kex->server_version, 345 kex->peer, 346 kex->my, 347 server_host_key_blob, 348 client_pubkey, 349 server_pubkey, 350 shared_secret, 351 hash, &hashlen)) != 0) 352 goto out; 353 354 /* sign H */ 355 if ((r = kex->sign(ssh, server_host_private, server_host_public, 356 &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0) 357 goto out; 358 359 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 360 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 || 361 (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 || 362 (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 || 363 (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 364 (r = sshpkt_send(ssh)) != 0) 365 goto out; 366 367 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 || 368 (r = kex_send_newkeys(ssh)) != 0) 369 goto out; 370 /* retain copy of hostkey used at initial KEX */ 371 if (kex->initial_hostkey == NULL && 372 (r = sshkey_from_private(server_host_public, 373 &kex->initial_hostkey)) != 0) 374 goto out; 375 /* success */ 376 out: 377 explicit_bzero(hash, sizeof(hash)); 378 sshbuf_free(server_host_key_blob); 379 free(signature); 380 sshbuf_free(shared_secret); 381 sshbuf_free(client_pubkey); 382 sshbuf_free(server_pubkey); 383 return r; 384 } 385