Blame SOURCES/0011-Evp-cipher-buffers.patch

f6b043
diff --git a/print-esp.c b/print-esp.c
f6b043
index 511ee8a3..5b282526 100644
f6b043
--- a/print-esp.c
f6b043
+++ b/print-esp.c
f6b043
@@ -192,8 +192,8 @@ int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
f6b043
 	const u_char *iv;
f6b043
 	unsigned int len;
f6b043
 	EVP_CIPHER_CTX *ctx;
f6b043
-	unsigned int block_size, output_buffer_size;
f6b043
-	u_char *output_buffer;
f6b043
+	unsigned int block_size, buffer_size;
f6b043
+	u_char *input_buffer, *output_buffer;
f6b043
 
f6b043
 	/* initiator arg is any non-zero value */
f6b043
 	if(initiator) initiator=1;
f6b043
@@ -228,19 +228,41 @@ int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
f6b043
 		(*ndo->ndo_warning)(ndo, "espkey init failed");
f6b043
 	set_cipher_parameters(ctx, NULL, NULL, iv, 0);
f6b043
 	/*
f6b043
-	 * Allocate a buffer for the decrypted data.
f6b043
-	 * The output buffer must be separate from the input buffer, and
f6b043
-	 * its size must be a multiple of the cipher block size.
f6b043
+	 * Allocate buffers for the encrypted and decrypted data.
f6b043
+	 * Both buffers' sizes must be a multiple of the cipher block
f6b043
+	 * size, and the output buffer must be separate from the input
f6b043
+	 * buffer.
f6b043
 	 */
f6b043
 	block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
f6b043
-	output_buffer_size = len + (block_size - len % block_size);
f6b043
-	output_buffer = (u_char *)malloc(output_buffer_size);
f6b043
+	buffer_size = len + (block_size - len % block_size);
f6b043
+
f6b043
+	/*
f6b043
+	 * Attempt to allocate the input buffer.
f6b043
+	 */
f6b043
+	input_buffer = (u_char *)malloc(buffer_size);
f6b043
+	if (input_buffer == NULL) {
f6b043
+		(*ndo->ndo_warning)(ndo, "can't allocate memory for encrypted data buffer");
f6b043
+		EVP_CIPHER_CTX_free(ctx);
f6b043
+		return 0;
f6b043
+	}
f6b043
+	/*
f6b043
+	 * Copy the input data to the encrypted data buffer, and pad it
f6b043
+	 * with zeroes.
f6b043
+	 */
f6b043
+	memcpy(input_buffer, buf, len);
f6b043
+	memset(input_buffer + len, 0, buffer_size - len);
f6b043
+
f6b043
+	/*
f6b043
+	 * Attempt to allocate the output buffer.
f6b043
+	 */
f6b043
+	output_buffer = (u_char *)malloc(buffer_size);
f6b043
 	if (output_buffer == NULL) {
f6b043
 		(*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
f6b043
+		free(input_buffer);
f6b043
 		EVP_CIPHER_CTX_free(ctx);
f6b043
 		return 0;
f6b043
 	}
f6b043
-	EVP_Cipher(ctx, output_buffer, buf, len);
f6b043
+	EVP_Cipher(ctx, output_buffer, input_buffer, buffer_size);
f6b043
 	EVP_CIPHER_CTX_free(ctx);
f6b043
 
f6b043
 	/*
f6b043
@@ -249,6 +272,7 @@ int esp_print_decrypt_buffer_by_ikev2(netdissect_options *ndo,
f6b043
 	 * but changing this would require a more complicated fix.
f6b043
 	 */
f6b043
 	memcpy(buf, output_buffer, len);
f6b043
+	free(input_buffer);
f6b043
 	free(output_buffer);
f6b043
 
f6b043
 	ndo->ndo_packetp = buf;
f6b043
@@ -666,8 +690,8 @@ esp_print(netdissect_options *ndo,
f6b043
 	const u_char *ivoff;
f6b043
 	const u_char *p;
f6b043
 	EVP_CIPHER_CTX *ctx;
f6b043
-	unsigned int block_size, output_buffer_size;
f6b043
-	u_char *output_buffer;
f6b043
+	unsigned int block_size, buffer_size;
f6b043
+	u_char *input_buffer, *output_buffer;
f6b043
 #endif
f6b043
 
f6b043
 	esp = (const struct newesp *)bp;
f6b043
@@ -784,21 +808,43 @@ esp_print(netdissect_options *ndo,
f6b043
 			len = ep - (p + ivlen);
f6b043
 
f6b043
 			/*
f6b043
-			 * Allocate a buffer for the decrypted data.
f6b043
-			 * The output buffer must be separate from the
f6b043
-			 * input buffer, and its size must be a multiple
f6b043
-			 * of the cipher block size.
f6b043
+			 * Allocate buffers for the encrypted and decrypted
f6b043
+			 * data.  Both buffers' sizes must be a multiple of
f6b043
+			 * the cipher block size, and the output buffer must
f6b043
+			 * be separate from the input buffer.
f6b043
 			 */
f6b043
 			block_size = (unsigned int)EVP_CIPHER_CTX_block_size(ctx);
f6b043
-			output_buffer_size = len + (block_size - len % block_size);
f6b043
-			output_buffer = (u_char *)malloc(output_buffer_size);
f6b043
+			buffer_size = len + (block_size - len % block_size);
f6b043
+
f6b043
+			/*
f6b043
+			 * Attempt to allocate the input buffer.
f6b043
+			 */
f6b043
+			input_buffer = (u_char *)malloc(buffer_size);
f6b043
+			if (input_buffer == NULL) {
f6b043
+				(*ndo->ndo_warning)(ndo, "can't allocate memory for encrypted data buffer");
f6b043
+				EVP_CIPHER_CTX_free(ctx);
f6b043
+				return 0;
f6b043
+			}
f6b043
+			/*
f6b043
+			 * Copy the input data to the encrypted data buffer,
f6b043
+			 * and pad it with zeroes.
f6b043
+			 */
f6b043
+			memcpy(input_buffer, p + ivlen, len);
f6b043
+			memset(input_buffer + len, 0, buffer_size - len);
f6b043
+
f6b043
+			/*
f6b043
+			 * Attempt to allocate the output buffer.
f6b043
+			 */
f6b043
+			output_buffer = (u_char *)malloc(buffer_size);
f6b043
 			if (output_buffer == NULL) {
f6b043
 				(*ndo->ndo_warning)(ndo, "can't allocate memory for decryption buffer");
f6b043
+				free(input_buffer);
f6b043
 				EVP_CIPHER_CTX_free(ctx);
f6b043
 				return -1;
f6b043
 			}
f6b043
 
f6b043
-			EVP_Cipher(ctx, output_buffer, p + ivlen, len);
f6b043
+			EVP_Cipher(ctx, output_buffer, input_buffer, len);
f6b043
+			free(input_buffer);
f6b043
 			EVP_CIPHER_CTX_free(ctx);
f6b043
 			/*
f6b043
 			 * XXX - of course this is wrong, because buf is a