Blame SOURCES/cyrus-sasl-pr559-RC4-openssl.patch

138d55
From 8aa9ae816ddf66921b4a8a0f422517e6f2e55ac6 Mon Sep 17 00:00:00 2001
138d55
From: Simo Sorce <simo@redhat.com>
138d55
Date: Wed, 27 Mar 2019 14:29:08 -0400
138d55
Subject: [PATCH] Use Openssl RC4 when available
138d55
138d55
Signed-off-by: Simo Sorce <simo@redhat.com>
138d55
---
138d55
 configure.ac        |   5 +--
138d55
 plugins/digestmd5.c | 107 +++++++++++++++++++++++++++++++++++++++++++-
138d55
 2 files changed, 108 insertions(+), 4 deletions(-)
138d55
138d55
diff --git a/configure.ac b/configure.ac
138d55
index 388f5d02..cfdee4a2 100644
138d55
--- a/configure.ac
138d55
+++ b/configure.ac
138d55
@@ -1103,13 +1103,12 @@ AC_ARG_WITH(configdir, [   --with-configdir=DIR    set the directory where confi
138d55
   configdir='${plugindir}:${sysconfdir}/sasl2')
138d55
 AC_SUBST(configdir)
138d55
 
138d55
-dnl look for rc4 libraries. we accept the CMU one or one from openSSL
138d55
-AC_ARG_WITH(rc4, [  --with-rc4              use internal rc4 routines [[yes]] ],
138d55
+AC_ARG_WITH(rc4, [  --with-rc4              use rc4 routines [[yes]] ],
138d55
 	with_rc4=$withval,
138d55
 	with_rc4=yes)
138d55
 
138d55
 if test "$with_rc4" != no; then
138d55
-    AC_DEFINE(WITH_RC4,[],[Use internal RC4 implementation?])
138d55
+    AC_DEFINE(WITH_RC4,[],[Use RC4])
138d55
 fi
138d55
 
138d55
 building_for_macosx=no
138d55
diff --git a/plugins/digestmd5.c b/plugins/digestmd5.c
138d55
index df35093d..c6b54317 100644
138d55
--- a/plugins/digestmd5.c
138d55
+++ b/plugins/digestmd5.c
138d55
@@ -1117,6 +1117,111 @@ static void free_des(context_t *text)
138d55
 #endif /* WITH_DES */
138d55
 
138d55
 #ifdef WITH_RC4
138d55
+#ifdef HAVE_OPENSSL
138d55
+#include <openssl/evp.h>
138d55
+
138d55
+static void free_rc4(context_t *text)
138d55
+{
138d55
+    if (text->cipher_enc_context) {
138d55
+        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->cipher_enc_context);
138d55
+        text->cipher_enc_context = NULL;
138d55
+    }
138d55
+    if (text->cipher_dec_context) {
138d55
+        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->cipher_dec_context);
138d55
+        text->cipher_dec_context = NULL;
138d55
+    }
138d55
+}
138d55
+
138d55
+static int init_rc4(context_t *text,
138d55
+		    unsigned char enckey[16],
138d55
+		    unsigned char deckey[16])
138d55
+{
138d55
+    EVP_CIPHER_CTX *ctx;
138d55
+    int rc;
138d55
+
138d55
+    ctx = EVP_CIPHER_CTX_new();
138d55
+    if (ctx == NULL) return SASL_NOMEM;
138d55
+
138d55
+    rc = EVP_EncryptInit_ex(ctx, EVP_rc4(), NULL, enckey, NULL);
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    text->cipher_enc_context = (void *)ctx;
138d55
+
138d55
+    ctx = EVP_CIPHER_CTX_new();
138d55
+    if (ctx == NULL) return SASL_NOMEM;
138d55
+
138d55
+    rc = EVP_DecryptInit_ex(ctx, EVP_rc4(), NULL, deckey, NULL);
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    text->cipher_dec_context = (void *)ctx;
138d55
+
138d55
+    return SASL_OK;
138d55
+}
138d55
+
138d55
+static int dec_rc4(context_t *text,
138d55
+		   const char *input,
138d55
+		   unsigned inputlen,
138d55
+		   unsigned char digest[16] __attribute__((unused)),
138d55
+		   char *output,
138d55
+		   unsigned *outputlen)
138d55
+{
138d55
+    int len;
138d55
+    int rc;
138d55
+
138d55
+    /* decrypt the text part & HMAC */
138d55
+    rc = EVP_DecryptUpdate((EVP_CIPHER_CTX *)text->cipher_dec_context,
138d55
+                           (unsigned char *)output, &len,
138d55
+                           (const unsigned char *)input, inputlen);
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    *outputlen = len;
138d55
+
138d55
+    rc = EVP_DecryptFinal_ex((EVP_CIPHER_CTX *)text->cipher_dec_context,
138d55
+                             (unsigned char *)output + len, &len;;
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    *outputlen += len;
138d55
+
138d55
+    /* subtract the HMAC to get the text length */
138d55
+    *outputlen -= 10;
138d55
+
138d55
+    return SASL_OK;
138d55
+}
138d55
+
138d55
+static int enc_rc4(context_t *text,
138d55
+		   const char *input,
138d55
+		   unsigned inputlen,
138d55
+		   unsigned char digest[16],
138d55
+		   char *output,
138d55
+		   unsigned *outputlen)
138d55
+{
138d55
+    int len;
138d55
+    int rc;
138d55
+    /* encrypt the text part */
138d55
+    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->cipher_enc_context,
138d55
+                           (unsigned char *)output, &len,
138d55
+                           (const unsigned char *)input, inputlen);
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    *outputlen = len;
138d55
+
138d55
+    /* encrypt the `MAC part */
138d55
+    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->cipher_enc_context,
138d55
+                           (unsigned char *)output + *outputlen, &len,
138d55
+                           digest, 10);
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    *outputlen += len;
138d55
+
138d55
+    rc = EVP_EncryptFinal_ex((EVP_CIPHER_CTX *)text->cipher_enc_context,
138d55
+                             (unsigned char *)output + *outputlen, &len;;
138d55
+    if (rc != 1) return SASL_FAIL;
138d55
+
138d55
+    *outputlen += len;
138d55
+
138d55
+    return SASL_OK;
138d55
+}
138d55
+#else
138d55
 /* quick generic implementation of RC4 */
138d55
 struct rc4_context_s {
138d55
     unsigned char sbox[256];
138d55
@@ -1296,7 +1401,7 @@ static int enc_rc4(context_t *text,
138d55
     
138d55
     return SASL_OK;
138d55
 }
138d55
-
138d55
+#endif /* HAVE_OPENSSL */
138d55
 #endif /* WITH_RC4 */
138d55
 
138d55
 struct digest_cipher available_ciphers[] =