blob: 80670f4199381972c121512d3ed06f2daa750cbb [file] [log] [blame]
/* -*- 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_CHALLENGE_MODULE_HPP
#define NDNCERT_CHALLENGE_MODULE_HPP
#include "ndncert-common.hpp"
#include "certificate-request.hpp"
namespace ndn {
namespace ndncert {
class ChallengeModule : noncopyable
{
public:
/**
* @brief Error that can be thrown from ChallengeModule
*
* ChallengeModule should throw Error to notice CA there's an Error. In this case, CA will
* generate an Error JSON file back to end entity.
*/
class Error : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
public:
explicit
ChallengeModule(const std::string& uniqueType);
virtual
~ChallengeModule();
template<class ChallengeType>
static void
registerChallengeModule(const std::string& typeName)
{
ChallengeFactory& factory = getFactory();
BOOST_ASSERT(factory.count(typeName) == 0);
factory[typeName] = [] { return make_unique<ChallengeType>(); };
}
static bool
supportChallenge(const std::string& challengeType);
static unique_ptr<ChallengeModule>
createChallengeModule(const std::string& challengeType);
// For CA
virtual void
handleChallengeRequest(const JsonSection& params, CertificateRequest& request) = 0;
// For Client
virtual JsonSection
getRequirementForChallenge(int status, const std::string& challengeStatus) = 0;
virtual JsonSection
genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) = 0;
// helpers
static std::string
generateSecretCode();
protected:
void
updateRequestOnChallengeEnd(CertificateRequest& request);
public:
const std::string CHALLENGE_TYPE;
private:
typedef function<unique_ptr<ChallengeModule> ()> ChallengeCreateFunc;
typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
static ChallengeFactory&
getFactory();
};
#define NDNCERT_REGISTER_CHALLENGE(C, T) \
static class NdnCert ## C ## ChallengeRegistrationClass \
{ \
public: \
NdnCert ## C ## ChallengeRegistrationClass() \
{ \
::ndn::ndncert::ChallengeModule::registerChallengeModule<C>(T);\
} \
} g_NdnCert ## C ## ChallengeRegistrationVariable
} // namespace ndncert
} // namespace ndn
#endif // NDNCERT_CHALLENGE_MODULE_HPP