// File: /workspace/github_cpp_makefile_star2_O1/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 0x69aa
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5fa3
movq %rbx, %rdi
callq 0x6978
movq %rax, 0xd8(%rsp)
testq %rax, %rax
je 0x5fa3
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 0x6030
leaq 0x57dc(%rip), %rsi      # 0xb118
leaq 0x57f2(%rip), %rdx      # 0xb135
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x9464
movq %r14, %rdi
movq %r15, %rsi
callq 0x96a2
testb %al, %al
je 0x5fab
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 0x9858
testb %al, %al
je 0x5ffe
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x992a
leaq 0xabe1(%rip), %rdi      # 0x10590
movq %rax, %rsi
callq 0xa064
leaq 0x5793(%rip), %rsi      # 0xb151
leaq 0xa0(%rsp), %rdi
callq 0x4dd5
testb %al, %al
je 0x5fb8
movq 0xa5f6(%rip), %rdi      # 0xffd0
leaq 0x579f(%rip), %rsi      # 0xb180
movl $0x1d, %edx
callq 0x4530
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa5ce(%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 0x575e(%rip), %rdi      # 0xb19e
leaq 0x5bd5(%rip), %rsi      # 0xb61c
callq 0x4560
testq %rax, %rax
je 0x60c5
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 0x610a
leaq 0x98(%rsp), %rsi
movq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4380
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x6858
movq 0x98(%rsp), %rsi
testb %al, %al
je 0x6043
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x6972
movq 0x98(%rsp), %rdi
testb %al, %al
je 0x604d
leaq 0x5739(%rip), %rsi      # 0xb20b
movl $0x13c, %edx            # imm = 0x13C
callq 0x4310
movq %r14, %rdi
callq 0x4260
leaq 0xaaa5(%rip), %rsi      # 0x10590
leaq 0x40(%rsp), %rdi
callq 0x8330
leaq 0x5727(%rip), %rsi      # 0xb223
leaq 0x30(%rsp), %rdi
callq 0x9ccc
leaq 0x30(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x9dea
movq %rax, %r14
testq %rax, %rax
je 0x6127
leaq 0x40(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x6061
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x6858
testb %al, %al
je 0x6059
movl 0x4(%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x83d4
testb %al, %al
je 0x6061
movl 0x4(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6972
movl %eax, %ebp
movq %r14, %rdi
callq 0x4610
testb %bpl, %bpl
je 0x6061
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x69aa
testb %al, %al
je 0x6061
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x6061
movl (%rsp), %esi
movq %rbx, %rdi
callq 0x6978
movq %rax, %r15
movl (%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x83d4
testq %r15, %r15
sete %cl
xorb $0x1, %al
orb %cl, %al
jne 0x6061
movl (%rsp), %edx
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x9ea6
movq %rax, %r14
movq %r15, %rdi
callq 0x4610
testq %r14, %r14
je 0x6061
leaq 0x28(%rsp), %rdi
callq 0x9f7c
movl 0x10(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x9fbe
testb %al, %al
je 0x607a
leaq 0x28(%rsp), %rdi
callq 0x9fe6
movq %rax, %r15
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x70c8
movq %r14, %rdi
callq 0x4610
testq %r15, %r15
je 0x6090
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 0x70c8
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 0x9f1c
leaq 0xc0(%rsp), %rdi
callq 0x9f28
leaq 0xe0(%rsp), %rdi
callq 0x9f34
leaq 0x40(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x10, %edx
callq 0x83d4
testb %al, %al
je 0x6090
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
movl $0x10, %edx
callq 0x6972
testb %al, %al
je 0x6090
leaq 0x80(%rsp), %rdi
callq 0xa0c4
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0xa198
movl $0x4, 0x90(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xa1da
leaq 0x20(%rsp), %rdi
callq 0x7c74
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x7d72
testb %al, %al
je 0x609f
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7df0
testb %al, %al
je 0x609f
leaq 0x68(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x609f
movl 0x68(%rsp), %esi
movq %rbx, %rdi
callq 0x6858
testb %al, %al
je 0x609f
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x609f
leaq 0x58(%rsp), %rdi
callq 0xa262
movl 0x68(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6972
testb %al, %al
je 0x609f
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x69aa
testb %al, %al
je 0x609f
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x609f
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x6978
testq %rax, %rax
je 0x609f
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0xa228
movl 0x8(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x7e5c
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x7efa
testb %al, %al
je 0x6131
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x8018
testb %al, %al
je 0x6131
leaq 0x80(%rsp), %rdi
callq 0xa1d2
leaq 0x7c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x40(%rsp), %rdi
movl $0x4, %edx
callq 0x83d4
testb %al, %al
je 0x6131
movl 0x7c(%rsp), %edi
callq 0x824a
movl 0xc(%rsp), %edi
callq 0x823a
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x70c8
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x70c8
leaq 0x78(%rsp), %rsi
movq %rbx, %rdi
callq 0x6f64
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x843e
movl %eax, %ebp
leaq 0x5149(%rip), %rsi      # 0xb071
leaq 0x100(%rsp), %rdi
callq 0x8490
movl 0x78(%rsp), %edx
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x8556
testb %al, %al
je 0x6143
testq %r14, %r14
je 0x5f5e
movq %r14, %rdi
callq 0x4610
leaq 0x100(%rsp), %rdi
leaq 0x74(%rsp), %rsi
callq 0x85ce
movq %rax, %r15
testq %rax, %rax
je 0x6150
movl 0x74(%rsp), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6eb6
movl %eax, %r14d
movq %r15, %rdi
callq 0x4610
testl %ebp, %ebp
setne %al
andb %al, %r14b
jmp 0x6153
xorl %r14d, %r14d
jmp 0x6030
movq %r15, %rdi
callq 0x4260
xorl %r14d, %r14d
jmp 0x6026
movq 0xa011(%rip), %rbx      # 0xffd0
leaq 0x51a4(%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 0x9fdf(%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 0x6026
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x968e
movl %r14d, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
je 0x6006
movq %rsi, %rdi
jmp 0x6052
testq %rdi, %rdi
je 0x6006
callq 0x4610
jmp 0x6006
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x30(%rsp), %rdi
callq 0x9f06
leaq 0x40(%rsp), %rdi
callq 0x846a
jmp 0x6009
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x70c8
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0xa044
jmp 0x6064
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7cfe
leaq 0x58(%rsp), %rdi
callq 0xa1ee
leaq 0x80(%rsp), %rdi
callq 0xa0d8
jmp 0x6093
movq 0x9f24(%rip), %rdi      # 0xfff0
leaq 0x50ec(%rip), %rsi      # 0xb1bf
movl $0x19, %edx
callq 0x4530
movq 0x9f0c(%rip), %rdi      # 0xfff0
leaq 0x50b3(%rip), %rsi      # 0xb19e
movl $0x20, %edx
callq 0x4530
movq 0x9ef4(%rip), %rdi      # 0xfff0
leaq 0x50d6(%rip), %rsi      # 0xb1d9
callq 0x4450
jmp 0x611d
movq 0x9edf(%rip), %rdi      # 0xfff0
leaq 0x50cf(%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 0x7ee6
jmp 0x60a2
testq %r14, %r14
je 0x6150
movq %r14, %rdi
callq 0x4610
xorl %r14d, %r14d
leaq 0x100(%rsp), %rdi
callq 0x873c
jmp 0x6134
jmp 0x6164
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x873c
jmp 0x6183
jmp 0x6180
jmp 0x6180
jmp 0x6180
jmp 0x6180
jmp 0x61a1
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x7ee6
jmp 0x61a4
jmp 0x61a1
jmp 0x61a1
movq %rax, %rbx
jmp 0x61ae
jmp 0x619a
movq %rax, %rbx
jmp 0x61b8
jmp 0x61d3
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7cfe
leaq 0x58(%rsp), %rdi
callq 0xa1ee
leaq 0x80(%rsp), %rdi
callq 0xa0d8
jmp 0x61d6
jmp 0x61d3
jmp 0x61d3
jmp 0x61d3
jmp 0x61eb
jmp 0x61d3
jmp 0x61eb
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xa044
jmp 0x61ee
jmp 0x61eb
movq %rax, %rbx
jmp 0x61f8
jmp 0x620f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x9f06
leaq 0x40(%rsp), %rdi
callq 0x846a
jmp 0x6212
jmp 0x620f
jmp 0x620f
jmp 0x620f
movq %rax, %rbx
jmp 0x622f
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r12, %rdi
je 0x622f
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x968e
movq %rbx, %rdi
callq 0x4830