blob: cd08d9b8a2a0a556f333097560aeb141d83cbfcf [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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"
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070022#include "../ca-module.hpp"
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080023#include "../logging.hpp"
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070024#include <regex>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080025
26namespace ndn {
27namespace ndncert {
28
29_LOG_INIT(ndncert.ChallengeEmail);
30
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 Zhangdefa9592017-02-21 10:56:22 -080035const std::string ChallengeEmail::FAILURE_INVALID_EMAIL = "failure-invalid-email";
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080036const std::string ChallengeEmail::JSON_EMAIL = "email";
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080037const std::string ChallengeEmail::JSON_CODE = "code";
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080038
39ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
40 const size_t& maxAttemptTimes,
41 const time::seconds secretLifetime)
Zhiyi Zhang36706832019-07-04 21:33:03 -070042 : ChallengeModule("email")
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080043 , m_sendEmailScript(scriptPath)
44 , m_maxAttemptTimes(maxAttemptTimes)
45 , m_secretLifetime(secretLifetime)
46{
47}
48
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049// For CA
50void
Suyong Won19fba4d2020-05-09 13:39:46 -070051ChallengeEmail::handleChallengeRequest(const Block& params, CertificateRequest& request)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080052{
Suyong Won44d0cce2020-05-10 04:07:43 -070053 params.parse();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080054 auto currentTime = time::system_clock::now();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 if (request.m_challengeStatus == "") {
56 // for the first time, init the challenge
Suyong Won19fba4d2020-05-09 13:39:46 -070057 std::string emailAddress = readString(params.get(tlv_parameter_value));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 if (!isValidEmailAddress(emailAddress)) {
Zhiyi Zhang48f23782020-09-28 12:11:24 -070059 request.m_status = Status::FAILURE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 request.m_challengeStatus = FAILURE_INVALID_EMAIL;
61 return;
62 }
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070063 // check whether this email is the same as the one used in PROBE
64 if (request.m_probeToken != nullptr) {
65 const auto& content = request.m_probeToken->getContent();
66 const auto& json = CaModule::jsonFromBlock(content);
67 const auto& expectedEmail = json.get("email", "");
68 Name expectedPrefix(json.get(JSON_CA_NAME, ""));
69 if (expectedEmail != emailAddress || !expectedPrefix.isPrefixOf(request.m_cert.getName())) {
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070070 _LOG_ERROR("Cannot match with the PROBE token. Input email: " << emailAddress
71 << " Email in Token: " << expectedEmail
72 << " Requested Cert Name: " << request.m_cert.getName()
73 << " Identity Name got from Token: " << expectedPrefix);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070074 return;
75 }
76 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -070077 request.m_status = Status::CHALLENGE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 request.m_challengeStatus = NEED_CODE;
79 request.m_challengeType = CHALLENGE_TYPE;
80 std::string emailCode = generateSecretCode();
81 JsonSection secretJson;
82 secretJson.add(JSON_CODE, emailCode);
83 request.m_challengeSecrets = secretJson;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080084 request.m_challengeTp = time::toIsoString(currentTime);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 request.m_remainingTime = m_secretLifetime.count();
86 request.m_remainingTries = m_maxAttemptTimes;
87 // send out the email
88 sendEmail(emailAddress, emailCode, request);
89 _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode);
90 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080091 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092 else if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) {
93 _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus);
94 // the incoming interest should bring the pin code
Suyong Won19fba4d2020-05-09 13:39:46 -070095 std::string givenCode = readString(params.get(tlv_parameter_value));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096 const auto realCode = request.m_challengeSecrets.get<std::string>(JSON_CODE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080097 if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 // secret expires
Zhiyi Zhang48f23782020-09-28 12:11:24 -070099 request.m_status = Status::FAILURE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700100 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_TIMEOUT;
101 updateRequestOnChallengeEnd(request);
102 _LOG_TRACE("Secret expired. Challenge failed.");
103 return;
104 }
105 else if (givenCode == realCode) {
106 // the code is correct
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700107 request.m_status = Status::PENDING;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS;
109 updateRequestOnChallengeEnd(request);
110 _LOG_TRACE("Secret code matched. Challenge succeeded.");
111 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800112 }
113 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114 // check rest attempt times
115 if (request.m_remainingTries > 1) {
116 request.m_challengeStatus = WRONG_CODE;
117 request.m_remainingTries = request.m_remainingTries - 1;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800118 auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 request.m_remainingTime = remainTime.count();
120 _LOG_TRACE("Secret code didn't match. Remaining Tries - 1.");
121 return;
122 }
123 else {
124 // run out times
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700125 request.m_status = Status::FAILURE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700126 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_MAXRETRY;
127 updateRequestOnChallengeEnd(request);
128 _LOG_TRACE("Secret code didn't match. Ran out tires. Challenge failed.");
129 return;
130 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800131 }
132 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700133 else {
134 _LOG_ERROR("The challenge status is wrong");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700135 request.m_status = Status::FAILURE;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800137 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700138}
139
140// For Client
141JsonSection
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700142ChallengeEmail::getRequirementForChallenge(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700143{
144 JsonSection result;
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700145 if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700146 result.put(JSON_EMAIL, "Please_input_your_email_address");
147 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700148 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700149 result.put(JSON_CODE, "Please_input_your_verification_code");
150 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700151 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700152 result.put(JSON_CODE, "Incorrect_code_please_try_again");
153 }
154 else {
155 _LOG_ERROR("CA's status and challenge status are wrong");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800156 }
157 return result;
158}
159
160JsonSection
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700161ChallengeEmail::genChallengeRequestJson(Status status, const std::string& challengeStatus, const JsonSection& params)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800162{
163 JsonSection result;
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700164 if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700165 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800166 result.put(JSON_EMAIL, params.get(JSON_EMAIL, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700167 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700168 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700169 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800170 result.put(JSON_CODE, params.get(JSON_CODE, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700171 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700172 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700173 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800174 result.put(JSON_CODE, params.get(JSON_CODE, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700175 }
176 else {
177 _LOG_ERROR("Client's status and challenge status are wrong");
178 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800179 return result;
180}
181
Suyong Won19fba4d2020-05-09 13:39:46 -0700182Block
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700183ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus, const JsonSection& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700184{
185 Block request = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700186 if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") {
Suyong Won19fba4d2020-05-09 13:39:46 -0700187 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
188 request.push_back(makeStringBlock(tlv_parameter_key, JSON_EMAIL));
189 request.push_back(makeStringBlock(tlv_parameter_value, params.get(JSON_EMAIL,"")));
190 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700191 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Suyong Won19fba4d2020-05-09 13:39:46 -0700192 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
193 request.push_back(makeStringBlock(tlv_parameter_key, JSON_CODE));
194 request.push_back(makeStringBlock(tlv_parameter_value, params.get(JSON_CODE,"")));
195 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700196 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Suyong Won19fba4d2020-05-09 13:39:46 -0700197 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
198 request.push_back(makeStringBlock(tlv_parameter_key, JSON_CODE));
199 request.push_back(makeStringBlock(tlv_parameter_value, params.get(JSON_CODE,"")));
200 }
201 else {
202 _LOG_ERROR("Client's status and challenge status are wrong");
203 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700204 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700205 return request;
206}
207
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800208bool
209ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
210{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700211 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
212 static const std::regex emailPattern(pattern);
213 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800214}
215
216void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700217ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700218 const CertificateRequest& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800219{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700220 std::string command = m_sendEmailScript;
Zhiyi Zhang70d74b42019-06-11 22:27:07 -0700221 command += " \"" + emailAddress + "\" \"" + secret + "\" \""
Suyong Won256c9062020-05-11 02:45:56 -0700222 + request.m_caPrefix.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\"";
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700223 int result = system(command.c_str());
224 if (result == -1) {
225 _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800226 }
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700227 _LOG_TRACE("EmailSending Script " + m_sendEmailScript +
228 " was executed successfully with return value" + std::to_string(result) + ".");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800229 return;
230}
231
232} // namespace ndncert
233} // namespace ndn