-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
149 lines (123 loc) · 4.25 KB
/
server.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
'''
Simple socket server using threads
'''
import socket
import sys
from thread import *
from dh import DH
import random
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
import base64
secret_match = 123456
secret_other = 123123
possible_secrets = [secret_match, secret_other]
#Load key
bobPrivKey = RSA.importKey(open('bob.priv').read())
signer = PKCS1_v1_5.new(bobPrivKey)
alicePubKey = RSA.importKey(open('alice.pub').read())
verifier = PKCS1_v1_5.new(alicePubKey)
def signAndSend(conn, msg):
signature = signer.sign(SHA256.new(msg))
conn.sendall(msg + ',' + base64.b64encode(signature))
def verifyMsg(data):
elements = data.split(',')[:-1]
msg = ','.join(elements)
if not verifier.verify(SHA256.new(msg), base64.b64decode(data.split(',')[-1])):
print('Protocol failure! Bad RSA signature!')
sys.exit()
HOST = '' # Symbolic name meaning all available interfaces
PORT = 4321 # Arbitrary non-privileged port
exchange = DH()
PRIME = exchange.getPrime()
GENERATOR = exchange.getGen()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
#infinite loop so that function do not terminate and thread do not end.
b = exchange.getRandomElement()
b2 = exchange.getRandomElement()
d = exchange.getRandomElement()
m1 = str(exchange.getPublic(b))
m2 = str(exchange.getPublic(b2))
sb = random.choice(possible_secrets)
print "BOB'S SECRET: %d\n---------------\n" % (sb)
while True:
#Step 2 respond
data = conn.recv(1024)
print 'received ' + data
verifyMsg(data)
data = ','.join(data.split(',')[:-1])
if not data:
break
k = int(exchange.getShared(b, int(data)))
print 'secret: %d' % (k)
print 'Step 2 send: %s' % (m1)
signAndSend(conn, m1)
#conn.sendall(m1)
#Step 3 respond
data = conn.recv(1024)
print 'received ' + data
verifyMsg(data)
data = ','.join(data.split(',')[:-1])
if not data:
break
k2 = int(exchange.getShared(b2, int(data)))
print 'secret: %d' % (k2)
print 'Step 3 send: %s' % (m2)
signAndSend(conn, m2)
#conn.sendall(m2)
#Step 4 receive, step 5 respond
data = conn.recv(1024)
print 'received ' + data
verifyMsg(data)
data = ','.join(data.split(',')[:-1])
if not data or len(data.split(',')) != 2:
break
kc = data.split(',')[0]
kd = (k ** d) % PRIME
challengeA = int(data.split(',')[1])
print 'k2: %d, sb: %d, d: %d' % (k2,sb,d)
challengeB = ((k2 ** sb) * (GENERATOR ** d)) % PRIME
challengeBInv = (challengeB ** (PRIME - 2)) % PRIME
print 'Step 5 send: %d' % (challengeBInv)
m5 = str(int(challengeBInv))
signAndSend(conn, m5)
#conn.sendall(m5)
#Step 6 receive, step 7 respond
data = conn.recv(1024)
print 'received ' + data
verifyMsg(data)
data = ','.join(data.split(',')[:-1])
kdInv = (kd ** (PRIME - 2)) % PRIME
k3Partial = (((challengeA * challengeBInv) % PRIME) ** b) % PRIME
m7 = str(k3Partial) + ',' + str(int(kdInv))
print 'Step 7: %s' % (m7)
signAndSend(conn, m7)
#conn.sendall(m7)
k3 = int(exchange.getShared(b, int(data)))
print 'secret: %d' % (k3)
break
conn.close()
#came out of loop
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()