rcolebaugh / rpms / openssh

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