Reduce namespace nesting (ndn::ndncert -> ndncert)

Change-Id: I5b69a2c3673cccdf07ea0ba3a0e7181894328f47
diff --git a/src/challenge/challenge-email.cpp b/src/challenge/challenge-email.cpp
index 41d53d6..bc624bc 100644
--- a/src/challenge/challenge-email.cpp
+++ b/src/challenge/challenge-email.cpp
@@ -22,7 +22,6 @@
 #include <regex>
 #include <boost/process.hpp>
 
-namespace ndn {
 namespace ndncert {
 
 NDN_LOG_INIT(ndncert.challenge.email);
@@ -37,8 +36,8 @@
 ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
                                const size_t& maxAttemptTimes,
                                const time::seconds secretLifetime)
-    : ChallengeModule("email", maxAttemptTimes, secretLifetime)
-    , m_sendEmailScript(scriptPath)
+  : ChallengeModule("email", maxAttemptTimes, secretLifetime)
+  , m_sendEmailScript(scriptPath)
 {
 }
 
@@ -52,7 +51,8 @@
     // for the first time, init the challenge
     std::string emailAddress = readString(params.get(tlv::ParameterValue));
     if (!isValidEmailAddress(emailAddress)) {
-      return returnWithNewChallengeStatus(request, INVALID_EMAIL, JsonSection(), m_maxAttemptTimes - 1, m_secretLifetime);
+      return returnWithNewChallengeStatus(request, INVALID_EMAIL, JsonSection(), m_maxAttemptTimes - 1,
+                                          m_secretLifetime);
     }
     auto lastComponentRequested = readString(request.cert.getIdentity().get(-1));
     if (lastComponentRequested != emailAddress) {
@@ -64,8 +64,10 @@
     secretJson.add(PARAMETER_KEY_CODE, emailCode);
     // send out the email
     sendEmail(emailAddress, emailCode, request);
-    NDN_LOG_TRACE("Secret for request " << toHex(request.requestId.data(), request.requestId.size()) << " : " << emailCode);
-    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
+    NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId.data(), request.requestId.size())
+                  << " : " << emailCode);
+    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson),
+                                        m_maxAttemptTimes, m_secretLifetime);
   }
   if (request.challengeState) {
     if (request.challengeState->challengeStatus == NEED_CODE ||
@@ -134,17 +136,17 @@
     if (params.size() != 1 || params.find(PARAMETER_KEY_EMAIL) == params.end()) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
     }
-    request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
-    request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_EMAIL));
-    request.push_back(makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_EMAIL)->second));
+    request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_EMAIL));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_EMAIL)->second));
   }
   else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
     if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
     }
-    request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
-    request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
-    request.push_back(makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
+    request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
   }
   else {
     NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
@@ -179,4 +181,3 @@
 }
 
 } // namespace ndncert
-} // namespace ndn
diff --git a/src/challenge/challenge-email.hpp b/src/challenge/challenge-email.hpp
index e2c7267..c5d76de 100644
--- a/src/challenge/challenge-email.hpp
+++ b/src/challenge/challenge-email.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,7 +23,6 @@
 
 #include "challenge-module.hpp"
 
-namespace ndn {
 namespace ndncert {
 
 /**
@@ -88,6 +87,5 @@
 };
 
 } // namespace ndncert
-} // namespace ndn
 
 #endif // NDNCERT_CHALLENGE_EMAIL_HPP
diff --git a/src/challenge/challenge-module.cpp b/src/challenge/challenge-module.cpp
index f184ca9..bf3b966 100644
--- a/src/challenge/challenge-module.cpp
+++ b/src/challenge/challenge-module.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -21,7 +21,6 @@
 #include "challenge/challenge-module.hpp"
 #include <ndn-cxx/util/random.hpp>
 
-namespace ndn {
 namespace ndncert {
 
 ChallengeModule::ChallengeModule(const std::string& challengeType,
@@ -41,7 +40,7 @@
   return i == factory.end() ? false : true;
 }
 
-unique_ptr<ChallengeModule>
+std::unique_ptr<ChallengeModule>
 ChallengeModule::createChallengeModule(const std::string& challengeType)
 {
   ChallengeFactory& factory = getFactory();
@@ -61,11 +60,11 @@
 {
   uint32_t securityCode = 0;
   do {
-    securityCode = random::generateSecureWord32();
+    securityCode = ndn::random::generateSecureWord32();
   }
   while (securityCode >= 4294000000);
   securityCode /= 4294;
-  std::string result = std::to_string(securityCode);
+  std::string result = ndn::to_string(securityCode);
   while (result.length() < 6) {
     result = "0" + result;
   }
@@ -103,4 +102,3 @@
 }
 
 } // namespace ndncert
-} // namespace ndn
diff --git a/src/challenge/challenge-module.hpp b/src/challenge/challenge-module.hpp
index 9d7d859..1e33bfc 100644
--- a/src/challenge/challenge-module.hpp
+++ b/src/challenge/challenge-module.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,16 +23,16 @@
 
 #include "detail/ca-request-state.hpp"
 
-namespace ndn {
 namespace ndncert {
 
-class ChallengeModule : noncopyable
+class ChallengeModule : boost::noncopyable
 {
 public:
   explicit
   ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
 
-  virtual ~ChallengeModule() = default;
+  virtual
+  ~ChallengeModule() = default;
 
   template <class ChallengeType>
   static void
@@ -46,7 +46,7 @@
   static bool
   isChallengeSupported(const std::string& challengeType);
 
-  static unique_ptr<ChallengeModule>
+  static std::unique_ptr<ChallengeModule>
   createChallengeModule(const std::string& challengeType);
 
   // For CA
@@ -83,7 +83,7 @@
   const time::seconds m_secretLifetime;
 
 private:
-  typedef function<unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
+  typedef std::function<std::unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
   typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
 
   static ChallengeFactory&
@@ -91,15 +91,15 @@
 };
 
 #define NDNCERT_REGISTER_CHALLENGE(C, T)                              \
-  static class NdnCert##C##ChallengeRegistrationClass {               \
+  static class NdnCert##C##ChallengeRegistrationClass                 \
+  {                                                                   \
   public:                                                             \
     NdnCert##C##ChallengeRegistrationClass()                          \
     {                                                                 \
-      ::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
+      ::ndncert::ChallengeModule::registerChallengeModule<C>(T);      \
     }                                                                 \
   } g_NdnCert##C##ChallengeRegistrationVariable
 
 } // namespace ndncert
-} // namespace ndn
 
 #endif // NDNCERT_CHALLENGE_MODULE_HPP
diff --git a/src/challenge/challenge-pin.cpp b/src/challenge/challenge-pin.cpp
index 5d3ea8a..6e42969 100644
--- a/src/challenge/challenge-pin.cpp
+++ b/src/challenge/challenge-pin.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -19,9 +19,9 @@
  */
 
 #include "challenge-pin.hpp"
+
 #include <ndn-cxx/util/random.hpp>
 
-namespace ndn {
 namespace ndncert {
 
 NDN_LOG_INIT(ndncert.challenge.pin);
@@ -32,7 +32,7 @@
 const std::string ChallengePin::PARAMETER_KEY_CODE = "code";
 
 ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime)
-    : ChallengeModule("pin", maxAttemptTimes, secretLifetime)
+  : ChallengeModule("pin", maxAttemptTimes, secretLifetime)
 {
 }
 
@@ -48,8 +48,10 @@
     std::string secretCode = generateSecretCode();
     JsonSection secretJson;
     secretJson.add(PARAMETER_KEY_CODE, secretCode);
-    NDN_LOG_TRACE("Secret for request " << toHex(request.requestId.data(), request.requestId.size()) << " : " << secretCode);
-    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
+    NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId.data(), request.requestId.size())
+                  << " : " << secretCode);
+    return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes,
+                                        m_secretLifetime);
   }
   if (request.challengeState) {
     if (request.challengeState->challengeStatus == NEED_CODE ||
@@ -109,15 +111,15 @@
 {
   Block request(tlv::EncryptedPayload);
   if (status == Status::BEFORE_CHALLENGE) {
-    request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
+    request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
   }
   else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
     if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
     }
-    request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
-    request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
-    request.push_back(makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
+    request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
+    request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
   }
   else {
     NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
@@ -125,5 +127,5 @@
   request.encode();
   return request;
 }
+
 } // namespace ndncert
-} // namespace ndn
diff --git a/src/challenge/challenge-pin.hpp b/src/challenge/challenge-pin.hpp
index 61e0fb6..cfcf508 100644
--- a/src/challenge/challenge-pin.hpp
+++ b/src/challenge/challenge-pin.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -23,7 +23,6 @@
 
 #include "challenge-module.hpp"
 
-namespace ndn {
 namespace ndncert {
 
 /**
@@ -70,6 +69,5 @@
 };
 
 } // namespace ndncert
-} // namespace ndn
 
 #endif // NDNCERT_CHALLENGE_PIN_HPP
diff --git a/src/challenge/challenge-possession.cpp b/src/challenge/challenge-possession.cpp
index ef93d25..a7f0f77 100644
--- a/src/challenge/challenge-possession.cpp
+++ b/src/challenge/challenge-possession.cpp
@@ -27,7 +27,6 @@
 
 #include <boost/property_tree/json_parser.hpp>
 
-namespace ndn {
 namespace ndncert {
 
 NDN_LOG_INIT(ndncert.challenge.possession);
@@ -58,7 +57,7 @@
   }
   catch (const boost::property_tree::file_parser_error& error) {
     NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " +
-                                 error.message() + " on line " + std::to_string(error.line())));
+                                 error.message() + " on line " + ndn::to_string(error.line())));
   }
 
   if (config.begin() == config.end()) {
@@ -70,7 +69,7 @@
   auto it = anchorList.begin();
   for (; it != anchorList.end(); it++) {
     std::istringstream ss(it->second.get("certificate", ""));
-    auto cert = io::load<security::Certificate>(ss);
+    auto cert = ndn::io::load<Certificate>(ss);
     if (cert == nullptr) {
       NDN_LOG_ERROR("Cannot load the certificate from config file");
       continue;
@@ -87,7 +86,7 @@
   if (m_trustAnchors.empty()) {
     parseConfigFile();
   }
-  security::Certificate credential;
+  Certificate credential;
   const uint8_t* signature = nullptr;
   size_t signatureLen = 0;
   const auto& elements = params.elements();
@@ -113,16 +112,18 @@
   // verify the credential and the self-signed cert
   if (request.status == Status::BEFORE_CHALLENGE) {
     NDN_LOG_TRACE("Challenge Interest arrives. Check certificate and init the challenge");
+    using ndn::toHex;
+
     // check the certificate
     bool checkOK = false;
     if (credential.hasContent() && signatureLen == 0) {
       Name signingKeyName = credential.getSignatureInfo().getKeyLocator().getName();
-      security::transform::PublicKey key;
+      ndn::security::transform::PublicKey key;
       const auto& pubKeyBuffer = credential.getPublicKey();
       key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size());
       for (auto anchor : m_trustAnchors) {
         if (anchor.getKeyName() == signingKeyName) {
-          if (security::verifySignature(credential, anchor)) {
+          if (ndn::security::verifySignature(credential, anchor)) {
             checkOK = true;
           }
         }
@@ -137,7 +138,7 @@
 
     // for the first time, init the challenge
     std::array<uint8_t, 16> secretCode{};
-    random::generateSecureBytes(secretCode.data(), 16);
+    ndn::random::generateSecureBytes(secretCode.data(), 16);
     JsonSection secretJson;
     secretJson.add(PARAMETER_KEY_NONCE, toHex(secretCode.data(), 16));
     auto credential_block = credential.wireEncode();
@@ -152,14 +153,14 @@
     if (credential.hasContent() || signatureLen == 0) {
       return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate");
     }
-    credential = security::Certificate(Block(fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, ""))));
-    auto secretCode = *fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, ""));
+    credential = Certificate(Block(ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, ""))));
+    auto secretCode = *ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, ""));
 
     //check the proof
-    security::transform::PublicKey key;
+    ndn::security::transform::PublicKey key;
     const auto& pubKeyBuffer = credential.getPublicKey();
     key.loadPkcs8(pubKeyBuffer.data(), pubKeyBuffer.size());
-    if (security::verifySignature({{secretCode.data(), secretCode.size()}}, signature, signatureLen, key)) {
+    if (ndn::security::verifySignature({{secretCode.data(), secretCode.size()}}, signature, signatureLen, key)) {
       return returnWithSuccess(request);
     }
     return returnWithError(request, ErrorCode::INVALID_PARAMETER,
@@ -196,10 +197,10 @@
     if (params.size() != 1) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
     }
-    request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
+    request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
     for (const auto& item : params) {
       if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) {
-        request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT));
+        request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT));
         Block valueBlock(tlv::ParameterValue);
         auto& certTlvStr = std::get<1>(item);
         valueBlock.push_back(Block(reinterpret_cast<const uint8_t*>(certTlvStr.data()), certTlvStr.size()));
@@ -210,17 +211,17 @@
       }
     }
   }
-  else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF){
+  else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) {
     if (params.size() != 1) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
     }
     for (const auto& item : params) {
       if (std::get<0>(item) == PARAMETER_KEY_PROOF) {
-        request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF));
+        request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_PROOF));
         auto& sigTlvStr = std::get<1>(item);
-        Block valueBlock = makeBinaryBlock(tlv::ParameterValue,
-                                           reinterpret_cast<const uint8_t*>(sigTlvStr.data()),
-                                           sigTlvStr.size());
+        auto valueBlock = ndn::makeBinaryBlock(tlv::ParameterValue,
+                                               reinterpret_cast<const uint8_t*>(sigTlvStr.data()),
+                                               sigTlvStr.size());
         request.push_back(valueBlock);
       }
       else {
@@ -237,14 +238,14 @@
 
 void
 ChallengePossession::fulfillParameters(std::multimap<std::string, std::string>& params,
-                                       KeyChain& keyChain, const Name& issuedCertName,
+                                       ndn::KeyChain& keyChain, const Name& issuedCertName,
                                        const std::array<uint8_t, 16>& nonce)
 {
-  auto keyName = security::extractKeyNameFromCertName(issuedCertName);
-  auto id = keyChain.getPib().getIdentity(security::extractIdentityFromCertName(issuedCertName));
+  auto keyName = ndn::security::extractKeyNameFromCertName(issuedCertName);
+  auto id = keyChain.getPib().getIdentity(ndn::security::extractIdentityFromCertName(issuedCertName));
   auto issuedCert = id.getKey(keyName).getCertificate(issuedCertName);
   auto issuedCertTlv = issuedCert.wireEncode();
-  auto signature = keyChain.getTpm().sign({{nonce.data(), nonce.size()}}, keyName, DigestAlgorithm::SHA256);
+  auto signature = keyChain.getTpm().sign({{nonce.data(), nonce.size()}}, keyName, ndn::DigestAlgorithm::SHA256);
 
   for (auto& item : params) {
     if (item.first == PARAMETER_KEY_CREDENTIAL_CERT) {
@@ -257,4 +258,3 @@
 }
 
 } // namespace ndncert
-} // namespace ndn
diff --git a/src/challenge/challenge-possession.hpp b/src/challenge/challenge-possession.hpp
index 165e20e..ab5e641 100644
--- a/src/challenge/challenge-possession.hpp
+++ b/src/challenge/challenge-possession.hpp
@@ -1,5 +1,5 @@
-/**
- * Copyright (c) 2017-2020, Regents of the University of California.
+/*
+ * Copyright (c) 2017-2021, Regents of the University of California.
  *
  * This file is part of ndncert, a certificate management system based on NDN.
  *
@@ -22,7 +22,8 @@
 
 #include "challenge-module.hpp"
 
-namespace ndn {
+#include <ndn-cxx/security/key-chain.hpp>
+
 namespace ndncert {
 
 /**
@@ -66,7 +67,8 @@
 
   static void
   fulfillParameters(std::multimap<std::string, std::string>& params,
-                    KeyChain& keyChain, const Name& issuedCertName, const std::array<uint8_t, 16>& nonce);
+                    ndn::KeyChain& keyChain, const Name& issuedCertName,
+                    const std::array<uint8_t, 16>& nonce);
 
   // challenge parameters
   static const std::string PARAMETER_KEY_CREDENTIAL_CERT;
@@ -79,11 +81,10 @@
   parseConfigFile();
 
 NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  std::list<security::Certificate> m_trustAnchors;
+  std::list<Certificate> m_trustAnchors;
   std::string m_configFile;
 };
 
 } // namespace ndncert
-} // namespace ndn
 
 #endif // NDNCERT_CHALLENGE_POSSESSION_HPP