blob: 39dd16288a72101224ff8cba5fa4ec3412b086e5 [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, 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 Zhang523f0c22020-09-29 14:19:20 -070022#include <regex>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080023
24namespace ndn {
25namespace ndncert {
26
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070027NDN_LOG_INIT(ndncert.challenge.email);
Zhiyi Zhang36706832019-07-04 21:33:03 -070028NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080029
30const std::string ChallengeEmail::NEED_CODE = "need-code";
31const std::string ChallengeEmail::WRONG_CODE = "wrong-code";
Zhiyi Zhangead9f002020-10-03 15:42:52 -070032const std::string ChallengeEmail::INVALID_EMAIL = "invalid-email";
Zhiyi Zhang46049832020-09-28 17:08:12 -070033const std::string ChallengeEmail::PARAMETER_KEY_EMAIL = "email";
34const std::string ChallengeEmail::PARAMETER_KEY_CODE = "code";
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080035
36ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
37 const size_t& maxAttemptTimes,
38 const time::seconds secretLifetime)
Zhiyi Zhangead9f002020-10-03 15:42:52 -070039 : ChallengeModule("email", maxAttemptTimes, secretLifetime)
Zhiyi Zhang46049832020-09-28 17:08:12 -070040 , m_sendEmailScript(scriptPath)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080041{
42}
43
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044// For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070045std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070046ChallengeEmail::handleChallengeRequest(const Block& params, CaState& request)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080047{
Suyong Won44d0cce2020-05-10 04:07:43 -070048 params.parse();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080049 auto currentTime = time::system_clock::now();
Zhiyi Zhang46049832020-09-28 17:08:12 -070050 if (request.m_status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 // for the first time, init the challenge
Suyong Won19fba4d2020-05-09 13:39:46 -070052 std::string emailAddress = readString(params.get(tlv_parameter_value));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 if (!isValidEmailAddress(emailAddress)) {
Zhiyi Zhangead9f002020-10-03 15:42:52 -070054 return returnWithNewChallengeStatus(request, INVALID_EMAIL, JsonSection(), m_maxAttemptTimes - 1, m_secretLifetime);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 }
Zhiyi Zhang46049832020-09-28 17:08:12 -070056 auto lastComponentRequested = readString(request.m_cert.getIdentity().get(-1));
57 if (lastComponentRequested != emailAddress) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070058 NDN_LOG_TRACE("Email and requested name do not match. Email " << emailAddress << "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);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070065 NDN_LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070066 return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080067 }
Zhiyi Zhanga749f442020-09-29 17:19:51 -070068 if (request.m_challengeState) {
69 if (request.m_challengeState->m_challengeStatus == NEED_CODE ||
70 request.m_challengeState->m_challengeStatus == WRONG_CODE) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070071 NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeState->m_challengeStatus);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070072 // the incoming interest should bring the pin code
73 std::string givenCode = readString(params.get(tlv_parameter_value));
74 auto secret = request.m_challengeState->m_secrets;
75 // check if run out of time
76 if (currentTime - request.m_challengeState->m_timestamp >= m_secretLifetime) {
77 return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
78 }
79 // check if provided secret is correct
80 if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
81 // the code is correct
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070082 NDN_LOG_TRACE("Correct secret code. Challenge succeeded.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070083 return returnWithSuccess(request);
84 }
85 // otherwise, check remaining attempt times
86 if (request.m_challengeState->m_remainingTries > 1) {
87 auto remainTime = m_secretLifetime - (currentTime - request.m_challengeState->m_timestamp);
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070088 NDN_LOG_TRACE("Wrong secret code provided. Remaining Tries - 1.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070089 return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
90 request.m_challengeState->m_remainingTries - 1,
91 time::duration_cast<time::seconds>(remainTime));
92 }
93 else {
94 // run out times
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070095 NDN_LOG_TRACE("Wrong secret code provided. Ran out tires. Challenge failed.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070096 return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
97 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080098 }
99 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -0700100 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700101}
102
103// For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -0700104std::vector<std::tuple<std::string, std::string>>
105ChallengeEmail::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700106{
Zhiyi Zhang46049832020-09-28 17:08:12 -0700107 std::vector<std::tuple<std::string, std::string>> result;
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700108 if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700109 result.push_back(std::make_tuple(PARAMETER_KEY_EMAIL, "Please input your email address"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700111 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700112 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your verification code"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700113 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700114 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700115 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect code, please try again"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116 }
117 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700118 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800120 return result;
121}
122
Suyong Won19fba4d2020-05-09 13:39:46 -0700123Block
Zhiyi Zhang46049832020-09-28 17:08:12 -0700124ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700125{
126 Block request = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700127 if (status == Status::BEFORE_CHALLENGE) {
128 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_EMAIL) {
129 BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided."));
130 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700131 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700132 request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_EMAIL));
133 request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700134 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700135 else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
136 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) {
137 BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided."));
138 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700139 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700140 request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE));
141 request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700142 }
143 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700144 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700145 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700146 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700147 return request;
148}
149
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800150bool
151ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
152{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700153 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
154 static const std::regex emailPattern(pattern);
155 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800156}
157
158void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700159ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
tylerliu8704d032020-06-23 10:18:15 -0700160 const CaState& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800161{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700162 std::string command = m_sendEmailScript;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700163 command += " \"" + emailAddress + "\" \"" + secret + "\" \"" + request.m_caPrefix.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\"";
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700164 int result = system(command.c_str());
165 if (result == -1) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700166 NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800167 }
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -0700168 NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript +
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700169 " was executed successfully with return value" + std::to_string(result) + ".");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800170 return;
171}
172
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700173} // namespace ndncert
174} // namespace ndn