1 /* $OpenBSD: kex.c,v 1.82 2009/10/24 11:13:54 andreas Exp $ */ 2 /* 3 * Copyright (c) 2000, 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 #include <sys/param.h> 29 30 #include <signal.h> 31 #include <stdarg.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 36 #include <openssl/crypto.h> 37 38 #include "xmalloc.h" 39 #include "ssh2.h" 40 #include "buffer.h" 41 #include "packet.h" 42 #include "compat.h" 43 #include "cipher.h" 44 #include "key.h" 45 #include "kex.h" 46 #include "log.h" 47 #include "mac.h" 48 #include "match.h" 49 #include "dispatch.h" 50 #include "monitor.h" 51 #include "roaming.h" 52 #include "canohost.h" 53 54 #if OPENSSL_VERSION_NUMBER >= 0x00907000L 55 # if defined(HAVE_EVP_SHA256) 56 # define evp_ssh_sha256 EVP_sha256 57 # else 58 extern const EVP_MD *evp_ssh_sha256(void); 59 # endif 60 #endif 61 62 /* prototype */ 63 static void kex_kexinit_finish(Kex *); 64 static void kex_choose_conf(Kex *); 65 66 /* put algorithm proposal into buffer */ 67 /* used in sshconnect.c as well as kex.c */ 68 void 69 kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX]) 70 { 71 u_int i; 72 73 buffer_clear(b); 74 /* 75 * add a dummy cookie, the cookie will be overwritten by 76 * kex_send_kexinit(), each time a kexinit is set 77 */ 78 for (i = 0; i < KEX_COOKIE_LEN; i++) 79 buffer_put_char(b, 0); 80 for (i = 0; i < PROPOSAL_MAX; i++) 81 buffer_put_cstring(b, proposal[i]); 82 buffer_put_char(b, 0); /* first_kex_packet_follows */ 83 buffer_put_int(b, 0); /* uint32 reserved */ 84 } 85 86 /* parse buffer and return algorithm proposal */ 87 static char ** 88 kex_buf2prop(Buffer *raw, int *first_kex_follows) 89 { 90 Buffer b; 91 u_int i; 92 char **proposal; 93 94 proposal = xcalloc(PROPOSAL_MAX, sizeof(char *)); 95 96 buffer_init(&b); 97 buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); 98 /* skip cookie */ 99 for (i = 0; i < KEX_COOKIE_LEN; i++) 100 buffer_get_char(&b); 101 /* extract kex init proposal strings */ 102 for (i = 0; i < PROPOSAL_MAX; i++) { 103 proposal[i] = buffer_get_string(&b,NULL); 104 debug2("kex_parse_kexinit: %s", proposal[i]); 105 } 106 /* first kex follows / reserved */ 107 i = buffer_get_char(&b); 108 if (first_kex_follows != NULL) 109 *first_kex_follows = i; 110 debug2("kex_parse_kexinit: first_kex_follows %d ", i); 111 i = buffer_get_int(&b); 112 debug2("kex_parse_kexinit: reserved %u ", i); 113 buffer_free(&b); 114 return proposal; 115 } 116 117 static void 118 kex_prop_free(char **proposal) 119 { 120 u_int i; 121 122 for (i = 0; i < PROPOSAL_MAX; i++) 123 xfree(proposal[i]); 124 xfree(proposal); 125 } 126 127 /* ARGSUSED */ 128 static void 129 kex_protocol_error(int type, u_int32_t seq, void *ctxt) 130 { 131 error("Hm, kex protocol error: type %d seq %u", type, seq); 132 } 133 134 static void 135 kex_reset_dispatch(void) 136 { 137 dispatch_range(SSH2_MSG_TRANSPORT_MIN, 138 SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); 139 dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 140 } 141 142 void 143 kex_finish(Kex *kex) 144 { 145 kex_reset_dispatch(); 146 147 packet_start(SSH2_MSG_NEWKEYS); 148 packet_send(); 149 /* packet_write_wait(); */ 150 debug("SSH2_MSG_NEWKEYS sent"); 151 152 debug("expecting SSH2_MSG_NEWKEYS"); 153 packet_read_expect(SSH2_MSG_NEWKEYS); 154 packet_check_eom(); 155 debug("SSH2_MSG_NEWKEYS received"); 156 157 kex->done = 1; 158 buffer_clear(&kex->peer); 159 /* buffer_clear(&kex->my); */ 160 kex->flags &= ~KEX_INIT_SENT; 161 xfree(kex->name); 162 kex->name = NULL; 163 } 164 165 void 166 kex_send_kexinit(Kex *kex) 167 { 168 u_int32_t rnd = 0; 169 u_char *cookie; 170 u_int i; 171 172 if (kex == NULL) { 173 error("kex_send_kexinit: no kex, cannot rekey"); 174 return; 175 } 176 if (kex->flags & KEX_INIT_SENT) { 177 debug("KEX_INIT_SENT"); 178 return; 179 } 180 kex->done = 0; 181 182 /* generate a random cookie */ 183 if (buffer_len(&kex->my) < KEX_COOKIE_LEN) 184 fatal("kex_send_kexinit: kex proposal too short"); 185 cookie = buffer_ptr(&kex->my); 186 for (i = 0; i < KEX_COOKIE_LEN; i++) { 187 if (i % 4 == 0) 188 rnd = arc4random(); 189 cookie[i] = rnd; 190 rnd >>= 8; 191 } 192 packet_start(SSH2_MSG_KEXINIT); 193 packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my)); 194 packet_send(); 195 debug("SSH2_MSG_KEXINIT sent"); 196 kex->flags |= KEX_INIT_SENT; 197 } 198 199 /* ARGSUSED */ 200 void 201 kex_input_kexinit(int type, u_int32_t seq, void *ctxt) 202 { 203 char *ptr; 204 u_int i, dlen; 205 Kex *kex = (Kex *)ctxt; 206 207 debug("SSH2_MSG_KEXINIT received"); 208 if (kex == NULL) 209 fatal("kex_input_kexinit: no kex, cannot rekey"); 210 211 ptr = packet_get_raw(&dlen); 212 buffer_append(&kex->peer, ptr, dlen); 213 214 /* discard packet */ 215 for (i = 0; i < KEX_COOKIE_LEN; i++) 216 packet_get_char(); 217 for (i = 0; i < PROPOSAL_MAX; i++) 218 xfree(packet_get_string(NULL)); 219 (void) packet_get_char(); 220 (void) packet_get_int(); 221 packet_check_eom(); 222 223 kex_kexinit_finish(kex); 224 } 225 226 Kex * 227 kex_setup(char *proposal[PROPOSAL_MAX]) 228 { 229 Kex *kex; 230 231 kex = xcalloc(1, sizeof(*kex)); 232 buffer_init(&kex->peer); 233 buffer_init(&kex->my); 234 kex_prop2buf(&kex->my, proposal); 235 kex->done = 0; 236 237 kex_send_kexinit(kex); /* we start */ 238 kex_reset_dispatch(); 239 240 return kex; 241 } 242 243 static void 244 kex_kexinit_finish(Kex *kex) 245 { 246 if (!(kex->flags & KEX_INIT_SENT)) 247 kex_send_kexinit(kex); 248 249 kex_choose_conf(kex); 250 251 if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX && 252 kex->kex[kex->kex_type] != NULL) { 253 (kex->kex[kex->kex_type])(kex); 254 } else { 255 fatal("Unsupported key exchange %d", kex->kex_type); 256 } 257 } 258 259 static void 260 choose_enc(Enc *enc, char *client, char *server) 261 { 262 char *name = match_list(client, server, NULL); 263 if (name == NULL) 264 fatal("no matching cipher found: client %s server %s", 265 client, server); 266 if ((enc->cipher = cipher_by_name(name)) == NULL) 267 fatal("matching cipher is not supported: %s", name); 268 enc->name = name; 269 enc->enabled = 0; 270 enc->iv = NULL; 271 enc->key = NULL; 272 enc->key_len = cipher_keylen(enc->cipher); 273 enc->block_size = cipher_blocksize(enc->cipher); 274 } 275 276 static void 277 choose_mac(Mac *mac, char *client, char *server) 278 { 279 char *name = match_list(client, server, NULL); 280 if (name == NULL) 281 fatal("no matching mac found: client %s server %s", 282 client, server); 283 if (mac_setup(mac, name) < 0) 284 fatal("unsupported mac %s", name); 285 /* truncate the key */ 286 if (datafellows & SSH_BUG_HMAC) 287 mac->key_len = 16; 288 mac->name = name; 289 mac->key = NULL; 290 mac->enabled = 0; 291 } 292 293 static void 294 choose_comp(Comp *comp, char *client, char *server) 295 { 296 char *name = match_list(client, server, NULL); 297 if (name == NULL) 298 fatal("no matching comp found: client %s server %s", client, server); 299 if (strcmp(name, "zlib@openssh.com") == 0) { 300 comp->type = COMP_DELAYED; 301 } else if (strcmp(name, "zlib") == 0) { 302 comp->type = COMP_ZLIB; 303 } else if (strcmp(name, "none") == 0) { 304 comp->type = COMP_NONE; 305 } else { 306 fatal("unsupported comp %s", name); 307 } 308 comp->name = name; 309 } 310 311 static void 312 choose_kex(Kex *k, char *client, char *server) 313 { 314 k->name = match_list(client, server, NULL); 315 if (k->name == NULL) 316 fatal("Unable to negotiate a key exchange method"); 317 if (strcmp(k->name, KEX_DH1) == 0) { 318 k->kex_type = KEX_DH_GRP1_SHA1; 319 k->evp_md = EVP_sha1(); 320 } else if (strcmp(k->name, KEX_DH14) == 0) { 321 k->kex_type = KEX_DH_GRP14_SHA1; 322 k->evp_md = EVP_sha1(); 323 } else if (strcmp(k->name, KEX_DHGEX_SHA1) == 0) { 324 k->kex_type = KEX_DH_GEX_SHA1; 325 k->evp_md = EVP_sha1(); 326 #if OPENSSL_VERSION_NUMBER >= 0x00907000L 327 } else if (strcmp(k->name, KEX_DHGEX_SHA256) == 0) { 328 k->kex_type = KEX_DH_GEX_SHA256; 329 k->evp_md = evp_ssh_sha256(); 330 #endif 331 } else 332 fatal("bad kex alg %s", k->name); 333 } 334 335 static void 336 choose_hostkeyalg(Kex *k, char *client, char *server) 337 { 338 char *hostkeyalg = match_list(client, server, NULL); 339 if (hostkeyalg == NULL) 340 fatal("no hostkey alg"); 341 k->hostkey_type = key_type_from_name(hostkeyalg); 342 if (k->hostkey_type == KEY_UNSPEC) 343 fatal("bad hostkey alg '%s'", hostkeyalg); 344 xfree(hostkeyalg); 345 } 346 347 static int 348 proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) 349 { 350 static int check[] = { 351 PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 352 }; 353 int *idx; 354 char *p; 355 356 for (idx = &check[0]; *idx != -1; idx++) { 357 if ((p = strchr(my[*idx], ',')) != NULL) 358 *p = '\0'; 359 if ((p = strchr(peer[*idx], ',')) != NULL) 360 *p = '\0'; 361 if (strcmp(my[*idx], peer[*idx]) != 0) { 362 debug2("proposal mismatch: my %s peer %s", 363 my[*idx], peer[*idx]); 364 return (0); 365 } 366 } 367 debug2("proposals match"); 368 return (1); 369 } 370 371 static void 372 kex_choose_conf(Kex *kex) 373 { 374 Newkeys *newkeys; 375 char **my, **peer; 376 char **cprop, **sprop; 377 int nenc, nmac, ncomp; 378 u_int mode, ctos, need; 379 int first_kex_follows, type; 380 int log_flag = 0; 381 382 int auth_flag; 383 384 auth_flag = packet_authentication_state(); 385 386 debug ("AUTH STATE IS %d", auth_flag); 387 388 my = kex_buf2prop(&kex->my, NULL); 389 peer = kex_buf2prop(&kex->peer, &first_kex_follows); 390 391 if (kex->server) { 392 cprop=peer; 393 sprop=my; 394 } else { 395 cprop=my; 396 sprop=peer; 397 } 398 399 /* Check whether server offers roaming */ 400 if (!kex->server) { 401 char *roaming; 402 roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL); 403 if (roaming) { 404 kex->roaming = 1; 405 xfree(roaming); 406 } 407 } 408 409 /* Algorithm Negotiation */ 410 for (mode = 0; mode < MODE_MAX; mode++) { 411 newkeys = xcalloc(1, sizeof(*newkeys)); 412 kex->newkeys[mode] = newkeys; 413 ctos = (!kex->server && mode == MODE_OUT) || 414 (kex->server && mode == MODE_IN); 415 nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; 416 nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; 417 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; 418 choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]); 419 choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]); 420 choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]); 421 debug("REQUESTED ENC.NAME is '%s'", newkeys->enc.name); 422 if (strcmp(newkeys->enc.name, "none") == 0) { 423 debug("Requesting NONE. Authflag is %d", auth_flag); 424 if (auth_flag == 1) { 425 debug("None requested post authentication."); 426 } else { 427 fatal("Pre-authentication none cipher requests are not allowed."); 428 } 429 } 430 debug("kex: %s %s %s %s", 431 ctos ? "client->server" : "server->client", 432 newkeys->enc.name, 433 newkeys->mac.name, 434 newkeys->comp.name); 435 /* client starts withctos = 0 && log flag = 0 and no log*/ 436 /* 2nd client pass ctos=1 and flag = 1 so no log*/ 437 /* server starts with ctos =1 && log_flag = 0 so log */ 438 /* 2nd sever pass ctos = 1 && log flag = 1 so no log*/ 439 /* -cjr*/ 440 if (ctos && !log_flag) { 441 logit("SSH: Server;Ltype: Kex;Remote: %s-%d;Enc: %s;MAC: %s;Comp: %s", 442 get_remote_ipaddr(), 443 get_remote_port(), 444 newkeys->enc.name, 445 newkeys->mac.name, 446 newkeys->comp.name); 447 } 448 log_flag = 1; 449 } 450 choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]); 451 choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], 452 sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]); 453 need = 0; 454 for (mode = 0; mode < MODE_MAX; mode++) { 455 newkeys = kex->newkeys[mode]; 456 if (need < newkeys->enc.key_len) 457 need = newkeys->enc.key_len; 458 if (need < newkeys->enc.block_size) 459 need = newkeys->enc.block_size; 460 if (need < newkeys->mac.key_len) 461 need = newkeys->mac.key_len; 462 } 463 /* XXX need runden? */ 464 kex->we_need = need; 465 466 /* ignore the next message if the proposals do not match */ 467 if (first_kex_follows && !proposals_match(my, peer) && 468 !(datafellows & SSH_BUG_FIRSTKEX)) { 469 type = packet_read(); 470 debug2("skipping next packet (type %u)", type); 471 } 472 473 kex_prop_free(my); 474 kex_prop_free(peer); 475 } 476 477 static u_char * 478 derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen, 479 BIGNUM *shared_secret) 480 { 481 Buffer b; 482 EVP_MD_CTX md; 483 char c = id; 484 u_int have; 485 int mdsz; 486 u_char *digest; 487 488 if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0) 489 fatal("bad kex md size %d", mdsz); 490 digest = xmalloc(roundup(need, mdsz)); 491 492 buffer_init(&b); 493 buffer_put_bignum2(&b, shared_secret); 494 495 /* K1 = HASH(K || H || "A" || session_id) */ 496 EVP_DigestInit(&md, kex->evp_md); 497 if (!(datafellows & SSH_BUG_DERIVEKEY)) 498 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); 499 EVP_DigestUpdate(&md, hash, hashlen); 500 EVP_DigestUpdate(&md, &c, 1); 501 EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len); 502 EVP_DigestFinal(&md, digest, NULL); 503 504 /* 505 * expand key: 506 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) 507 * Key = K1 || K2 || ... || Kn 508 */ 509 for (have = mdsz; need > have; have += mdsz) { 510 EVP_DigestInit(&md, kex->evp_md); 511 if (!(datafellows & SSH_BUG_DERIVEKEY)) 512 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); 513 EVP_DigestUpdate(&md, hash, hashlen); 514 EVP_DigestUpdate(&md, digest, have); 515 EVP_DigestFinal(&md, digest + have, NULL); 516 } 517 buffer_free(&b); 518 #ifdef DEBUG_KEX 519 fprintf(stderr, "key '%c'== ", c); 520 dump_digest("key", digest, need); 521 #endif 522 return digest; 523 } 524 525 Newkeys *current_keys[MODE_MAX]; 526 527 #define NKEYS 6 528 void 529 kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret) 530 { 531 u_char *keys[NKEYS]; 532 u_int i, mode, ctos; 533 534 for (i = 0; i < NKEYS; i++) { 535 keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen, 536 shared_secret); 537 } 538 539 debug2("kex_derive_keys"); 540 for (mode = 0; mode < MODE_MAX; mode++) { 541 current_keys[mode] = kex->newkeys[mode]; 542 kex->newkeys[mode] = NULL; 543 ctos = (!kex->server && mode == MODE_OUT) || 544 (kex->server && mode == MODE_IN); 545 current_keys[mode]->enc.iv = keys[ctos ? 0 : 1]; 546 current_keys[mode]->enc.key = keys[ctos ? 2 : 3]; 547 current_keys[mode]->mac.key = keys[ctos ? 4 : 5]; 548 } 549 } 550 551 Newkeys * 552 kex_get_newkeys(int mode) 553 { 554 Newkeys *ret; 555 556 ret = current_keys[mode]; 557 current_keys[mode] = NULL; 558 return ret; 559 } 560 561 void 562 derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus, 563 u_int8_t cookie[8], u_int8_t id[16]) 564 { 565 const EVP_MD *evp_md = EVP_md5(); 566 EVP_MD_CTX md; 567 u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE]; 568 int len; 569 570 EVP_DigestInit(&md, evp_md); 571 572 len = BN_num_bytes(host_modulus); 573 if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) 574 fatal("%s: bad host modulus (len %d)", __func__, len); 575 BN_bn2bin(host_modulus, nbuf); 576 EVP_DigestUpdate(&md, nbuf, len); 577 578 len = BN_num_bytes(server_modulus); 579 if (len < (512 / 8) || (u_int)len > sizeof(nbuf)) 580 fatal("%s: bad server modulus (len %d)", __func__, len); 581 BN_bn2bin(server_modulus, nbuf); 582 EVP_DigestUpdate(&md, nbuf, len); 583 584 EVP_DigestUpdate(&md, cookie, 8); 585 586 EVP_DigestFinal(&md, obuf, NULL); 587 memcpy(id, obuf, 16); 588 589 memset(nbuf, 0, sizeof(nbuf)); 590 memset(obuf, 0, sizeof(obuf)); 591 memset(&md, 0, sizeof(md)); 592 } 593 594 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) 595 void 596 dump_digest(char *msg, u_char *digest, int len) 597 { 598 u_int i; 599 600 fprintf(stderr, "%s\n", msg); 601 for (i = 0; i < len; i++) { 602 fprintf(stderr, "%02x", digest[i]); 603 if (i%32 == 31) 604 fprintf(stderr, "\n"); 605 else if (i%8 == 7) 606 fprintf(stderr, " "); 607 } 608 fprintf(stderr, "\n"); 609 } 610 #endif 611