1
1
import os ,sys ,DH ,pickle ,binascii
2
2
import hashlib ,zlib ,json
3
+ from Auth import Auth
3
4
from random import randint
4
5
from cryptography .hazmat .primitives import serialization ,hashes
5
6
from cryptography .hazmat .primitives .asymmetric import padding
@@ -20,7 +21,9 @@ def __init__(self):
20
21
Purpose : 1) Initialise Connection object
21
22
2) Read server private key for future use
22
23
'''
23
- self .diffiObj = DH .DiffieHellman ()
24
+ self .__diffiObj = DH .DiffieHellman ()
25
+ self .__authDict = {}
26
+ self .__sessionKeyDict = {}
24
27
with open ("private_key.pem" , "rb" ) as key_file :
25
28
try :
26
29
self .__privateKey = serialization .load_pem_private_key (
@@ -32,7 +35,7 @@ def __init__(self):
32
35
sys .exit (0 )
33
36
34
37
35
- def __nowOnlineResponse (self ):
38
+ def __nowOnlineResponse (self , senderObj ):
36
39
'''
37
40
__nowOnlineResponse(None):
38
41
Input : None
@@ -47,11 +50,20 @@ def __nowOnlineResponse(self):
47
50
sha = hashlib .sha256 ()
48
51
sha .update (rand + str (t ))
49
52
guess = sha .digest ()
53
+ self .__authDict [senderObj ["user" ]] = Auth (str (t ))
50
54
obj = {"message-type" :"quiz" ,"challange" :rand ,"answer" :guess }
51
55
ret = pickle .dumps (obj )
52
56
return ret
53
57
54
58
def __findPasswordHashForUser (self ,user ):
59
+ '''
60
+ __findPasswordHashForUser(String):
61
+ Input : (String) UserName
62
+ Output : False -> If username not found
63
+ String -> Password hash
64
+ Purpose : Given a username searches if the user is registerd
65
+ and returns the username
66
+ '''
55
67
with open ("SERVER.conf" ) as json_file :
56
68
json_data = json .load (json_file )
57
69
if user .lower () in json_data :
@@ -64,30 +76,60 @@ def __challangeResponse(self,senderObj):
64
76
__challangeResponse(Object):
65
77
Input : Object {messageType:"quiz-response", encoded } (Response from server to challenge)
66
78
encoded -> {g^a mod p,response}s
67
- Output : Object {}
79
+ Output : String
68
80
Message format :
69
81
{messageType:"initiageSecret", sha256(g^ab mod p + g^bw mod p), g^b mod p}
70
82
Purpose : Send server public secret and augmented information
71
83
72
84
'''
73
85
74
- pubKey = self .diffiObj .gen_public_key () # This is (gb mod p)
75
- self .__sharedSecret = self .diffiObj .gen_shared_key (long (senderObj ["pubKey" ])) # This is (gab mop p)
76
- print "Shared Secret is : " , self .__sharedSecret
86
+ if senderObj ["user" ] in self .__authDict :
87
+ authInfo = self .__authDict [senderObj ["user" ]]
88
+ print "Server side quizz" ,authInfo .getQuizz ()
89
+ print "Client Side" , senderObj ["answer" ]
90
+ if authInfo .getQuizz () == str (senderObj ["answer" ]):
91
+ return self .__challangeResponseHelper (senderObj , authInfo )
92
+ else :
93
+ self .__authDict .pop (senderObj ["user" ])
94
+ return False
95
+
96
+ def __challangeResponseHelper (self ,senderObj ,authInfo ):
97
+ '''
98
+ __challangeResponseHelper(Object,Object):
99
+ Input : The Objectified stream data from user
100
+ and Authentication info on server
101
+ Output : String (Data to be send on wire)
102
+ Message format :
103
+ {messageType:"initiageSecret", sha256(g^ab mod p + g^bw mod p), g^b mod p}
104
+
105
+ '''
106
+ pubKey = self .__diffiObj .gen_public_key () # This is (gb mod p)
107
+ sharedSecret = self .__diffiObj .gen_shared_key (long (senderObj ["pubKey" ])) # This is (gab mop p)
108
+ authInfo .setResponse ()
109
+ authInfo .setSharedSecret (sharedSecret )
77
110
userPassHash = self .__findPasswordHashForUser (senderObj ["user" ])
78
111
if userPassHash :
79
- gpowbw = self .diffiObj .gen_gpowxw (pubKey ,userPassHash )
80
- sha = hashlib .sha256 ()
81
- sha . update ( str (gpowbw )+ str (self . __sharedSecret ))
82
- hash = int ( binascii . hexlify ( sha . digest ()), base = 16 )
112
+ gpowbw = self .__diffiObj .gen_gpowxw (pubKey , userPassHash )
113
+ hash256 = self . __genShaX ( hashlib .sha256 (), str ( gpowbw ) + str ( sharedSecret ) )
114
+ hash384 = self . __genShaX ( hashlib . sha384 (), str (gpowbw ) + str (sharedSecret ))
115
+ authInfo . setSha348 ( hash384 )
83
116
return pickle .dumps ({
84
- "messageType" : "initiateSecret" ,
85
- "hash" : hash ,
86
- "pubKey" : pubKey ,
87
- })
117
+ "messageType" : "initiateSecret" ,
118
+ "hash" : hash256 ,
119
+ "pubKey" : pubKey ,
120
+ })
88
121
return False
89
122
123
+ def __genShaX (self ,sha ,message ):
124
+ '''
125
+ __genShaX(Object,String):
126
+ Input : Object,Strint (THe sha object ie.sha256,384,512 and the message
127
+ to be encrypted)
128
+ Output : String (Returns the digest of the message)
90
129
130
+ '''
131
+ sha .update (message )
132
+ return int (binascii .hexlify (sha .digest ()), base = 16 )
91
133
92
134
def __decryptMessageUsingPrivateKey (self , message ):
93
135
'''
@@ -134,20 +176,31 @@ def __gen384Hash(self,gpowbw,sharedSecret):
134
176
hash = int (binascii .hexlify (sha .digest ()), base = 16 )
135
177
return hash
136
178
137
- def __completeAuth (self ,data ):
179
+ def __completeAuth (self ,senderObj ):
138
180
'''
139
181
__completeAuth(Object) :
140
- Input : Object
141
- Output :
142
-
143
- '''
144
- hash = data ["hash" ]
145
- #TODO : Verify if sha384 is same
146
- #TODO : Store sesssion key and complete this whole process
147
- print "Hurray Hurry"
182
+ Input : Object (The sender Objectified stream data from user
183
+ and Authentication info on server)
184
+ Output : False -> If sha384 doesnt match
185
+ True -> Password is verified and session key is established
186
+
187
+ '''
188
+ if senderObj ["user" ] in self .__authDict :
189
+ if senderObj ["hash" ] == self .__authDict [senderObj ["user" ]].getSha384 () :
190
+ print "User " + senderObj ["user" ] + " Connected"
191
+ self .__sessionKeyDict [senderObj ["user" ]] = self .__authDict [senderObj ["user" ]].getSharedSecret ()
192
+ return True
193
+ else :
194
+ self .__authDict .pop (senderObj ["user" ])
148
195
return False
149
196
150
197
def __loadPickledData (self ,message ):
198
+ '''
199
+ __loadPickledData(String):
200
+ Input : String (Stream data from socket)
201
+ Output : Object
202
+ Purpose : Convert the stream data to object
203
+ '''
151
204
try :
152
205
return pickle .loads (message )
153
206
except Exception as e :
@@ -182,13 +235,11 @@ def parseData(self,data,address):
182
235
decryptedResponse = self .__parseStreamData (data )
183
236
ret = False
184
237
if decryptedResponse ["messageType" ] == "now-online" :
185
- ret = self .__nowOnlineResponse ()
238
+ ret = self .__nowOnlineResponse (decryptedResponse )
186
239
elif decryptedResponse ["messageType" ] == "quiz-response" :
187
240
ret = self .__challangeResponse (decryptedResponse )
188
241
elif decryptedResponse ["messageType" ] == "complete" :
189
242
ret = self .__completeAuth (decryptedResponse )
190
243
if not ret :
191
244
self .__logErrors ("Response from sender" ,address )
192
- return "unknownMessage"
193
- else :
194
- return ret
245
+ return ret
0 commit comments