version 1.3, 2011/07/25 03:03:10 |
version 1.3.10.1, 2017/08/15 04:39:21 |
|
|
/* $NetBSD$ */ |
/* $NetBSD$ */ |
/* $OpenBSD: kexgexc.c,v 1.12 2010/11/10 01:33:07 djm Exp $ */ |
/* $OpenBSD: kexgexc.c,v 1.23 2016/09/12 01:22:38 deraadt 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. |
|
|
|
|
#include "includes.h" |
#include "includes.h" |
__RCSID("$NetBSD$"); |
__RCSID("$NetBSD$"); |
|
|
|
#include <sys/param.h> |
#include <sys/types.h> |
#include <sys/types.h> |
|
|
#include <openssl/dh.h> |
#include <openssl/dh.h> |
Line 35 __RCSID("$NetBSD$"); |
|
Line 37 __RCSID("$NetBSD$"); |
|
#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" |
|
#include "misc.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 *); |
{ |
|
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; |
|
BIGNUM *p = NULL, *g = NULL; |
|
Key *server_host_key; |
|
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->we_need * 8); |
|
|
|
if (datafellows & SSH_OLD_DHGEX) { |
|
/* Old GEX request */ |
|
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD); |
|
packet_put_int(nbits); |
|
min = DH_GRP_MIN; |
|
max = DH_GRP_MAX; |
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits); |
|
} else { |
|
/* New GEX request */ |
|
min = DH_GRP_MIN; |
|
max = DH_GRP_MAX; |
|
packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST); |
|
packet_put_int(min); |
|
packet_put_int(nbits); |
|
packet_put_int(max); |
|
|
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", |
int |
min, nbits, max); |
kexgex_client(struct ssh *ssh) |
} |
{ |
|
struct kex *kex = ssh->kex; |
|
int r; |
|
u_int nbits; |
|
|
|
nbits = dh_estimate(kex->dh_need * 8); |
|
|
|
kex->min = DH_GRP_MIN; |
|
kex->max = DH_GRP_MAX; |
|
kex->nbits = nbits; |
|
if (datafellows & SSH_BUG_DHGEX_LARGE) |
|
kex->nbits = MINIMUM(kex->nbits, 4096); |
|
/* New GEX request */ |
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 || |
|
(r = sshpkt_put_u32(ssh, kex->min)) != 0 || |
|
(r = sshpkt_put_u32(ssh, kex->nbits)) != 0 || |
|
(r = sshpkt_put_u32(ssh, kex->max)) != 0 || |
|
(r = sshpkt_send(ssh)) != 0) |
|
goto out; |
|
debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent", |
|
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) |
|
fatal("DH_GEX group out of range: %d !< %d !< %d", |
|
min, BN_num_bits(p), max); |
|
|
|
dh = dh_new_group(g, p); |
if ((p = BN_new()) == NULL || |
dh_gen_key(dh, kex->we_need * 8); |
(g = BN_new()) == NULL) { |
|
r = SSH_ERR_ALLOC_FAIL; |
|
goto out; |
|
} |
|
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 (server_host_key->type != kex->hostkey_type || |
|
(kex->hostkey_type == KEY_ECDSA && |
|
server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
|
r = SSH_ERR_KEY_TYPE_MISMATCH; |
|
goto out; |
|
} |
|
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"); |
|
memset(kbuf, 0, klen); |
|
xfree(kbuf); |
|
|
|
if (datafellows & SSH_OLD_DHGEX) |
|
min = max = -1; |
|
|
|
/* calc and verify H */ |
/* calc and verify H */ |
kexgex_hash( |
hashlen = sizeof(hash); |
kex->evp_md, |
if ((r = kexgex_hash( |
|
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) |
xfree(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); |
|
xfree(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, 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; |
} |
} |