// File: /workspace/github_cpp_makefile_star2_O2/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 0x655c
movl (%r14), %esi
cmpl $0xbb9, %esi            # imm = 0xBB9
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5c82
movq %rbx, %rdi
callq 0x652a
movq %rax, 0xb8(%rsp)
testq %rax, %rax
je 0x5c82
movq %rax, %r12
movslq 0x14(%rsp), %rdx
xorl %r14d, %r14d
leaq 0xb8(%rsp), %rsi
xorl %edi, %edi
callq 0x40d0
movq %rax, %r15
movq %r12, %rdi
callq 0x45e0
testq %r15, %r15
je 0x5cd1
leaq 0x4aa3(%rip), %rsi      # 0xa118
leaq 0x4ab9(%rip), %rdx      # 0xa135
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x8770
movq %r14, %rdi
movq %r15, %rsi
callq 0x89aa
testb %al, %al
je 0x5c87
leaq 0xf0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x38(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
movq %r15, %rsi
callq 0x8b04
testb %al, %al
je 0x5caf
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x8baa
leaq 0x9e9f(%rip), %rdi      # 0xf580
movq %rax, %rsi
callq 0x925e
leaq 0x4a61(%rip), %rsi      # 0xa151
leaq 0xe0(%rsp), %rdi
callq 0x4d85
testb %al, %al
je 0x5c94
movq 0x98c4(%rip), %rdi      # 0xefd0
leaq 0x4a6d(%rip), %rsi      # 0xa180
callq 0x4440
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x4410
movq %rax, %rdi
callq 0x4230
movq %r15, %rdi
callq 0x4260
leaq 0x4a5f(%rip), %rdi      # 0xa19e
leaq 0x4ed6(%rip), %rsi      # 0xa61c
callq 0x4530
testq %rax, %rax
je 0x5d66
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4640
movq %rax, %r14
movq %r15, %rdi
callq 0x4450
testq %r14, %r14
je 0x5d99
leaq 0x98(%rsp), %rsi
andq $0x0, (%rsi)
movq %r14, %rdi
callq 0x4380
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %eax, %esi
callq 0x6424
movq 0x98(%rsp), %rsi
testb %al, %al
je 0x5ce4
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x6524
movq 0x98(%rsp), %rdi
testb %al, %al
je 0x5cee
leaq 0x4a3d(%rip), %rsi      # 0xa20b
movl $0x13c, %edx            # imm = 0x13C
callq 0x4310
movq %r14, %rdi
callq 0x4260
leaq 0x9d99(%rip), %rsi      # 0xf580
leaq 0x40(%rsp), %rdi
callq 0x7a6e
leaq 0x4a2b(%rip), %rsi      # 0xa223
leaq 0x30(%rsp), %rdi
callq 0x8f2c
leaq 0x30(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x9012
movq %rax, %r14
testq %rax, %rax
je 0x5db0
leaq 0x40(%rsp), %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d02
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x6424
testb %al, %al
je 0x5cfa
movl 0x4(%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x7b12
testb %al, %al
je 0x5d02
movl 0x4(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6524
movl %eax, %ebp
movq %r14, %rdi
callq 0x45e0
testb %bpl, %bpl
je 0x5d02
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x655c
testb %al, %al
je 0x5d02
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d02
movl (%rsp), %esi
movq %rbx, %rdi
callq 0x652a
movq %rax, %r15
movl (%rsp), %edx
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x7b12
testq %r15, %r15
sete %cl
xorb $0x1, %al
orb %cl, %al
jne 0x5d02
movl (%rsp), %edx
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x90a2
movq %rax, %r14
movq %r15, %rdi
callq 0x45e0
testq %r14, %r14
je 0x5d02
leaq 0x28(%rsp), %rdi
callq 0x9178
movl 0x10(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x91ba
testb %al, %al
je 0x5d1b
leaq 0x28(%rsp), %rdi
callq 0x91e2
movq %rax, %r15
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x6ae0
movq %r14, %rdi
callq 0x45e0
testq %r15, %r15
je 0x5d31
movups (%r15), %xmm0
movaps %xmm0, 0xd0(%rsp)
movups 0x10(%r15), %xmm0
movaps %xmm0, 0xc0(%rsp)
pushq $0x20
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x6ae0
movq %r15, %rdi
callq 0x45e0
callq 0x46c0
leaq 0xa0(%rsp), %rdi
pushq $0x10
popq %rsi
callq 0x46e0
leaq 0xc(%rsp), %rdi
andl $0x0, (%rdi)
pushq $0x4
popq %rsi
callq 0x46e0
leaq 0xd0(%rsp), %rdi
callq 0x9118
leaq 0xa0(%rsp), %rdi
callq 0x9124
leaq 0xc0(%rsp), %rdi
callq 0x9130
leaq 0x40(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d31
leaq 0xa0(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rbx, %rdi
callq 0x6524
testb %al, %al
je 0x5d31
leaq 0x80(%rsp), %rdi
callq 0x92ac
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x9328
movl $0x4, 0x90(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x9350
leaq 0x20(%rsp), %rdi
callq 0x73b8
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x74b6
testb %al, %al
je 0x5d40
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7534
testb %al, %al
je 0x5d40
leaq 0x68(%rsp), %rsi
leaq 0x40(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d40
movl 0x68(%rsp), %esi
movq %rbx, %rdi
callq 0x6424
testb %al, %al
je 0x5d40
leaq 0x40(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d40
leaq 0x58(%rsp), %rdi
callq 0x93b6
movl 0x68(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6524
testb %al, %al
je 0x5d40
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x655c
testb %al, %al
je 0x5d40
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5d40
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x652a
testq %rax, %rax
je 0x5d40
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x9396
movl 0x8(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x75a0
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x763e
testb %al, %al
je 0x5db8
leaq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x775c
testb %al, %al
je 0x5db8
leaq 0x80(%rsp), %rdi
callq 0x9348
leaq 0x7c(%rsp), %rsi
movl %eax, (%rsi)
leaq 0x40(%rsp), %rdi
pushq $0x4
popq %rdx
callq 0x7b12
testb %al, %al
je 0x5db8
movl 0x7c(%rsp), %edi
callq 0x798a
movl 0xc(%rsp), %edi
callq 0x797a
leaq 0xd0(%rsp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x6ae0
leaq 0xc0(%rsp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x6ae0
leaq 0x78(%rsp), %rsi
movq %rbx, %rdi
callq 0x69f2
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x7b7c
movl %eax, %ebp
leaq 0x446a(%rip), %rsi      # 0xa071
leaq 0x100(%rsp), %rdi
callq 0x7bd0
movl 0x78(%rsp), %edx
leaq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x7c70
testb %al, %al
je 0x5dca
testq %r14, %r14
je 0x5c3d
movq %r14, %rdi
callq 0x45e0
leaq 0x100(%rsp), %rdi
leaq 0x74(%rsp), %rsi
callq 0x7ce8
movq %rax, %r15
testq %rax, %rax
je 0x5dd7
movl 0x74(%rsp), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6946
movl %eax, %r14d
movq %r15, %rdi
callq 0x45e0
testl %ebp, %ebp
setne %al
andb %al, %r14b
jmp 0x5dda
xorl %r14d, %r14d
jmp 0x5cd1
movq %r15, %rdi
callq 0x4260
xorl %r14d, %r14d
jmp 0x5cc7
movq 0x9335(%rip), %rdi      # 0xefd0
leaq 0x44c8(%rip), %rsi      # 0xa16a
callq 0x4440
movq %rax, %rdi
callq 0x4230
movq %r15, %rdi
callq 0x4260
xorl %r14d, %r14d
leaq 0xe0(%rsp), %rdi
callq 0x4210
leaq 0x38(%rsp), %rdi
callq 0x8996
movl %r14d, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testq %rsi, %rsi
je 0x5cb7
movq %rsi, %rdi
jmp 0x5cf3
testq %rdi, %rdi
je 0x5cb7
callq 0x45e0
jmp 0x5cb7
movq %r14, %rdi
callq 0x45e0
xorl %r14d, %r14d
leaq 0x30(%rsp), %rdi
callq 0x9102
leaq 0x40(%rsp), %rdi
callq 0x7ba8
jmp 0x5cba
movl 0x10(%rsp), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x6ae0
movq %r14, %rdi
callq 0x45e0
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
callq 0x923e
jmp 0x5d05
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
callq 0x7442
leaq 0x58(%rsp), %rdi
callq 0x935e
leaq 0x80(%rsp), %rdi
callq 0x92ba
jmp 0x5d34
movq 0x9283(%rip), %rdi      # 0xeff0
leaq 0x444b(%rip), %rsi      # 0xa1bf
callq 0x4440
leaq 0x441e(%rip), %rsi      # 0xa19e
movq %rax, %rdi
callq 0x4440
leaq 0x444a(%rip), %rsi      # 0xa1d9
movq %rax, %rdi
callq 0x4440
jmp 0x5dac
movq 0x9250(%rip), %rdi      # 0xeff0
leaq 0x4440(%rip), %rsi      # 0xa1e7
callq 0x4440
pushq $0x1
jmp 0x5db2
pushq $-0x1
popq %rdi
callq 0x4580
xorl %r14d, %r14d
leaq 0x18(%rsp), %rdi
callq 0x762a
jmp 0x5d43
testq %r14, %r14
je 0x5dd7
movq %r14, %rdi
callq 0x45e0
xorl %r14d, %r14d
leaq 0x100(%rsp), %rdi
callq 0x7df6
jmp 0x5dbb
jmp 0x5deb
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x7df6
jmp 0x5e0a
jmp 0x5e07
jmp 0x5e07
jmp 0x5e07
jmp 0x5e07
jmp 0x5e28
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x762a
jmp 0x5e2b
jmp 0x5e28
jmp 0x5e28
movq %rax, %rbx
jmp 0x5e35
jmp 0x5e21
movq %rax, %rbx
jmp 0x5e3f
jmp 0x5e5a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7442
leaq 0x58(%rsp), %rdi
callq 0x935e
leaq 0x80(%rsp), %rdi
callq 0x92ba
jmp 0x5e5d
jmp 0x5e5a
jmp 0x5e5a
jmp 0x5e5a
jmp 0x5e72
jmp 0x5e5a
jmp 0x5e72
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x923e
jmp 0x5e75
jmp 0x5e72
movq %rax, %rbx
jmp 0x5e7f
jmp 0x5e96
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x9102
leaq 0x40(%rsp), %rdi
callq 0x7ba8
jmp 0x5e99
jmp 0x5e96
jmp 0x5e96
jmp 0x5e96
movq %rax, %rbx
jmp 0x5ea6
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x4210
leaq 0x38(%rsp), %rdi
callq 0x8996
movq %rbx, %rdi
callq 0x47f0