rcolebaugh / rpms / openssh

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