jonathancammack / rpms / openssh

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