rcolebaugh / rpms / openssh

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