-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecureAdditionClient.java
More file actions
executable file
·287 lines (225 loc) · 7.01 KB
/
Copy pathSecureAdditionClient.java
File metadata and controls
executable file
·287 lines (225 loc) · 7.01 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
//package start;
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.ssl.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
class ExtraThread extends Thread {
BufferedReader incoming;
SecureAdditionClient client;
Boolean wait_for_command;
ExtraThread(BufferedReader in, SecureAdditionClient c) {
this.incoming = in;
this.client = c;
wait_for_command = true;
}
public void run() {
try {
String inputLine;
while (wait_for_command) {
inputLine = this.incoming.readLine();
wait_for_command = client.handle_command(inputLine);
}
} catch(IOException ioe) {
}
}
}
public class SecureAdditionClient implements ActionListener {
private InetAddress host;
// This is not a reserved port number
static final int port = 8189;
static final String KEYSTORE = "ClientKeystore.ks";
static final String TRUSTSTORE = "ClientTruststore.ks";
static final String STOREPASSWD = "111111";
static final String ALIASPASSWD = "333333";
private BufferedReader in = null;
private PrintWriter out = null;
private Vector<JRadioButton> buttonList = null;
private Vector<String> alternativeList = null;
private Vector<JLabel> nrOfVotes = null;
private JPanel alternatives;
private JRadioButton alt1;
private JButton voteButton;
private JPanel results;
private ButtonGroup buttonGroup;
private int token;
public SecureAdditionClient () {
}
public SecureAdditionClient( InetAddress host ) {
this.host = host;
}
//GUI
private void createAndShowGUI()
{
String inStr = "";
int nrOfArguments = 0;
buttonList = new Vector<JRadioButton>();
alternativeList = new Vector<String>();
nrOfVotes = new Vector<JLabel>();
try {
//Window
JFrame frame = new JFrame("VotingMachine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Layout
JPanel contentPane = new JPanel(new BorderLayout());
//Label
this.out.println("fetch_question"); //ask for a question from server
inStr = this.in.readLine();
CommandParser question = new CommandParser(inStr);
JLabel label = new JLabel(question.next_argument());
contentPane.add(label, BorderLayout.PAGE_START);
//Buttons
this.out.println("fetch_alternatives");
inStr= this.in.readLine();
CommandParser command = new CommandParser(inStr);
//Statistics
this.out.println("fetch_statistics");
inStr= this.in.readLine();
CommandParser stats = new CommandParser(inStr);
//buttongroup
buttonGroup = new ButtonGroup();
alternatives = new JPanel();
alternatives.setLayout(new GridLayout(command.count_arguments(),1));
print_cmd("stats = " + stats.count_arguments());
System.out.println(command.count_arguments());
for (int i = 0; i<command.count_arguments(); i++)
{
//alternatives
String arg = command.next_argument();
alternativeList.add(arg);
System.out.println(arg);
JRadioButton temp = new JRadioButton(arg);
buttonList.add(temp);
buttonGroup.add(temp);
alternatives.add(temp);
}
contentPane.add(alternatives , BorderLayout.CENTER);
//VoteButton
voteButton = new JButton("Vote!");
voteButton.addActionListener(this);
contentPane.add(voteButton, BorderLayout.EAST);
//VotingResults
results = new JPanel();
results.setLayout(new GridLayout(alternativeList.size(),2)); //alternative, number of votes
for(int i=0; i<alternativeList.size(); i++)
{
JLabel label1 = new JLabel(alternativeList.get(i));
JLabel label2 = new JLabel(stats.next_argument());
nrOfVotes.add(label2);
results.add(label1);
results.add(label2);
label1 = null;
label2 = null;
}
contentPane.add(results, BorderLayout.SOUTH);
frame.setContentPane(contentPane);
//Display the window
frame.pack();
frame.setVisible(true);
// listen for commands
ExtraThread incThread = new ExtraThread(in, this);
incThread.start();
}
catch(Exception e)
{
}
}
//End of GUI
//Lyssnarmetod
public void actionPerformed(ActionEvent e) {
if(e.getSource() == voteButton)
{
System.out.println("Du har röstat!");
for(int i=0; i<buttonList.size(); i++)
{
if(buttonList.get(i).isSelected()){
this.out.println("vote " + this.token + " " + i);
}
}
}
}
public Boolean handle_command(String command) {
try {
this.print_cmd("Recieved: " + command);
CommandParser cmd = new CommandParser(command);
if (cmd.isEqual("quit", "q")) {
return false;
}
if (cmd.isEqual("statistics")) { //update labels for statistics
for(int i=0; i<nrOfVotes.size(); i++){
nrOfVotes.get(i).setText(cmd.next_argument());
}
}
}
catch( Exception x ) {
System.out.println( x );
x.printStackTrace();
return false;
}
return true;
}
// The method used to start a client object
public void run() {
try {
KeyStore ks = KeyStore.getInstance( "JCEKS" );
ks.load( new FileInputStream( KEYSTORE ), STOREPASSWD.toCharArray() );
KeyStore ts = KeyStore.getInstance( "JCEKS" );
ts.load( new FileInputStream( TRUSTSTORE ), ALIASPASSWD.toCharArray() );
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( ks, STOREPASSWD.toCharArray() );
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( ts );
SSLContext sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
SSLSocketFactory sslFact = sslContext.getSocketFactory();
SSLSocket clientSocket = (SSLSocket)sslFact.createSocket(host, port);
//clientSocket.setEnabledCipherSuites( clientSocket.getSupportedCipherSuites() );
String[] ciphers = clientSocket.getSupportedCipherSuites();
String[] selectedCiphers = { ciphers[9] };
System.out.println(ciphers[9]);
clientSocket.setEnabledCipherSuites( selectedCiphers );
this.in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() ) );
this.out = new PrintWriter( clientSocket.getOutputStream(), true );
}
catch( Exception x ) {
System.out.println( x );
x.printStackTrace();
}
}
public void print_cmd(String msg) {
System.out.println(">>>> Secure Client: " + msg);
}
public Boolean get_token() {
Authenticate auth = new Authenticate();
String name = null;
String password = null;
while(name == null || name.equals("")) {
name = JOptionPane.showInputDialog(null, "Enter username: ", "", 1);
}
while(password == null || password.equals("")) {
password = JOptionPane.showInputDialog(null, "Enter password: ", "", 1);
}
this.token = auth.get_token(name, password);
return this.token != -1;
}
public static void main( String[] args ) {
SecureAdditionClient client = null;
try {
InetAddress host = InetAddress.getLocalHost();
if ( args.length > 0 ) {
host = InetAddress.getByName( args[0] );
}
client = new SecureAdditionClient( host );
}
catch ( UnknownHostException uhx ) {
client = new SecureAdditionClient();
}
if (client.get_token()) {
client.run();
client.createAndShowGUI();
}
}
}