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 | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 22 | #include "../ca-module.hpp" |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 23 | #include "../logging.hpp" |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 24 | #include <regex> |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
| 29 | _LOG_INIT(ndncert.ChallengeEmail); |
| 30 | |
| 31 | NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "Email"); |
| 32 | |
| 33 | const std::string ChallengeEmail::NEED_CODE = "need-code"; |
| 34 | const std::string ChallengeEmail::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 35 | const std::string ChallengeEmail::FAILURE_INVALID_EMAIL = "failure-invalid-email"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 36 | const std::string ChallengeEmail::JSON_EMAIL = "email"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 37 | const std::string ChallengeEmail::JSON_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 | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 42 | : ChallengeModule("Email") |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 43 | , m_sendEmailScript(scriptPath) |
| 44 | , m_maxAttemptTimes(maxAttemptTimes) |
| 45 | , m_secretLifetime(secretLifetime) |
| 46 | { |
| 47 | } |
| 48 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | // For CA |
| 50 | void |
| 51 | ChallengeEmail::handleChallengeRequest(const JsonSection& params, CertificateRequest& request) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 52 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 53 | 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 Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 61 | // 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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 71 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 85 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 86 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 106 | } |
| 107 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 108 | // 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 127 | else { |
| 128 | _LOG_ERROR("The challenge status is wrong"); |
| 129 | request.m_status = STATUS_FAILURE; |
| 130 | return; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 131 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // For Client |
| 135 | JsonSection |
| 136 | ChallengeEmail::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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 150 | } |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 155 | ChallengeEmail::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 156 | { |
| 157 | JsonSection result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 158 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 173 | return result; |
| 174 | } |
| 175 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 176 | bool |
| 177 | ChallengeEmail::isValidEmailAddress(const std::string& emailAddress) |
| 178 | { |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 179 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 185 | ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 186 | const CertificateRequest& request) const |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 187 | { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 188 | std::string command = m_sendEmailScript; |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 189 | command += " \"" + emailAddress + "\" \"" + secret + "\" \"" |
| 190 | + request.m_caName.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\""; |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 191 | int result = system(command.c_str()); |
| 192 | if (result == -1) { |
| 193 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 194 | } |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 195 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + |
| 196 | " was executed successfully with return value" + std::to_string(result) + "."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
| 200 | } // namespace ndncert |
| 201 | } // namespace ndn |