blob: aec785946986074c9ae47530225f5c6ee82e58bd [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 Zhang523f0c22020-09-29 14:19:20 -070023#include <regex>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080024
25namespace ndn {
26namespace ndncert {
27
Zhiyi Zhang46049832020-09-28 17:08:12 -070028_LOG_INIT(ndncert.challenge.email);
Zhiyi Zhang36706832019-07-04 21:33:03 -070029NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080030
31const std::string ChallengeEmail::NEED_CODE = "need-code";
32const std::string ChallengeEmail::WRONG_CODE = "wrong-code";
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 Zhang46049832020-09-28 17:08:12 -070039 : ChallengeModule("email")
40 , m_sendEmailScript(scriptPath)
41 , m_maxAttemptTimes(maxAttemptTimes)
42 , m_secretLifetime(secretLifetime)
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>
Suyong Won19fba4d2020-05-09 13:39:46 -070048ChallengeEmail::handleChallengeRequest(const Block& params, CertificateRequest& 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();
Zhiyi Zhang46049832020-09-28 17:08:12 -070052 if (request.m_status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070053 // for the first time, init the challenge
Suyong Won19fba4d2020-05-09 13:39:46 -070054 std::string emailAddress = readString(params.get(tlv_parameter_value));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 if (!isValidEmailAddress(emailAddress)) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070056 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Invalid email address format.");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057 }
Zhiyi Zhang46049832020-09-28 17:08:12 -070058 auto lastComponentRequested = readString(request.m_cert.getIdentity().get(-1));
59 if (lastComponentRequested != emailAddress) {
60 _LOG_TRACE("Email and requested name do not match. Email " << emailAddress << "requested last component " << lastComponentRequested);
Zhiyi Zhang5f749a22019-06-12 17:02:33 -070061 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 std::string emailCode = generateSecretCode();
63 JsonSection secretJson;
Zhiyi Zhang46049832020-09-28 17:08:12 -070064 secretJson.add(PARAMETER_KEY_CODE, emailCode);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 // send out the email
66 sendEmail(emailAddress, emailCode, request);
67 _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode);
Zhiyi Zhang46049832020-09-28 17:08:12 -070068 return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime.count());
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080069 }
Zhiyi Zhang46049832020-09-28 17:08:12 -070070
71 if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072 _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus);
73 // the incoming interest should bring the pin code
Suyong Won19fba4d2020-05-09 13:39:46 -070074 std::string givenCode = readString(params.get(tlv_parameter_value));
Zhiyi Zhang46049832020-09-28 17:08:12 -070075 auto secret = request.m_challengeSecrets;
76 // check if run out of time
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080077 if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) {
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070078 return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070079 }
Zhiyi Zhang46049832020-09-28 17:08:12 -070080 // check if provided secret is correct
81 if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082 // the code is correct
Zhiyi Zhang46049832020-09-28 17:08:12 -070083 _LOG_TRACE("Correct secret code. Challenge succeeded.");
84 return returnWithSuccess(request);
85 }
86 // otherwise, check remaining attempt times
87 if (request.m_remainingTries > 1) {
88 auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp));
89 _LOG_TRACE("Wrong secret code provided. Remaining Tries - 1.");
90 return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), request.m_remainingTries - 1, remainTime.count());
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080091 }
92 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -070093 // run out times
94 _LOG_TRACE("Wrong secret code provided. Ran out tires. Challenge failed.");
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070095 return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080096 }
97 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070098 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099}
100
101// For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -0700102std::vector<std::tuple<std::string, std::string>>
103ChallengeEmail::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700104{
Zhiyi Zhang46049832020-09-28 17:08:12 -0700105 std::vector<std::tuple<std::string, std::string>> result;
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700106 if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700107 result.push_back(std::make_tuple(PARAMETER_KEY_EMAIL, "Please input your email address"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700109 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700110 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your verification code"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700112 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700113 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect code, please try again"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700114 }
115 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700116 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700117 }
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800118 return result;
119}
120
Suyong Won19fba4d2020-05-09 13:39:46 -0700121Block
Zhiyi Zhang46049832020-09-28 17:08:12 -0700122ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700123{
124 Block request = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700125 if (status == Status::BEFORE_CHALLENGE) {
126 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_EMAIL) {
127 BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided."));
128 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700129 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700130 request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_EMAIL));
131 request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700132 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700133 else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
134 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) {
135 BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided."));
136 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700137 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700138 request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE));
139 request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700140 }
141 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700142 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700143 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700144 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700145 return request;
146}
147
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800148bool
149ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
150{
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700151 const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
152 static const std::regex emailPattern(pattern);
153 return std::regex_match(emailAddress, emailPattern);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800154}
155
156void
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700157ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700158 const CertificateRequest& request) const
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800159{
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700160 std::string command = m_sendEmailScript;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700161 command += " \"" + emailAddress + "\" \"" + secret + "\" \"" + request.m_caPrefix.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\"";
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700162 int result = system(command.c_str());
163 if (result == -1) {
164 _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800165 }
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700166 _LOG_TRACE("EmailSending Script " + m_sendEmailScript +
167 " was executed successfully with return value" + std::to_string(result) + ".");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800168 return;
169}
170
Zhiyi Zhang46049832020-09-28 17:08:12 -0700171} // namespace ndncert
172} // namespace ndn