// File: /workspace/github_cpp_makefile_star2_O3/luigix25[P]SSLP/client_files/client.cpp
// Function: initial_protocol(NetSocket&)
bool initial_protocol(NetSocket &server_socket){

	//read my certificate from file
	FILE* cacert_file = fopen(CERT_CLIENT_PATH, "r");
	if(!cacert_file){ cerr << "Error: cannot open file '" << CERT_CLIENT_PATH << "' (missing?)\n"; exit(1); }
	X509* client_cert = PEM_read_X509(cacert_file, NULL, NULL, NULL);
	fclose(cacert_file);
	if(!client_cert){ cerr << "Error: PEM_read_X509 returned NULL\n"; exit(1); }

	//serialize it

	unsigned char* cert_buf= NULL; 
	int cert_size = i2d_X509(client_cert, &cert_buf); 
	if(cert_size< 0) { /* handle error */ } 
	
	//send through the socket

	if(!server_socket.sendInt(cert_size)){
		X509_free(client_cert);
		OPENSSL_free(cert_buf);
		return false;
	} 								//chiedere perazzo


	if(!server_socket.sendData((const char*)cert_buf,cert_size)){
		OPENSSL_free(cert_buf);
		X509_free(client_cert);
		return false;
	} 

	OPENSSL_free(cert_buf);
	X509_free(client_cert);

	//I receive the server's certificate

	if(!server_socket.recvInt(cert_size)) 		return false;

	if(cert_size < 0 || cert_size > MAX_CERT_LENGTH)
		return false;

	char *server_cert_data = server_socket.recvData(cert_size);
	if(server_cert_data == NULL)
		return false;

	char *old_ptr = server_cert_data;

	X509 *server_cert = d2i_X509(NULL,(const unsigned char **)&server_cert_data,cert_size);
	delete[] old_ptr;


	if(!server_cert)
		return false;


	CertificateManager cm(CERT_CA_PATH,CERT_CA_CRL_PATH);
	if(!cm.verifyCertificate(server_cert)){
		cout<<"[Error] verifying certificate"<<endl;
		X509_free(server_cert);
		return false;
	}

	string name;
	if(!cm.extractCommonName(server_cert,name)){
		X509_free(server_cert);
		cout<<"[Error] extractCommonName"<<endl;
		return false;
	}

	if(name != SERVER_NAME){
		X509_free(server_cert);
		return false;
	}

	public_key_rsa.setKey(cm.extractPubKey(server_cert));
	X509_free(server_cert);


	cout<<"Connection established with: "<<name<<endl;

	DHManager dh(DH_PARAMS_PATH);
	int pub_key_len;
	char *pub_key = dh.generatePublicKey(pub_key_len);
	if(pub_key == NULL){
		exit(-1);
	}

	char *opponent_pub_key;
	int opponent_pub_key_len;

	RSASignManager sign(CLIENT_PRIVKEY_PATH);


	if(!server_socket.recvInt(opponent_pub_key_len)) 	return false;

	if(!sign.RSAUpdate((const char*)&opponent_pub_key_len,sizeof(uint32_t)))			return false;


	//add check to int received
	opponent_pub_key = server_socket.recvData(opponent_pub_key_len);
	if(opponent_pub_key == NULL)
		return false;

	if(!sign.RSAUpdate((const char*)opponent_pub_key,opponent_pub_key_len))			return false;

	if(!sign.RSAUpdate((const char*)&pub_key_len,sizeof(uint32_t)))			return false;
	if(!server_socket.sendInt(pub_key_len)) 			return false;

	if(!sign.RSAUpdate((const char*)pub_key,pub_key_len))			return false;
	if(!server_socket.sendData(pub_key,pub_key_len)) 	return false;

	delete[] pub_key;

	int key_length;
	char *simmetric_key = dh.computeSimmetricKey(opponent_pub_key,opponent_pub_key_len,key_length);

	delete[] opponent_pub_key;



	HashManager keys;
	keys.HashUpdate(simmetric_key,key_length);

	memset_s(simmetric_key,0,key_length);
	delete[] simmetric_key;

	char* digest_keys = keys.HashFinal();

	char AES_symmetric_key[AES_KEY_SIZE];
	memcpy(AES_symmetric_key,digest_keys,AES_KEY_SIZE);

	char HMAC_key[HMAC_KEY_SIZE];
	memcpy(HMAC_key,&digest_keys[HMAC_KEY_SIZE],HMAC_KEY_SIZE);

	memset_s(digest_keys,0,HASH_SIZE);
	delete[] digest_keys;

	KeyManager::setAESKey(AES_symmetric_key);
	KeyManager::setHMACKey(HMAC_key);

	memset_s(AES_symmetric_key,0,AES_KEY_SIZE);
	memset_s(HMAC_key,0,HMAC_KEY_SIZE);

	char *revc_IV = server_socket.recvData(AES_BLOCK);
	if(revc_IV == NULL){
		//delete robbba
		return false;
	}
	if(!sign.RSAUpdate((const char*)revc_IV,AES_BLOCK))			return false;



	KeyManager::setAESIV(revc_IV);
	delete[] revc_IV;

	RAND_poll();

	uint32_t local_nonce = 0;
	RAND_bytes((unsigned char*)&local_nonce,sizeof(uint32_t));

	Chunk c;
	EncryptedChunk ec;

	int remote_nonce_size; 
	if(!server_socket.recvInt(remote_nonce_size)) return false;

	if(!sign.RSAUpdate((const char*)&remote_nonce_size,sizeof(uint32_t)))			return false;


	char *recv_data = server_socket.recvData(remote_nonce_size);

	if(recv_data == NULL){
		//delete robba
		return false;
	}

	ec.setCipherText(recv_data);
	ec.size = remote_nonce_size;

	DecryptManager dm;
	if(!dm.DecryptUpdate(c,ec))			return false;
	if(!dm.DecryptFinal(c))				return false;

	int remote_nonce = c.getInt();

	if(!sign.RSAUpdate((const char*)&remote_nonce,sizeof(uint32_t)))			return false;

	//delete[] c.plaintext;
	//delete[] ec.ciphertext;

 	c.setInt(local_nonce);
	c.size = sizeof(uint32_t);

	EncryptManager em;
	if(!em.EncryptUpdate(ec,c)){
		return false;
	}

	if(!em.EncryptFinal(ec)) return false;

	if(!server_socket.sendInt(ec.size)) return false;
	
	if(!sign.RSAUpdate((const char*)&ec.size,sizeof(uint32_t)))			return false;
	if(!sign.RSAUpdate((const char*)&local_nonce,sizeof(uint32_t)))			return false;


	if(!server_socket.sendData(ec.getCipherText(),ec.size)){
		//delete robba
		return false;
	}

	uint32_t sign_final_len;
	char* sign_final = sign.RSAFinal(sign_final_len);
	if(sign_final == NULL) return false;

	HMACManager::setLocalNonce(local_nonce);
	HMACManager::setRemoteNonce(remote_nonce);

	if(!sendDataHMAC(server_socket,sign_final,sign_final_len))			return false;

	int32_t received_sign_len;
	char *received_sign = recvDataHMAC(server_socket,received_sign_len);
	if(received_sign == NULL)
		return false;

	RSAVerifyManager verify(public_key_rsa);
	if(!verify.RSAUpdate(sign_final,sign_final_len)){
		delete[] sign_final;
		return false;
	}

	int result = verify.RSAFinal(received_sign);

	delete[] received_sign;

	delete[] sign_final;

	return result;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rdi, %rbx
leaq 0x5593(%rip), %rdi      # 0xb22a
leaq 0x568b(%rip), %rsi      # 0xb329
callq 0x4510
testq %rax, %rax
je 0x6489
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4610
movq %rax, %r14
movq %r15, %rdi
callq 0x4430
testq %r14, %r14
je 0x64b7
leaq 0x60(%rsp), %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4340
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x6b5c
testb %al, %al
je 0x5e2f
movq 0x60(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x6c6c
movq 0x60(%rsp), %rdi
leaq 0x557f(%rip), %rsi      # 0xb297
testb %al, %al
je 0x5e4f
movl $0x125, %edx            # imm = 0x125
callq 0x42d0
movq %r14, %rdi
callq 0x4210
leaq 0x14(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6ca4
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5e61
movq %rbx, %rdi
callq 0x6c72
movq %rax, 0xd8(%rsp)
testq %rax, %rax
je 0x5e61
movq %rax, %r12
movslq 0x14(%rsp), %rdx
xorl %r14d, %r14d
leaq 0xd8(%rsp), %rsi
xorl %edi, %edi
callq 0x40b0
movq %rax, %r15
movq %r12, %rdi
callq 0x45c0
testq %r15, %r15
je 0x5e64
leaq 0x5508(%rip), %rsi      # 0xb2af
leaq 0x551e(%rip), %rdx      # 0xb2cc
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x9680
movq %r14, %rdi
movq %r15, %rsi
callq 0x98be
testb %al, %al
je 0x5e77
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x40(%rsp), %rdi
leaq 0x88(%rsp), %rdx
movq %r15, %rsi
callq 0x9a72
testb %al, %al
je 0x5eca
leaq 0x5513(%rip), %rsi      # 0xb320
leaq 0x88(%rsp), %rdi
callq 0x4120
testl %eax, %eax
je 0x5f25
movq %r15, %rdi
callq 0x4210
jmp 0x5ef6
movq %r14, %rdi
callq 0x4210
movq 0x60(%rsp), %rdi
leaq 0x5454(%rip), %rsi      # 0xb297
movl $0x11a, %edx            # imm = 0x11A
callq 0x42d0
jmp 0x5e61
movl $0x120, %edx            # imm = 0x120
callq 0x42d0
movq %r14, %rdi
callq 0x4210
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0xa142(%rip), %rbx      # 0xffc0
leaq 0x5463(%rip), %rsi      # 0xb2e8
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x44e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0xa110(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
movq %r15, %rdi
callq 0x4210
xorl %r14d, %r14d
jmp 0x5f16
movq %r15, %rdi
callq 0x4210
movq 0xa0e7(%rip), %rdi      # 0xffc0
leaq 0x5426(%rip), %rsi      # 0xb306
movl $0x19, %edx
callq 0x44e0
movq 0xa0cf(%rip), %rdi      # 0xffc0
callq 0x41e0
xorl %r14d, %r14d
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x5f16
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x4450
leaq 0x40(%rsp), %rdi
callq 0x98aa
jmp 0x5e64
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x9b40
leaq 0xa61f(%rip), %rdi      # 0x10558
movq %rax, %rsi
callq 0xa278
movq %r15, %rdi
callq 0x4210
movq 0xa070(%rip), %rdi      # 0xffc0
leaq 0x53d4(%rip), %rsi      # 0xb32b
movl $0x1d, %edx
callq 0x44e0
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0xa048(%rip), %rdi      # 0xffc0
callq 0x44e0
movq %rax, %rdi
callq 0x41e0
leaq 0x53bd(%rip), %rsi      # 0xb349
leaq 0x38(%rsp), %rdi
callq 0x9ee0
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9ffe
movq %rax, %r14
testq %rax, %rax
je 0x64e3
leaq 0x51f8(%rip), %rsi      # 0xb1b0
leaq 0xa8(%rsp), %rdi
callq 0x86d0
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x6ca4
testb %al, %al
je 0x6433
leaq 0xa8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x6433
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x6c72
movq %rax, %r15
testq %rax, %rax
je 0x6433
movl 0x4(%rsp), %edx
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x8796
testb %al, %al
je 0x6433
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x6433
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x6b5c
testb %al, %al
je 0x6433
movl 0x8(%rsp), %edx
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x8796
testb %al, %al
je 0x6433
movl 0x8(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6c6c
testb %al, %al
je 0x6433
movq %r14, %rdi
callq 0x45c0
movl 0x4(%rsp), %edx
leaq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rcx
movq %r15, %rsi
callq 0xa0ba
movq %rax, %r14
movq %r15, %rdi
callq 0x45c0
leaq 0x30(%rsp), %rdi
callq 0xa190
movl 0x1c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0xa1d2
movl 0x1c(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x7398
testq %r14, %r14
je 0x60f2
movq %r14, %rdi
callq 0x45c0
leaq 0x30(%rsp), %rdi
callq 0xa1fa
movq %rax, %r14
movups (%rax), %xmm0
movaps %xmm0, 0x110(%rsp)
movups 0x10(%rax), %xmm0
movaps %xmm0, 0x100(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x7398
movq %r14, %rdi
callq 0x45c0
leaq 0x110(%rsp), %rdi
callq 0xa130
leaq 0x100(%rsp), %rdi
callq 0xa148
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x7398
leaq 0x100(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x7398
movq %rbx, %rdi
movl $0x10, %esi
callq 0x6c72
movq %rax, %r14
testq %rax, %rax
je 0x6452
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x8796
testb %al, %al
je 0x6452
movq %r14, %rdi
callq 0xa13c
movq %r14, %rdi
callq 0x45c0
callq 0x4680
leaq 0x10(%rsp), %rdi
movl $0x0, (%rdi)
movl $0x4, %esi
callq 0x46a0
leaq 0x70(%rsp), %rdi
callq 0xa2d0
leaq 0x48(%rsp), %rdi
callq 0xa3c2
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x6ca4
testb %al, %al
je 0x6457
leaq 0xa8(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x6457
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x6c72
testq %rax, %rax
je 0x6457
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0xa410
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x809c
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x813a
testb %al, %al
je 0x647a
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x8258
testb %al, %al
je 0x647a
leaq 0x70(%rsp), %rdi
callq 0xa3ba
leaq 0x6c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0xa8(%rsp), %rdi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x647a
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa38c
movl $0x4, 0x80(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x7eb4
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x7fb2
testb %al, %al
je 0x64d4
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x8030
testb %al, %al
je 0x64d4
movl 0x58(%rsp), %esi
movq %rbx, %rdi
callq 0x6b5c
testb %al, %al
je 0x64d4
leaq 0x58(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x64d4
leaq 0xa8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x8796
testb %al, %al
je 0x64d4
leaq 0x48(%rsp), %rdi
callq 0xa43e
movl 0x58(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6c6c
testb %al, %al
je 0x64d4
leaq 0xa8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x880e
movq %rax, %r14
testq %rax, %rax
je 0x64d4
movl 0x10(%rsp), %edi
callq 0x847a
movl 0x6c(%rsp), %edi
callq 0x848a
movl 0x18(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71a2
testb %al, %al
je 0x64d4
leaq 0xe4(%rsp), %rsi
movq %rbx, %rdi
callq 0x7250
movq %rax, %rbx
testq %rax, %rax
je 0x64d4
leaq 0xa17b(%rip), %rsi      # 0x10558
leaq 0xe8(%rsp), %rdi
callq 0x8570
movl 0x18(%rsp), %edx
leaq 0xe8(%rsp), %rdi
movq %r14, %rsi
callq 0x8614
testb %al, %al
je 0x64ed
leaq 0xe8(%rsp), %rdi
movq %rbx, %rsi
callq 0x867e
movl %eax, %ebp
movq %rbx, %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
testl %ebp, %ebp
setne %r14b
jmp 0x64f8
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rdi
callq 0x897c
leaq 0x38(%rsp), %rdi
callq 0xa11a
jmp 0x5ef9
xorl %r14d, %r14d
jmp 0x646e
xorl %r14d, %r14d
leaq 0x48(%rsp), %rdi
callq 0xa3d6
leaq 0x70(%rsp), %rdi
callq 0xa2e4
leaq 0x30(%rsp), %rdi
callq 0xa258
jmp 0x6436
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x8126
jmp 0x645a
movq 0x9b58(%rip), %rdi      # 0xffe8
leaq 0x4db4(%rip), %rsi      # 0xb24b
callq 0x4420
leaq 0x4d87(%rip), %rsi      # 0xb22a
movq %rax, %rdi
callq 0x4420
leaq 0x4db3(%rip), %rsi      # 0xb265
movq %rax, %rdi
jmp 0x64c5
movq 0x9b2a(%rip), %rdi      # 0xffe8
leaq 0x4dae(%rip), %rsi      # 0xb273
callq 0x4420
movl $0x1, %edi
callq 0x4560
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7f3e
jmp 0x647d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4560
movq %r14, %rdi
callq 0x45c0
xorl %r14d, %r14d
leaq 0xe8(%rsp), %rdi
callq 0x86aa
jmp 0x64d7
jmp 0x6509
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x86aa
jmp 0x6526
jmp 0x6523
jmp 0x6523
jmp 0x6523
jmp 0x6536
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7f3e
jmp 0x6539
jmp 0x6536
jmp 0x6550
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8126
jmp 0x6553
jmp 0x6550
movq %rax, %rbx
jmp 0x655d
jmp 0x6573
jmp 0x6573
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xa3d6
leaq 0x70(%rsp), %rdi
callq 0xa2e4
jmp 0x6576
jmp 0x6573
jmp 0x658b
jmp 0x658b
jmp 0x6573
jmp 0x6573
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa258
jmp 0x658e
jmp 0x6584
movq %rax, %rbx
jmp 0x659b
jmp 0x65a7
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x897c
leaq 0x38(%rsp), %rdi
callq 0xa11a
jmp 0x65aa
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x65cc
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x4450
jmp 0x65cc
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x98aa
movq %rbx, %rdi
callq 0x47d0