Changeset 18 in chevmsgr


Ignore:
Timestamp:
11/23/15 00:36:26 (8 years ago)
Author:
cheese
Message:

random number를 이용한 인증방식 적용

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/msg.cpp

    r16 r18  
    7373    }
    7474
    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
    7676    {
    7777        DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::JOIN);
     
    8989
    9090        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;
    91102
    92103        return json::Serialize(obj);
  • trunk/msg.hpp

    r17 r18  
    2626
    2727    DECLARE_KEY(NONE);
     28    DECLARE_KEY(SECRET);
     29    DECLARE_KEY(RANDOM);
    2830    DECLARE_KEY(ID);
    2931    DECLARE_KEY(TYPE);
     
    107109        std::string sms(const std::string & phone) const;
    108110
    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;
    110112
    111113        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;
    112116
    113117        std::string logout() const;
     
    167171}
    168172
    169 
    170173class SecureSocket
    171174{
     
    176179public :
    177180    SecureSocket() {}
    178     SecureSocket(cf::network::tcp * sock, cf::bin & key)
    179     {
    180         init(sock, key);
    181     }
    182 
    183     void init(cf::network::tcp * sock, cf::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)
    184187    {
    185188        this->sock = sock;
     189
     190        cf::bin seed = sms + DELIMITER + ip;
     191        cf::bin key = crypto().sha256(seed);
     192
    186193        aria.setKey(key);
    187194    }
  • trunk/msgclnt.cpp

    r16 r18  
     1#include "cf/codec.h"
    12
    23#include "msgclnt.h"
     
    67
    78#include <stdlib.h>
    8 
    9 #include "cf/codec.h"
     9#include <time.h>
    1010
    1111// --------------------------------------------------------------
     
    1313typedef struct SMessageQWorkerArg
    1414{
    15     cf::network::tcp * socket;
     15    SecureSocket * secureSocket;
    1616    MessageQ * messageQ;
    1717} SMessageQWorkerArg;
     
    3636            size = 0;
    3737
    38             cf::bin raw = inst->socket->receive();
     38            cf::bin raw = inst->secureSocket->receive();
    3939            size = raw.size();
    4040
     
    115115}
    116116
     117static 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
     131static inline std::string hashPassword(const std::string & pw)
     132{
     133    return cf::codec::hex::getInstance()->encode(crypto().sha256(cf::bin(pw)));
     134}
     135
     136static 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
    117144// --------------------------------------------------------------
    118145
     
    159186chev::~chev()
    160187{
    161     socket.close();
     188    secureSocket.close();
    162189    listener.join();
    163190    caller.join();
     
    179206            return false;
    180207
    181         arg->socket = &socket;
     208        arg->secureSocket = &secureSocket;
    182209        arg->messageQ = &messageQ;
    183210
     
    198225    try
    199226    {
    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));
    201231
    202232        return messageQ.pop(ProtocolType::JOIN).get<bool>(ProtocolType::RESULT);
     
    210240}
    211241
    212 bool chev::login(const std::string & id, const std::string & pw)
     242bool chev::login(const std::string & id, const std::string & pw, const std::string & sms)
    213243{
    214244    try
     
    216246        request.setUserID(id);
    217247
    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));
    219252
    220253        return messageQ.pop(ProtocolType::LOGIN).get<bool>(ProtocolType::RESULT);
     
    248281    try
    249282    {
    250         socket.send(request.addFriend(id));
     283        secureSocket.send(request.addFriend(id));
    251284
    252285        return messageQ.pop(ProtocolType::ADD_FRIEND).get<bool>(ProtocolType::RESULT);
     
    266299    try
    267300    {
    268         socket.send(request.getFriendList());
     301        secureSocket.send(request.getFriendList());
    269302
    270303        friendList = messageQ.pop(ProtocolType::FRIEND_LIST).getFriendList();
     
    289322        if (sessionMap.find(concat) == sessionMap.end())
    290323        {
    291             socket.send(request.openSession(toList));
     324            secureSocket.send(request.openSession(toList));
    292325
    293326            Protocol::Message message = messageQ.pop(ProtocolType::OPEN_SESSION);
     
    319352            c.message = cf::codec::hex::getInstance()->encode(c.message);
    320353
    321         socket.send(request.tell(c.sessid, c.message, c.sensitive));
     354        secureSocket.send(request.tell(c.sessid, c.message, c.sensitive));
    322355
    323356        return messageQ.pop(ProtocolType::TELL).get<bool>(ProtocolType::RESULT);
  • trunk/msgclnt.h

    r17 r18  
    5555
    5656    MessageQ            messageQ;
     57    SecureSocket        secureSocket;
    5758
    5859    std::map<std::string, std::string>  sessionMap;
     
    7172    bool join(const std::string & id, const std::string & pw, const std::string & sms);
    7273
    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);
    7475
    7576    bool addFriend(const std::string & id);
  • trunk/msgsrv.cpp

    r17 r18  
    130130
    131131        result = sqlite3_exec(db, query.c_str(), cb, userArg, &errMsg);
     132        LOG("[SQL] " + query);
    132133
    133134        if (result != SQLITE_OK)
     
    286287static void logout(const std::string & id)
    287288{
     289    gOnlineUsers[id].close();
    288290    gOnlineUsers.erase(id);
    289291    LOG(STR(id << " was logged out"));
     
    339341}
    340342
     343static 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
    341359static bool join(const Protocol::Message & parser, const std::string & sms, const std::string & address)
    342360    throw (cf::exception)
     
    345363        THROW_EXCEPTION("SMS is not same");
    346364
    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");
    349373       
    350374    return dbmgr.join(id, pw, sms, address);
     
    354378    throw (cf::exception)
    355379{
    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);
    358384
    359385    bool result = dbmgr.login(id, pw);
    360386    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);
    367388
    368389    return result;
     
    438459}
    439460
    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)
     461static std::string authenticator(cf::network::tcp & sock)
    450462{
    451463    Protocol::Message parser;
     
    495507}
    496508
    497 static int worker(void * arg)
     509static int deliverer(void * arg)
    498510{
    499511    Runner * runner = reinterpret_cast<Runner *>(arg);
     
    504516    try
    505517    {
    506         id = workerInitiator(*sock);
     518        id = authenticator(*sock);
    507519
    508520        Protocol::Response response;
     
    563575        {
    564576            cf::network::tcp client = sock.accept();
    565             Runner * runner = new(std::nothrow) Runner(client, worker);
     577            Runner * runner = new(std::nothrow) Runner(client, deliverer);
    566578            if (!runner)
    567579                THROW_EXCEPTION("cannot create thread argument");
  • trunk/msvc14/testClient/test.cpp

    r16 r18  
    8686    //std::getline(std::cin, sms);
    8787
    88     T(c.login(id, pw));
     88    T(c.login(id, pw, sms));
    8989
    9090    while (true)
  • trunk/msvc14/testClient/testClient.vcxproj

    r14 r18  
    9797      <SubSystem>Console</SubSystem>
    9898      <GenerateDebugInformation>true</GenerateDebugInformation>
    99       <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
     99      <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
    100100    </Link>
    101101  </ItemDefinitionGroup>
     
    111111      <SubSystem>Console</SubSystem>
    112112      <GenerateDebugInformation>true</GenerateDebugInformation>
    113       <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
     113      <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
    114114    </Link>
    115115  </ItemDefinitionGroup>
     
    129129      <EnableCOMDATFolding>true</EnableCOMDATFolding>
    130130      <OptimizeReferences>true</OptimizeReferences>
    131       <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
     131      <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
    132132    </Link>
    133133  </ItemDefinitionGroup>
     
    147147      <EnableCOMDATFolding>true</EnableCOMDATFolding>
    148148      <OptimizeReferences>true</OptimizeReferences>
    149       <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
     149      <AdditionalDependencies>$(OutDir)\client.lib;$(OutDir)\crypto.lib;$(OutDir)\cf++.lib;%(AdditionalDependencies)</AdditionalDependencies>
    150150    </Link>
    151151  </ItemDefinitionGroup>
Note: See TracChangeset for help on using the changeset viewer.