2020-05-08 05:13:14 +00:00
|
|
|
/* $OpenBSD: kexdh.c,v 1.33 2020/05/08 05:13:14 djm Exp $ */
|
2001-04-04 17:39:19 +00:00
|
|
|
/*
|
2019-01-21 10:28:01 +00:00
|
|
|
* Copyright (c) 2019 Markus Friedl. All rights reserved.
|
2001-04-04 17:39:19 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* 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"
|
|
|
|
|
2015-01-14 15:21:31 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
2019-01-21 10:28:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
|
2019-01-21 23:49:52 +00:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
2019-01-21 23:50:40 +00:00
|
|
|
#include <openssl/dh.h>
|
2019-01-21 23:49:52 +00:00
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshkey.h"
|
2003-02-24 01:03:03 +00:00
|
|
|
#include "kex.h"
|
2015-01-19 20:16:15 +00:00
|
|
|
#include "sshbuf.h"
|
2014-01-09 23:58:53 +00:00
|
|
|
#include "digest.h"
|
2019-01-21 12:11:21 +00:00
|
|
|
#include "ssherr.h"
|
2019-01-21 10:03:37 +00:00
|
|
|
#include "dh.h"
|
2020-05-08 05:13:14 +00:00
|
|
|
#include "log.h"
|
2001-04-04 17:39:19 +00:00
|
|
|
|
2019-01-21 10:00:23 +00:00
|
|
|
int
|
|
|
|
kex_dh_keygen(struct kex *kex)
|
|
|
|
{
|
|
|
|
switch (kex->kex_type) {
|
|
|
|
case KEX_DH_GRP1_SHA1:
|
|
|
|
kex->dh = dh_new_group1();
|
|
|
|
break;
|
|
|
|
case KEX_DH_GRP14_SHA1:
|
|
|
|
case KEX_DH_GRP14_SHA256:
|
|
|
|
kex->dh = dh_new_group14();
|
|
|
|
break;
|
|
|
|
case KEX_DH_GRP16_SHA512:
|
|
|
|
kex->dh = dh_new_group16();
|
|
|
|
break;
|
|
|
|
case KEX_DH_GRP18_SHA512:
|
|
|
|
kex->dh = dh_new_group18();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
if (kex->dh == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
return (dh_gen_key(kex->dh, kex->we_need * 8));
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:03:37 +00:00
|
|
|
int
|
|
|
|
kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
|
|
|
|
{
|
|
|
|
BIGNUM *shared_secret = NULL;
|
|
|
|
u_char *kbuf = NULL;
|
|
|
|
size_t klen = 0;
|
|
|
|
int kout, r;
|
|
|
|
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
fprintf(stderr, "dh_pub= ");
|
|
|
|
BN_print_fp(stderr, dh_pub);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
debug("bits %d", BN_num_bits(dh_pub));
|
|
|
|
DHparams_print_fp(stderr, kex->dh);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!dh_pub_is_valid(kex->dh, dh_pub)) {
|
|
|
|
r = SSH_ERR_MESSAGE_INCOMPLETE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
klen = DH_size(kex->dh);
|
|
|
|
if ((kbuf = malloc(klen)) == NULL ||
|
|
|
|
(shared_secret = BN_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
|
|
|
|
BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
|
|
|
|
r = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
dump_digest("shared secret", kbuf, kout);
|
|
|
|
#endif
|
|
|
|
r = sshbuf_put_bignum2(out, shared_secret);
|
|
|
|
out:
|
|
|
|
freezero(kbuf, klen);
|
|
|
|
BN_clear_free(shared_secret);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:16:15 +00:00
|
|
|
int
|
2019-01-21 10:28:01 +00:00
|
|
|
kex_dh_keypair(struct kex *kex)
|
2001-04-04 17:39:19 +00:00
|
|
|
{
|
2019-01-21 10:28:01 +00:00
|
|
|
const BIGNUM *pub_key;
|
|
|
|
struct sshbuf *buf = NULL;
|
2015-01-19 20:16:15 +00:00
|
|
|
int r;
|
2001-04-04 17:39:19 +00:00
|
|
|
|
2019-01-21 10:28:01 +00:00
|
|
|
if ((r = kex_dh_keygen(kex)) != 0)
|
2015-01-19 20:16:15 +00:00
|
|
|
return r;
|
2019-01-21 10:28:01 +00:00
|
|
|
DH_get0_key(kex->dh, &pub_key, NULL);
|
|
|
|
if ((buf = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_bignum2(buf, pub_key)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(buf, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
#ifdef DEBUG_KEXDH
|
|
|
|
DHparams_print_fp(stderr, kex->dh);
|
|
|
|
fprintf(stderr, "pub= ");
|
|
|
|
BN_print_fp(stderr, pub_key);
|
|
|
|
fprintf(stderr, "\n");
|
2001-04-04 17:39:19 +00:00
|
|
|
#endif
|
2019-01-21 10:40:11 +00:00
|
|
|
kex->client_pub = buf;
|
2019-01-21 10:28:01 +00:00
|
|
|
buf = NULL;
|
|
|
|
out:
|
|
|
|
sshbuf_free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_dh_enc(struct kex *kex, const struct sshbuf *client_blob,
|
2019-01-21 10:28:01 +00:00
|
|
|
struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
const BIGNUM *pub_key;
|
|
|
|
struct sshbuf *server_blob = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*server_blobp = NULL;
|
|
|
|
*shared_secretp = NULL;
|
|
|
|
|
|
|
|
if ((r = kex_dh_keygen(kex)) != 0)
|
|
|
|
goto out;
|
|
|
|
DH_get0_key(kex->dh, &pub_key, NULL);
|
|
|
|
if ((server_blob = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
2015-01-19 20:16:15 +00:00
|
|
|
}
|
2019-01-21 10:28:01 +00:00
|
|
|
if ((r = sshbuf_put_bignum2(server_blob, pub_key)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(server_blob, NULL)) != 0)
|
|
|
|
goto out;
|
2019-01-21 10:35:09 +00:00
|
|
|
if ((r = kex_dh_dec(kex, client_blob, shared_secretp)) != 0)
|
2019-01-21 10:28:01 +00:00
|
|
|
goto out;
|
|
|
|
*server_blobp = server_blob;
|
|
|
|
server_blob = NULL;
|
|
|
|
out:
|
|
|
|
DH_free(kex->dh);
|
|
|
|
kex->dh = NULL;
|
|
|
|
sshbuf_free(server_blob);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-21 10:35:09 +00:00
|
|
|
kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
|
2019-01-21 10:28:01 +00:00
|
|
|
struct sshbuf **shared_secretp)
|
|
|
|
{
|
|
|
|
struct sshbuf *buf = NULL;
|
|
|
|
BIGNUM *dh_pub = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
*shared_secretp = NULL;
|
|
|
|
|
|
|
|
if ((buf = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-01-21 10:35:09 +00:00
|
|
|
if ((r = sshbuf_put_stringb(buf, dh_blob)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum2(buf, &dh_pub)) != 0)
|
2019-01-21 10:28:01 +00:00
|
|
|
goto out;
|
|
|
|
sshbuf_reset(buf);
|
|
|
|
if ((r = kex_dh_compute_key(kex, dh_pub, buf)) != 0)
|
|
|
|
goto out;
|
|
|
|
*shared_secretp = buf;
|
|
|
|
buf = NULL;
|
|
|
|
out:
|
|
|
|
DH_free(kex->dh);
|
|
|
|
kex->dh = NULL;
|
|
|
|
sshbuf_free(buf);
|
|
|
|
return r;
|
2001-04-04 17:39:19 +00:00
|
|
|
}
|
2015-01-14 15:21:31 +00:00
|
|
|
#endif /* WITH_OPENSSL */
|