Add list function

Change-Id: I3923d4f12e2134250b5a61419d53582e5450bdde
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 38b23b8..f2aa498 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -21,7 +21,7 @@
 #include "ca-module.hpp"
 #include "challenge-module.hpp"
 #include "logging.hpp"
-#include <ndn-cxx/face.hpp>
+#include <ndn-cxx/util/io.hpp>
 #include <ndn-cxx/security/verification-helpers.hpp>
 #include <ndn-cxx/security/signing-helpers.hpp>
 #include <ndn-cxx/util/random.hpp>
@@ -40,48 +40,7 @@
   m_config.load(configPath);
   m_storage = CaStorage::createCaStorage(storageType);
 
-  // register prefix
-  for (const auto& item : m_config.m_caItems) {
-    Name prefix = item.m_caName;
-    prefix.append("CA");
-    try {
-      const RegisteredPrefixId* prefixId = m_face.registerPrefix(prefix,
-        [&] (const Name& name) {
-          // NEW
-          const InterestFilterId* filterId = m_face.setInterestFilter(Name(name).append("_NEW"),
-                                              bind(&CaModule::handleNew, this, _2, item));
-          m_interestFilterIds.push_back(filterId);
-          // SELECT
-          filterId = m_face.setInterestFilter(Name(name).append("_SELECT"),
-                                              bind(&CaModule::handleSelect, this, _2, item));
-          m_interestFilterIds.push_back(filterId);
-          // VALIDATE
-          filterId = m_face.setInterestFilter(Name(name).append("_VALIDATE"),
-                                              bind(&CaModule::handleValidate, this, _2, item));
-          m_interestFilterIds.push_back(filterId);
-          // STATUS
-          filterId = m_face.setInterestFilter(Name(name).append("_STATUS"),
-                                              bind(&CaModule::handleStatus, this, _2, item));
-          m_interestFilterIds.push_back(filterId);
-          // DOWNLOAD
-          filterId = m_face.setInterestFilter(Name(name).append("_DOWNLOAD"),
-                                              bind(&CaModule::handleDownload, this, _2, item));
-          m_interestFilterIds.push_back(filterId);
-          // PROBE
-          if (item.m_probe != "") {
-            filterId = m_face.setInterestFilter(Name(name).append("_PROBE"),
-                                                bind(&CaModule::handleProbe, this, _2, item));
-            m_interestFilterIds.push_back(filterId);
-          }
-          _LOG_TRACE("Prefix " << name << " got registered");
-        },
-        bind(&CaModule::onRegisterFailed, this, _2));
-      m_registeredPrefixIds.push_back(prefixId);
-    }
-    catch (const std::exception& e) {
-      _LOG_ERROR(e.what());
-    }
-  }
+  registerPrefix();
 }
 
 CaModule::~CaModule()
@@ -95,6 +54,63 @@
 }
 
 void
+CaModule::registerPrefix()
+{
+  // register localhost list prefix
+  Name localProbePrefix("/localhost/CA/_LIST");
+  auto prefixId = m_face.setInterestFilter(InterestFilter(localProbePrefix),
+                                           bind(&CaModule::handleLocalhostList, this, _2),
+                                           bind(&CaModule::onRegisterFailed, this, _2));
+  m_registeredPrefixIds.push_back(prefixId);
+  _LOG_TRACE("Prefix " << localProbePrefix << " got registered");
+
+  // register prefixes for each CA
+  for (const auto& item : m_config.m_caItems) {
+    Name prefix = item.m_caName;
+    prefix.append("CA");
+
+    prefixId = m_face.registerPrefix(prefix,
+      [&] (const Name& name) {
+        // NEW
+        auto filterId = m_face.setInterestFilter(Name(name).append("_NEW"),
+                                                 bind(&CaModule::handleNew, this, _2, item));
+        m_interestFilterIds.push_back(filterId);
+        // SELECT
+        filterId = m_face.setInterestFilter(Name(name).append("_SELECT"),
+                                            bind(&CaModule::handleSelect, this, _2, item));
+        m_interestFilterIds.push_back(filterId);
+        // VALIDATE
+        filterId = m_face.setInterestFilter(Name(name).append("_VALIDATE"),
+                                            bind(&CaModule::handleValidate, this, _2, item));
+        m_interestFilterIds.push_back(filterId);
+        // STATUS
+        filterId = m_face.setInterestFilter(Name(name).append("_STATUS"),
+                                            bind(&CaModule::handleStatus, this, _2, item));
+        m_interestFilterIds.push_back(filterId);
+        // DOWNLOAD
+        filterId = m_face.setInterestFilter(Name(name).append("_DOWNLOAD"),
+                                            bind(&CaModule::handleDownload, this, _2, item));
+        m_interestFilterIds.push_back(filterId);
+        // PROBE
+        if (item.m_probe != "") {
+          filterId = m_face.setInterestFilter(Name(name).append("_PROBE"),
+                                              bind(&CaModule::handleProbe, this, _2, item));
+          m_interestFilterIds.push_back(filterId);
+        }
+        // LIST
+        if (item.m_relatedCaList.size() > 0) {
+          filterId = m_face.setInterestFilter(Name(name).append("_LIST"),
+                                              bind(&CaModule::handleList, this, _2, item));
+          m_interestFilterIds.push_back(filterId);
+        }
+        _LOG_TRACE("Prefix " << name << " got registered");
+      },
+      bind(&CaModule::onRegisterFailed, this, _2));
+    m_registeredPrefixIds.push_back(prefixId);
+  }
+}
+
+void
 CaModule::setProbeHandler(const Name caName, const ProbeHandler& handler)
 {
   for (auto& entry : m_config.m_caItems) {
@@ -125,6 +141,123 @@
 }
 
 void
+CaModule::handleLocalhostList(const Interest& request)
+{
+  _LOG_TRACE("Got Localhost LIST request");
+
+  JsonSection root;
+  JsonSection caListSection;
+
+  for (const auto& entry : m_config.m_caItems) {
+    JsonSection caItem;
+
+    const auto& pib = m_keyChain.getPib();
+    auto identity = pib.getIdentity(entry.m_caName);
+    auto cert = identity.getDefaultKey().getDefaultCertificate();
+
+    // ca-prefix
+    Name caName = entry.m_caName;
+    caName.append("CA");
+    caItem.put("ca-prefix", caName.toUri());
+
+    // ca-info
+    std::string caInfo;
+    if (entry.m_caInfo == "") {
+      caInfo = "Issued by " + cert.getSignature().getKeyLocator().getName().toUri();
+    }
+    else {
+      caInfo = entry.m_caInfo;
+    }
+    caItem.put("ca-info", caInfo);
+
+    // probe is always false for local client
+
+    // ca-target list
+    caItem.put("target-list", entry.m_targetedList);
+
+    // certificate
+    std::stringstream ss;
+    io::save(cert, ss);
+    caItem.put("certificate", ss.str());
+
+    caListSection.push_back(std::make_pair("", caItem));
+  }
+  root.add_child("ca-list", caListSection);
+
+  Data result;
+  Name dataName = request.getName();
+  dataName.appendTimestamp();
+  result.setName(dataName);
+  result.setContent(dataContentFromJson(root));
+  m_keyChain.sign(result, signingByIdentity(m_keyChain.getPib().getDefaultIdentity().getName()));
+  m_face.put(result);
+}
+
+void
+CaModule::handleList(const Interest& request, const CaItem& caItem)
+{
+  _LOG_TRACE("Got LIST request");
+
+  bool getRecommendation = false;
+  Name recommendedCaName;
+  std::string identityName;
+
+  // LIST naming convention: /CA-prefix/CA/_LIST/[optional info]
+  if (readString(request.getName().at(-1)) != "_LIST" && caItem.m_recommendCaHandler) {
+    const auto& additionInfo = readString(request.getName().at(-1));
+    try {
+      std::tie(recommendedCaName, identityName) = caItem.m_recommendCaHandler(additionInfo, caItem.m_relatedCaList);
+      getRecommendation = true;
+    }
+    catch (const std::exception& e) {
+      _LOG_TRACE("Cannot recommend CA for LIST request. Degrade to non-target list." << e.what());
+    }
+  }
+
+  JsonSection root;
+  JsonSection caListSection;
+  if (getRecommendation) {
+    // JSON format
+    // {
+    //   "recommended-ca": "/ndn/edu/ucla"
+    //   "recommended-identity": "something"
+    //   "trust-schema": "schema Data packet name"
+    // }
+    root.put("recommended-ca", recommendedCaName.toUri());
+    root.put("recommended-identity", identityName);
+  }
+  else {
+    // JSON format
+    // {
+    //   "ca-list": [
+    //     {"ca-prefix": "/ndn/edu/ucla"},
+    //     {"ca-prefix": "/ndn/edu/memphis"},
+    //     ...
+    //   ]
+    //   "trust-schema": "schema Data packet name"
+    // }
+    for (const auto& entry : caItem.m_relatedCaList) {
+      JsonSection caItem;
+      caItem.put("ca-prefix", entry.toUri());
+      caListSection.push_back(std::make_pair("", caItem));
+    }
+    root.add_child("ca-list", caListSection);
+  }
+
+  // TODO: add trust schema
+  std::string schemaDataName = "TODO: add trust schema";
+  root.put("trust-schema", schemaDataName);
+
+  Data result;
+  Name dataName = request.getName();
+  dataName.appendTimestamp();
+  result.setName(dataName);
+  result.setContent(dataContentFromJson(root));
+  m_keyChain.sign(result, signingByIdentity(caItem.m_caName));
+  m_face.put(result);
+}
+
+void
 CaModule::handleProbe(const Interest& request, const CaItem& caItem)
 {
   // PROBE Naming Convention: /CA-prefix/CA/_PROBE/<Probe Information>
@@ -346,20 +479,56 @@
   // DOWNLOAD Naming Convention: /CA-prefix/CA/_DOWNLOAD/{Request-ID JSON}
   _LOG_TRACE("Handle DOWNLOAD request");
 
-  JsonSection requestIdJson = jsonFromNameComponent(request.getName(), caItem.m_caName.size() + 2);
-  std::string requestId = requestIdJson.get(JSON_REQUEST_ID, "");
-  security::v2::Certificate signedCert;
-  try {
-    signedCert = m_storage->getCertificate(requestId);
-  }
-  catch (const std::exception& e) {
-    _LOG_ERROR(e.what());
-    return;
-  }
-
   Data result;
   result.setName(request.getName());
-  result.setContent(signedCert.wireEncode());
+  if (readString(request.getName().at(-1)) == "ANCHOR") {
+    JsonSection contentJson;
+
+    const auto& pib = m_keyChain.getPib();
+    auto identity = pib.getIdentity(caItem.m_caName);
+    auto cert = identity.getDefaultKey().getDefaultCertificate();
+
+    // ca-prefix
+    Name caName = caItem.m_caName;
+    caName.append("CA");
+    contentJson.put("ca-prefix", caName.toUri());
+
+    // ca-info
+    std::string caInfo;
+    if (caItem.m_caInfo == "") {
+      caInfo = "Issued by " + cert.getSignature().getKeyLocator().getName().toUri();
+    }
+    else {
+      caInfo = caItem.m_caInfo;
+    }
+    contentJson.put("ca-info", caInfo);
+
+    // probe
+    contentJson.put("probe", caItem.m_probe);
+
+    // ca-target list
+    contentJson.put("target-list", caItem.m_targetedList);
+
+    // certificate
+    std::stringstream ss;
+    io::save(cert, ss);
+    contentJson.put("certificate", ss.str());
+
+    result.setContent(dataContentFromJson(contentJson));
+  }
+  else {
+    JsonSection requestIdJson = jsonFromNameComponent(request.getName(), caItem.m_caName.size() + 2);
+    std::string requestId = requestIdJson.get(JSON_REQUEST_ID, "");
+    security::v2::Certificate signedCert;
+    try {
+      signedCert = m_storage->getCertificate(requestId);
+    }
+    catch (const std::exception& e) {
+      _LOG_ERROR(e.what());
+      return;
+    }
+    result.setContent(signedCert.wireEncode());
+  }
   m_keyChain.sign(result, signingByIdentity(caItem.m_caName));
   m_face.put(result);
 }
diff --git a/src/ca-module.hpp b/src/ca-module.hpp
index 7e3a5e6..5ef9ced 100644
--- a/src/ca-module.hpp
+++ b/src/ca-module.hpp
@@ -67,9 +67,14 @@
   void
   setRequestUpdateCallback(const Name caName, const RequestUpdateCallback& onUpateCallback);
 
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   void
+  handleLocalhostList(const Interest& query);
+
+  void
+  handleList(const Interest& request, const CaItem& caItem);
+
+  void
   handleProbe(const Interest& request, const CaItem& caItem);
 
   void
@@ -102,6 +107,9 @@
   static Block
   dataContentFromJson(const JsonSection& jsonSection);
 
+  void
+  registerPrefix();
+
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   Face& m_face;
   CaConfig m_config;
diff --git a/src/client-config.cpp b/src/client-config.cpp
index 116e1d8..aeeaddc 100644
--- a/src/client-config.cpp
+++ b/src/client-config.cpp
@@ -50,23 +50,24 @@
   auto caList = configSection.get_child("ca-list");
   auto it = caList.begin();
   for (; it != caList.end(); it++) {
-    ClientCaItem item;
-    item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
-    item.m_caInfo = it->second.get<std::string>("ca-info");
-    item.m_probe = it->second.get("probe", "");
-    item.m_targetedList = it->second.get("target-list", "");
-
-    std::istringstream ss(it->second.get<std::string>("certificate"));
-    item.m_anchor = *(io::load<security::v2::Certificate>(ss));
-
-    m_caItems.push_back(item);
+    m_caItems.push_back(extractCaItem(it->second));
   }
+  m_localNdncertAnchor = configSection.get("local-ndncert-anchor", "");
 }
 
-void
-ClientConfig::addNewCaItem(const ClientCaItem& item)
+ClientCaItem
+ClientConfig::extractCaItem(const JsonSection& configSection)
 {
-  m_caItems.push_back(item);
+  ClientCaItem item;
+  item.m_caName = Name(configSection.get<std::string>("ca-prefix"));
+  item.m_caInfo = configSection.get<std::string>("ca-info");
+  item.m_probe = configSection.get("probe", "");
+  item.m_targetedList = configSection.get("target-list", "");
+
+  std::istringstream ss(configSection.get<std::string>("certificate"));
+  item.m_anchor = *(io::load<security::v2::Certificate>(ss));
+
+  return item;
 }
 
 void
diff --git a/src/client-config.hpp b/src/client-config.hpp
index e32eab3..97c282b 100644
--- a/src/client-config.hpp
+++ b/src/client-config.hpp
@@ -76,11 +76,12 @@
   void
   removeCaItem(const Name& caName);
 
+  static ClientCaItem
+  extractCaItem(const JsonSection& configSection);
+
 public:
   std::list<ClientCaItem> m_caItems;
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  JsonSection m_config;
+  std::string m_localNdncertAnchor;
 };
 
 } // namespace ndncert
diff --git a/src/client-module.cpp b/src/client-module.cpp
index 9994c31..e4162d2 100644
--- a/src/client-module.cpp
+++ b/src/client-module.cpp
@@ -22,6 +22,7 @@
 #include "logging.hpp"
 #include "json-helper.hpp"
 #include "challenge-module.hpp"
+#include <ndn-cxx/util/io.hpp>
 #include <ndn-cxx/security/signing-helpers.hpp>
 #include <ndn-cxx/security/verification-helpers.hpp>
 
@@ -38,17 +39,124 @@
 }
 
 void
+ClientModule::requestCaTrustAnchor(const Name& caName, const DataCallback& trustAnchorCallback,
+                                   const ErrorCallback& errorCallback)
+{
+  Name interestName = caName;
+  interestName.append("CA").append("_DOWNLOAD").append("ANCHOR");
+  Interest interest(interestName);
+  interest.setMustBeFresh(true);
+
+  m_face.expressInterest(interest, trustAnchorCallback,
+                         bind(&ClientModule::onNack, this, _1, _2, errorCallback),
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              trustAnchorCallback, errorCallback));
+}
+
+void
+ClientModule::requestLocalhostList(const LocalhostListCallback& listCallback,
+                                   const ErrorCallback& errorCallback)
+{
+  Interest interest(Name("/localhost/CA/_LIST"));
+  interest.setMustBeFresh(true);
+  DataCallback dataCb = bind(&ClientModule::handleLocalhostListResponse,
+                             this, _1, _2, listCallback, errorCallback);
+  m_face.expressInterest(interest, dataCb,
+                         bind(&ClientModule::onNack, this, _1, _2, errorCallback),
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
+}
+
+void
+ClientModule::handleLocalhostListResponse(const Interest& request, const Data& reply,
+                                          const LocalhostListCallback& listCallback,
+                                          const ErrorCallback& errorCallback)
+{
+  // TODO: use the file path to replace the cert
+  // const auto& pib = m_keyChain.getPib();
+  // auto identity = pib.getDefaultIdentity();
+  // auto key = identity.getDefaultKey();
+  // auto cert = key.getDefaultCertificate();
+
+  auto cert = *(io::load<security::v2::Certificate>(m_config.m_localNdncertAnchor));
+
+  if (!security::verifySignature(reply, cert)) {
+    errorCallback("Cannot verify data from localhost CA");
+    return;
+  };
+
+  JsonSection contentJson = getJsonFromData(reply);
+  ClientConfig clientConf;
+  clientConf.load(contentJson);
+  listCallback(clientConf);
+}
+
+void
+ClientModule::requestList(const ClientCaItem& ca, const std::string& additionalInfo,
+                          const ListCallback& listCallback, const ErrorCallback& errorCallback)
+{
+  Name requestName(ca.m_caName);
+  requestName.append("_LIST");
+  if (additionalInfo != "") {
+    requestName.append(additionalInfo);
+  }
+  Interest interest(requestName);
+  interest.setMustBeFresh(true);
+  DataCallback dataCb = bind(&ClientModule::handleListResponse,
+                             this, _1, _2, ca, listCallback, errorCallback);
+  m_face.expressInterest(interest, dataCb,
+                         bind(&ClientModule::onNack, this, _1, _2, errorCallback),
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
+}
+
+void
+ClientModule::handleListResponse(const Interest& request, const Data& reply,
+                                 const ClientCaItem& ca,
+                                 const ListCallback& listCallback,
+                                 const ErrorCallback& errorCallback)
+{
+  if (!security::verifySignature(reply, ca.m_anchor)) {
+    errorCallback("Cannot verify data from " + ca.m_caName.toUri());
+    return;
+  };
+
+  std::list<Name> caList;
+  Name assignedName;
+
+  JsonSection contentJson = getJsonFromData(reply);
+  auto recommendedName = contentJson.get("recommended-identity", "");
+  if (recommendedName == "") {
+    // without recommendation
+    auto caListJson = contentJson.get_child("ca-list");
+    auto it = caListJson.begin();
+    for(; it != caListJson.end(); it++) {
+      caList.push_back(Name(it->second.get<std::string>("ca-prefix")));
+    }
+  }
+  else {
+    // with recommendation
+    Name caName(contentJson.get<std::string>("recommended-ca"));
+    caList.push_back(caName);
+    assignedName = caName.append(recommendedName);
+  }
+  Name schemaDataName(contentJson.get("trust-schema", ""));
+  listCallback(caList, assignedName, schemaDataName);
+}
+
+void
 ClientModule::sendProbe(const ClientCaItem& ca, const std::string& probeInfo,
                         const RequestCallback& requestCallback,
                         const ErrorCallback& errorCallback)
 {
   Interest interest(Name(ca.m_caName).append("_PROBE").append(probeInfo));
   interest.setMustBeFresh(true);
-  DataCallback dataCb = bind(&ClientModule::handleProbeResponse, this, _1, _2, ca, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleProbeResponse,
+                             this, _1, _2, ca, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
   _LOG_TRACE("PROBE interest sent with Probe info " << probeInfo);
 }
@@ -104,11 +212,12 @@
   Interest interest(Name(ca.m_caName).append(Name("_NEW")).append(certRequest.wireEncode()));
   m_keyChain.sign(interest, signingByKey(state->m_key.getName()));
 
-  DataCallback dataCb = bind(&ClientModule::handleNewResponse, this, _1, _2, state, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleNewResponse,
+                             this, _1, _2, state, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
   _LOG_TRACE("NEW interest sent with identity " << identityName);
 }
@@ -166,11 +275,12 @@
   Interest interest(interestName);
   m_keyChain.sign(interest, signingByKey(state->m_key.getName()));
 
-  DataCallback dataCb = bind(&ClientModule::handleSelectResponse, this, _1, _2, state, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleSelectResponse,
+                             this, _1, _2, state, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
    _LOG_TRACE("SELECT interest sent with challenge type " << challengeType);
 }
@@ -189,7 +299,8 @@
 
   JsonSection json = getJsonFromData(reply);
 
-  _LOG_TRACE("SELECT response would change the status from " << state->m_status << " to " + json.get<std::string>(JSON_STATUS));
+  _LOG_TRACE("SELECT response would change the status from "
+             << state->m_status << " to " + json.get<std::string>(JSON_STATUS));
 
   state->m_status = json.get<std::string>(JSON_STATUS);
 
@@ -219,11 +330,12 @@
   Interest interest(interestName);
   m_keyChain.sign(interest, signingByKey(state->m_key.getName()));
 
-  DataCallback dataCb = bind(&ClientModule::handleValidateResponse, this, _1, _2, state, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleValidateResponse,
+                             this, _1, _2, state, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
   _LOG_TRACE("VALIDATE interest sent");
 }
@@ -267,11 +379,12 @@
 
   m_keyChain.sign(interest, signingByKey(state->m_key.getName()));
 
-  DataCallback dataCb = bind(&ClientModule::handleStatusResponse, this, _1, _2, state, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleStatusResponse,
+                             this, _1, _2, state, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
   _LOG_TRACE("STATUS interest sent");
 }
@@ -312,11 +425,12 @@
   Interest interest(interestName);
   interest.setMustBeFresh(true);
 
-  DataCallback dataCb = bind(&ClientModule::handleDownloadResponse, this, _1, _2, state, requestCallback, errorCallback);
+  DataCallback dataCb = bind(&ClientModule::handleDownloadResponse,
+                             this, _1, _2, state, requestCallback, errorCallback);
   m_face.expressInterest(interest, dataCb,
                          bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes, dataCb,
-                              requestCallback, errorCallback));
+                         bind(&ClientModule::onTimeout, this, _1, m_retryTimes,
+                              dataCb, errorCallback));
 
   _LOG_TRACE("DOWNLOAD interest sent");
 }
@@ -349,13 +463,13 @@
 
 void
 ClientModule::onTimeout(const Interest& interest, int nRetriesLeft, const DataCallback& dataCallback,
-                        const RequestCallback& requestCallback, const ErrorCallback& errorCallback)
+                        const ErrorCallback& errorCallback)
 {
   if (nRetriesLeft > 0) {
     m_face.expressInterest(interest, dataCallback,
                            bind(&ClientModule::onNack, this, _1, _2, errorCallback),
-                           bind(&ClientModule::onTimeout, this, _1, nRetriesLeft - 1, dataCallback,
-                                requestCallback, errorCallback));
+                           bind(&ClientModule::onTimeout, this, _1, nRetriesLeft - 1,
+                                dataCallback, errorCallback));
   }
   else {
     errorCallback("Run out retries: still timeout");
diff --git a/src/client-module.hpp b/src/client-module.hpp
index 5293b9a..e285ce2 100644
--- a/src/client-module.hpp
+++ b/src/client-module.hpp
@@ -29,6 +29,7 @@
 
 class RequestState
 {
+
 public:
   ClientCaItem m_ca;
   security::Key m_key;
@@ -57,6 +58,8 @@
     using std::runtime_error::runtime_error;
   };
 
+  using LocalhostListCallback = function<void (const ClientConfig&)>;
+  using ListCallback = function<void (const std::list<Name>&, const Name&, const Name&)>;
   using RequestCallback = function<void (const shared_ptr<RequestState>&)>;
   using ErrorCallback = function<void (const std::string&)>;
 
@@ -70,6 +73,37 @@
     return m_config;
   }
 
+  /**
+   * @brief Send /CA-prefix/CA/_DOWNLOAD/ANCHOR to get CA's latest anchor with the config
+   */
+  void
+  requestCaTrustAnchor(const Name& caName, const DataCallback& trustAnchorCallback,
+                       const ErrorCallback& errorCallback);
+
+  /**
+   * @brief Send /localhost/CA/List to query local available CAs
+   *
+   * For more information:
+   *   https://github.com/named-data/ndncert/wiki/Intra-Node-Design
+   */
+  void
+  requestLocalhostList(const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback);
+
+  /**
+   * @brief Handle the list request response
+   */
+  void
+  handleLocalhostListResponse(const Interest& request, const Data& reply,
+                              const LocalhostListCallback& listCallback, const ErrorCallback& errorCallback);
+
+  void
+  requestList(const ClientCaItem& ca, const std::string& additionalInfo,
+              const ListCallback& listCallback, const ErrorCallback& errorCallback);
+
+  void
+  handleListResponse(const Interest& request, const Data& reply, const ClientCaItem& ca,
+                     const ListCallback& listCallback, const ErrorCallback& errorCallback);
+
   void
   sendProbe(const ClientCaItem& ca, const std::string& probeInfo,
             const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
@@ -136,8 +170,8 @@
 
 protected:
   virtual void
-  onTimeout(const Interest& interest, int nRetriesLeft, const DataCallback& dataCallback,
-            const RequestCallback& requestCallback, const ErrorCallback& errorCallback);
+  onTimeout(const Interest& interest, int nRetriesLeft,
+            const DataCallback& dataCallback, const ErrorCallback& errorCallback);
 
   virtual void
   onNack(const Interest& interest, const lp::Nack& nack, const ErrorCallback& errorCallback);