-
Notifications
You must be signed in to change notification settings - Fork 0
/
EchoClient.java
409 lines (362 loc) · 14.3 KB
/
EchoClient.java
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package myClient;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.Certificate;
import java.security.PrivateKey;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.net.ssl.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import login.UserLogin;
import rsaEncryptSign.DHCryptoInitiator;
import rsaEncryptSign.DHKeyAlice;
import rsaEncryptSign.DHKeyBob;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream; // Used to write objects to the server
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class connects to an EchoServer to sends encrypted messages
* back and forth. Java message serialization is used to pass
* Message objects around.
*
*
*/
public class EchoClient implements ActionListener, Runnable
{
public static final String host = "localhost";
private String serverHost;
private int serverPort;
private String ksName; //file path of keystore
private String storePass;
private String tsName; //file path of trust store
private String trustStorePass;
private String alias;
private char[] keyPass;
private String userName;
private String email;
private char[] password;
private java.security.cert.Certificate myCert;
private PrivateKey myPrivKey;
private ObjectOutputStream oos;
private SSLSocket sock;
private AES userAes;
private int new_return;//'0'=new user, '1'=returning user
private int initiate_DH;//'0' if chose to initiate DH with next logged in user
//'1' if chose to be the next logged in user
private final JFrame f = new JFrame();
private final JTextField tf = new JTextField(25);
protected final JTextPane tp = new JTextPane();
private final JButton send = new JButton("Send");
private Thread thread;
public static void main(String[] args) {
new LoginWindow(host, EchoServer.SERVER_PORT);
}//-- end main(String[])
public EchoClient(String userName, char[] password, String alias, char[] keyPass, String email,
String host, int portNumber, int new_return, int initiate_DH) {
this.userName = userName;
this.password = password;
this.alias = alias;
this.keyPass = keyPass;
this.email = email;
serverPort = portNumber;
serverHost = host;
this.new_return = new_return;
this.initiate_DH = initiate_DH;
//setup GUI
f.setTitle("Secure Chat Client");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getRootPane().setDefaultButton(send);
f.setPreferredSize(new Dimension(400, 600));
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(tp), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);
f.pack();
send.addActionListener(this);
tp.setEditable(false);
DefaultCaret caret = (DefaultCaret) tp.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
display("Trying " + host + " on port " + portNumber);
thread = new Thread(this);
}
public void start() {
f.setVisible(true);
thread.start();
}
public void run() {
ArrayList<char[]> jksPassWs = assignKeystorePaths();
SSLContext sc = initSSLContext(jksPassWs);
SSLSocketFactory factory = sc.getSocketFactory();
sock = connect(factory);
try {
DHKeyAlice dhka = null; //may use this later if client chose to initiate DH exchange
Thread.sleep(200);
display("begin DH key exchange with server");
DHKeyBob dhkb = new DHKeyBob(sock, myCert, myPrivKey, false);
display("Communication with server now encrypted");
//initialize an AES obj with new shared DH key
userAes = new AES(dhkb.getBobSecret());
display("Begin secure login");
/*If returning user, send credentials and receive authentication response
* If new user, send new credentials, create new certificate
* server stores new data and access is denied, program closes.
* a new user will not be able to chat immediately because his certificate
* has not been signed yet. This must happen offline.
*/
UserLogin ul = new UserLogin(sock, userAes, alias, ksName, storePass, "", userName, email,
password, keyPass, new_return, initiate_DH);
if (!ul.getVerified()) {
display("Access denied. Exiting program");
System.exit(1);
} else {display("Access granted."); }
if (ul.getInitiator()) {
display("Please hold for your correspondent to log in." );
/* DH key exchange will begin after 2nd client joins.
* This client connection was handed off to her DHKeyEchoThread2
* which gets put in waiting state.
*/
dhka = new DHKeyAlice(sock, myCert, myPrivKey, true);
userAes = new AES(dhka.getAliceSecret());
} else {
display("You are the second user. Your correspondent has initiated Diffie-Hellman." );
/*DH key exchange between two clients may now begin.
* 2nd client's connection was handed off to his DHKeyEchoThread2
* which notifies the waiting thread
*/
dhkb = new DHKeyBob(sock, myCert, myPrivKey, true);
userAes = new AES(dhkb.getBobSecret());
}
/*newly created DH key is passed along with socket.
* The server has passed its socket to an EchoThread
* which will distribute chatroom messages
*/
startChat();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvalidKeyException | NoSuchAlgorithmException |
NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*This sets up file descriptors and passwords for keystores and truststores
* used in the TLS connection. This information is also used for RSA signatures
* during DH key exchanges.
* Method produces:
* ArrayList<char[]> passwords:
* '0' = Key Store Pass
* '1' = Trust Store Pass
* '2' = alias pass
*/
private ArrayList<char[]> assignKeystorePaths() {
/*
* in production we would not use multiple keystores because
* A) one user per client, presumably would not want to share
* sccess to this program with another user
* B) Java JSSE KeyManagerFactory cannot be initialized with
* keystores containing multiple PrivateKey entries
*/
if (alias.equals("newClientA")) {
keyPass = "newClientAPass".toCharArray();
} else { keyPass = "clientBPass".toCharArray();}
//again, hard-coded for testing purposes
if (alias.equals("newClientA")) {
ksName = "C:\\temp-openssl-32build\\clientKeystore\\newClientKeystore.jks";
} else {
ksName = "C:\\temp-openssl-32build\\clientKeystore\\CLIENTBkeystore.jks";
}
//also hard-coded for testing purposes
if (alias.equals("newClientA")) {
storePass = "newClientStorePass";
} else {
storePass = "CLIENTBstorepass";
}
char[] spass = storePass.toCharArray();// password for keystore
tsName = "C:\\temp-openssl-32build\\clientKeystore\\newClientTrustStore";
trustStorePass = "newClientTrustPass";
char[] tspass = trustStorePass.toCharArray();// password for TrustStore
char[] kpass = keyPass;// password for alias' RSA private key
ArrayList<char[]> passwords = new ArrayList<>();
passwords.add(spass);
passwords.add(tspass);
passwords.add(kpass);
return passwords;
}
/*initialize an SSLContext for the client.
* Set for TLS1.2 and register the client's
* keystore and truststore
*/
public SSLContext initSSLContext(ArrayList<char[]> jksPassWs) {
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLSv1.2");
//initialize KeyStore
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream ksfis = new FileInputStream(ksName);
BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
ks.load(ksbufin, jksPassWs.get(0));
ksfis.close();
//initialize TrustStore
KeyStore ts = KeyStore.getInstance("JKS");
FileInputStream tsfis = new FileInputStream(tsName);
BufferedInputStream tsbufin = new BufferedInputStream(tsfis);
ts.load(tsbufin, jksPassWs.get(1));
tsfis.close();
//get client certificate and private key
myCert = ks.getCertificate(alias);
myPrivKey = (PrivateKey) ks.getKey(alias, jksPassWs.get(2));
//init factories
KeyManagerFactory kmf = KeyManagerFactory.getInstance("Sunx509");
kmf.init(ks, jksPassWs.get(2));
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
tmf.init(ts);
SecureRandom random = SecureRandom.getInstanceStrong();
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), random);
} catch (NoSuchAlgorithmException e1) {
System.err.println(e1.getMessage());
e1.printStackTrace();
} catch (KeyStoreException e2) {
System.err.println(e2.getMessage());
e2.printStackTrace();
} catch (CertificateException | IOException
| UnrecoverableKeyException |KeyManagementException e) {
e.printStackTrace();
}
return sc;
}
/*Return the socket for this connection
* This is where TLS ciphersuites should be restricted
*/
public SSLSocket connect(SSLSocketFactory factory) {
SSLSocket sock = null;
try{
// Connect to the specified server
sock = (SSLSocket) factory.createSocket(serverHost, serverPort);
String[] suites = sock.getSupportedCipherSuites();
//{"TLS_RSA_WITH_AES_128_CBC_SHA256"};
// sock.setEnabledCipherSuites(suites);
Thread.sleep(1000);
display("\nConnected to " + host + " on port " + EchoServer.SERVER_PORT);
} catch(Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace(System.err);
}
return sock;
}
/*Start a ServerThread to receive chat messages
* Initialize our ObjectOutputStream
* Print our TLS session details
*/
public void startChat() throws IOException, InterruptedException,
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
ServerThread serverThread = new ServerThread(sock, userName, userAes, tp);
serverThread.execute();
oos = new ObjectOutputStream(sock.getOutputStream());
//get session after connection is established
SSLSession session = sock.getSession();
display("Session details: ");
display("\tProtocol: " + session.getProtocol());
display("\tCipher suite: " + session.getCipherSuite());
display("Begin chatting.");
}
/*Pushing 'send' button
* chat messages are encrypted here using DH session key shared with
* one correspondent, then create an HMAC of the encrypted message,
* then send them together to the correspondent.
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
//@Override
public void actionPerformed(ActionEvent ae) {
String s = tf.getText();
Message clientMsg = null;
try {
if (oos != null) {
if (s.toUpperCase().contains("goodbye")) {
sock.close();
display("Leaving chat");
}
else {
String nextSend = userName + " > " + s;
String ciphertext = userAes.encrypt(nextSend);
byte[] cipherhash = Message.computeHash(ciphertext, userAes);
clientMsg = new Message(ciphertext, cipherhash);
oos.writeObject(clientMsg);
oos.flush();
}
}//end if
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException | IOException e) {
e.printStackTrace();
}
tf.setText("");
}
/*Used to find out supported cipher suites and
* supported TLS versions
*/
public void printParams(SSLContext sc) {
SSLParameters params = sc.getSupportedSSLParameters();
String[] ciphers = params.getCipherSuites();
String[] protocols = params.getProtocols();
display("supported cipher suites are: \n");
for (String c : ciphers) {
display(c + "\n");
}
display("Supported protocols are: \n");
for (String p : protocols) {
display(p +"\n");
}
display("client session : " + sc.getClientSessionContext());
}
/*Print stylized messages to the GUI
* Each new line is added to the document model of
* the JTextPane
*/
private void display(final String s) {
SwingUtilities.invokeLater(new Runnable() {
//@Override
public void run() {
StyledDocument doc = tp.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
// Add some text
try
{
doc.insertString(doc.getLength(), s + "\n", keyWord );
}
catch(Exception e) { System.out.println(e); }
}
});
}
} //-- end class EchoClient