Changeset 31 in chevmsgr


Ignore:
Timestamp:
12/01/15 00:36:56 (8 years ago)
Author:
cheese
Message:

키 ㅋ

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/msg.hpp

    r24 r31  
    1010#include <string>
    1111#include <vector>
     12#include <time.h>
    1213
    1314typedef struct SFriend
     
    4445    DECLARE_KEY(FROM);
    4546    DECLARE_KEY(SESSION_ID);
     47    DECLARE_KEY(SESSION_KEY);
    4648    DECLARE_KEY(MESSAGE);
    4749    DECLARE_KEY(SENSITIVE);
     
    171173}
    172174
     175static cf::bin generateRandom()
     176{
     177    int t = (int)time(NULL);
     178
     179    cf::bin b;
     180    b.resize(sizeof(int));
     181
     182    b.set((cf::uint8_t*)&t, sizeof(int));
     183    return crypto().sha256(b);
     184}
     185
    173186class SecureSocket
    174187{
    175 private :
     188private:
    176189    cf::network::tcp * sock;
    177190    crypto aria;
    178191
    179 public :
     192public:
    180193    SecureSocket()
    181194    {
    182195        sock = NULL;
    183196    }
     197
    184198    SecureSocket(cf::network::tcp * sock, const std::string & ip, const std::string & sms)
    185199    {
     
    193207    }
    194208
     209    cf::network::tcp & getTcpSocket()
     210    {
     211        return *sock;
     212    }
     213
    195214    void setKey(const std::string & ip, const std::string & sms)
    196215    {
  • trunk/msgclnt.cpp

    r25 r31  
    77
    88#include <stdlib.h>
    9 #include <time.h>
    109
    1110// --------------------------------------------------------------
     
    2120    MessageQ * messageQ;
    2221    SCallback callback;
     22    std::map<std::string, cf::bin> * keyMap;
    2323} SCallbackWorkerArg;
    2424
     
    6363}
    6464
    65 static inline SConversation toConversation(const Protocol::Message & message)
     65static inline SConversation toConversation(const Protocol::Message & message, std::map<std::string, cf::bin> * keyMap)
    6666{
    6767    SConversation c;
    68     std::string chat = message.get<std::string>(ProtocolType::MESSAGE);
    69 
    70     if (chat.length() > 0)
    71         c.message = cf::codec::hex::getInstance()->decode(chat).toString();
    7268
    7369    c.sessid = message.get<std::string>(ProtocolType::SESSION_ID);
    7470    c.sensitive = message.get<bool>(ProtocolType::SENSITIVE);
    75 
    7671    c.from = message.get<std::string>(ProtocolType::FROM);
    7772
     73    std::string chat = message.get<std::string>(ProtocolType::MESSAGE);
     74    if (chat.length() > 0)
     75    {
     76        crypto aria;
     77        aria.setKey((*keyMap)[c.sessid]);
     78
     79        cf::bin decoded = cf::codec::hex::getInstance()->decode(chat);
     80        cf::bin plain = aria.decrypt(decoded);
     81        c.message = plain.toString();
     82    }
     83
    7884    return c;
    7985}
    8086
    81 static inline SOpenSession toOpenSession(const Protocol::Message & message)
     87static inline SOpenSession toOpenSession(const Protocol::Message & message, std::map<std::string, cf::bin> * keyMap)
    8288{
    8389    SOpenSession o;
     
    8692    o.idList = message.getList<std::string>(ProtocolType::ID_LIST);
    8793
     94    if (message.mObject.HasKey(ProtocolType::SESSION_KEY))
     95    {
     96        std::string encodedKey = message.get<std::string>(ProtocolType::SESSION_KEY);
     97        cf::bin key = cf::codec::hex::getInstance()->decode(encodedKey);
     98        (*keyMap)[o.sessid] = key;
     99    }
     100
    88101    return o;
    89102}
     
    99112        message = inst->messageQ->pop(ProtocolType::LISTEN, false);
    100113        if (message.type() != ProtocolType::NONE)
    101             inst->callback.onListen(toConversation(message));
     114            inst->callback.onListen(toConversation(message, inst->keyMap));
    102115
    103116        message = inst->messageQ->pop(ProtocolType::OPEN_SESSION, false);
    104117        if (message.type() != ProtocolType::NONE)
    105             inst->callback.onOpenSession(toOpenSession(message));
     118            inst->callback.onOpenSession(toOpenSession(message, inst->keyMap));
    106119
    107120        message = inst->messageQ->pop(ProtocolType::LOGOUT, false);
     
    113126
    114127    return 0;
    115 }
    116 
    117 static std::string generateRandom()
    118 {
    119     int t = (int)time(NULL);
    120 
    121     cf::bin b;
    122     b.resize(sizeof(int));
    123 
    124     b.set((cf::uint8_t*)&t, sizeof(int));
    125     cf::bin s = crypto().sha256(b);
    126 
    127     return cf::codec::hex::getInstance()->encode(s);
    128128}
    129129
     
    226226    try
    227227    {
    228         std::string random = generateRandom();
     228        std::string random = cf::codec::hex::getInstance()->encode(generateRandom());
    229229        std::string sub = request.join(id, hashPassword(pw), sms);
    230230        std::string secret = getSecret(sub, sms, random);
     
    247247        request.setUserID(id);
    248248
    249         std::string random = generateRandom();
     249        std::string random = cf::codec::hex::getInstance()->encode(generateRandom());
    250250        std::string sub = request.login(hashPassword(pw));
    251251        std::string secret = getSecret(sub, sms, random);
     
    341341            Protocol::Message message = messageQ.pop(ProtocolType::OPEN_SESSION);
    342342
    343             sessionMap[concat] = message.get<std::string>(ProtocolType::SESSION_ID);
     343            std::string sessionID = message.get<std::string>(ProtocolType::SESSION_ID);
     344            sessionMap[concat] = sessionID;
     345
     346            if (message.mObject.HasKey(ProtocolType::SESSION_KEY))
     347            {
     348                std::string encodedKey = message.get<std::string>(ProtocolType::SESSION_KEY);
     349                cf::bin key = cf::codec::hex::getInstance()->decode(encodedKey);
     350                keyMap[sessionID] = key;
     351            }
    344352
    345353            bool result = messageQ.pop(ProtocolType::OPEN_SESSION).get<bool>(ProtocolType::RESULT);
     
    365373
    366374        if (c.message.length() > 0)
    367             c.message = cf::codec::hex::getInstance()->encode(c.message);
     375        {
     376            crypto aria;
     377            aria.setKey(keyMap[c.sessid]);
     378
     379            cf::bin cipher = aria.encrypt(cf::bin(c.message));
     380            c.message = cf::codec::hex::getInstance()->encode(cipher);
     381        }
    368382
    369383        secureSocket.send(request.tell(c.sessid, c.message, c.sensitive));
     
    387401    arg->messageQ = &messageQ;
    388402    arg->callback = callback;
     403    arg->keyMap = &keyMap;
    389404
    390405    caller.start(arg);
  • trunk/msgclnt.h

    r25 r31  
    5858
    5959    std::map<std::string, std::string>  sessionMap;
     60    std::map<std::string, cf::bin>      keyMap;
    6061
    6162public:
  • trunk/msgsrv.cpp

    r30 r31  
    115115            "("
    116116            "   id      TEXT NOT NULL,"
    117             "   friend  TEXT NOT NULL,"
    118             "   unique  (friend)"
     117            "   friend  TEXT NOT NULL"
    119118            ")";
    120119
     
    456455static bool openSession(const Protocol::Message & message)
    457456{
     457    const static cf::bin delim(DELIMITER);
     458
    458459    Protocol::Message parser = message;
    459     bool result = false;
     460    bool result = true;
    460461    std::string sessid;
    461462    std::vector<std::string> idList = parser.getList<std::string>(ProtocolType::ID_LIST);
     
    465466
    466467    parser.mObject[ProtocolType::SESSION_ID] = sessid;
     468
     469    cf::bin allKeys;
     470
     471    for (size_t iter = 0; iter < idList.size(); iter++)
     472    {
     473        std::string id = idList[iter];
     474        if (isOnline(id))
     475        {
     476            SecureSocket & secsock = gOnlineUsers[id];
     477            std::string ip = secsock.getTcpSocket().local().address();
     478            std::string sms = dbmgr.getSMS(id, ip);
     479
     480            cf::bin seed = sms + DELIMITER + ip;
     481            cf::bin key = crypto().sha256(seed);
     482
     483            allKeys += key;
     484        }
     485    }
     486
     487    allKeys += generateRandom();
     488    std::string hashedKey = cf::codec::hex::getInstance()->encode(crypto().sha256(allKeys));
     489    parser.mObject[ProtocolType::SESSION_KEY] = hashedKey;
     490
    467491    std::string serialized = parser.serialize();
    468492
     
    472496        if (isOnline(id))
    473497            gOnlineUsers[id].send(serialized);
    474 
    475         result = true;
    476498    }
    477499
     
    562584    {
    563585        id = authenticator(*sock);
    564         SecureSocket * secsock = &gOnlineUsers[id];
     586        SecureSocket & secsock = gOnlineUsers[id];
    565587
    566588        while (true)
     
    568590            bool result = true;
    569591            Protocol::Response response;
    570             std::string message = secsock->receive();
     592            std::string message = secsock.receive();
    571593            Protocol::Message parser;
    572594            parser.parse(message);
     
    591613                {
    592614                    std::vector<SFriend> fl = getFriendList(parser);
    593                     secsock->send(response.friendList(fl));
     615                    secsock.send(response.friendList(fl));
    594616                } catch (cf::exception & e)
    595617                {
     
    599621            }
    600622
    601             secsock->send(response.result(parser.type(), result));
     623            secsock.send(response.result(parser.type(), result));
    602624        }
    603625    }
  • trunk/msvc14/ChevMsgrClient_MFC/AddFriendDlg.cpp

    r27 r31  
    3939{
    4040    // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù.
    41     CString friendId;
    42        
    4341    GetDlgItemText(IDC_EDIT_FRIEND_ID, friendId);
    44     chev->addFriend(cstr2str(friendId));
    45 
    46     mMsgrClntDlg->mFriendListBox.InsertString(mMsgrClntDlg->mFriendListBox.GetCount(), friendId);
    47 
     42   
    4843    CDialogEx::OnOK();
    4944}
  • trunk/msvc14/ChevMsgrClient_MFC/AddFriendDlg.h

    r27 r31  
    1313    CAddFriendDlg(CWnd* pParent = NULL);   // Ç¥ÁØ »ý¼ºÀÚÀÔ´Ï´Ù.
    1414    virtual ~CAddFriendDlg();
    15     chev *chev;
    16     CChevMsgrClient_MFCDlg *mMsgrClntDlg;
     15    CString friendId;
    1716
    1817// ´ëÈ­ »óÀÚ µ¥ÀÌÅÍÀÔ´Ï´Ù.
  • trunk/msvc14/ChevMsgrClient_MFC/ChevMsgrClient_MFCDlg.cpp

    r30 r31  
    99#include <fstream>
    1010#include <algorithm>
     11#include <time.h>
    1112
    1213#include "LoginDlg.h"
     
    1920#define new DEBUG_NEW
    2021#endif
     22
     23#define SENSITIVE_TIME  10
     24#define TIMER_EVENT WM_USER +1
    2125
    2226int cb_listen(SConversation & chat);
     
    8690    ON_BN_CLICKED(IDC_BTN_ADD_CHAT, &CChevMsgrClient_MFCDlg::OnBnClickedBtnAddChat)
    8791    ON_LBN_SELCHANGE(IDC_LIST_FRIENDS, &CChevMsgrClient_MFCDlg::OnLbnSelchangeListFriends)
     92    ON_WM_TIMER()
    8893END_MESSAGE_MAP()
    8994
     
    121126
    122127    // TODO: ¿©±â¿¡ Ãß°¡ ÃʱâÈ­ ÀÛ¾÷À» Ãß°¡ÇÕ´Ï´Ù.
     128    this->SetTimer(TIMER_EVENT, 2000, NULL);
    123129
    124130    std::ifstream f("cnct2srv.ini");
     
    158164
    159165    UpdateFriendList();
     166
     167    CString cstrId;
     168
     169    for (size_t iter = 0; iter < idList.size(); iter++)
     170    {
     171        cstrId = str2cstr(idList[iter].id);
     172        this->mFriendListBox.InsertString(this->mFriendListBox.GetCount(), cstrId);
     173    }
    160174
    161175    scl.resize(idList.size());
     
    221235        if (chat.sessid == gDlg->scl[idx].sessionId)
    222236        {
     237            if (chat.sensitive == true)
     238            {
     239                SensitiveCtrl sc;
     240
     241                sc.sensitiveIdx = gDlg->scl[idx].chatList.size();
     242                sc.timestamp = (int)time(NULL);
     243                gDlg->scl[idx].sensitiveCtrl.push_back(sc);
     244            }
     245           
    223246            gDlg->scl[idx].chatList.push_back(chat);
     247
    224248            break;
    225249        }
     
    238262int cb_opensession(SOpenSession & opensession)
    239263{
    240     if (opensession.idList.size() == 2)
    241         return 0;
    242 
    243264    CString cstrIdList = _T("");
    244265
     
    254275    }
    255276
    256     gDlg->mFriendListBox.InsertString(gDlg->mFriendListBox.GetCount(), cstrIdList);
    257 
    258     Session s;
    259     s.sessionId = opensession.sessid;
    260 
    261     gDlg->scl.push_back(s);
     277    if (opensession.idList.size() > 2)
     278    {
     279        gDlg->mFriendListBox.InsertString(gDlg->mFriendListBox.GetCount(), cstrIdList);
     280
     281        Session s;
     282
     283        s.sessionId = opensession.sessid;
     284
     285        gDlg->scl.push_back(s);
     286    }
     287    else
     288    {
     289        CString cmpIdList;
     290        for (int iter = 0; iter < gDlg->mFriendListBox.GetCount(); iter++)
     291        {
     292            gDlg->mFriendListBox.GetText(iter, cmpIdList);
     293            if (cstrIdList == cmpIdList)
     294                gDlg->scl[iter].sessionId = opensession.sessid;
     295        }
     296    }
    262297
    263298    return 0;
     
    298333    if (chev.tell(conversation))
    299334    {
     335        if (conversation.sensitive == true)
     336        {
     337            SensitiveCtrl sc;
     338
     339            sc.sensitiveIdx = gDlg->scl[idx].chatList.size();
     340            sc.timestamp = (int)time(NULL);
     341            gDlg->scl[idx].sensitiveCtrl.push_back(sc);
     342        }
     343        gDlg->scl[idx].chatList.push_back(conversation);
     344
    300345        mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + mId + ">"));
    301346        mChatListBox.InsertString(mChatListBox.GetCount(), chat);
    302347        mChatListBox.InsertString(mChatListBox.GetCount(), _T(""));
    303348    }
    304 
    305     gDlg->scl[gDlg->mFriendListBox.GetCurSel()].chatList.push_back(conversation);
    306349   
    307350    SetDlgItemText(IDC_EDIT_INPUT, _T(""));
     
    313356    CAddFriendDlg AFDlg;
    314357
    315     AFDlg.chev = &this->chev;
    316     AFDlg.mMsgrClntDlg = this;
    317 
    318358    AFDlg.DoModal();
     359   
     360    CString existFriendId;
     361    for (int iter = 0; iter < mFriendListBox.GetCount(); iter++)
     362    {
     363        mFriendListBox.GetText(iter, existFriendId);
     364        if (AFDlg.friendId == existFriendId)
     365            return;
     366    }
     367    if (!chev.addFriend(cstr2str(AFDlg.friendId)))
     368    {
     369        AfxMessageBox(_T("Add Friend Fail"));
     370        return;
     371    }
    319372    UpdateFriendList();
     373    scl.resize(scl.size() + 1);
     374    mFriendListBox.InsertString(mFriendListBox.GetCount(), AFDlg.friendId);
    320375}
    321376
     
    344399void CChevMsgrClient_MFCDlg::UpdateFriendList()
    345400{
    346     CString cstrId;
    347401    if (!chev.getFriendList(idList))
    348402    {
    349403        AfxMessageBox(_T("cannot get friend list"));
    350404        ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL);
    351     }
    352 
    353     this->mFriendListBox.ResetContent();
    354 
    355     for (size_t iter = 0; iter < idList.size(); iter++)
    356     {
    357         cstrId = str2cstr(idList[iter].id);
    358         this->mFriendListBox.InsertString(this->mFriendListBox.GetCount(), cstrId);
    359405    }
    360406}
     
    373419        return;
    374420
    375     for (std::list<SConversation>::iterator iter = scl[idx].chatList.begin();
    376     iter != scl[idx].chatList.end(); iter++)
     421    std::vector<SConversation>::iterator iter = scl[idx].chatList.begin();
     422    for ( ; iter != scl[idx].chatList.end(); iter++)
    377423    {
    378424        mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + iter->from + ">"));
     
    381427    }
    382428}
     429
     430
     431void CChevMsgrClient_MFCDlg::OnTimer(UINT_PTR nIDEvent)
     432{
     433    // TODO: ¿©±â¿¡ ¸Þ½ÃÁö 󸮱â Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº»°ªÀ» È£ÃâÇÕ´Ï´Ù.
     434    int timestamp = (int)time(NULL);
     435
     436    for (size_t iter = 0; iter < scl.size(); iter++)
     437    {
     438        if (scl[iter].sensitiveCtrl.size() == 0)
     439            continue;
     440
     441        SensitiveCtrl &sc = scl[iter].sensitiveCtrl.front();
     442        if ((timestamp - sc.timestamp) >= SENSITIVE_TIME)
     443        {
     444            scl[iter].chatList.erase(scl[iter].chatList.begin() + sc.sensitiveIdx);
     445            if (iter == mFriendListBox.GetCurSel())
     446            {
     447                mChatListBox.DeleteString(sc.sensitiveIdx*3);
     448                mChatListBox.DeleteString(sc.sensitiveIdx*3);
     449                mChatListBox.DeleteString(sc.sensitiveIdx*3);
     450            }
     451            scl[iter].sensitiveCtrl.pop_front();
     452           
     453            std::list<SensitiveCtrl>::iterator it = scl[iter].sensitiveCtrl.begin();
     454            for (; it != scl[iter].sensitiveCtrl.end(); it++)
     455            {
     456                it->sensitiveIdx--;
     457            }
     458
     459        }
     460    }
     461
     462    CDialogEx::OnTimer(nIDEvent);
     463}
  • trunk/msvc14/ChevMsgrClient_MFC/ChevMsgrClient_MFCDlg.h

    r28 r31  
    99// CChevMsgrClient_MFCDlg ´ëÈ­ »óÀÚ
    1010typedef struct {
     11    int timestamp;
     12    int sensitiveIdx;
     13} SensitiveCtrl;
     14
     15typedef struct {
    1116    std::string sessionId;
    12     std::list<SConversation> chatList;
     17    std::vector<SConversation> chatList;
     18    std::list<SensitiveCtrl> sensitiveCtrl;
    1319} Session;
    1420
     
    5056    void UpdateFriendList();
    5157    afx_msg void OnLbnSelchangeListFriends();
     58    afx_msg void OnTimer(UINT_PTR nIDEvent);
    5259};
  • trunk/msvc14/ChevMsgrClient_MFC/LoginDlg.cpp

    r25 r31  
    7171    GetDlgItemText(IDC_EDIT_LOGIN_PW, pw);
    7272    f >> str;
    73     decoded = cf::codec::hex::getInstance()->decode(str);
    7473
    75     c.setKey(cf::bin(cstr2str(pw)));
    76     str = c.decrypt(decoded).toString();
     74    try
     75    {
     76        decoded = cf::codec::hex::getInstance()->decode(str);
    7777
     78        c.setKey(cf::bin(cstr2str(pw)));
     79        str = c.decrypt(decoded).toString();
     80    }
     81    catch(cf::exception &e)
     82    {
     83        AfxMessageBox(_T("Invalid Password"));
     84        (void)e;
     85        return;
     86    }
     87   
    7888    if (str.size() == 6)
    7989    {
Note: See TracChangeset for help on using the changeset viewer.