-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
361 lines (310 loc) · 10.1 KB
/
ChatServer.java
File metadata and controls
361 lines (310 loc) · 10.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.net.*;
import java.io.*;
import java.security.*;
public class ChatServer implements Runnable
{
private ChatServerThread clients[] = new ChatServerThread[20];
private ServerSocket server_socket = null;
private Thread thread = null;
private int clientCount = 0;
private Utils utils = null;
private PrivateKey privateKey;
private PublicKey publicKey;
private int messageCounter = 0;
private int forbiddenIDs[];
public PrivateKey getPrivateKey() {
return privateKey;
}
public ChatServer(int port)
{
try
{
// Binds to port and starts server
System.out.println("Binding to port " + port);
server_socket = new ServerSocket(port);
System.out.println("Server started: " + server_socket);
//Creates server keypair
this.utils = new Utils();
KeyPair kp = this.utils.kPGGen(1024);
this.privateKey = kp.getPrivate();
this.publicKey = kp.getPublic();
this.forbiddenIDs = new int[]{23, 12, 7};
start();
}
catch(IOException ioexception)
{
// Error binding to port
System.out.println("Binding error (port=" + port + "): " + ioexception.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
public void run()
{
while (thread != null)
{
try
{
// Adds new thread for new client
System.out.println("Waiting for a client ...");
addThread(server_socket.accept());
}
catch(IOException ioexception)
{
System.out.println("Accept error: " + ioexception); stop();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
}
public void start()
{
if (thread == null)
{
// Starts new thread for client
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
if (thread != null)
{
// Stops running thread for client
thread.stop();
thread = null;
}
}
private int findClient(int ID)
{
// Returns client from id
for (int i = 0; i < clientCount; i++)
if (clients[i].getID() == ID)
return i;
return -1;
}
public synchronized void handle(int ID, Message message) throws BadPaddingException, InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, SignatureException, IOException {
if(message.getSharekey()==0){
byte[] encryptedData = message.getEncryptedData();
SecretKey key = utils.unwrapKey(message.getSymmetric(), this.privateKey);
String input = this.utils.decryptMessage(encryptedData, key);
int id = findClient(ID);
boolean verified = utils.verifySign(message.getSignatureBytes(), input.getBytes(), clients[id].getClientPublicKey());
this.messageCounter++;
if (!verified) {
System.out.println("WARNING - MESSAGE COMPROMISED");
return;
}
if (input.equals(".quit"))
{
int leaving_id = findClient(ID);
// Client exits
clients[leaving_id].send(".quit");
// Notify remaing users
for (int i = 0; i < clientCount; i++)
if (i!=leaving_id)
clients[i].send("Client " +ID + " exits..");
remove(ID);
}
else
// Brodcast message for every other client online
for (int i = 0; i < clientCount; i++)
clients[i].send(ID + ": " + input);
if(messageCounter>100)
{
this.utils = new Utils();
KeyPair kp = this.utils.kPGGen(1024);
this.privateKey = kp.getPrivate();
this.publicKey = kp.getPublic();
Message msg = new Message(this.publicKey);
for (int i = 0; i < clientCount; i++)
clients[i].shareKey(msg);
this.messageCounter = 0;
}
}
else if (message.getSharekey()==2)
{
//verifica se o ID do cliente nao e proibido (uso 23 como exemplo)
for (int i = 0; i < this.forbiddenIDs.length; i++) {
if (message.getID() == this.forbiddenIDs[i]) {
System.out.println("CLient refused by ID");
clients[findClient(ID)].close();
return;
}
}
}
else if (message.getSharekey()==1)
{
int id = findClient(ID);
clients[id].setClientPublicKey(message.getKeyToShare());
//System.out.println("client Pub key shared "+message.getKeyToShare().toString());
}
}
public synchronized void remove(int ID)
{
int pos = findClient(ID);
if (pos >= 0)
{
// Removes thread for exiting client
ChatServerThread toTerminate = clients[pos];
System.out.println("Removing client thread " + ID + " at " + pos);
if (pos < clientCount-1)
for (int i = pos+1; i < clientCount; i++)
clients[i-1] = clients[i];
clientCount--;
try
{
toTerminate.close();
}
catch(IOException ioe)
{
System.out.println("Error closing thread: " + ioe);
}
toTerminate.stop();
}
}
private void addThread(Socket socket) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException {
if (clientCount < clients.length)
{
InetAddress forbidden = InetAddress.getByName("www.google.com");
if(socket.getInetAddress().equals(forbidden))
{
System.out.println("Client Refused");
socket.close();
return;
}
// Adds thread for new accepted client
System.out.println("Client accepted: " + socket);
clients[clientCount] = new ChatServerThread(this, socket);
try
{
clients[clientCount].open();
clients[clientCount].start();
//share server public key
Message sharKeyMessage = new Message(this.publicKey);
clients[clientCount].shareKey(sharKeyMessage);
clientCount++;
}
catch(IOException ioe)
{
System.out.println("Error opening thread: " + ioe);
}
}
else
System.out.println("Client refused: maximum " + clients.length + " reached.");
}
public static void main(String args[])
{
ChatServer server = null;
if (args.length != 1)
// Displays correct usage for server
System.out.println("Usage: java ChatServer port");
else
// Calls new server
server = new ChatServer(Integer.parseInt(args[0]));
}
}
class ChatServerThread extends Thread
{
private ChatServer server = null;
private Socket socket = null;
private int ID = -1;
private ObjectInputStream streamIn = null;
private ObjectOutputStream streamOut = null;
public PublicKey getClientPublicKey() {
return clientPublicKey;
}
public void setClientPublicKey(PublicKey clientPublicKey) {
this.clientPublicKey = clientPublicKey;
}
private Utils utils = null;
private PublicKey clientPublicKey;
public ChatServerThread(ChatServer _server, Socket _socket) throws NoSuchPaddingException, NoSuchAlgorithmException {
super();
server = _server;
socket = _socket;
ID = socket.getPort();
utils = new Utils();
}
// Sends message to client
public void send(String msg)
{
try
{
SecretKey symmetric = utils.generateKey();
byte[] encrypted = utils.encryptMessage(msg.getBytes(), symmetric);
byte[] encryptedSymmetric = utils.wrapKey(symmetric,clientPublicKey);
byte[] signatureBytes = utils.signMessage(msg.getBytes(), server.getPrivateKey());
Message message = new Message(encrypted, encryptedSymmetric, signatureBytes);
streamOut.writeObject(message);
streamOut.flush();
}
catch(IOException ioexception) {
System.out.println(ID + " ERROR sending message: " + ioexception.getMessage());
server.remove(ID);
stop();
} catch (Exception e) {
e.printStackTrace();
}
}
public void shareKey(Message msg)
{
try {
streamOut.writeObject(msg);
} catch (IOException e) {
e.printStackTrace();
}
try {
streamOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// Gets id for client
public int getID()
{
return ID;
}
// Runs thread
public void run()
{
System.out.println("Server Thread " + ID + " running.");
while (true)
{
try
{
server.handle(ID,(Message) streamIn.readObject());
}
catch(IOException ioe)
{
System.out.println(ID + " ERROR reading: " + ioe.getMessage());
server.remove(ID);
stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Opens thread
public void open() throws IOException
{
streamIn = new ObjectInputStream(new
BufferedInputStream(socket.getInputStream()));
streamOut = new ObjectOutputStream(new
BufferedOutputStream(socket.getOutputStream()));
streamOut.flush();
}
// Closes thread
public void close() throws IOException
{
if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}
}