· 6 years ago · Feb 22, 2020, 01:34 AM
1private byte[] runSupportClient(int clientNonce) { //this client nonce is the server nonce from step 4 in tbe original client
2 Socket newConnection;
3 try {
4 newConnection = new Socket("localhost", portNo);
5 } catch (Exception e) {
6 System.out.println(e);
7 return null;
8 }
9 DataOutputStream outStream;
10 DataInputStream inStream;
11 try {
12 outStream = new DataOutputStream(newConnection.getOutputStream());
13 inStream = new DataInputStream(newConnection.getInputStream());
14 try {
15 // Use crypto API to calculate x & g^x
16 DHParameterSpec dhSpec = new DHParameterSpec(p,g);
17 KeyPairGenerator diffieHellmanGen = KeyPairGenerator.getInstance("DiffieHellman");
18 diffieHellmanGen.initialize(dhSpec);
19 KeyPair serverPair = diffieHellmanGen.generateKeyPair();
20 PrivateKey x = serverPair.getPrivate();
21 PublicKey gToTheX = serverPair.getPublic();
22
23 //Protocol message 1
24 //PublicKey cert can vary in length, therefore the length is sent first
25 outStream.writeInt(gToTheX.getEncoded().length);
26 outStream.write(gToTheX.getEncoded());
27 if (debug) System.out.println("g^x len: "+gToTheX.getEncoded().length);
28 if (debug) System.out.println("g^x cert: "+byteArrayToHexString(gToTheX.getEncoded()));
29
30
31 //Protocol message 2
32 int publicKeyLen = inStream.readInt();
33 byte[] message2 = new byte[publicKeyLen];
34 inStream.read(message2);
35 KeyFactory keyfactoryDH = KeyFactory.getInstance("DH");
36 X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(message2);
37 PublicKey gToTheY = keyfactoryDH.generatePublic(x509Spec);
38 if (debug) System.out.println("g^y len: "+publicKeyLen);
39 if (debug) System.out.println("g^y cert: "+byteArrayToHexString(gToTheY.getEncoded()));
40
41 //Calculate session key
42 // This method sets decAESsessionCipher & encAESsessionCipher
43 calculateSessionKeySupport(x, gToTheY);
44
45 //Protocol Step 3
46 byte[] encryptedClientNonce = encAESsessionSupport.doFinal(BigInteger.valueOf(clientNonce).toByteArray());
47 outStream.write(encryptedClientNonce);
48
49 if (debug) System.out.println("Client nonce: "+clientNonce);
50
51 //Protocol Step 4
52 byte[] encryptedMsg4 = new byte[32];
53 inStream.read(encryptedMsg4);
54 byte[] decryptedMsg4 = decAESsessionSupport.doFinal(encryptedMsg4);
55
56 byte[] encryptedClientNonceInc = new byte[16];
57 System.arraycopy(decryptedMsg4, 0, encryptedClientNonceInc, 0, 16);
58
59 newConnection.close();
60 System.out.println("Returning now");
61 return encryptedClientNonceInc;
62
63
64 } catch (IllegalBlockSizeException e) {
65 outStream.write("Bad block size".getBytes());
66 if (debug) System.out.println("Doh "+e);
67 myConnection.close();
68 return null;
69 } catch (BadPaddingException e) {
70 outStream.write("Bad padding".getBytes());
71 myConnection.close();
72 if (debug) System.out.println("Doh "+e);
73 return null;
74 } catch (InvalidKeySpecException e) {
75 outStream.write("Bad certificate for PublicKey (g^x)".getBytes());
76 myConnection.close();
77 if (debug) System.out.println("Doh "+e);
78 return null;
79 } catch (NoSuchAlgorithmException e) {
80 System.out.println(e);// Not going to happen, AES hard wired
81 } catch (InvalidAlgorithmParameterException e) {
82 System.out.println(e);// Not going to happen, DH Spec hard wired
83 e.printStackTrace();
84 }
85
86 } catch (IOException e) {
87 //Nothing we can do about this one
88 if (debug) System.out.println("Your wi-fi sucks: "+e);
89 return null;
90 }
91 return null;
92 }