security: Integrate certificate exchanging into sync

Change-Id: I6ea284b0683d75013f6b72845d894eaed29e468c
diff --git a/src/sync-intro-certificate.h b/src/sync-intro-certificate.h
index 48dae45..3c28366 100644
--- a/src/sync-intro-certificate.h
+++ b/src/sync-intro-certificate.h
@@ -18,6 +18,12 @@
 
 class IntroCertificate : public ndn::Data
 {
+  /**
+   * Naming convention of IntroCertificate:
+   * /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
+   * Content: introducee's identity certificate;
+   * KeyLocator: introducer's identity certificate;
+   */
 public:
   struct Error : public ndn::Data::Error { Error(const std::string &what) : ndn::Data::Error(what) {} };
 
@@ -33,7 +39,7 @@
    */
   IntroCertificate(const ndn::Name& syncPrefix,
                    const ndn::IdentityCertificate& introduceeCert,
-                   const ndn::Name& introducerName); //without version number
+                   const ndn::Name& introducerCertName); //without version number
 
   /**
    * @brief Construct IntroCertificate using a plain data.
@@ -55,59 +61,60 @@
   }
 
   const ndn::Name&
-  getIntroducerName() const
+  getIntroducerCertName() const
   {
-    return m_introducerName;
+    return m_introducerCertName;
   }
 
   const ndn::Name&
-  getIntroduceeName() const
+  getIntroduceeCertName() const
   {
-    return m_introduceeName;
+    return m_introduceeCertName;
   }
 
 private:
   ndn::Name m_syncPrefix;
   ndn::IdentityCertificate m_introduceeCert;
-  ndn::Name m_introducerName;
-  ndn::Name m_introduceeName;
+  ndn::Name m_introducerCertName;
+  ndn::Name m_introduceeCertName;
 };
 
 inline
 IntroCertificate::IntroCertificate(const ndn::Name& syncPrefix,
                                    const ndn::IdentityCertificate& introduceeCert,
-                                   const ndn::Name& introducerName)
+                                   const ndn::Name& introducerCertName)
   : m_syncPrefix(syncPrefix)
   , m_introduceeCert(introduceeCert)
-  , m_introducerName(introducerName)
-  , m_introduceeName(introduceeCert.getName().getPrefix(-1))
+  , m_introducerCertName(introducerCertName)
+  , m_introduceeCertName(introduceeCert.getName().getPrefix(-1))
 {
-  // Naming convention /<sync_prefix>/intro-cert/introducee_certname/introducer_certname/version/
+  // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
   ndn::Name dataName = m_syncPrefix;
-  dataName.append("intro-cert").append(introduceeCert.getName().getPrefix(-1).wireEncode()).append(introducerName.wireEncode()).appendVersion();
+  dataName.append("CHRONOS-INTRO-CERT")
+    .append(m_introduceeCertName.wireEncode())
+    .append(m_introducerCertName.wireEncode())
+    .appendVersion();
   
   setName(dataName);
-  setContent(introduceeCert.wireEncode());
+  setContent(m_introduceeCert.wireEncode());
 }
 
 inline
 IntroCertificate::IntroCertificate(const ndn::Data& data)
   : Data(data)
 {
+  // Naming convention /<sync_prefix>/CHRONOS-INTRO-CERT/introducee_certname/introducer_certname/version
   ndn::Name dataName = data.getName();
-  ndn::Name introduceeCertName;
-  ndn::Name introducerName;
 
-  if(dataName.size() < 4 || dataName.get(-4).toEscapedString() != "intro-cert")
+  if(dataName.size() < 4 || dataName.get(-4).toEscapedString() != "CHRONOS-INTRO-CERT")
     throw Error("Not a Sync::IntroCertificate");
 
-  m_syncPrefix = dataName.getPrefix(-4);
-
   try
     {
       m_introduceeCert.wireDecode(data.getContent().blockFromValue());
-      m_introducerName.wireDecode(dataName.get(-2).blockFromValue());
-      introduceeCertName.wireDecode(dataName.get(-3).blockFromValue());
+      m_introducerCertName.wireDecode(dataName.get(-2).blockFromValue());
+      m_introduceeCertName.wireDecode(dataName.get(-3).blockFromValue());
+      m_syncPrefix = dataName.getPrefix(-4);
     }
   catch(ndn::IdentityCertificate::Error& e)
     {
@@ -122,15 +129,14 @@
       throw Error("Cannot decode block name");
     }
 
-  if(introduceeCertName != m_introduceeCert.getName().getPrefix(-1))
+  if(m_introduceeCertName != m_introduceeCert.getName().getPrefix(-1))
     throw Error("Invalid Sync::IntroCertificate (inconsistent introducee name)");
 
-  m_introduceeName = introduceeCertName;
-
+  ndn::Name keyLocatorName;
   try
     {
       ndn::SignatureSha256WithRsa sig(data.getSignature());
-      introducerName = sig.getKeyLocator().getName();
+      keyLocatorName = sig.getKeyLocator().getName();
     }
   catch(ndn::KeyLocator::Error& e)
     {
@@ -141,10 +147,7 @@
       throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#2)");
     }
 
-  if(m_introducerName != introducerName)
-    throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
-
-  if(m_introducerName != introducerName)
+  if(m_introducerCertName != keyLocatorName)
     throw Error("Invalid Sync::IntroCertificate (inconsistent introducer name#3)");
 }