version 1.11, 2015/01/19 19:52:16 |
version 1.12, 2015/01/19 20:16:15 |
|
|
|
|
#include <openssl/ecdh.h> |
#include <openssl/ecdh.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" |
|
|
void |
#include "dispatch.h" |
kexecdh_server(Kex *kex) |
#include "compat.h" |
|
#include "ssherr.h" |
|
#include "sshbuf.h" |
|
|
|
static int input_kex_ecdh_init(int, u_int32_t, void *); |
|
|
|
int |
|
kexecdh_server(struct ssh *ssh) |
{ |
{ |
|
debug("expecting SSH2_MSG_KEX_ECDH_INIT"); |
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init); |
|
return 0; |
|
} |
|
|
|
static int |
|
input_kex_ecdh_init(int type, u_int32_t seq, void *ctxt) |
|
{ |
|
struct ssh *ssh = ctxt; |
|
struct kex *kex = ssh->kex; |
EC_POINT *client_public; |
EC_POINT *client_public; |
EC_KEY *server_key; |
EC_KEY *server_key = NULL; |
const EC_GROUP *group; |
const EC_GROUP *group; |
BIGNUM *shared_secret; |
const EC_POINT *public_key; |
Key *server_host_private, *server_host_public; |
BIGNUM *shared_secret = NULL; |
|
struct sshkey *server_host_private, *server_host_public; |
u_char *server_host_key_blob = NULL, *signature = NULL; |
u_char *server_host_key_blob = NULL, *signature = NULL; |
u_char *kbuf, *hash; |
u_char *kbuf = NULL; |
u_int klen, slen, sbloblen, hashlen; |
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) |
if ((server_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(server_key) != 1) |
goto out; |
fatal("%s: EC_KEY_generate_key failed", __func__); |
} |
|
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, |
|
ssh)) == NULL) { |
|
r = SSH_ERR_NO_HOSTKEY_LOADED; |
|
goto out; |
|
} |
|
if ((client_public = EC_POINT_new(group)) == NULL) { |
|
r = SSH_ERR_ALLOC_FAIL; |
|
goto out; |
|
} |
|
if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 || |
|
(r = sshpkt_get_end(ssh)) != 0) |
|
goto out; |
|
|
debug("expecting SSH2_MSG_KEX_ECDH_INIT"); |
|
packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); |
|
if ((client_public = EC_POINT_new(group)) == NULL) |
|
fatal("%s: EC_POINT_new failed", __func__); |
|
packet_get_ecpoint(group, client_public); |
|
packet_check_eom(); |
|
|
|
if (key_ec_validate_public(group, client_public) != 0) |
|
fatal("%s: invalid client public key", __func__); |
|
|
|
#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); |
|
} |
} |