security: Fix security bugs, now ChronoChat should work with security

Change-Id: I81e7687bb4b02798ad9de3da34b36b0f011ffd66
diff --git a/src/chat-dialog.cpp b/src/chat-dialog.cpp
index 954a4c6..baa9f78 100644
--- a/src/chat-dialog.cpp
+++ b/src/chat-dialog.cpp
@@ -37,7 +37,7 @@
 static const uint8_t CHRONOS_RP_SEPARATOR[2] = {0xF0, 0x2E}; // %F0.
 
 Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> )
-Q_DECLARE_METATYPE(ndn::Data)
+Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
 Q_DECLARE_METATYPE(ndn::Interest)
 Q_DECLARE_METATYPE(size_t)
 
@@ -66,7 +66,7 @@
   , m_inviteListDialog(new InviteListDialog)
 {
   qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
-  qRegisterMetaType<ndn::Data>("ndn.Data");
+  qRegisterMetaType<ndn::shared_ptr<const ndn::Data> >("ndn.DataPtr");
   qRegisterMetaType<ndn::Interest>("ndn.Interest");
   qRegisterMetaType<size_t>("size_t");
 
@@ -98,16 +98,16 @@
   connect(m_timer, SIGNAL(timeout()), 
           this, SLOT(onReplot()));
 
-  connect(this, SIGNAL(processData(const ndn::Data&, bool, bool)), 
-          this, SLOT(onProcessData(const ndn::Data&, bool, bool)));
+  connect(this, SIGNAL(processData(const ndn::shared_ptr<const ndn::Data>&, bool, bool)), 
+          this, SLOT(onProcessData(const ndn::shared_ptr<const ndn::Data>&, bool, bool)));
   connect(this, SIGNAL(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)), 
           this, SLOT(onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
-  connect(this, SIGNAL(reply(const ndn::Interest&, const ndn::Data&, size_t, bool)),
-          this, SLOT(onReply(const ndn::Interest&, const ndn::Data&, size_t, bool)));
+  connect(this, SIGNAL(reply(const ndn::Interest&, const ndn::shared_ptr<const ndn::Data>&, size_t, bool)),
+          this, SLOT(onReply(const ndn::Interest&, const ndn::shared_ptr<const ndn::Data>&, size_t, bool)));
   connect(this, SIGNAL(replyTimeout(const ndn::Interest&, size_t)),
           this, SLOT(onReplyTimeout(const ndn::Interest&, size_t)));
-  connect(this, SIGNAL(introCert(const ndn::Interest&, const ndn::Data&)),
-          this, SLOT(onIntroCert(const ndn::Interest&, const ndn::Data&)));
+  connect(this, SIGNAL(introCert(const ndn::Interest&, const ndn::shared_ptr<const ndn::Data>&)),
+          this, SLOT(onIntroCert(const ndn::Interest&, const ndn::shared_ptr<const ndn::Data>&)));
   connect(this, SIGNAL(introCertTimeout(const ndn::Interest&, int, const QString&)),
           this, SLOT(onIntroCertTimeout(const ndn::Interest&, int, const QString&)));
 
@@ -124,6 +124,8 @@
               this, SLOT(onInviteListDialogRequested()));
       connect(m_inviteListDialog, SIGNAL(sendInvitation(const QString&)),
               this, SLOT(onSendInvitation(const QString&)));
+      connect(this, SIGNAL(waitForContactList()),
+              m_contactManager, SLOT(onWaitForContactList()));
       connect(m_contactManager, SIGNAL(contactAliasListReady(const QStringList&)),
               m_inviteListDialog, SLOT(onContactAliasListReady(const QStringList&)));
       connect(m_contactManager, SIGNAL(contactIdListReady(const QStringList&)),
@@ -154,6 +156,7 @@
 void
 ChatDialog::addSyncAnchor(const Invitation& invitation)
 {
+  _LOG_DEBUG("Add sync anchor from invation");
   // Add inviter certificate as trust anchor.
   m_sock->addParticipant(invitation.getInviterCertificate());
 
@@ -172,14 +175,14 @@
 void
 ChatDialog::processDataWrapper(const shared_ptr<const Data>& data)
 {
-  emit processData(*data, true, false);
+  emit processData(data, true, false);
   _LOG_DEBUG("<<< " << data->getName() << " fetched");
 }
 
 void
 ChatDialog::processDataNoShowWrapper(const shared_ptr<const Data>& data)
 {
-  emit processData(*data, false, false);
+  emit processData(data, false, false);
 }
 
 void
@@ -202,6 +205,22 @@
 }
 
 void
+ChatDialog::changeEvent(QEvent *e)
+{
+  switch(e->type())
+  {
+  case QEvent::ActivationChange:
+    if (isActiveWindow())
+    {
+      emit resetIcon();
+    }
+    break;
+  default:
+    break;
+  }
+}
+
+void
 ChatDialog::resizeEvent(QResizeEvent *e)
 {
   fitView();
@@ -326,6 +345,7 @@
   // Send the invitation out
   Interest interest(interestName);
   interest.setMustBeFresh(true);
+  _LOG_DEBUG("sendInvitation: " << interest.getName());
   m_face->expressInterest(interest,
                           bind(&ChatDialog::replyWrapper, this, _1, _2, routablePrefixOffset, isIntroducer),
                           bind(&ChatDialog::replyTimeoutWrapper, this, _1, routablePrefixOffset));
@@ -337,13 +357,16 @@
                          size_t routablePrefixOffset,
                          bool isIntroducer)
 {
-  emit reply(interest, data, routablePrefixOffset, isIntroducer);
+  _LOG_DEBUG("ChatDialog::replyWrapper");
+  emit reply(interest, data.shared_from_this(), routablePrefixOffset, isIntroducer);
+  _LOG_DEBUG("OK?");
 }
 
 void
 ChatDialog::replyTimeoutWrapper(const Interest& interest, 
                                 size_t routablePrefixOffset)
 {
+  _LOG_DEBUG("ChatDialog::replyTimeoutWrapper");
   emit replyTimeout(interest, routablePrefixOffset);
 }
 
@@ -429,6 +452,8 @@
       Interest interest(certName);
       interest.setMustBeFresh(true);
 
+      _LOG_DEBUG("onIntroCertList: to fetch " << certName);
+
       m_face->expressInterest(interest,
                               bind(&ChatDialog::introCertWrapper, this, _1, _2),
                               bind(&ChatDialog::introCertTimeoutWrapper, this, _1, 0, 
@@ -450,7 +475,7 @@
 void
 ChatDialog::introCertWrapper(const Interest& interest, Data& data)
 {
-  emit introCert(interest, data);
+  emit introCert(interest, data.shared_from_this());
 }
 
 void
@@ -500,6 +525,7 @@
       const Sync::IntroCertificate& introCert = m_sock->getIntroCertificate(certName);
       Data data(interest.getName());
       data.setContent(introCert.wireEncode());
+      m_keyChain.sign(data,  m_myCertificate.getName());
       m_face->put(data);
     }
   catch(Sync::SyncSocket::Error& e)
@@ -526,12 +552,12 @@
     _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?");
     abort();
   }
+  uint64_t nextSequence = m_sock->getNextSeq();
   m_sock->publishData(os.buf()->buf(), os.buf()->size(), FRESHNESS);
 
   m_lastMsgTime = time::now();
 
-  uint64_t nextSequence = m_sock->getNextSeq();
-  Sync::MissingDataInfo mdi = {m_localChatPrefix.toUri(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
+  Sync::MissingDataInfo mdi = {m_localChatPrefix.toUri(), Sync::SeqNo(0), Sync::SeqNo(nextSequence)};
   std::vector<Sync::MissingDataInfo> v;
   v.push_back(mdi);
   {
@@ -913,13 +939,13 @@
 }
 
 void
-ChatDialog::onProcessData(const Data& data, bool show, bool isHistory)
+ChatDialog::onProcessData(const shared_ptr<const Data>& data, bool show, bool isHistory)
 {
   SyncDemo::ChatMessage msg;
   bool corrupted = false;
-  if (!msg.ParseFromArray(data.getContent().value(), data.getContent().value_size()))
+  if (!msg.ParseFromArray(data->getContent().value(), data->getContent().value_size()))
   {
-    _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << data.getName() << ". what is happening?");
+    _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << data->getName() << ". what is happening?");
     // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
     msg.set_from("inconnu");
     msg.set_type(SyncDemo::ChatMessage::OTHER);
@@ -939,7 +965,7 @@
   if (!isHistory)
   {
     // update the tree view
-    std::string stdStrName = data.getName().toUri();
+    std::string stdStrName = data->getName().toUri();
     std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
     std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
     _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from());
@@ -1030,6 +1056,7 @@
 void
 ChatDialog::onInviteListDialogRequested()
 {
+  emit waitForContactList();
   m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri());
   m_inviteListDialog->show();
 }
@@ -1112,7 +1139,7 @@
 
 void 
 ChatDialog::onReply(const Interest& interest, 
-                    const Data& data,
+                    const shared_ptr<const Data>& data,
                     size_t routablePrefixOffset,
                     bool isIntroducer)
 {
@@ -1127,12 +1154,12 @@
   if(routablePrefixOffset > 0)
     {
       // It is an encapsulated packet, we only validate the inner packet.
-      Data innerData;
-      innerData.wireDecode(data.getContent().blockFromValue());
-      m_invitationValidator->validate(innerData, onValidated, onFailed);
+      shared_ptr<Data> innerData = make_shared<Data>();
+      innerData->wireDecode(data->getContent().blockFromValue());
+      m_invitationValidator->validate(*innerData, onValidated, onFailed);
     }
   else
-    m_invitationValidator->validate(data, onValidated, onFailed);
+    m_invitationValidator->validate(*data, onValidated, onFailed);
 }
 
 void
@@ -1152,16 +1179,16 @@
 }
 
 void
-ChatDialog::onIntroCert(const ndn::Interest& interest, const Data& data)
+ChatDialog::onIntroCert(const Interest& interest, const shared_ptr<const Data>& data)
 {
   Data innerData;
-  innerData.wireDecode(data.getContent().blockFromValue());
+  innerData.wireDecode(data->getContent().blockFromValue());
   Sync::IntroCertificate introCert(innerData);
   m_sock->addParticipant(introCert);
 }
 
 void
-ChatDialog::onIntroCertTimeout(const ndn::Interest& interest, int retry, const QString& msg)
+ChatDialog::onIntroCertTimeout(const Interest& interest, int retry, const QString& msg)
 {
   _LOG_DEBUG("onIntroCertTimeout: " << msg.toStdString());
 }