// File: /workspace/github_cpp_makefile_star2_O2/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 0x49fc(%rip), %rdi      # 0xa22a
leaq 0x4af4(%rip), %rsi      # 0xa329
callq 0x44f0
testq %rax, %rax
je 0x5fa9
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4600
movq %rax, %r14
movq %r15, %rdi
callq 0x4410
testq %r14, %r14
je 0x5fd7
leaq 0x60(%rsp), %rsi
andq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4330
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x6574
testb %al, %al
je 0x59bc
movq 0x60(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x6674
movq 0x60(%rsp), %rdi
leaq 0x49eb(%rip), %rsi      # 0xa297
testb %al, %al
je 0x59dc
movl $0x125, %edx            # imm = 0x125
callq 0x42c0
movq %r14, %rdi
callq 0x4200
leaq 0x14(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x66ac
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x59ee
movq %rbx, %rdi
callq 0x667a
movq %rax, 0xb8(%rsp)
testq %rax, %rax
je 0x59ee
movq %rax, %r12
movslq 0x14(%rsp), %rdx
xorl %r14d, %r14d
leaq 0xb8(%rsp), %rsi
xorl %edi, %edi
callq 0x40a0
movq %rax, %r15
movq %r12, %rdi
callq 0x45a0
testq %r15, %r15
je 0x59f1
leaq 0x4974(%rip), %rsi      # 0xa2af
leaq 0x498a(%rip), %rdx      # 0xa2cc
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x8870
movq %r14, %rdi
movq %r15, %rsi
callq 0x8aaa
testb %al, %al
je 0x5a04
leaq 0x110(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%rsp), %rdi
leaq 0x100(%rsp), %rdx
movq %r15, %rsi
callq 0x8c04
testb %al, %al
je 0x5a2f
leaq 0x4986(%rip), %rsi      # 0xa320
leaq 0x100(%rsp), %rdi
callq 0x6433
testb %al, %al
je 0x5a5a
movq %r15, %rdi
callq 0x4200
jmp 0x5a52
movq %r14, %rdi
callq 0x4200
movq 0x60(%rsp), %rdi
leaq 0x48c7(%rip), %rsi      # 0xa297
movl $0x11a, %edx            # imm = 0x11A
callq 0x42c0
jmp 0x59ee
movl $0x120, %edx            # imm = 0x120
callq 0x42c0
movq %r14, %rdi
callq 0x4200
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x95bd(%rip), %rdi      # 0xefc8
leaq 0x48d6(%rip), %rsi      # 0xa2e8
callq 0x4400
movq %rax, %rdi
callq 0x41d0
movq %r15, %rdi
callq 0x4200
xorl %r14d, %r14d
jmp 0x5f63
movq %r15, %rdi
callq 0x4200
movq 0x958a(%rip), %rdi      # 0xefc8
leaq 0x48c1(%rip), %rsi      # 0xa306
callq 0x4400
movq %rax, %rdi
callq 0x41d0
xorl %r14d, %r14d
jmp 0x5f56
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x8caa
leaq 0x9aea(%rip), %rdi      # 0xf558
movq %rax, %rsi
callq 0x935e
movq %r15, %rdi
callq 0x4200
movq 0x9543(%rip), %rdi      # 0xefc8
leaq 0x489f(%rip), %rsi      # 0xa32b
callq 0x4400
leaq 0x100(%rsp), %rsi
movq %rax, %rdi
callq 0x43d0
movq %rax, %rdi
callq 0x41d0
leaq 0x4899(%rip), %rsi      # 0xa349
leaq 0x38(%rsp), %rdi
callq 0x902c
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9112
movq %rax, %r14
testq %rax, %rax
je 0x6001
leaq 0x46d4(%rip), %rsi      # 0xa1b0
leaq 0x88(%rsp), %rdi
callq 0x7cd0
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x66ac
testb %al, %al
je 0x5f3c
leaq 0x88(%rsp), %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5f3c
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x667a
movq %rax, %r15
testq %rax, %rax
je 0x5f3c
movl 0x4(%rsp), %edx
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0x7d70
testb %al, %al
je 0x5f3c
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5f3c
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x6574
testb %al, %al
je 0x5f3c
movl 0x8(%rsp), %edx
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x7d70
testb %al, %al
je 0x5f3c
movl 0x8(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6674
testb %al, %al
je 0x5f3c
movq %r14, %rdi
callq 0x45a0
movl 0x4(%rsp), %edx
leaq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rcx
movq %r15, %rsi
callq 0x91a2
movq %rax, %r14
movq %r15, %rdi
callq 0x45a0
leaq 0x30(%rsp), %rdi
callq 0x9278
movl 0x1c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x92ba
movl 0x1c(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x6c30
testq %r14, %r14
je 0x5c12
movq %r14, %rdi
callq 0x45a0
leaq 0x30(%rsp), %rdi
callq 0x92e2
movq %rax, %r14
movups (%rax), %xmm0
movaps %xmm0, 0xf0(%rsp)
movups 0x10(%rax), %xmm0
movaps %xmm0, 0xe0(%rsp)
pushq $0x20
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x6c30
movq %r14, %rdi
callq 0x45a0
leaq 0xf0(%rsp), %rdi
callq 0x9218
leaq 0xe0(%rsp), %rdi
callq 0x9230
leaq 0xf0(%rsp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x6c30
leaq 0xe0(%rsp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x6c30
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x667a
movq %rax, %r14
testq %rax, %rax
je 0x5f72
leaq 0x88(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x7d70
testb %al, %al
je 0x5f72
movq %r14, %rdi
callq 0x9224
movq %r14, %rdi
callq 0x45a0
callq 0x4680
leaq 0x10(%rsp), %rdi
andl $0x0, (%rdi)
pushq $0x4
popq %rsi
callq 0x46a0
leaq 0x70(%rsp), %rdi
callq 0x93ac
leaq 0x48(%rsp), %rdi
callq 0x9450
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x66ac
testb %al, %al
je 0x5f77
leaq 0x88(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5f77
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x667a
testq %rax, %rax
je 0x5f77
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x9496
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x76a0
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x773e
testb %al, %al
je 0x5f9a
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x785c
testb %al, %al
je 0x5f9a
leaq 0x70(%rsp), %rdi
callq 0x9448
leaq 0x6c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x88(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5f9a
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x9428
movl $0x4, 0x80(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x74b8
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x75b6
testb %al, %al
je 0x5ff2
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x7634
testb %al, %al
je 0x5ff2
movl 0x58(%rsp), %esi
movq %rbx, %rdi
callq 0x6574
testb %al, %al
je 0x5ff2
leaq 0x58(%rsp), %rsi
leaq 0x88(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5ff2
leaq 0x88(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7d70
testb %al, %al
je 0x5ff2
leaq 0x48(%rsp), %rdi
callq 0x94b6
movl 0x58(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6674
testb %al, %al
je 0x5ff2
leaq 0x88(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7de8
movq %rax, %r14
testq %rax, %rax
je 0x5ff2
movl 0x10(%rsp), %edi
callq 0x7a7a
movl 0x6c(%rsp), %edi
callq 0x7a8a
movl 0x18(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a96
testb %al, %al
je 0x5ff2
leaq 0xc4(%rsp), %rsi
movq %rbx, %rdi
callq 0x6b42
movq %rax, %rbx
testq %rax, %rax
je 0x5ff2
leaq 0x9672(%rip), %rsi      # 0xf558
leaq 0xc8(%rsp), %rdi
callq 0x7b6e
movl 0x18(%rsp), %edx
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x7c12
testb %al, %al
je 0x6005
leaq 0xc8(%rsp), %rdi
movq %rbx, %rsi
callq 0x7c7c
movl %eax, %ebp
movq %rbx, %rdi
callq 0x45a0
movq %r14, %rdi
callq 0x45a0
testl %ebp, %ebp
setne %r14b
jmp 0x6010
xorl %r14d, %r14d
leaq 0x88(%rsp), %rdi
callq 0x7ef6
leaq 0x38(%rsp), %rdi
callq 0x9202
leaq 0x100(%rsp), %rdi
callq 0x41b0
leaq 0x40(%rsp), %rdi
callq 0x8a96
jmp 0x59f1
xorl %r14d, %r14d
jmp 0x5f8e
xorl %r14d, %r14d
leaq 0x48(%rsp), %rdi
callq 0x945e
leaq 0x70(%rsp), %rdi
callq 0x93ba
leaq 0x30(%rsp), %rdi
callq 0x933e
jmp 0x5f3f
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x772a
jmp 0x5f7a
movq 0x9040(%rip), %rdi      # 0xeff0
leaq 0x4294(%rip), %rsi      # 0xa24b
callq 0x4400
leaq 0x4267(%rip), %rsi      # 0xa22a
movq %rax, %rdi
callq 0x4400
leaq 0x4293(%rip), %rsi      # 0xa265
movq %rax, %rdi
jmp 0x5fe5
movq 0x9012(%rip), %rdi      # 0xeff0
leaq 0x428e(%rip), %rsi      # 0xa273
callq 0x4400
pushq $0x1
popq %rdi
callq 0x4540
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7542
jmp 0x5f9d
pushq $-0x1
jmp 0x5fec
movq %r14, %rdi
callq 0x45a0
xorl %r14d, %r14d
leaq 0xc8(%rsp), %rdi
callq 0x7ca8
jmp 0x5ff5
jmp 0x6021
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x7ca8
jmp 0x603e
jmp 0x603b
jmp 0x603b
jmp 0x603b
jmp 0x604e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7542
jmp 0x6051
jmp 0x604e
jmp 0x6068
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x772a
jmp 0x606b
jmp 0x6068
movq %rax, %rbx
jmp 0x6075
jmp 0x608b
jmp 0x608b
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x945e
leaq 0x70(%rsp), %rdi
callq 0x93ba
jmp 0x608e
jmp 0x608b
jmp 0x60a3
jmp 0x60a3
jmp 0x608b
jmp 0x608b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x933e
jmp 0x60a6
jmp 0x609c
movq %rax, %rbx
jmp 0x60b3
jmp 0x60bf
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x7ef6
leaq 0x38(%rsp), %rdi
callq 0x9202
jmp 0x60c2
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x41b0
jmp 0x60d4
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x8a96
movq %rbx, %rdi
callq 0x47a0