-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureChatClient.java
More file actions
345 lines (214 loc) · 12.1 KB
/
SecureChatClient.java
File metadata and controls
345 lines (214 loc) · 12.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
/****************************************************************
* ITESM-CEM *
* Sistemas Operativos II *
* Prof. Rolando Menchaca Mendez *
* *
* SecureChatClient.java v 0.5 *
* *
* Cliente de un Chat seguro usando SSL y Certificados *
* digitales, tanto por parte del cliente como por *
* parte del servidor. *
* *
* *
* Autores: *
* Erika Vilches González 461595 *
* Gabriel Téllez Morales 453955 *
* *
* *
****************************************************************/
import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
/***************************
Clase threadClient
***************************/
class threadClient extends Thread
{
// Declaración del flujo de entrada, así como del socket
private Socket socket;
private BufferedReader in;
private String nick;
// Constructor de la clase threadClient
public threadClient(Socket s, String n) throws IOException
{
// Asignación de s a socket
socket = s;
nick = n;
// Creación del flujo de entrada
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}// fin de constructor threadClient(Socket s)
// Método run()
public void run()
{
try
{
for (;;)
{
try
{
//Comenzar a escuchar al servidor
String str = in.readLine();
/* Imprimir en el cliente todo lo que llegue
desde el servidor*/
if(str.equals(nick + "SALIR"))
{
//System.out.println(str);
//in.close();
//socket.close();
break;
}
System.out.println(str);
}//try
catch (Exception e)
{
throw new IOException(e.getMessage());
}
finally
{
}
}// fin de for infinito
}//fin de try
catch (IOException e)
{
System.out.println("Error en el hilo de servicio: " + e);
}
finally
{
try
{
System.out.println("el cliente dejo de escuchar");
in.close();
}
catch(IOException e)
{
System.out.println("Error al cerrar flujos: " + e);
}
}//fin de finally
}//fin de run()
}//fin de class threadClient
/***************************
Clase SecureChatClient
***************************/
public class SecureChatClient {
// Puerto donde escucha el servidor
static final int SPORT = 1717;
// main
public static void main(String[] args)throws IOException {
// Revisar los argumentos introducidos
if(args.length<1) {
System.out.println("uso: java -Djavax.net.ssl.trustStore=ckeystore -Djavax.net.ssl.trustStorePassword=ckeystore SecureChatClient 127.0.0.1");
System.exit(-1);
}
// Guardar dirección del servidor en addr
InetAddress addr = InetAddress.getByName(args[0]);
try
{
/* Establece el administrador de llaves para realizar
la autentificacion del cliente. Utiliza la
implementacion por defecto de "TrustStore" y de
las rutinas "secureRandom"*/
// Fabrica de sockets seguros
SSLSocketFactory factory = null;
try
{
/* Establece un administrador de llaves para
realizar autentificacion del servidor*/
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
// El password del almacen de llaves
char[] passphrase = "ckeystore".toCharArray();
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
// Tipo de almacen
ks = KeyStore.getInstance("JKS");
//Nombre del almacen de llaves
ks.load(new FileInputStream("ckeystore"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);
factory = ctx.getSocketFactory();
}
catch (Exception e)
{
throw new IOException(e.getMessage());
}
/* Creación del socket del cliente con la dirección del
servidor y el puerto en el que escucha*/
SSLSocket socket = (SSLSocket)factory.createSocket(addr, SPORT);
// Comienza el handshake entre cliente/servidor
socket.startHandshake();
// Creación del flujo de salida
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
// Creación de inconsole para leer de la consola
BufferedReader inconsole = new BufferedReader(new InputStreamReader(System.in));
// Creación de inconsole para leer de la consola
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));
// Deplegar información del servidor en el cliente
System.out.println("********************************************");
System.out.println("* *");
System.out.println("* Secure Chat Client v 0.5 *");
System.out.println("* *");
System.out.println("* BIENVENIDO *");
System.out.println("* *");
System.out.println("********************************************");
System.out.println("");
// Petición del nick al cliente
System.out.println("Dame tu nombre de usuario por favor: ");
// Leer nick
String nick = stdin.readLine();
// Enviar nick al servidor
out.println(nick);
// Desplegar información de bienvenida al usuario
System.out.println("Hola " + nick + " :)");
System.out.println("Para salir del chat escribe: SALIR");
System.out.println("");
try
{
// Iniciar hilo de servicio
new threadClient(socket,nick).start();
}
catch(Exception e)
{
System.out.println("Error al crear hilo de servicio: " + e);
socket.close();
}
try
{
// Ciclo infinito
for(;;)
{
// Leer de consola lo escrito por el cliente
String str = inconsole.readLine();
/* Condición de salida; enviada al servidor
por el cliente*/
if(str.equals("SALIR"))
{
/* Enviar el comando SALIR del cliente
hacia el servidor*/
out.println(str);
System.out.println("Cerrando");
break;
}
/* Enviar lo escrito por el cliente hacia
el servidor*/
out.println(str);
}// fin de for infinito
//socket.close();
}// fin de try
catch(IOException e)
{
System.out.println("Error al cerrar flujos: " + e);
}
finally
{
out.close();
}
}// fin de try
catch(IOException e)
{
System.out.println("Error al crear la fabrica de sockets: " + e);
}
}// fin del main
}//fin de class SecureChatClient