forked from lolsteve/CPEN442-VPN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
171 lines (135 loc) · 5.16 KB
/
client.py
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
import sys
import socket
from DiffieHellman import DiffieHellman
from AESCipher import AESCipher
from Crypto import Random
class client(object):
def send(self, data):
try:
# Connect to server and send data
self.sock.sendall(data + "\n")
except:
self.sock.close()
def close(self):
self.sock.close()
def __init__(self, address, port):
self.sessionKey = ''
self.address = address, port
# Create a socket (SOCK_STREAM means a TCP socket)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
self.sock.connect(self.address)
except:
self.close()
# Wait to receive
def waitToRec(self):
#wait on socket to get data
try:
return self.sock.recv(1024)
except:
self.close()
#key exchange for client
def DH(self, sharedKey):
print '\n############### STARTING D-H ##################'
# Create AES object with shared key
cipher = AESCipher(sharedKey)
#generate key to send to server
myDiffieHellman = DiffieHellman()
print 'g^a mod p value is: ', myDiffieHellman.public_key
print '\n'
#send key to server
sendDH = cipher.encrypt(str(myDiffieHellman.public_key))
print 'Sending encrypted value: ', sendDH.encode('hex')
self.send(str(sendDH))
print '\n'
recvDH = self.waitToRec()
#decrypt received DH value
reply = cipher.decrypt(recvDH)
print 'Received encrypted value: ', recvDH.encode('hex')
print '\n'
print 'g^b mod p value is: ', reply
print '\n'
#calculate session key
myDiffieHellman.calc_shared_key(long(reply))
print "Calculated session key:", myDiffieHellman.key
self.sessionKey = myDiffieHellman.key
print '################## D-H OVER ###################\n'
def sendMessage(self):
#generate cipher for session
sessionCipher = AESCipher(str(self.sessionKey))
print 'VPN Connected'
while True:
try:
#prompt client for message
dataCl = raw_input('')
#encrypt message
cipherText = sessionCipher.encrypt(dataCl)
print '\n############## NOW SENDING MESSAGE ############'
print 'Sending encrypted message:', cipherText.encode('hex')
print '############## MESSAGE SENT ###################\n'
#send message to server
self.send(cipherText)
except:
print 'Exiting'
self.close()
sys.exit(1)
#return
def waitForMessage(self):
sessionCipher = AESCipher(str(self.sessionKey))
while True:
try:
reply = self.sock.recv(1024).strip()
#decrypt message gotten from server
plainText = sessionCipher.decrypt(reply)
print '\n$$$$$$$$$$$$$$ RECIEVING MESSAGE $$$$$$$$$$$$$$'
print 'Encrypted message received: ', reply.encode('hex')
print 'Decrypted message:', plainText
print '$$$$$$$$$$$$$$ END OF MESSAGE $$$$$$$$$$$$$$$$$\n'
except:
print 'Connection closed'
return
def mutAuthClient(self, sharedKey):
try:
# Create AES object with shared key
cipher = AESCipher(sharedKey)
# Client's challenge to server
Ra = Random.new().read(16)
print 'Ra:', Ra.encode('hex')
message= 'Client'+ Ra
print 'Sending message:', message
self.send(message)
# Wait for response from server
reply = self.waitToRec()
print 'Received:', reply.encode('hex')
# Decrypt response
plainText = cipher.decrypt(reply)
print 'Decrypted:', plainText
# Obtain Ra and Rb from response
RaTest = plainText[-32:-16]
print 'Ra received:', RaTest.encode('hex')
Rb=plainText[-16:]
print 'Rb received:', Rb.encode('hex')
# Compare received Ra with sent Ra
if RaTest!= Ra:
print 'Different Ra received: mutual auth failed'
self.close()
sys.exit(1)
# Encrypt "name","Rb" with shared key and send it
finalReply = 'Client' + Rb
print 'Encrypting reply:', finalReply
finalCipher = cipher.encrypt(finalReply)
print 'Sending cipher:', finalCipher.encode('hex')
self.send(finalCipher)
# Wait for response from server
replyauth = self.waitToRec()
if replyauth == 'mutual auth failed':
print 'Server denied authentication: mutual auth failed'
self.close()
sys.exit(1)
else:
print 'CLIENT: mutual auth passed'
except:
print 'Mutual auth failed'
self.close()
sys.exit(1)