SelfEndorseCertificate is working
diff --git a/src/contact-storage.cpp b/src/contact-storage.cpp
index 8026644..21fcf51 100644
--- a/src/contact-storage.cpp
+++ b/src/contact-storage.cpp
@@ -13,6 +13,9 @@
 
 #include <string>
 #include <boost/filesystem.hpp>
+#include <ndn.cxx/fields/signature-sha256-with-rsa.h>
+#include <ndn.cxx/security/exception.h>
+#include <ndn.cxx/helpers/der/exception.h>
 #include "logging.h"
 
 using namespace std;
@@ -365,7 +368,6 @@
 void
 ContactStorage::updateProfileData(const Name& identity) const
 {
-   _LOG_DEBUG("Enter updateProfileData!");
   // Get current profile;
   Ptr<Profile> newProfile = getSelfProfile(identity);
   if(NULL == newProfile)
@@ -381,39 +383,41 @@
     {
       sqlite3_finalize (stmt);
 
-      Ptr<ProfileData> newProfileData = getSignedSelfProfileData(identity, *newProfile);
+      Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
       _LOG_DEBUG("Signing DONE!");
-      if(NULL == newProfileData)
+      if(NULL == newEndorseCertificate)
         return;
-      Ptr<Blob> newProfileDataBlob = newProfileData->encodeToWire();
+      Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
 
       _LOG_DEBUG("Before Inserting!");
 
       sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileData (identity, profile_data) values (?, ?)", -1, &stmt, 0);
       sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
-      sqlite3_bind_text(stmt, 2, newProfileDataBlob->buf(), newProfileDataBlob->size(), SQLITE_TRANSIENT);
+      sqlite3_bind_text(stmt, 2, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
       sqlite3_step(stmt);
     }
   else
     {
       Ptr<Blob> profileDataBlob = Ptr<Blob>(new Blob(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
       Ptr<Data> plainData = Data::decodeFromWire(profileDataBlob);
-      const Blob& oldProfileBlob = plainData->content();
+      EndorseCertificate oldEndorseCertificate(*plainData);    
+      // _LOG_DEBUG("Certificate converted!");
+      const Blob& oldProfileBlob = oldEndorseCertificate.getProfileData()->content();
       sqlite3_finalize (stmt);
 
       if(oldProfileBlob == *newProfileBlob)
         return;
 
-      Ptr<ProfileData> newProfileData = getSignedSelfProfileData(identity, *newProfile);
+      Ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(identity, *newProfile);
       _LOG_DEBUG("Signing DONE!");
-      if(NULL == newProfileData)
+      if(NULL == newEndorseCertificate)
         return;
-      Ptr<Blob> newProfileDataBlob = newProfileData->encodeToWire();
+      Ptr<Blob> newEndorseCertificateBlob = newEndorseCertificate->encodeToWire();
 
       _LOG_DEBUG("Before Updating!");
 
       sqlite3_prepare_v2 (m_db, "UPDATE ProfileData SET profile_data=? WHERE identity=?", -1, &stmt, 0);
-      sqlite3_bind_text(stmt, 1, newProfileDataBlob->buf(), newProfileDataBlob->size(), SQLITE_TRANSIENT);
+      sqlite3_bind_text(stmt, 1, newEndorseCertificateBlob->buf(), newEndorseCertificateBlob->size(), SQLITE_TRANSIENT);
       sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
       sqlite3_step(stmt);
     }
@@ -437,19 +441,33 @@
   return profile;
 }
 
-Ptr<ProfileData>
-ContactStorage::getSignedSelfProfileData(const Name& identity,
-                         const Profile& profile) const
+Ptr<EndorseCertificate>
+ContactStorage::getSignedSelfEndorseCertificate(const Name& identity,
+                                                const Profile& profile) const
 {
   Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(identity);
   if(0 == certificateName.size())
     return NULL;
 
   Ptr<ProfileData> profileData = Ptr<ProfileData>(new ProfileData(identity, profile));
-  _LOG_DEBUG("Get ProfileData, size: " << profileData->content().size());
-  _LOG_DEBUG("Get SigningCert, name: " << certificateName.toUri());
   m_identityManager->signByCertificate(*profileData, certificateName);
 
-  return profileData;
+  Ptr<security::IdentityCertificate> dskCert = m_identityManager->getCertificate(certificateName);
+  Ptr<const signature::Sha256WithRsa> dskCertSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(dskCert->getSignature());
+  Ptr<security::IdentityCertificate> kskCert = m_identityManager->getCertificate(dskCertSig->getKeyLocator().getKeyName());
+
+  vector<string> endorseList;
+  Profile::const_iterator it = profile.begin();
+  for(; it != profile.end(); it++)
+    endorseList.push_back(it->first);
+  
+  Ptr<EndorseCertificate> selfEndorseCertificate = Ptr<EndorseCertificate>(new EndorseCertificate(*kskCert,
+                                                                                                  kskCert->getNotBefore(),
+                                                                                                  kskCert->getNotAfter(),
+                                                                                                  profileData,
+                                                                                                  endorseList));
+  m_identityManager->signByCertificate(*selfEndorseCertificate, kskCert->getName());
+
+  return selfEndorseCertificate;
 }
 
diff --git a/src/contact-storage.h b/src/contact-storage.h
index 5b87146..7fc3171 100644
--- a/src/contact-storage.h
+++ b/src/contact-storage.h
@@ -14,7 +14,7 @@
 #include <sqlite3.h>
 #include "trusted-contact.h"
 #include "contact-item.h"
-#include "profile-data.h"
+#include "endorse-certificate.h"
 #include <ndn.cxx/security/identity/identity-manager.h>
 #include <ndn.cxx/fields/signature-sha256-with-rsa.h>
 
@@ -68,9 +68,9 @@
   ndn::Ptr<Profile>
   getSelfProfile(const ndn::Name& identity) const;
 
-  ndn::Ptr<ProfileData>
-  getSignedSelfProfileData(const ndn::Name& identity,
-                           const Profile& profile) const;
+  ndn::Ptr<EndorseCertificate>
+  getSignedSelfEndorseCertificate(const ndn::Name& identity,
+                                  const Profile& profile) const;
   
 private:
   ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
diff --git a/src/endorse-certificate.cpp b/src/endorse-certificate.cpp
index c6d338a..8dd43e0 100644
--- a/src/endorse-certificate.cpp
+++ b/src/endorse-certificate.cpp
@@ -13,12 +13,14 @@
 #include <ndn.cxx/helpers/der/der.h>
 #include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
 #include <ndn.cxx/security/certificate/certificate-subdescrpt.h>
-
+#include "logging.h"
 
 using namespace std;
 using namespace ndn;
 using namespace ndn::security;
 
+INIT_LOGGER("EndorseCertificate");
+
 ProfileExtension::ProfileExtension(const ProfileData & profileData)
   : CertificateExtension("1.3.6.1.5.32.2.1", true, *profileData.encodeToWire())
 {}
@@ -37,6 +39,7 @@
 Ptr<ProfileData>
 ProfileExtension::getProfileData()
 {
+  // _LOG_DEBUG("size: " << m_extnValue.size ());
   boost::iostreams::stream
     <boost::iostreams::array_source> is (m_extnValue.buf (), m_extnValue.size ());
   return Ptr<ProfileData>(new ProfileData(*Data::decodeFromWire(is)));
@@ -115,7 +118,7 @@
   setNotBefore(notBefore);
   setNotAfter(notAfter);
   addSubjectDescription(CertificateSubDescrypt("2.5.4.41", m_keyName.toUri()));
-  setPublicKeyInfo(kskCertificate.getPublicKeyInfo());
+  setPublicKeyInfo(kskCertificate.getPublicKeyInfo());  
   addExtension(ProfileExtension(*m_profileData));
   addExtension(EndorseExtension(m_endorseList));
   
@@ -161,9 +164,11 @@
 EndorseCertificate::EndorseCertificate(const Data& data)
   : Certificate(data)
 {
+  // _LOG_DEBUG("0");
   const Name& dataName = data.getName();
+  // _LOG_DEBUG("1");
   name::Component certFlag(string("PROFILE-CERT"));  
-
+  // _LOG_DEBUG("2");
   int profileIndex = -1;
   for(int i = 0; i < dataName.size(); i++)
     {
@@ -173,26 +178,35 @@
 	  break;
 	}
     }
-
+  // _LOG_DEBUG("3");
   if(profileIndex < 0)
     throw LnException("No PROFILE-CERT component in data name!");
 
   m_keyName = dataName.getSubName(0, profileIndex);
   m_signer = dataName.getSubName(profileIndex + 1, dataName.size() - profileIndex - 2);
 
+  // _LOG_DEBUG("keyName: " << m_keyName.toUri());
+  // _LOG_DEBUG("signer: " << m_signer.toUri());
+
   OID profileExtensionOID("1.3.6.1.5.32.2.1");
   OID endorseExtensionOID("1.3.6.1.5.32.2.2");
 
+  // _LOG_DEBUG("OID ready");
   ExtensionList::iterator it = m_extnList.begin();
   for(; it != m_extnList.end(); it++)
     {
+      // _LOG_DEBUG("entry");
       if(profileExtensionOID == it->getOID())
 	{
+          // _LOG_DEBUG("ProfileExtn");
           ProfileExtension profileExtension(*it);
+          // _LOG_DEBUG("ProfileExtn created");
 	  m_profileData = profileExtension.getProfileData();
+          // _LOG_DEBUG("get profileDate");
 	}
       if(endorseExtensionOID == it->getOID())
         {
+          // _LOG_DEBUG("EndorseExtn");
           EndorseExtension endorseExtension(*it);
           m_endorseList = endorseExtension.getEndorsedList();
         }
diff --git a/src/profile-data.cpp b/src/profile-data.cpp
index b779adc..f057a6d 100644
--- a/src/profile-data.cpp
+++ b/src/profile-data.cpp
@@ -11,10 +11,14 @@
 #include "profile-data.h"
 #include "exception.h"
 #include <ndn.cxx/fields/signature-sha256-with-rsa.h>
+#include "logging.h"
+
 
 using namespace ndn;
 using namespace std;
 
+INIT_LOGGER("ProfileData");
+
 ProfileData::ProfileData(const Name& identity,
 			 const Profile& profile)
   : Data()
@@ -57,6 +61,7 @@
 ProfileData::ProfileData(const Data& data)
   : Data()
 {
+  // _LOG_DEBUG("ProfileData constructor");
   const Name& dataName = data.getName();
   name::Component appFlag(string("PROFILE"));  
 
@@ -90,6 +95,7 @@
   setSignature(newSig);
   setContent(data.getContent());
   setSignedBlob(newSignedBlob);
-
+  // _LOG_DEBUG("Decode Profile");
   m_profile = *Profile::fromDerBlob(data.content());
+  // _LOG_DEBUG("Profile Decoded");
 }
diff --git a/src/profile.cpp b/src/profile.cpp
index 7f2ba6e..e5c5325 100644
--- a/src/profile.cpp
+++ b/src/profile.cpp
@@ -10,11 +10,14 @@
 
 #include "profile.h"
 #include <ndn.cxx/helpers/der/der.h>
+#include <ndn.cxx/helpers/der/visitor/print-visitor.h>
 #include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
+#include "logging.h"
 
 using namespace std;
 using namespace ndn;
 
+INIT_LOGGER("Profile");
 Profile::Profile(const Name& identityName)
   : m_identityName(identityName)
 {
@@ -91,7 +94,6 @@
     <boost::iostreams::array_source> is (derBlob.buf(), derBlob.size());
 
   Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
-  
   const der::DerNodePtrList & children = root->getChildren();
   der::SimpleVisitor simpleVisitor;
   string identityName = boost::any_cast<string>(children[0]->accept(simpleVisitor));
@@ -100,7 +102,7 @@
   for(int i = 1; i < children.size(); i++)
     {
       Ptr<der::DerSequence> entry = DynamicCast<der::DerSequence>(children[i]);
-      const der::DerNodePtrList & tuple = root->getChildren();
+      const der::DerNodePtrList & tuple = entry->getChildren();
       string type = boost::any_cast<string>(tuple[0]->accept(simpleVisitor));
       Ptr<Blob> value = boost::any_cast<Ptr<Blob> >(tuple[1]->accept(simpleVisitor));
       profile->setProfileEntry(type, *value);