version 1.16, 2015/01/19 19:52:16 |
version 1.17, 2015/01/19 20:16:15 |
|
|
#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; |
} |
} |
dh_gen_key(dh, kex->we_need * 8); |
if (kex->dh == NULL) { |
packet_start(SSH2_MSG_KEXDH_INIT); |
r = SSH_ERR_ALLOC_FAIL; |
packet_put_bignum2(dh->pub_key); |
goto out; |
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; |
|
} |
|
|
/* key, cert */ |
static int |
server_host_key_blob = packet_get_string(&sbloblen); |
input_kex_dh(int type, u_int32_t seq, void *ctxt) |
server_host_key = key_from_blob(server_host_key_blob, sbloblen); |
{ |
if (server_host_key == NULL) |
struct ssh *ssh = ctxt; |
fatal("cannot decode server_host_key_blob"); |
struct kex *kex = ssh->kex; |
if (server_host_key->type != kex->hostkey_type) |
BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; |
fatal("type mismatch for decoded server_host_key_blob"); |
struct sshkey *server_host_key = NULL; |
if (kex->verify_host_key == NULL) |
u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL; |
fatal("cannot verify server_host_key"); |
u_char hash[SSH_DIGEST_MAX_LENGTH]; |
if (kex->verify_host_key(server_host_key) == -1) |
size_t klen = 0, slen, sbloblen, hashlen; |
fatal("server_host_key verification failed"); |
int kout, r; |
|
|
|
if (kex->verify_host_key == NULL) { |
|
r = SSH_ERR_INVALID_ARGUMENT; |
|
goto out; |
|
} |
|
/* key, cert */ |
|
if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
|
&sbloblen)) != 0 || |
|
(r = sshkey_from_blob(server_host_key_blob, sbloblen, |
|
&server_host_key)) != 0) |
|
goto out; |
|
if (server_host_key->type != kex->hostkey_type) { |
|
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) |
|
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; |
} |
} |