blob: c4de9ce73ab66e520f322ca25f84dd9fd1e49b8b [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
tylerliu1f2c3cf2021-11-15 15:43:02 -08002/*
Davide Pesavento9510c912024-02-25 17:50:05 -05003 * Copyright (c) 2017-2024, Regents of the University of California.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "challenge-email.hpp"
Davide Pesavento9510c912024-02-25 17:50:05 -050022
23#include <ndn-cxx/util/logger.hpp>
24
Zhiyi Zhang523f0c22020-09-29 14:19:20 -070025#include <regex>
tylerliu1f480be2020-11-10 13:02:53 -080026#include <boost/process.hpp>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080027
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080028namespace ndncert {
29
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070030NDN_LOG_INIT(ndncert.challenge.email);
Zhiyi Zhang36706832019-07-04 21:33:03 -070031NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080032
33const std::string ChallengeEmail::NEED_CODE = "need-code";
34const std::string ChallengeEmail::WRONG_CODE = "wrong-code";
Zhiyi Zhang46049832020-09-28 17:08:12 -070035const std::string ChallengeEmail::PARAMETER_KEY_EMAIL = "email";
36const std::string ChallengeEmail::PARAMETER_KEY_CODE = "code";
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080037
38ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
39 const size_t& maxAttemptTimes,
40 const time::seconds secretLifetime)
Davide Pesavento0dc02012021-11-23 22:55:03 -050041 : ChallengeModule("email", maxAttemptTimes, secretLifetime)
42 , m_sendEmailScript(scriptPath)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080043{
44}
45
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070046// For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070047std::tuple<ErrorCode, std::string>
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070048ChallengeEmail::handleChallengeRequest(const Block& params, ca::RequestState& request)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080049{
Suyong Won44d0cce2020-05-10 04:07:43 -070050 params.parse();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080051 auto currentTime = time::system_clock::now();
tylerliu7b9185c2020-11-24 12:15:18 -080052 if (request.status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 // for the first time, init the challenge
tylerliu50d679e2020-10-14 14:08:39 -070054 std::string emailAddress = readString(params.get(tlv::ParameterValue));
tylerliu7b9185c2020-11-24 12:15:18 -080055 auto lastComponentRequested = readString(request.cert.getIdentity().get(-1));
Zhiyi Zhang46049832020-09-28 17:08:12 -070056 if (lastComponentRequested != emailAddress) {
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040057 NDN_LOG_TRACE("Email and requested name do not match. Email " << emailAddress
58 << " - requested last component " << lastComponentRequested);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070059 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 std::string emailCode = generateSecretCode();
61 JsonSection secretJson;
Zhiyi Zhang46049832020-09-28 17:08:12 -070062 secretJson.add(PARAMETER_KEY_CODE, emailCode);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063 // send out the email
64 sendEmail(emailAddress, emailCode, request);
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040065 NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " : " << emailCode);
Davide Pesavento0dc02012021-11-23 22:55:03 -050066 return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson),
67 m_maxAttemptTimes, m_secretLifetime);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080068 }
tylerliu7b9185c2020-11-24 12:15:18 -080069 if (request.challengeState) {
70 if (request.challengeState->challengeStatus == NEED_CODE ||
71 request.challengeState->challengeStatus == WRONG_CODE) {
72 NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070073 // the incoming interest should bring the pin code
tylerliu50d679e2020-10-14 14:08:39 -070074 std::string givenCode = readString(params.get(tlv::ParameterValue));
tylerliu7b9185c2020-11-24 12:15:18 -080075 auto secret = request.challengeState->secrets;
Zhiyi Zhanga749f442020-09-29 17:19:51 -070076 // check if run out of time
tylerliu7b9185c2020-11-24 12:15:18 -080077 if (currentTime - request.challengeState->timestamp >= m_secretLifetime) {
Zhiyi Zhanga749f442020-09-29 17:19:51 -070078 return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
79 }
80 // check if provided secret is correct
81 if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
82 // the code is correct
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070083 NDN_LOG_TRACE("Correct secret code. Challenge succeeded.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070084 return returnWithSuccess(request);
85 }
86 // otherwise, check remaining attempt times
tylerliu7b9185c2020-11-24 12:15:18 -080087 if (request.challengeState->remainingTries > 1) {
88 auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070089 NDN_LOG_TRACE("Wrong secret code provided. Remaining Tries - 1.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070090 return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
tylerliu7b9185c2020-11-24 12:15:18 -080091 request.challengeState->remainingTries - 1,
Zhiyi Zhanga749f442020-09-29 17:19:51 -070092 time::duration_cast<time::seconds>(remainTime));
93 }
94 else {
95 // run out times
Tianyuan Yu13aac732022-03-03 20:59:54 -080096 NDN_LOG_TRACE("Wrong secret code provided. Ran out of tries. Challenge failed.");
97 return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070098 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080099 }
100 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700101 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102}
103
104// For Client
tylerliu40226332020-11-11 15:37:16 -0800105std::multimap<std::string, std::string>
Zhiyi Zhang46049832020-09-28 17:08:12 -0700106ChallengeEmail::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700107{
tylerliu40226332020-11-11 15:37:16 -0800108 std::multimap<std::string, std::string> result;
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -0400109 if (status == Status::BEFORE_CHALLENGE && challengeStatus.empty()) {
tylerliu40226332020-11-11 15:37:16 -0800110 result.emplace(PARAMETER_KEY_EMAIL, "Please input your email address");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700112 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
tylerliu40226332020-11-11 15:37:16 -0800113 result.emplace(PARAMETER_KEY_CODE, "Please input your verification code");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700115 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
tylerliu40226332020-11-11 15:37:16 -0800116 result.emplace(PARAMETER_KEY_CODE, "Incorrect code, please try again");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700117 }
118 else {
tylerliu41c11532020-10-10 16:14:45 -0700119 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800121 return result;
122}
123
Suyong Won19fba4d2020-05-09 13:39:46 -0700124Block
Zhiyi Zhangba8e45e2020-11-03 08:55:32 -0800125ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
tylerliuf2e6bb52020-12-13 13:23:05 -0800126 const std::multimap<std::string, std::string>& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700127{
tylerliu1f480be2020-11-10 13:02:53 -0800128 Block request(tlv::EncryptedPayload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700129 if (status == Status::BEFORE_CHALLENGE) {
tylerliu40226332020-11-11 15:37:16 -0800130 if (params.size() != 1 || params.find(PARAMETER_KEY_EMAIL) == params.end()) {
tylerliu41c11532020-10-10 16:14:45 -0700131 NDN_THROW(std::runtime_error("Wrong parameter provided."));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700132 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500133 request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
134 request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_EMAIL));
135 request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_EMAIL)->second));
Suyong Won19fba4d2020-05-09 13:39:46 -0700136 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700137 else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
tylerliu40226332020-11-11 15:37:16 -0800138 if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) {
tylerliu41c11532020-10-10 16:14:45 -0700139 NDN_THROW(std::runtime_error("Wrong parameter provided."));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700140 }
Davide Pesavento0dc02012021-11-23 22:55:03 -0500141 request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
142 request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
143 request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
Suyong Won19fba4d2020-05-09 13:39:46 -0700144 }
145 else {
tylerliu41c11532020-10-10 16:14:45 -0700146 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700147 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700148 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700149 return request;
150}
151
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800152bool
153ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
154{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700155 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
156 static const std::regex emailPattern(pattern);
157 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800158}
159
160void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700161ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700162 const ca::RequestState& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800163{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700164 std::string command = m_sendEmailScript;
Zhiyi Zhangba8e45e2020-11-03 08:55:32 -0800165 command += " \"" + emailAddress + "\" \"" + secret + "\" \"" +
tylerliu7b9185c2020-11-24 12:15:18 -0800166 request.caPrefix.toUri() + "\" \"" +
167 request.cert.getName().toUri() + "\"";
tylerliu1f480be2020-11-10 13:02:53 -0800168 boost::process::child child(command);
169 child.wait();
170 if (child.exit_code() != 0) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700171 NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800172 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800173 else {
174 NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript +
175 " was executed successfully with return value 0.");
176 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800177}
178
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700179} // namespace ndncert