Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, 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" |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 22 | |
| 23 | #include <regex> |
| 24 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 25 | #include "../ca-module.hpp" |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 26 | #include "../logging.hpp" |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 31 | _LOG_INIT(ndncert.challenge.email); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 32 | NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "email"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 33 | |
| 34 | const std::string ChallengeEmail::NEED_CODE = "need-code"; |
| 35 | const std::string ChallengeEmail::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 36 | const std::string ChallengeEmail::PARAMETER_KEY_EMAIL = "email"; |
| 37 | const std::string ChallengeEmail::PARAMETER_KEY_CODE = "code"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 38 | |
| 39 | ChallengeEmail::ChallengeEmail(const std::string& scriptPath, |
| 40 | const size_t& maxAttemptTimes, |
| 41 | const time::seconds secretLifetime) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 42 | : ChallengeModule("email") |
| 43 | , m_sendEmailScript(scriptPath) |
| 44 | , m_maxAttemptTimes(maxAttemptTimes) |
| 45 | , m_secretLifetime(secretLifetime) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 50 | std::tuple<ErrorCode, std::string> |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 51 | ChallengeEmail::handleChallengeRequest(const Block& params, CertificateRequest& request) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 52 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 53 | params.parse(); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 54 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 55 | if (request.m_status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | // for the first time, init the challenge |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 57 | std::string emailAddress = readString(params.get(tlv_parameter_value)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | if (!isValidEmailAddress(emailAddress)) { |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 59 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Invalid email address format."); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 61 | auto lastComponentRequested = readString(request.m_cert.getIdentity().get(-1)); |
| 62 | if (lastComponentRequested != emailAddress) { |
| 63 | _LOG_TRACE("Email and requested name do not match. Email " << emailAddress << "requested last component " << lastComponentRequested); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 64 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 65 | std::string emailCode = generateSecretCode(); |
| 66 | JsonSection secretJson; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 67 | secretJson.add(PARAMETER_KEY_CODE, emailCode); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 68 | // send out the email |
| 69 | sendEmail(emailAddress, emailCode, request); |
| 70 | _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 71 | return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime.count()); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 72 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 73 | |
| 74 | if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus); |
| 76 | // the incoming interest should bring the pin code |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 77 | std::string givenCode = readString(params.get(tlv_parameter_value)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 78 | auto secret = request.m_challengeSecrets; |
| 79 | // check if run out of time |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 80 | if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) { |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 81 | return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired."); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 82 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 83 | // check if provided secret is correct |
| 84 | if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | // the code is correct |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 86 | _LOG_TRACE("Correct secret code. Challenge succeeded."); |
| 87 | return returnWithSuccess(request); |
| 88 | } |
| 89 | // otherwise, check remaining attempt times |
| 90 | if (request.m_remainingTries > 1) { |
| 91 | auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp)); |
| 92 | _LOG_TRACE("Wrong secret code provided. Remaining Tries - 1."); |
| 93 | return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), request.m_remainingTries - 1, remainTime.count()); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 94 | } |
| 95 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 96 | // run out times |
| 97 | _LOG_TRACE("Wrong secret code provided. Ran out tires. Challenge failed."); |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 98 | return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 101 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 105 | std::vector<std::tuple<std::string, std::string>> |
| 106 | ChallengeEmail::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 107 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 108 | std::vector<std::tuple<std::string, std::string>> result; |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 109 | if (status == Status::BEFORE_CHALLENGE && challengeStatus == "") { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 110 | result.push_back(std::make_tuple(PARAMETER_KEY_EMAIL, "Please input your email address")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 111 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 112 | else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 113 | result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your verification code")); |
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 == WRONG_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 116 | result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect code, please try again")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | } |
| 118 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 119 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 121 | return result; |
| 122 | } |
| 123 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 124 | Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 125 | ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 126 | { |
| 127 | Block request = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 128 | if (status == Status::BEFORE_CHALLENGE) { |
| 129 | if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_EMAIL) { |
| 130 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 131 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 132 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 133 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_EMAIL)); |
| 134 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0]))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 135 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 136 | else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) { |
| 137 | if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) { |
| 138 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 139 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 140 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 141 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE)); |
| 142 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0]))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 143 | } |
| 144 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 145 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 146 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 147 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 148 | return request; |
| 149 | } |
| 150 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 151 | bool |
| 152 | ChallengeEmail::isValidEmailAddress(const std::string& emailAddress) |
| 153 | { |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 154 | const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_"; |
| 155 | static const std::regex emailPattern(pattern); |
| 156 | return std::regex_match(emailAddress, emailPattern); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 160 | ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | const CertificateRequest& request) const |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 162 | { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 163 | std::string command = m_sendEmailScript; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 164 | command += " \"" + emailAddress + "\" \"" + secret + "\" \"" + request.m_caPrefix.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\""; |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 165 | int result = system(command.c_str()); |
| 166 | if (result == -1) { |
| 167 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 168 | } |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 169 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + |
| 170 | " was executed successfully with return value" + std::to_string(result) + "."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 174 | } // namespace ndncert |
| 175 | } // namespace ndn |