// File: /workspace/github_cpp_makefile_star2_O3/luigix25[P]SSLP/server_files/server.cpp
// Function: initial_protocol(NetSocket&)
bool initial_protocol(NetSocket &client_socket){

	int cert_size;

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

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

	char *client_cert_data = client_socket.recvData(cert_size);
	if(client_cert_data == NULL)
		return false;

	char *old_ptr = client_cert_data;

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

	if(!client_cert)
		return false;

	CertificateManager cm(CERT_CA_PATH,CERT_CA_CRL_PATH);
	if(!cm.verifyCertificate(client_cert)){
		X509_free(client_cert);
		return false;
	}

	string name;

	if(!cm.extractCommonName(client_cert,name)){
		X509_free(client_cert);
		return false;
	}


	public_key_rsa.setKey(cm.extractPubKey(client_cert));

	if(!isClientAuthorized(name,CLIENTS_LIST)){
		cout<<"Client not authorized"<<endl;
		X509_free(client_cert);
		return false;
	}

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

	X509_free(client_cert);

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

	//serialize it

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

	if(!client_socket.sendInt(cert_size)){
		delete[] cert_buf;
		return false;					
	}

	if(!client_socket.sendData((const char*)cert_buf,cert_size)){
		delete[] cert_buf;
		return false;
	}

	OPENSSL_free(cert_buf);
	X509_free(server_cert);

	RSAVerifyManager verify(public_key_rsa);

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

	if(!verify.RSAUpdate((const char*)&len,sizeof(int32_t)))		return false; //check

	if(!client_socket.sendInt(len)){
		delete[] pub_key;
		return false;
	}

	if(!verify.RSAUpdate(pub_key,len))		return false; //check

	if(!client_socket.sendData(pub_key,len)){
		delete[] pub_key; 	
		return false;
	}

	delete[] pub_key;
		
	char *opponent_pub_key;
	int opponent_pub_key_len;


	if(!client_socket.recvInt(opponent_pub_key_len))	return false;			//add check to int received
	if(!verify.RSAUpdate((const char*)&opponent_pub_key_len,sizeof(int32_t)))		return false; //check

	opponent_pub_key = client_socket.recvData(opponent_pub_key_len);
	if(!verify.RSAUpdate(opponent_pub_key,opponent_pub_key_len))		return false; //check


	if(opponent_pub_key == NULL)
		return false;


	int key_length;

	char *simmetric_key = dh.computeSimmetricKey(opponent_pub_key,opponent_pub_key_len,key_length);
	delete[] opponent_pub_key;

	if(simmetric_key == NULL){
		return false;
	}

  	HashManager keys;
  	if(!keys.HashUpdate(simmetric_key,key_length)){
  		memset_s(simmetric_key,0,key_length);
  		delete[] simmetric_key;
  		return false;
  	}

	char* digest_keys = keys.HashFinal();

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

	if(digest_keys == NULL)								return false;


	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;

	char IV[AES_BLOCK];

	RAND_poll();
	RAND_bytes((unsigned char*)&IV,AES_BLOCK);

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


	KeyManager::setAESKey(AES_symmetric_key);
	KeyManager::setAESIV(IV);

	KeyManager::setHMACKey(HMAC_key);

	if(!verify.RSAUpdate(IV,AES_BLOCK))		return false; //check

	if(!client_socket.sendData(IV,AES_BLOCK))	return false;

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


	EncryptedChunk ec;

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

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

	if(!verify.RSAUpdate((const char*)&ec.size,sizeof(int32_t)))		return false; //check

	if(!client_socket.sendInt(ec.size)){
		return false;
	} 

	if(!verify.RSAUpdate((const char*)&local_nonce,sizeof(int32_t)))		return false; //check


	if(!client_socket.sendData(ec.getCipherText(),ec.size)){
		return false;
	}


	int nonce_cipher_size;

	if(!client_socket.recvInt(nonce_cipher_size)) return false;
	if(!verify.RSAUpdate((const char*)&nonce_cipher_size,sizeof(int32_t)))		return false; //check

	char *recv_data = client_socket.recvData(nonce_cipher_size);
	if(recv_data == NULL)	return false;

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

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

	if(!dm.DecryptFinal(c)){				
		return false;
	}

	int remote_nonce = c.getInt();
	if(!verify.RSAUpdate((const char*)&remote_nonce,sizeof(int32_t)))		return false; //check

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

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


	int received_signature_len;
	char *received_signature = recvDataHMAC(client_socket,received_signature_len);

	int value = verify.RSAFinal(received_signature);

	RSASignManager sign(SERVER_PRIVKEY_PATH);
	if(!sign.RSAUpdate(received_signature,received_signature_len)){
		delete[] received_signature;
		return false;
	}

	delete[] received_signature;


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

	if(!sendDataHMAC(client_socket,sign_final,sign_final_len)){
		delete[] sign_final;
		return false;
	}

	delete[] sign_final;

    return value;


	//mancano free
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x130, %rsp            # imm = 0x130
movq %rdi, %rbx
leaq 0x14(%rsp), %r14
movq %r14, %rsi
callq 0x6974
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5f8a
movq %rbx, %rdi
callq 0x6942
movq %rax, 0xd8(%rsp)
testq %rax, %rax
je 0x5f8a
movq %rax, %r12
movslq 0x14(%rsp), %rdx
xorl %r14d, %r14d
leaq 0xd8(%rsp), %rsi
xorl %edi, %edi
callq 0x40e0
movq %rax, %r15
movq %r12, %rdi
callq 0x4610
testq %r15, %r15
je 0x6017
leaq 0x57f5(%rip), %rsi      # 0xb118
leaq 0x580b(%rip), %rdx      # 0xb135
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x93a8
movq %r14, %rdi
movq %r15, %rsi
callq 0x95e6
testb %al, %al
je 0x5f92
leaq 0xb0(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x38(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
movq %r15, %rsi
callq 0x979a
testb %al, %al
je 0x5fe5
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x9868
leaq 0xabfa(%rip), %rdi      # 0x10590
movq %rax, %rsi
callq 0x9fa0
leaq 0x57ac(%rip), %rsi      # 0xb151
leaq 0xa0(%rsp), %rdi
callq 0x4dd5
testb %al, %al
je 0x5f9f
movq 0xa60f(%rip), %rdi      # 0xffd0
leaq 0x57b8(%rip), %rsi      # 0xb180
movl $0x1d, %edx
callq 0x4530
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa5e7(%rip), %rdi      # 0xffd0
callq 0x4530
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4440
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4330
movq %r15, %rdi
callq 0x4260
leaq 0x5777(%rip), %rdi      # 0xb19e
leaq 0x5bee(%rip), %rsi      # 0xb61c
callq 0x4560
testq %rax, %rax
je 0x60ac
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4660
movq %rax, %r14
movq %r15, %rdi
callq 0x4460
testq %r14, %r14
je 0x60f1
leaq 0x98(%rsp), %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4380
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x682c
movq 0x98(%rsp), %rsi
testb %al, %al
je 0x602a
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x693c
movq 0x98(%rsp), %rdi
testb %al, %al
je 0x6034
leaq 0x5752(%rip), %rsi      # 0xb20b
movl $0x13c, %edx            # imm = 0x13C
callq 0x4310
movq %r14, %rdi
callq 0x4260
leaq 0xaabe(%rip), %rsi      # 0x10590
leaq 0x40(%rsp), %rdi
callq 0x8298
leaq 0x5740(%rip), %rsi      # 0xb223
leaq 0x30(%rsp), %rdi
callq 0x9c08
leaq 0x30(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x9d26
movq %rax, %r14
testq %rax, %rax
je 0x610e
leaq 0x40(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6048
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x682c
testb %al, %al
je 0x6040
movl 0x4(%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x833c
testb %al, %al
je 0x6048
movl 0x4(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x693c
movl %eax, %ebp
movq %r14, %rdi
callq 0x4610
testb %bpl, %bpl
je 0x6048
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x6974
testb %al, %al
je 0x6048
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6048
movl (%rsp), %esi
movq %rbx, %rdi
callq 0x6942
movq %rax, %r15
movl (%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x833c
testq %r15, %r15
sete %cl
xorb $0x1, %al
orb %cl, %al
jne 0x6048
movl (%rsp), %edx
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x9de2
movq %rax, %r14
movq %r15, %rdi
callq 0x4610
testq %r14, %r14
je 0x6048
leaq 0x28(%rsp), %rdi
callq 0x9eb8
movl 0x10(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x9efa
testb %al, %al
je 0x6061
leaq 0x28(%rsp), %rdi
callq 0x9f22
movq %rax, %r15
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x7068
movq %r14, %rdi
callq 0x4610
testq %r15, %r15
je 0x6077
movups (%r15), %xmm0
movaps %xmm0, 0xf0(%rsp)
movups 0x10(%r15), %xmm0
movaps %xmm0, 0xe0(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x7068
movq %r15, %rdi
callq 0x4610
callq 0x46d0
leaq 0xc0(%rsp), %rdi
movl $0x10, %esi
callq 0x46f0
leaq 0xc(%rsp), %rdi
movl $0x0, (%rdi)
movl $0x4, %esi
callq 0x46f0
leaq 0xf0(%rsp), %rdi
callq 0x9e58
leaq 0xc0(%rsp), %rdi
callq 0x9e64
leaq 0xe0(%rsp), %rdi
callq 0x9e70
leaq 0x40(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x10, %edx
callq 0x833c
testb %al, %al
je 0x6077
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
movl $0x10, %edx
callq 0x693c
testb %al, %al
je 0x6077
leaq 0x80(%rsp), %rdi
callq 0x9ff8
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0xa0b4
movl $0x4, 0x90(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xa0ea
leaq 0x20(%rsp), %rdi
callq 0x7bdc
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x7cda
testb %al, %al
je 0x6086
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7d58
testb %al, %al
je 0x6086
leaq 0x68(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6086
movl 0x68(%rsp), %esi
movq %rbx, %rdi
callq 0x682c
testb %al, %al
je 0x6086
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6086
leaq 0x58(%rsp), %rdi
callq 0xa166
movl 0x68(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x693c
testb %al, %al
je 0x6086
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6974
testb %al, %al
je 0x6086
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6086
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x6942
testq %rax, %rax
je 0x6086
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0xa138
movl 0x8(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x7dc4
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x7e62
testb %al, %al
je 0x6118
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x7f80
testb %al, %al
je 0x6118
leaq 0x80(%rsp), %rdi
callq 0xa0e2
leaq 0x7c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x40(%rsp), %rdi
movl $0x4, %edx
callq 0x833c
testb %al, %al
je 0x6118
movl 0x7c(%rsp), %edi
callq 0x81b2
movl 0xc(%rsp), %edi
callq 0x81a2
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x7068
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x7068
leaq 0x78(%rsp), %rsi
movq %rbx, %rdi
callq 0x6f20
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x83a6
movl %eax, %ebp
leaq 0x5162(%rip), %rsi      # 0xb071
leaq 0x100(%rsp), %rdi
callq 0x83f8
movl 0x78(%rsp), %edx
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x84be
testb %al, %al
je 0x612a
testq %r14, %r14
je 0x5f45
movq %r14, %rdi
callq 0x4610
leaq 0x100(%rsp), %rdi
leaq 0x74(%rsp), %rsi
callq 0x8536
movq %rax, %r15
testq %rax, %rax
je 0x6137
movl 0x74(%rsp), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6e72
movl %eax, %r14d
movq %r15, %rdi
callq 0x4610
testl %ebp, %ebp
setne %al
andb %al, %r14b
jmp 0x613a
xorl %r14d, %r14d
jmp 0x6017
movq %r15, %rdi
callq 0x4260
xorl %r14d, %r14d
jmp 0x600d
movq 0xa02a(%rip), %rbx      # 0xffd0
leaq 0x51bd(%rip), %rsi      # 0xb16a
movl $0x15, %edx
movq %rbx, %rdi
callq 0x4530
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4440
movsbl %al, %esi
movq 0x9ff8(%rip), %rdi      # 0xffd0
callq 0x4040
movq %rax, %rdi
callq 0x4330
movq %r15, %rdi
callq 0x4260
xorl %r14d, %r14d
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x600d
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x95d2
movl %r14d, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
je 0x5fed
movq %rsi, %rdi
jmp 0x6039
testq %rdi, %rdi
je 0x5fed
callq 0x4610
jmp 0x5fed
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x30(%rsp), %rdi
callq 0x9e42
leaq 0x40(%rsp), %rdi
callq 0x83d2
jmp 0x5ff0
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x7068
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x9f80
jmp 0x604b
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7c66
leaq 0x58(%rsp), %rdi
callq 0xa0fe
leaq 0x80(%rsp), %rdi
callq 0xa00c
jmp 0x607a
movq 0x9f3d(%rip), %rdi      # 0xfff0
leaq 0x5105(%rip), %rsi      # 0xb1bf
movl $0x19, %edx
callq 0x4530
movq 0x9f25(%rip), %rdi      # 0xfff0
leaq 0x50cc(%rip), %rsi      # 0xb19e
movl $0x20, %edx
callq 0x4530
movq 0x9f0d(%rip), %rdi      # 0xfff0
leaq 0x50ef(%rip), %rsi      # 0xb1d9
callq 0x4450
jmp 0x6104
movq 0x9ef8(%rip), %rdi      # 0xfff0
leaq 0x50e8(%rip), %rsi      # 0xb1e7
callq 0x4450
movl $0x1, %edi
callq 0x45b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x45b0
xorl %r14d, %r14d
leaq 0x18(%rsp), %rdi
callq 0x7e4e
jmp 0x6089
testq %r14, %r14
je 0x6137
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x100(%rsp), %rdi
callq 0x86a4
jmp 0x611b
jmp 0x614b
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x86a4
jmp 0x616a
jmp 0x6167
jmp 0x6167
jmp 0x6167
jmp 0x6167
jmp 0x6188
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x7e4e
jmp 0x618b
jmp 0x6188
jmp 0x6188
movq %rax, %rbx
jmp 0x6195
jmp 0x6181
movq %rax, %rbx
jmp 0x619f
jmp 0x61ba
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7c66
leaq 0x58(%rsp), %rdi
callq 0xa0fe
leaq 0x80(%rsp), %rdi
callq 0xa00c
jmp 0x61bd
jmp 0x61ba
jmp 0x61ba
jmp 0x61ba
jmp 0x61d2
jmp 0x61ba
jmp 0x61d2
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x9f80
jmp 0x61d5
jmp 0x61d2
movq %rax, %rbx
jmp 0x61df
jmp 0x61f6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x9e42
leaq 0x40(%rsp), %rdi
callq 0x83d2
jmp 0x61f9
jmp 0x61f6
jmp 0x61f6
jmp 0x61f6
movq %rax, %rbx
jmp 0x6216
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x6216
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x95d2
movq %rbx, %rdi
callq 0x4830