upstream commit
adapt kex to sshbuf and struct ssh; ok djm@
This commit is contained in:
parent
3fdc88a0de
commit
57d10cbe86
13
auth.h
13
auth.h
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: auth.h,v 1.79 2014/12/22 07:51:30 djm Exp $ */
|
/* $OpenBSD: auth.h,v 1.80 2015/01/19 20:16:15 markus Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
||||||
|
@ -42,6 +42,7 @@
|
||||||
#include <krb5.h>
|
#include <krb5.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct ssh;
|
||||||
struct sshkey;
|
struct sshkey;
|
||||||
|
|
||||||
typedef struct Authctxt Authctxt;
|
typedef struct Authctxt Authctxt;
|
||||||
|
@ -202,12 +203,12 @@ check_key_in_hostfiles(struct passwd *, Key *, const char *,
|
||||||
|
|
||||||
/* hostkey handling */
|
/* hostkey handling */
|
||||||
Key *get_hostkey_by_index(int);
|
Key *get_hostkey_by_index(int);
|
||||||
Key *get_hostkey_public_by_index(int);
|
Key *get_hostkey_public_by_index(int, struct ssh *);
|
||||||
Key *get_hostkey_public_by_type(int);
|
Key *get_hostkey_public_by_type(int, struct ssh *);
|
||||||
Key *get_hostkey_private_by_type(int);
|
Key *get_hostkey_private_by_type(int, struct ssh *);
|
||||||
int get_hostkey_index(Key *);
|
int get_hostkey_index(Key *, struct ssh *);
|
||||||
int ssh1_session_key(BIGNUM *);
|
int ssh1_session_key(BIGNUM *);
|
||||||
void sshd_hostkey_sign(Key *, Key *, u_char **, u_int *, u_char *, u_int);
|
int sshd_hostkey_sign(Key *, Key *, u_char **, size_t *, u_char *, size_t, u_int);
|
||||||
|
|
||||||
/* debug messages during authentication */
|
/* debug messages during authentication */
|
||||||
void auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
|
void auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
|
||||||
|
|
11
clientloop.c
11
clientloop.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: clientloop.c,v 1.264 2015/01/19 20:07:45 markus Exp $ */
|
/* $OpenBSD: clientloop.c,v 1.265 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
||||||
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
||||||
|
@ -1413,8 +1413,7 @@ client_process_output(fd_set *writeset)
|
||||||
static void
|
static void
|
||||||
client_process_buffered_input_packets(void)
|
client_process_buffered_input_packets(void)
|
||||||
{
|
{
|
||||||
dispatch_run(DISPATCH_NONBLOCK, &quit_pending,
|
dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
|
||||||
compat20 ? active_state->kex : NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* scan buf[] for '~' before sending data to the peer */
|
/* scan buf[] for '~' before sending data to the peer */
|
||||||
|
@ -1468,7 +1467,7 @@ client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
|
||||||
{
|
{
|
||||||
fd_set *readset = NULL, *writeset = NULL;
|
fd_set *readset = NULL, *writeset = NULL;
|
||||||
double start_time, total_time;
|
double start_time, total_time;
|
||||||
int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
|
int r, max_fd = 0, max_fd2 = 0, len, rekeying = 0;
|
||||||
u_int64_t ibytes, obytes;
|
u_int64_t ibytes, obytes;
|
||||||
u_int nalloc = 0;
|
u_int nalloc = 0;
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
@ -1598,7 +1597,9 @@ client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
|
||||||
if (need_rekeying || packet_need_rekeying()) {
|
if (need_rekeying || packet_need_rekeying()) {
|
||||||
debug("need rekeying");
|
debug("need rekeying");
|
||||||
active_state->kex->done = 0;
|
active_state->kex->done = 0;
|
||||||
kex_send_kexinit(active_state->kex);
|
if ((r = kex_send_kexinit(active_state)) != 0)
|
||||||
|
fatal("%s: kex_send_kexinit: %s",
|
||||||
|
__func__, ssh_err(r));
|
||||||
need_rekeying = 0;
|
need_rekeying = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
59
dh.c
59
dh.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: dh.c,v 1.53 2013/11/21 00:45:44 djm Exp $ */
|
/* $OpenBSD: dh.c,v 1.54 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -39,6 +39,7 @@
|
||||||
#include "pathnames.h"
|
#include "pathnames.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
|
||||||
static int
|
static int
|
||||||
parse_prime(int linenum, char *line, struct dhgroup *dhg)
|
parse_prime(int linenum, char *line, struct dhgroup *dhg)
|
||||||
|
@ -107,10 +108,11 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((dhg->g = BN_new()) == NULL)
|
if ((dhg->g = BN_new()) == NULL ||
|
||||||
fatal("parse_prime: BN_new failed");
|
(dhg->p = BN_new()) == NULL) {
|
||||||
if ((dhg->p = BN_new()) == NULL)
|
error("parse_prime: BN_new failed");
|
||||||
fatal("parse_prime: BN_new failed");
|
goto fail;
|
||||||
|
}
|
||||||
if (BN_hex2bn(&dhg->g, gen) == 0) {
|
if (BN_hex2bn(&dhg->g, gen) == 0) {
|
||||||
error("moduli:%d: could not parse generator value", linenum);
|
error("moduli:%d: could not parse generator value", linenum);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -128,7 +130,6 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
|
||||||
error("moduli:%d: generator is invalid", linenum);
|
error("moduli:%d: generator is invalid", linenum);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
|
@ -137,7 +138,6 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
|
||||||
if (dhg->p != NULL)
|
if (dhg->p != NULL)
|
||||||
BN_clear_free(dhg->p);
|
BN_clear_free(dhg->p);
|
||||||
dhg->g = dhg->p = NULL;
|
dhg->g = dhg->p = NULL;
|
||||||
error("Bad prime description in line %d", linenum);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,9 +200,11 @@ choose_dh(int min, int wantbits, int max)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if (linenum != which+1)
|
if (linenum != which+1) {
|
||||||
fatal("WARNING: line %d disappeared in %s, giving up",
|
logit("WARNING: line %d disappeared in %s, giving up",
|
||||||
which, _PATH_DH_PRIMES);
|
which, _PATH_DH_PRIMES);
|
||||||
|
return (dh_new_group14());
|
||||||
|
}
|
||||||
|
|
||||||
return (dh_new_group(dhg.g, dhg.p));
|
return (dh_new_group(dhg.g, dhg.p));
|
||||||
}
|
}
|
||||||
|
@ -251,22 +253,22 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
dh_gen_key(DH *dh, int need)
|
dh_gen_key(DH *dh, int need)
|
||||||
{
|
{
|
||||||
int pbits;
|
int pbits;
|
||||||
|
|
||||||
if (need <= 0)
|
if (need < 0 || dh->p == NULL ||
|
||||||
fatal("%s: need <= 0", __func__);
|
(pbits = BN_num_bits(dh->p)) <= 0 ||
|
||||||
if (dh->p == NULL)
|
need > INT_MAX / 2 || 2 * need >= pbits)
|
||||||
fatal("%s: dh->p == NULL", __func__);
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
if ((pbits = BN_num_bits(dh->p)) <= 0)
|
|
||||||
fatal("%s: bits(p) <= 0", __func__);
|
|
||||||
dh->length = MIN(need * 2, pbits - 1);
|
dh->length = MIN(need * 2, pbits - 1);
|
||||||
if (DH_generate_key(dh) == 0)
|
if (DH_generate_key(dh) == 0 ||
|
||||||
fatal("%s: key generation failed", __func__);
|
!dh_pub_is_valid(dh, dh->pub_key)) {
|
||||||
if (!dh_pub_is_valid(dh, dh->pub_key))
|
BN_clear_free(dh->priv_key);
|
||||||
fatal("%s: generated invalid key", __func__);
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
DH *
|
DH *
|
||||||
|
@ -275,13 +277,12 @@ dh_new_group_asc(const char *gen, const char *modulus)
|
||||||
DH *dh;
|
DH *dh;
|
||||||
|
|
||||||
if ((dh = DH_new()) == NULL)
|
if ((dh = DH_new()) == NULL)
|
||||||
fatal("dh_new_group_asc: DH_new");
|
return NULL;
|
||||||
|
if (BN_hex2bn(&dh->p, modulus) == 0 ||
|
||||||
if (BN_hex2bn(&dh->p, modulus) == 0)
|
BN_hex2bn(&dh->g, gen) == 0) {
|
||||||
fatal("BN_hex2bn p");
|
DH_free(dh);
|
||||||
if (BN_hex2bn(&dh->g, gen) == 0)
|
return NULL;
|
||||||
fatal("BN_hex2bn g");
|
}
|
||||||
|
|
||||||
return (dh);
|
return (dh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,7 +297,7 @@ dh_new_group(BIGNUM *gen, BIGNUM *modulus)
|
||||||
DH *dh;
|
DH *dh;
|
||||||
|
|
||||||
if ((dh = DH_new()) == NULL)
|
if ((dh = DH_new()) == NULL)
|
||||||
fatal("dh_new_group: DH_new");
|
return NULL;
|
||||||
dh->p = modulus;
|
dh->p = modulus;
|
||||||
dh->g = gen;
|
dh->g = gen;
|
||||||
|
|
||||||
|
@ -344,7 +345,7 @@ dh_new_group14(void)
|
||||||
* from RFC4419 section 3.
|
* from RFC4419 section 3.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
u_int
|
||||||
dh_estimate(int bits)
|
dh_estimate(int bits)
|
||||||
{
|
{
|
||||||
if (bits <= 112)
|
if (bits <= 112)
|
||||||
|
|
6
dh.h
6
dh.h
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: dh.h,v 1.11 2013/10/08 11:42:13 dtucker Exp $ */
|
/* $OpenBSD: dh.h,v 1.12 2015/01/19 20:16:15 markus Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
||||||
|
@ -38,10 +38,10 @@ DH *dh_new_group(BIGNUM *, BIGNUM *);
|
||||||
DH *dh_new_group1(void);
|
DH *dh_new_group1(void);
|
||||||
DH *dh_new_group14(void);
|
DH *dh_new_group14(void);
|
||||||
|
|
||||||
void dh_gen_key(DH *, int);
|
int dh_gen_key(DH *, int);
|
||||||
int dh_pub_is_valid(DH *, BIGNUM *);
|
int dh_pub_is_valid(DH *, BIGNUM *);
|
||||||
|
|
||||||
int dh_estimate(int);
|
u_int dh_estimate(int);
|
||||||
|
|
||||||
/* Min and max values from RFC4419. */
|
/* Min and max values from RFC4419. */
|
||||||
#define DH_GRP_MIN 1024
|
#define DH_GRP_MIN 1024
|
||||||
|
|
569
kex.c
569
kex.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kex.c,v 1.101 2015/01/19 20:07:45 markus Exp $ */
|
/* $OpenBSD: kex.c,v 1.102 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -37,20 +37,22 @@
|
||||||
#include <openssl/crypto.h>
|
#include <openssl/crypto.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "xmalloc.h"
|
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "mac.h"
|
#include "mac.h"
|
||||||
#include "match.h"
|
#include "match.h"
|
||||||
|
#include "misc.h"
|
||||||
#include "dispatch.h"
|
#include "dispatch.h"
|
||||||
#include "monitor.h"
|
#include "monitor.h"
|
||||||
#include "roaming.h"
|
#include "roaming.h"
|
||||||
|
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
|
|
||||||
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
|
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
|
||||||
|
@ -62,12 +64,12 @@ extern const EVP_MD *evp_ssh_sha256(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* prototype */
|
/* prototype */
|
||||||
static void kex_kexinit_finish(Kex *);
|
static int kex_choose_conf(struct ssh *);
|
||||||
static void kex_choose_conf(Kex *);
|
static int kex_input_newkeys(int, u_int32_t, void *);
|
||||||
|
|
||||||
struct kexalg {
|
struct kexalg {
|
||||||
char *name;
|
char *name;
|
||||||
int type;
|
u_int type;
|
||||||
int ec_nid;
|
int ec_nid;
|
||||||
int hash_alg;
|
int hash_alg;
|
||||||
};
|
};
|
||||||
|
@ -99,7 +101,7 @@ static const struct kexalg kexalgs[] = {
|
||||||
char *
|
char *
|
||||||
kex_alg_list(char sep)
|
kex_alg_list(char sep)
|
||||||
{
|
{
|
||||||
char *ret = NULL;
|
char *ret = NULL, *tmp;
|
||||||
size_t nlen, rlen = 0;
|
size_t nlen, rlen = 0;
|
||||||
const struct kexalg *k;
|
const struct kexalg *k;
|
||||||
|
|
||||||
|
@ -107,7 +109,11 @@ kex_alg_list(char sep)
|
||||||
if (ret != NULL)
|
if (ret != NULL)
|
||||||
ret[rlen++] = sep;
|
ret[rlen++] = sep;
|
||||||
nlen = strlen(k->name);
|
nlen = strlen(k->name);
|
||||||
ret = xrealloc(ret, 1, rlen + nlen + 2);
|
if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
|
||||||
|
free(ret);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ret = tmp;
|
||||||
memcpy(ret + rlen, k->name, nlen + 1);
|
memcpy(ret + rlen, k->name, nlen + 1);
|
||||||
rlen += nlen;
|
rlen += nlen;
|
||||||
}
|
}
|
||||||
|
@ -134,7 +140,8 @@ kex_names_valid(const char *names)
|
||||||
|
|
||||||
if (names == NULL || strcmp(names, "") == 0)
|
if (names == NULL || strcmp(names, "") == 0)
|
||||||
return 0;
|
return 0;
|
||||||
s = cp = xstrdup(names);
|
if ((s = cp = strdup(names)) == NULL)
|
||||||
|
return 0;
|
||||||
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
for ((p = strsep(&cp, ",")); p && *p != '\0';
|
||||||
(p = strsep(&cp, ","))) {
|
(p = strsep(&cp, ","))) {
|
||||||
if (kex_alg_by_name(p) == NULL) {
|
if (kex_alg_by_name(p) == NULL) {
|
||||||
|
@ -149,56 +156,75 @@ kex_names_valid(const char *names)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* put algorithm proposal into buffer */
|
/* put algorithm proposal into buffer */
|
||||||
static void
|
int
|
||||||
kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
|
kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
|
||||||
{
|
{
|
||||||
u_int i;
|
u_int i;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
sshbuf_reset(b);
|
||||||
|
|
||||||
buffer_clear(b);
|
|
||||||
/*
|
/*
|
||||||
* add a dummy cookie, the cookie will be overwritten by
|
* add a dummy cookie, the cookie will be overwritten by
|
||||||
* kex_send_kexinit(), each time a kexinit is set
|
* kex_send_kexinit(), each time a kexinit is set
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
||||||
buffer_put_char(b, 0);
|
if ((r = sshbuf_put_u8(b, 0)) != 0)
|
||||||
for (i = 0; i < PROPOSAL_MAX; i++)
|
return r;
|
||||||
buffer_put_cstring(b, proposal[i]);
|
}
|
||||||
buffer_put_char(b, 0); /* first_kex_packet_follows */
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
||||||
buffer_put_int(b, 0); /* uint32 reserved */
|
if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
if ((r = sshbuf_put_u8(b, 0)) != 0 || /* first_kex_packet_follows */
|
||||||
|
(r = sshbuf_put_u32(b, 0)) != 0) /* uint32 reserved */
|
||||||
|
return r;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse buffer and return algorithm proposal */
|
/* parse buffer and return algorithm proposal */
|
||||||
static char **
|
int
|
||||||
kex_buf2prop(Buffer *raw, int *first_kex_follows)
|
kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct sshbuf *b = NULL;
|
||||||
|
u_char v;
|
||||||
u_int i;
|
u_int i;
|
||||||
char **proposal;
|
char **proposal = NULL;
|
||||||
|
int r;
|
||||||
|
|
||||||
proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
|
*propp = NULL;
|
||||||
|
if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
|
||||||
buffer_init(&b);
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
|
if ((b = sshbuf_fromb(raw)) == NULL) {
|
||||||
/* skip cookie */
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
goto out;
|
||||||
buffer_get_char(&b);
|
}
|
||||||
|
if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
|
||||||
|
goto out;
|
||||||
/* extract kex init proposal strings */
|
/* extract kex init proposal strings */
|
||||||
for (i = 0; i < PROPOSAL_MAX; i++) {
|
for (i = 0; i < PROPOSAL_MAX; i++) {
|
||||||
proposal[i] = buffer_get_cstring(&b,NULL);
|
if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
|
||||||
|
goto out;
|
||||||
debug2("kex_parse_kexinit: %s", proposal[i]);
|
debug2("kex_parse_kexinit: %s", proposal[i]);
|
||||||
}
|
}
|
||||||
/* first kex follows / reserved */
|
/* first kex follows / reserved */
|
||||||
i = buffer_get_char(&b);
|
if ((r = sshbuf_get_u8(b, &v)) != 0 ||
|
||||||
|
(r = sshbuf_get_u32(b, &i)) != 0)
|
||||||
|
goto out;
|
||||||
if (first_kex_follows != NULL)
|
if (first_kex_follows != NULL)
|
||||||
*first_kex_follows = i;
|
*first_kex_follows = i;
|
||||||
debug2("kex_parse_kexinit: first_kex_follows %d ", i);
|
debug2("kex_parse_kexinit: first_kex_follows %d ", v);
|
||||||
i = buffer_get_int(&b);
|
|
||||||
debug2("kex_parse_kexinit: reserved %u ", i);
|
debug2("kex_parse_kexinit: reserved %u ", i);
|
||||||
buffer_free(&b);
|
r = 0;
|
||||||
return proposal;
|
*propp = proposal;
|
||||||
|
out:
|
||||||
|
if (r != 0 && proposal != NULL)
|
||||||
|
kex_prop_free(proposal);
|
||||||
|
sshbuf_free(b);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
kex_prop_free(char **proposal)
|
kex_prop_free(char **proposal)
|
||||||
{
|
{
|
||||||
u_int i;
|
u_int i;
|
||||||
|
@ -217,91 +243,103 @@ kex_protocol_error(int type, u_int32_t seq, void *ctxt)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
kex_reset_dispatch(void)
|
kex_reset_dispatch(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
dispatch_range(SSH2_MSG_TRANSPORT_MIN,
|
ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
|
||||||
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
|
||||||
dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
kex_finish(Kex *kex)
|
kex_send_newkeys(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
kex_reset_dispatch();
|
int r;
|
||||||
|
|
||||||
packet_start(SSH2_MSG_NEWKEYS);
|
kex_reset_dispatch(ssh);
|
||||||
packet_send();
|
if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
|
||||||
/* packet_write_wait(); */
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
return r;
|
||||||
debug("SSH2_MSG_NEWKEYS sent");
|
debug("SSH2_MSG_NEWKEYS sent");
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_NEWKEYS");
|
debug("expecting SSH2_MSG_NEWKEYS");
|
||||||
packet_read_expect(SSH2_MSG_NEWKEYS);
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
|
||||||
packet_check_eom();
|
return 0;
|
||||||
debug("SSH2_MSG_NEWKEYS received");
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
debug("SSH2_MSG_NEWKEYS received");
|
||||||
|
ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
|
||||||
|
if ((r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
return r;
|
||||||
kex->done = 1;
|
kex->done = 1;
|
||||||
buffer_clear(kex->peer);
|
sshbuf_reset(kex->peer);
|
||||||
/* buffer_clear(kex->my); */
|
/* sshbuf_reset(kex->my); */
|
||||||
kex->flags &= ~KEX_INIT_SENT;
|
kex->flags &= ~KEX_INIT_SENT;
|
||||||
free(kex->name);
|
free(kex->name);
|
||||||
kex->name = NULL;
|
kex->name = NULL;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
kex_send_kexinit(Kex *kex)
|
kex_send_kexinit(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
u_int32_t rnd = 0;
|
|
||||||
u_char *cookie;
|
u_char *cookie;
|
||||||
u_int i;
|
struct kex *kex = ssh->kex;
|
||||||
|
int r;
|
||||||
|
|
||||||
if (kex == NULL) {
|
if (kex == NULL)
|
||||||
error("kex_send_kexinit: no kex, cannot rekey");
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
return;
|
if (kex->flags & KEX_INIT_SENT)
|
||||||
}
|
return 0;
|
||||||
if (kex->flags & KEX_INIT_SENT) {
|
|
||||||
debug("KEX_INIT_SENT");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
kex->done = 0;
|
kex->done = 0;
|
||||||
|
|
||||||
/* generate a random cookie */
|
/* generate a random cookie */
|
||||||
if (buffer_len(kex->my) < KEX_COOKIE_LEN)
|
if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
|
||||||
fatal("kex_send_kexinit: kex proposal too short");
|
return SSH_ERR_INVALID_FORMAT;
|
||||||
cookie = buffer_ptr(kex->my);
|
if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
|
||||||
for (i = 0; i < KEX_COOKIE_LEN; i++) {
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
if (i % 4 == 0)
|
arc4random_buf(cookie, KEX_COOKIE_LEN);
|
||||||
rnd = arc4random();
|
|
||||||
cookie[i] = rnd;
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
rnd >>= 8;
|
(r = sshpkt_putb(ssh, kex->my)) != 0 ||
|
||||||
}
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
packet_start(SSH2_MSG_KEXINIT);
|
return r;
|
||||||
packet_put_raw(buffer_ptr(kex->my), buffer_len(kex->my));
|
|
||||||
packet_send();
|
|
||||||
debug("SSH2_MSG_KEXINIT sent");
|
debug("SSH2_MSG_KEXINIT sent");
|
||||||
kex->flags |= KEX_INIT_SENT;
|
kex->flags |= KEX_INIT_SENT;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ARGSUSED */
|
/* ARGSUSED */
|
||||||
int
|
int
|
||||||
kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
|
kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
|
||||||
{
|
{
|
||||||
const char *ptr;
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
const u_char *ptr;
|
||||||
u_int i;
|
u_int i;
|
||||||
size_t dlen;
|
size_t dlen;
|
||||||
Kex *kex = (Kex *)ctxt;
|
int r;
|
||||||
|
|
||||||
debug("SSH2_MSG_KEXINIT received");
|
debug("SSH2_MSG_KEXINIT received");
|
||||||
if (kex == NULL)
|
if (kex == NULL)
|
||||||
fatal("kex_input_kexinit: no kex, cannot rekey");
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
|
||||||
ptr = packet_get_raw(&dlen);
|
ptr = sshpkt_ptr(ssh, &dlen);
|
||||||
buffer_append(kex->peer, ptr, dlen);
|
if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
/* discard packet */
|
/* discard packet */
|
||||||
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
for (i = 0; i < KEX_COOKIE_LEN; i++)
|
||||||
packet_get_char();
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0)
|
||||||
|
return r;
|
||||||
for (i = 0; i < PROPOSAL_MAX; i++)
|
for (i = 0; i < PROPOSAL_MAX; i++)
|
||||||
free(packet_get_string(NULL));
|
if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0)
|
||||||
|
return r;
|
||||||
/*
|
/*
|
||||||
* XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
|
* XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
|
||||||
* KEX method has the server move first, but a server might be using
|
* KEX method has the server move first, but a server might be using
|
||||||
|
@ -312,12 +350,47 @@ kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
|
||||||
* for cases where the server *doesn't* go first. I guess we should
|
* for cases where the server *doesn't* go first. I guess we should
|
||||||
* ignore it when it is set for these cases, which is what we do now.
|
* ignore it when it is set for these cases, which is what we do now.
|
||||||
*/
|
*/
|
||||||
(void) packet_get_char(); /* first_kex_follows */
|
if ((r = sshpkt_get_u8(ssh, NULL)) != 0 || /* first_kex_follows */
|
||||||
(void) packet_get_int(); /* reserved */
|
(r = sshpkt_get_u32(ssh, NULL)) != 0 || /* reserved */
|
||||||
packet_check_eom();
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
kex_kexinit_finish(kex);
|
if (!(kex->flags & KEX_INIT_SENT))
|
||||||
return 0;
|
if ((r = kex_send_kexinit(ssh)) != 0)
|
||||||
|
return r;
|
||||||
|
if ((r = kex_choose_conf(ssh)) != 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
|
||||||
|
return (kex->kex[kex->kex_type])(ssh);
|
||||||
|
|
||||||
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp)
|
||||||
|
{
|
||||||
|
struct kex *kex;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
*kexp = NULL;
|
||||||
|
if ((kex = calloc(1, sizeof(*kex))) == NULL)
|
||||||
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
|
if ((kex->peer = sshbuf_new()) == NULL ||
|
||||||
|
(kex->my = sshbuf_new()) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((r = kex_prop2buf(kex->my, proposal)) != 0)
|
||||||
|
goto out;
|
||||||
|
kex->done = 0;
|
||||||
|
kex_reset_dispatch(ssh);
|
||||||
|
r = 0;
|
||||||
|
*kexp = kex;
|
||||||
|
out:
|
||||||
|
if (r != 0)
|
||||||
|
kex_free(kex);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -351,51 +424,53 @@ kex_free_newkeys(struct newkeys *newkeys)
|
||||||
free(newkeys);
|
free(newkeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
Kex *
|
void
|
||||||
kex_setup(char *proposal[PROPOSAL_MAX])
|
kex_free(struct kex *kex)
|
||||||
{
|
{
|
||||||
struct kex *kex;
|
u_int mode;
|
||||||
|
|
||||||
if ((kex = calloc(1, sizeof(*kex))) == NULL)
|
#ifdef WITH_OPENSSL
|
||||||
fatal("%s: calloc", __func__);
|
if (kex->dh)
|
||||||
if ((kex->peer = sshbuf_new()) == NULL ||
|
DH_free(kex->dh);
|
||||||
(kex->my = sshbuf_new()) == NULL) {
|
if (kex->ec_client_key)
|
||||||
fatal("%s: sshbuf_new", __func__);
|
EC_KEY_free(kex->ec_client_key);
|
||||||
|
#endif
|
||||||
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
||||||
|
kex_free_newkeys(kex->newkeys[mode]);
|
||||||
|
kex->newkeys[mode] = NULL;
|
||||||
}
|
}
|
||||||
kex_prop2buf(kex->my, proposal);
|
sshbuf_free(kex->peer);
|
||||||
kex->done = 0;
|
sshbuf_free(kex->my);
|
||||||
|
free(kex->session_id);
|
||||||
kex_send_kexinit(kex); /* we start */
|
free(kex->client_version_string);
|
||||||
kex_reset_dispatch();
|
free(kex->server_version_string);
|
||||||
|
free(kex);
|
||||||
return kex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
int
|
||||||
kex_kexinit_finish(Kex *kex)
|
kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
|
||||||
{
|
{
|
||||||
if (!(kex->flags & KEX_INIT_SENT))
|
int r;
|
||||||
kex_send_kexinit(kex);
|
|
||||||
|
|
||||||
kex_choose_conf(kex);
|
if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0)
|
||||||
|
return r;
|
||||||
if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
|
if ((r = kex_send_kexinit(ssh)) != 0) { /* we start */
|
||||||
kex->kex[kex->kex_type] != NULL) {
|
kex_free(ssh->kex);
|
||||||
(kex->kex[kex->kex_type])(kex);
|
ssh->kex = NULL;
|
||||||
} else {
|
return r;
|
||||||
fatal("Unsupported key exchange %d", kex->kex_type);
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
choose_enc(Enc *enc, char *client, char *server)
|
choose_enc(struct sshenc *enc, char *client, char *server)
|
||||||
{
|
{
|
||||||
char *name = match_list(client, server, NULL);
|
char *name = match_list(client, server, NULL);
|
||||||
|
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
fatal("no matching cipher found: client %s server %s",
|
return SSH_ERR_NO_CIPHER_ALG_MATCH;
|
||||||
client, server);
|
|
||||||
if ((enc->cipher = cipher_by_name(name)) == NULL)
|
if ((enc->cipher = cipher_by_name(name)) == NULL)
|
||||||
fatal("matching cipher is not supported: %s", name);
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
enc->name = name;
|
enc->name = name;
|
||||||
enc->enabled = 0;
|
enc->enabled = 0;
|
||||||
enc->iv = NULL;
|
enc->iv = NULL;
|
||||||
|
@ -403,31 +478,34 @@ choose_enc(Enc *enc, char *client, char *server)
|
||||||
enc->key = NULL;
|
enc->key = NULL;
|
||||||
enc->key_len = cipher_keylen(enc->cipher);
|
enc->key_len = cipher_keylen(enc->cipher);
|
||||||
enc->block_size = cipher_blocksize(enc->cipher);
|
enc->block_size = cipher_blocksize(enc->cipher);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
choose_mac(Mac *mac, char *client, char *server)
|
choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
|
||||||
{
|
{
|
||||||
char *name = match_list(client, server, NULL);
|
char *name = match_list(client, server, NULL);
|
||||||
|
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
fatal("no matching mac found: client %s server %s",
|
return SSH_ERR_NO_MAC_ALG_MATCH;
|
||||||
client, server);
|
|
||||||
if (mac_setup(mac, name) < 0)
|
if (mac_setup(mac, name) < 0)
|
||||||
fatal("unsupported mac %s", name);
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
/* truncate the key */
|
/* truncate the key */
|
||||||
if (datafellows & SSH_BUG_HMAC)
|
if (ssh->compat & SSH_BUG_HMAC)
|
||||||
mac->key_len = 16;
|
mac->key_len = 16;
|
||||||
mac->name = name;
|
mac->name = name;
|
||||||
mac->key = NULL;
|
mac->key = NULL;
|
||||||
mac->enabled = 0;
|
mac->enabled = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
choose_comp(Comp *comp, char *client, char *server)
|
choose_comp(struct sshcomp *comp, char *client, char *server)
|
||||||
{
|
{
|
||||||
char *name = match_list(client, server, NULL);
|
char *name = match_list(client, server, NULL);
|
||||||
|
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
fatal("no matching comp found: client %s server %s", client, server);
|
return SSH_ERR_NO_COMPRESS_ALG_MATCH;
|
||||||
if (strcmp(name, "zlib@openssh.com") == 0) {
|
if (strcmp(name, "zlib@openssh.com") == 0) {
|
||||||
comp->type = COMP_DELAYED;
|
comp->type = COMP_DELAYED;
|
||||||
} else if (strcmp(name, "zlib") == 0) {
|
} else if (strcmp(name, "zlib") == 0) {
|
||||||
|
@ -435,36 +513,41 @@ choose_comp(Comp *comp, char *client, char *server)
|
||||||
} else if (strcmp(name, "none") == 0) {
|
} else if (strcmp(name, "none") == 0) {
|
||||||
comp->type = COMP_NONE;
|
comp->type = COMP_NONE;
|
||||||
} else {
|
} else {
|
||||||
fatal("unsupported comp %s", name);
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
}
|
}
|
||||||
comp->name = name;
|
comp->name = name;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
choose_kex(Kex *k, char *client, char *server)
|
choose_kex(struct kex *k, char *client, char *server)
|
||||||
{
|
{
|
||||||
const struct kexalg *kexalg;
|
const struct kexalg *kexalg;
|
||||||
|
|
||||||
k->name = match_list(client, server, NULL);
|
k->name = match_list(client, server, NULL);
|
||||||
|
|
||||||
if (k->name == NULL)
|
if (k->name == NULL)
|
||||||
fatal("Unable to negotiate a key exchange method");
|
return SSH_ERR_NO_KEX_ALG_MATCH;
|
||||||
if ((kexalg = kex_alg_by_name(k->name)) == NULL)
|
if ((kexalg = kex_alg_by_name(k->name)) == NULL)
|
||||||
fatal("unsupported kex alg %s", k->name);
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
k->kex_type = kexalg->type;
|
k->kex_type = kexalg->type;
|
||||||
k->hash_alg = kexalg->hash_alg;
|
k->hash_alg = kexalg->hash_alg;
|
||||||
k->ec_nid = kexalg->ec_nid;
|
k->ec_nid = kexalg->ec_nid;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
choose_hostkeyalg(Kex *k, char *client, char *server)
|
choose_hostkeyalg(struct kex *k, char *client, char *server)
|
||||||
{
|
{
|
||||||
char *hostkeyalg = match_list(client, server, NULL);
|
char *hostkeyalg = match_list(client, server, NULL);
|
||||||
|
|
||||||
if (hostkeyalg == NULL)
|
if (hostkeyalg == NULL)
|
||||||
fatal("no hostkey alg");
|
return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
|
||||||
k->hostkey_type = key_type_from_name(hostkeyalg);
|
k->hostkey_type = sshkey_type_from_name(hostkeyalg);
|
||||||
if (k->hostkey_type == KEY_UNSPEC)
|
if (k->hostkey_type == KEY_UNSPEC)
|
||||||
fatal("bad hostkey alg '%s'", hostkeyalg);
|
return SSH_ERR_INTERNAL_ERROR;
|
||||||
free(hostkeyalg);
|
free(hostkeyalg);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -491,18 +574,20 @@ proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
kex_choose_conf(Kex *kex)
|
kex_choose_conf(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
Newkeys *newkeys;
|
struct kex *kex = ssh->kex;
|
||||||
char **my, **peer;
|
struct newkeys *newkeys;
|
||||||
|
char **my = NULL, **peer = NULL;
|
||||||
char **cprop, **sprop;
|
char **cprop, **sprop;
|
||||||
int nenc, nmac, ncomp;
|
int nenc, nmac, ncomp;
|
||||||
u_int mode, ctos, need, dh_need, authlen;
|
u_int mode, ctos, need, dh_need, authlen;
|
||||||
int first_kex_follows, type;
|
int r, first_kex_follows;
|
||||||
|
|
||||||
my = kex_buf2prop(kex->my, NULL);
|
if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0 ||
|
||||||
peer = kex_buf2prop(kex->peer, &first_kex_follows);
|
(r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
if (kex->server) {
|
if (kex->server) {
|
||||||
cprop=peer;
|
cprop=peer;
|
||||||
|
@ -514,8 +599,9 @@ kex_choose_conf(Kex *kex)
|
||||||
|
|
||||||
/* Check whether server offers roaming */
|
/* Check whether server offers roaming */
|
||||||
if (!kex->server) {
|
if (!kex->server) {
|
||||||
char *roaming;
|
char *roaming = match_list(KEX_RESUME,
|
||||||
roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
|
peer[PROPOSAL_KEX_ALGS], NULL);
|
||||||
|
|
||||||
if (roaming) {
|
if (roaming) {
|
||||||
kex->roaming = 1;
|
kex->roaming = 1;
|
||||||
free(roaming);
|
free(roaming);
|
||||||
|
@ -524,28 +610,39 @@ kex_choose_conf(Kex *kex)
|
||||||
|
|
||||||
/* Algorithm Negotiation */
|
/* Algorithm Negotiation */
|
||||||
for (mode = 0; mode < MODE_MAX; mode++) {
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
||||||
newkeys = xcalloc(1, sizeof(*newkeys));
|
if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
kex->newkeys[mode] = newkeys;
|
kex->newkeys[mode] = newkeys;
|
||||||
ctos = (!kex->server && mode == MODE_OUT) ||
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
||||||
(kex->server && mode == MODE_IN);
|
(kex->server && mode == MODE_IN);
|
||||||
nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
|
nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
|
||||||
nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
|
nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
|
||||||
ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
|
ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
|
||||||
choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
|
if ((r = choose_enc(&newkeys->enc, cprop[nenc],
|
||||||
/* ignore mac for authenticated encryption */
|
sprop[nenc])) != 0)
|
||||||
|
goto out;
|
||||||
authlen = cipher_authlen(newkeys->enc.cipher);
|
authlen = cipher_authlen(newkeys->enc.cipher);
|
||||||
if (authlen == 0)
|
/* ignore mac for authenticated encryption */
|
||||||
choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
|
if (authlen == 0 &&
|
||||||
choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
|
(r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
|
||||||
|
sprop[nmac])) != 0)
|
||||||
|
goto out;
|
||||||
|
if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
|
||||||
|
sprop[ncomp])) != 0)
|
||||||
|
goto out;
|
||||||
debug("kex: %s %s %s %s",
|
debug("kex: %s %s %s %s",
|
||||||
ctos ? "client->server" : "server->client",
|
ctos ? "client->server" : "server->client",
|
||||||
newkeys->enc.name,
|
newkeys->enc.name,
|
||||||
authlen == 0 ? newkeys->mac.name : "<implicit>",
|
authlen == 0 ? newkeys->mac.name : "<implicit>",
|
||||||
newkeys->comp.name);
|
newkeys->comp.name);
|
||||||
}
|
}
|
||||||
choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
|
if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
|
||||||
choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
sprop[PROPOSAL_KEX_ALGS])) != 0 ||
|
||||||
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
|
(r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
|
||||||
|
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0)
|
||||||
|
goto out;
|
||||||
need = dh_need = 0;
|
need = dh_need = 0;
|
||||||
for (mode = 0; mode < MODE_MAX; mode++) {
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
||||||
newkeys = kex->newkeys[mode];
|
newkeys = kex->newkeys[mode];
|
||||||
|
@ -564,45 +661,47 @@ kex_choose_conf(Kex *kex)
|
||||||
|
|
||||||
/* ignore the next message if the proposals do not match */
|
/* ignore the next message if the proposals do not match */
|
||||||
if (first_kex_follows && !proposals_match(my, peer) &&
|
if (first_kex_follows && !proposals_match(my, peer) &&
|
||||||
!(datafellows & SSH_BUG_FIRSTKEX)) {
|
!(ssh->compat & SSH_BUG_FIRSTKEX))
|
||||||
type = packet_read();
|
ssh->dispatch_skip_packets = 1;
|
||||||
debug2("skipping next packet (type %u)", type);
|
r = 0;
|
||||||
}
|
out:
|
||||||
|
|
||||||
kex_prop_free(my);
|
kex_prop_free(my);
|
||||||
kex_prop_free(peer);
|
kex_prop_free(peer);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static u_char *
|
static int
|
||||||
derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
|
derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
|
||||||
const u_char *shared_secret, u_int slen)
|
const struct sshbuf *shared_secret, u_char **keyp)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct kex *kex = ssh->kex;
|
||||||
struct ssh_digest_ctx *hashctx;
|
struct ssh_digest_ctx *hashctx = NULL;
|
||||||
char c = id;
|
char c = id;
|
||||||
u_int have;
|
u_int have;
|
||||||
size_t mdsz;
|
size_t mdsz;
|
||||||
u_char *digest;
|
u_char *digest;
|
||||||
|
int r;
|
||||||
|
|
||||||
if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
|
if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
|
||||||
fatal("bad kex md size %zu", mdsz);
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
digest = xmalloc(roundup(need, mdsz));
|
if ((digest = calloc(1, roundup(need, mdsz))) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
buffer_init(&b);
|
goto out;
|
||||||
buffer_append(&b, shared_secret, slen);
|
}
|
||||||
|
|
||||||
/* K1 = HASH(K || H || "A" || session_id) */
|
/* K1 = HASH(K || H || "A" || session_id) */
|
||||||
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL)
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
||||||
fatal("%s: ssh_digest_start failed", __func__);
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
||||||
if (ssh_digest_update_buffer(hashctx, &b) != 0 ||
|
|
||||||
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
||||||
ssh_digest_update(hashctx, &c, 1) != 0 ||
|
ssh_digest_update(hashctx, &c, 1) != 0 ||
|
||||||
ssh_digest_update(hashctx, kex->session_id,
|
ssh_digest_update(hashctx, kex->session_id,
|
||||||
kex->session_id_len) != 0)
|
kex->session_id_len) != 0 ||
|
||||||
fatal("%s: ssh_digest_update failed", __func__);
|
ssh_digest_final(hashctx, digest, mdsz) != 0) {
|
||||||
if (ssh_digest_final(hashctx, digest, mdsz) != 0)
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
fatal("%s: ssh_digest_final failed", __func__);
|
goto out;
|
||||||
|
}
|
||||||
ssh_digest_free(hashctx);
|
ssh_digest_free(hashctx);
|
||||||
|
hashctx = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* expand key:
|
* expand key:
|
||||||
|
@ -610,38 +709,49 @@ derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
|
||||||
* Key = K1 || K2 || ... || Kn
|
* Key = K1 || K2 || ... || Kn
|
||||||
*/
|
*/
|
||||||
for (have = mdsz; need > have; have += mdsz) {
|
for (have = mdsz; need > have; have += mdsz) {
|
||||||
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL)
|
if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
|
||||||
fatal("%s: ssh_digest_start failed", __func__);
|
ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
|
||||||
if (ssh_digest_update_buffer(hashctx, &b) != 0 ||
|
|
||||||
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
ssh_digest_update(hashctx, hash, hashlen) != 0 ||
|
||||||
ssh_digest_update(hashctx, digest, have) != 0)
|
ssh_digest_update(hashctx, digest, have) != 0 ||
|
||||||
fatal("%s: ssh_digest_update failed", __func__);
|
ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
|
||||||
if (ssh_digest_final(hashctx, digest + have, mdsz) != 0)
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
fatal("%s: ssh_digest_final failed", __func__);
|
goto out;
|
||||||
|
}
|
||||||
ssh_digest_free(hashctx);
|
ssh_digest_free(hashctx);
|
||||||
|
hashctx = NULL;
|
||||||
}
|
}
|
||||||
buffer_free(&b);
|
|
||||||
#ifdef DEBUG_KEX
|
#ifdef DEBUG_KEX
|
||||||
fprintf(stderr, "key '%c'== ", c);
|
fprintf(stderr, "key '%c'== ", c);
|
||||||
dump_digest("key", digest, need);
|
dump_digest("key", digest, need);
|
||||||
#endif
|
#endif
|
||||||
return digest;
|
*keyp = digest;
|
||||||
|
digest = NULL;
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
if (digest)
|
||||||
|
free(digest);
|
||||||
|
ssh_digest_free(hashctx);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define NKEYS 6
|
#define NKEYS 6
|
||||||
void
|
int
|
||||||
kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen,
|
kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
|
||||||
const u_char *shared_secret, u_int slen)
|
const struct sshbuf *shared_secret)
|
||||||
{
|
{
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
u_char *keys[NKEYS];
|
u_char *keys[NKEYS];
|
||||||
u_int i, mode, ctos;
|
u_int i, j, mode, ctos;
|
||||||
|
int r;
|
||||||
|
|
||||||
for (i = 0; i < NKEYS; i++) {
|
for (i = 0; i < NKEYS; i++) {
|
||||||
keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
|
if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
|
||||||
shared_secret, slen);
|
shared_secret, &keys[i])) != 0) {
|
||||||
|
for (j = 0; j < i; j++)
|
||||||
|
free(keys[j]);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug2("kex_derive_keys");
|
|
||||||
for (mode = 0; mode < MODE_MAX; mode++) {
|
for (mode = 0; mode < MODE_MAX; mode++) {
|
||||||
ctos = (!kex->server && mode == MODE_OUT) ||
|
ctos = (!kex->server && mode == MODE_OUT) ||
|
||||||
(kex->server && mode == MODE_IN);
|
(kex->server && mode == MODE_IN);
|
||||||
|
@ -649,54 +759,54 @@ kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen,
|
||||||
kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
|
kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
|
||||||
kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
|
kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_OPENSSL
|
#ifdef WITH_OPENSSL
|
||||||
void
|
int
|
||||||
kex_derive_keys_bn(Kex *kex, u_char *hash, u_int hashlen, const BIGNUM *secret)
|
kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
|
||||||
|
const BIGNUM *secret)
|
||||||
{
|
{
|
||||||
Buffer shared_secret;
|
struct sshbuf *shared_secret;
|
||||||
|
int r;
|
||||||
|
|
||||||
buffer_init(&shared_secret);
|
if ((shared_secret = sshbuf_new()) == NULL)
|
||||||
buffer_put_bignum2(&shared_secret, secret);
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
kex_derive_keys(kex, hash, hashlen,
|
if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
|
||||||
buffer_ptr(&shared_secret), buffer_len(&shared_secret));
|
r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
|
||||||
buffer_free(&shared_secret);
|
sshbuf_free(shared_secret);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WITH_SSH1
|
#ifdef WITH_SSH1
|
||||||
void
|
int
|
||||||
derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
|
derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
|
||||||
u_int8_t cookie[8], u_int8_t id[16])
|
u_int8_t cookie[8], u_int8_t id[16])
|
||||||
{
|
{
|
||||||
u_int8_t nbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
|
u_int8_t nbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
|
||||||
int len;
|
struct ssh_digest_ctx *hashctx = NULL;
|
||||||
struct ssh_digest_ctx *hashctx;
|
size_t len;
|
||||||
|
int r;
|
||||||
if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL)
|
|
||||||
fatal("%s: ssh_digest_start", __func__);
|
|
||||||
|
|
||||||
len = BN_num_bytes(host_modulus);
|
len = BN_num_bytes(host_modulus);
|
||||||
if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
|
if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
|
||||||
fatal("%s: bad host modulus (len %d)", __func__, len);
|
return SSH_ERR_KEY_BITS_MISMATCH;
|
||||||
BN_bn2bin(host_modulus, nbuf);
|
if (BN_bn2bin(host_modulus, nbuf) <= 0 ||
|
||||||
if (ssh_digest_update(hashctx, nbuf, len) != 0)
|
(hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
|
||||||
fatal("%s: ssh_digest_update failed", __func__);
|
ssh_digest_update(hashctx, nbuf, len) != 0 ||
|
||||||
|
ssh_digest_update(hashctx, cookie, 8) != 0 ||
|
||||||
len = BN_num_bytes(server_modulus);
|
ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
|
||||||
if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
fatal("%s: bad server modulus (len %d)", __func__, len);
|
goto out;
|
||||||
BN_bn2bin(server_modulus, nbuf);
|
}
|
||||||
if (ssh_digest_update(hashctx, nbuf, len) != 0 ||
|
|
||||||
ssh_digest_update(hashctx, cookie, 8) != 0)
|
|
||||||
fatal("%s: ssh_digest_update failed", __func__);
|
|
||||||
if (ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0)
|
|
||||||
fatal("%s: ssh_digest_final failed", __func__);
|
|
||||||
memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
|
memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
ssh_digest_free(hashctx);
|
||||||
explicit_bzero(nbuf, sizeof(nbuf));
|
explicit_bzero(nbuf, sizeof(nbuf));
|
||||||
explicit_bzero(obuf, sizeof(obuf));
|
explicit_bzero(obuf, sizeof(obuf));
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -704,16 +814,7 @@ derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
|
||||||
void
|
void
|
||||||
dump_digest(char *msg, u_char *digest, int len)
|
dump_digest(char *msg, u_char *digest, int len)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
fprintf(stderr, "%s\n", msg);
|
fprintf(stderr, "%s\n", msg);
|
||||||
for (i = 0; i < len; i++) {
|
sshbuf_dump_data(digest, len, stderr);
|
||||||
fprintf(stderr, "%02x", digest[i]);
|
|
||||||
if (i%32 == 31)
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
else if (i%8 == 7)
|
|
||||||
fprintf(stderr, " ");
|
|
||||||
}
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
121
kex.h
121
kex.h
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kex.h,v 1.68 2015/01/19 20:07:45 markus Exp $ */
|
/* $OpenBSD: kex.h,v 1.69 2015/01/19 20:16:15 markus Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
||||||
|
@ -30,8 +30,8 @@
|
||||||
#include "buffer.h" /* XXX for typedef */
|
#include "buffer.h" /* XXX for typedef */
|
||||||
#include "key.h" /* XXX for typedef */
|
#include "key.h" /* XXX for typedef */
|
||||||
|
|
||||||
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
|
#ifdef WITH_LEAKMALLOC
|
||||||
#include <openssl/ec.h>
|
#include "leakmalloc.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define KEX_COOKIE_LEN 16
|
#define KEX_COOKIE_LEN 16
|
||||||
|
@ -50,6 +50,8 @@
|
||||||
#define COMP_ZLIB 1
|
#define COMP_ZLIB 1
|
||||||
#define COMP_DELAYED 2
|
#define COMP_DELAYED 2
|
||||||
|
|
||||||
|
#define CURVE25519_SIZE 32
|
||||||
|
|
||||||
enum kex_init_proposals {
|
enum kex_init_proposals {
|
||||||
PROPOSAL_KEX_ALGS,
|
PROPOSAL_KEX_ALGS,
|
||||||
PROPOSAL_SERVER_HOST_KEY_ALGS,
|
PROPOSAL_SERVER_HOST_KEY_ALGS,
|
||||||
|
@ -82,12 +84,6 @@ enum kex_exchange {
|
||||||
|
|
||||||
#define KEX_INIT_SENT 0x0001
|
#define KEX_INIT_SENT 0x0001
|
||||||
|
|
||||||
typedef struct kex Kex;
|
|
||||||
typedef struct sshcomp Comp;
|
|
||||||
typedef struct sshmac Mac;
|
|
||||||
typedef struct sshenc Enc;
|
|
||||||
typedef struct newkeys Newkeys;
|
|
||||||
|
|
||||||
struct sshenc {
|
struct sshenc {
|
||||||
char *name;
|
char *name;
|
||||||
const struct sshcipher *cipher;
|
const struct sshcipher *cipher;
|
||||||
|
@ -106,8 +102,11 @@ struct sshcomp {
|
||||||
struct newkeys {
|
struct newkeys {
|
||||||
struct sshenc enc;
|
struct sshenc enc;
|
||||||
struct sshmac mac;
|
struct sshmac mac;
|
||||||
struct sshcomp comp;
|
struct sshcomp comp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ssh;
|
||||||
|
|
||||||
struct kex {
|
struct kex {
|
||||||
u_char *session_id;
|
u_char *session_id;
|
||||||
size_t session_id_len;
|
size_t session_id_len;
|
||||||
|
@ -117,73 +116,87 @@ struct kex {
|
||||||
int server;
|
int server;
|
||||||
char *name;
|
char *name;
|
||||||
int hostkey_type;
|
int hostkey_type;
|
||||||
int kex_type;
|
u_int kex_type;
|
||||||
int roaming;
|
int roaming;
|
||||||
struct sshbuf *my;
|
struct sshbuf *my;
|
||||||
struct sshbuf *peer;
|
struct sshbuf *peer;
|
||||||
sig_atomic_t done;
|
sig_atomic_t done;
|
||||||
int flags;
|
u_int flags;
|
||||||
int hash_alg;
|
int hash_alg;
|
||||||
int ec_nid;
|
int ec_nid;
|
||||||
char *client_version_string;
|
char *client_version_string;
|
||||||
char *server_version_string;
|
char *server_version_string;
|
||||||
int (*verify_host_key)(Key *);
|
int (*verify_host_key)(struct sshkey *, struct ssh *);
|
||||||
Key *(*load_host_public_key)(int);
|
struct sshkey *(*load_host_public_key)(int, struct ssh *);
|
||||||
Key *(*load_host_private_key)(int);
|
struct sshkey *(*load_host_private_key)(int, struct ssh *);
|
||||||
int (*host_key_index)(Key *);
|
int (*host_key_index)(struct sshkey *, struct ssh *);
|
||||||
void (*sign)(Key *, Key *, u_char **, u_int *, u_char *, u_int);
|
int (*sign)(struct sshkey *, struct sshkey *,
|
||||||
void (*kex[KEX_MAX])(Kex *);
|
u_char **, size_t *, u_char *, size_t, u_int);
|
||||||
|
int (*kex[KEX_MAX])(struct ssh *);
|
||||||
|
/* kex specific state */
|
||||||
|
DH *dh; /* DH */
|
||||||
|
u_int min, max, nbits; /* GEX */
|
||||||
|
EC_KEY *ec_client_key; /* ECDH */
|
||||||
|
const EC_GROUP *ec_group; /* ECDH */
|
||||||
|
u_char c25519_client_key[CURVE25519_SIZE]; /* 25519 */
|
||||||
|
u_char c25519_client_pubkey[CURVE25519_SIZE]; /* 25519 */
|
||||||
};
|
};
|
||||||
|
|
||||||
int kex_names_valid(const char *);
|
int kex_names_valid(const char *);
|
||||||
char *kex_alg_list(char);
|
char *kex_alg_list(char);
|
||||||
|
|
||||||
Kex *kex_setup(char *[PROPOSAL_MAX]);
|
int kex_new(struct ssh *, char *[PROPOSAL_MAX], struct kex **);
|
||||||
void kex_finish(Kex *);
|
int kex_setup(struct ssh *, char *[PROPOSAL_MAX]);
|
||||||
void kex_free_newkeys(struct newkeys *);
|
void kex_free_newkeys(struct newkeys *);
|
||||||
|
void kex_free(struct kex *);
|
||||||
|
|
||||||
void kex_send_kexinit(Kex *);
|
int kex_buf2prop(struct sshbuf *, int *, char ***);
|
||||||
|
int kex_prop2buf(struct sshbuf *, char *proposal[PROPOSAL_MAX]);
|
||||||
|
void kex_prop_free(char **);
|
||||||
|
|
||||||
|
int kex_send_kexinit(struct ssh *);
|
||||||
int kex_input_kexinit(int, u_int32_t, void *);
|
int kex_input_kexinit(int, u_int32_t, void *);
|
||||||
void kex_derive_keys(Kex *, u_char *, u_int, const u_char *, u_int);
|
int kex_derive_keys(struct ssh *, u_char *, u_int, const struct sshbuf *);
|
||||||
void kex_derive_keys_bn(Kex *, u_char *, u_int, const BIGNUM *);
|
int kex_derive_keys_bn(struct ssh *, u_char *, u_int, const BIGNUM *);
|
||||||
|
int kex_send_newkeys(struct ssh *);
|
||||||
|
|
||||||
void kexdh_client(Kex *);
|
int kexdh_client(struct ssh *);
|
||||||
void kexdh_server(Kex *);
|
int kexdh_server(struct ssh *);
|
||||||
void kexgex_client(Kex *);
|
int kexgex_client(struct ssh *);
|
||||||
void kexgex_server(Kex *);
|
int kexgex_server(struct ssh *);
|
||||||
void kexecdh_client(Kex *);
|
int kexecdh_client(struct ssh *);
|
||||||
void kexecdh_server(Kex *);
|
int kexecdh_server(struct ssh *);
|
||||||
void kexc25519_client(Kex *);
|
int kexc25519_client(struct ssh *);
|
||||||
void kexc25519_server(Kex *);
|
int kexc25519_server(struct ssh *);
|
||||||
|
|
||||||
void
|
int kex_dh_hash(const char *, const char *,
|
||||||
kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
|
const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
|
||||||
BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
|
const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
|
||||||
void
|
|
||||||
kexgex_hash(int, char *, char *, char *, int, char *,
|
|
||||||
int, u_char *, int, int, int, int, BIGNUM *, BIGNUM *, BIGNUM *,
|
|
||||||
BIGNUM *, BIGNUM *, u_char **, u_int *);
|
|
||||||
#ifdef OPENSSL_HAS_ECC
|
|
||||||
void
|
|
||||||
kex_ecdh_hash(int, const EC_GROUP *, char *, char *, char *, int,
|
|
||||||
char *, int, u_char *, int, const EC_POINT *, const EC_POINT *,
|
|
||||||
const BIGNUM *, u_char **, u_int *);
|
|
||||||
#endif
|
|
||||||
void
|
|
||||||
kex_c25519_hash(int, char *, char *, char *, int,
|
|
||||||
char *, int, u_char *, int, const u_char *, const u_char *,
|
|
||||||
const u_char *, u_int, u_char **, u_int *);
|
|
||||||
|
|
||||||
#define CURVE25519_SIZE 32
|
int kexgex_hash(int, const char *, const char *,
|
||||||
void kexc25519_keygen(u_char[CURVE25519_SIZE], u_char[CURVE25519_SIZE])
|
const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
|
||||||
|
int, int, int,
|
||||||
|
const BIGNUM *, const BIGNUM *, const BIGNUM *,
|
||||||
|
const BIGNUM *, const BIGNUM *,
|
||||||
|
u_char *, size_t *);
|
||||||
|
|
||||||
|
int kex_ecdh_hash(int, const EC_GROUP *, const char *, const char *,
|
||||||
|
const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
|
||||||
|
const EC_POINT *, const EC_POINT *, const BIGNUM *, u_char *, size_t *);
|
||||||
|
|
||||||
|
int kex_c25519_hash(int, const char *, const char *, const char *, size_t,
|
||||||
|
const char *, size_t, const u_char *, size_t, const u_char *, const u_char *,
|
||||||
|
const u_char *, size_t, u_char *, size_t *);
|
||||||
|
|
||||||
|
void kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
|
||||||
__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
|
__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
|
||||||
__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
|
__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
|
||||||
void kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
|
int kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
|
||||||
const u_char pub[CURVE25519_SIZE], Buffer *out)
|
const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
|
||||||
__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
|
__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
|
||||||
__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
|
__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
|
||||||
|
|
||||||
void
|
int
|
||||||
derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
|
derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
|
||||||
|
|
||||||
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
|
#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
|
||||||
|
|
94
kexc25519.c
94
kexc25519.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexc25519.c,v 1.7 2014/05/02 03:27:54 djm Exp $ */
|
/* $OpenBSD: kexc25519.c,v 1.8 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001, 2013 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001, 2013 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -35,13 +35,14 @@
|
||||||
#include <openssl/bn.h>
|
#include <openssl/bn.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "sshbuf.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
|
||||||
extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE],
|
extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE],
|
||||||
const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE])
|
const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE])
|
||||||
|
@ -58,65 +59,70 @@ kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
|
||||||
crypto_scalarmult_curve25519(pub, key, basepoint);
|
crypto_scalarmult_curve25519(pub, key, basepoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
|
kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
|
||||||
const u_char pub[CURVE25519_SIZE], Buffer *out)
|
const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
|
||||||
{
|
{
|
||||||
u_char shared_key[CURVE25519_SIZE];
|
u_char shared_key[CURVE25519_SIZE];
|
||||||
|
int r;
|
||||||
|
|
||||||
crypto_scalarmult_curve25519(shared_key, key, pub);
|
crypto_scalarmult_curve25519(shared_key, key, pub);
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("shared secret", shared_key, CURVE25519_SIZE);
|
dump_digest("shared secret", shared_key, CURVE25519_SIZE);
|
||||||
#endif
|
#endif
|
||||||
buffer_clear(out);
|
sshbuf_reset(out);
|
||||||
buffer_put_bignum2_from_string(out, shared_key, CURVE25519_SIZE);
|
r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE);
|
||||||
explicit_bzero(shared_key, CURVE25519_SIZE);
|
explicit_bzero(shared_key, CURVE25519_SIZE);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
kex_c25519_hash(
|
kex_c25519_hash(
|
||||||
int hash_alg,
|
int hash_alg,
|
||||||
char *client_version_string,
|
const char *client_version_string,
|
||||||
char *server_version_string,
|
const char *server_version_string,
|
||||||
char *ckexinit, int ckexinitlen,
|
const char *ckexinit, size_t ckexinitlen,
|
||||||
char *skexinit, int skexinitlen,
|
const char *skexinit, size_t skexinitlen,
|
||||||
u_char *serverhostkeyblob, int sbloblen,
|
const u_char *serverhostkeyblob, size_t sbloblen,
|
||||||
const u_char client_dh_pub[CURVE25519_SIZE],
|
const u_char client_dh_pub[CURVE25519_SIZE],
|
||||||
const u_char server_dh_pub[CURVE25519_SIZE],
|
const u_char server_dh_pub[CURVE25519_SIZE],
|
||||||
const u_char *shared_secret, u_int secretlen,
|
const u_char *shared_secret, size_t secretlen,
|
||||||
u_char **hash, u_int *hashlen)
|
u_char *hash, size_t *hashlen)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct sshbuf *b;
|
||||||
static u_char digest[SSH_DIGEST_MAX_LENGTH];
|
int r;
|
||||||
|
|
||||||
buffer_init(&b);
|
|
||||||
buffer_put_cstring(&b, client_version_string);
|
|
||||||
buffer_put_cstring(&b, server_version_string);
|
|
||||||
|
|
||||||
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
|
||||||
buffer_put_int(&b, ckexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, ckexinit, ckexinitlen);
|
|
||||||
buffer_put_int(&b, skexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, skexinit, skexinitlen);
|
|
||||||
|
|
||||||
buffer_put_string(&b, serverhostkeyblob, sbloblen);
|
|
||||||
buffer_put_string(&b, client_dh_pub, CURVE25519_SIZE);
|
|
||||||
buffer_put_string(&b, server_dh_pub, CURVE25519_SIZE);
|
|
||||||
buffer_append(&b, shared_secret, secretlen);
|
|
||||||
|
|
||||||
|
if (*hashlen < ssh_digest_bytes(hash_alg))
|
||||||
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
if ((b = sshbuf_new()) == NULL)
|
||||||
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
|
if ((r = sshbuf_put_cstring(b, client_version_string)) < 0 ||
|
||||||
|
(r = sshbuf_put_cstring(b, server_version_string)) < 0 ||
|
||||||
|
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
||||||
|
(r = sshbuf_put_u32(b, ckexinitlen+1)) < 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 ||
|
||||||
|
(r = sshbuf_put(b, ckexinit, ckexinitlen)) < 0 ||
|
||||||
|
(r = sshbuf_put_u32(b, skexinitlen+1)) < 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 ||
|
||||||
|
(r = sshbuf_put(b, skexinit, skexinitlen)) < 0 ||
|
||||||
|
(r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) < 0 ||
|
||||||
|
(r = sshbuf_put_string(b, client_dh_pub, CURVE25519_SIZE)) < 0 ||
|
||||||
|
(r = sshbuf_put_string(b, server_dh_pub, CURVE25519_SIZE)) < 0 ||
|
||||||
|
(r = sshbuf_put(b, shared_secret, secretlen)) < 0) {
|
||||||
|
sshbuf_free(b);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEX
|
#ifdef DEBUG_KEX
|
||||||
buffer_dump(&b);
|
sshbuf_dump(b, stderr);
|
||||||
#endif
|
#endif
|
||||||
if (ssh_digest_buffer(hash_alg, &b, digest, sizeof(digest)) != 0)
|
if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
|
||||||
fatal("%s: digest_buffer failed", __func__);
|
sshbuf_free(b);
|
||||||
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
buffer_free(&b);
|
}
|
||||||
|
sshbuf_free(b);
|
||||||
#ifdef DEBUG_KEX
|
|
||||||
dump_digest("hash", digest, ssh_digest_bytes(hash_alg));
|
|
||||||
#endif
|
|
||||||
*hash = digest;
|
|
||||||
*hashlen = ssh_digest_bytes(hash_alg);
|
*hashlen = ssh_digest_bytes(hash_alg);
|
||||||
|
#ifdef DEBUG_KEX
|
||||||
|
dump_digest("hash", hash, *hashlen);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
159
kexc25519c.c
159
kexc25519c.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexc25519c.c,v 1.5 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexc25519c.c,v 1.6 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -33,97 +33,136 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
#include "digest.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
|
||||||
void
|
static int
|
||||||
kexc25519_client(Kex *kex)
|
input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt);
|
||||||
|
|
||||||
|
int
|
||||||
|
kexc25519_client(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
Key *server_host_key;
|
struct kex *kex = ssh->kex;
|
||||||
u_char client_key[CURVE25519_SIZE];
|
int r;
|
||||||
u_char client_pubkey[CURVE25519_SIZE];
|
|
||||||
u_char *server_pubkey = NULL;
|
|
||||||
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
||||||
u_char *hash;
|
|
||||||
u_int slen, sbloblen, hashlen;
|
|
||||||
Buffer shared_secret;
|
|
||||||
|
|
||||||
kexc25519_keygen(client_key, client_pubkey);
|
|
||||||
|
|
||||||
packet_start(SSH2_MSG_KEX_ECDH_INIT);
|
|
||||||
packet_put_string(client_pubkey, sizeof(client_pubkey));
|
|
||||||
packet_send();
|
|
||||||
debug("sending SSH2_MSG_KEX_ECDH_INIT");
|
|
||||||
|
|
||||||
|
kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey);
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("client private key:", client_key, sizeof(client_key));
|
dump_digest("client private key:", kex->c25519_client_key,
|
||||||
|
sizeof(kex->c25519_client_key));
|
||||||
#endif
|
#endif
|
||||||
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
|
||||||
|
(r = sshpkt_put_string(ssh, kex->c25519_client_pubkey,
|
||||||
|
sizeof(kex->c25519_client_pubkey))) != 0 ||
|
||||||
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
|
debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
|
||||||
packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
struct sshkey *server_host_key = NULL;
|
||||||
|
struct sshbuf *shared_secret = NULL;
|
||||||
|
u_char *server_pubkey = NULL;
|
||||||
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t slen, pklen, sbloblen, hashlen;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if (kex->verify_host_key == NULL) {
|
||||||
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* hostkey */
|
/* hostkey */
|
||||||
server_host_key_blob = packet_get_string(&sbloblen);
|
if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
|
||||||
server_host_key = key_from_blob(server_host_key_blob, sbloblen);
|
&sbloblen)) != 0 ||
|
||||||
if (server_host_key == NULL)
|
(r = sshkey_from_blob(server_host_key_blob, sbloblen,
|
||||||
fatal("cannot decode server_host_key_blob");
|
&server_host_key)) != 0)
|
||||||
if (server_host_key->type != kex->hostkey_type)
|
goto out;
|
||||||
fatal("type mismatch for decoded server_host_key_blob");
|
if (server_host_key->type != kex->hostkey_type) {
|
||||||
if (kex->verify_host_key == NULL)
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
||||||
fatal("cannot verify server_host_key");
|
goto out;
|
||||||
if (kex->verify_host_key(server_host_key) == -1)
|
}
|
||||||
fatal("server_host_key verification failed");
|
if (kex->verify_host_key(server_host_key, ssh) == -1) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* Q_S, server public key */
|
/* Q_S, server public key */
|
||||||
server_pubkey = packet_get_string(&slen);
|
/* signed H */
|
||||||
if (slen != CURVE25519_SIZE)
|
if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
|
||||||
fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
|
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
if (pklen != CURVE25519_SIZE) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
|
dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* signed H */
|
if ((shared_secret = sshbuf_new()) == NULL) {
|
||||||
signature = packet_get_string(&slen);
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
packet_check_eom();
|
goto out;
|
||||||
|
}
|
||||||
buffer_init(&shared_secret);
|
if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey,
|
||||||
kexc25519_shared_key(client_key, server_pubkey, &shared_secret);
|
shared_secret)) < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
/* calc and verify H */
|
/* calc and verify H */
|
||||||
kex_c25519_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_c25519_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
client_pubkey,
|
kex->c25519_client_pubkey,
|
||||||
server_pubkey,
|
server_pubkey,
|
||||||
buffer_ptr(&shared_secret), buffer_len(&shared_secret),
|
sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
|
||||||
&hash, &hashlen
|
hash, &hashlen)) < 0)
|
||||||
);
|
goto out;
|
||||||
free(server_host_key_blob);
|
|
||||||
free(server_pubkey);
|
if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
|
||||||
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
ssh->compat)) != 0)
|
||||||
fatal("key_verify failed for server_host_key");
|
goto out;
|
||||||
key_free(server_host_key);
|
|
||||||
free(signature);
|
|
||||||
|
|
||||||
/* save session id */
|
/* save session id */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
kex_derive_keys(kex, hash, hashlen,
|
|
||||||
buffer_ptr(&shared_secret), buffer_len(&shared_secret));
|
if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
buffer_free(&shared_secret);
|
r = kex_send_newkeys(ssh);
|
||||||
kex_finish(kex);
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
|
||||||
|
free(server_host_key_blob);
|
||||||
|
free(server_pubkey);
|
||||||
|
free(signature);
|
||||||
|
sshkey_free(server_host_key);
|
||||||
|
sshbuf_free(shared_secret);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
131
kexc25519s.c
131
kexc25519s.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexc25519s.c,v 1.5 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexc25519s.c,v 1.6 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -30,97 +30,128 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
|
||||||
void
|
static int input_kex_c25519_init(int, u_int32_t, void *);
|
||||||
kexc25519_server(Kex *kex)
|
|
||||||
|
int
|
||||||
|
kexc25519_server(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
Key *server_host_private, *server_host_public;
|
debug("expecting SSH2_MSG_KEX_ECDH_INIT");
|
||||||
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_c25519_init);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
input_kex_c25519_init(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
struct sshkey *server_host_private, *server_host_public;
|
||||||
|
struct sshbuf *shared_secret = NULL;
|
||||||
u_char *server_host_key_blob = NULL, *signature = NULL;
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
||||||
u_char server_key[CURVE25519_SIZE];
|
u_char server_key[CURVE25519_SIZE];
|
||||||
u_char *client_pubkey = NULL;
|
u_char *client_pubkey = NULL;
|
||||||
u_char server_pubkey[CURVE25519_SIZE];
|
u_char server_pubkey[CURVE25519_SIZE];
|
||||||
u_char *hash;
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
u_int slen, sbloblen, hashlen;
|
size_t slen, pklen, sbloblen, hashlen;
|
||||||
Buffer shared_secret;
|
int r;
|
||||||
|
|
||||||
/* generate private key */
|
/* generate private key */
|
||||||
kexc25519_keygen(server_key, server_pubkey);
|
kexc25519_keygen(server_key, server_pubkey);
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("server private key:", server_key, sizeof(server_key));
|
dump_digest("server private key:", server_key, sizeof(server_key));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (kex->load_host_public_key == NULL ||
|
if (kex->load_host_public_key == NULL ||
|
||||||
kex->load_host_private_key == NULL)
|
kex->load_host_private_key == NULL) {
|
||||||
fatal("Cannot load hostkey");
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
goto out;
|
||||||
if (server_host_public == NULL)
|
}
|
||||||
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
|
||||||
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
ssh)) == NULL ||
|
||||||
|
(server_host_private = kex->load_host_private_key(kex->hostkey_type,
|
||||||
debug("expecting SSH2_MSG_KEX_ECDH_INIT");
|
ssh)) == NULL) {
|
||||||
packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
|
r = SSH_ERR_NO_HOSTKEY_LOADED;
|
||||||
client_pubkey = packet_get_string(&slen);
|
goto out;
|
||||||
if (slen != CURVE25519_SIZE)
|
}
|
||||||
fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
|
|
||||||
packet_check_eom();
|
|
||||||
|
|
||||||
|
if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
if (pklen != CURVE25519_SIZE) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
|
dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
buffer_init(&shared_secret);
|
if ((shared_secret = sshbuf_new()) == NULL) {
|
||||||
kexc25519_shared_key(server_key, client_pubkey, &shared_secret);
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((r = kexc25519_shared_key(server_key, client_pubkey,
|
||||||
|
shared_secret)) < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
/* calc H */
|
/* calc H */
|
||||||
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
|
||||||
kex_c25519_hash(
|
&sbloblen)) != 0)
|
||||||
|
goto out;
|
||||||
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_c25519_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
client_pubkey,
|
client_pubkey,
|
||||||
server_pubkey,
|
server_pubkey,
|
||||||
buffer_ptr(&shared_secret), buffer_len(&shared_secret),
|
sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
|
||||||
&hash, &hashlen
|
hash, &hashlen)) < 0)
|
||||||
);
|
goto out;
|
||||||
|
|
||||||
/* save session id := H */
|
/* save session id := H */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sign H */
|
/* sign H */
|
||||||
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
if ((r = kex->sign(server_host_private, server_host_public,
|
||||||
hash, hashlen);
|
&signature, &slen, hash, hashlen, ssh->compat)) < 0)
|
||||||
|
goto out;
|
||||||
/* destroy_sensitive_data(); */
|
|
||||||
|
|
||||||
/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
|
/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
|
||||||
packet_start(SSH2_MSG_KEX_ECDH_REPLY);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
|
||||||
packet_put_string(server_host_key_blob, sbloblen);
|
(r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
|
||||||
packet_put_string(server_pubkey, sizeof(server_pubkey));
|
(r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 ||
|
||||||
packet_put_string(signature, slen);
|
(r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
|
||||||
packet_send();
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
free(signature);
|
if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
|
r = kex_send_newkeys(ssh);
|
||||||
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
explicit_bzero(server_key, sizeof(server_key));
|
||||||
free(server_host_key_blob);
|
free(server_host_key_blob);
|
||||||
/* have keys, free server key */
|
free(signature);
|
||||||
free(client_pubkey);
|
free(client_pubkey);
|
||||||
|
sshbuf_free(shared_secret);
|
||||||
kex_derive_keys(kex, hash, hashlen,
|
return r;
|
||||||
buffer_ptr(&shared_secret), buffer_len(&shared_secret));
|
|
||||||
buffer_free(&shared_secret);
|
|
||||||
kex_finish(kex);
|
|
||||||
}
|
}
|
||||||
|
|
89
kexdh.c
89
kexdh.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexdh.c,v 1.24 2014/01/09 23:20:00 djm Exp $ */
|
/* $OpenBSD: kexdh.c,v 1.25 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -33,58 +33,61 @@
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
|
||||||
#include "buffer.h"
|
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
#include "log.h"
|
|
||||||
|
|
||||||
void
|
int
|
||||||
kex_dh_hash(
|
kex_dh_hash(
|
||||||
char *client_version_string,
|
const char *client_version_string,
|
||||||
char *server_version_string,
|
const char *server_version_string,
|
||||||
char *ckexinit, int ckexinitlen,
|
const u_char *ckexinit, size_t ckexinitlen,
|
||||||
char *skexinit, int skexinitlen,
|
const u_char *skexinit, size_t skexinitlen,
|
||||||
u_char *serverhostkeyblob, int sbloblen,
|
const u_char *serverhostkeyblob, size_t sbloblen,
|
||||||
BIGNUM *client_dh_pub,
|
const BIGNUM *client_dh_pub,
|
||||||
BIGNUM *server_dh_pub,
|
const BIGNUM *server_dh_pub,
|
||||||
BIGNUM *shared_secret,
|
const BIGNUM *shared_secret,
|
||||||
u_char **hash, u_int *hashlen)
|
u_char *hash, size_t *hashlen)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct sshbuf *b;
|
||||||
static u_char digest[SSH_DIGEST_MAX_LENGTH];
|
int r;
|
||||||
|
|
||||||
buffer_init(&b);
|
|
||||||
buffer_put_cstring(&b, client_version_string);
|
|
||||||
buffer_put_cstring(&b, server_version_string);
|
|
||||||
|
|
||||||
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
|
||||||
buffer_put_int(&b, ckexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, ckexinit, ckexinitlen);
|
|
||||||
buffer_put_int(&b, skexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, skexinit, skexinitlen);
|
|
||||||
|
|
||||||
buffer_put_string(&b, serverhostkeyblob, sbloblen);
|
|
||||||
buffer_put_bignum2(&b, client_dh_pub);
|
|
||||||
buffer_put_bignum2(&b, server_dh_pub);
|
|
||||||
buffer_put_bignum2(&b, shared_secret);
|
|
||||||
|
|
||||||
|
if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
|
||||||
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
if ((b = sshbuf_new()) == NULL)
|
||||||
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
|
if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
|
||||||
|
(r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
|
||||||
|
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
||||||
|
(r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
|
(r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
|
||||||
|
(r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
|
(r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
|
||||||
|
(r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
|
||||||
|
sshbuf_free(b);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEX
|
#ifdef DEBUG_KEX
|
||||||
buffer_dump(&b);
|
sshbuf_dump(b, stderr);
|
||||||
#endif
|
#endif
|
||||||
if (ssh_digest_buffer(SSH_DIGEST_SHA1, &b, digest, sizeof(digest)) != 0)
|
if (ssh_digest_buffer(SSH_DIGEST_SHA1, b, hash, *hashlen) != 0) {
|
||||||
fatal("%s: ssh_digest_buffer failed", __func__);
|
sshbuf_free(b);
|
||||||
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
buffer_free(&b);
|
}
|
||||||
|
sshbuf_free(b);
|
||||||
#ifdef DEBUG_KEX
|
|
||||||
dump_digest("hash", digest, ssh_digest_bytes(SSH_DIGEST_SHA1));
|
|
||||||
#endif
|
|
||||||
*hash = digest;
|
|
||||||
*hashlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
|
*hashlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
|
||||||
|
#ifdef DEBUG_KEX
|
||||||
|
dump_digest("hash", hash, *hashlen);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
194
kexdhc.c
194
kexdhc.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexdhc.c,v 1.16 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexdhc.c,v 1.17 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -36,129 +36,175 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dh.h"
|
#include "dh.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
#include "dispatch.h"
|
||||||
|
#include "compat.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
void
|
static int input_kex_dh(int, u_int32_t, void *);
|
||||||
kexdh_client(Kex *kex)
|
|
||||||
|
int
|
||||||
|
kexdh_client(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
struct kex *kex = ssh->kex;
|
||||||
DH *dh;
|
int r;
|
||||||
Key *server_host_key;
|
|
||||||
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
||||||
u_char *kbuf, *hash;
|
|
||||||
u_int klen, slen, sbloblen, hashlen;
|
|
||||||
int kout;
|
|
||||||
|
|
||||||
/* generate and send 'e', client DH public key */
|
/* generate and send 'e', client DH public key */
|
||||||
switch (kex->kex_type) {
|
switch (kex->kex_type) {
|
||||||
case KEX_DH_GRP1_SHA1:
|
case KEX_DH_GRP1_SHA1:
|
||||||
dh = dh_new_group1();
|
kex->dh = dh_new_group1();
|
||||||
break;
|
break;
|
||||||
case KEX_DH_GRP14_SHA1:
|
case KEX_DH_GRP14_SHA1:
|
||||||
dh = dh_new_group14();
|
kex->dh = dh_new_group14();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if (kex->dh == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
dh_gen_key(dh, kex->we_need * 8);
|
|
||||||
packet_start(SSH2_MSG_KEXDH_INIT);
|
|
||||||
packet_put_bignum2(dh->pub_key);
|
|
||||||
packet_send();
|
|
||||||
|
|
||||||
debug("sending SSH2_MSG_KEXDH_INIT");
|
debug("sending SSH2_MSG_KEXDH_INIT");
|
||||||
|
if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
|
||||||
|
(r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
|
||||||
|
(r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
|
||||||
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
DHparams_print_fp(stderr, dh);
|
DHparams_print_fp(stderr, kex->dh);
|
||||||
fprintf(stderr, "pub= ");
|
fprintf(stderr, "pub= ");
|
||||||
BN_print_fp(stderr, dh->pub_key);
|
BN_print_fp(stderr, kex->dh->pub_key);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEXDH_REPLY");
|
debug("expecting SSH2_MSG_KEXDH_REPLY");
|
||||||
packet_read_expect(SSH2_MSG_KEXDH_REPLY);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
input_kex_dh(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
||||||
|
struct sshkey *server_host_key = NULL;
|
||||||
|
u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t klen = 0, slen, sbloblen, hashlen;
|
||||||
|
int kout, r;
|
||||||
|
|
||||||
|
if (kex->verify_host_key == NULL) {
|
||||||
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
/* key, cert */
|
/* key, cert */
|
||||||
server_host_key_blob = packet_get_string(&sbloblen);
|
if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
|
||||||
server_host_key = key_from_blob(server_host_key_blob, sbloblen);
|
&sbloblen)) != 0 ||
|
||||||
if (server_host_key == NULL)
|
(r = sshkey_from_blob(server_host_key_blob, sbloblen,
|
||||||
fatal("cannot decode server_host_key_blob");
|
&server_host_key)) != 0)
|
||||||
if (server_host_key->type != kex->hostkey_type)
|
goto out;
|
||||||
fatal("type mismatch for decoded server_host_key_blob");
|
if (server_host_key->type != kex->hostkey_type) {
|
||||||
if (kex->verify_host_key == NULL)
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
||||||
fatal("cannot verify server_host_key");
|
goto out;
|
||||||
if (kex->verify_host_key(server_host_key) == -1)
|
}
|
||||||
fatal("server_host_key verification failed");
|
if (kex->verify_host_key(server_host_key, ssh) == -1) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
/* DH parameter f, server public DH key */
|
/* DH parameter f, server public DH key */
|
||||||
if ((dh_server_pub = BN_new()) == NULL)
|
if ((dh_server_pub = BN_new()) == NULL) {
|
||||||
fatal("dh_server_pub == NULL");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
packet_get_bignum2(dh_server_pub);
|
goto out;
|
||||||
|
}
|
||||||
|
/* signed H */
|
||||||
|
if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
|
||||||
|
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
fprintf(stderr, "dh_server_pub= ");
|
fprintf(stderr, "dh_server_pub= ");
|
||||||
BN_print_fp(stderr, dh_server_pub);
|
BN_print_fp(stderr, dh_server_pub);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
debug("bits %d", BN_num_bits(dh_server_pub));
|
debug("bits %d", BN_num_bits(dh_server_pub));
|
||||||
#endif
|
#endif
|
||||||
|
if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
|
||||||
|
sshpkt_disconnect(ssh, "bad server public DH value");
|
||||||
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* signed H */
|
klen = DH_size(kex->dh);
|
||||||
signature = packet_get_string(&slen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
packet_check_eom();
|
(shared_secret = BN_new()) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
if (!dh_pub_is_valid(dh, dh_server_pub))
|
goto out;
|
||||||
packet_disconnect("bad server public DH value");
|
}
|
||||||
|
if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
|
||||||
klen = DH_size(dh);
|
BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
|
||||||
kbuf = xmalloc(klen);
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
|
goto out;
|
||||||
fatal("DH_compute_key: failed");
|
}
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
dump_digest("shared secret", kbuf, kout);
|
dump_digest("shared secret", kbuf, kout);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
|
||||||
fatal("kexdh_client: BN_new failed");
|
|
||||||
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
||||||
fatal("kexdh_client: BN_bin2bn failed");
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
/* calc and verify H */
|
/* calc and verify H */
|
||||||
kex_dh_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_dh_hash(
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
dh->pub_key,
|
kex->dh->pub_key,
|
||||||
dh_server_pub,
|
dh_server_pub,
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
free(server_host_key_blob);
|
|
||||||
BN_clear_free(dh_server_pub);
|
|
||||||
DH_free(dh);
|
|
||||||
|
|
||||||
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
|
||||||
fatal("key_verify failed for server_host_key");
|
ssh->compat)) != 0)
|
||||||
key_free(server_host_key);
|
goto out;
|
||||||
free(signature);
|
|
||||||
|
|
||||||
/* save session id */
|
/* save session id */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
BN_clear_free(shared_secret);
|
r = kex_send_newkeys(ssh);
|
||||||
kex_finish(kex);
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
DH_free(kex->dh);
|
||||||
|
kex->dh = NULL;
|
||||||
|
if (dh_server_pub)
|
||||||
|
BN_clear_free(dh_server_pub);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
|
sshkey_free(server_host_key);
|
||||||
|
free(server_host_key_blob);
|
||||||
|
free(signature);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
184
kexdhs.c
184
kexdhs.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexdhs.c,v 1.19 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexdhs.c,v 1.20 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
*
|
*
|
||||||
|
@ -35,55 +35,88 @@
|
||||||
|
|
||||||
#include <openssl/dh.h>
|
#include <openssl/dh.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dh.h"
|
#include "dh.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
|
||||||
void
|
#include "dispatch.h"
|
||||||
kexdh_server(Kex *kex)
|
#include "compat.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
|
static int input_kex_dh_init(int, u_int32_t, void *);
|
||||||
|
|
||||||
|
int
|
||||||
|
kexdh_server(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
struct kex *kex = ssh->kex;
|
||||||
DH *dh;
|
int r;
|
||||||
Key *server_host_public, *server_host_private;
|
|
||||||
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
|
||||||
u_int sbloblen, klen, hashlen, slen;
|
|
||||||
int kout;
|
|
||||||
|
|
||||||
/* generate server DH public key */
|
/* generate server DH public key */
|
||||||
switch (kex->kex_type) {
|
switch (kex->kex_type) {
|
||||||
case KEX_DH_GRP1_SHA1:
|
case KEX_DH_GRP1_SHA1:
|
||||||
dh = dh_new_group1();
|
kex->dh = dh_new_group1();
|
||||||
break;
|
break;
|
||||||
case KEX_DH_GRP14_SHA1:
|
case KEX_DH_GRP14_SHA1:
|
||||||
dh = dh_new_group14();
|
kex->dh = dh_new_group14();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
dh_gen_key(dh, kex->we_need * 8);
|
if (kex->dh == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEXDH_INIT");
|
debug("expecting SSH2_MSG_KEXDH_INIT");
|
||||||
packet_read_expect(SSH2_MSG_KEXDH_INIT);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
||||||
|
struct sshkey *server_host_public, *server_host_private;
|
||||||
|
u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t sbloblen, slen;
|
||||||
|
size_t klen = 0, hashlen;
|
||||||
|
int kout, r;
|
||||||
|
|
||||||
if (kex->load_host_public_key == NULL ||
|
if (kex->load_host_public_key == NULL ||
|
||||||
kex->load_host_private_key == NULL)
|
kex->load_host_private_key == NULL) {
|
||||||
fatal("Cannot load hostkey");
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
goto out;
|
||||||
if (server_host_public == NULL)
|
}
|
||||||
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
|
||||||
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
ssh)) == NULL ||
|
||||||
|
(server_host_private = kex->load_host_private_key(kex->hostkey_type,
|
||||||
|
ssh)) == NULL) {
|
||||||
|
r = SSH_ERR_NO_HOSTKEY_LOADED;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* key, cert */
|
/* key, cert */
|
||||||
if ((dh_client_pub = BN_new()) == NULL)
|
if ((dh_client_pub = BN_new()) == NULL) {
|
||||||
fatal("dh_client_pub == NULL");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
packet_get_bignum2(dh_client_pub);
|
goto out;
|
||||||
packet_check_eom();
|
}
|
||||||
|
if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
fprintf(stderr, "dh_client_pub= ");
|
fprintf(stderr, "dh_client_pub= ");
|
||||||
|
@ -93,71 +126,90 @@ kexdh_server(Kex *kex)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
DHparams_print_fp(stderr, dh);
|
DHparams_print_fp(stderr, kex->dh);
|
||||||
fprintf(stderr, "pub= ");
|
fprintf(stderr, "pub= ");
|
||||||
BN_print_fp(stderr, dh->pub_key);
|
BN_print_fp(stderr, kex->dh->pub_key);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
#endif
|
||||||
if (!dh_pub_is_valid(dh, dh_client_pub))
|
if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
|
||||||
packet_disconnect("bad client public DH value");
|
sshpkt_disconnect(ssh, "bad client public DH value");
|
||||||
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
klen = DH_size(dh);
|
klen = DH_size(kex->dh);
|
||||||
kbuf = xmalloc(klen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
|
(shared_secret = BN_new()) == NULL) {
|
||||||
fatal("DH_compute_key: failed");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
|
||||||
|
BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
dump_digest("shared secret", kbuf, kout);
|
dump_digest("shared secret", kbuf, kout);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
|
||||||
fatal("kexdh_server: BN_new failed");
|
&sbloblen)) != 0)
|
||||||
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
goto out;
|
||||||
fatal("kexdh_server: BN_bin2bn failed");
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
|
||||||
|
|
||||||
/* calc H */
|
/* calc H */
|
||||||
kex_dh_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_dh_hash(
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
dh_client_pub,
|
dh_client_pub,
|
||||||
dh->pub_key,
|
kex->dh->pub_key,
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
BN_clear_free(dh_client_pub);
|
|
||||||
|
|
||||||
/* save session id := H */
|
/* save session id := H */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sign H */
|
/* sign H */
|
||||||
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
if ((r = kex->sign(server_host_private, server_host_public,
|
||||||
hash, hashlen);
|
&signature, &slen, hash, hashlen, ssh->compat)) < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
/* destroy_sensitive_data(); */
|
/* destroy_sensitive_data(); */
|
||||||
|
|
||||||
/* send server hostkey, DH pubkey 'f' and singed H */
|
/* send server hostkey, DH pubkey 'f' and singed H */
|
||||||
packet_start(SSH2_MSG_KEXDH_REPLY);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
|
||||||
packet_put_string(server_host_key_blob, sbloblen);
|
(r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
|
||||||
packet_put_bignum2(dh->pub_key); /* f */
|
(r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
|
||||||
packet_put_string(signature, slen);
|
(r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
|
||||||
packet_send();
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
free(signature);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
|
r = kex_send_newkeys(ssh);
|
||||||
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
DH_free(kex->dh);
|
||||||
|
kex->dh = NULL;
|
||||||
|
if (dh_client_pub)
|
||||||
|
BN_clear_free(dh_client_pub);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
free(server_host_key_blob);
|
free(server_host_key_blob);
|
||||||
/* have keys, free DH */
|
free(signature);
|
||||||
DH_free(dh);
|
return r;
|
||||||
|
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
|
||||||
BN_clear_free(shared_secret);
|
|
||||||
kex_finish(kex);
|
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
83
kexecdh.c
83
kexecdh.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexecdh.c,v 1.5 2014/01/09 23:20:00 djm Exp $ */
|
/* $OpenBSD: kexecdh.c,v 1.6 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -38,60 +38,63 @@
|
||||||
#include <openssl/ec.h>
|
#include <openssl/ec.h>
|
||||||
#include <openssl/ecdh.h>
|
#include <openssl/ecdh.h>
|
||||||
|
|
||||||
#include "buffer.h"
|
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "sshbuf.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
|
||||||
void
|
int
|
||||||
kex_ecdh_hash(
|
kex_ecdh_hash(
|
||||||
int hash_alg,
|
int hash_alg,
|
||||||
const EC_GROUP *ec_group,
|
const EC_GROUP *ec_group,
|
||||||
char *client_version_string,
|
const char *client_version_string,
|
||||||
char *server_version_string,
|
const char *server_version_string,
|
||||||
char *ckexinit, int ckexinitlen,
|
const u_char *ckexinit, size_t ckexinitlen,
|
||||||
char *skexinit, int skexinitlen,
|
const u_char *skexinit, size_t skexinitlen,
|
||||||
u_char *serverhostkeyblob, int sbloblen,
|
const u_char *serverhostkeyblob, size_t sbloblen,
|
||||||
const EC_POINT *client_dh_pub,
|
const EC_POINT *client_dh_pub,
|
||||||
const EC_POINT *server_dh_pub,
|
const EC_POINT *server_dh_pub,
|
||||||
const BIGNUM *shared_secret,
|
const BIGNUM *shared_secret,
|
||||||
u_char **hash, u_int *hashlen)
|
u_char *hash, size_t *hashlen)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct sshbuf *b;
|
||||||
static u_char digest[SSH_DIGEST_MAX_LENGTH];
|
int r;
|
||||||
|
|
||||||
buffer_init(&b);
|
|
||||||
buffer_put_cstring(&b, client_version_string);
|
|
||||||
buffer_put_cstring(&b, server_version_string);
|
|
||||||
|
|
||||||
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
|
||||||
buffer_put_int(&b, ckexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, ckexinit, ckexinitlen);
|
|
||||||
buffer_put_int(&b, skexinitlen+1);
|
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
|
||||||
buffer_append(&b, skexinit, skexinitlen);
|
|
||||||
|
|
||||||
buffer_put_string(&b, serverhostkeyblob, sbloblen);
|
|
||||||
buffer_put_ecpoint(&b, ec_group, client_dh_pub);
|
|
||||||
buffer_put_ecpoint(&b, ec_group, server_dh_pub);
|
|
||||||
buffer_put_bignum2(&b, shared_secret);
|
|
||||||
|
|
||||||
|
if (*hashlen < ssh_digest_bytes(hash_alg))
|
||||||
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
if ((b = sshbuf_new()) == NULL)
|
||||||
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
|
if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
|
||||||
|
(r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
|
||||||
|
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
||||||
|
(r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
|
(r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
|
||||||
|
(r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
|
(r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
|
||||||
|
(r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
|
||||||
|
(r = sshbuf_put_ec(b, client_dh_pub, ec_group)) != 0 ||
|
||||||
|
(r = sshbuf_put_ec(b, server_dh_pub, ec_group)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
|
||||||
|
sshbuf_free(b);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEX
|
#ifdef DEBUG_KEX
|
||||||
buffer_dump(&b);
|
sshbuf_dump(b, stderr);
|
||||||
#endif
|
#endif
|
||||||
if (ssh_digest_buffer(hash_alg, &b, digest, sizeof(digest)) != 0)
|
if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
|
||||||
fatal("%s: ssh_digest_buffer failed", __func__);
|
sshbuf_free(b);
|
||||||
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
buffer_free(&b);
|
}
|
||||||
|
sshbuf_free(b);
|
||||||
#ifdef DEBUG_KEX
|
|
||||||
dump_digest("hash", digest, ssh_digest_bytes(hash_alg));
|
|
||||||
#endif
|
|
||||||
*hash = digest;
|
|
||||||
*hashlen = ssh_digest_bytes(hash_alg);
|
*hashlen = ssh_digest_bytes(hash_alg);
|
||||||
|
#ifdef DEBUG_KEX
|
||||||
|
dump_digest("hash", hash, *hashlen);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
||||||
|
|
207
kexecdhc.c
207
kexecdhc.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexecdhc.c,v 1.8 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexecdhc.c,v 1.9 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -34,126 +34,193 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include <openssl/ecdh.h>
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dh.h"
|
#include "dh.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
#include "dispatch.h"
|
||||||
|
#include "compat.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
#include <openssl/ecdh.h>
|
static int input_kex_ecdh_reply(int, u_int32_t, void *);
|
||||||
|
|
||||||
void
|
int
|
||||||
kexecdh_client(Kex *kex)
|
kexecdh_client(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
EC_KEY *client_key;
|
struct kex *kex = ssh->kex;
|
||||||
EC_POINT *server_public;
|
EC_KEY *client_key = NULL;
|
||||||
const EC_GROUP *group;
|
const EC_GROUP *group;
|
||||||
BIGNUM *shared_secret;
|
const EC_POINT *public_key;
|
||||||
Key *server_host_key;
|
int r;
|
||||||
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
||||||
u_char *kbuf, *hash;
|
|
||||||
u_int klen, slen, sbloblen, hashlen;
|
|
||||||
|
|
||||||
if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL)
|
if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
|
||||||
fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
if (EC_KEY_generate_key(client_key) != 1)
|
goto out;
|
||||||
fatal("%s: EC_KEY_generate_key failed", __func__);
|
}
|
||||||
|
if (EC_KEY_generate_key(client_key) != 1) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
group = EC_KEY_get0_group(client_key);
|
group = EC_KEY_get0_group(client_key);
|
||||||
|
public_key = EC_KEY_get0_public_key(client_key);
|
||||||
|
|
||||||
packet_start(SSH2_MSG_KEX_ECDH_INIT);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
|
||||||
packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key));
|
(r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
|
||||||
packet_send();
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
debug("sending SSH2_MSG_KEX_ECDH_INIT");
|
debug("sending SSH2_MSG_KEX_ECDH_INIT");
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
fputs("client private key:\n", stderr);
|
fputs("client private key:\n", stderr);
|
||||||
key_dump_ec_key(client_key);
|
sshkey_dump_ec_key(client_key);
|
||||||
#endif
|
#endif
|
||||||
|
kex->ec_client_key = client_key;
|
||||||
|
kex->ec_group = group;
|
||||||
|
client_key = NULL; /* owned by the kex */
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
|
debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
|
||||||
packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
if (client_key)
|
||||||
|
EC_KEY_free(client_key);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
input_kex_ecdh_reply(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
const EC_GROUP *group;
|
||||||
|
EC_POINT *server_public = NULL;
|
||||||
|
EC_KEY *client_key;
|
||||||
|
BIGNUM *shared_secret = NULL;
|
||||||
|
struct sshkey *server_host_key = NULL;
|
||||||
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
||||||
|
u_char *kbuf = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t slen, sbloblen;
|
||||||
|
size_t klen = 0, hashlen;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if (kex->verify_host_key == NULL) {
|
||||||
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
group = kex->ec_group;
|
||||||
|
client_key = kex->ec_client_key;
|
||||||
|
|
||||||
/* hostkey */
|
/* hostkey */
|
||||||
server_host_key_blob = packet_get_string(&sbloblen);
|
if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
|
||||||
server_host_key = key_from_blob(server_host_key_blob, sbloblen);
|
&sbloblen)) != 0 ||
|
||||||
if (server_host_key == NULL)
|
(r = sshkey_from_blob(server_host_key_blob, sbloblen,
|
||||||
fatal("cannot decode server_host_key_blob");
|
&server_host_key)) != 0)
|
||||||
if (server_host_key->type != kex->hostkey_type)
|
goto out;
|
||||||
fatal("type mismatch for decoded server_host_key_blob");
|
if (server_host_key->type != kex->hostkey_type) {
|
||||||
if (kex->verify_host_key == NULL)
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
||||||
fatal("cannot verify server_host_key");
|
goto out;
|
||||||
if (kex->verify_host_key(server_host_key) == -1)
|
}
|
||||||
fatal("server_host_key verification failed");
|
if (kex->verify_host_key(server_host_key, ssh) == -1) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* Q_S, server public key */
|
/* Q_S, server public key */
|
||||||
if ((server_public = EC_POINT_new(group)) == NULL)
|
/* signed H */
|
||||||
fatal("%s: EC_POINT_new failed", __func__);
|
if ((server_public = EC_POINT_new(group)) == NULL) {
|
||||||
packet_get_ecpoint(group, server_public);
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
if (key_ec_validate_public(group, server_public) != 0)
|
}
|
||||||
fatal("%s: invalid server public key", __func__);
|
if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
|
||||||
|
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
fputs("server public key:\n", stderr);
|
fputs("server public key:\n", stderr);
|
||||||
key_dump_ec_point(group, server_public);
|
sshkey_dump_ec_point(group, server_public);
|
||||||
#endif
|
#endif
|
||||||
|
if (sshkey_ec_validate_public(group, server_public) != 0) {
|
||||||
/* signed H */
|
sshpkt_disconnect(ssh, "invalid server public key");
|
||||||
signature = packet_get_string(&slen);
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
packet_check_eom();
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
klen = (EC_GROUP_get_degree(group) + 7) / 8;
|
klen = (EC_GROUP_get_degree(group) + 7) / 8;
|
||||||
kbuf = xmalloc(klen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
|
(shared_secret = BN_new()) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
if (ECDH_compute_key(kbuf, klen, server_public,
|
if (ECDH_compute_key(kbuf, klen, server_public,
|
||||||
client_key, NULL) != (int)klen)
|
client_key, NULL) != (int)klen ||
|
||||||
fatal("%s: ECDH_compute_key failed", __func__);
|
BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("shared secret", kbuf, klen);
|
dump_digest("shared secret", kbuf, klen);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
|
||||||
fatal("%s: BN_new failed", __func__);
|
|
||||||
if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
|
|
||||||
fatal("%s: BN_bin2bn failed", __func__);
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
/* calc and verify H */
|
/* calc and verify H */
|
||||||
kex_ecdh_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_ecdh_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
group,
|
group,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
EC_KEY_get0_public_key(client_key),
|
EC_KEY_get0_public_key(client_key),
|
||||||
server_public,
|
server_public,
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
free(server_host_key_blob);
|
|
||||||
EC_POINT_clear_free(server_public);
|
|
||||||
EC_KEY_free(client_key);
|
|
||||||
|
|
||||||
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
if ((r = sshkey_verify(server_host_key, signature, slen, hash,
|
||||||
fatal("key_verify failed for server_host_key");
|
hashlen, ssh->compat)) != 0)
|
||||||
key_free(server_host_key);
|
goto out;
|
||||||
free(signature);
|
|
||||||
|
|
||||||
/* save session id */
|
/* save session id */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
BN_clear_free(shared_secret);
|
r = kex_send_newkeys(ssh);
|
||||||
kex_finish(kex);
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
if (kex->ec_client_key) {
|
||||||
|
EC_KEY_free(kex->ec_client_key);
|
||||||
|
kex->ec_client_key = NULL;
|
||||||
|
}
|
||||||
|
if (server_public)
|
||||||
|
EC_POINT_clear_free(server_public);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
|
sshkey_free(server_host_key);
|
||||||
|
free(server_host_key_blob);
|
||||||
|
free(signature);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
||||||
|
|
||||||
|
|
192
kexecdhs.c
192
kexecdhs.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexecdhs.c,v 1.11 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexecdhs.c,v 1.12 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
||||||
|
@ -32,124 +32,176 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include <openssl/ecdh.h>
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
#include "sshkey.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
|
||||||
#include <openssl/ecdh.h>
|
#include "dispatch.h"
|
||||||
|
#include "compat.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
void
|
static int input_kex_ecdh_init(int, u_int32_t, void *);
|
||||||
kexecdh_server(Kex *kex)
|
|
||||||
|
int
|
||||||
|
kexecdh_server(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
EC_POINT *client_public;
|
debug("expecting SSH2_MSG_KEX_ECDH_INIT");
|
||||||
EC_KEY *server_key;
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init);
|
||||||
const EC_GROUP *group;
|
return 0;
|
||||||
BIGNUM *shared_secret;
|
}
|
||||||
Key *server_host_private, *server_host_public;
|
|
||||||
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
||||||
u_char *kbuf, *hash;
|
|
||||||
u_int klen, slen, sbloblen, hashlen;
|
|
||||||
|
|
||||||
if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL)
|
static int
|
||||||
fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
|
input_kex_ecdh_init(int type, u_int32_t seq, void *ctxt)
|
||||||
if (EC_KEY_generate_key(server_key) != 1)
|
{
|
||||||
fatal("%s: EC_KEY_generate_key failed", __func__);
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
EC_POINT *client_public;
|
||||||
|
EC_KEY *server_key = NULL;
|
||||||
|
const EC_GROUP *group;
|
||||||
|
const EC_POINT *public_key;
|
||||||
|
BIGNUM *shared_secret = NULL;
|
||||||
|
struct sshkey *server_host_private, *server_host_public;
|
||||||
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
||||||
|
u_char *kbuf = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t slen, sbloblen;
|
||||||
|
size_t klen = 0, hashlen;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if (EC_KEY_generate_key(server_key) != 1) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
group = EC_KEY_get0_group(server_key);
|
group = EC_KEY_get0_group(server_key);
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
fputs("server private key:\n", stderr);
|
fputs("server private key:\n", stderr);
|
||||||
key_dump_ec_key(server_key);
|
sshkey_dump_ec_key(server_key);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (kex->load_host_public_key == NULL ||
|
if (kex->load_host_public_key == NULL ||
|
||||||
kex->load_host_private_key == NULL)
|
kex->load_host_private_key == NULL) {
|
||||||
fatal("Cannot load hostkey");
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
goto out;
|
||||||
if (server_host_public == NULL)
|
}
|
||||||
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
|
||||||
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
ssh)) == NULL ||
|
||||||
|
(server_host_private = kex->load_host_private_key(kex->hostkey_type,
|
||||||
debug("expecting SSH2_MSG_KEX_ECDH_INIT");
|
ssh)) == NULL) {
|
||||||
packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
|
r = SSH_ERR_NO_HOSTKEY_LOADED;
|
||||||
if ((client_public = EC_POINT_new(group)) == NULL)
|
goto out;
|
||||||
fatal("%s: EC_POINT_new failed", __func__);
|
}
|
||||||
packet_get_ecpoint(group, client_public);
|
if ((client_public = EC_POINT_new(group)) == NULL) {
|
||||||
packet_check_eom();
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
if (key_ec_validate_public(group, client_public) != 0)
|
}
|
||||||
fatal("%s: invalid client public key", __func__);
|
if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG_KEXECDH
|
#ifdef DEBUG_KEXECDH
|
||||||
fputs("client public key:\n", stderr);
|
fputs("client public key:\n", stderr);
|
||||||
key_dump_ec_point(group, client_public);
|
sshkey_dump_ec_point(group, client_public);
|
||||||
#endif
|
#endif
|
||||||
|
if (sshkey_ec_validate_public(group, client_public) != 0) {
|
||||||
|
sshpkt_disconnect(ssh, "invalid client public key");
|
||||||
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* Calculate shared_secret */
|
/* Calculate shared_secret */
|
||||||
klen = (EC_GROUP_get_degree(group) + 7) / 8;
|
klen = (EC_GROUP_get_degree(group) + 7) / 8;
|
||||||
kbuf = xmalloc(klen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
|
(shared_secret = BN_new()) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
if (ECDH_compute_key(kbuf, klen, client_public,
|
if (ECDH_compute_key(kbuf, klen, client_public,
|
||||||
server_key, NULL) != (int)klen)
|
server_key, NULL) != (int)klen ||
|
||||||
fatal("%s: ECDH_compute_key failed", __func__);
|
BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXECDH
|
||||||
dump_digest("shared secret", kbuf, klen);
|
dump_digest("shared secret", kbuf, klen);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
|
||||||
fatal("%s: BN_new failed", __func__);
|
|
||||||
if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
|
|
||||||
fatal("%s: BN_bin2bn failed", __func__);
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
/* calc H */
|
/* calc H */
|
||||||
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
|
||||||
kex_ecdh_hash(
|
&sbloblen)) != 0)
|
||||||
|
goto out;
|
||||||
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kex_ecdh_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
group,
|
group,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
client_public,
|
client_public,
|
||||||
EC_KEY_get0_public_key(server_key),
|
EC_KEY_get0_public_key(server_key),
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
EC_POINT_clear_free(client_public);
|
|
||||||
|
|
||||||
/* save session id := H */
|
/* save session id := H */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sign H */
|
/* sign H */
|
||||||
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
if ((r = kex->sign(server_host_private, server_host_public,
|
||||||
hash, hashlen);
|
&signature, &slen, hash, hashlen, ssh->compat)) < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
/* destroy_sensitive_data(); */
|
/* destroy_sensitive_data(); */
|
||||||
|
|
||||||
|
public_key = EC_KEY_get0_public_key(server_key);
|
||||||
/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
|
/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
|
||||||
packet_start(SSH2_MSG_KEX_ECDH_REPLY);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
|
||||||
packet_put_string(server_host_key_blob, sbloblen);
|
(r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
|
||||||
packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
|
(r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
|
||||||
packet_put_string(signature, slen);
|
(r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
|
||||||
packet_send();
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
free(signature);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
|
r = kex_send_newkeys(ssh);
|
||||||
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
if (kex->ec_client_key) {
|
||||||
|
EC_KEY_free(kex->ec_client_key);
|
||||||
|
kex->ec_client_key = NULL;
|
||||||
|
}
|
||||||
|
if (server_key)
|
||||||
|
EC_KEY_free(server_key);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
free(server_host_key_blob);
|
free(server_host_key_blob);
|
||||||
/* have keys, free server key */
|
free(signature);
|
||||||
EC_KEY_free(server_key);
|
return r;
|
||||||
|
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
|
||||||
BN_clear_free(shared_secret);
|
|
||||||
kex_finish(kex);
|
|
||||||
}
|
}
|
||||||
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */
|
||||||
|
|
||||||
|
|
105
kexgex.c
105
kexgex.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexgex.c,v 1.28 2014/01/09 23:20:00 djm Exp $ */
|
/* $OpenBSD: kexgex.c,v 1.29 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
|
@ -33,69 +33,70 @@
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "buffer.h"
|
#include "sshkey.h"
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
#include "log.h"
|
|
||||||
|
|
||||||
void
|
int
|
||||||
kexgex_hash(
|
kexgex_hash(
|
||||||
int hash_alg,
|
int hash_alg,
|
||||||
char *client_version_string,
|
const char *client_version_string,
|
||||||
char *server_version_string,
|
const char *server_version_string,
|
||||||
char *ckexinit, int ckexinitlen,
|
const u_char *ckexinit, size_t ckexinitlen,
|
||||||
char *skexinit, int skexinitlen,
|
const u_char *skexinit, size_t skexinitlen,
|
||||||
u_char *serverhostkeyblob, int sbloblen,
|
const u_char *serverhostkeyblob, size_t sbloblen,
|
||||||
int min, int wantbits, int max, BIGNUM *prime, BIGNUM *gen,
|
int min, int wantbits, int max,
|
||||||
BIGNUM *client_dh_pub,
|
const BIGNUM *prime,
|
||||||
BIGNUM *server_dh_pub,
|
const BIGNUM *gen,
|
||||||
BIGNUM *shared_secret,
|
const BIGNUM *client_dh_pub,
|
||||||
u_char **hash, u_int *hashlen)
|
const BIGNUM *server_dh_pub,
|
||||||
|
const BIGNUM *shared_secret,
|
||||||
|
u_char *hash, size_t *hashlen)
|
||||||
{
|
{
|
||||||
Buffer b;
|
struct sshbuf *b;
|
||||||
static u_char digest[SSH_DIGEST_MAX_LENGTH];
|
int r;
|
||||||
|
|
||||||
buffer_init(&b);
|
if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
|
||||||
buffer_put_cstring(&b, client_version_string);
|
return SSH_ERR_INVALID_ARGUMENT;
|
||||||
buffer_put_cstring(&b, server_version_string);
|
if ((b = sshbuf_new()) == NULL)
|
||||||
|
return SSH_ERR_ALLOC_FAIL;
|
||||||
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
|
||||||
buffer_put_int(&b, ckexinitlen+1);
|
(r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
|
||||||
buffer_append(&b, ckexinit, ckexinitlen);
|
(r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
|
||||||
buffer_put_int(&b, skexinitlen+1);
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
buffer_put_char(&b, SSH2_MSG_KEXINIT);
|
(r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
|
||||||
buffer_append(&b, skexinit, skexinitlen);
|
(r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
|
||||||
|
(r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
|
||||||
buffer_put_string(&b, serverhostkeyblob, sbloblen);
|
(r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
|
||||||
if (min == -1 || max == -1)
|
(r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
|
||||||
buffer_put_int(&b, wantbits);
|
(min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
|
||||||
else {
|
(r = sshbuf_put_u32(b, wantbits)) != 0 ||
|
||||||
buffer_put_int(&b, min);
|
(max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
|
||||||
buffer_put_int(&b, wantbits);
|
(r = sshbuf_put_bignum2(b, prime)) != 0 ||
|
||||||
buffer_put_int(&b, max);
|
(r = sshbuf_put_bignum2(b, gen)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
|
||||||
|
(r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
|
||||||
|
sshbuf_free(b);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
buffer_put_bignum2(&b, prime);
|
|
||||||
buffer_put_bignum2(&b, gen);
|
|
||||||
buffer_put_bignum2(&b, client_dh_pub);
|
|
||||||
buffer_put_bignum2(&b, server_dh_pub);
|
|
||||||
buffer_put_bignum2(&b, shared_secret);
|
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
buffer_dump(&b);
|
sshbuf_dump(b, stderr);
|
||||||
#endif
|
#endif
|
||||||
if (ssh_digest_buffer(hash_alg, &b, digest, sizeof(digest)) != 0)
|
if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
|
||||||
fatal("%s: ssh_digest_buffer failed", __func__);
|
sshbuf_free(b);
|
||||||
|
return SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
buffer_free(&b);
|
}
|
||||||
|
sshbuf_free(b);
|
||||||
#ifdef DEBUG_KEX
|
|
||||||
dump_digest("hash", digest, ssh_digest_bytes(hash_alg));
|
|
||||||
#endif
|
|
||||||
*hash = digest;
|
|
||||||
*hashlen = ssh_digest_bytes(hash_alg);
|
*hashlen = ssh_digest_bytes(hash_alg);
|
||||||
|
#ifdef DEBUG_KEXDH
|
||||||
|
dump_digest("hash", hash, *hashlen);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
287
kexgexc.c
287
kexgexc.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexgexc.c,v 1.18 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexgexc.c,v 1.19 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
|
@ -37,174 +37,237 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dh.h"
|
#include "dh.h"
|
||||||
#include "ssh2.h"
|
#include "ssh2.h"
|
||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
|
#include "dispatch.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
void
|
static int input_kex_dh_gex_group(int, u_int32_t, void *);
|
||||||
kexgex_client(Kex *kex)
|
static int input_kex_dh_gex_reply(int, u_int32_t, void *);
|
||||||
|
|
||||||
|
int
|
||||||
|
kexgex_client(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
struct kex *kex = ssh->kex;
|
||||||
BIGNUM *p = NULL, *g = NULL;
|
int r;
|
||||||
Key *server_host_key;
|
u_int nbits;
|
||||||
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
|
||||||
u_int klen, slen, sbloblen, hashlen;
|
|
||||||
int kout;
|
|
||||||
int min, max, nbits;
|
|
||||||
DH *dh;
|
|
||||||
|
|
||||||
nbits = dh_estimate(kex->dh_need * 8);
|
nbits = dh_estimate(kex->dh_need * 8);
|
||||||
|
|
||||||
if (datafellows & SSH_OLD_DHGEX) {
|
kex->min = DH_GRP_MIN;
|
||||||
|
kex->max = DH_GRP_MAX;
|
||||||
|
kex->nbits = nbits;
|
||||||
|
if (ssh->compat & SSH_OLD_DHGEX) {
|
||||||
/* Old GEX request */
|
/* Old GEX request */
|
||||||
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD))
|
||||||
packet_put_int(nbits);
|
!= 0 ||
|
||||||
min = DH_GRP_MIN;
|
(r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
|
||||||
max = DH_GRP_MAX;
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", kex->nbits);
|
||||||
} else {
|
} else {
|
||||||
/* New GEX request */
|
/* New GEX request */
|
||||||
min = DH_GRP_MIN;
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
|
||||||
max = DH_GRP_MAX;
|
(r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
|
||||||
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
|
(r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
|
||||||
packet_put_int(min);
|
(r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
|
||||||
packet_put_int(nbits);
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
packet_put_int(max);
|
goto out;
|
||||||
|
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
|
||||||
min, nbits, max);
|
kex->min, kex->nbits, kex->max);
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
|
fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
|
||||||
min, nbits, max);
|
kex->min, kex->nbits, kex->max);
|
||||||
#endif
|
#endif
|
||||||
packet_send();
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
|
||||||
|
&input_kex_dh_gex_group);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
|
static int
|
||||||
packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
|
input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
BIGNUM *p = NULL, *g = NULL;
|
||||||
|
int r, bits;
|
||||||
|
|
||||||
if ((p = BN_new()) == NULL)
|
debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
|
||||||
fatal("BN_new");
|
|
||||||
packet_get_bignum2(p);
|
|
||||||
if ((g = BN_new()) == NULL)
|
|
||||||
fatal("BN_new");
|
|
||||||
packet_get_bignum2(g);
|
|
||||||
packet_check_eom();
|
|
||||||
|
|
||||||
if (BN_num_bits(p) < min || BN_num_bits(p) > max)
|
if ((p = BN_new()) == NULL ||
|
||||||
fatal("DH_GEX group out of range: %d !< %d !< %d",
|
(g = BN_new()) == NULL) {
|
||||||
min, BN_num_bits(p), max);
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
dh = dh_new_group(g, p);
|
}
|
||||||
dh_gen_key(dh, kex->we_need * 8);
|
if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
|
||||||
|
(r = sshpkt_get_bignum2(ssh, g)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
if ((bits = BN_num_bits(p)) < 0 ||
|
||||||
|
(u_int)bits < kex->min || (u_int)bits > kex->max) {
|
||||||
|
r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((kex->dh = dh_new_group(g, p)) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
p = g = NULL; /* belong to kex->dh now */
|
||||||
|
|
||||||
|
/* generate and send 'e', client DH public key */
|
||||||
|
if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
|
||||||
|
(r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
|
||||||
|
(r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
|
||||||
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
DHparams_print_fp(stderr, dh);
|
DHparams_print_fp(stderr, kex->dh);
|
||||||
fprintf(stderr, "pub= ");
|
fprintf(stderr, "pub= ");
|
||||||
BN_print_fp(stderr, dh->pub_key);
|
BN_print_fp(stderr, kex->dh->pub_key);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
#endif
|
||||||
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
|
||||||
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
if (p)
|
||||||
|
BN_clear_free(p);
|
||||||
|
if (g)
|
||||||
|
BN_clear_free(g);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
|
static int
|
||||||
/* generate and send 'e', client DH public key */
|
input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
|
||||||
packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
|
{
|
||||||
packet_put_bignum2(dh->pub_key);
|
struct ssh *ssh = ctxt;
|
||||||
packet_send();
|
struct kex *kex = ssh->kex;
|
||||||
|
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
|
||||||
debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
|
struct sshkey *server_host_key = NULL;
|
||||||
packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
|
u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t klen = 0, slen, sbloblen, hashlen;
|
||||||
|
int kout, r;
|
||||||
|
|
||||||
|
debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
|
||||||
|
if (kex->verify_host_key == NULL) {
|
||||||
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
/* key, cert */
|
/* key, cert */
|
||||||
server_host_key_blob = packet_get_string(&sbloblen);
|
if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
|
||||||
server_host_key = key_from_blob(server_host_key_blob, sbloblen);
|
&sbloblen)) != 0 ||
|
||||||
if (server_host_key == NULL)
|
(r = sshkey_from_blob(server_host_key_blob, sbloblen,
|
||||||
fatal("cannot decode server_host_key_blob");
|
&server_host_key)) != 0)
|
||||||
if (server_host_key->type != kex->hostkey_type)
|
goto out;
|
||||||
fatal("type mismatch for decoded server_host_key_blob");
|
if (server_host_key->type != kex->hostkey_type) {
|
||||||
if (kex->verify_host_key == NULL)
|
r = SSH_ERR_KEY_TYPE_MISMATCH;
|
||||||
fatal("cannot verify server_host_key");
|
goto out;
|
||||||
if (kex->verify_host_key(server_host_key) == -1)
|
}
|
||||||
fatal("server_host_key verification failed");
|
if (kex->verify_host_key(server_host_key, ssh) == -1) {
|
||||||
|
r = SSH_ERR_SIGNATURE_INVALID;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
/* DH parameter f, server public DH key */
|
/* DH parameter f, server public DH key */
|
||||||
if ((dh_server_pub = BN_new()) == NULL)
|
if ((dh_server_pub = BN_new()) == NULL) {
|
||||||
fatal("dh_server_pub == NULL");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
packet_get_bignum2(dh_server_pub);
|
goto out;
|
||||||
|
}
|
||||||
|
/* signed H */
|
||||||
|
if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
|
||||||
|
(r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
fprintf(stderr, "dh_server_pub= ");
|
fprintf(stderr, "dh_server_pub= ");
|
||||||
BN_print_fp(stderr, dh_server_pub);
|
BN_print_fp(stderr, dh_server_pub);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
debug("bits %d", BN_num_bits(dh_server_pub));
|
debug("bits %d", BN_num_bits(dh_server_pub));
|
||||||
#endif
|
#endif
|
||||||
|
if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
|
||||||
|
sshpkt_disconnect(ssh, "bad server public DH value");
|
||||||
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* signed H */
|
klen = DH_size(kex->dh);
|
||||||
signature = packet_get_string(&slen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
packet_check_eom();
|
(shared_secret = BN_new()) == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
if (!dh_pub_is_valid(dh, dh_server_pub))
|
goto out;
|
||||||
packet_disconnect("bad server public DH value");
|
}
|
||||||
|
if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
|
||||||
klen = DH_size(dh);
|
BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
|
||||||
kbuf = xmalloc(klen);
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
|
goto out;
|
||||||
fatal("DH_compute_key: failed");
|
}
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
dump_digest("shared secret", kbuf, kout);
|
dump_digest("shared secret", kbuf, kout);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
if (ssh->compat & SSH_OLD_DHGEX)
|
||||||
fatal("kexgex_client: BN_new failed");
|
kex->min = kex->max = -1;
|
||||||
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
|
||||||
fatal("kexgex_client: BN_bin2bn failed");
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
if (datafellows & SSH_OLD_DHGEX)
|
|
||||||
min = max = -1;
|
|
||||||
|
|
||||||
/* calc and verify H */
|
/* calc and verify H */
|
||||||
kexgex_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kexgex_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
min, nbits, max,
|
kex->min, kex->nbits, kex->max,
|
||||||
dh->p, dh->g,
|
kex->dh->p, kex->dh->g,
|
||||||
dh->pub_key,
|
kex->dh->pub_key,
|
||||||
dh_server_pub,
|
dh_server_pub,
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
|
|
||||||
/* have keys, free DH */
|
if ((r = sshkey_verify(server_host_key, signature, slen, hash,
|
||||||
DH_free(dh);
|
hashlen, ssh->compat)) != 0)
|
||||||
free(server_host_key_blob);
|
goto out;
|
||||||
BN_clear_free(dh_server_pub);
|
|
||||||
|
|
||||||
if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
|
|
||||||
fatal("key_verify failed for server_host_key");
|
|
||||||
key_free(server_host_key);
|
|
||||||
free(signature);
|
|
||||||
|
|
||||||
/* save session id */
|
/* save session id */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
|
||||||
BN_clear_free(shared_secret);
|
|
||||||
|
|
||||||
kex_finish(kex);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
|
r = kex_send_newkeys(ssh);
|
||||||
|
out:
|
||||||
|
explicit_bzero(hash, sizeof(hash));
|
||||||
|
DH_free(kex->dh);
|
||||||
|
kex->dh = NULL;
|
||||||
|
if (dh_server_pub)
|
||||||
|
BN_clear_free(dh_server_pub);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
|
sshkey_free(server_host_key);
|
||||||
|
free(server_host_key_blob);
|
||||||
|
free(signature);
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
255
kexgexs.c
255
kexgexs.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: kexgexs.c,v 1.20 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: kexgexs.c,v 1.21 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
* Copyright (c) 2000 Niels Provos. All rights reserved.
|
||||||
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
||||||
|
@ -37,10 +37,9 @@
|
||||||
|
|
||||||
#include <openssl/dh.h>
|
#include <openssl/dh.h>
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "sshkey.h"
|
||||||
#include "buffer.h"
|
|
||||||
#include "key.h"
|
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
|
#include "digest.h"
|
||||||
#include "kex.h"
|
#include "kex.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
|
@ -51,33 +50,43 @@
|
||||||
#include "ssh-gss.h"
|
#include "ssh-gss.h"
|
||||||
#endif
|
#endif
|
||||||
#include "monitor_wrap.h"
|
#include "monitor_wrap.h"
|
||||||
|
#include "dispatch.h"
|
||||||
|
#include "ssherr.h"
|
||||||
|
#include "sshbuf.h"
|
||||||
|
|
||||||
void
|
static int input_kex_dh_gex_request(int, u_int32_t, void *);
|
||||||
kexgex_server(Kex *kex)
|
static int input_kex_dh_gex_init(int, u_int32_t, void *);
|
||||||
|
|
||||||
|
int
|
||||||
|
kexgex_server(struct ssh *ssh)
|
||||||
{
|
{
|
||||||
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD,
|
||||||
Key *server_host_public, *server_host_private;
|
&input_kex_dh_gex_request);
|
||||||
DH *dh;
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
|
||||||
u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
|
&input_kex_dh_gex_request);
|
||||||
u_int sbloblen, klen, slen, hashlen;
|
debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
|
||||||
int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
|
return 0;
|
||||||
int type, kout;
|
}
|
||||||
|
|
||||||
if (kex->load_host_public_key == NULL ||
|
static int
|
||||||
kex->load_host_private_key == NULL)
|
input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
|
||||||
fatal("Cannot load hostkey");
|
{
|
||||||
server_host_public = kex->load_host_public_key(kex->hostkey_type);
|
struct ssh *ssh = ctxt;
|
||||||
if (server_host_public == NULL)
|
struct kex *kex = ssh->kex;
|
||||||
fatal("Unsupported hostkey type %d", kex->hostkey_type);
|
int r;
|
||||||
server_host_private = kex->load_host_private_key(kex->hostkey_type);
|
u_int min = 0, max = 0, nbits = 0;
|
||||||
|
|
||||||
type = packet_read();
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SSH2_MSG_KEX_DH_GEX_REQUEST:
|
case SSH2_MSG_KEX_DH_GEX_REQUEST:
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
|
||||||
omin = min = packet_get_int();
|
if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
|
||||||
onbits = nbits = packet_get_int();
|
(r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
|
||||||
omax = max = packet_get_int();
|
(r = sshpkt_get_u32(ssh, &max)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
kex->nbits = nbits;
|
||||||
|
kex->min = min;
|
||||||
|
kex->max = max;
|
||||||
min = MAX(DH_GRP_MIN, min);
|
min = MAX(DH_GRP_MIN, min);
|
||||||
max = MIN(DH_GRP_MAX, max);
|
max = MIN(DH_GRP_MAX, max);
|
||||||
nbits = MAX(DH_GRP_MIN, nbits);
|
nbits = MAX(DH_GRP_MIN, nbits);
|
||||||
|
@ -85,45 +94,88 @@ kexgex_server(Kex *kex)
|
||||||
break;
|
break;
|
||||||
case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
|
case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
|
||||||
onbits = nbits = packet_get_int();
|
if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
kex->nbits = nbits;
|
||||||
/* unused for old GEX */
|
/* unused for old GEX */
|
||||||
omin = min = DH_GRP_MIN;
|
kex->min = min = DH_GRP_MIN;
|
||||||
omax = max = DH_GRP_MAX;
|
kex->max = max = DH_GRP_MAX;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
packet_check_eom();
|
|
||||||
|
|
||||||
if (omax < omin || onbits < omin || omax < onbits)
|
if (kex->max < kex->min || kex->nbits < kex->min ||
|
||||||
fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
|
kex->max < kex->nbits) {
|
||||||
omin, onbits, omax);
|
r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* Contact privileged parent */
|
/* Contact privileged parent */
|
||||||
dh = PRIVSEP(choose_dh(min, nbits, max));
|
kex->dh = PRIVSEP(choose_dh(min, nbits, max));
|
||||||
if (dh == NULL)
|
if (kex->dh == NULL) {
|
||||||
packet_disconnect("Protocol error: no matching DH grp found");
|
sshpkt_disconnect(ssh, "no matching DH grp found");
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
|
debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
|
||||||
packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
|
||||||
packet_put_bignum2(dh->p);
|
(r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
|
||||||
packet_put_bignum2(dh->g);
|
(r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
|
||||||
packet_send();
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
/* flush */
|
|
||||||
packet_write_wait();
|
|
||||||
|
|
||||||
/* Compute our exchange value in parallel with the client */
|
/* Compute our exchange value in parallel with the client */
|
||||||
dh_gen_key(dh, kex->we_need * 8);
|
if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
/* old KEX does not use min/max in kexgex_hash() */
|
||||||
|
if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
|
||||||
|
kex->min = kex->max = -1;
|
||||||
|
|
||||||
debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
|
debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
|
||||||
packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
|
||||||
|
r = 0;
|
||||||
|
out:
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
|
||||||
|
{
|
||||||
|
struct ssh *ssh = ctxt;
|
||||||
|
struct kex *kex = ssh->kex;
|
||||||
|
BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
|
||||||
|
struct sshkey *server_host_public, *server_host_private;
|
||||||
|
u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
|
||||||
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
||||||
|
size_t sbloblen, slen;
|
||||||
|
size_t klen = 0, hashlen;
|
||||||
|
int kout, r;
|
||||||
|
|
||||||
|
if (kex->load_host_public_key == NULL ||
|
||||||
|
kex->load_host_private_key == NULL) {
|
||||||
|
r = SSH_ERR_INVALID_ARGUMENT;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
|
||||||
|
ssh)) == NULL ||
|
||||||
|
(server_host_private = kex->load_host_private_key(kex->hostkey_type,
|
||||||
|
ssh)) == NULL) {
|
||||||
|
r = SSH_ERR_NO_HOSTKEY_LOADED;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* key, cert */
|
/* key, cert */
|
||||||
if ((dh_client_pub = BN_new()) == NULL)
|
if ((dh_client_pub = BN_new()) == NULL) {
|
||||||
fatal("dh_client_pub == NULL");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
packet_get_bignum2(dh_client_pub);
|
goto out;
|
||||||
packet_check_eom();
|
}
|
||||||
|
if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
|
||||||
|
(r = sshpkt_get_end(ssh)) != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
fprintf(stderr, "dh_client_pub= ");
|
fprintf(stderr, "dh_client_pub= ");
|
||||||
|
@ -133,79 +185,92 @@ kexgex_server(Kex *kex)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
DHparams_print_fp(stderr, dh);
|
DHparams_print_fp(stderr, kex->dh);
|
||||||
fprintf(stderr, "pub= ");
|
fprintf(stderr, "pub= ");
|
||||||
BN_print_fp(stderr, dh->pub_key);
|
BN_print_fp(stderr, kex->dh->pub_key);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
#endif
|
||||||
if (!dh_pub_is_valid(dh, dh_client_pub))
|
if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
|
||||||
packet_disconnect("bad client public DH value");
|
sshpkt_disconnect(ssh, "bad client public DH value");
|
||||||
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
klen = DH_size(dh);
|
klen = DH_size(kex->dh);
|
||||||
kbuf = xmalloc(klen);
|
if ((kbuf = malloc(klen)) == NULL ||
|
||||||
if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
|
(shared_secret = BN_new()) == NULL) {
|
||||||
fatal("DH_compute_key: failed");
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
|
||||||
|
BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
|
||||||
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
#ifdef DEBUG_KEXDH
|
#ifdef DEBUG_KEXDH
|
||||||
dump_digest("shared secret", kbuf, kout);
|
dump_digest("shared secret", kbuf, kout);
|
||||||
#endif
|
#endif
|
||||||
if ((shared_secret = BN_new()) == NULL)
|
if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
|
||||||
fatal("kexgex_server: BN_new failed");
|
&sbloblen)) != 0)
|
||||||
if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
|
goto out;
|
||||||
fatal("kexgex_server: BN_bin2bn failed");
|
|
||||||
explicit_bzero(kbuf, klen);
|
|
||||||
free(kbuf);
|
|
||||||
|
|
||||||
key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
|
|
||||||
|
|
||||||
if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
|
|
||||||
omin = min = omax = max = -1;
|
|
||||||
|
|
||||||
/* calc H */
|
/* calc H */
|
||||||
kexgex_hash(
|
hashlen = sizeof(hash);
|
||||||
|
if ((r = kexgex_hash(
|
||||||
kex->hash_alg,
|
kex->hash_alg,
|
||||||
kex->client_version_string,
|
kex->client_version_string,
|
||||||
kex->server_version_string,
|
kex->server_version_string,
|
||||||
buffer_ptr(kex->peer), buffer_len(kex->peer),
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
||||||
buffer_ptr(kex->my), buffer_len(kex->my),
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
||||||
server_host_key_blob, sbloblen,
|
server_host_key_blob, sbloblen,
|
||||||
omin, onbits, omax,
|
kex->min, kex->nbits, kex->max,
|
||||||
dh->p, dh->g,
|
kex->dh->p, kex->dh->g,
|
||||||
dh_client_pub,
|
dh_client_pub,
|
||||||
dh->pub_key,
|
kex->dh->pub_key,
|
||||||
shared_secret,
|
shared_secret,
|
||||||
&hash, &hashlen
|
hash, &hashlen)) != 0)
|
||||||
);
|
goto out;
|
||||||
BN_clear_free(dh_client_pub);
|
|
||||||
|
|
||||||
/* save session id := H */
|
/* save session id := H */
|
||||||
if (kex->session_id == NULL) {
|
if (kex->session_id == NULL) {
|
||||||
kex->session_id_len = hashlen;
|
kex->session_id_len = hashlen;
|
||||||
kex->session_id = xmalloc(kex->session_id_len);
|
kex->session_id = malloc(kex->session_id_len);
|
||||||
|
if (kex->session_id == NULL) {
|
||||||
|
r = SSH_ERR_ALLOC_FAIL;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
memcpy(kex->session_id, hash, kex->session_id_len);
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sign H */
|
/* sign H */
|
||||||
kex->sign(server_host_private, server_host_public, &signature, &slen,
|
if ((r = kex->sign(server_host_private, server_host_public,
|
||||||
hash, hashlen);
|
&signature, &slen, hash, hashlen, ssh->compat)) < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
/* destroy_sensitive_data(); */
|
/* destroy_sensitive_data(); */
|
||||||
|
|
||||||
/* send server hostkey, DH pubkey 'f' and singed H */
|
/* send server hostkey, DH pubkey 'f' and singed H */
|
||||||
debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
|
||||||
packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
|
(r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
|
||||||
packet_put_string(server_host_key_blob, sbloblen);
|
(r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
|
||||||
packet_put_bignum2(dh->pub_key); /* f */
|
(r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
|
||||||
packet_put_string(signature, slen);
|
(r = sshpkt_send(ssh)) != 0)
|
||||||
packet_send();
|
goto out;
|
||||||
|
|
||||||
free(signature);
|
if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
|
||||||
|
r = kex_send_newkeys(ssh);
|
||||||
|
out:
|
||||||
|
DH_free(kex->dh);
|
||||||
|
kex->dh = NULL;
|
||||||
|
if (dh_client_pub)
|
||||||
|
BN_clear_free(dh_client_pub);
|
||||||
|
if (kbuf) {
|
||||||
|
explicit_bzero(kbuf, klen);
|
||||||
|
free(kbuf);
|
||||||
|
}
|
||||||
|
if (shared_secret)
|
||||||
|
BN_clear_free(shared_secret);
|
||||||
free(server_host_key_blob);
|
free(server_host_key_blob);
|
||||||
/* have keys, free DH */
|
free(signature);
|
||||||
DH_free(dh);
|
return r;
|
||||||
|
|
||||||
kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
|
|
||||||
BN_clear_free(shared_secret);
|
|
||||||
|
|
||||||
kex_finish(kex);
|
|
||||||
}
|
}
|
||||||
#endif /* WITH_OPENSSL */
|
#endif /* WITH_OPENSSL */
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: monitor.c,v 1.139 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: monitor.c,v 1.140 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
* Copyright 2002 Markus Friedl <markus@openbsd.org>
|
* Copyright 2002 Markus Friedl <markus@openbsd.org>
|
||||||
|
@ -717,7 +717,7 @@ mm_answer_sign(int sock, Buffer *m)
|
||||||
datafellows)) != 0)
|
datafellows)) != 0)
|
||||||
fatal("%s: sshkey_sign failed: %s",
|
fatal("%s: sshkey_sign failed: %s",
|
||||||
__func__, ssh_err(r));
|
__func__, ssh_err(r));
|
||||||
} else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
|
} else if ((key = get_hostkey_public_by_index(keyid, active_state)) != NULL &&
|
||||||
auth_sock > 0) {
|
auth_sock > 0) {
|
||||||
if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
|
if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
|
||||||
p, datlen, datafellows)) != 0) {
|
p, datlen, datafellows)) != 0) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: monitor_wrap.c,v 1.82 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: monitor_wrap.c,v 1.83 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
* Copyright 2002 Markus Friedl <markus@openbsd.org>
|
* Copyright 2002 Markus Friedl <markus@openbsd.org>
|
||||||
|
@ -221,13 +221,13 @@ mm_choose_dh(int min, int nbits, int max)
|
||||||
int
|
int
|
||||||
mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
|
mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
|
||||||
{
|
{
|
||||||
Kex *kex = *pmonitor->m_pkex;
|
struct kex *kex = *pmonitor->m_pkex;
|
||||||
Buffer m;
|
Buffer m;
|
||||||
|
|
||||||
debug3("%s entering", __func__);
|
debug3("%s entering", __func__);
|
||||||
|
|
||||||
buffer_init(&m);
|
buffer_init(&m);
|
||||||
buffer_put_int(&m, kex->host_key_index(key));
|
buffer_put_int(&m, kex->host_key_index(key, active_state));
|
||||||
buffer_put_string(&m, data, datalen);
|
buffer_put_string(&m, data, datalen);
|
||||||
|
|
||||||
mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
|
mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: serverloop.c,v 1.174 2015/01/19 20:07:45 markus Exp $ */
|
/* $OpenBSD: serverloop.c,v 1.175 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
||||||
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
||||||
|
@ -544,7 +544,7 @@ drain_output(void)
|
||||||
static void
|
static void
|
||||||
process_buffered_input_packets(void)
|
process_buffered_input_packets(void)
|
||||||
{
|
{
|
||||||
dispatch_run(DISPATCH_NONBLOCK, NULL, compat20 ? active_state->kex : NULL);
|
dispatch_run(DISPATCH_NONBLOCK, NULL, active_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -874,7 +874,7 @@ server_loop2(Authctxt *authctxt)
|
||||||
if (packet_need_rekeying()) {
|
if (packet_need_rekeying()) {
|
||||||
debug("need rekeying");
|
debug("need rekeying");
|
||||||
active_state->kex->done = 0;
|
active_state->kex->done = 0;
|
||||||
kex_send_kexinit(active_state->kex);
|
kex_send_kexinit(active_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
process_input(readset);
|
process_input(readset);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: ssh-keyscan.c,v 1.93 2014/12/11 08:20:09 djm Exp $ */
|
/* $OpenBSD: ssh-keyscan.c,v 1.94 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
|
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
|
||||||
*
|
*
|
||||||
|
@ -100,7 +100,7 @@ typedef struct Connection {
|
||||||
char *c_namelist; /* Pointer to other possible addresses */
|
char *c_namelist; /* Pointer to other possible addresses */
|
||||||
char *c_output_name; /* Hostname of connection for output */
|
char *c_output_name; /* Hostname of connection for output */
|
||||||
char *c_data; /* Data read from this fd */
|
char *c_data; /* Data read from this fd */
|
||||||
Kex *c_kex; /* The key-exchange struct for ssh2 */
|
struct kex *c_kex; /* The key-exchange struct for ssh2 */
|
||||||
struct timeval c_tv; /* Time at which connection gets aborted */
|
struct timeval c_tv; /* Time at which connection gets aborted */
|
||||||
TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
|
TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
|
||||||
} con;
|
} con;
|
||||||
|
@ -221,7 +221,7 @@ keygrab_ssh1(con *c)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int
|
static int
|
||||||
hostjump(Key *hostkey)
|
hostjump(Key *hostkey, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
kexjmp_key = hostkey;
|
kexjmp_key = hostkey;
|
||||||
longjmp(kexjmp, 1);
|
longjmp(kexjmp, 1);
|
||||||
|
@ -247,7 +247,7 @@ static Key *
|
||||||
keygrab_ssh2(con *c)
|
keygrab_ssh2(con *c)
|
||||||
{
|
{
|
||||||
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
||||||
int j;
|
int r, j;
|
||||||
|
|
||||||
packet_set_connection(c->c_fd, c->c_fd);
|
packet_set_connection(c->c_fd, c->c_fd);
|
||||||
enable_compat20();
|
enable_compat20();
|
||||||
|
@ -256,7 +256,9 @@ keygrab_ssh2(con *c)
|
||||||
(c->c_keytype == KT_RSA ? "ssh-rsa" :
|
(c->c_keytype == KT_RSA ? "ssh-rsa" :
|
||||||
(c->c_keytype == KT_ED25519 ? "ssh-ed25519" :
|
(c->c_keytype == KT_ED25519 ? "ssh-ed25519" :
|
||||||
"ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521"));
|
"ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521"));
|
||||||
c->c_kex = kex_setup(myproposal);
|
if ((r = kex_setup(active_state, myproposal)) < 0)
|
||||||
|
fatal("%s: kex_setup: %s", __func__, ssh_err(r));
|
||||||
|
c->c_kex = active_state->kex;
|
||||||
#ifdef WITH_OPENSSL
|
#ifdef WITH_OPENSSL
|
||||||
c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
||||||
c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
||||||
|
@ -269,7 +271,7 @@ keygrab_ssh2(con *c)
|
||||||
|
|
||||||
if (!(j = setjmp(kexjmp))) {
|
if (!(j = setjmp(kexjmp))) {
|
||||||
nonfatal_fatal = 1;
|
nonfatal_fatal = 1;
|
||||||
dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
|
dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, active_state);
|
||||||
fprintf(stderr, "Impossible! dispatch_run() returned!\n");
|
fprintf(stderr, "Impossible! dispatch_run() returned!\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: sshconnect2.c,v 1.218 2015/01/19 20:07:45 markus Exp $ */
|
/* $OpenBSD: sshconnect2.c,v 1.219 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
||||||
* Copyright (c) 2008 Damien Miller. All rights reserved.
|
* Copyright (c) 2008 Damien Miller. All rights reserved.
|
||||||
|
@ -92,7 +92,7 @@ char *xxx_host;
|
||||||
struct sockaddr *xxx_hostaddr;
|
struct sockaddr *xxx_hostaddr;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
verify_host_key_callback(Key *hostkey)
|
verify_host_key_callback(Key *hostkey, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
|
if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
|
||||||
fatal("Host key verification failed.");
|
fatal("Host key verification failed.");
|
||||||
|
@ -157,7 +157,7 @@ void
|
||||||
ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
|
ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
|
||||||
{
|
{
|
||||||
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
|
||||||
Kex *kex;
|
struct kex *kex;
|
||||||
|
|
||||||
xxx_host = host;
|
xxx_host = host;
|
||||||
xxx_hostaddr = hostaddr;
|
xxx_hostaddr = hostaddr;
|
||||||
|
@ -204,8 +204,8 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
|
||||||
(time_t)options.rekey_interval);
|
(time_t)options.rekey_interval);
|
||||||
|
|
||||||
/* start key exchange */
|
/* start key exchange */
|
||||||
kex = kex_setup(myproposal);
|
kex_setup(active_state, myproposal);
|
||||||
active_state->kex = kex;
|
kex = active_state->kex;
|
||||||
#ifdef WITH_OPENSSL
|
#ifdef WITH_OPENSSL
|
||||||
kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
|
||||||
kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
|
||||||
|
@ -218,7 +218,7 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
|
||||||
kex->server_version_string=server_version_string;
|
kex->server_version_string=server_version_string;
|
||||||
kex->verify_host_key=&verify_host_key_callback;
|
kex->verify_host_key=&verify_host_key_callback;
|
||||||
|
|
||||||
dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
|
dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
|
||||||
|
|
||||||
if (options.use_roaming && !kex->roaming) {
|
if (options.use_roaming && !kex->roaming) {
|
||||||
debug("Roaming not allowed by server");
|
debug("Roaming not allowed by server");
|
||||||
|
|
47
sshd.c
47
sshd.c
|
@ -1,4 +1,4 @@
|
||||||
/* $OpenBSD: sshd.c,v 1.434 2015/01/19 19:52:16 markus Exp $ */
|
/* $OpenBSD: sshd.c,v 1.435 2015/01/19 20:16:15 markus Exp $ */
|
||||||
/*
|
/*
|
||||||
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
||||||
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
||||||
|
@ -836,7 +836,7 @@ list_hostkey_types(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
static Key *
|
static Key *
|
||||||
get_hostkey_by_type(int type, int need_private)
|
get_hostkey_by_type(int type, int need_private, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
Key *key;
|
Key *key;
|
||||||
|
@ -865,15 +865,15 @@ get_hostkey_by_type(int type, int need_private)
|
||||||
}
|
}
|
||||||
|
|
||||||
Key *
|
Key *
|
||||||
get_hostkey_public_by_type(int type)
|
get_hostkey_public_by_type(int type, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
return get_hostkey_by_type(type, 0);
|
return get_hostkey_by_type(type, 0, ssh);
|
||||||
}
|
}
|
||||||
|
|
||||||
Key *
|
Key *
|
||||||
get_hostkey_private_by_type(int type)
|
get_hostkey_private_by_type(int type, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
return get_hostkey_by_type(type, 1);
|
return get_hostkey_by_type(type, 1, ssh);
|
||||||
}
|
}
|
||||||
|
|
||||||
Key *
|
Key *
|
||||||
|
@ -885,7 +885,7 @@ get_hostkey_by_index(int ind)
|
||||||
}
|
}
|
||||||
|
|
||||||
Key *
|
Key *
|
||||||
get_hostkey_public_by_index(int ind)
|
get_hostkey_public_by_index(int ind, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
if (ind < 0 || ind >= options.num_host_key_files)
|
if (ind < 0 || ind >= options.num_host_key_files)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
@ -893,7 +893,7 @@ get_hostkey_public_by_index(int ind)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
get_hostkey_index(Key *key)
|
get_hostkey_index(Key *key, struct ssh *ssh)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -2432,29 +2432,30 @@ do_ssh1_kex(void)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
int
|
||||||
sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, u_int *slen,
|
sshd_hostkey_sign(Key *privkey, Key *pubkey, u_char **signature, size_t *slen,
|
||||||
u_char *data, u_int dlen)
|
u_char *data, size_t dlen, u_int flag)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
u_int xxx_slen, xxx_dlen = dlen;
|
||||||
|
|
||||||
if (privkey) {
|
if (privkey) {
|
||||||
if (PRIVSEP(key_sign(privkey, signature, slen, data, dlen) < 0))
|
if (PRIVSEP(key_sign(privkey, signature, &xxx_slen, data, xxx_dlen) < 0))
|
||||||
fatal("%s: key_sign failed", __func__);
|
fatal("%s: key_sign failed", __func__);
|
||||||
|
if (slen)
|
||||||
|
*slen = xxx_slen;
|
||||||
} else if (use_privsep) {
|
} else if (use_privsep) {
|
||||||
if (mm_key_sign(pubkey, signature, slen, data, dlen) < 0)
|
if (mm_key_sign(pubkey, signature, &xxx_slen, data, xxx_dlen) < 0)
|
||||||
fatal("%s: pubkey_sign failed", __func__);
|
fatal("%s: pubkey_sign failed", __func__);
|
||||||
|
if (slen)
|
||||||
|
*slen = xxx_slen;
|
||||||
} else {
|
} else {
|
||||||
size_t xxx_slen;
|
if ((r = ssh_agent_sign(auth_sock, pubkey, signature, slen,
|
||||||
|
|
||||||
if ((r = ssh_agent_sign(auth_sock, pubkey, signature, &xxx_slen,
|
|
||||||
data, dlen, datafellows)) != 0)
|
data, dlen, datafellows)) != 0)
|
||||||
fatal("%s: ssh_agent_sign failed: %s",
|
fatal("%s: ssh_agent_sign failed: %s",
|
||||||
__func__, ssh_err(r));
|
__func__, ssh_err(r));
|
||||||
/* XXX: Old API is u_int; new size_t */
|
|
||||||
if (slen != NULL)
|
|
||||||
*slen = xxx_slen;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -2464,7 +2465,7 @@ static void
|
||||||
do_ssh2_kex(void)
|
do_ssh2_kex(void)
|
||||||
{
|
{
|
||||||
char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
|
char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
|
||||||
Kex *kex;
|
struct kex *kex;
|
||||||
|
|
||||||
if (options.ciphers != NULL) {
|
if (options.ciphers != NULL) {
|
||||||
myproposal[PROPOSAL_ENC_ALGS_CTOS] =
|
myproposal[PROPOSAL_ENC_ALGS_CTOS] =
|
||||||
|
@ -2500,8 +2501,8 @@ do_ssh2_kex(void)
|
||||||
list_hostkey_types());
|
list_hostkey_types());
|
||||||
|
|
||||||
/* start key exchange */
|
/* start key exchange */
|
||||||
kex = kex_setup(myproposal);
|
kex_setup(active_state, myproposal);
|
||||||
active_state->kex = kex;
|
kex = active_state->kex;
|
||||||
#ifdef WITH_OPENSSL
|
#ifdef WITH_OPENSSL
|
||||||
kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
|
kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
|
||||||
kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
|
kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
|
||||||
|
@ -2518,7 +2519,7 @@ do_ssh2_kex(void)
|
||||||
kex->host_key_index=&get_hostkey_index;
|
kex->host_key_index=&get_hostkey_index;
|
||||||
kex->sign = sshd_hostkey_sign;
|
kex->sign = sshd_hostkey_sign;
|
||||||
|
|
||||||
dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
|
dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
|
||||||
|
|
||||||
session_id2 = kex->session_id;
|
session_id2 = kex->session_id;
|
||||||
session_id2_len = kex->session_id_len;
|
session_id2_len = kex->session_id_len;
|
||||||
|
|
Loading…
Reference in New Issue