// File: /workspace/github_cpp_makefile_star2_O1/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 0x559a(%rip), %rdi      # 0xb22a
leaq 0x5692(%rip), %rsi      # 0xb329
callq 0x4510
testq %rax, %rax
je 0x6487
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 0x64b5
leaq 0x60(%rsp), %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4340
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x6b64
testb %al, %al
je 0x5e28
movq 0x60(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x6c7e
movq 0x60(%rsp), %rdi
leaq 0x5586(%rip), %rsi      # 0xb297
testb %al, %al
je 0x5e48
movl $0x125, %edx            # imm = 0x125
callq 0x42d0
movq %r14, %rdi
callq 0x4210
leaq 0x14(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cb6
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5e5a
movq %rbx, %rdi
callq 0x6c84
movq %rax, 0xd8(%rsp)
testq %rax, %rax
je 0x5e5a
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 0x5e5d
leaq 0x550f(%rip), %rsi      # 0xb2af
leaq 0x5525(%rip), %rdx      # 0xb2cc
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x9718
movq %r14, %rdi
movq %r15, %rsi
callq 0x9956
testb %al, %al
je 0x5e70
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 0x9b0c
testb %al, %al
je 0x5ec3
leaq 0x551a(%rip), %rsi      # 0xb320
leaq 0x88(%rsp), %rdi
callq 0x4120
testl %eax, %eax
je 0x5f1e
movq %r15, %rdi
callq 0x4210
jmp 0x5eef
movq %r14, %rdi
callq 0x4210
movq 0x60(%rsp), %rdi
leaq 0x545b(%rip), %rsi      # 0xb297
movl $0x11a, %edx            # imm = 0x11A
callq 0x42d0
jmp 0x5e5a
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 0xa149(%rip), %rbx      # 0xffc0
leaq 0x546a(%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 0xa117(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
movq %r15, %rdi
callq 0x4210
xorl %r14d, %r14d
jmp 0x5f0f
movq %r15, %rdi
callq 0x4210
movq 0xa0ee(%rip), %rdi      # 0xffc0
leaq 0x542d(%rip), %rsi      # 0xb306
movl $0x19, %edx
callq 0x44e0
movq 0xa0d6(%rip), %rdi      # 0xffc0
callq 0x41e0
xorl %r14d, %r14d
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x5f0f
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x4450
leaq 0x40(%rsp), %rdi
callq 0x9942
jmp 0x5e5d
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x9bde
leaq 0xa626(%rip), %rdi      # 0x10558
movq %rax, %rsi
callq 0xa318
movq %r15, %rdi
callq 0x4210
movq 0xa077(%rip), %rdi      # 0xffc0
leaq 0x53db(%rip), %rsi      # 0xb32b
movl $0x1d, %edx
callq 0x44e0
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0xa04f(%rip), %rdi      # 0xffc0
callq 0x44e0
movq %rax, %rdi
callq 0x41e0
leaq 0x53c4(%rip), %rsi      # 0xb349
leaq 0x38(%rsp), %rdi
callq 0x9f80
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xa09e
movq %rax, %r14
testq %rax, %rax
je 0x64e1
leaq 0x51ff(%rip), %rsi      # 0xb1b0
leaq 0xa8(%rsp), %rdi
callq 0x8744
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x6cb6
testb %al, %al
je 0x6431
leaq 0xa8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x6431
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x6c84
movq %rax, %r15
testq %rax, %rax
je 0x6431
movl 0x4(%rsp), %edx
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x880a
testb %al, %al
je 0x6431
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x6431
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x6b64
testb %al, %al
je 0x6431
movl 0x8(%rsp), %edx
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x880a
testb %al, %al
je 0x6431
movl 0x8(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6c7e
testb %al, %al
je 0x6431
movq %r14, %rdi
callq 0x45c0
movl 0x4(%rsp), %edx
leaq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rcx
movq %r15, %rsi
callq 0xa15a
movq %rax, %r14
movq %r15, %rdi
callq 0x45c0
leaq 0x30(%rsp), %rdi
callq 0xa230
movl 0x1c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0xa272
movl 0x1c(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x73d4
testq %r14, %r14
je 0x60eb
movq %r14, %rdi
callq 0x45c0
leaq 0x30(%rsp), %rdi
callq 0xa29a
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 0x73d4
testq %r14, %r14
je 0x612b
movq %r14, %rdi
callq 0x45c0
leaq 0x110(%rsp), %rdi
callq 0xa1d0
leaq 0x100(%rsp), %rdi
callq 0xa1e8
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x73d4
leaq 0x100(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x73d4
movq %rbx, %rdi
movl $0x10, %esi
callq 0x6c84
movq %rax, %r14
testq %rax, %rax
je 0x6450
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x880a
testb %al, %al
je 0x6450
movq %r14, %rdi
callq 0xa1dc
movq %r14, %rdi
callq 0x45c0
callq 0x4680
leaq 0x10(%rsp), %rdi
movl $0x0, (%rdi)
movl $0x4, %esi
callq 0x46a0
leaq 0x70(%rsp), %rdi
callq 0xa378
leaq 0x48(%rsp), %rdi
callq 0xa48e
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x6cb6
testb %al, %al
je 0x6455
leaq 0xa8(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x6455
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x6c84
testq %rax, %rax
je 0x6455
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0xa4dc
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x8110
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x81ae
testb %al, %al
je 0x6478
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x82cc
testb %al, %al
je 0x6478
leaq 0x70(%rsp), %rdi
callq 0xa486
leaq 0x6c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0xa8(%rsp), %rdi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x6478
leaq 0x70(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa44c
movl $0x4, 0x80(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x7f28
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x8026
testb %al, %al
je 0x64d2
leaq 0x20(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x80a4
testb %al, %al
je 0x64d2
movl 0x58(%rsp), %esi
movq %rbx, %rdi
callq 0x6b64
testb %al, %al
je 0x64d2
leaq 0x58(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x64d2
leaq 0xa8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
callq 0x880a
testb %al, %al
je 0x64d2
leaq 0x48(%rsp), %rdi
callq 0xa516
movl 0x58(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6c7e
testb %al, %al
je 0x64d2
leaq 0xa8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8882
movq %rax, %r14
testq %rax, %rax
je 0x64d2
movl 0x10(%rsp), %edi
callq 0x84ee
movl 0x6c(%rsp), %edi
callq 0x84fe
movl 0x18(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71c2
testb %al, %al
je 0x64d2
leaq 0xe4(%rsp), %rsi
movq %rbx, %rdi
callq 0x7270
movq %rax, %rbx
testq %rax, %rax
je 0x64d2
leaq 0xa17d(%rip), %rsi      # 0x10558
leaq 0xe8(%rsp), %rdi
callq 0x85e4
movl 0x18(%rsp), %edx
leaq 0xe8(%rsp), %rdi
movq %r14, %rsi
callq 0x8688
testb %al, %al
je 0x64eb
leaq 0xe8(%rsp), %rdi
movq %rbx, %rsi
callq 0x86f2
movl %eax, %ebp
movq %rbx, %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
testl %ebp, %ebp
setne %r14b
jmp 0x64f6
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rdi
callq 0x89f0
leaq 0x38(%rsp), %rdi
callq 0xa1ba
jmp 0x5ef2
xorl %r14d, %r14d
jmp 0x646c
xorl %r14d, %r14d
leaq 0x48(%rsp), %rdi
callq 0xa4a2
leaq 0x70(%rsp), %rdi
callq 0xa38c
leaq 0x30(%rsp), %rdi
callq 0xa2f8
jmp 0x6434
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x819a
jmp 0x6458
movq 0x9b5a(%rip), %rdi      # 0xffe8
leaq 0x4db6(%rip), %rsi      # 0xb24b
callq 0x4420
leaq 0x4d89(%rip), %rsi      # 0xb22a
movq %rax, %rdi
callq 0x4420
leaq 0x4db5(%rip), %rsi      # 0xb265
movq %rax, %rdi
jmp 0x64c3
movq 0x9b2c(%rip), %rdi      # 0xffe8
leaq 0x4db0(%rip), %rsi      # 0xb273
callq 0x4420
movl $0x1, %edi
callq 0x4560
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7fb2
jmp 0x647b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4560
movq %r14, %rdi
callq 0x45c0
xorl %r14d, %r14d
leaq 0xe8(%rsp), %rdi
callq 0x871e
jmp 0x64d5
jmp 0x6507
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x871e
jmp 0x6524
jmp 0x6521
jmp 0x6521
jmp 0x6521
jmp 0x6534
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7fb2
jmp 0x6537
jmp 0x6534
jmp 0x654e
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x819a
jmp 0x6551
jmp 0x654e
movq %rax, %rbx
jmp 0x655b
jmp 0x6571
jmp 0x6571
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xa4a2
leaq 0x70(%rsp), %rdi
callq 0xa38c
jmp 0x6574
jmp 0x6571
jmp 0x6589
jmp 0x6589
jmp 0x6571
jmp 0x6571
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa2f8
jmp 0x658c
jmp 0x6582
movq %rax, %rbx
jmp 0x6599
jmp 0x65a5
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x89f0
leaq 0x38(%rsp), %rdi
callq 0xa1ba
jmp 0x65a8
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x65ca
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x4450
jmp 0x65ca
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x9942
movq %rbx, %rdi
callq 0x47d0