// File: akw0088[P]openssl/aes_download_reverse.cpp
// Function: process_download(char*, char*, char*, int, unsigned char*, unsigned char*, unsigned int)
int process_download(char *file_name, char *remote_hash, char *response, int download_size, unsigned char *encrypted_key, unsigned char *prikey, unsigned int max_malloc_size)
{
	if (download_size == 0)
	{
		printf("Download failed\r\n");
		return 0;
	}

	printf("Download complete\r\n");
	printf("Got %d bytes file name %s remote md5sum %s\r\n", download_size, file_name, remote_hash);


	char new_filename[256] = {0};
	char strip_filename[256] = {0};

	StripChars(file_name, strip_filename, (char *)".\\/;:*?\"<>|");

	printf("Attempting to decrypt AES key with RSA\r\n");

	unsigned char decrypted[4098] = {0};

	RSA *rsa = createRSA(prikey, false);
	int decrypted_length = RSA_private_decrypt(256, encrypted_key, decrypted, rsa, RSA_PKCS1_PADDING);
	if (decrypted_length == -1)
	{
		char err[130];

		ERR_load_crypto_strings();
		ERR_error_string(ERR_get_error(), err);
		printf("ERROR: %s\n", err);
//		free((void *)response);
		return -1;
	}
	RSA_free(rsa);

//	printf("Decrypted Text: %s\n", decrypted);
//	printf("Decrypted Length: %d\n", decrypted_length);

	if (decrypted_length != 16 + 32 + 1)
	{
		printf("Unexpected AES key string size\r\n");
		//free((void *)response);
		return -1;
	}

	if (decrypted[16] != ' ')
	{
		printf("Unexpected AES key format: %s\r\n", decrypted);
		//free((void *)response);
		return -1;
	}



	unsigned char key[128] = {0};
	unsigned char iv[128] = {0};

	int ret = sscanf((char *)&decrypted[0], "%s %s", (char *)&iv[0], (char *)&key[0]);

	if (ret != 2)
	{
		printf("Unexpected AES key format: %s matched %d\r\n", decrypted, ret);
		//free((void *)response);
		return -1;
	}

	printf("Decrypted AES Key successfully\r\n");

	printf("Attempting to decrypt file with remote hash %s\r\n", remote_hash);
	aes_decrypt(key, iv, (unsigned char *)response, download_size, max_malloc_size, remote_hash, strip_filename);


	//free((void *)response);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x13f0, %rsp           # imm = 0x13F0
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x2b25
leaq 0x16b4(%rip), %rdi      # 0x41c6
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x2d5c
leaq 0x16ac(%rip), %rdi      # 0x41d8
movb $0x0, %al
callq 0x2050
movl -0x24(%rbp), %esi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0x16a7(%rip), %rdi      # 0x41ec
movb $0x0, %al
callq 0x2050
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2150
leaq -0x240(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2150
movq -0x10(%rbp), %rdi
leaq -0x240(%rbp), %rsi
leaq 0x1695(%rip), %rdx      # 0x4219
callq 0x2420
leaq 0x1695(%rip), %rdi      # 0x4225
movb $0x0, %al
callq 0x2050
leaq -0x1250(%rbp), %rdi
xorl %esi, %esi
movl $0x1002, %edx           # imm = 0x1002
callq 0x2150
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x2d70
movq %rax, -0x1258(%rbp)
movq -0x30(%rbp), %rsi
leaq -0x1250(%rbp), %rdx
movq -0x1258(%rbp), %rcx
movl $0x100, %edi            # imm = 0x100
movl $0x1, %r8d
callq 0x21c0
movl %eax, -0x125c(%rbp)
cmpl $-0x1, -0x125c(%rbp)
jne 0x2c30
movl $0x2, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x2130
callq 0x2280
movq %rax, %rdi
leaq -0x12e0(%rbp), %rsi
callq 0x20d0
leaq -0x12e0(%rbp), %rsi
leaq 0x1631(%rip), %rdi      # 0x424e
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d5c
movq -0x1258(%rbp), %rdi
callq 0x2220
cmpl $0x31, -0x125c(%rbp)
je 0x2c5f
leaq 0x160d(%rip), %rdi      # 0x4259
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d5c
movzbl -0x1240(%rbp), %eax
cmpl $0x20, %eax
je 0x2c8c
leaq -0x1250(%rbp), %rsi
leaq 0x1601(%rip), %rdi      # 0x427a
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d5c
leaq -0x1360(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2150
leaq -0x13e0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x2150
leaq -0x1250(%rbp), %rdi
leaq -0x13e0(%rbp), %rdx
leaq -0x1360(%rbp), %rcx
leaq 0x15cc(%rip), %rsi      # 0x429a
movb $0x0, %al
callq 0x21e0
movl %eax, -0x13e4(%rbp)
cmpl $0x2, -0x13e4(%rbp)
je 0x2d08
leaq -0x1250(%rbp), %rsi
movl -0x13e4(%rbp), %edx
leaq 0x15a8(%rip), %rdi      # 0x42a0
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d5c
leaq 0x15bc(%rip), %rdi      # 0x42cb
movb $0x0, %al
callq 0x2050
movq -0x18(%rbp), %rsi
leaq 0x15cb(%rip), %rdi      # 0x42ec
movb $0x0, %al
callq 0x2050
leaq -0x1360(%rbp), %rdi
leaq -0x13e0(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %r9
leaq -0x240(%rbp), %rax
leaq 0x10(%rbp), %r8
movq %rax, (%rsp)
callq 0x2f30
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x13f0, %rsp           # imm = 0x13F0
popq %rbp
retq
nopl (%rax,%rax)