security: Pair-up Tpm and Pib in KeyChain

In this commit, we also change the HOME setting for test cases.

Change-Id: I7fa15461555b3519d9d2005c6956c167ed07d66f
Refs: #2242
Refs: #2260
diff --git a/src/security/sec-public-info-sqlite3.cpp b/src/security/sec-public-info-sqlite3.cpp
index 946eb56..58d6267 100644
--- a/src/security/sec-public-info-sqlite3.cpp
+++ b/src/security/sec-public-info-sqlite3.cpp
@@ -40,71 +40,84 @@
 using std::string;
 using std::vector;
 
-static const string INIT_ID_TABLE = "\
-CREATE TABLE IF NOT EXISTS                                           \n \
-  Identity(                                                          \n \
-      identity_name     BLOB NOT NULL,                               \n \
-      default_identity  INTEGER DEFAULT 0,                           \n \
-                                                                     \
-      PRIMARY KEY (identity_name)                                    \n \
-  );                                                                 \n \
-                                                                     \
-CREATE INDEX identity_index ON Identity(identity_name);              \n \
-";
+const std::string SecPublicInfoSqlite3::SCHEME("pib-sqlite3:");
 
-static const string INIT_KEY_TABLE = "\
-CREATE TABLE IF NOT EXISTS                                           \n \
-  Key(                                                               \n \
-      identity_name     BLOB NOT NULL,                               \n \
-      key_identifier    BLOB NOT NULL,                               \n \
-      key_type          INTEGER,                                     \n \
-      public_key        BLOB,                                        \n \
-      default_key       INTEGER DEFAULT 0,                           \n \
-      active            INTEGER DEFAULT 0,                           \n \
-                                                                     \
-      PRIMARY KEY (identity_name, key_identifier)                    \n \
-  );                                                                 \n \
-                                                                     \
-CREATE INDEX key_index ON Key(identity_name);                        \n \
-";
+static const string INIT_TPM_INFO_TABLE =
+  "CREATE TABLE IF NOT EXISTS                "
+  "  TpmInfo(                                "
+  "      tpm_locator           BLOB NOT NULL,"
+  "      PRIMARY KEY (tpm_locator)           "
+  "  );                                      ";
 
-static const string INIT_CERT_TABLE = "\
-CREATE TABLE IF NOT EXISTS                                           \n \
-  Certificate(                                                       \n \
-      cert_name         BLOB NOT NULL,                               \n \
-      cert_issuer       BLOB NOT NULL,                               \n \
-      identity_name     BLOB NOT NULL,                               \n \
-      key_identifier    BLOB NOT NULL,                               \n \
-      not_before        TIMESTAMP,                                   \n \
-      not_after         TIMESTAMP,                                   \n \
-      certificate_data  BLOB NOT NULL,                               \n \
-      valid_flag        INTEGER DEFAULT 1,                           \n \
-      default_cert      INTEGER DEFAULT 0,                           \n \
-                                                                     \
-      PRIMARY KEY (cert_name)                                        \n \
-  );                                                                 \n \
-                                                                     \
-CREATE INDEX cert_index ON Certificate(cert_name);           \n \
-CREATE INDEX subject ON Certificate(identity_name);          \n \
-";
+static const string INIT_ID_TABLE =
+  "CREATE TABLE IF NOT EXISTS                             "
+  "  Identity(                                            "
+  "      identity_name     BLOB NOT NULL,                 "
+  "      default_identity  INTEGER DEFAULT 0,             "
+  "      PRIMARY KEY (identity_name)                      "
+  "  );                                                   "
+  "CREATE INDEX identity_index ON Identity(identity_name);";
+
+static const string INIT_KEY_TABLE =
+  "CREATE TABLE IF NOT EXISTS                       "
+  "  Key(                                           "
+  "      identity_name     BLOB NOT NULL,           "
+  "      key_identifier    BLOB NOT NULL,           "
+  "      key_type          INTEGER,                 "
+  "      public_key        BLOB,                    "
+  "      default_key       INTEGER DEFAULT 0,       "
+  "      active            INTEGER DEFAULT 0,       "
+  "      PRIMARY KEY (identity_name, key_identifier)"
+  "  );                                             "
+  "CREATE INDEX key_index ON Key(identity_name);    ";
+
+
+static const string INIT_CERT_TABLE =
+  "CREATE TABLE IF NOT EXISTS                         "
+  "  Certificate(                                     "
+  "      cert_name         BLOB NOT NULL,             "
+  "      cert_issuer       BLOB NOT NULL,             "
+  "      identity_name     BLOB NOT NULL,             "
+  "      key_identifier    BLOB NOT NULL,             "
+  "      not_before        TIMESTAMP,                 "
+  "      not_after         TIMESTAMP,                 "
+  "      certificate_data  BLOB NOT NULL,             "
+  "      valid_flag        INTEGER DEFAULT 1,         "
+  "      default_cert      INTEGER DEFAULT 0,         "
+  "      PRIMARY KEY (cert_name)                      "
+  "  );                                               "
+  "CREATE INDEX cert_index ON Certificate(cert_name); "
+  "CREATE INDEX subject ON Certificate(identity_name);";
 
 /**
  * A utility function to call the normal sqlite3_bind_text where the value and length are
  * value.c_str() and value.size().
  */
 static int
-sqlite3_bind_text(sqlite3_stmt* statement,
-                  int index,
-                  const string& value,
-                  void(*destructor)(void*))
+sqlite3_bind_string(sqlite3_stmt* statement,
+                    int index,
+                    const string& value,
+                    void(*destructor)(void*))
 {
   return sqlite3_bind_text(statement, index, value.c_str(), value.size(), destructor);
 }
 
-SecPublicInfoSqlite3::SecPublicInfoSqlite3()
-  : m_database(nullptr)
+static string
+sqlite3_column_string(sqlite3_stmt* statement, int column)
 {
-  boost::filesystem::path identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
+  return string(reinterpret_cast<const char*>(sqlite3_column_text(statement, column)),
+                sqlite3_column_bytes(statement, column));
+}
+
+SecPublicInfoSqlite3::SecPublicInfoSqlite3(const std::string& dir)
+  : SecPublicInfo(dir)
+  , m_database(nullptr)
+{
+  boost::filesystem::path identityDir;
+  if (dir == "")
+    identityDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
+  else
+    identityDir = boost::filesystem::path(dir) / ".ndn";
   boost::filesystem::create_directories(identityDir);
 
   /// @todo Add define for windows/unix in wscript. The following may completely fail on windows
@@ -119,71 +132,13 @@
   if (res != SQLITE_OK)
     throw Error("identity DB cannot be opened/created");
 
+
   BOOST_ASSERT(m_database != nullptr);
 
-  //Check if Key table exists;
-  sqlite3_stmt* statement;
-  sqlite3_prepare_v2(m_database,
-                     "SELECT name FROM sqlite_master WHERE type='table' And name='Identity'",
-                     -1, &statement, 0);
-  res = sqlite3_step(statement);
-
-  bool idTableExists = false;
-  if (res == SQLITE_ROW)
-    idTableExists = true;
-
-  sqlite3_finalize(statement);
-
-  if (!idTableExists) {
-    char* errorMessage = 0;
-    res = sqlite3_exec(m_database, INIT_ID_TABLE.c_str(), NULL, NULL, &errorMessage);
-
-    if (res != SQLITE_OK && errorMessage != 0) {
-      sqlite3_free(errorMessage);
-    }
-  }
-
-  //Check if Key table exists;
-  sqlite3_prepare_v2(m_database,
-                     "SELECT name FROM sqlite_master WHERE type='table' And name='Key'",
-                     -1, &statement, 0);
-  res = sqlite3_step(statement);
-
-  bool keyTableExists = false;
-  if (res == SQLITE_ROW)
-    keyTableExists = true;
-
-  sqlite3_finalize(statement);
-
-  if (!keyTableExists) {
-    char* errorMessage = 0;
-    res = sqlite3_exec(m_database, INIT_KEY_TABLE.c_str(), NULL, NULL, &errorMessage);
-
-    if (res != SQLITE_OK && errorMessage != 0) {
-      sqlite3_free(errorMessage);
-    }
-  }
-
-  //Check if Certificate table exists;
-  sqlite3_prepare_v2(m_database,
-                     "SELECT name FROM sqlite_master WHERE type='table' And name='Certificate'",
-                     -1, &statement, 0);
-  res = sqlite3_step(statement);
-
-  bool idCertificateTableExists = false;
-  if (res == SQLITE_ROW)
-    idCertificateTableExists = true;
-
-  sqlite3_finalize(statement);
-
-  if (!idCertificateTableExists) {
-    char* errorMessage = 0;
-    res = sqlite3_exec(m_database, INIT_CERT_TABLE.c_str(), NULL, NULL, &errorMessage);
-
-    if (res != SQLITE_OK && errorMessage != 0) {
-      sqlite3_free(errorMessage);
-    }
-  }
+  initializeTable("TpmInfo", INIT_TPM_INFO_TABLE); // Check if TpmInfo table exists;
+  initializeTable("Identity", INIT_ID_TABLE);      // Check if Identity table exists;
+  initializeTable("Key", INIT_KEY_TABLE);          // Check if Key table exists;
+  initializeTable("Certificate", INIT_CERT_TABLE); // Check if Certificate table exists;
 }
 
 SecPublicInfoSqlite3::~SecPublicInfoSqlite3()
@@ -193,6 +148,126 @@
 }
 
 bool
+SecPublicInfoSqlite3::doesTableExist(const string& tableName)
+{
+  // Check if the table exists;
+  bool doesTableExist = false;
+  string checkingString =
+    "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
+
+  sqlite3_stmt* statement;
+  sqlite3_prepare_v2(m_database, checkingString.c_str(), -1, &statement, 0);
+
+  int result = sqlite3_step(statement);
+  if (result == SQLITE_ROW)
+    doesTableExist = true;
+  sqlite3_finalize(statement);
+
+  return doesTableExist;
+}
+
+bool
+SecPublicInfoSqlite3::initializeTable(const string& tableName, const string& initCommand)
+{
+  // Create the table if it does not exist
+  if (!doesTableExist(tableName)) {
+    char* errorMessage = 0;
+    int result = sqlite3_exec(m_database, initCommand.c_str(), NULL, NULL, &errorMessage);
+
+    if (result != SQLITE_OK && errorMessage != 0) {
+      sqlite3_free(errorMessage);
+      return false;
+    }
+  }
+
+  return true;
+}
+
+void
+SecPublicInfoSqlite3::deleteTable(const string& tableName)
+{
+  string query = "DROP TABLE IF EXISTS " + tableName;
+
+  sqlite3_stmt* statement;
+  sqlite3_prepare_v2(m_database, query.c_str(), -1, &statement, 0);
+
+  sqlite3_step(statement);
+  sqlite3_finalize(statement);
+}
+
+void
+SecPublicInfoSqlite3::setTpmLocator(const string& tpmLocator)
+{
+  string currentTpm;
+  try {
+    currentTpm = getTpmLocator();
+  }
+  catch (SecPublicInfo::Error&) {
+    setTpmLocatorInternal(tpmLocator, false); // set tpmInfo without resetting
+    return;
+  }
+
+  if (currentTpm == tpmLocator)
+    return; // if the same, nothing will be changed
+
+  setTpmLocatorInternal(tpmLocator, true); // set tpmInfo and reset pib
+}
+
+string
+SecPublicInfoSqlite3::getTpmLocator()
+{
+  sqlite3_stmt* statement;
+  sqlite3_prepare_v2(m_database, "SELECT tpm_locator FROM TpmInfo", -1, &statement, 0);
+
+  int res = sqlite3_step(statement);
+
+  if (res == SQLITE_ROW) {
+    string tpmLocator = sqlite3_column_string(statement, 0);
+    sqlite3_finalize(statement);
+    return tpmLocator;
+  }
+  else {
+    sqlite3_finalize(statement);
+    throw SecPublicInfo::Error("TPM info does not exist");
+  }
+}
+
+void
+SecPublicInfoSqlite3::setTpmLocatorInternal(const string& tpmLocator, bool needReset)
+{
+  sqlite3_stmt* statement;
+
+  if (needReset) {
+    deleteTable("Identity");
+    deleteTable("Key");
+    deleteTable("Certificate");
+
+    initializeTable("Identity", INIT_ID_TABLE);
+    initializeTable("Key", INIT_KEY_TABLE);
+    initializeTable("Certificate", INIT_CERT_TABLE);
+
+    sqlite3_prepare_v2(m_database, "UPDATE TpmInfo SET tpm_locator = ?",
+                       -1, &statement, 0);
+    sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
+  }
+  else {
+    // no reset implies there is no tpmLocator record, insert one
+    sqlite3_prepare_v2(m_database, "INSERT INTO TpmInfo (tpm_locator) VALUES (?)",
+                       -1, &statement, 0);
+    sqlite3_bind_string(statement, 1, tpmLocator, SQLITE_TRANSIENT);
+  }
+
+  sqlite3_step(statement);
+  sqlite3_finalize(statement);
+}
+
+std::string
+SecPublicInfoSqlite3::getPibLocator()
+{
+  return string("pib-sqlite3:").append(m_location);
+}
+
+bool
 SecPublicInfoSqlite3::doesIdentityExist(const Name& identityName)
 {
   bool result = false;
@@ -202,7 +277,7 @@
                      "SELECT count(*) FROM Identity WHERE identity_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
   int res = sqlite3_step(statement);
 
   if (res == SQLITE_ROW) {
@@ -228,7 +303,7 @@
                      "INSERT OR REPLACE INTO Identity (identity_name) values (?)",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
 
   sqlite3_step(statement);
 
@@ -256,8 +331,8 @@
                      "SELECT count(*) FROM Key WHERE identity_name=? AND key_identifier=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -295,8 +370,8 @@
                       values (?, ?, ?, ?)",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
   sqlite3_bind_int(statement, 3, publicKeyDer.getKeyType());
   sqlite3_bind_blob(statement, 4,
                     publicKeyDer.get().buf(),
@@ -324,8 +399,8 @@
                      "SELECT public_key FROM Key WHERE identity_name=? AND key_identifier=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -359,8 +434,8 @@
                      "SELECT key_type FROM Key WHERE identity_name=? AND key_identifier=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -386,7 +461,7 @@
                      "SELECT count(*) FROM Certificate WHERE cert_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -479,22 +554,22 @@
                       values (?, ?, ?, ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)",
                       -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
 
   try
     {
       // this will throw an exception if the signature is not the standard one
       // or there is no key locator present
       std::string signerName = certificate.getSignature().getKeyLocator().getName().toUri();
-      sqlite3_bind_text(statement, 2, signerName, SQLITE_TRANSIENT);
+      sqlite3_bind_string(statement, 2, signerName, SQLITE_TRANSIENT);
     }
   catch (tlv::Error& e)
     {
       return;
     }
 
-  sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
+  sqlite3_bind_string(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 4, keyId, SQLITE_STATIC);
 
   sqlite3_bind_int64(statement, 5,
                      static_cast<sqlite3_int64>(
@@ -522,7 +597,7 @@
                      "SELECT certificate_data FROM Certificate WHERE cert_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, certificateName.toUri(), SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -554,8 +629,7 @@
 
   if (res == SQLITE_ROW)
     {
-      Name identity(string(reinterpret_cast<const char *>(sqlite3_column_text(statement, 0)),
-                           sqlite3_column_bytes(statement, 0)));
+      Name identity(sqlite3_column_string(statement, 0));
       sqlite3_finalize(statement);
       return identity;
     }
@@ -589,7 +663,7 @@
                      "UPDATE Identity SET default_identity=1 WHERE identity_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
 
   sqlite3_step(statement);
 
@@ -604,7 +678,7 @@
                      "SELECT key_identifier FROM Key WHERE identity_name=? AND default_key=1",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -639,7 +713,7 @@
                      "UPDATE Key SET default_key=0 WHERE default_key=1 and identity_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
 
   while (sqlite3_step(statement) == SQLITE_ROW)
     ;
@@ -651,8 +725,8 @@
                      "UPDATE Key SET default_key=1 WHERE identity_name=? AND key_identifier=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   sqlite3_step(statement);
 
@@ -674,8 +748,8 @@
                       WHERE identity_name=? AND key_identifier=? AND default_cert=1",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   int res = sqlite3_step(statement);
 
@@ -711,8 +785,8 @@
                       WHERE default_cert=1 AND identity_name=? AND key_identifier=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
 
   while (sqlite3_step(statement) == SQLITE_ROW)
     ;
@@ -725,9 +799,9 @@
                       WHERE identity_name=? AND key_identifier=? AND cert_name=?",
                      -1, &statement, 0);
 
-  sqlite3_bind_text(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 2, keyId, SQLITE_TRANSIENT);
-  sqlite3_bind_text(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 1, identityName.toUri(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 2, keyId, SQLITE_TRANSIENT);
+  sqlite3_bind_string(statement, 3, certificateName.toUri(), SQLITE_TRANSIENT);
 
   sqlite3_step(statement);
 
@@ -795,10 +869,7 @@
                        "SELECT key_identifier FROM Key WHERE default_key=0 and identity_name=?",
                        -1, &stmt, 0);
 
-  sqlite3_bind_text(stmt, 1,
-                    identity.toUri().c_str(),
-                    identity.toUri().size(),
-                    SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
 
   while (sqlite3_step(stmt) == SQLITE_ROW)
     {
@@ -852,10 +923,10 @@
                        -1, &stmt, 0);
 
   Name identity = keyName.getPrefix(-1);
-  sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity.toUri(), SQLITE_TRANSIENT);
 
   std::string baseKeyName = keyName.get(-1).toUri();
-  sqlite3_bind_text(stmt, 2, baseKeyName.c_str(), baseKeyName.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 2, baseKeyName, SQLITE_TRANSIENT);
 
   while (sqlite3_step(stmt) == SQLITE_ROW)
     nameList.push_back(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
@@ -872,7 +943,7 @@
 
   sqlite3_stmt* stmt;
   sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE cert_name=?", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, certName.toUri().c_str(), certName.toUri().size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, certName.toUri(), SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 }
@@ -890,16 +961,16 @@
   sqlite3_prepare_v2(m_database,
                      "DELETE FROM Certificate WHERE identity_name=? and key_identifier=?",
                      -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 
   sqlite3_prepare_v2(m_database,
                      "DELETE FROM Key WHERE identity_name=? and key_identifier=?",
                      -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
-  sqlite3_bind_text(stmt, 2, keyId.c_str(), keyId.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 2, keyId, SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 }
@@ -911,19 +982,25 @@
 
   sqlite3_stmt* stmt;
   sqlite3_prepare_v2(m_database, "DELETE FROM Certificate WHERE identity_name=?", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 
   sqlite3_prepare_v2(m_database, "DELETE FROM Key WHERE identity_name=?", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 
   sqlite3_prepare_v2(m_database, "DELETE FROM Identity WHERE identity_name=?", -1, &stmt, 0);
-  sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
+  sqlite3_bind_string(stmt, 1, identity, SQLITE_TRANSIENT);
   sqlite3_step(stmt);
   sqlite3_finalize(stmt);
 }
 
+std::string
+SecPublicInfoSqlite3::getScheme()
+{
+  return SCHEME;
+}
+
 } // namespace ndn