-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
243 lines (231 loc) · 6.72 KB
/
Copy pathServer.java
File metadata and controls
243 lines (231 loc) · 6.72 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
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: A simple multithreaded Chat Server program that uses *
* PKI & Secret key encryption to deliver chat messages to *
* the client *
* *
* @author Krishnan Subramanian for ISA656 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* required imports for the program
*/
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Random;
/**
* Implements the Serializable interface (for object stream to be sent through
* sockets) and Runnable Interface (for multithreading)
*
* @author Krishnan
*
*/
public class Server implements Serializable, Runnable {
/**
* Required for a serializable class
*/
private static final long serialVersionUID = 1L;
private String serverPath = "";
private ServerSocket servSock = null;
private int noConns = 0;
private int g_port = 0;
/**
* Constructor to initialize the Server
*
* @param port
* @throws IOException
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws ClassNotFoundException
*/
public Server(int port,String file) throws IOException, KeyStoreException,
NoSuchAlgorithmException, CertificateException,
ClassNotFoundException {
servSock = new ServerSocket(port);
g_port = port;
serverPath = file;
newThreadStart();
}
/**
* Starts a new client thread module
*/
private void newThreadStart() {
(new Thread(this)).start();
}
/**
* The run() method inherited abstract method from the runnable interface
*/
@Override
public void run() {
System.out.println("Server initialized, running on port: " + g_port);
Socket socket = null;
/**
* A sequence variable
*/
int seq = 0;
try {
socket = servSock.accept();
} catch (IOException e1) {
e1.printStackTrace();
}
newThreadStart();
/**
* number of client connections
*/
++noConns;
System.out.println("Client connected: " + socket.getInetAddress() + ":"
+ socket.getPort());
ServerAuth sa = null;
/**
* Create an object for the Server Authentication Module
*/
try {
sa = new ServerAuth(serverPath);
} catch (KeyStoreException e1) {
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (CertificateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ObjectOutputStream out = null;
ObjectInputStream in = null;
String currentUser = null;
while (true) {
try {
/**
* increment the sequence number
*/
++seq;
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
/**
* First sequence: try to authenticate the user by looking for
* the public in the server.key keystore
*/
if (seq == 1) {
byte[] cusr = (byte[]) in.readObject();
byte[] busr = sa.DecryptPKI(cusr);
String msg = new String(busr);
String usr = "pub_" + msg.trim();
currentUser = msg.trim();
/**
* Check if the user's public key exists on the server's
* keystore
*/
boolean flg = sa.CheckUser(usr);
if (!flg) {
String errcode = "404";
/**
* Tell the user if authentication is unsuccessful
*/
System.out.println("Client " + socket.getInetAddress()
+ ":" + socket.getPort() + " User \""
+ currentUser
+ "\" not registered: Authentication Failed");
// Write the bytestream as ObjectStream data
out.writeObject(errcode);
out.flush();
break;
} else {
/**
* Authentication was successful
*/
String authcode = "200";
out.writeObject(authcode);
out.flush();
continue;
}
}
/**
* Second sequence: Initialize the chat session using DES key
* supplied by the client
*/
else if (seq == 2) {
byte[] msg = (byte[]) in.readObject();
byte[] dec = sa.DecryptPKI(msg);
sa.SetDESKey(dec);
String cmsg = "200";
out.writeObject(cmsg);
out.flush();
continue;
}
/**
* The chat message: echo back the captialized message and send
* it back to the client with the username. The whole message is
* encrypted using the DES key supplied by the Client
*/
byte[] cmsg = (byte[]) in.readObject();
String ptxt = sa.DecryptDES(cmsg);
String fromServer = currentUser + ": " + ptxt.toUpperCase();
byte[] enc = sa.EncryptDES(fromServer);
out.writeObject(enc);
out.flush();
} catch (SocketException e) {
System.out.println("Connection was reset/closed by Client "
+ socket.getInetAddress() + ":" + socket.getPort());
break;
} catch (NullPointerException e) {
System.out.println("Connection was closed by Client "
+ socket.getInetAddress() + ":" + socket.getPort());
break;
} catch (EOFException e) {
System.out.println("Connection was closed by Client "
+ socket.getInetAddress() + ":" + socket.getPort());
break;
} catch (Exception e) {
System.out.println("Oops.. an exception occured ");
e.printStackTrace();
break;
}
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException,
KeyStoreException, NoSuchAlgorithmException, CertificateException,
ClassNotFoundException {
/**
* Generate a Random port number between 1035 and 65530
*/
int port = new Random().nextInt(65530 - 1035) + 1;
String file = "server.key";
/**
* Check for program syntax
*/
if (args.length > 2) {
System.out
.println("More than two arguments passed to the Server program");
System.out.println("Usage: <port> <serverkeyfile path>");
System.exit(0);
} else if (args.length == 2) {
try {
port = Integer.parseInt(args[0]);
file = args[1].trim();
} catch (Exception e) {
System.out
.println("An Exception occured while trying to parse the port number/file");
System.exit(0);
}
} else if(args.length<2){
System.out
.println("Too few arguments specified");
System.out.println("Usage: <port> <serverkeyfile path>");
System.exit(0);
}
new Server(port,file);
}
}