blob: b9ca344f60c9eec3fd5295022dcd149236cb2aa3 [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
51ChallengeEmail::handleChallengeRequest(const JsonSection& params, CertificateRequest& request)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080052{
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080053 auto currentTime = time::system_clock::now();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054 if (request.m_challengeStatus == "") {
55 // for the first time, init the challenge
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080056 std::string emailAddress = params.get(JSON_EMAIL, "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057 if (!isValidEmailAddress(emailAddress)) {
58 request.m_status = STATUS_FAILURE;
59 request.m_challengeStatus = FAILURE_INVALID_EMAIL;
60 return;
61 }
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070062 // check whether this email is the same as the one used in PROBE
63 if (request.m_probeToken != nullptr) {
64 const auto& content = request.m_probeToken->getContent();
65 const auto& json = CaModule::jsonFromBlock(content);
66 const auto& expectedEmail = json.get("email", "");
67 Name expectedPrefix(json.get(JSON_CA_NAME, ""));
68 if (expectedEmail != emailAddress || !expectedPrefix.isPrefixOf(request.m_cert.getName())) {
Zhiyi Zhang42e1cf32019-06-22 17:11:42 -070069 _LOG_ERROR("Cannot match with the PROBE token. Input email: " << emailAddress
70 << " Email in Token: " << expectedEmail
71 << " Requested Cert Name: " << request.m_cert.getName()
72 << " Identity Name got from Token: " << expectedPrefix);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070073 return;
74 }
75 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 request.m_status = STATUS_CHALLENGE;
77 request.m_challengeStatus = NEED_CODE;
78 request.m_challengeType = CHALLENGE_TYPE;
79 std::string emailCode = generateSecretCode();
80 JsonSection secretJson;
81 secretJson.add(JSON_CODE, emailCode);
82 request.m_challengeSecrets = secretJson;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080083 request.m_challengeTp = time::toIsoString(currentTime);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084 request.m_remainingTime = m_secretLifetime.count();
85 request.m_remainingTries = m_maxAttemptTimes;
86 // send out the email
87 sendEmail(emailAddress, emailCode, request);
88 _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode);
89 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080090 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091 else if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) {
92 _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus);
93 // the incoming interest should bring the pin code
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080094 std::string givenCode = params.get(JSON_CODE, "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095 const auto realCode = request.m_challengeSecrets.get<std::string>(JSON_CODE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080096 if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097 // secret expires
98 request.m_status = STATUS_FAILURE;
99 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_TIMEOUT;
100 updateRequestOnChallengeEnd(request);
101 _LOG_TRACE("Secret expired. Challenge failed.");
102 return;
103 }
104 else if (givenCode == realCode) {
105 // the code is correct
106 request.m_status = STATUS_PENDING;
107 request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS;
108 updateRequestOnChallengeEnd(request);
109 _LOG_TRACE("Secret code matched. Challenge succeeded.");
110 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800111 }
112 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113 // check rest attempt times
114 if (request.m_remainingTries > 1) {
115 request.m_challengeStatus = WRONG_CODE;
116 request.m_remainingTries = request.m_remainingTries - 1;
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800117 auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700118 request.m_remainingTime = remainTime.count();
119 _LOG_TRACE("Secret code didn't match. Remaining Tries - 1.");
120 return;
121 }
122 else {
123 // run out times
124 request.m_status = STATUS_FAILURE;
125 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_MAXRETRY;
126 updateRequestOnChallengeEnd(request);
127 _LOG_TRACE("Secret code didn't match. Ran out tires. Challenge failed.");
128 return;
129 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800130 }
131 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700132 else {
133 _LOG_ERROR("The challenge status is wrong");
134 request.m_status = STATUS_FAILURE;
135 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800136 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137}
138
139// For Client
140JsonSection
141ChallengeEmail::getRequirementForChallenge(int status, const std::string& challengeStatus)
142{
143 JsonSection result;
144 if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") {
145 result.put(JSON_EMAIL, "Please_input_your_email_address");
146 }
147 else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) {
148 result.put(JSON_CODE, "Please_input_your_verification_code");
149 }
150 else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) {
151 result.put(JSON_CODE, "Incorrect_code_please_try_again");
152 }
153 else {
154 _LOG_ERROR("CA's status and challenge status are wrong");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800155 }
156 return result;
157}
158
159JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700160ChallengeEmail::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800161{
162 JsonSection result;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700163 if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") {
164 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800165 result.put(JSON_EMAIL, params.get(JSON_EMAIL, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700166 }
167 else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) {
168 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800169 result.put(JSON_CODE, params.get(JSON_CODE, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700170 }
171 else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) {
172 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
Zhiyi Zhang8da54d62019-11-21 00:03:05 -0800173 result.put(JSON_CODE, params.get(JSON_CODE, ""));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700174 }
175 else {
176 _LOG_ERROR("Client's status and challenge status are wrong");
177 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800178 return result;
179}
180
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800181bool
182ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
183{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700184 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
185 static const std::regex emailPattern(pattern);
186 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800187}
188
189void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700190ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700191 const CertificateRequest& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800192{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700193 std::string command = m_sendEmailScript;
Zhiyi Zhang70d74b42019-06-11 22:27:07 -0700194 command += " \"" + emailAddress + "\" \"" + secret + "\" \""
195 + request.m_caName.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\"";
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700196 int result = system(command.c_str());
197 if (result == -1) {
198 _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800199 }
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700200 _LOG_TRACE("EmailSending Script " + m_sendEmailScript +
201 " was executed successfully with return value" + std::to_string(result) + ".");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800202 return;
203}
204
205} // namespace ndncert
206} // namespace ndn