Adding part of exception handling
diff --git a/src/contact-manager.cpp b/src/contact-manager.cpp
index 71b9ea2..2b834c6 100644
--- a/src/contact-manager.cpp
+++ b/src/contact-manager.cpp
@@ -13,13 +13,10 @@
 #ifndef Q_MOC_RUN
 #include <ndn.cxx/wrapper/wrapper.h>
 #include <ndn.cxx/security/keychain.h>
-#include <ndn.cxx/security/identity/basic-identity-storage.h>
-#include <ndn.cxx/security/identity/osx-privatekey-storage.h>
 #include <ndn.cxx/security/policy/simple-policy-manager.h>
 #include <ndn.cxx/security/policy/identity-policy-rule.h>
-#include <ndn.cxx/security/cache/ttl-certificate-cache.h>
-#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
 #include <ndn.cxx/helpers/der/der.h>
+#include <cryptopp/base64.h>
 #include <fstream>
 #include "logging.h"
 #endif
@@ -48,12 +45,10 @@
 void
 ContactManager::setKeychain()
 {
-  Ptr<OSXPrivatekeyStorage> privateStorage = Ptr<OSXPrivatekeyStorage>::Create();
-  Ptr<IdentityManager> identityManager = Ptr<IdentityManager>(new IdentityManager(Ptr<BasicIdentityStorage>::Create(), privateStorage));
-  Ptr<TTLCertificateCache> certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache());
-  Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager(10, certificateCache));
-  Ptr<EncryptionManager> encryptionManager = Ptr<EncryptionManager>(new BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
-  Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, encryptionManager));
+  Ptr<IdentityManager> identityManager = Ptr<IdentityManager>::Create();
+  Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>::Create();
+
+  Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, NULL));
 
   policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>",
                                                                                           "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
@@ -75,20 +70,65 @@
                                                                                      "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
                                                                                      "==", "\\1", "\\1\\2", true)));
 
-  ifstream is ("trust-anchor.data", ios::binary);
-  is.seekg (0, ios::end);
-  ifstream::pos_type size = is.tellg();
-  char * memblock = new char [size];    
-  is.seekg (0, ios::beg);
-  is.read (memblock, size);
-  is.close();
+  const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq5Azheu9IVyewcV4uM7ylfh67Y8MIxF3tDCTx5JgEn\
+HYMuCaYQm6XuaXTlVfDdWff/K7Xebq8IgGxjNBeU9eMf7Gy9iIMrRAOdBG0dBHmo\
+67biGs8F+P1oh1FwKu/FN1AE9vh8HSOJ94PWmjO+6PvITFIXuI3QbcCz8rhvbsfb\
+5X/DmfbJ8n8c4X3nVxrBm6fd4z8kOFOvvhgJImvqsow69Uy+38m8gJrmrcWMoPBJ\
+WsNLcEriZCt/Dlg7EqqVrIn6ukylKCvVrxA9vm/cEB74J/N+T0JyMRDnTLm17gpq\
+Gd75rhj+bLmpOMOBT7Nb27wUKq8gcXzeAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
+MzMyNTcyMAD6vUlELUNFUlQA+q39PgurHgAAAaID4gKF5vjua9EIr3/Fn8k1AdSc\
+nEryjVDW3ikvYoSwjK7egTkAArq1BSc+C6sdAAHiAery+p1uZG4A+p1LRVkA+vVr\
+c2stMTM4MzMyNTcyMAD6vUlELUNFUlQAAAAAAAGaFr0wggFjMCIYDzIwMTMxMTAx\
+MTcxMTIyWhgPMjAxNDExMDExNzExMjJaMBkwFwYDVQQpExBORE4gVGVzdGJlZCBS\
+b290MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA06x+elwzWCHa4I3b\
+yrYCMAIVxQpRVLuOXp0h+BS+5GNgMVPi7+40o4zSJG+kiU8CIH1mtj8RQAzBX9hF\
+I5VAyOC8nS8D8YOfBwt2yRDZPgt1E5PpyYUBiDYuq/zmJDL8xjxAlxrMzVOqD/uj\
+/vkkcBM/T1t9Q6p1CpRyq+GMRbV4EAHvH7MFb6bDrH9t8DHEg7NPUCaSQBrd7PvL\
+72P+QdiNH9zs/EiVzAkeMG4iniSXLuYM3z0gMqqcyUUUr6r1F9IBmDO+Kp97nZh8\
+VCL+cnIEwyzAFAupQH5GoXUWGiee8oKWwH2vGHX7u6sWZsCp15NMSG3OC4jUIZOE\
+iVUF1QIBEQAA");
 
-  Ptr<Blob> readBlob = Ptr<Blob>(new Blob(memblock, size));
-  Ptr<Data> readData = Data::decodeFromWire (readBlob);
-  Ptr<IdentityCertificate> anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*readData));   
+  string decoded;
+  CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()), 
+                            TrustAnchor.size(), 
+                            true,
+                            new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
+  Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size()));
+  Ptr<Data> data = Data::decodeFromWire(blob);
+  Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
   policyManager->addTrustAnchor(anchor);  
-  
-  delete memblock;
+
+#ifdef _DEBUG
+
+  const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
+zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
+qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
+6M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
+DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
+Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
+MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
+qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
+APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
+MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
+MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
+/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
+sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
+H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
+gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
+OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
+F7Wh5ayeo8NBKDsCAwEAAQAA");
+
+  string decoded2;
+  CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()), 
+                            FakeAnchor.size(), 
+                            true,
+                            new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
+  Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size()));
+  Ptr<Data> data2 = Data::decodeFromWire(blob2);
+  Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2));
+  policyManager->addTrustAnchor(anchor2);  
+
+#endif
 
   m_keychain = keychain;
 }
@@ -210,7 +250,7 @@
 void
 ContactManager::updateProfileData(const Name& identity)
 {
-  _LOG_DEBUG("updateProfileData: " << identity.toUri());
+  // _LOG_DEBUG("updateProfileData: " << identity.toUri());
   // Get current profile;
   Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
   if(NULL == newProfile)
@@ -233,7 +273,7 @@
       // _LOG_DEBUG("Signing DONE!");
       if(NULL == newEndorseCertificate)
         return;
-      _LOG_DEBUG("About to update");
+      // _LOG_DEBUG("About to update");
       m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
 
       publishSelfEndorseCertificateInDNS(newEndorseCertificate);
@@ -244,7 +284,7 @@
       // _LOG_DEBUG("Signing DONE!");
       if(NULL == newEndorseCertificate)
         return;
-      _LOG_DEBUG("About to Insert");
+      // _LOG_DEBUG("About to Insert");
       m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
 
       publishSelfEndorseCertificateInDNS(newEndorseCertificate);
@@ -278,6 +318,8 @@
 ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
 {
   Ptr<ContactItem> contact = getContact(identity);
+  if(contact == NULL)
+    return NULL;
 
   Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
   Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
@@ -312,6 +354,9 @@
   identityManager->signByCertificate(*profileData, certificateName);
 
   Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
+  if(NULL == signingCert)
+    return NULL;
+
   Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
 
   Ptr<security::IdentityCertificate> kskCert;
@@ -319,20 +364,21 @@
     {
       Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
       // HACK! KSK certificate should be retrieved from network.
-      _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName());
       Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
-      _LOG_DEBUG("keyName: " << keyName.toUri());
+
       Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
-      _LOG_DEBUG("ksk cert name: " << kskCertName);
+
       kskCert = identityManager->getCertificate(kskCertName);
 
     }
   else
     {
       kskCert = signingCert;
-      _LOG_DEBUG("ksk cert name: " << kskCert->getName().toUri());
     }
 
+  if(NULL == kskCert)
+    return NULL;
+
   vector<string> endorseList;
   Profile::const_iterator it = profile.begin();
   for(; it != profile.end(); it++)
@@ -359,18 +405,9 @@
   const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
 
   if(security::PolicyManager::verifySignature(*plainData, ksk))
-    {
-      // Profile profile = selfEndorseCertificate->getProfileData()->getProfile();
-      // Profile::const_iterator it = profile.getEntries().begin();
-      // it++;
-      // _LOG_DEBUG("Entry Size: " << it->first);
-
-      emit contactFetched (*selfEndorseCertificate); 
-    }
+    emit contactFetched (*selfEndorseCertificate); 
   else
-    {
-      emit contactFetchFailed (identity);
-    }
+    emit contactFetchFailed (identity);
 }
 
 void
@@ -396,11 +433,6 @@
   data->setName(dnsName);
   Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
 
-  // string encoded;
-  // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true,
-  //       		    new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false));
-
-  // Content content(encoded.c_str(), encoded.size());
   Content content(blob->buf(), blob->size());
   data->setContent(content);