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