apply Error Data packet in challenge status

Change-Id: I629e57ad96d33ad77dcc939a25577a15afcf2886
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 82f0eec..2a9b5d7 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -19,19 +19,22 @@
  */
 
 #include "ca-module.hpp"
-#include "challenge-module.hpp"
-#include "logging.hpp"
-#include "crypto-support/enc-tlv.hpp"
-#include "protocol-detail/info.hpp"
-#include "protocol-detail/probe.hpp"
-#include "protocol-detail/new.hpp"
-#include "protocol-detail/challenge.hpp"
-#include "protocol-detail/revoke.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>
+
 #include <ndn-cxx/metadata-object.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+#include <ndn-cxx/security/verification-helpers.hpp>
+#include <ndn-cxx/util/io.hpp>
+#include <ndn-cxx/util/random.hpp>
+
+#include "challenge-module.hpp"
+#include "crypto-support/enc-tlv.hpp"
+#include "logging.hpp"
+#include "protocol-detail/challenge.hpp"
+#include "protocol-detail/error.hpp"
+#include "protocol-detail/info.hpp"
+#include "protocol-detail/new.hpp"
+#include "protocol-detail/probe.hpp"
+#include "protocol-detail/revoke.hpp"
 
 namespace ndn {
 namespace ndncert {
@@ -44,8 +47,8 @@
 
 CaModule::CaModule(Face& face, security::v2::KeyChain& keyChain,
                    const std::string& configPath, const std::string& storageType)
-  : m_face(face)
-  , m_keyChain(keyChain)
+    : m_face(face)
+    , m_keyChain(keyChain)
 {
   // load the config and create storage
   m_config.load(configPath);
@@ -79,35 +82,36 @@
   Name prefix = m_config.m_caPrefix;
   prefix.append("CA");
 
-  prefixId = m_face.registerPrefix(prefix,
-    [&] (const Name& name) {
-      // register INFO prefix
-      auto filterId = m_face.setInterestFilter(Name(name).append("INFO"),
-                                          bind(&CaModule::onInfo, this, _2));
-      m_interestFilterHandles.push_back(filterId);
+  prefixId = m_face.registerPrefix(
+      prefix,
+      [&](const Name& name) {
+        // register INFO prefix
+        auto filterId = m_face.setInterestFilter(Name(name).append("INFO"),
+                                                 bind(&CaModule::onInfo, this, _2));
+        m_interestFilterHandles.push_back(filterId);
 
-      // register PROBE prefix
-      filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
-                                          bind(&CaModule::onProbe, this, _2));
-      m_interestFilterHandles.push_back(filterId);
+        // register PROBE prefix
+        filterId = m_face.setInterestFilter(Name(name).append("PROBE"),
+                                            bind(&CaModule::onProbe, this, _2));
+        m_interestFilterHandles.push_back(filterId);
 
-      // register NEW prefix
-      filterId = m_face.setInterestFilter(Name(name).append("NEW"),
-                                          bind(&CaModule::onNew, this, _2));
-      m_interestFilterHandles.push_back(filterId);
+        // register NEW prefix
+        filterId = m_face.setInterestFilter(Name(name).append("NEW"),
+                                            bind(&CaModule::onNew, this, _2));
+        m_interestFilterHandles.push_back(filterId);
 
-      // register SELECT prefix
-      filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
-                                          bind(&CaModule::onChallenge, this, _2));
-      m_interestFilterHandles.push_back(filterId);
+        // register SELECT prefix
+        filterId = m_face.setInterestFilter(Name(name).append("CHALLENGE"),
+                                            bind(&CaModule::onChallenge, this, _2));
+        m_interestFilterHandles.push_back(filterId);
 
-      // register REVOKE prefix
-      filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
-                                          bind(&CaModule::onRevoke, this, _2));
-      m_interestFilterHandles.push_back(filterId);
-      _LOG_TRACE("Prefix " << name << " got registered");
-    },
-    bind(&CaModule::onRegisterFailed, this, _2));
+        // register REVOKE prefix
+        filterId = m_face.setInterestFilter(Name(name).append("REVOKE"),
+                                            bind(&CaModule::onRevoke, this, _2));
+        m_interestFilterHandles.push_back(filterId);
+        _LOG_TRACE("Prefix " << name << " got registered");
+      },
+      bind(&CaModule::onRegisterFailed, this, _2));
   m_registeredPrefixHandles.push_back(prefixId);
 }
 
@@ -225,15 +229,15 @@
 void
 CaModule::onNew(const Interest& request)
 {
-    // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
-    onRequestInit(request, REQUEST_TYPE_NEW);
+  // NEW Naming Convention: /<CA-prefix>/CA/NEW/[SignedInterestParameters_Digest]
+  onRequestInit(request, REQUEST_TYPE_NEW);
 }
 
 void
 CaModule::onRevoke(const Interest& request)
 {
-    // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
-    onRequestInit(request, REQUEST_TYPE_REVOKE);
+  // REVOKE Naming Convention: /<CA-prefix>/CA/REVOKE/[SignedInterestParameters_Digest]
+  onRequestInit(request, REQUEST_TYPE_REVOKE);
 }
 
 void
@@ -281,7 +285,7 @@
       security::v2::Certificate cert = security::v2::Certificate(cert_req.get(tlv::Data));
       clientCert = make_shared<security::v2::Certificate>(cert);
     }
-    catch (const std::exception &e) {
+    catch (const std::exception& e) {
       _LOG_ERROR("Unrecognized certificate request: " << e.what());
       return;
     }
@@ -293,17 +297,23 @@
       return;
     }
     if (expectedPeriod.second > currentTime + m_config.m_maxValidityPeriod ||
-      expectedPeriod.second <= expectedPeriod.first) {
+        expectedPeriod.second <= expectedPeriod.first) {
       _LOG_ERROR("Client requests an invalid validity period or a notAfter timepoint beyond the allowed time period.");
       return;
     }
 
     // verify the self-signed certificate, the request, and the token
+<<<<<<< HEAD
     if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix
         || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name
         || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET
         || clientCert->getName().size() >
                 m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET - 1 + m_config.m_maxSuffixLength) {
+=======
+    if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName())             // under ca prefix
+        || !security::v2::Certificate::isValidName(clientCert->getName())  // is valid cert name
+        || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET) {
+>>>>>>> apply Error Data packet in challenge status
       _LOG_ERROR("Invalid self-signed certificate name " << clientCert->getName());
       return;
     }
@@ -315,7 +325,8 @@
       _LOG_ERROR("Interest with bad signature.");
       return;
     }
-  } else if (requestType == REQUEST_TYPE_REVOKE) {
+  }
+  else if (requestType == REQUEST_TYPE_REVOKE) {
     // parse certificate request
     Block cert_revoke = parameterTLV.get(tlv_cert_to_revoke);
     cert_revoke.parse();
@@ -324,17 +335,23 @@
       security::v2::Certificate cert = security::v2::Certificate(cert_revoke.get(tlv::Data));
       clientCert = make_shared<security::v2::Certificate>(cert);
     }
-    catch (const std::exception &e) {
+    catch (const std::exception& e) {
       _LOG_ERROR("Unrecognized certificate: " << e.what());
       return;
     }
 
     // verify the certificate
+<<<<<<< HEAD
     if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName()) // under ca prefix
         || !security::v2::Certificate::isValidName(clientCert->getName()) // is valid cert name
         || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET
         || clientCert->getName().size() >
                 m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET - 1 + m_config.m_maxSuffixLength) {
+=======
+    if (!m_config.m_caPrefix.isPrefixOf(clientCert->getName())             // under ca prefix
+        || !security::v2::Certificate::isValidName(clientCert->getName())  // is valid cert name
+        || clientCert->getName().size() < m_config.m_caPrefix.size() + IS_SUBNAME_MIN_OFFSET) {
+>>>>>>> apply Error Data packet in challenge status
       _LOG_ERROR("Invalid certificate name " << clientCert->getName());
       return;
     }
@@ -362,10 +379,11 @@
   result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
   if (requestType == REQUEST_TYPE_NEW) {
     result.setContent(NEW::encodeDataContent(myEcdhPubKeyBase64,
-                                               std::to_string(saltInt),
-                                               certRequest,
-                                               m_config.m_supportedChallenges));
-  } else if (requestType == REQUEST_TYPE_REVOKE) {
+                                             std::to_string(saltInt),
+                                             certRequest,
+                                             m_config.m_supportedChallenges));
+  }
+  else if (requestType == REQUEST_TYPE_REVOKE) {
     result.setContent(REVOKE::encodeDataContent(myEcdhPubKeyBase64,
                                                 std::to_string(saltInt),
                                                 certRequest,
@@ -387,104 +405,102 @@
   if (certRequest.m_requestId == "") {
     // cannot get the request state
     _LOG_ERROR("Cannot find certificate request state from CA's storage.");
+    m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
+                                       "Cannot find certificate request state from CA's storage."));
     return;
   }
   // verify signature
   if (!security::verifySignature(request, certRequest.m_cert)) {
     _LOG_ERROR("Challenge Interest with bad signature.");
+    m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::BAD_SIGNATURE, "Bad signature."));
     return;
   }
   // decrypt the parameters
   Buffer paramTLVPayload;
   try {
     paramTLVPayload = decodeBlockWithAesGcm128(request.getApplicationParameters(), m_aesKey,
-                                                (uint8_t*)"test", strlen("test"));
+                                               (uint8_t*)"test", strlen("test"));
   }
   catch (const std::exception& e) {
     _LOG_ERROR("Cannot successfully decrypt the Interest parameters: " << e.what());
+    m_storage->deleteRequest(certRequest.m_requestId);
+    m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
+                                       "Cannot successfully decrypt the Interest parameters."));
     return;
   }
   if (paramTLVPayload.size() == 0) {
     _LOG_ERROR("Got an empty buffer from content decryption.");
+    m_storage->deleteRequest(certRequest.m_requestId);
+    m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
+                                       "Empty buffer from content decryption."));
     return;
   }
-
   Block paramTLV = makeBinaryBlock(tlv_encrypted_payload, paramTLVPayload.data(), paramTLVPayload.size());
   paramTLV.parse();
 
   // load the corresponding challenge module
   std::string challengeType = readString(paramTLV.get(tlv_selected_challenge));
   auto challenge = ChallengeModule::createChallengeModule(challengeType);
-
-  Block payload;
-
   if (challenge == nullptr) {
     _LOG_TRACE("Unrecognized challenge type " << challengeType);
-    certRequest.m_status = Status::FAILURE;
-    certRequest.m_challengeStatus = CHALLENGE_STATUS_UNKNOWN_CHALLENGE;
-    payload = CHALLENGE::encodeDataPayload(certRequest);
+    m_storage->deleteRequest(certRequest.m_requestId);
+    m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER, "Unrecognized challenge type"));
+    return;
   }
-  else {
-    _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
-    // let challenge module handle the request
-    challenge->handleChallengeRequest(paramTLV, certRequest);
-    if (certRequest.m_status == Status::FAILURE) {
-      // if challenge failed
-      m_storage->deleteRequest(certRequest.m_requestId);
-      payload = CHALLENGE::encodeDataPayload(certRequest);
-      _LOG_TRACE("Challenge failed");
-    }
-    else if (certRequest.m_status == Status::PENDING) {
-      // if challenge succeeded
-      if (certRequest.m_requestType == REQUEST_TYPE_NEW) {
-        auto issuedCert = issueCertificate(certRequest);
-        certRequest.m_cert = issuedCert;
-        certRequest.m_status = Status::SUCCESS;
-        try {
-          m_storage->addCertificate(certRequest.m_requestId, issuedCert);
-          m_storage->deleteRequest(certRequest.m_requestId);
-          _LOG_TRACE("New Certificate Issued " << issuedCert.getName());
-        }
-        catch (const std::exception& e) {
-          _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
-          return;
-        }
 
-        payload = CHALLENGE::encodeDataPayload(certRequest);
-        payload.parse();
-        payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
-        payload.encode();
+  _LOG_TRACE("CHALLENGE module to be load: " << challengeType);
+  auto errorInfo = challenge->handleChallengeRequest(paramTLV, certRequest);
+  if (std::get<0>(errorInfo) != ErrorCode::NO_ERROR) {
+    m_storage->deleteRequest(certRequest.m_requestId);
+    m_face.put(generateErrorDataPacket(request.getName(), std::get<0>(errorInfo), std::get<1>(errorInfo)));
+    return;
+  }
 
-        //contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1)));
-        _LOG_TRACE("Challenge succeeded. Certificate has been issued");
-      }
-      else if (certRequest.m_requestType == REQUEST_TYPE_REVOKE) {
-        certRequest.m_status = Status::SUCCESS;
-        try {
-          m_storage->deleteRequest(certRequest.m_requestId);
-          _LOG_TRACE("Certificate Revoked");
-        }
-        catch (const std::exception& e) {
-          _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
-          return;
-        }
-
-        payload = CHALLENGE::encodeDataPayload(certRequest);
-        payload.parse();
-        _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
-      }
-    }
-    else {
+  Block payload;
+  if (certRequest.m_status == Status::PENDING) {
+    // if challenge succeeded
+    if (certRequest.m_requestType == REQUEST_TYPE_NEW) {
+      auto issuedCert = issueCertificate(certRequest);
+      certRequest.m_cert = issuedCert;
+      certRequest.m_status = Status::SUCCESS;
       try {
-        m_storage->updateRequest(certRequest);
+        m_storage->addCertificate(certRequest.m_requestId, issuedCert);
+        m_storage->deleteRequest(certRequest.m_requestId);
+        _LOG_TRACE("New Certificate Issued " << issuedCert.getName());
       }
       catch (const std::exception& e) {
-        _LOG_TRACE("Cannot update request instance: " << e.what());
+        _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
         return;
       }
+
       payload = CHALLENGE::encodeDataPayload(certRequest);
-      _LOG_TRACE("No failure no success. Challenge moves on");
+      payload.parse();
+      payload.push_back(makeNestedBlock(tlv_issued_cert_name, issuedCert.getName()));
+      payload.encode();
+
+      //contentJson.add(JSON_CA_CERT_ID, readString(issuedCert.getName().at(-1)));
+      _LOG_TRACE("Challenge succeeded. Certificate has been issued");
     }
+    else if (certRequest.m_requestType == REQUEST_TYPE_REVOKE) {
+      certRequest.m_status = Status::SUCCESS;
+      try {
+        m_storage->deleteRequest(certRequest.m_requestId);
+        _LOG_TRACE("Certificate Revoked");
+      }
+      catch (const std::exception& e) {
+        _LOG_ERROR("Cannot add issued cert and remove the request: " << e.what());
+        return;
+      }
+
+      payload = CHALLENGE::encodeDataPayload(certRequest);
+      payload.parse();
+      _LOG_TRACE("Challenge succeeded. Certificate has been revoked");
+    }
+  }
+  else {
+    m_storage->updateRequest(certRequest);
+    payload = CHALLENGE::encodeDataPayload(certRequest);
+    _LOG_TRACE("No failure no success. Challenge moves on");
   }
 
   Data result;
@@ -507,7 +523,7 @@
 CaModule::issueCertificate(const CertificateRequest& certRequest)
 {
   auto expectedPeriod =
-    certRequest.m_cert.getValidityPeriod().getPeriod();
+      certRequest.m_cert.getValidityPeriod().getPeriod();
   security::ValidityPeriod period(expectedPeriod.first, expectedPeriod.second);
   security::v2::Certificate newCert;
 
@@ -578,5 +594,16 @@
   }
 }
 
-} // namespace ndncert
-} // namespace ndn
+Data
+CaModule::generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo)
+{
+  Data result;
+  result.setName(name);
+  result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
+  result.setContent(ErrorTLV::encodeDataContent(error, errorInfo));
+  m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix));
+  return result;
+}
+
+}  // namespace ndncert
+}  // namespace ndn
diff --git a/src/ca-module.hpp b/src/ca-module.hpp
index b370132..1cdd246 100644
--- a/src/ca-module.hpp
+++ b/src/ca-module.hpp
@@ -107,6 +107,9 @@
   void
   registerPrefix();
 
+  Data
+  generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo);
+
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   Face& m_face;
   CaConfig m_config;
diff --git a/src/challenge-module.cpp b/src/challenge-module.cpp
index 47dca5d..a54a3e7 100644
--- a/src/challenge-module.cpp
+++ b/src/challenge-module.cpp
@@ -70,8 +70,8 @@
   return result;
 }
 
-std::tuple<Error, std::string>
-ChallengeModule::returnWithError(CertificateRequest& request, Error errorCode, std::string&& errorInfo)
+std::tuple<ErrorCode, std::string>
+ChallengeModule::returnWithError(CertificateRequest& request, ErrorCode errorCode, std::string&& errorInfo)
 {
   request.m_status = Status::FAILURE;
   request.m_challengeType = "";
@@ -83,7 +83,7 @@
   return std::make_tuple(errorCode, std::move(errorInfo));
 }
 
-std::tuple<Error, std::string>
+std::tuple<ErrorCode, std::string>
 ChallengeModule::returnWithNewChallengeStatus(CertificateRequest& request, const std::string& challengeStatus,
                                               JsonSection&& challengeSecret, size_t remainingTries, size_t remainingTime)
 {
@@ -94,10 +94,10 @@
   request.m_challengeTp = time::toIsoString(time::system_clock::now());
   request.m_remainingTime = remainingTries;
   request.m_remainingTries = remainingTime;
-  return std::make_tuple(Error::NO_ERROR, "");
+  return std::make_tuple(ErrorCode::NO_ERROR, "");
 }
 
-std::tuple<Error, std::string>
+std::tuple<ErrorCode, std::string>
 ChallengeModule::returnWithSuccess(CertificateRequest& request)
 {
   request.m_status = Status::PENDING;
@@ -107,7 +107,7 @@
   request.m_challengeTp = "";
   request.m_remainingTime = 0;
   request.m_remainingTries = 0;
-  return std::make_tuple(Error::NO_ERROR, "");
+  return std::make_tuple(ErrorCode::NO_ERROR, "");
 }
 
 } // namespace ndncert
diff --git a/src/challenge-module.hpp b/src/challenge-module.hpp
index 660ff15..37fd611 100644
--- a/src/challenge-module.hpp
+++ b/src/challenge-module.hpp
@@ -51,7 +51,7 @@
   createChallengeModule(const std::string& challengeType);
 
   // For CA
-  virtual std::tuple<Error, std::string>
+  virtual std::tuple<ErrorCode, std::string>
   handleChallengeRequest(const Block& params, CertificateRequest& request) = 0;
 
   // For Client
@@ -68,14 +68,14 @@
 
 protected:
   // used by challenge modules
-  std::tuple<Error, std::string>
-  returnWithError(CertificateRequest& request, Error errorCode, std::string&& errorInfo);
+  std::tuple<ErrorCode, std::string>
+  returnWithError(CertificateRequest& request, ErrorCode errorCode, std::string&& errorInfo);
 
-  std::tuple<Error, std::string>
+  std::tuple<ErrorCode, std::string>
   returnWithNewChallengeStatus(CertificateRequest& request, const std::string& challengeStatus,
                                JsonSection&& challengeSecret, size_t remainingTries, size_t remainingTime);
 
-  std::tuple<Error, std::string>
+  std::tuple<ErrorCode, std::string>
   returnWithSuccess(CertificateRequest& request);
 
 public:
diff --git a/src/challenge-module/challenge-credential.cpp b/src/challenge-module/challenge-credential.cpp
index fe120eb..1486c30 100644
--- a/src/challenge-module/challenge-credential.cpp
+++ b/src/challenge-module/challenge-credential.cpp
@@ -76,7 +76,7 @@
 }
 
 // For CA
-std::tuple<Error, std::string>
+std::tuple<ErrorCode, std::string>
 ChallengeCredential::handleChallengeRequest(const Block& params, CertificateRequest& request)
 {
   params.parse();
@@ -92,7 +92,7 @@
         credential = io::load<security::v2::Certificate>(ss);
         if (credential == nullptr) {
           _LOG_ERROR("Cannot load challenge parameter: credential");
-          return returnWithError(request, Error::INVALID_PARAMETER, "Cannot challenge credential: credential.");
+          return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot challenge credential: credential.");
         }
       }
       else if (readString(elements[i]) == PARAMETER_KEY_PROOF_OF_PRIVATE_KEY) {
@@ -100,7 +100,7 @@
         selfSigned = io::load<security::v2::Certificate>(ss);
         if (selfSigned == nullptr) {
           _LOG_ERROR("Cannot load challenge parameter: proof of private key");
-          return returnWithError(request, Error::INVALID_PARAMETER, "Cannot load challenge parameter: proof of private key.");
+          return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot load challenge parameter: proof of private key.");
         }
       }
       else {
@@ -122,7 +122,7 @@
   }
 
   _LOG_TRACE("Cannot verify the proof of private key against credential");
-  return returnWithError(request, Error::INVALID_PARAMETER, "Cannot verify the proof of private key against credential.");
+  return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot verify the proof of private key against credential.");
 }
 
 // For Client
diff --git a/src/challenge-module/challenge-credential.hpp b/src/challenge-module/challenge-credential.hpp
index 2afe38e..93838a8 100644
--- a/src/challenge-module/challenge-credential.hpp
+++ b/src/challenge-module/challenge-credential.hpp
@@ -52,7 +52,7 @@
   ChallengeCredential(const std::string& configPath = "");
 
   // For CA
-  std::tuple<Error, std::string>
+  std::tuple<ErrorCode, std::string>
   handleChallengeRequest(const Block& params, CertificateRequest& request) override;
 
   // For Client
diff --git a/src/challenge-module/challenge-email.cpp b/src/challenge-module/challenge-email.cpp
index 7e0134a..cac0084 100644
--- a/src/challenge-module/challenge-email.cpp
+++ b/src/challenge-module/challenge-email.cpp
@@ -47,7 +47,7 @@
 }
 
 // For CA
-std::tuple<Error, std::string>
+std::tuple<ErrorCode, std::string>
 ChallengeEmail::handleChallengeRequest(const Block& params, CertificateRequest& request)
 {
   params.parse();
@@ -56,7 +56,7 @@
     // for the first time, init the challenge
     std::string emailAddress = readString(params.get(tlv_parameter_value));
     if (!isValidEmailAddress(emailAddress)) {
-      return returnWithError(request, Error::INVALID_PARAMETER, "Invalid email address format.");
+      return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Invalid email address format.");
     }
     auto lastComponentRequested = readString(request.m_cert.getIdentity().get(-1));
     if (lastComponentRequested != emailAddress) {
@@ -78,7 +78,7 @@
     auto secret = request.m_challengeSecrets;
     // check if run out of time
     if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
-      return returnWithError(request, Error::OUT_OF_TIME, "Secret expired.");
+      return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
     }
     // check if provided secret is correct
     if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
@@ -95,10 +95,10 @@
     else {
       // run out times
       _LOG_TRACE("Wrong secret code provided. Ran out tires. Challenge failed.");
-      return returnWithError(request, Error::OUT_OF_TRIES, "Ran out tires.");
+      return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
     }
   }
-  return returnWithError(request, Error::INVALID_PARAMETER, "Unexpected status or challenge status");
+  return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
 }
 
 // For Client
diff --git a/src/challenge-module/challenge-email.hpp b/src/challenge-module/challenge-email.hpp
index f148d06..42a329d 100644
--- a/src/challenge-module/challenge-email.hpp
+++ b/src/challenge-module/challenge-email.hpp
@@ -58,7 +58,7 @@
                  const time::seconds secretLifetime = time::minutes(20));
 
   // For CA
-  std::tuple<Error, std::string>
+  std::tuple<ErrorCode, std::string>
   handleChallengeRequest(const Block& params, CertificateRequest& request) override;
 
   // For Client
diff --git a/src/challenge-module/challenge-pin.cpp b/src/challenge-module/challenge-pin.cpp
index cd20c51..556929f 100644
--- a/src/challenge-module/challenge-pin.cpp
+++ b/src/challenge-module/challenge-pin.cpp
@@ -42,7 +42,7 @@
 }
 
 // For CA
-std::tuple<Error, std::string>
+std::tuple<ErrorCode, std::string>
 ChallengePin::handleChallengeRequest(const Block& params, CertificateRequest& request)
 {
   params.parse();
@@ -63,7 +63,7 @@
     std::string givenCode = readString(params.get(tlv_parameter_value));
     auto secret = request.m_challengeSecrets;
     if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
-      return returnWithError(request, Error::OUT_OF_TIME, "Secret expired.");
+      return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
     }
     if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
       _LOG_TRACE("Correct PIN code. Challenge succeeded.");
@@ -78,10 +78,10 @@
     else {
       // run out times
       _LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
-      return returnWithError(request, Error::OUT_OF_TRIES, "Ran out tires.");
+      return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
     }
   }
-  return returnWithError(request, Error::INVALID_PARAMETER, "Unexpected status or challenge status");
+  return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
 }
 
 // For Client
diff --git a/src/challenge-module/challenge-pin.hpp b/src/challenge-module/challenge-pin.hpp
index 88cf9bd..9e7d8b0 100644
--- a/src/challenge-module/challenge-pin.hpp
+++ b/src/challenge-module/challenge-pin.hpp
@@ -52,7 +52,7 @@
                const time::seconds& secretLifetime = time::seconds(3600));
 
   // For CA
-  std::tuple<Error, std::string>
+  std::tuple<ErrorCode, std::string>
   handleChallengeRequest(const Block& params, CertificateRequest& request) override;
 
   // For Client
diff --git a/src/ndncert-common.hpp b/src/ndncert-common.hpp
index 6727a88..63c7881 100644
--- a/src/ndncert-common.hpp
+++ b/src/ndncert-common.hpp
@@ -148,7 +148,7 @@
 
 std::string statusToString(Status status);
 
-enum class Error : uint16_t {
+enum class ErrorCode : uint16_t {
   NO_ERROR = 0,
   BAD_INTEREST_FORMAT = 1,
   BAD_PARAMETER_FORMAT = 2,
diff --git a/src/protocol-detail/error.cpp b/src/protocol-detail/error.cpp
index 0b24eec..739c3b7 100644
--- a/src/protocol-detail/error.cpp
+++ b/src/protocol-detail/error.cpp
@@ -24,7 +24,7 @@
 namespace ndncert {
 
 Block
-ERROR::encodeDataContent(Error errorCode, const std::string& description)
+ErrorTLV::encodeDataContent(ErrorCode errorCode, const std::string& description)
 {
   Block response = makeEmptyBlock(tlv::Content);
   response.push_back(makeNonNegativeIntegerBlock(tlv_error_code, static_cast<size_t>(errorCode)));
@@ -33,11 +33,11 @@
   return response;
 }
 
-std::tuple<Error, std::string>
-ERROR::decodefromDataContent(const Block& block)
+std::tuple<ErrorCode, std::string>
+ErrorTLV::decodefromDataContent(const Block& block)
 {
   block.parse();
-  Error error = static_cast<Error>(readNonNegativeInteger(block.get(tlv_error_code)));
+  ErrorCode error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv_error_code)));
   auto description = readString(block.get(tlv_error_info));
   return std::make_tuple(error, description);
 }
diff --git a/src/protocol-detail/error.hpp b/src/protocol-detail/error.hpp
index c03ec05..1234233 100644
--- a/src/protocol-detail/error.hpp
+++ b/src/protocol-detail/error.hpp
@@ -27,18 +27,18 @@
 namespace ndn {
 namespace ndncert {
 
-class ERROR {
+class ErrorTLV {
 public:
   /**
    * Encode error information into a Data content TLV
    */
   static Block
-  encodeDataContent(Error errorCode, const std::string& description);
+  encodeDataContent(ErrorCode errorCode, const std::string& description);
 
   /**
    * Decode error information from Data content TLV
    */
-  static std::tuple<Error, std::string>
+  static std::tuple<ErrorCode, std::string>
   decodefromDataContent(const Block& block);
 };