-
Notifications
You must be signed in to change notification settings - Fork 0
/
StealthNetComms.java
executable file
·338 lines (290 loc) · 9.25 KB
/
StealthNetComms.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
/***********************************************************************************
* ELEC5616
* Computer and Network Security, The University of Sydney
*
* PROJECT: StealthNet
* FILENAME: StealthNetComms.java
* AUTHORS: Stephen Gould, Matt Barrie, Ryan Junee
* DESCRIPTION: Implementation of StealthNet Communications for ELEC5616
* programming assignment.
* This code has been written for the purposes of teaching
* cryptography and computer security. It is to be used as
* a demonstration only. No attempt has been made to optimise
* the source code.
* VERSION: 1.0
* IMPLEMENTS: initiateSession();
* acceptSession();
* terminateSession();
* sendPacket();
* recvPacket();
* recvReady();
*
* REVISION HISTORY:
*
**********************************************************************************/
/* Import Libraries **********************************************************/
import java.net.*;
import java.security.SecureRandom;
import java.io.*;
/* StealthNetComms class *****************************************************/
public class StealthNetComms {
private static final char[] HEXTABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final int NONCE_SIZE = 32;
public static String getDefaultServerName(){
return "localhost";
}
public static int getDefaultServerPort(){
return 5616;
}
private Socket commsSocket; // communications socket
private PrintWriter dataOut; // output data stream
private BufferedReader dataIn; // input data stream
public final SecureLayer secureLayer;
SecureRandom nonce;
public static class CommsException extends IOException{
public CommsException(String message){
super(message);
}
}
/**public StealthNetComms() {
commsSocket = null;
dataIn = null;
dataOut = null;
secureLayer = new SecureLayer();
try {
secureLayer.selfInitRSA();
} catch (Exception e) {
System.out.println("Problem initialising RSA.");
//TODO Do something better here?
}
}**/
public StealthNetComms(SecureLayer sl) {
commsSocket = null;
dataIn = null;
dataOut = null;
secureLayer = sl;
}
protected void finalize() throws IOException {
if (dataOut != null)
dataOut.close();
if (dataIn != null)
dataIn.close();
if (commsSocket != null)
commsSocket.close();
}
public boolean initiateSession(Socket socket) {
try {
System.out.println("Init Session");
commsSocket = socket;
dataOut = new PrintWriter(commsSocket.getOutputStream(), true);
dataIn = new BufferedReader(new InputStreamReader(
commsSocket.getInputStream()));
byte[] data;
/* Get public key */
data = recvData();
secureLayer.initRSAYou(data);
/* Send public key */
sendData(secureLayer.descMyPublic());
/* Get MAC */
data = recvData();
secureLayer.initMac(secureLayer.getRSADecrypted(data), true);
/* Send AES */
secureLayer.selfInitAES();
byte[] msg = SecureLayer.byteJoin(secureLayer.descAES(), secureLayer.descIV());
msg = SecureLayer.byteJoin(msg, secureLayer.getRSADecrypted(data));
sendData(secureLayer.getRSAEncrypted(msg));
/* Receive IV */
data = recvData();
if (!byteEqual(secureLayer.getRSADecrypted(data), secureLayer.descIV()))
{
throw new CommsException("IVs do not match!");
}
byte[] seed = new SecureRandom().generateSeed(32);
System.out.println(HashStalk.hexify(seed));
nonce = new SecureRandom(seed);
sendData(secureLayer.getRSAEncrypted(seed));
} catch (Exception e) {
System.err.println("Connection terminated.");
return false;
}
try{
byte[] cosign = SecureLayer.stob("the cake is a lie");
byte[] signed = secureLayer.countersign(cosign, true);
this.sendPacket(StealthNetPacket.CMD_MSG, signed);
}
catch(Exception e){
e.printStackTrace();
System.err.println("Signing test failed");
}
return true;
}
public boolean acceptSession(Socket socket) {
try {
System.out.println("Accept Session");
commsSocket = socket;
dataOut = new PrintWriter(commsSocket.getOutputStream(), true);
dataIn = new BufferedReader(new InputStreamReader(
commsSocket.getInputStream()));
byte[] data;
byte[] key = new byte[16];
byte[] iv = new byte[16];
byte[] tmp = new byte[32];
byte[] mac = new byte[32];
/* Send public key */
sendData(secureLayer.descMyPublic());
/* Get public key */
data = recvData();
secureLayer.initRSAYou(data);
/* Send MAC */
secureLayer.selfInitMac(true);
sendData(secureLayer.getRSAEncrypted(secureLayer.descMAC(true)));
/* Get AES */
data = recvData();
SecureLayer.byteSplit(secureLayer.getRSADecrypted(data), tmp, mac);
SecureLayer.byteSplit(tmp, key, iv);
secureLayer.initAES(key, iv);
/* Check received MAC is correct */
if (!byteEqual(mac, secureLayer.descMAC(true)))
{
throw new CommsException("MACs do not match!");
}
/* Send decrypted IV */
sendData(secureLayer.getRSAEncrypted(iv));
data = recvData();
System.out.println(HashStalk.hexify(secureLayer.getRSADecrypted(data)));
nonce = new SecureRandom(secureLayer.getRSADecrypted(data));
} catch (Exception e) {
System.err.println("Connection terminated.");
return false;
}
try{
StealthNetPacket snp = this.recvPacket();
System.out.println("signing test: " + snp.command + ", " + StealthNetPacket.CMD_MSG);
byte[] signed = snp.data;
byte[] unsigned = secureLayer.countersign(signed, false);
System.out.println(SecureLayer.btos(unsigned));
}
catch(Exception e){
e.printStackTrace();
System.err.println("signing test failed");
}
return true;
}
public boolean terminateSession() {
try {
System.out.println("Terminate Session");
if (commsSocket == null)
return false;
dataIn.close();
dataOut.close();
commsSocket.close();
commsSocket = null;
} catch (Exception e) {
return false;
}
return true;
}
public boolean sendPacket(byte command) {
return sendPacket(command, new byte[0]);
}
public boolean sendPacket(byte command, String data) {
return sendPacket(command, data.getBytes());
}
public boolean sendPacket(byte command, byte[] data) {
return sendPacket(command, data, data.length);
}
public boolean sendPacket(byte command, byte[] data, int size) {
StealthNetPacket pckt = new StealthNetPacket();
pckt.command = command;
pckt.data = new byte[size];
System.arraycopy(data, 0, pckt.data, 0, size);
return sendPacket(pckt);
}
public boolean sendPacket(StealthNetPacket pckt) {
if (dataOut == null) return false;
try {
byte[] n = new byte[NONCE_SIZE];
nonce.nextBytes(n);
byte[] clear = SecureLayer.byteJoin(n, pckt.toBytes());
sendData(secureLayer.sendAES(clear));
} catch (SecureLayer.SecurityException e) {
System.out.println("Security failed on comms");
}
return true;
}
public StealthNetPacket recvPacket() throws IOException {
byte[] data = secureLayer.receiveAES(recvData());
byte[] ctr = new byte[4];
byte[] their_nonce = new byte[NONCE_SIZE];
byte[] my_nonce = new byte[NONCE_SIZE];
byte[] rest = new byte[data.length - NONCE_SIZE];
nonce.nextBytes(my_nonce);
SecureLayer.byteSplit(data, their_nonce, rest);
if (!byteEqual(my_nonce, their_nonce))
{
throw new IOException("Replay attack detected");
}
return new StealthNetPacket(rest);
}
private void sendData(byte[] data)
{
dataOut.println(hexEncode(data));
}
private byte[] recvData() throws IOException
{
return hexDecode(dataIn.readLine());
}
public static String hexEncode(byte[] pckt){
StringBuffer buf = new StringBuffer();
for(int i=0; i<pckt.length; i++){
int highByte = (pckt[i] >= 0) ? pckt[i] : 256 + pckt[i];
int lowByte = highByte & 15;
highByte /= 16;
buf.append(HEXTABLE[highByte]);
buf.append(HEXTABLE[lowByte]);
}
return buf.toString();
}
public static byte[] hexDecode(String hexenc){
if(hexenc.length() % 2 != 0){
System.err.println("WTF! SOme idiot doesnt know how to encode hex strings");
return null;
}
byte[] ret = new byte[hexenc.length()/2];
for(int i=0; i<ret.length; i++){
String abyte = hexenc.substring(2*i, 2*(i+1));//up to but not including arg2
ret[i] = (byte)Integer.parseInt(abyte, 16);
}
return ret;
}
public static final byte[] itob(int value) {
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
public static final int btoi(byte[] b) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i] & 0x000000FF) << shift;
}
return value;
}
public boolean recvReady() throws IOException {
return dataIn.ready();
}
public static boolean byteEqual(byte[] a, byte[] b)
{
if (a.length != b.length) return false;
for (int i = 0; i != a.length; ++i)
{
if (a[i] != b[i]) return false;
}
return true;
}
}
/******************************************************************************
* END OF FILE: StealthNetComms.java
*****************************************************************************/