2018-12-27 03:25:24 +00:00
|
|
|
/* $OpenBSD: kexc25519s.c,v 1.12 2018/12/27 03:25:25 djm Exp $ */
|
2013-11-04 11:59:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
|
|
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
|
|
|
* Copyright (c) 2013 Aris Adamantiadis. All rights reserved.
|
|
|
|
*
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2015-04-27 00:37:53 +00:00
|
|
|
#include <stdio.h>
|
2013-11-04 11:59:27 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshkey.h"
|
2013-11-04 11:59:27 +00:00
|
|
|
#include "cipher.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "digest.h"
|
2013-11-04 11:59:27 +00:00
|
|
|
#include "kex.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "ssh2.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "ssherr.h"
|
2013-11-04 11:59:27 +00:00
|
|
|
|
2017-05-31 04:56:07 +00:00
|
|
|
static int input_kex_c25519_init(int, u_int32_t, struct ssh *);
|
2015-01-19 20:16:15 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
kexc25519_server(struct ssh *ssh)
|
|
|
|
{
|
|
|
|
debug("expecting SSH2_MSG_KEX_ECDH_INIT");
|
|
|
|
ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_c25519_init);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-05-31 04:56:07 +00:00
|
|
|
input_kex_c25519_init(int type, u_int32_t seq, struct ssh *ssh)
|
2013-11-04 11:59:27 +00:00
|
|
|
{
|
2015-01-19 20:16:15 +00:00
|
|
|
struct kex *kex = ssh->kex;
|
|
|
|
struct sshkey *server_host_private, *server_host_public;
|
|
|
|
struct sshbuf *shared_secret = NULL;
|
2013-11-04 11:59:27 +00:00
|
|
|
u_char *server_host_key_blob = NULL, *signature = NULL;
|
|
|
|
u_char server_key[CURVE25519_SIZE];
|
|
|
|
u_char *client_pubkey = NULL;
|
|
|
|
u_char server_pubkey[CURVE25519_SIZE];
|
2015-01-19 20:16:15 +00:00
|
|
|
u_char hash[SSH_DIGEST_MAX_LENGTH];
|
|
|
|
size_t slen, pklen, sbloblen, hashlen;
|
|
|
|
int r;
|
2013-11-04 11:59:27 +00:00
|
|
|
|
|
|
|
/* generate private key */
|
|
|
|
kexc25519_keygen(server_key, server_pubkey);
|
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
dump_digest("server private key:", server_key, sizeof(server_key));
|
|
|
|
#endif
|
|
|
|
if (kex->load_host_public_key == NULL ||
|
2015-01-19 20:16:15 +00:00
|
|
|
kex->load_host_private_key == NULL) {
|
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-26 06:10:03 +00:00
|
|
|
server_host_public = kex->load_host_public_key(kex->hostkey_type,
|
|
|
|
kex->hostkey_nid, ssh);
|
|
|
|
server_host_private = kex->load_host_private_key(kex->hostkey_type,
|
|
|
|
kex->hostkey_nid, ssh);
|
2015-01-20 07:55:33 +00:00
|
|
|
if (server_host_public == NULL) {
|
2015-01-19 20:16:15 +00:00
|
|
|
r = SSH_ERR_NO_HOSTKEY_LOADED;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-11-04 11:59:27 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-11-04 11:59:27 +00:00
|
|
|
#ifdef DEBUG_KEXECDH
|
|
|
|
dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
|
|
|
|
#endif
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((shared_secret = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = kexc25519_shared_key(server_key, client_pubkey,
|
|
|
|
shared_secret)) < 0)
|
|
|
|
goto out;
|
2013-11-04 11:59:27 +00:00
|
|
|
|
|
|
|
/* calc H */
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
|
|
|
|
&sbloblen)) != 0)
|
|
|
|
goto out;
|
|
|
|
hashlen = sizeof(hash);
|
|
|
|
if ((r = kex_c25519_hash(
|
2014-01-09 23:58:53 +00:00
|
|
|
kex->hash_alg,
|
2018-12-27 03:25:24 +00:00
|
|
|
kex->client_version,
|
|
|
|
kex->server_version,
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
|
|
|
|
sshbuf_ptr(kex->my), sshbuf_len(kex->my),
|
2013-11-04 11:59:27 +00:00
|
|
|
server_host_key_blob, sbloblen,
|
|
|
|
client_pubkey,
|
|
|
|
server_pubkey,
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
|
|
|
|
hash, &hashlen)) < 0)
|
|
|
|
goto out;
|
2013-11-04 11:59:27 +00:00
|
|
|
|
|
|
|
/* save session id := H */
|
|
|
|
if (kex->session_id == NULL) {
|
|
|
|
kex->session_id_len = hashlen;
|
2015-01-19 20:16:15 +00:00
|
|
|
kex->session_id = malloc(kex->session_id_len);
|
|
|
|
if (kex->session_id == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-11-04 11:59:27 +00:00
|
|
|
memcpy(kex->session_id, hash, kex->session_id_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sign H */
|
2015-12-04 16:41:28 +00:00
|
|
|
if ((r = kex->sign(server_host_private, server_host_public, &signature,
|
|
|
|
&slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
|
2015-01-19 20:16:15 +00:00
|
|
|
goto out;
|
2013-11-04 11:59:27 +00:00
|
|
|
|
|
|
|
/* send server hostkey, ECDH pubkey 'Q_S' and signed H */
|
2015-01-19 20:16:15 +00:00
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
|
|
|
|
(r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
|
|
|
|
(r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 ||
|
|
|
|
(r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
|
|
|
|
(r = sshpkt_send(ssh)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
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));
|
2013-11-04 11:59:27 +00:00
|
|
|
free(server_host_key_blob);
|
2015-01-19 20:16:15 +00:00
|
|
|
free(signature);
|
2013-11-04 11:59:27 +00:00
|
|
|
free(client_pubkey);
|
2015-01-19 20:16:15 +00:00
|
|
|
sshbuf_free(shared_secret);
|
|
|
|
return r;
|
2013-11-04 11:59:27 +00:00
|
|
|
}
|