Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
tylerliu | 1f2c3cf | 2021-11-15 15:43:02 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2024, Regents of the University of California. |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 4 | * |
| 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" |
Davide Pesavento | 9510c91 | 2024-02-25 17:50:05 -0500 | [diff] [blame] | 22 | |
| 23 | #include <ndn-cxx/util/logger.hpp> |
| 24 | |
Zhiyi Zhang | 523f0c2 | 2020-09-29 14:19:20 -0700 | [diff] [blame] | 25 | #include <regex> |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 26 | #include <boost/process.hpp> |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 27 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 28 | namespace ndncert { |
| 29 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 30 | NDN_LOG_INIT(ndncert.challenge.email); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 31 | NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "email"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 32 | |
| 33 | const std::string ChallengeEmail::NEED_CODE = "need-code"; |
| 34 | const std::string ChallengeEmail::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 35 | const std::string ChallengeEmail::PARAMETER_KEY_EMAIL = "email"; |
| 36 | const std::string ChallengeEmail::PARAMETER_KEY_CODE = "code"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 37 | |
| 38 | ChallengeEmail::ChallengeEmail(const std::string& scriptPath, |
| 39 | const size_t& maxAttemptTimes, |
| 40 | const time::seconds secretLifetime) |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 41 | : ChallengeModule("email", maxAttemptTimes, secretLifetime) |
| 42 | , m_sendEmailScript(scriptPath) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 46 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 47 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 48 | ChallengeEmail::handleChallengeRequest(const Block& params, ca::RequestState& request) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 49 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 50 | params.parse(); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 51 | auto currentTime = time::system_clock::now(); |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 52 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 53 | if (request.status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 54 | // for the first time, init the challenge |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 55 | std::string emailAddress = readString(params.get(tlv::ParameterValue)); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 56 | auto lastComponentRequested = readString(request.cert.getIdentity().get(-1)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 57 | if (lastComponentRequested != emailAddress) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 58 | NDN_LOG_TRACE("Email and requested name do not match: email=" << emailAddress |
| 59 | << " requested=" << lastComponentRequested); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 60 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | std::string emailCode = generateSecretCode(); |
| 62 | JsonSection secretJson; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 63 | secretJson.add(PARAMETER_KEY_CODE, emailCode); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 64 | // send out the email |
| 65 | sendEmail(emailAddress, emailCode, request); |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 66 | NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << emailCode); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 67 | return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), |
| 68 | m_maxAttemptTimes, m_secretLifetime); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 69 | } |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 70 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 71 | if (request.challengeState) { |
| 72 | if (request.challengeState->challengeStatus == NEED_CODE || |
| 73 | request.challengeState->challengeStatus == WRONG_CODE) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 74 | NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus); |
Davide Pesavento | 397faa9 | 2025-01-04 20:56:56 -0500 | [diff] [blame] | 75 | // the incoming interest should bring the verification code |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 76 | std::string givenCode = readString(params.get(tlv::ParameterValue)); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 77 | auto secret = request.challengeState->secrets; |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 78 | // check if run out of time |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 79 | if (currentTime - request.challengeState->timestamp >= m_secretLifetime) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 80 | NDN_LOG_TRACE("Secret expired"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 81 | return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired."); |
| 82 | } |
| 83 | // check if provided secret is correct |
| 84 | if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) { |
| 85 | // the code is correct |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 86 | NDN_LOG_TRACE("Secret is correct, challenge succeeded"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 87 | return returnWithSuccess(request); |
| 88 | } |
| 89 | // otherwise, check remaining attempt times |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 90 | if (request.challengeState->remainingTries > 1) { |
| 91 | auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp); |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 92 | NDN_LOG_TRACE("Wrong secret, remaining tries = " << request.challengeState->remainingTries - 1); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 93 | return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 94 | request.challengeState->remainingTries - 1, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 95 | time::duration_cast<time::seconds>(remainTime)); |
| 96 | } |
| 97 | else { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 98 | NDN_LOG_TRACE("Wrong secret, no tries remaining"); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 99 | return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 100 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 103 | |
| 104 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected challenge status."); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // For Client |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 108 | std::multimap<std::string, std::string> |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 109 | ChallengeEmail::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 110 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 111 | std::multimap<std::string, std::string> result; |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 112 | if (status == Status::BEFORE_CHALLENGE && challengeStatus.empty()) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 113 | result.emplace(PARAMETER_KEY_EMAIL, "Please input your email address"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 115 | else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 116 | result.emplace(PARAMETER_KEY_CODE, "Please input your verification code"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 118 | else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 119 | result.emplace(PARAMETER_KEY_CODE, "Incorrect code, please try again"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | } |
| 121 | else { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 122 | NDN_THROW(std::runtime_error("Unexpected challenge status")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 124 | return result; |
| 125 | } |
| 126 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 127 | Block |
Zhiyi Zhang | ba8e45e | 2020-11-03 08:55:32 -0800 | [diff] [blame] | 128 | ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 129 | const std::multimap<std::string, std::string>& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 130 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 131 | Block request(tlv::EncryptedPayload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 132 | if (status == Status::BEFORE_CHALLENGE) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 133 | if (params.size() != 1 || params.find(PARAMETER_KEY_EMAIL) == params.end()) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 134 | NDN_THROW(std::runtime_error("Wrong parameter provided")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 135 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 136 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
| 137 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_EMAIL)); |
| 138 | request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_EMAIL)->second)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 139 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 140 | else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 141 | if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 142 | NDN_THROW(std::runtime_error("Wrong parameter provided")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 143 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 144 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
| 145 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE)); |
| 146 | request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 147 | } |
| 148 | else { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 149 | NDN_THROW(std::runtime_error("Unexpected challenge status")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 150 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 151 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 152 | return request; |
| 153 | } |
| 154 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 155 | bool |
| 156 | ChallengeEmail::isValidEmailAddress(const std::string& emailAddress) |
| 157 | { |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 158 | const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_"; |
| 159 | static const std::regex emailPattern(pattern); |
| 160 | return std::regex_match(emailAddress, emailPattern); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 164 | ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret, |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 165 | const ca::RequestState& request) const |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 166 | { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 167 | std::string command = m_sendEmailScript; |
Zhiyi Zhang | ba8e45e | 2020-11-03 08:55:32 -0800 | [diff] [blame] | 168 | command += " \"" + emailAddress + "\" \"" + secret + "\" \"" + |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 169 | request.caPrefix.toUri() + "\" \"" + |
| 170 | request.cert.getName().toUri() + "\""; |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 171 | boost::process::child child(command); |
| 172 | child.wait(); |
| 173 | if (child.exit_code() != 0) { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 174 | NDN_LOG_ERROR("Email sending script " + m_sendEmailScript + " failed"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 175 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 176 | else { |
Davide Pesavento | 6866b90 | 2024-12-22 23:11:26 -0500 | [diff] [blame] | 177 | NDN_LOG_TRACE("Email sending script " + m_sendEmailScript + " succeeded"); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 178 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 181 | } // namespace ndncert |