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