Changeset 18 in chevmsgr
- Timestamp:
- 11/23/15 00:36:26 (8 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/msg.cpp
r16 r18 73 73 } 74 74 75 std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms) 75 std::string Request::join(const std::string & id, const std::string & pw, const std::string & sms) const 76 76 { 77 77 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN); … … 89 89 90 90 obj[ProtocolType::PW] = pw; 91 92 return json::Serialize(obj); 93 } 94 95 std::string Request::secretWithRandom(const std::string & id, std::string & secret, const std::string & random) const 96 { 97 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SECRET); 98 99 obj[ProtocolType::ID] = id; 100 obj[ProtocolType::SECRET] = secret; 101 obj[ProtocolType::RANDOM] = random; 91 102 92 103 return json::Serialize(obj); -
trunk/msg.hpp
r17 r18 26 26 27 27 DECLARE_KEY(NONE); 28 DECLARE_KEY(SECRET); 29 DECLARE_KEY(RANDOM); 28 30 DECLARE_KEY(ID); 29 31 DECLARE_KEY(TYPE); … … 107 109 std::string sms(const std::string & phone) const; 108 110 109 std::string join(const std::string & id, const std::string & pw, const std::string & sms) ;111 std::string join(const std::string & id, const std::string & pw, const std::string & sms) const; 110 112 111 113 std::string login(const std::string & pw) const; 114 115 std::string secretWithRandom(const std::string & id, std::string & secret, const std::string & random) const; 112 116 113 117 std::string logout() const; … … 167 171 } 168 172 169 170 173 class SecureSocket 171 174 { … … 176 179 public : 177 180 SecureSocket() {} 178 SecureSocket(cf::network::tcp * sock, c f::bin & key)179 { 180 init(sock, key);181 } 182 183 void init(cf::network::tcp * sock, c f::bin & key)181 SecureSocket(cf::network::tcp * sock, const std::string & ip, const std::string & sms) 182 { 183 init(sock, ip, sms); 184 } 185 186 void init(cf::network::tcp * sock, const std::string & ip, const std::string & sms) 184 187 { 185 188 this->sock = sock; 189 190 cf::bin seed = sms + DELIMITER + ip; 191 cf::bin key = crypto().sha256(seed); 192 186 193 aria.setKey(key); 187 194 } -
trunk/msgclnt.cpp
r16 r18 1 #include "cf/codec.h" 1 2 2 3 #include "msgclnt.h" … … 6 7 7 8 #include <stdlib.h> 8 9 #include "cf/codec.h" 9 #include <time.h> 10 10 11 11 // -------------------------------------------------------------- … … 13 13 typedef struct SMessageQWorkerArg 14 14 { 15 cf::network::tcp * socket;15 SecureSocket * secureSocket; 16 16 MessageQ * messageQ; 17 17 } SMessageQWorkerArg; … … 36 36 size = 0; 37 37 38 cf::bin raw = inst->s ocket->receive();38 cf::bin raw = inst->secureSocket->receive(); 39 39 size = raw.size(); 40 40 … … 115 115 } 116 116 117 static std::string generateRandom() 118 { 119 unsigned int ret = 0; 120 int t = (int)time(NULL); 121 122 cf::bin b; 123 b.resize(sizeof(int)); 124 125 b.set((cf::uint8_t*)&t, sizeof(int)); 126 cf::bin s = crypto().sha256(b); 127 128 return cf::codec::hex::getInstance()->encode(s); 129 } 130 131 static inline std::string hashPassword(const std::string & pw) 132 { 133 return cf::codec::hex::getInstance()->encode(crypto().sha256(cf::bin(pw))); 134 } 135 136 static inline std::string getSecret(const std::string & msg, const std::string & sms, const std::string & random) 137 { 138 crypto crypt; 139 crypt.setKey(cf::bin(sms + DELIMITER + random)); 140 141 return crypt.encrypt(cf::bin(msg)).toString(); 142 } 143 117 144 // -------------------------------------------------------------- 118 145 … … 159 186 chev::~chev() 160 187 { 161 s ocket.close();188 secureSocket.close(); 162 189 listener.join(); 163 190 caller.join(); … … 179 206 return false; 180 207 181 arg->s ocket = &socket;208 arg->secureSocket = &secureSocket; 182 209 arg->messageQ = &messageQ; 183 210 … … 198 225 try 199 226 { 200 socket.send(request.join(id, pw, sms)); 227 std::string random = generateRandom(); 228 std::string msg = request.join(id, hashPassword(pw), sms); 229 std::string secret = getSecret(msg, sms, random); 230 socket.send(request.secretWithRandom(id, secret, random)); 201 231 202 232 return messageQ.pop(ProtocolType::JOIN).get<bool>(ProtocolType::RESULT); … … 210 240 } 211 241 212 bool chev::login(const std::string & id, const std::string & pw )242 bool chev::login(const std::string & id, const std::string & pw, const std::string & sms) 213 243 { 214 244 try … … 216 246 request.setUserID(id); 217 247 218 socket.send(request.login(pw)); 248 std::string random = generateRandom(); 249 std::string msg = request.login(hashPassword(pw)); 250 std::string secret = getSecret(msg, sms, random); 251 socket.send(request.secretWithRandom(id, secret, random)); 219 252 220 253 return messageQ.pop(ProtocolType::LOGIN).get<bool>(ProtocolType::RESULT); … … 248 281 try 249 282 { 250 s ocket.send(request.addFriend(id));283 secureSocket.send(request.addFriend(id)); 251 284 252 285 return messageQ.pop(ProtocolType::ADD_FRIEND).get<bool>(ProtocolType::RESULT); … … 266 299 try 267 300 { 268 s ocket.send(request.getFriendList());301 secureSocket.send(request.getFriendList()); 269 302 270 303 friendList = messageQ.pop(ProtocolType::FRIEND_LIST).getFriendList(); … … 289 322 if (sessionMap.find(concat) == sessionMap.end()) 290 323 { 291 s ocket.send(request.openSession(toList));324 secureSocket.send(request.openSession(toList)); 292 325 293 326 Protocol::Message message = messageQ.pop(ProtocolType::OPEN_SESSION); … … 319 352 c.message = cf::codec::hex::getInstance()->encode(c.message); 320 353 321 s ocket.send(request.tell(c.sessid, c.message, c.sensitive));354 secureSocket.send(request.tell(c.sessid, c.message, c.sensitive)); 322 355 323 356 return messageQ.pop(ProtocolType::TELL).get<bool>(ProtocolType::RESULT); -
trunk/msgclnt.h
r17 r18 55 55 56 56 MessageQ messageQ; 57 SecureSocket secureSocket; 57 58 58 59 std::map<std::string, std::string> sessionMap; … … 71 72 bool join(const std::string & id, const std::string & pw, const std::string & sms); 72 73 73 bool login(const std::string & id, const std::string & pw );74 bool login(const std::string & id, const std::string & pw, const std::string & sms); 74 75 75 76 bool addFriend(const std::string & id); -
trunk/msgsrv.cpp
r17 r18 130 130 131 131 result = sqlite3_exec(db, query.c_str(), cb, userArg, &errMsg); 132 LOG("[SQL] " + query); 132 133 133 134 if (result != SQLITE_OK) … … 286 287 static void logout(const std::string & id) 287 288 { 289 gOnlineUsers[id].close(); 288 290 gOnlineUsers.erase(id); 289 291 LOG(STR(id << " was logged out")); … … 339 341 } 340 342 343 static Protocol::Message getSecret(const Protocol::Message & parser, const std::string & sms) 344 { 345 std::string secret = parser.get<std::string>(ProtocolType::SECRET); 346 std::string random = parser.get<std::string>(ProtocolType::RANDOM); 347 348 crypto crypt; 349 crypt.setKey(cf::bin(sms + DELIMITER + random)); 350 351 std::string auth = crypt.decrypt(cf::bin(secret)).toString(); 352 Protocol::Message authParser; 353 354 authParser.parse(auth); 355 356 return authParser; 357 } 358 341 359 static bool join(const Protocol::Message & parser, const std::string & sms, const std::string & address) 342 360 throw (cf::exception) … … 345 363 THROW_EXCEPTION("SMS is not same"); 346 364 347 std::string id = parser.get<std::string>(ProtocolType::ID); 348 std::string pw = parser.get<std::string>(ProtocolType::PW); 365 Protocol::Message & authParser = getSecret(parser, sms); 366 367 std::string id = authParser.get<std::string>(ProtocolType::ID); 368 std::string pw = authParser.get<std::string>(ProtocolType::PW); 369 std::string rcvdSMS = authParser.get<std::string>(ProtocolType::SMS); 370 371 if (sms != rcvdSMS) 372 THROW_EXCEPTION("invalid sms code"); 349 373 350 374 return dbmgr.join(id, pw, sms, address); … … 354 378 throw (cf::exception) 355 379 { 356 std::string id = parser.get<std::string>(ProtocolType::ID); 357 std::string pw = parser.get<std::string>(ProtocolType::PW); 380 Protocol::Message & authParser = getSecret(parser, sms); 381 382 std::string id = authParser.get<std::string>(ProtocolType::ID); 383 std::string pw = authParser.get<std::string>(ProtocolType::PW); 358 384 359 385 bool result = dbmgr.login(id, pw); 360 386 if (result) 361 { 362 cf::bin seed = sms + DELIMITER + ip; 363 cf::bin key = crypto().sha256(seed); 364 365 gOnlineUsers[id] = SecureSocket(&sock, key); 366 } 387 gOnlineUsers[id] = SecureSocket(&sock, ip, sms); 367 388 368 389 return result; … … 438 459 } 439 460 440 static std::string keyExchange(const std::string sms, const std::string address) 441 { 442 cf::bin sessKey; 443 444 sessKey = crypto().sha256(cf::bin(sms + address)); 445 446 return cf::codec::hex::getInstance()->encode(sessKey); 447 } 448 449 static std::string workerInitiator(cf::network::tcp & sock) 461 static std::string authenticator(cf::network::tcp & sock) 450 462 { 451 463 Protocol::Message parser; … … 495 507 } 496 508 497 static int worker(void * arg)509 static int deliverer(void * arg) 498 510 { 499 511 Runner * runner = reinterpret_cast<Runner *>(arg); … … 504 516 try 505 517 { 506 id = workerInitiator(*sock);518 id = authenticator(*sock); 507 519 508 520 Protocol::Response response; … … 563 575 { 564 576 cf::network::tcp client = sock.accept(); 565 Runner * runner = new(std::nothrow) Runner(client, worker);577 Runner * runner = new(std::nothrow) Runner(client, deliverer); 566 578 if (!runner) 567 579 THROW_EXCEPTION("cannot create thread argument"); -
trunk/msvc14/testClient/test.cpp
r16 r18 86 86 //std::getline(std::cin, sms); 87 87 88 T(c.login(id, pw ));88 T(c.login(id, pw, sms)); 89 89 90 90 while (true) -
trunk/msvc14/testClient/testClient.vcxproj
r14 r18 97 97 <SubSystem>Console</SubSystem> 98 98 <GenerateDebugInformation>true</GenerateDebugInformation> 99 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\c f++.lib;%(AdditionalDependencies)</AdditionalDependencies>99 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies> 100 100 </Link> 101 101 </ItemDefinitionGroup> … … 111 111 <SubSystem>Console</SubSystem> 112 112 <GenerateDebugInformation>true</GenerateDebugInformation> 113 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\c f++.lib;%(AdditionalDependencies)</AdditionalDependencies>113 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies> 114 114 </Link> 115 115 </ItemDefinitionGroup> … … 129 129 <EnableCOMDATFolding>true</EnableCOMDATFolding> 130 130 <OptimizeReferences>true</OptimizeReferences> 131 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\c f++.lib;%(AdditionalDependencies)</AdditionalDependencies>131 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies> 132 132 </Link> 133 133 </ItemDefinitionGroup> … … 147 147 <EnableCOMDATFolding>true</EnableCOMDATFolding> 148 148 <OptimizeReferences>true</OptimizeReferences> 149 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\c f++.lib;%(AdditionalDependencies)</AdditionalDependencies>149 <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies> 150 150 </Link> 151 151 </ItemDefinitionGroup>
Note:
See TracChangeset
for help on using the changeset viewer.