Blame SOURCES/decrypt_aesgcm.patch

e89858
commit 71dcbcda4692922360fe6222bd6556cce89d98e4
e89858
Author: John Dennis <jdennis@redhat.com>
e89858
Date:   Thu Mar 31 16:09:11 2016 -0400
e89858
e89858
    apr_jwe_decrypt_content_aesgcm() null terminate string
e89858
    
e89858
    Also fixes unit test failure.
e89858
    
e89858
    The test test_jwt_decrypt_gcm() in test.c would sometimes fail. The
e89858
    failure was caused by extra garbage appearing in the decrypted string
e89858
    after the expected string. This is due to the failure of
e89858
    apr_jwe_decrypt_content_aesgcm() to null terminate the string as is
e89858
    similarity done in apr_jwe_decrypt_content_aescbc().
e89858
    
e89858
    This patch copies the logic from apr_jwe_decrypt_content_aescbc()
e89858
    regarding the decryption output length (p_len) and the final output
e89858
    length (f_len) which are then used to compute the end of the data in
e89858
    the output buffer, the next octet after the decrypted data in the
e89858
    output buffer is then assigned a null terminator.
e89858
    
e89858
    Signed-off-by: John Dennis <jdennis@redhat.com>
e89858
e89858
diff --git a/src/jose/apr_jwe.c b/src/jose/apr_jwe.c
e89858
index 6800033..d4b64cb 100644
e89858
--- a/src/jose/apr_jwe.c
e89858
+++ b/src/jose/apr_jwe.c
e89858
@@ -382,7 +382,7 @@ apr_byte_t apr_jwe_decrypt_content_aesgcm(apr_pool_t *pool,
e89858
 		apr_jwt_error_t *err) {
e89858
 
e89858
 	EVP_CIPHER_CTX *ctx;
e89858
-	int outlen, rv;
e89858
+	int rv;
e89858
 
e89858
 	ctx = EVP_CIPHER_CTX_new();
e89858
 	if (!EVP_DecryptInit_ex(ctx, apr_jwe_enc_to_openssl_cipher(header->enc),
e89858
@@ -391,8 +391,9 @@ apr_byte_t apr_jwe_decrypt_content_aesgcm(apr_pool_t *pool,
e89858
 		return FALSE;
e89858
 	}
e89858
 
e89858
+	int p_len = cipher_text->len, f_len = 0;
e89858
 	unsigned char *plaintext = apr_palloc(pool,
e89858
-			cipher_text->len
e89858
+			p_len
e89858
 			+ EVP_CIPHER_block_size(
e89858
 					apr_jwe_enc_to_openssl_cipher(header->enc)));
e89858
 
e89858
@@ -407,13 +408,13 @@ apr_byte_t apr_jwe_decrypt_content_aesgcm(apr_pool_t *pool,
e89858
 		return FALSE;
e89858
 	}
e89858
 	/* zero or more calls to specify any AAD */
e89858
-	if (!EVP_DecryptUpdate(ctx, NULL, &outlen, (unsigned char *) aad,
e89858
+	if (!EVP_DecryptUpdate(ctx, NULL, &p_len, (unsigned char *) aad,
e89858
 			aad_len)) {
e89858
 		apr_jwt_error_openssl(err, "EVP_DecryptUpdate (aad)");
e89858
 		return FALSE;
e89858
 	}
e89858
 	/* decrypt plaintext */
e89858
-	if (!EVP_DecryptUpdate(ctx, plaintext, &outlen,
e89858
+	if (!EVP_DecryptUpdate(ctx, plaintext, &p_len,
e89858
 			(unsigned char *) cipher_text->value, cipher_text->len)) {
e89858
 		apr_jwt_error_openssl(err, "EVP_DecryptUpdate (ciphertext)");
e89858
 		return FALSE;
e89858
@@ -425,7 +426,8 @@ apr_byte_t apr_jwe_decrypt_content_aesgcm(apr_pool_t *pool,
e89858
 	}
e89858
 
e89858
 	/* finalise: note get no output for GCM */
e89858
-	rv = EVP_DecryptFinal_ex(ctx, plaintext, &outlen);
e89858
+	rv = EVP_DecryptFinal_ex(ctx, plaintext, &f_len);
e89858
+	plaintext[p_len + f_len] = '\0';
e89858
 
e89858
 	EVP_CIPHER_CTX_free(ctx);
e89858