f726e5
commit 2c3ef499bfffce3cfd315edeebf202850ba4e00a
f726e5
Author: Jakub Jelen <jjelen@redhat.com>
f726e5
Date:   Tue Apr 16 15:35:18 2019 +0200
f726e5
f726e5
    Use the new OpenSSL KDF
f726e5
f726e5
diff --git a/configure.ac b/configure.ac
f726e5
index 2a455e4e..e01c3d43 100644
f726e5
--- a/configure.ac
f726e5
+++ b/configure.ac
f726e5
@@ -2712,6 +2712,7 @@ if test "x$openssl" = "xyes" ; then
f726e5
 		HMAC_CTX_init \
f726e5
 		RSA_generate_key_ex \
f726e5
 		RSA_get_default_method \
f726e5
+		EVP_KDF_CTX_new_id \
f726e5
 	])
f726e5
 
f726e5
 	# OpenSSL_add_all_algorithms may be a macro.
f726e5
diff --git a/kex.c b/kex.c
f726e5
index b6f041f4..1fbce2bb 100644
f726e5
--- a/kex.c
f726e5
+++ b/kex.c
f726e5
@@ -38,6 +38,9 @@
f726e5
 #ifdef WITH_OPENSSL
f726e5
 #include <openssl/crypto.h>
f726e5
 #include <openssl/dh.h>
f726e5
+# ifdef HAVE_EVP_KDF_CTX_NEW_ID
f726e5
+# include <openssl/kdf.h>
f726e5
+# endif
f726e5
 #endif
f726e5
 
f726e5
 #include "ssh.h"
f726e5
@@ -942,6 +945,95 @@ kex_choose_conf(struct ssh *ssh)
f726e5
 	return r;
f726e5
 }
f726e5
 
f726e5
+#ifdef HAVE_EVP_KDF_CTX_NEW_ID
f726e5
+static const EVP_MD *
f726e5
+digest_to_md(int digest_type)
f726e5
+{
f726e5
+	switch (digest_type) {
f726e5
+	case SSH_DIGEST_SHA1:
f726e5
+		return EVP_sha1();
f726e5
+	case SSH_DIGEST_SHA256:
f726e5
+		return EVP_sha256();
f726e5
+	case SSH_DIGEST_SHA384:
f726e5
+		return EVP_sha384();
f726e5
+	case SSH_DIGEST_SHA512:
f726e5
+		return EVP_sha512();
f726e5
+	}
f726e5
+	return NULL;
f726e5
+}
f726e5
+
f726e5
+static int
f726e5
+derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
f726e5
+    const struct sshbuf *shared_secret, u_char **keyp)
f726e5
+{
f726e5
+	struct kex *kex = ssh->kex;
f726e5
+	EVP_KDF_CTX *ctx = NULL;
f726e5
+	u_char *key = NULL;
f726e5
+	int r, key_len;
f726e5
+
f726e5
+	if ((key_len = ssh_digest_bytes(kex->hash_alg)) == 0)
f726e5
+		return SSH_ERR_INVALID_ARGUMENT;
f726e5
+	key_len = ROUNDUP(need, key_len);
f726e5
+	if ((key = calloc(1, key_len)) == NULL) {
f726e5
+		r = SSH_ERR_ALLOC_FAIL;
f726e5
+		goto out;
f726e5
+	}
f726e5
+
f726e5
+	ctx = EVP_KDF_CTX_new_id(EVP_KDF_SSHKDF);
f726e5
+	if (!ctx) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+
f726e5
+	r = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_MD, digest_to_md(kex->hash_alg));
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+	r = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_KEY,
f726e5
+	    sshbuf_ptr(shared_secret), sshbuf_len(shared_secret));
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+	r = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_XCGHASH, hash, hashlen);
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+	r = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_TYPE, id);
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+	r = EVP_KDF_ctrl(ctx, EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID,
f726e5
+	    kex->session_id, kex->session_id_len);
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+	r = EVP_KDF_derive(ctx, key, key_len);
f726e5
+	if (r != 1) {
f726e5
+		r = SSH_ERR_LIBCRYPTO_ERROR;
f726e5
+		goto out;
f726e5
+	}
f726e5
+#ifdef DEBUG_KEX
f726e5
+	fprintf(stderr, "key '%c'== ", id);
f726e5
+	dump_digest("key", key, key_len);
f726e5
+#endif
f726e5
+	*keyp = key;
f726e5
+	key = NULL;
f726e5
+	r = 0;
f726e5
+
f726e5
+out:
f726e5
+	free (key);
f726e5
+	EVP_KDF_CTX_free(ctx);
f726e5
+	if (r < 0) {
f726e5
+		return r;
f726e5
+	}
f726e5
+	return 0;
f726e5
+}
f726e5
+#else
f726e5
 static int
f726e5
 derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
f726e5
     const struct sshbuf *shared_secret, u_char **keyp)
f726e5
@@ -1004,6 +1096,7 @@ derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
f726e5
 	ssh_digest_free(hashctx);
f726e5
 	return r;
f726e5
 }
f726e5
+#endif /* HAVE_OPENSSL_EVP_KDF_CTX_NEW_ID */
f726e5
 
f726e5
 #define NKEYS	6
f726e5
 int
f726e5