forked from LuboO/pb173_ateam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
163 lines (143 loc) · 4.75 KB
/
server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef SERVER_H
#define SERVER_H
#include <list>
#include <afxwin.h>
#include <windows.h> //mutex
#include "polarssl/entropy.h"
#include "polarssl/ctr_drbg.h"
#include "socket.h"
#include "struct.h"
#define THREADCOUNT 5
struct User
{
std::string login;
std::string passwordHash;
cert* certificate;
int port;
User(std::string login,std::string passwordHash,cert* certificate):login(login),passwordHash(passwordHash),certificate(certificate){}
};
class Server{
private:
_Guarded_by_(ghMutex) std::list<User*> registeredUsers;
_Guarded_by_(ghMutex) std::list<User*> onlineUsers;
cert certificate;
unsigned char publicKey[128];
unsigned char privateKey[128];
public:
_Has_lock_kind_( _Lock_kind_mutex_ ) HANDLE ghMutex;
Server();
//static UINT waiting(LPVOID a);
//static UINT answer(LPVOID s);
/**
* Checks authenticity of given certificate.
*
* @param userCert certficate to be checked
* @param CApublicKey public key of certification authority
*
* @return true when check succesful, false otherwise
*/
_Check_return_ bool checkCert(_In_ cert userCert , _In_ unsigned char* CApublicKey);
/**
* Register user into database.
*
* @param registeredUsers list with registered users
* @param login user's login
* @param pwd user's password
* @param userCert user's certificate
*
* @return zero when succesful, nonzero value when error occurs
*/
_Check_return_ _Requires_lock_held_(ghMutex) int registration(_In_ std::string login , _In_ std::string pwd , _In_ cert* userCert);
/**
* Generates random AES key.
*
* @param key generated key
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int randGenAES(_In_ unsigned char* key);
/**
* Generates random pair of RSA keys.
*
* @param publicKey generated public key
* @param privateKey generated private key
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int randGenRSA(_In_ unsigned char* publicKey ,_In_ unsigned char* privateKey);
/**
* Encrypts/decrypts given data with AES - 128.
*
* @param key 128 bit long key
* @param iv initialisation vector
* @param data input data
* @param outData output data
* @param mode 0 - encryption ; 1 - decryption
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int cryptoSym(_In_ unsigned char* key ,_In_ unsigned char* iv , _In_ unsigned char* data , _Out_ unsigned char* outData , _In_ int mode);
/**
* Encrypts/decrypts given data with RSA - 1024.
*
* @param key 1024 bit long key
* @param data input data
* @param outData output data
* @param mode 0 - encryption ; 1 - decryption
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int cryptoAsym(unsigned char* key , unsigned char* data , unsigned char* outData , int mode);
/**
* Creates a hash of given data.
*
* @param data data to be hashed
* @param output output of hash
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int hash(_In_ unsigned char* data , _In_ unsigned char* output);
/**
* Sends data to client.
*
* @param clientAdress client's adress
* @param data data to be send
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int sendData(_In_reads_(len) char *Buf, _In_ int len, _In_ int Client);
/**
* Adds user into list of online users.
*
* @param onlineUsers list of online users
* @param login user's login
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ _Requires_lock_held_(ghMutex) int login(_In_ std::string login, _In_ std::string password, _In_ int port);
/**
* Removes user from list of online users.
*
* @param onlineUsers list of online users
* @param login user's login
*
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ _Requires_lock_held_(ghMutex) int logout(_In_ std::string login);
/**
* Accepts user's request, takes action based on request type.
*
* @param requestType type of request
* @return returns zero when succesful, nonzero value otherwise
*/
_Check_return_ int requestAccept(_In_ int rT);
_Check_return_ _Requires_lock_held_(ghMutex) _Ret_notnull_ User* getUser(_In_ std::string login);
_Check_return_ _Requires_lock_held_(ghMutex) _Ret_notnull_ User* getOnlineUser(_In_ std::string login);
_Check_return_ int startServer(_In_ int port);
_Check_return_ int receiveData(_In_reads_(len) char *Buf, _In_ int len, _In_ int Client);
_Check_return_ int endSocket();
_Check_return_ _Requires_lock_held_(ghMutex) _Ret_z_ std::string sendlist(_In_ std::string login);
_Check_return_ static _Ret_z_ string generatePassword();
_Check_return_ _Requires_lock_held_(ghMutex) int startClientCommunication(_In_ std::string fromC, _In_ std::string toClient);
};
#endif //SERVER_H