Skip to content

Commit

Permalink
Improve log messages and general code cleanup in CaModule
Browse files Browse the repository at this point in the history
Change-Id: Ie455ec14594e7662800faa887da72574bff73407
  • Loading branch information
Pesa committed Dec 23, 2024
1 parent 6481e1e commit 6866b90
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 291 deletions.
377 changes: 183 additions & 194 deletions src/ca-module.cpp

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions src/ca-module.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017-2022, Regents of the University of California.
* Copyright (c) 2017-2024, Regents of the University of California.
*
* This file is part of ndncert, a certificate management system based on NDN.
*
Expand All @@ -22,7 +22,6 @@
#define NDNCERT_CA_MODULE_HPP

#include "detail/ca-configuration.hpp"
#include "detail/crypto-helpers.hpp"
#include "detail/ca-storage.hpp"

#include <ndn-cxx/face.hpp>
Expand All @@ -46,8 +45,6 @@ class CaModule : boost::noncopyable
CaModule(ndn::Face& face, ndn::KeyChain& keyChain, const std::string& configPath,
const std::string& storageType = "ca-storage-sqlite3");

~CaModule();

CaConfig&
getCaConf()
{
Expand All @@ -61,12 +58,18 @@ class CaModule : boost::noncopyable
}

void
setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback);
setStatusUpdateCallback(StatusUpdateCallback cb)
{
m_statusUpdateCallback = std::move(cb);
}

Data
const Data&
getCaProfileData();

NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
void
registerPrefix();

void
onCaProfileDiscovery(const Interest& request);

Expand All @@ -79,35 +82,27 @@ class CaModule : boost::noncopyable
void
onChallenge(const Interest& request);

void
onRegisterFailed(const std::string& reason);

std::unique_ptr<RequestState>
getCertificateRequest(const Interest& request);

Certificate
issueCertificate(const RequestState& requestState);

void
registerPrefix();

Data
generateErrorDataPacket(const Name& name, ErrorCode error, const std::string& errorInfo);
makeErrorPacket(const Name& name, ErrorCode errorCode, std::string_view errorInfo);

NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
ndn::Face& m_face;
ndn::KeyChain& m_keyChain;
CaConfig m_config;
std::unique_ptr<CaStorage> m_storage;
ndn::KeyChain& m_keyChain;

uint8_t m_requestIdGenKey[32];
std::unique_ptr<Data> m_profileData;
/**
* StatusUpdate Callback function
*/
StatusUpdateCallback m_statusUpdateCallback;

std::list<ndn::RegisteredPrefixHandle> m_registeredPrefixHandles;
std::list<ndn::InterestFilterHandle> m_interestFilterHandles;
std::vector<ndn::ScopedRegisteredPrefixHandle> m_registeredPrefixes;
std::vector<ndn::ScopedInterestFilterHandle> m_interestFilters;
};

} // namespace ndncert::ca
Expand Down
34 changes: 18 additions & 16 deletions src/challenge/challenge-email.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,59 @@ ChallengeEmail::handleChallengeRequest(const Block& params, ca::RequestState& re
{
params.parse();
auto currentTime = time::system_clock::now();

if (request.status == Status::BEFORE_CHALLENGE) {
// for the first time, init the challenge
std::string emailAddress = readString(params.get(tlv::ParameterValue));
auto lastComponentRequested = readString(request.cert.getIdentity().get(-1));
if (lastComponentRequested != emailAddress) {
NDN_LOG_TRACE("Email and requested name do not match. Email " << emailAddress
<< " - requested last component " << lastComponentRequested);
NDN_LOG_TRACE("Email and requested name do not match: email=" << emailAddress
<< " requested=" << lastComponentRequested);
}
std::string emailCode = generateSecretCode();
JsonSection secretJson;
secretJson.add(PARAMETER_KEY_CODE, emailCode);
// send out the email
sendEmail(emailAddress, emailCode, request);
NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " : " << emailCode);
NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << emailCode);
return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson),
m_maxAttemptTimes, m_secretLifetime);
}

if (request.challengeState) {
if (request.challengeState->challengeStatus == NEED_CODE ||
request.challengeState->challengeStatus == WRONG_CODE) {
NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus);
NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus);
// the incoming interest should bring the pin code
std::string givenCode = readString(params.get(tlv::ParameterValue));
auto secret = request.challengeState->secrets;
// check if run out of time
if (currentTime - request.challengeState->timestamp >= m_secretLifetime) {
NDN_LOG_TRACE("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)) {
// the code is correct
NDN_LOG_TRACE("Correct secret code. Challenge succeeded.");
NDN_LOG_TRACE("Secret is correct, challenge succeeded");
return returnWithSuccess(request);
}
// otherwise, check remaining attempt times
if (request.challengeState->remainingTries > 1) {
auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp);
NDN_LOG_TRACE("Wrong secret code provided. Remaining Tries - 1.");
NDN_LOG_TRACE("Wrong secret, remaining tries = " << request.challengeState->remainingTries - 1);
return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
request.challengeState->remainingTries - 1,
time::duration_cast<time::seconds>(remainTime));
}
else {
// run out times
NDN_LOG_TRACE("Wrong secret code provided. Ran out of tries. Challenge failed.");
NDN_LOG_TRACE("Wrong secret, no tries remaining");
return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries.");
}
}
}
return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");

return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected challenge status.");
}

// For Client
Expand All @@ -116,7 +119,7 @@ ChallengeEmail::getRequestedParameterList(Status status, const std::string& chal
result.emplace(PARAMETER_KEY_CODE, "Incorrect code, please try again");
}
else {
NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
return result;
}
Expand All @@ -128,22 +131,22 @@ ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challen
Block request(tlv::EncryptedPayload);
if (status == Status::BEFORE_CHALLENGE) {
if (params.size() != 1 || params.find(PARAMETER_KEY_EMAIL) == params.end()) {
NDN_THROW(std::runtime_error("Wrong parameter provided."));
NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
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."));
NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
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."));
NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
request.encode();
return request;
Expand All @@ -168,11 +171,10 @@ ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& se
boost::process::child child(command);
child.wait();
if (child.exit_code() != 0) {
NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
NDN_LOG_ERROR("Email sending script " + m_sendEmailScript + " failed");
}
else {
NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript +
" was executed successfully with return value 0.");
NDN_LOG_TRACE("Email sending script " + m_sendEmailScript + " succeeded");
}
}

Expand Down
28 changes: 15 additions & 13 deletions src/challenge/challenge-pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,49 @@ ChallengePin::handleChallengeRequest(const Block& params, ca::RequestState& requ
{
params.parse();
auto currentTime = time::system_clock::now();

if (request.status == Status::BEFORE_CHALLENGE) {
NDN_LOG_TRACE("Challenge Interest arrives. Init the challenge");
NDN_LOG_TRACE("Begin challenge");
// for the first time, init the challenge
std::string secretCode = generateSecretCode();
JsonSection secretJson;
secretJson.add(PARAMETER_KEY_CODE, secretCode);
NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId)
<< " : " << secretCode);
NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << secretCode);
return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes,
m_secretLifetime);
}

if (request.challengeState) {
if (request.challengeState->challengeStatus == NEED_CODE ||
request.challengeState->challengeStatus == WRONG_CODE) {
NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus);
NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus);
// the incoming interest should bring the pin code
std::string givenCode = readString(params.get(tlv::ParameterValue));
auto secret = request.challengeState->secrets;
if (currentTime - request.challengeState->timestamp >= m_secretLifetime) {
NDN_LOG_TRACE("Secret expired");
return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
}
if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
NDN_LOG_TRACE("Correct PIN code. Challenge succeeded.");
NDN_LOG_TRACE("PIN is correct, challenge succeeded");
return returnWithSuccess(request);
}
// check rest attempt times
if (request.challengeState->remainingTries > 1) {
auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp);
NDN_LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
NDN_LOG_TRACE("Wrong PIN, remaining tries = " << request.challengeState->remainingTries - 1);
return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
request.challengeState->remainingTries - 1,
time::duration_cast<time::seconds>(remainTime));
}
else {
// run out times
NDN_LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
NDN_LOG_TRACE("Wrong PIN, no tries remaining");
return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries.");
}
}
}
return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");

return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected challenge status.");
}

// For Client
Expand All @@ -101,7 +103,7 @@ ChallengePin::getRequestedParameterList(Status status, const std::string& challe
result.emplace(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again");
}
else {
NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
return result;
}
Expand All @@ -116,14 +118,14 @@ ChallengePin::genChallengeRequestTLV(Status status, const std::string& challenge
}
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."));
NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
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."));
NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
request.encode();
return request;
Expand Down
Loading

0 comments on commit 6866b90

Please sign in to comment.