10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate * 24*7574SJan.Pechanec@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "includes.h" 290Sstevel@tonic-gate RCSID("$OpenBSD: kex.c,v 1.51 2002/06/24 14:55:38 markus Exp $"); 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <locale.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <openssl/crypto.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "ssh2.h" 360Sstevel@tonic-gate #include "xmalloc.h" 370Sstevel@tonic-gate #include "buffer.h" 380Sstevel@tonic-gate #include "bufaux.h" 390Sstevel@tonic-gate #include "packet.h" 400Sstevel@tonic-gate #include "compat.h" 410Sstevel@tonic-gate #include "cipher.h" 420Sstevel@tonic-gate #include "kex.h" 430Sstevel@tonic-gate #include "key.h" 440Sstevel@tonic-gate #include "log.h" 450Sstevel@tonic-gate #include "mac.h" 460Sstevel@tonic-gate #include "match.h" 470Sstevel@tonic-gate #include "dispatch.h" 480Sstevel@tonic-gate #include "g11n.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate #ifdef GSSAPI 510Sstevel@tonic-gate #include "ssh-gss.h" 520Sstevel@tonic-gate #endif 530Sstevel@tonic-gate 540Sstevel@tonic-gate #define KEX_COOKIE_LEN 16 550Sstevel@tonic-gate 560Sstevel@tonic-gate char *session_lang = NULL; 570Sstevel@tonic-gate 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* prototype */ 600Sstevel@tonic-gate static void kex_do_hook(Kex *kex); 610Sstevel@tonic-gate static void kex_kexinit_finish(Kex *); 620Sstevel@tonic-gate static void kex_choose_conf(Kex *); 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* put algorithm proposal into buffer */ 650Sstevel@tonic-gate static 660Sstevel@tonic-gate void 670Sstevel@tonic-gate kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX]) 680Sstevel@tonic-gate { 690Sstevel@tonic-gate int i; 700Sstevel@tonic-gate 710Sstevel@tonic-gate buffer_clear(b); 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * add a dummy cookie, the cookie will be overwritten by 740Sstevel@tonic-gate * kex_send_kexinit(), each time a kexinit is set 750Sstevel@tonic-gate */ 760Sstevel@tonic-gate for (i = 0; i < KEX_COOKIE_LEN; i++) 770Sstevel@tonic-gate buffer_put_char(b, 0); 780Sstevel@tonic-gate for (i = 0; i < PROPOSAL_MAX; i++) 790Sstevel@tonic-gate buffer_put_cstring(b, proposal[i]); 800Sstevel@tonic-gate buffer_put_char(b, 0); /* first_kex_packet_follows */ 810Sstevel@tonic-gate buffer_put_int(b, 0); /* uint32 reserved */ 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* parse buffer and return algorithm proposal */ 850Sstevel@tonic-gate static 860Sstevel@tonic-gate char ** 870Sstevel@tonic-gate kex_buf2prop(Buffer *raw, int *first_kex_follows) 880Sstevel@tonic-gate { 890Sstevel@tonic-gate Buffer b; 900Sstevel@tonic-gate int i; 910Sstevel@tonic-gate char **proposal; 920Sstevel@tonic-gate 930Sstevel@tonic-gate proposal = xmalloc(PROPOSAL_MAX * sizeof(char *)); 940Sstevel@tonic-gate 950Sstevel@tonic-gate buffer_init(&b); 960Sstevel@tonic-gate buffer_append(&b, buffer_ptr(raw), buffer_len(raw)); 970Sstevel@tonic-gate /* skip cookie */ 980Sstevel@tonic-gate for (i = 0; i < KEX_COOKIE_LEN; i++) 990Sstevel@tonic-gate buffer_get_char(&b); 1000Sstevel@tonic-gate /* extract kex init proposal strings */ 1010Sstevel@tonic-gate for (i = 0; i < PROPOSAL_MAX; i++) { 1020Sstevel@tonic-gate proposal[i] = buffer_get_string(&b,NULL); 1030Sstevel@tonic-gate debug2("kex_parse_kexinit: %s", proposal[i]); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate /* first kex follows / reserved */ 1060Sstevel@tonic-gate i = buffer_get_char(&b); 1070Sstevel@tonic-gate if (first_kex_follows != NULL) 1080Sstevel@tonic-gate *first_kex_follows = i; 1090Sstevel@tonic-gate debug2("kex_parse_kexinit: first_kex_follows %d ", i); 1100Sstevel@tonic-gate i = buffer_get_int(&b); 1110Sstevel@tonic-gate debug2("kex_parse_kexinit: reserved %d ", i); 1120Sstevel@tonic-gate buffer_free(&b); 1130Sstevel@tonic-gate return proposal; 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate static 1170Sstevel@tonic-gate void 1180Sstevel@tonic-gate kex_prop_free(char **proposal) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate int i; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate for (i = 0; i < PROPOSAL_MAX; i++) 1230Sstevel@tonic-gate xfree(proposal[i]); 1240Sstevel@tonic-gate xfree(proposal); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate static void 1280Sstevel@tonic-gate kex_protocol_error(int type, u_int32_t seq, void *ctxt) 1290Sstevel@tonic-gate { 1300Sstevel@tonic-gate error("Hm, kex protocol error: type %d seq %u", type, seq); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate static void 1340Sstevel@tonic-gate kex_reset_dispatch(void) 1350Sstevel@tonic-gate { 1360Sstevel@tonic-gate #ifdef ALTPRIVSEP 1370Sstevel@tonic-gate /* unprivileged sshd has a kex packet handler that must not be reset */ 1380Sstevel@tonic-gate debug3("kex_reset_dispatch -- should we dispatch_set(KEXINIT) here? %d && !%d", 1390Sstevel@tonic-gate packet_is_server(), packet_is_monitor()); 1400Sstevel@tonic-gate if (packet_is_server() && !packet_is_monitor()) { 1410Sstevel@tonic-gate debug3("kex_reset_dispatch -- skipping dispatch_set(KEXINIT) in unpriv proc"); 1420Sstevel@tonic-gate return; 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate #endif /* ALTPRIVSEP */ 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate dispatch_range(SSH2_MSG_TRANSPORT_MIN, 1470Sstevel@tonic-gate SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error); 1480Sstevel@tonic-gate dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate void 1520Sstevel@tonic-gate kex_finish(Kex *kex) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate kex_reset_dispatch(); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate packet_start(SSH2_MSG_NEWKEYS); 1570Sstevel@tonic-gate packet_send(); 1580Sstevel@tonic-gate /* packet_write_wait(); */ 1590Sstevel@tonic-gate debug("SSH2_MSG_NEWKEYS sent"); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate #ifdef ALTPRIVSEP 1620Sstevel@tonic-gate if (packet_is_monitor()) 1630Sstevel@tonic-gate goto skip_newkeys; 1640Sstevel@tonic-gate #endif /* ALTPRIVSEP */ 1650Sstevel@tonic-gate debug("expecting SSH2_MSG_NEWKEYS"); 1660Sstevel@tonic-gate packet_read_expect(SSH2_MSG_NEWKEYS); 1670Sstevel@tonic-gate packet_check_eom(); 1680Sstevel@tonic-gate debug("SSH2_MSG_NEWKEYS received"); 1690Sstevel@tonic-gate #ifdef ALTPRIVSEP 1700Sstevel@tonic-gate skip_newkeys: 1710Sstevel@tonic-gate #endif /* ALTPRIVSEP */ 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate kex->done = 1; 1740Sstevel@tonic-gate kex->initial_kex_done = 1; /* never to be cleared once set */ 1750Sstevel@tonic-gate buffer_clear(&kex->peer); 1760Sstevel@tonic-gate /* buffer_clear(&kex->my); */ 1770Sstevel@tonic-gate kex->flags &= ~KEX_INIT_SENT; 1780Sstevel@tonic-gate xfree(kex->name); 1790Sstevel@tonic-gate kex->name = NULL; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate void 1830Sstevel@tonic-gate kex_send_kexinit(Kex *kex) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate u_int32_t rand = 0; 1860Sstevel@tonic-gate u_char *cookie; 1870Sstevel@tonic-gate int i; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (kex == NULL) { 1900Sstevel@tonic-gate error("kex_send_kexinit: no kex, cannot rekey"); 1910Sstevel@tonic-gate return; 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate if (kex->flags & KEX_INIT_SENT) { 1940Sstevel@tonic-gate debug("KEX_INIT_SENT"); 1950Sstevel@tonic-gate return; 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate kex->done = 0; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* update my proposal -- e.g., add/remove GSS kexalgs */ 2000Sstevel@tonic-gate kex_do_hook(kex); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* generate a random cookie */ 2030Sstevel@tonic-gate if (buffer_len(&kex->my) < KEX_COOKIE_LEN) 2040Sstevel@tonic-gate fatal("kex_send_kexinit: kex proposal too short"); 2050Sstevel@tonic-gate cookie = buffer_ptr(&kex->my); 2060Sstevel@tonic-gate for (i = 0; i < KEX_COOKIE_LEN; i++) { 2070Sstevel@tonic-gate if (i % 4 == 0) 2080Sstevel@tonic-gate rand = arc4random(); 2090Sstevel@tonic-gate cookie[i] = rand; 2100Sstevel@tonic-gate rand >>= 8; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate packet_start(SSH2_MSG_KEXINIT); 2130Sstevel@tonic-gate packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my)); 2140Sstevel@tonic-gate packet_send(); 2150Sstevel@tonic-gate debug("SSH2_MSG_KEXINIT sent"); 2160Sstevel@tonic-gate kex->flags |= KEX_INIT_SENT; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate void 2200Sstevel@tonic-gate kex_input_kexinit(int type, u_int32_t seq, void *ctxt) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate char *ptr; 2230Sstevel@tonic-gate u_int dlen; 2240Sstevel@tonic-gate int i; 2250Sstevel@tonic-gate Kex *kex = (Kex *)ctxt; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate debug("SSH2_MSG_KEXINIT received"); 2280Sstevel@tonic-gate if (kex == NULL) 2290Sstevel@tonic-gate fatal("kex_input_kexinit: no kex, cannot rekey"); 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate ptr = packet_get_raw(&dlen); 2320Sstevel@tonic-gate buffer_append(&kex->peer, ptr, dlen); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* discard packet */ 2350Sstevel@tonic-gate for (i = 0; i < KEX_COOKIE_LEN; i++) 2360Sstevel@tonic-gate packet_get_char(); 2370Sstevel@tonic-gate for (i = 0; i < PROPOSAL_MAX; i++) 2380Sstevel@tonic-gate xfree(packet_get_string(NULL)); 2390Sstevel@tonic-gate (void) packet_get_char(); 2400Sstevel@tonic-gate (void) packet_get_int(); 2410Sstevel@tonic-gate packet_check_eom(); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate kex_kexinit_finish(kex); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * This is for GSS keyex, where actual KEX offer can change at rekey 2480Sstevel@tonic-gate * time due to credential expiration/renewal... 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate static 2510Sstevel@tonic-gate void 2520Sstevel@tonic-gate kex_do_hook(Kex *kex) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate char **prop; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (kex->kex_hook == NULL) 2570Sstevel@tonic-gate return; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* Unmarshall my proposal, let the hook modify it, remarshall it */ 2600Sstevel@tonic-gate prop = kex_buf2prop(&kex->my, NULL); 2610Sstevel@tonic-gate buffer_clear(&kex->my); 2620Sstevel@tonic-gate (kex->kex_hook)(kex, prop); 2630Sstevel@tonic-gate kex_prop2buf(&kex->my, prop); 2640Sstevel@tonic-gate kex_prop_free(prop); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 267*7574SJan.Pechanec@Sun.COM /* Initiate the key exchange by sending the SSH2_MSG_KEXINIT message. */ 268*7574SJan.Pechanec@Sun.COM void 269*7574SJan.Pechanec@Sun.COM kex_start(Kex *kex) 270*7574SJan.Pechanec@Sun.COM { 271*7574SJan.Pechanec@Sun.COM kex_send_kexinit(kex); 272*7574SJan.Pechanec@Sun.COM kex_reset_dispatch(); 273*7574SJan.Pechanec@Sun.COM } 274*7574SJan.Pechanec@Sun.COM 275*7574SJan.Pechanec@Sun.COM /* 276*7574SJan.Pechanec@Sun.COM * Allocate a key exchange structure and populate it with a proposal we are 277*7574SJan.Pechanec@Sun.COM * going to use. This function does not start the actual key exchange. 278*7574SJan.Pechanec@Sun.COM */ 2790Sstevel@tonic-gate Kex * 2800Sstevel@tonic-gate kex_setup(const char *host, char *proposal[PROPOSAL_MAX], Kex_hook_func hook) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate Kex *kex; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate kex = xmalloc(sizeof(*kex)); 2850Sstevel@tonic-gate memset(kex, 0, sizeof(*kex)); 2860Sstevel@tonic-gate buffer_init(&kex->peer); 2870Sstevel@tonic-gate buffer_init(&kex->my); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate kex->kex_hook = hook; /* called by kex_send_kexinit() */ 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate if (host != NULL && *host != '\0') 2920Sstevel@tonic-gate kex->serverhost = xstrdup(host); 2930Sstevel@tonic-gate else 2940Sstevel@tonic-gate kex->server = 1; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate kex_prop2buf(&kex->my, proposal); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate return kex; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate static void 3020Sstevel@tonic-gate kex_kexinit_finish(Kex *kex) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate if (!(kex->flags & KEX_INIT_SENT)) 3050Sstevel@tonic-gate kex_send_kexinit(kex); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate kex_choose_conf(kex); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX && 3100Sstevel@tonic-gate kex->kex[kex->kex_type] != NULL) 3110Sstevel@tonic-gate (kex->kex[kex->kex_type])(kex); 3120Sstevel@tonic-gate else 3130Sstevel@tonic-gate fatal("Unsupported key exchange %d", kex->kex_type); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate static void 3170Sstevel@tonic-gate choose_lang(char **lang, char *client, char *server) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate if (datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS) 3200Sstevel@tonic-gate *lang = match_list(client, server, NULL); 3210Sstevel@tonic-gate else 3220Sstevel@tonic-gate *lang = g11n_srvr_locale_negotiate(client, NULL); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate static void 3250Sstevel@tonic-gate choose_enc(Enc *enc, char *client, char *server) 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate char *name = match_list(client, server, NULL); 3280Sstevel@tonic-gate if (name == NULL) 3290Sstevel@tonic-gate fatal("no matching cipher found: client %s server %s", client, server); 3300Sstevel@tonic-gate if ((enc->cipher = cipher_by_name(name)) == NULL) 3310Sstevel@tonic-gate fatal("matching cipher is not supported: %s", name); 3320Sstevel@tonic-gate enc->name = name; 3330Sstevel@tonic-gate enc->enabled = 0; 3340Sstevel@tonic-gate enc->iv = NULL; 3350Sstevel@tonic-gate enc->key = NULL; 3360Sstevel@tonic-gate enc->key_len = cipher_keylen(enc->cipher); 3370Sstevel@tonic-gate enc->block_size = cipher_blocksize(enc->cipher); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate static void 3400Sstevel@tonic-gate choose_mac(Mac *mac, char *client, char *server) 3410Sstevel@tonic-gate { 3420Sstevel@tonic-gate char *name = match_list(client, server, NULL); 3430Sstevel@tonic-gate if (name == NULL) 3440Sstevel@tonic-gate fatal("no matching mac found: client %s server %s", client, server); 3450Sstevel@tonic-gate if (mac_init(mac, name) < 0) 3460Sstevel@tonic-gate fatal("unsupported mac %s", name); 3470Sstevel@tonic-gate /* truncate the key */ 3480Sstevel@tonic-gate if (datafellows & SSH_BUG_HMAC) 3490Sstevel@tonic-gate mac->key_len = 16; 3500Sstevel@tonic-gate mac->name = name; 3510Sstevel@tonic-gate mac->key = NULL; 3520Sstevel@tonic-gate mac->enabled = 0; 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate static void 3550Sstevel@tonic-gate choose_comp(Comp *comp, char *client, char *server) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate char *name = match_list(client, server, NULL); 3580Sstevel@tonic-gate if (name == NULL) 3590Sstevel@tonic-gate fatal("no matching comp found: client %s server %s", client, server); 3600Sstevel@tonic-gate if (strcmp(name, "zlib") == 0) { 3610Sstevel@tonic-gate comp->type = 1; 3620Sstevel@tonic-gate } else if (strcmp(name, "none") == 0) { 3630Sstevel@tonic-gate comp->type = 0; 3640Sstevel@tonic-gate } else { 3650Sstevel@tonic-gate fatal("unsupported comp %s", name); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate comp->name = name; 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate static void 3700Sstevel@tonic-gate choose_kex(Kex *k, char *client, char *server) 3710Sstevel@tonic-gate { 3720Sstevel@tonic-gate k->name = match_list(client, server, NULL); 3730Sstevel@tonic-gate if (k->name == NULL) 374*7574SJan.Pechanec@Sun.COM fatal("no common kex alg: client '%s', server '%s'", client, 375*7574SJan.Pechanec@Sun.COM server); 3760Sstevel@tonic-gate /* XXX Finish 3.6/7 merge of kex stuff -- choose_kex() done */ 3770Sstevel@tonic-gate if (strcmp(k->name, KEX_DH1) == 0) { 3780Sstevel@tonic-gate k->kex_type = KEX_DH_GRP1_SHA1; 3790Sstevel@tonic-gate } else if (strcmp(k->name, KEX_DHGEX) == 0) { 3800Sstevel@tonic-gate k->kex_type = KEX_DH_GEX_SHA1; 3810Sstevel@tonic-gate #ifdef GSSAPI 3820Sstevel@tonic-gate } else if (strncmp(k->name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) == 0) { 3830Sstevel@tonic-gate k->kex_type = KEX_GSS_GRP1_SHA1; 3840Sstevel@tonic-gate #endif 3850Sstevel@tonic-gate } else 3860Sstevel@tonic-gate fatal("bad kex alg %s", k->name); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate static void 3890Sstevel@tonic-gate choose_hostkeyalg(Kex *k, char *client, char *server) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate char *hostkeyalg = match_list(client, server, NULL); 3920Sstevel@tonic-gate if (hostkeyalg == NULL) 3930Sstevel@tonic-gate fatal("no hostkey alg"); 3940Sstevel@tonic-gate k->hostkey_type = key_type_from_name(hostkeyalg); 3950Sstevel@tonic-gate if (k->hostkey_type == KEY_UNSPEC) 3960Sstevel@tonic-gate fatal("bad hostkey alg '%s'", hostkeyalg); 3970Sstevel@tonic-gate xfree(hostkeyalg); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate static int 4010Sstevel@tonic-gate proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX]) 4020Sstevel@tonic-gate { 4030Sstevel@tonic-gate static int check[] = { 4040Sstevel@tonic-gate PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1 4050Sstevel@tonic-gate }; 4060Sstevel@tonic-gate int *idx; 4070Sstevel@tonic-gate char *p; 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate for (idx = &check[0]; *idx != -1; idx++) { 4100Sstevel@tonic-gate if ((p = strchr(my[*idx], ',')) != NULL) 4110Sstevel@tonic-gate *p = '\0'; 4120Sstevel@tonic-gate if ((p = strchr(peer[*idx], ',')) != NULL) 4130Sstevel@tonic-gate *p = '\0'; 4140Sstevel@tonic-gate if (strcmp(my[*idx], peer[*idx]) != 0) { 4150Sstevel@tonic-gate debug2("proposal mismatch: my %s peer %s", 4160Sstevel@tonic-gate my[*idx], peer[*idx]); 4170Sstevel@tonic-gate return (0); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate debug2("proposals match"); 4210Sstevel@tonic-gate return (1); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate static void 4250Sstevel@tonic-gate kex_choose_conf(Kex *kex) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate Newkeys *newkeys; 4280Sstevel@tonic-gate char **my, **peer; 4290Sstevel@tonic-gate char **cprop, **sprop; 4300Sstevel@tonic-gate char *p_langs_c2s, *p_langs_s2c; /* peer's langs */ 4310Sstevel@tonic-gate char *plangs = NULL; /* peer's langs*/ 4320Sstevel@tonic-gate char *mlangs = NULL; /* my langs */ 4330Sstevel@tonic-gate int nenc, nmac, ncomp; 4340Sstevel@tonic-gate int mode; 4350Sstevel@tonic-gate int ctos; /* direction: if true client-to-server */ 4360Sstevel@tonic-gate int need; 4370Sstevel@tonic-gate int first_kex_follows, type; 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate my = kex_buf2prop(&kex->my, NULL); 4400Sstevel@tonic-gate peer = kex_buf2prop(&kex->peer, &first_kex_follows); 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate if (kex->server) { 4430Sstevel@tonic-gate cprop=peer; 4440Sstevel@tonic-gate sprop=my; 4450Sstevel@tonic-gate } else { 4460Sstevel@tonic-gate cprop=my; 4470Sstevel@tonic-gate sprop=peer; 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* Algorithm Negotiation */ 4510Sstevel@tonic-gate for (mode = 0; mode < MODE_MAX; mode++) { 4520Sstevel@tonic-gate newkeys = xmalloc(sizeof(*newkeys)); 4530Sstevel@tonic-gate memset(newkeys, 0, sizeof(*newkeys)); 4540Sstevel@tonic-gate kex->newkeys[mode] = newkeys; 4550Sstevel@tonic-gate ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); 4560Sstevel@tonic-gate nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC; 4570Sstevel@tonic-gate nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC; 4580Sstevel@tonic-gate ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC; 4590Sstevel@tonic-gate choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]); 4600Sstevel@tonic-gate choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]); 4610Sstevel@tonic-gate choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]); 4620Sstevel@tonic-gate debug("kex: %s %s %s %s", 4630Sstevel@tonic-gate ctos ? "client->server" : "server->client", 4640Sstevel@tonic-gate newkeys->enc.name, 4650Sstevel@tonic-gate newkeys->mac.name, 4660Sstevel@tonic-gate newkeys->comp.name); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]); 4690Sstevel@tonic-gate choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS], 4700Sstevel@tonic-gate sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]); 4710Sstevel@tonic-gate need = 0; 4720Sstevel@tonic-gate for (mode = 0; mode < MODE_MAX; mode++) { 4730Sstevel@tonic-gate newkeys = kex->newkeys[mode]; 4740Sstevel@tonic-gate if (need < newkeys->enc.key_len) 4750Sstevel@tonic-gate need = newkeys->enc.key_len; 4760Sstevel@tonic-gate if (need < newkeys->enc.block_size) 4770Sstevel@tonic-gate need = newkeys->enc.block_size; 4780Sstevel@tonic-gate if (need < newkeys->mac.key_len) 4790Sstevel@tonic-gate need = newkeys->mac.key_len; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate /* XXX need runden? */ 4820Sstevel@tonic-gate kex->we_need = need; 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate /* ignore the next message if the proposals do not match */ 4850Sstevel@tonic-gate if (first_kex_follows && !proposals_match(my, peer) && 4860Sstevel@tonic-gate !(datafellows & SSH_BUG_FIRSTKEX)) { 4870Sstevel@tonic-gate type = packet_read(); 4880Sstevel@tonic-gate debug2("skipping next packet (type %u)", type); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* Language/locale negotiation -- not worth doing on re-key */ 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate if (!kex->initial_kex_done) { 4940Sstevel@tonic-gate p_langs_c2s = peer[PROPOSAL_LANG_CTOS]; 4950Sstevel@tonic-gate p_langs_s2c = peer[PROPOSAL_LANG_STOC]; 4960Sstevel@tonic-gate debug("Peer sent proposed langtags, ctos: %s", p_langs_c2s); 4970Sstevel@tonic-gate debug("Peer sent proposed langtags, stoc: %s", p_langs_s2c); 4980Sstevel@tonic-gate plangs = NULL; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* We propose the same langs for each protocol direction */ 5010Sstevel@tonic-gate mlangs = my[PROPOSAL_LANG_STOC]; 5020Sstevel@tonic-gate debug("We proposed langtags, ctos: %s", my[PROPOSAL_LANG_CTOS]); 5030Sstevel@tonic-gate debug("We proposed langtags, stoc: %s", mlangs); 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Why oh why did they bother with negotiating langs for 5070Sstevel@tonic-gate * each protocol direction?! 5080Sstevel@tonic-gate * 5090Sstevel@tonic-gate * The semantics of this are vaguely specified, but one can 5100Sstevel@tonic-gate * imagine using one language (locale) for the whole session and 5110Sstevel@tonic-gate * a different one for message localization (e.g., 'en_US.UTF-8' 5120Sstevel@tonic-gate * overall and 'fr' for messages). Weird? Maybe. But lang 5130Sstevel@tonic-gate * tags don't include codeset info, like locales do... 5140Sstevel@tonic-gate * 5150Sstevel@tonic-gate * So, server-side we want: 5160Sstevel@tonic-gate * - setlocale(LC_ALL, c2s_locale); 5170Sstevel@tonic-gate * and 5180Sstevel@tonic-gate * - setlocale(LC_MESSAGES, s2c_locale); 5190Sstevel@tonic-gate * 5200Sstevel@tonic-gate * Client-side we don't really care. But we could do: 5210Sstevel@tonic-gate * 5220Sstevel@tonic-gate * - when very verbose, tell the use what lang the server's 5230Sstevel@tonic-gate * messages are in, if left out in the protocol 5240Sstevel@tonic-gate * - when sending messages to the server, and if applicable, we 5250Sstevel@tonic-gate * can localize them according to the language negotiated for 5260Sstevel@tonic-gate * that direction. 5270Sstevel@tonic-gate * 5280Sstevel@tonic-gate * But for now we do nothing on the client side. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate if ((p_langs_c2s && *p_langs_c2s) && !(p_langs_s2c && *p_langs_s2c)) 5310Sstevel@tonic-gate plangs = p_langs_c2s; 5320Sstevel@tonic-gate else if ((p_langs_s2c && *p_langs_s2c) && !(p_langs_c2s && *p_langs_c2s)) 5330Sstevel@tonic-gate plangs = p_langs_s2c; 5340Sstevel@tonic-gate else 5350Sstevel@tonic-gate plangs = p_langs_c2s; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate if (kex->server) { 5380Sstevel@tonic-gate if (plangs && mlangs && *plangs && *mlangs) { 5390Sstevel@tonic-gate char *locale; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate choose_lang(&locale, plangs, mlangs); 5420Sstevel@tonic-gate if (locale) { 5430Sstevel@tonic-gate g11n_setlocale(LC_ALL, locale); 5440Sstevel@tonic-gate debug("Negotiated main locale: %s", locale); 5450Sstevel@tonic-gate packet_send_debug("Negotiated main locale: %s", locale); 5465562Sjp161948 xfree(locale); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate if (plangs != p_langs_s2c && 5490Sstevel@tonic-gate p_langs_s2c && *p_langs_s2c) { 5500Sstevel@tonic-gate choose_lang(&locale, p_langs_s2c, mlangs); 5510Sstevel@tonic-gate if (locale) { 5520Sstevel@tonic-gate g11n_setlocale(LC_MESSAGES, locale); 5530Sstevel@tonic-gate debug("Negotiated messages locale: %s", locale); 5545562Sjp161948 packet_send_debug("Negotiated " 5555562Sjp161948 "messages locale: %s", locale); 5565562Sjp161948 xfree(locale); 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate else { 5620Sstevel@tonic-gate if (plangs && mlangs && *plangs && *mlangs && 5630Sstevel@tonic-gate !(datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS)) { 5640Sstevel@tonic-gate char *lang; 5650Sstevel@tonic-gate lang = g11n_clnt_langtag_negotiate(mlangs, plangs); 5660Sstevel@tonic-gate if (lang) { 5670Sstevel@tonic-gate session_lang = lang; 5680Sstevel@tonic-gate debug("Negotiated lang: %s", lang); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate kex_prop_free(my); 5750Sstevel@tonic-gate kex_prop_free(peer); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate static u_char * 5790Sstevel@tonic-gate derive_key(Kex *kex, int id, int need, u_char *hash, BIGNUM *shared_secret) 5800Sstevel@tonic-gate { 5810Sstevel@tonic-gate Buffer b; 5820Sstevel@tonic-gate const EVP_MD *evp_md = EVP_sha1(); 5830Sstevel@tonic-gate EVP_MD_CTX md; 5840Sstevel@tonic-gate char c = id; 5850Sstevel@tonic-gate int have; 5860Sstevel@tonic-gate int mdsz = EVP_MD_size(evp_md); 5870Sstevel@tonic-gate u_char *digest = xmalloc(roundup(need, mdsz)); 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate buffer_init(&b); 5900Sstevel@tonic-gate buffer_put_bignum2(&b, shared_secret); 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* K1 = HASH(K || H || "A" || session_id) */ 5930Sstevel@tonic-gate EVP_DigestInit(&md, evp_md); 5940Sstevel@tonic-gate if (!(datafellows & SSH_BUG_DERIVEKEY)) 5950Sstevel@tonic-gate EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); 5960Sstevel@tonic-gate EVP_DigestUpdate(&md, hash, mdsz); 5970Sstevel@tonic-gate EVP_DigestUpdate(&md, &c, 1); 5980Sstevel@tonic-gate EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len); 5990Sstevel@tonic-gate EVP_DigestFinal(&md, digest, NULL); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * expand key: 6030Sstevel@tonic-gate * Kn = HASH(K || H || K1 || K2 || ... || Kn-1) 6040Sstevel@tonic-gate * Key = K1 || K2 || ... || Kn 6050Sstevel@tonic-gate */ 6060Sstevel@tonic-gate for (have = mdsz; need > have; have += mdsz) { 6070Sstevel@tonic-gate EVP_DigestInit(&md, evp_md); 6080Sstevel@tonic-gate if (!(datafellows & SSH_BUG_DERIVEKEY)) 6090Sstevel@tonic-gate EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); 6100Sstevel@tonic-gate EVP_DigestUpdate(&md, hash, mdsz); 6110Sstevel@tonic-gate EVP_DigestUpdate(&md, digest, have); 6120Sstevel@tonic-gate EVP_DigestFinal(&md, digest + have, NULL); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate buffer_free(&b); 6150Sstevel@tonic-gate #ifdef DEBUG_KEX 6160Sstevel@tonic-gate fprintf(stderr, "key '%c'== ", c); 6170Sstevel@tonic-gate dump_digest("key", digest, need); 6180Sstevel@tonic-gate #endif 6190Sstevel@tonic-gate return digest; 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate Newkeys *current_keys[MODE_MAX]; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate #define NKEYS 6 6250Sstevel@tonic-gate void 6260Sstevel@tonic-gate kex_derive_keys(Kex *kex, u_char *hash, BIGNUM *shared_secret) 6270Sstevel@tonic-gate { 6280Sstevel@tonic-gate u_char *keys[NKEYS]; 6290Sstevel@tonic-gate int i, mode, ctos; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate for (i = 0; i < NKEYS; i++) 6320Sstevel@tonic-gate keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, shared_secret); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate debug2("kex_derive_keys"); 6350Sstevel@tonic-gate for (mode = 0; mode < MODE_MAX; mode++) { 6360Sstevel@tonic-gate current_keys[mode] = kex->newkeys[mode]; 6370Sstevel@tonic-gate kex->newkeys[mode] = NULL; 6380Sstevel@tonic-gate ctos = (!kex->server && mode == MODE_OUT) || (kex->server && mode == MODE_IN); 6390Sstevel@tonic-gate current_keys[mode]->enc.iv = keys[ctos ? 0 : 1]; 6400Sstevel@tonic-gate current_keys[mode]->enc.key = keys[ctos ? 2 : 3]; 6410Sstevel@tonic-gate current_keys[mode]->mac.key = keys[ctos ? 4 : 5]; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate Newkeys * 6460Sstevel@tonic-gate kex_get_newkeys(int mode) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate Newkeys *ret; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate ret = current_keys[mode]; 6510Sstevel@tonic-gate current_keys[mode] = NULL; 6520Sstevel@tonic-gate return ret; 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) 6560Sstevel@tonic-gate void 6570Sstevel@tonic-gate dump_digest(char *msg, u_char *digest, int len) 6580Sstevel@tonic-gate { 6590Sstevel@tonic-gate int i; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate fprintf(stderr, "%s\n", msg); 6620Sstevel@tonic-gate for (i = 0; i< len; i++) { 6630Sstevel@tonic-gate fprintf(stderr, "%02x", digest[i]); 6640Sstevel@tonic-gate if (i%32 == 31) 6650Sstevel@tonic-gate fprintf(stderr, "\n"); 6660Sstevel@tonic-gate else if (i%8 == 7) 6670Sstevel@tonic-gate fprintf(stderr, " "); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate fprintf(stderr, "\n"); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate #endif 672