kex.c revision cd7d5faf5bbb52336a6f85578a90b31a648ac3fa
/*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "includes.h"
#include <locale.h>
#include "ssh2.h"
#include "xmalloc.h"
#include "buffer.h"
#include "bufaux.h"
#include "packet.h"
#include "compat.h"
#include "cipher.h"
#include "kex.h"
#include "key.h"
#include "log.h"
#include "mac.h"
#include "match.h"
#include "dispatch.h"
#include "g11n.h"
#ifdef GSSAPI
#include "ssh-gss.h"
#endif
#define KEX_COOKIE_LEN 16
char *session_lang = NULL;
/* prototype */
static void kex_kexinit_finish(Kex *);
static void kex_choose_conf(Kex *);
/* put algorithm proposal into buffer */
static
void
{
int i;
buffer_clear(b);
/*
* add a dummy cookie, the cookie will be overwritten by
* kex_send_kexinit(), each time a kexinit is set
*/
for (i = 0; i < KEX_COOKIE_LEN; i++)
buffer_put_char(b, 0);
for (i = 0; i < PROPOSAL_MAX; i++)
buffer_put_cstring(b, proposal[i]);
buffer_put_char(b, 0); /* first_kex_packet_follows */
buffer_put_int(b, 0); /* uint32 reserved */
}
/* parse buffer and return algorithm proposal */
static
char **
{
Buffer b;
int i;
char **proposal;
buffer_init(&b);
/* skip cookie */
for (i = 0; i < KEX_COOKIE_LEN; i++)
buffer_get_char(&b);
/* extract kex init proposal strings */
for (i = 0; i < PROPOSAL_MAX; i++) {
}
/* first kex follows / reserved */
i = buffer_get_char(&b);
if (first_kex_follows != NULL)
*first_kex_follows = i;
debug2("kex_parse_kexinit: first_kex_follows %d ", i);
i = buffer_get_int(&b);
debug2("kex_parse_kexinit: reserved %d ", i);
buffer_free(&b);
return proposal;
}
static
void
kex_prop_free(char **proposal)
{
int i;
for (i = 0; i < PROPOSAL_MAX; i++)
}
static void
{
}
static void
kex_reset_dispatch(void)
{
#ifdef ALTPRIVSEP
/* unprivileged sshd has a kex packet handler that must not be reset */
debug3("kex_reset_dispatch -- should we dispatch_set(KEXINIT) here? %d && !%d",
if (packet_is_server() && !packet_is_monitor()) {
debug3("kex_reset_dispatch -- skipping dispatch_set(KEXINIT) in unpriv proc");
return;
}
#endif /* ALTPRIVSEP */
}
void
{
packet_send();
/* packet_write_wait(); */
debug("SSH2_MSG_NEWKEYS sent");
#ifdef ALTPRIVSEP
if (packet_is_monitor())
goto skip_newkeys;
#endif /* ALTPRIVSEP */
debug("expecting SSH2_MSG_NEWKEYS");
debug("SSH2_MSG_NEWKEYS received");
#ifdef ALTPRIVSEP
#endif /* ALTPRIVSEP */
/* buffer_clear(&kex->my); */
}
void
{
int i;
error("kex_send_kexinit: no kex, cannot rekey");
return;
}
debug("KEX_INIT_SENT");
return;
}
/* generate a random cookie */
fatal("kex_send_kexinit: kex proposal too short");
for (i = 0; i < KEX_COOKIE_LEN; i++) {
if (i % 4 == 0)
rand = arc4random();
rand >>= 8;
}
packet_send();
debug("SSH2_MSG_KEXINIT sent");
}
void
{
char *ptr;
int i;
debug("SSH2_MSG_KEXINIT received");
fatal("kex_input_kexinit: no kex, cannot rekey");
/* discard packet */
for (i = 0; i < KEX_COOKIE_LEN; i++)
for (i = 0; i < PROPOSAL_MAX; i++)
(void) packet_get_char();
(void) packet_get_int();
}
/*
* This is for GSS keyex, where actual KEX offer can change at rekey
* time due to credential expiration/renewal...
*/
static
void
{
char **prop;
return;
/* Unmarshall my proposal, let the hook modify it, remarshall it */
}
/* Initiate the key exchange by sending the SSH2_MSG_KEXINIT message. */
void
{
}
/*
* Allocate a key exchange structure and populate it with a proposal we are
* going to use. This function does not start the actual key exchange.
*/
Kex *
{
else
return kex;
}
static void
{
else
}
static void
{
else
}
static void
{
}
static void
{
/* truncate the key */
if (datafellows & SSH_BUG_HMAC)
}
static void
{
} else {
}
}
static void
{
server);
/* XXX Finish 3.6/7 merge of kex stuff -- choose_kex() done */
k->kex_type = KEX_DH_GRP1_SHA1;
k->kex_type = KEX_DH_GEX_SHA1;
#ifdef GSSAPI
k->kex_type = KEX_GSS_GRP1_SHA1;
#endif
} else
}
static void
{
if (hostkeyalg == NULL)
fatal("no hostkey alg");
if (k->hostkey_type == KEY_UNSPEC)
}
static int
{
static int check[] = {
};
int *idx;
char *p;
*p = '\0';
*p = '\0';
debug2("proposal mismatch: my %s peer %s",
return (0);
}
}
debug2("proposals match");
return (1);
}
static void
{
int mode;
int ctos; /* direction: if true client-to-server */
int need;
int first_kex_follows, type;
} else {
}
/* Algorithm Negotiation */
debug("kex: %s %s %s %s",
}
need = 0;
}
/* XXX need runden? */
/* ignore the next message if the proposals do not match */
!(datafellows & SSH_BUG_FIRSTKEX)) {
type = packet_read();
}
if (!kex->initial_kex_done) {
/* We propose the same langs for each protocol direction */
/*
* Why oh why did they bother with negotiating langs for
* each protocol direction?!
*
* The semantics of this are vaguely specified, but one can
* imagine using one language (locale) for the whole session and
* a different one for message localization (e.g., 'en_US.UTF-8'
* overall and 'fr' for messages). Weird? Maybe. But lang
* tags don't include codeset info, like locales do...
*
* So, server-side we want:
* - setlocale(LC_ALL, c2s_locale);
* and
* - setlocale(LC_MESSAGES, s2c_locale);
*
* Client-side we don't really care. But we could do:
*
* - when very verbose, tell the use what lang the server's
* messages are in, if left out in the protocol
* - when sending messages to the server, and if applicable, we
* can localize them according to the language negotiated for
* that direction.
*
* But for now we do nothing on the client side.
*/
else
char *locale;
if (locale) {
}
if (plangs != p_langs_s2c &&
p_langs_s2c && *p_langs_s2c) {
if (locale) {
packet_send_debug("Negotiated "
"messages locale: %s", locale);
}
}
}
}
else {
char *lang;
if (lang) {
session_lang = lang;
}
}
}
}
}
static u_char *
{
Buffer b;
char c = id;
int have;
buffer_init(&b);
/* K1 = HASH(K || H || "A" || session_id) */
if (!(datafellows & SSH_BUG_DERIVEKEY))
/*
* expand key:
* Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
* Key = K1 || K2 || ... || Kn
*/
if (!(datafellows & SSH_BUG_DERIVEKEY))
}
buffer_free(&b);
#ifdef DEBUG_KEX
#endif
return digest;
}
#define NKEYS 6
void
{
for (i = 0; i < NKEYS; i++)
debug2("kex_derive_keys");
}
}
Newkeys *
kex_get_newkeys(int mode)
{
return ret;
}
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH)
void
{
int i;
for (i = 0; i< len; i++) {
if (i%32 == 31)
else if (i%8 == 7)
}
}
#endif