bmh10 / rpms / openssh

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