Blame SOURCES/cyrus-sasl-2.1.27-cumulative-digestmd5.patch

d7115b
diff -uPr cyrus-sasl-2.1.27/plugins/digestmd5.c cyrus-sasl-2.1.27.digestmd5/plugins/digestmd5.c
ee1218
--- cyrus-sasl-2.1.27/plugins/digestmd5.c	2022-09-08 12:22:03.782961573 -0400
ee1218
+++ cyrus-sasl-2.1.27.digestmd5/plugins/digestmd5.c	2022-09-08 12:24:20.289994669 -0400
d7115b
@@ -80,6 +80,12 @@
d7115b
 # endif
d7115b
 #endif /* WITH_DES */
d7115b
 
d7115b
+/* legacy provider with openssl 3.0 */
d7115b
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
d7115b
+#  include <openssl/provider.h>
d7115b
+#  include <openssl/crypto.h>
d7115b
+#endif
d7115b
+
d7115b
 #ifdef WIN32
d7115b
 # include <winsock2.h>
d7115b
 #else /* Unix */
d7115b
@@ -170,6 +176,12 @@
d7115b
 
d7115b
 typedef struct cipher_context cipher_context_t;
d7115b
 
d7115b
+typedef struct crypto_context {
d7115b
+    void *libctx;
d7115b
+    cipher_context_t *enc_ctx;
d7115b
+    cipher_context_t *dec_ctx;
d7115b
+} crypto_context_t;
d7115b
+
d7115b
 /* cached auth info used for fast reauth */
d7115b
 typedef struct reauth_entry {
d7115b
     char *authid;
d7115b
@@ -254,12 +266,12 @@
d7115b
     decode_context_t decode_context;
d7115b
 
d7115b
     /* if privacy mode is used use these functions for encode and decode */
d7115b
+    char *cipher_name;
d7115b
     cipher_function_t *cipher_enc;
d7115b
     cipher_function_t *cipher_dec;
d7115b
     cipher_init_t *cipher_init;
d7115b
     cipher_free_t *cipher_free;
d7115b
-    struct cipher_context *cipher_enc_context;
d7115b
-    struct cipher_context *cipher_dec_context;
d7115b
+    crypto_context_t crypto;
d7115b
 } context_t;
d7115b
 
d7115b
 struct digest_cipher {
d7115b
@@ -888,7 +900,7 @@
d7115b
 		    char *output,
d7115b
 		    unsigned *outputlen)
d7115b
 {
d7115b
-    des_context_t *c = (des_context_t *) text->cipher_dec_context;
d7115b
+    des_context_t *c = (des_context_t *) text->crypto.dec_ctx;
d7115b
     int padding, p;
d7115b
     
d7115b
     des_ede2_cbc_encrypt((void *) input,
d7115b
@@ -925,7 +937,7 @@
d7115b
 		    char *output,
d7115b
 		    unsigned *outputlen)
d7115b
 {
d7115b
-    des_context_t *c = (des_context_t *) text->cipher_enc_context;
d7115b
+    des_context_t *c = (des_context_t *) text->crypto.enc_ctx;
d7115b
     int len;
d7115b
     int paddinglen;
d7115b
     
d7115b
@@ -973,7 +985,7 @@
d7115b
 	return SASL_FAIL;
d7115b
     memcpy(c->ivec, ((char *) enckey) + 8, 8);
d7115b
 
d7115b
-    text->cipher_enc_context = (cipher_context_t *) c;
d7115b
+    text->crypto.enc_ctx = (cipher_context_t *) c;
d7115b
 
d7115b
     /* setup dec context */
d7115b
     c++;
d7115b
@@ -987,7 +999,7 @@
d7115b
     
d7115b
     memcpy(c->ivec, ((char *) deckey) + 8, 8);
d7115b
 
d7115b
-    text->cipher_dec_context = (cipher_context_t *) c;
d7115b
+    text->crypto.dec_ctx = (cipher_context_t *) c;
d7115b
     
d7115b
     return SASL_OK;
d7115b
 }
d7115b
@@ -1006,7 +1018,7 @@
d7115b
 		   char *output,
d7115b
 		   unsigned *outputlen)
d7115b
 {
d7115b
-    des_context_t *c = (des_context_t *) text->cipher_dec_context;
d7115b
+    des_context_t *c = (des_context_t *) text->crypto.dec_ctx;
d7115b
     int p, padding = 0;
d7115b
     
d7115b
     des_cbc_encrypt((void *) input,
d7115b
@@ -1046,7 +1058,7 @@
d7115b
 		   char *output,
d7115b
 		   unsigned *outputlen)
d7115b
 {
d7115b
-    des_context_t *c = (des_context_t *) text->cipher_enc_context;
d7115b
+    des_context_t *c = (des_context_t *) text->crypto.enc_ctx;
d7115b
     int len;
d7115b
     int paddinglen;
d7115b
   
d7115b
@@ -1093,7 +1105,7 @@
d7115b
 
d7115b
     memcpy(c->ivec, ((char *) enckey) + 8, 8);
d7115b
     
d7115b
-    text->cipher_enc_context = (cipher_context_t *) c;
d7115b
+    text->crypto.enc_ctx = (cipher_context_t *) c;
d7115b
 
d7115b
     /* setup dec context */
d7115b
     c++;
ee1218
@@ -1102,34 +1114,83 @@
d7115b
 
d7115b
     memcpy(c->ivec, ((char *) deckey) + 8, 8);
d7115b
     
d7115b
-    text->cipher_dec_context = (cipher_context_t *) c;
d7115b
+    text->crypto.dec_ctx = (cipher_context_t *) c;
d7115b
 
d7115b
     return SASL_OK;
d7115b
 }
d7115b
 
d7115b
 static void free_des(context_t *text)
d7115b
 {
d7115b
-    /* free des contextss. only cipher_enc_context needs to be free'd,
d7115b
-       since cipher_dec_context was allocated at the same time. */
d7115b
-    if (text->cipher_enc_context) text->utils->free(text->cipher_enc_context);
d7115b
+    /* free des contextss. only enc_ctx needs to be free'd,
d7115b
+       since dec_cxt was allocated at the same time. */
d7115b
+    if (text->crypto.enc_ctx) {
d7115b
+        text->utils->free(text->crypto.enc_ctx);
d7115b
+    }
d7115b
 }
d7115b
 
d7115b
 #endif /* WITH_DES */
d7115b
 
d7115b
 #ifdef WITH_RC4
d7115b
-#ifdef HAVE_OPENSSL
d7115b
 #include <openssl/evp.h>
d7115b
 
d7115b
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
d7115b
+typedef struct ossl3_library_context {
d7115b
+    OSSL_LIB_CTX *libctx;
d7115b
+    OSSL_PROVIDER *legacy_provider;
d7115b
+    OSSL_PROVIDER *default_provider;
d7115b
+} ossl3_context_t;
d7115b
+
d7115b
+static int init_ossl3_ctx(context_t *text)
d7115b
+{
d7115b
+    ossl3_context_t *ctx = text->utils->malloc(sizeof(ossl3_context_t));
d7115b
+    if (!ctx) return SASL_NOMEM;
d7115b
+
d7115b
+    ctx->libctx = OSSL_LIB_CTX_new();
d7115b
+    if (!ctx->libctx) {
d7115b
+        text->utils->free(ctx);
d7115b
+        return SASL_FAIL;
d7115b
+    }
d7115b
+
d7115b
+    /* Load both legacy and default provider as both may be needed */
d7115b
+    /* if they fail keep going and an error will be raised when we try to
d7115b
+     * fetch the cipher later */
d7115b
+    ctx->legacy_provider = OSSL_PROVIDER_load(ctx->libctx, "legacy");
d7115b
+    ctx->default_provider = OSSL_PROVIDER_load(ctx->libctx, "default");
d7115b
+    text->crypto.libctx = (void *)ctx;
d7115b
+
d7115b
+    return SASL_OK;
d7115b
+}
d7115b
+
d7115b
+static void free_ossl3_ctx(context_t *text)
d7115b
+{
d7115b
+    ossl3_context_t *ctx;
d7115b
+
d7115b
+    if (!text->crypto.libctx) return;
d7115b
+
d7115b
+    ctx = (ossl3_context_t *)text->crypto.libctx;
d7115b
+
d7115b
+    if (ctx->legacy_provider) OSSL_PROVIDER_unload(ctx->legacy_provider);
d7115b
+    if (ctx->default_provider) OSSL_PROVIDER_unload(ctx->default_provider);
d7115b
+    if (ctx->libctx) OSSL_LIB_CTX_free(ctx->libctx);
d7115b
+
d7115b
+    text->utils->free(ctx);
d7115b
+    text->crypto.libctx = NULL;
d7115b
+}
d7115b
+#endif
d7115b
+
d7115b
 static void free_rc4(context_t *text)
d7115b
 {
d7115b
-    if (text->cipher_enc_context) {
d7115b
-        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->cipher_enc_context);
d7115b
-        text->cipher_enc_context = NULL;
d7115b
-    }
d7115b
-    if (text->cipher_dec_context) {
d7115b
-        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->cipher_dec_context);
d7115b
-        text->cipher_dec_context = NULL;
d7115b
+    if (text->crypto.enc_ctx) {
d7115b
+        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->crypto.enc_ctx);
d7115b
+        text->crypto.enc_ctx = NULL;
d7115b
+    }
d7115b
+    if (text->crypto.dec_ctx) {
d7115b
+        EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)text->crypto.dec_ctx);
d7115b
+        text->crypto.dec_ctx = NULL;
d7115b
     }
d7115b
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
d7115b
+    free_ossl3_ctx(text);
d7115b
+#endif
d7115b
 }
d7115b
 
d7115b
 static int init_rc4(context_t *text,
ee1218
@@ -1139,23 +1200,57 @@
d7115b
     EVP_CIPHER_CTX *ctx;
d7115b
     int rc;
d7115b
 
d7115b
-    ctx = EVP_CIPHER_CTX_new();
d7115b
-    if (ctx == NULL) return SASL_NOMEM;
d7115b
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
ee1218
+    EVP_CIPHER *cipher;
d7115b
+    ossl3_context_t *ossl3_ctx;
d7115b
 
d7115b
-    rc = EVP_EncryptInit_ex(ctx, EVP_rc4(), NULL, enckey, NULL);
d7115b
-    if (rc != 1) return SASL_FAIL;
d7115b
+    rc = init_ossl3_ctx(text);
d7115b
+    if (rc != SASL_OK) return rc;
d7115b
+
d7115b
+    ossl3_ctx = (ossl3_context_t *)text->crypto.libctx;
d7115b
+    cipher = EVP_CIPHER_fetch(ossl3_ctx->libctx, "RC4", "");
d7115b
+#else
ee1218
+    const EVP_CIPHER *cipher;
d7115b
+    cipher = EVP_rc4();
d7115b
+#endif
d7115b
 
d7115b
-    text->cipher_enc_context = (void *)ctx;
d7115b
 
d7115b
     ctx = EVP_CIPHER_CTX_new();
d7115b
-    if (ctx == NULL) return SASL_NOMEM;
d7115b
+    if (ctx == NULL) {
d7115b
+        rc = SASL_NOMEM;
d7115b
+        goto done;
d7115b
+    }
d7115b
 
d7115b
-    rc = EVP_DecryptInit_ex(ctx, EVP_rc4(), NULL, deckey, NULL);
d7115b
-    if (rc != 1) return SASL_FAIL;
d7115b
+    rc = EVP_EncryptInit_ex(ctx, cipher, NULL, enckey, NULL);
d7115b
+    if (rc != 1) {
d7115b
+        rc = SASL_FAIL;
d7115b
+        goto done;
d7115b
+    }
d7115b
+    text->crypto.enc_ctx = (void *)ctx;
ee1218
+
d7115b
+    ctx = EVP_CIPHER_CTX_new();
d7115b
+    if (ctx == NULL) {
d7115b
+        rc = SASL_NOMEM;
d7115b
+        goto done;
d7115b
+    }
ee1218
+
d7115b
+    rc = EVP_DecryptInit_ex(ctx, cipher, NULL, deckey, NULL);
d7115b
+    if (rc != 1) {
d7115b
+        rc = SASL_FAIL;
d7115b
+        goto done;
d7115b
+    }
d7115b
+    text->crypto.dec_ctx = (void *)ctx;
ee1218
 
ee1218
-    text->cipher_dec_context = (void *)ctx;
d7115b
+    rc = SASL_OK;
ee1218
 
ee1218
-    return SASL_OK;
d7115b
+done:
ee1218
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
ee1218
+    EVP_CIPHER_free(cipher);
ee1218
+#endif
d7115b
+    if (rc != SASL_OK) {
d7115b
+        free_rc4(text);
d7115b
+    }
d7115b
+    return rc;
d7115b
 }
d7115b
 
d7115b
 static int dec_rc4(context_t *text,
ee1218
@@ -1169,14 +1264,14 @@
d7115b
     int rc;
d7115b
 
d7115b
     /* decrypt the text part & HMAC */
d7115b
-    rc = EVP_DecryptUpdate((EVP_CIPHER_CTX *)text->cipher_dec_context,
d7115b
+    rc = EVP_DecryptUpdate((EVP_CIPHER_CTX *)text->crypto.dec_ctx,
d7115b
                            (unsigned char *)output, &len,
d7115b
                            (const unsigned char *)input, inputlen);
d7115b
     if (rc != 1) return SASL_FAIL;
d7115b
 
d7115b
     *outputlen = len;
d7115b
 
d7115b
-    rc = EVP_DecryptFinal_ex((EVP_CIPHER_CTX *)text->cipher_dec_context,
d7115b
+    rc = EVP_DecryptFinal_ex((EVP_CIPHER_CTX *)text->crypto.dec_ctx,
d7115b
                              (unsigned char *)output + len, &len;;
d7115b
     if (rc != 1) return SASL_FAIL;
d7115b
 
ee1218
@@ -1198,7 +1293,7 @@
d7115b
     int len;
d7115b
     int rc;
d7115b
     /* encrypt the text part */
d7115b
-    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->cipher_enc_context,
d7115b
+    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->crypto.enc_ctx,
d7115b
                            (unsigned char *)output, &len,
d7115b
                            (const unsigned char *)input, inputlen);
d7115b
     if (rc != 1) return SASL_FAIL;
ee1218
@@ -1206,14 +1301,14 @@
d7115b
     *outputlen = len;
d7115b
 
d7115b
     /* encrypt the `MAC part */
d7115b
-    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->cipher_enc_context,
d7115b
+    rc = EVP_EncryptUpdate((EVP_CIPHER_CTX *)text->crypto.enc_ctx,
d7115b
                            (unsigned char *)output + *outputlen, &len,
d7115b
                            digest, 10);
d7115b
     if (rc != 1) return SASL_FAIL;
d7115b
 
d7115b
     *outputlen += len;
d7115b
 
d7115b
-    rc = EVP_EncryptFinal_ex((EVP_CIPHER_CTX *)text->cipher_enc_context,
d7115b
+    rc = EVP_EncryptFinal_ex((EVP_CIPHER_CTX *)text->crypto.enc_ctx,
d7115b
                              (unsigned char *)output + *outputlen, &len;;
d7115b
     if (rc != 1) return SASL_FAIL;
d7115b
 
ee1218
@@ -1221,188 +1316,11 @@
d7115b
 
d7115b
     return SASL_OK;
d7115b
 }
d7115b
-#else
d7115b
-/* quick generic implementation of RC4 */
d7115b
-struct rc4_context_s {
d7115b
-    unsigned char sbox[256];
d7115b
-    int i, j;
d7115b
-};
d7115b
-
d7115b
-typedef struct rc4_context_s rc4_context_t;
d7115b
-
d7115b
-static void rc4_init(rc4_context_t *text,
d7115b
-		     const unsigned char *key,
d7115b
-		     unsigned keylen)
d7115b
-{
d7115b
-    int i, j;
d7115b
-    
d7115b
-    /* fill in linearly s0=0 s1=1... */
d7115b
-    for (i=0;i<256;i++)
d7115b
-	text->sbox[i]=i;
d7115b
-    
d7115b
-    j=0;
d7115b
-    for (i = 0; i < 256; i++) {
d7115b
-	unsigned char tmp;
d7115b
-	/* j = (j + Si + Ki) mod 256 */
d7115b
-	j = (j + text->sbox[i] + key[i % keylen]) % 256;
d7115b
-	
d7115b
-	/* swap Si and Sj */
d7115b
-	tmp = text->sbox[i];
d7115b
-	text->sbox[i] = text->sbox[j];
d7115b
-	text->sbox[j] = tmp;
d7115b
-    }
d7115b
-    
d7115b
-    /* counters initialized to 0 */
d7115b
-    text->i = 0;
d7115b
-    text->j = 0;
d7115b
-}
d7115b
-
d7115b
-static void rc4_encrypt(rc4_context_t *text,
d7115b
-			const char *input,
d7115b
-			char *output,
d7115b
-			unsigned len)
d7115b
-{
d7115b
-    int tmp;
d7115b
-    int i = text->i;
d7115b
-    int j = text->j;
d7115b
-    int t;
d7115b
-    int K;
d7115b
-    const char *input_end = input + len;
d7115b
-    
d7115b
-    while (input < input_end) {
d7115b
-	i = (i + 1) % 256;
d7115b
-	
d7115b
-	j = (j + text->sbox[i]) % 256;
d7115b
-	
d7115b
-	/* swap Si and Sj */
d7115b
-	tmp = text->sbox[i];
d7115b
-	text->sbox[i] = text->sbox[j];
d7115b
-	text->sbox[j] = tmp;
d7115b
-	
d7115b
-	t = (text->sbox[i] + text->sbox[j]) % 256;
d7115b
-	
d7115b
-	K = text->sbox[t];
d7115b
-	
d7115b
-	/* byte K is Xor'ed with plaintext */
d7115b
-	*output++ = *input++ ^ K;
d7115b
-    }
d7115b
-    
d7115b
-    text->i = i;
d7115b
-    text->j = j;
d7115b
-}
d7115b
-
d7115b
-static void rc4_decrypt(rc4_context_t *text,
d7115b
-			const char *input,
d7115b
-			char *output,
d7115b
-			unsigned len)
d7115b
-{
d7115b
-    int tmp;
d7115b
-    int i = text->i;
d7115b
-    int j = text->j;
d7115b
-    int t;
d7115b
-    int K;
d7115b
-    const char *input_end = input + len;
d7115b
-    
d7115b
-    while (input < input_end) {
d7115b
-	i = (i + 1) % 256;
d7115b
-	
d7115b
-	j = (j + text->sbox[i]) % 256;
d7115b
-	
d7115b
-	/* swap Si and Sj */
d7115b
-	tmp = text->sbox[i];
d7115b
-	text->sbox[i] = text->sbox[j];
d7115b
-	text->sbox[j] = tmp;
d7115b
-	
d7115b
-	t = (text->sbox[i] + text->sbox[j]) % 256;
d7115b
-	
d7115b
-	K = text->sbox[t];
d7115b
-	
d7115b
-	/* byte K is Xor'ed with plaintext */
d7115b
-	*output++ = *input++ ^ K;
d7115b
-    }
d7115b
-    
d7115b
-    text->i = i;
d7115b
-    text->j = j;
d7115b
-}
d7115b
-
d7115b
-static void free_rc4(context_t *text)
d7115b
-{
d7115b
-    /* free rc4 context structures */
d7115b
-
d7115b
-    if(text->cipher_enc_context) text->utils->free(text->cipher_enc_context);
d7115b
-    if(text->cipher_dec_context) text->utils->free(text->cipher_dec_context);
d7115b
-}
d7115b
-
d7115b
-static int init_rc4(context_t *text, 
d7115b
-		    unsigned char enckey[16],
d7115b
-		    unsigned char deckey[16])
d7115b
-{
d7115b
-    /* allocate rc4 context structures */
d7115b
-    text->cipher_enc_context=
d7115b
-	(cipher_context_t *) text->utils->malloc(sizeof(rc4_context_t));
d7115b
-    if (text->cipher_enc_context == NULL) return SASL_NOMEM;
d7115b
-    
d7115b
-    text->cipher_dec_context=
d7115b
-	(cipher_context_t *) text->utils->malloc(sizeof(rc4_context_t));
d7115b
-    if (text->cipher_dec_context == NULL) return SASL_NOMEM;
d7115b
-    
d7115b
-    /* initialize them */
d7115b
-    rc4_init((rc4_context_t *) text->cipher_enc_context,
d7115b
-             (const unsigned char *) enckey, 16);
d7115b
-    rc4_init((rc4_context_t *) text->cipher_dec_context,
d7115b
-             (const unsigned char *) deckey, 16);
d7115b
-    
d7115b
-    return SASL_OK;
d7115b
-}
d7115b
-
d7115b
-static int dec_rc4(context_t *text,
d7115b
-		   const char *input,
d7115b
-		   unsigned inputlen,
d7115b
-		   unsigned char digest[16] __attribute__((unused)),
d7115b
-		   char *output,
d7115b
-		   unsigned *outputlen)
d7115b
-{
d7115b
-    /* decrypt the text part & HMAC */
d7115b
-    rc4_decrypt((rc4_context_t *) text->cipher_dec_context, 
d7115b
-                input, output, inputlen);
d7115b
-
d7115b
-    /* no padding so we just subtract the HMAC to get the text length */
d7115b
-    *outputlen = inputlen - 10;
d7115b
-    
d7115b
-    return SASL_OK;
d7115b
-}
d7115b
-
d7115b
-static int enc_rc4(context_t *text,
d7115b
-		   const char *input,
d7115b
-		   unsigned inputlen,
d7115b
-		   unsigned char digest[16],
d7115b
-		   char *output,
d7115b
-		   unsigned *outputlen)
d7115b
-{
d7115b
-    /* pad is zero */
d7115b
-    *outputlen = inputlen+10;
d7115b
-    
d7115b
-    /* encrypt the text part */
d7115b
-    rc4_encrypt((rc4_context_t *) text->cipher_enc_context,
d7115b
-                input,
d7115b
-                output,
d7115b
-                inputlen);
d7115b
-    
d7115b
-    /* encrypt the HMAC part */
d7115b
-    rc4_encrypt((rc4_context_t *) text->cipher_enc_context, 
d7115b
-                (const char *) digest, 
d7115b
-		(output)+inputlen, 10);
d7115b
-    
d7115b
-    return SASL_OK;
d7115b
-}
d7115b
-#endif /* HAVE_OPENSSL */
d7115b
 #endif /* WITH_RC4 */
d7115b
 
d7115b
 struct digest_cipher available_ciphers[] =
d7115b
 {
d7115b
 #ifdef WITH_RC4
d7115b
-    { "rc4-40", 40, 5, 0x01, &enc_rc4, &dec_rc4, &init_rc4, &free_rc4 },
d7115b
-    { "rc4-56", 56, 7, 0x02, &enc_rc4, &dec_rc4, &init_rc4, &free_rc4 },
d7115b
     { "rc4", 128, 16, 0x04, &enc_rc4, &dec_rc4, &init_rc4, &free_rc4 },
d7115b
 #endif
d7115b
 #ifdef WITH_DES
ee1218
@@ -2815,6 +2733,7 @@
d7115b
 	}
d7115b
 	
d7115b
 	if (cptr->name) {
d7115b
+	    text->cipher_name = cptr->name;
d7115b
 	    text->cipher_enc = cptr->cipher_enc;
d7115b
 	    text->cipher_dec = cptr->cipher_dec;
d7115b
 	    text->cipher_init = cptr->cipher_init;
ee1218
@@ -2958,7 +2877,10 @@
d7115b
 	if (text->cipher_init) {
d7115b
 	    if (text->cipher_init(text, enckey, deckey) != SASL_OK) {
d7115b
 		sparams->utils->seterror(sparams->utils->conn, 0,
d7115b
-					 "couldn't init cipher");
d7115b
+					 "couldn't init cipher '%s'",
d7115b
+                                         text->cipher_name);
d7115b
+                result = SASL_FAIL;
d7115b
+                goto FreeAllMem;
d7115b
 	    }
d7115b
 	}
d7115b
     }
ee1218
@@ -3509,6 +3431,7 @@
d7115b
 	oparams->mech_ssf = ctext->cipher->ssf;
d7115b
 
d7115b
 	nbits = ctext->cipher->n;
d7115b
+	text->cipher_name = ctext->cipher->name;
d7115b
 	text->cipher_enc = ctext->cipher->cipher_enc;
d7115b
 	text->cipher_dec = ctext->cipher->cipher_dec;
d7115b
 	text->cipher_free = ctext->cipher->cipher_free;
ee1218
@@ -3733,7 +3656,13 @@
d7115b
 	
d7115b
 	/* initialize cipher if need be */
d7115b
 	if (text->cipher_init) {
d7115b
-	    text->cipher_init(text, enckey, deckey);
d7115b
+	    if (text->cipher_init(text, enckey, deckey) != SASL_OK) {
d7115b
+	        params->utils->seterror(params->utils->conn, 0,
d7115b
+		         "internal error: failed to init cipher '%s'",
d7115b
+                         text->cipher_name);
d7115b
+                result = SASL_FAIL;
d7115b
+                goto FreeAllocatedMem;
d7115b
+            }
d7115b
 	}
d7115b
     }
d7115b