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