do some file move and rename

Change-Id: I034ea026c52c588966d2c66fcb3d3deea2e40102
diff --git a/src/ca-storage/ca-memory.cpp b/src/ca-storage/ca-memory.cpp
new file mode 100644
index 0000000..1fe27e1
--- /dev/null
+++ b/src/ca-storage/ca-memory.cpp
@@ -0,0 +1,100 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "ca-memory.hpp"
+
+#include <ndn-cxx/security/v2/validation-policy.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+const std::string
+CaMemory::STORAGE_TYPE = "ca-storage-memory";
+
+NDNCERT_REGISTER_CA_STORAGE(CaMemory);
+
+CaMemory::CaMemory(const Name& caName, const std::string& path)
+  : CaStorage()
+{
+}
+
+CaState
+CaMemory::getRequest(const std::string& requestId)
+{
+  auto search = m_requests.find(requestId);
+  if (search == m_requests.end()) {
+    BOOST_THROW_EXCEPTION(std::runtime_error("Request " + requestId + " doest not exists"));
+  }
+  return search->second;
+}
+
+void
+CaMemory::addRequest(const CaState& request)
+{
+  auto search = m_requests.find(request.m_requestId);
+  if (search == m_requests.end()) {
+    m_requests[request.m_requestId] = request;
+  }
+  else {
+    BOOST_THROW_EXCEPTION(std::runtime_error("Request " + request.m_requestId + " already exists"));
+  }
+}
+
+void
+CaMemory::updateRequest(const CaState& request)
+{
+  m_requests[request.m_requestId].m_status = request.m_status;
+  m_requests[request.m_requestId].m_challengeState = request.m_challengeState;
+}
+
+void
+CaMemory::deleteRequest(const std::string& requestId)
+{
+  auto search = m_requests.find(requestId);
+  auto keyName = search->second.m_cert.getKeyName();
+  if (search != m_requests.end()) {
+    m_requests.erase(search);
+  }
+}
+
+std::list<CaState>
+CaMemory::listAllRequests()
+{
+  std::list<CaState> result;
+  for (const auto& entry : m_requests) {
+    result.push_back(entry.second);
+  }
+  return result;
+}
+
+std::list<CaState>
+CaMemory::listAllRequests(const Name& caName)
+{
+  std::list<CaState> result;
+  for (const auto& entry : m_requests) {
+    if (entry.second.m_caPrefix == caName) {
+      result.push_back(entry.second);
+    }
+  }
+  return result;
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/ca-storage/ca-memory.hpp b/src/ca-storage/ca-memory.hpp
new file mode 100644
index 0000000..842121b
--- /dev/null
+++ b/src/ca-storage/ca-memory.hpp
@@ -0,0 +1,67 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_CA_DETAIL_CA_MEMORY_HPP
+#define NDNCERT_CA_DETAIL_CA_MEMORY_HPP
+
+#include "ca-storage.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+class CaMemory : public CaStorage
+{
+public:
+  CaMemory(const Name& caName = Name(), const std::string& path = "");
+  const static std::string STORAGE_TYPE;
+
+public:
+  /**
+   * @throw if request cannot be fetched from underlying data storage
+   */
+  CaState
+  getRequest(const std::string& requestId) override;
+
+  /**
+   * @throw if there is an existing request with the same request ID
+   */
+  void
+  addRequest(const CaState& request) override;
+
+  void
+  updateRequest(const CaState& request) override;
+
+  void
+  deleteRequest(const std::string& requestId) override;
+
+  std::list<CaState>
+  listAllRequests() override;
+
+  std::list<CaState>
+  listAllRequests(const Name& caName) override;
+
+private:
+  std::map<Name, CaState> m_requests;
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CA_DETAIL_CA_MEMORY_HPP
diff --git a/src/ca-storage/ca-sqlite.cpp b/src/ca-storage/ca-sqlite.cpp
new file mode 100644
index 0000000..8292f36
--- /dev/null
+++ b/src/ca-storage/ca-sqlite.cpp
@@ -0,0 +1,288 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "ca-sqlite.hpp"
+
+#include <sqlite3.h>
+
+#include <boost/filesystem.hpp>
+#include <ndn-cxx/security/v2/validation-policy.hpp>
+#include <ndn-cxx/util/sqlite3-statement.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+const std::string CaSqlite::STORAGE_TYPE = "ca-storage-sqlite3";
+
+NDNCERT_REGISTER_CA_STORAGE(CaSqlite);
+
+using namespace ndn::util;
+
+static const std::string INITIALIZATION = R"_DBTEXT_(
+CREATE TABLE IF NOT EXISTS
+  CaStates(
+    id INTEGER PRIMARY KEY,
+    request_id TEXT NOT NULL,
+    ca_name BLOB NOT NULL,
+    request_type INTEGER NOT NULL,
+    status INTEGER NOT NULL,
+    cert_request BLOB NOT NULL,
+    challenge_type TEXT,
+    challenge_status TEXT,
+    challenge_tp TEXT,
+    remaining_tries INTEGER,
+    remaining_time INTEGER,
+    challenge_secrets TEXT,
+    encryption_key BLOB NOT NULL
+  );
+CREATE UNIQUE INDEX IF NOT EXISTS
+  CaStateIdIndex ON CaStates(request_id);
+)_DBTEXT_";
+
+CaSqlite::CaSqlite(const Name& caName, const std::string& path)
+    : CaStorage()
+{
+  // Determine the path of sqlite db
+  boost::filesystem::path dbDir;
+  if (!path.empty()) {
+    dbDir = boost::filesystem::path(path);
+  }
+  else {
+    std::string dbName = caName.toUri();
+    std::replace(dbName.begin(), dbName.end(), '/', '_');
+    dbName += ".db";
+    if (getenv("HOME") != nullptr) {
+      dbDir = boost::filesystem::path(getenv("HOME")) / ".ndncert";
+    }
+    else {
+      dbDir = boost::filesystem::current_path() / ".ndncert";
+    }
+    boost::filesystem::create_directories(dbDir);
+    dbDir /= dbName;
+  }
+
+  // open and initialize database
+  int result = sqlite3_open_v2(dbDir.c_str(), &m_database,
+                               SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
+#ifdef NDN_CXX_DISABLE_SQLITE3_FS_LOCKING
+                               "unix-dotfile"
+#else
+                               nullptr
+#endif
+  );
+  if (result != SQLITE_OK)
+    BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be opened/created: " + dbDir.string()));
+
+  // initialize database specific tables
+  char* errorMessage = nullptr;
+  result = sqlite3_exec(m_database, INITIALIZATION.data(),
+                        nullptr, nullptr, &errorMessage);
+  if (result != SQLITE_OK && errorMessage != nullptr) {
+    sqlite3_free(errorMessage);
+    BOOST_THROW_EXCEPTION(std::runtime_error("CaSqlite DB cannot be initialized"));
+  }
+}
+
+CaSqlite::~CaSqlite()
+{
+  sqlite3_close(m_database);
+}
+
+CaState
+CaSqlite::getRequest(const std::string& requestId)
+{
+  Sqlite3Statement statement(m_database,
+                             R"_SQLTEXT_(SELECT id, ca_name, status,
+                             challenge_status, cert_request,
+                             challenge_type, challenge_secrets,
+                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             FROM CaStates where request_id = ?)_SQLTEXT_");
+  statement.bind(1, requestId, SQLITE_TRANSIENT);
+
+  if (statement.step() == SQLITE_ROW) {
+    Name caName(statement.getBlock(1));
+    auto status = static_cast<Status>(statement.getInt(2));
+    auto challengeStatus = statement.getString(3);
+    security::v2::Certificate cert(statement.getBlock(4));
+    auto challengeType = statement.getString(5);
+    auto challengeSecrets = statement.getString(6);
+    auto challengeTp = statement.getString(7);
+    auto remainingTries = statement.getInt(8);
+    auto remainingTime = statement.getInt(9);
+    auto requestType = static_cast<RequestType>(statement.getInt(10));
+    auto encryptionKey = statement.getBlock(11);
+    if (challengeType != "") {
+      return CaState(caName, requestId, requestType, status, cert,
+                     challengeType, challengeStatus, time::fromIsoString(challengeTp),
+                     remainingTries, time::seconds(remainingTime),
+                     convertString2Json(challengeSecrets), encryptionKey);
+    }
+    else {
+      return CaState(caName, requestId, requestType, status, cert, encryptionKey);
+    }
+  }
+  else {
+    BOOST_THROW_EXCEPTION(std::runtime_error("Request " + requestId + " cannot be fetched from database"));
+  }
+}
+
+void
+CaSqlite::addRequest(const CaState& request)
+{
+  Sqlite3Statement statement(
+      m_database,
+      R"_SQLTEXT_(INSERT OR ABORT INTO CaStates (request_id, ca_name, status, request_type,
+                  cert_request, challenge_type, challenge_status, challenge_secrets,
+                  challenge_tp, remaining_tries, remaining_time, encryption_key)
+                  values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
+  statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
+  statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
+  statement.bind(3, static_cast<int>(request.m_status));
+  statement.bind(4, static_cast<int>(request.m_requestType));
+  statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
+  statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT);
+  if (request.m_challengeState) {
+    statement.bind(6, request.m_challengeType, SQLITE_TRANSIENT);
+    statement.bind(7, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
+    statement.bind(8, convertJson2String(request.m_challengeState->m_secrets),
+                   SQLITE_TRANSIENT);
+    statement.bind(9, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
+    statement.bind(10, request.m_challengeState->m_remainingTries);
+    statement.bind(11, request.m_challengeState->m_remainingTime.count());
+  }
+  if (statement.step() != SQLITE_DONE) {
+    BOOST_THROW_EXCEPTION(std::runtime_error("Request " + request.m_requestId + " cannot be added to database"));
+  }
+}
+
+void
+CaSqlite::updateRequest(const CaState& request)
+{
+  Sqlite3Statement statement(m_database,
+                             R"_SQLTEXT_(UPDATE CaStates
+                             SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
+                             challenge_tp = ?, remaining_tries = ?, remaining_time = ?
+                             WHERE request_id = ?)_SQLTEXT_");
+  statement.bind(1, static_cast<int>(request.m_status));
+  statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT);
+  if (request.m_challengeState) {
+    statement.bind(3, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
+    statement.bind(4, convertJson2String(request.m_challengeState->m_secrets),
+                   SQLITE_TRANSIENT);
+    statement.bind(5, time::toIsoString(request.m_challengeState->m_timestamp), SQLITE_TRANSIENT);
+    statement.bind(6, request.m_challengeState->m_remainingTries);
+    statement.bind(7, request.m_challengeState->m_remainingTime.count());
+  }
+  else {
+    statement.bind(3, "", SQLITE_TRANSIENT);
+    statement.bind(4, "", SQLITE_TRANSIENT);
+    statement.bind(5, "", SQLITE_TRANSIENT);
+    statement.bind(6, 0);
+    statement.bind(7, 0);
+  }
+  statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
+
+  if (statement.step() != SQLITE_DONE) {
+    addRequest(request);
+  }
+}
+
+std::list<CaState>
+CaSqlite::listAllRequests()
+{
+  std::list<CaState> result;
+  Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
+                             challenge_status, cert_request, challenge_type, challenge_secrets,
+                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             FROM CaStates)_SQLTEXT_");
+  while (statement.step() == SQLITE_ROW) {
+    auto requestId = statement.getString(1);
+    Name caName(statement.getBlock(2));
+    auto status = static_cast<Status>(statement.getInt(3));
+    auto challengeStatus = statement.getString(4);
+    security::v2::Certificate cert(statement.getBlock(5));
+    auto challengeType = statement.getString(6);
+    auto challengeSecrets = statement.getString(7);
+    auto challengeTp = statement.getString(8);
+    auto remainingTries = statement.getInt(9);
+    auto remainingTime = statement.getInt(10);
+    auto requestType = static_cast<RequestType>(statement.getInt(11));
+    auto encryptionKey = statement.getBlock(12);
+    if (challengeType != "") {
+      result.push_back(CaState(caName, requestId, requestType, status, cert,
+                               challengeType, challengeStatus, time::fromIsoString(challengeTp),
+                               remainingTries, time::seconds(remainingTime),
+                               convertString2Json(challengeSecrets), encryptionKey));
+    }
+    else {
+      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
+    }
+  }
+  return result;
+}
+
+std::list<CaState>
+CaSqlite::listAllRequests(const Name& caName)
+{
+  std::list<CaState> result;
+  Sqlite3Statement statement(m_database,
+                             R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
+                             challenge_status, cert_request, challenge_type, challenge_secrets,
+                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
+  statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
+
+  while (statement.step() == SQLITE_ROW) {
+    auto requestId = statement.getString(1);
+    Name caName(statement.getBlock(2));
+    auto status = static_cast<Status>(statement.getInt(3));
+    auto challengeStatus = statement.getString(4);
+    security::v2::Certificate cert(statement.getBlock(5));
+    auto challengeType = statement.getString(6);
+    auto challengeSecrets = statement.getString(7);
+    auto challengeTp = statement.getString(8);
+    auto remainingTries = statement.getInt(9);
+    auto remainingTime = statement.getInt(10);
+    auto requestType = static_cast<RequestType>(statement.getInt(11));
+    auto encryptionKey = statement.getBlock(12);
+    if (challengeType != "") {
+      result.push_back(CaState(caName, requestId, requestType, status, cert,
+                               challengeType, challengeStatus, time::fromIsoString(challengeTp),
+                               remainingTries, time::seconds(remainingTime),
+                               convertString2Json(challengeSecrets), encryptionKey));
+    }
+    else {
+      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
+    }
+  }
+  return result;
+}
+
+void
+CaSqlite::deleteRequest(const std::string& requestId)
+{
+  Sqlite3Statement statement(m_database,
+                             R"_SQLTEXT_(DELETE FROM CaStates WHERE request_id = ?)_SQLTEXT_");
+  statement.bind(1, requestId, SQLITE_TRANSIENT);
+  statement.step();
+}
+
+}  // namespace ndncert
+}  // namespace ndn
diff --git a/src/ca-storage/ca-sqlite.hpp b/src/ca-storage/ca-sqlite.hpp
new file mode 100644
index 0000000..5d12188
--- /dev/null
+++ b/src/ca-storage/ca-sqlite.hpp
@@ -0,0 +1,73 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_CA_DETAIL_CA_SQLITE_HPP
+#define NDNCERT_CA_DETAIL_CA_SQLITE_HPP
+
+#include "ca-storage.hpp"
+
+struct sqlite3;
+
+namespace ndn {
+namespace ndncert {
+
+class CaSqlite : public CaStorage
+{
+public:
+  const static std::string STORAGE_TYPE;
+
+  explicit
+  CaSqlite(const Name& caName, const std::string& path = "");
+
+  ~CaSqlite();
+
+public:
+  /**
+   * @throw if request cannot be fetched from underlying data storage
+   */
+  CaState
+  getRequest(const std::string& requestId) override;
+
+  /**
+   * @throw if there is an existing request with the same request ID
+   */
+  void
+  addRequest(const CaState& request) override;
+
+  void
+  updateRequest(const CaState& request) override;
+
+  void
+  deleteRequest(const std::string& requestId) override;
+
+  std::list<CaState>
+  listAllRequests() override;
+
+  std::list<CaState>
+  listAllRequests(const Name& caName) override;
+
+private:
+  sqlite3* m_database;
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CA_DETAIL_CA_SQLITE_HPP
diff --git a/src/ca-storage/ca-storage.cpp b/src/ca-storage/ca-storage.cpp
new file mode 100644
index 0000000..dfcced2
--- /dev/null
+++ b/src/ca-storage/ca-storage.cpp
@@ -0,0 +1,42 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2019, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "ca-storage/ca-storage.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+unique_ptr<CaStorage>
+CaStorage::createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path)
+{
+  CaStorageFactory& factory = getFactory();
+  auto i = factory.find(caStorageType);
+  return i == factory.end() ? nullptr : i->second(caName, path);
+}
+
+CaStorage::CaStorageFactory&
+CaStorage::getFactory()
+{
+  static CaStorage::CaStorageFactory factory;
+  return factory;
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/ca-storage/ca-storage.hpp b/src/ca-storage/ca-storage.hpp
new file mode 100644
index 0000000..7fbc1ec
--- /dev/null
+++ b/src/ca-storage/ca-storage.hpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2019, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_CA_STORAGE_HPP
+#define NDNCERT_CA_STORAGE_HPP
+
+#include "ca-state.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+class CaStorage : noncopyable
+{
+public: // request related
+  /**
+   * @throw if request cannot be fetched from underlying data storage
+   */
+  virtual CaState
+  getRequest(const std::string& requestId) = 0;
+
+  /**
+   * @throw if there is an existing request with the same request ID
+   */
+  virtual void
+  addRequest(const CaState& request) = 0;
+
+  virtual void
+  updateRequest(const CaState& request) = 0;
+
+  virtual void
+  deleteRequest(const std::string& requestId) = 0;
+
+  virtual std::list<CaState>
+  listAllRequests() = 0;
+
+  virtual std::list<CaState>
+  listAllRequests(const Name& caName) = 0;
+
+public: // factory
+  template<class CaStorageType>
+  static void
+  registerCaStorage(const std::string& caStorageType = CaStorageType::STORAGE_TYPE)
+  {
+    CaStorageFactory& factory = getFactory();
+    BOOST_ASSERT(factory.count(caStorageType) == 0);
+    factory[caStorageType] = [] (const Name& caName, const std::string& path) {
+      return make_unique<CaStorageType>(caName, path);
+    };
+  }
+
+  static unique_ptr<CaStorage>
+  createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path);
+
+  virtual
+  ~CaStorage() = default;
+
+private:
+  using CaStorageCreateFunc = function<unique_ptr<CaStorage> (const Name&, const std::string&)>;
+  using CaStorageFactory = std::map<std::string, CaStorageCreateFunc>;
+
+  static CaStorageFactory&
+  getFactory();
+};
+
+#define NDNCERT_REGISTER_CA_STORAGE(C)                           \
+static class NdnCert ## C ## CaStorageRegistrationClass          \
+{                                                                \
+public:                                                          \
+  NdnCert ## C ## CaStorageRegistrationClass()                   \
+  {                                                              \
+    ::ndn::ndncert::CaStorage::registerCaStorage<C>();           \
+  }                                                              \
+} g_NdnCert ## C ## CaStorageRegistrationVariable
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CA_STORAGE_HPP