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

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