blob: 793eed7fa145a74b673b5ea787b26ce3213edb68 [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
31NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "Email");
32
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 Zhanga9bda732017-05-20 22:58:55 -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 Zhangaf7c2902019-03-14 22:13:21 -070053 if (request.m_challengeStatus == "") {
54 // for the first time, init the challenge
55 std::string emailAddress = params.get<std::string>(JSON_EMAIL);
56 if (!isValidEmailAddress(emailAddress)) {
57 request.m_status = STATUS_FAILURE;
58 request.m_challengeStatus = FAILURE_INVALID_EMAIL;
59 return;
60 }
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070061 // check whether this email is the same as the one used in PROBE
62 if (request.m_probeToken != nullptr) {
63 const auto& content = request.m_probeToken->getContent();
64 const auto& json = CaModule::jsonFromBlock(content);
65 const auto& expectedEmail = json.get("email", "");
66 Name expectedPrefix(json.get(JSON_CA_NAME, ""));
67 if (expectedEmail != emailAddress || !expectedPrefix.isPrefixOf(request.m_cert.getName())) {
68 return;
69 }
70 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071 request.m_status = STATUS_CHALLENGE;
72 request.m_challengeStatus = NEED_CODE;
73 request.m_challengeType = CHALLENGE_TYPE;
74 std::string emailCode = generateSecretCode();
75 JsonSection secretJson;
76 secretJson.add(JSON_CODE, emailCode);
77 request.m_challengeSecrets = secretJson;
78 request.m_challengeTp = time::toIsoString(time::system_clock::now());
79 request.m_remainingTime = m_secretLifetime.count();
80 request.m_remainingTries = m_maxAttemptTimes;
81 // send out the email
82 sendEmail(emailAddress, emailCode, request);
83 _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode);
84 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080085 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 else if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) {
87 _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus);
88 // the incoming interest should bring the pin code
89 std::string givenCode = params.get<std::string>(JSON_CODE);
90 const auto realCode = request.m_challengeSecrets.get<std::string>(JSON_CODE);
91 if (time::system_clock::now() - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
92 // secret expires
93 request.m_status = STATUS_FAILURE;
94 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_TIMEOUT;
95 updateRequestOnChallengeEnd(request);
96 _LOG_TRACE("Secret expired. Challenge failed.");
97 return;
98 }
99 else if (givenCode == realCode) {
100 // the code is correct
101 request.m_status = STATUS_PENDING;
102 request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS;
103 updateRequestOnChallengeEnd(request);
104 _LOG_TRACE("Secret code matched. Challenge succeeded.");
105 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800106 }
107 else {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 // check rest attempt times
109 if (request.m_remainingTries > 1) {
110 request.m_challengeStatus = WRONG_CODE;
111 request.m_remainingTries = request.m_remainingTries - 1;
112 auto remainTime = m_secretLifetime - (time::system_clock::now() - time::fromIsoString(request.m_challengeTp));
113 request.m_remainingTime = remainTime.count();
114 _LOG_TRACE("Secret code didn't match. Remaining Tries - 1.");
115 return;
116 }
117 else {
118 // run out times
119 request.m_status = STATUS_FAILURE;
120 request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_MAXRETRY;
121 updateRequestOnChallengeEnd(request);
122 _LOG_TRACE("Secret code didn't match. Ran out tires. Challenge failed.");
123 return;
124 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800125 }
126 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700127 else {
128 _LOG_ERROR("The challenge status is wrong");
129 request.m_status = STATUS_FAILURE;
130 return;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800131 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700132}
133
134// For Client
135JsonSection
136ChallengeEmail::getRequirementForChallenge(int status, const std::string& challengeStatus)
137{
138 JsonSection result;
139 if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") {
140 result.put(JSON_EMAIL, "Please_input_your_email_address");
141 }
142 else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) {
143 result.put(JSON_CODE, "Please_input_your_verification_code");
144 }
145 else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) {
146 result.put(JSON_CODE, "Incorrect_code_please_try_again");
147 }
148 else {
149 _LOG_ERROR("CA's status and challenge status are wrong");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800150 }
151 return result;
152}
153
154JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700155ChallengeEmail::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800156{
157 JsonSection result;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700158 if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") {
159 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
160 result.put(JSON_EMAIL, params.get<std::string>(JSON_EMAIL, ""));
161 }
162 else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) {
163 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
164 result.put(JSON_CODE, params.get<std::string>(JSON_CODE, ""));
165 }
166 else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) {
167 result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE);
168 result.put(JSON_CODE, params.get<std::string>(JSON_CODE, ""));
169 }
170 else {
171 _LOG_ERROR("Client's status and challenge status are wrong");
172 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800173 return result;
174}
175
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800176bool
177ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
178{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700179 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
180 static const std::regex emailPattern(pattern);
181 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800182}
183
184void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700185ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700186 const CertificateRequest& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800187{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700188 std::string command = m_sendEmailScript;
Zhiyi Zhang70d74b42019-06-11 22:27:07 -0700189 command += " \"" + emailAddress + "\" \"" + secret + "\" \""
190 + request.m_caName.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\"";
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700191 int result = system(command.c_str());
192 if (result == -1) {
193 _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800194 }
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700195 _LOG_TRACE("EmailSending Script " + m_sendEmailScript +
196 " was executed successfully with return value" + std::to_string(result) + ".");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800197 return;
198}
199
200} // namespace ndncert
201} // namespace ndn