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 | |
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 | 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 | 3670683 | 2019-07-04 21:33:03 -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 | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 53 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 54 | if (request.m_challengeStatus == "") { |
| 55 | // for the first time, init the challenge |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 56 | std::string emailAddress = params.get(JSON_EMAIL, ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 57 | if (!isValidEmailAddress(emailAddress)) { |
| 58 | request.m_status = STATUS_FAILURE; |
| 59 | request.m_challengeStatus = FAILURE_INVALID_EMAIL; |
| 60 | return; |
| 61 | } |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 62 | // check whether this email is the same as the one used in PROBE |
| 63 | if (request.m_probeToken != nullptr) { |
| 64 | const auto& content = request.m_probeToken->getContent(); |
| 65 | const auto& json = CaModule::jsonFromBlock(content); |
| 66 | const auto& expectedEmail = json.get("email", ""); |
| 67 | Name expectedPrefix(json.get(JSON_CA_NAME, "")); |
| 68 | if (expectedEmail != emailAddress || !expectedPrefix.isPrefixOf(request.m_cert.getName())) { |
Zhiyi Zhang | 42e1cf3 | 2019-06-22 17:11:42 -0700 | [diff] [blame] | 69 | _LOG_ERROR("Cannot match with the PROBE token. Input email: " << emailAddress |
| 70 | << " Email in Token: " << expectedEmail |
| 71 | << " Requested Cert Name: " << request.m_cert.getName() |
| 72 | << " Identity Name got from Token: " << expectedPrefix); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 76 | request.m_status = STATUS_CHALLENGE; |
| 77 | request.m_challengeStatus = NEED_CODE; |
| 78 | request.m_challengeType = CHALLENGE_TYPE; |
| 79 | std::string emailCode = generateSecretCode(); |
| 80 | JsonSection secretJson; |
| 81 | secretJson.add(JSON_CODE, emailCode); |
| 82 | request.m_challengeSecrets = secretJson; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 83 | request.m_challengeTp = time::toIsoString(currentTime); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 84 | request.m_remainingTime = m_secretLifetime.count(); |
| 85 | request.m_remainingTries = m_maxAttemptTimes; |
| 86 | // send out the email |
| 87 | sendEmail(emailAddress, emailCode, request); |
| 88 | _LOG_TRACE("Secret for request " << request.m_requestId << " : " << emailCode); |
| 89 | return; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 90 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 91 | else if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) { |
| 92 | _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus); |
| 93 | // the incoming interest should bring the pin code |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 94 | std::string givenCode = params.get(JSON_CODE, ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 95 | const auto realCode = request.m_challengeSecrets.get<std::string>(JSON_CODE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 96 | if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 97 | // secret expires |
| 98 | request.m_status = STATUS_FAILURE; |
| 99 | request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_TIMEOUT; |
| 100 | updateRequestOnChallengeEnd(request); |
| 101 | _LOG_TRACE("Secret expired. Challenge failed."); |
| 102 | return; |
| 103 | } |
| 104 | else if (givenCode == realCode) { |
| 105 | // the code is correct |
| 106 | request.m_status = STATUS_PENDING; |
| 107 | request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS; |
| 108 | updateRequestOnChallengeEnd(request); |
| 109 | _LOG_TRACE("Secret code matched. Challenge succeeded."); |
| 110 | return; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 111 | } |
| 112 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 113 | // check rest attempt times |
| 114 | if (request.m_remainingTries > 1) { |
| 115 | request.m_challengeStatus = WRONG_CODE; |
| 116 | request.m_remainingTries = request.m_remainingTries - 1; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 117 | auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 118 | request.m_remainingTime = remainTime.count(); |
| 119 | _LOG_TRACE("Secret code didn't match. Remaining Tries - 1."); |
| 120 | return; |
| 121 | } |
| 122 | else { |
| 123 | // run out times |
| 124 | request.m_status = STATUS_FAILURE; |
| 125 | request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_MAXRETRY; |
| 126 | updateRequestOnChallengeEnd(request); |
| 127 | _LOG_TRACE("Secret code didn't match. Ran out tires. Challenge failed."); |
| 128 | return; |
| 129 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | else { |
| 133 | _LOG_ERROR("The challenge status is wrong"); |
| 134 | request.m_status = STATUS_FAILURE; |
| 135 | return; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 136 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // For Client |
| 140 | JsonSection |
| 141 | ChallengeEmail::getRequirementForChallenge(int status, const std::string& challengeStatus) |
| 142 | { |
| 143 | JsonSection result; |
| 144 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 145 | result.put(JSON_EMAIL, "Please_input_your_email_address"); |
| 146 | } |
| 147 | else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) { |
| 148 | result.put(JSON_CODE, "Please_input_your_verification_code"); |
| 149 | } |
| 150 | else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) { |
| 151 | result.put(JSON_CODE, "Incorrect_code_please_try_again"); |
| 152 | } |
| 153 | else { |
| 154 | _LOG_ERROR("CA's status and challenge status are wrong"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 155 | } |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 160 | ChallengeEmail::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 161 | { |
| 162 | JsonSection result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 163 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 164 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 165 | result.put(JSON_EMAIL, params.get(JSON_EMAIL, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 166 | } |
| 167 | else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) { |
| 168 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 169 | result.put(JSON_CODE, params.get(JSON_CODE, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 170 | } |
| 171 | else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) { |
| 172 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 173 | result.put(JSON_CODE, params.get(JSON_CODE, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 174 | } |
| 175 | else { |
| 176 | _LOG_ERROR("Client's status and challenge status are wrong"); |
| 177 | } |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 178 | return result; |
| 179 | } |
| 180 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 181 | bool |
| 182 | ChallengeEmail::isValidEmailAddress(const std::string& emailAddress) |
| 183 | { |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 184 | const std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_"; |
| 185 | static const std::regex emailPattern(pattern); |
| 186 | return std::regex_match(emailAddress, emailPattern); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 190 | ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 191 | const CertificateRequest& request) const |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 192 | { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 193 | std::string command = m_sendEmailScript; |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 194 | command += " \"" + emailAddress + "\" \"" + secret + "\" \"" |
| 195 | + request.m_caName.toUri() + "\" \"" + request.m_cert.getName().toUri() + "\""; |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 196 | int result = system(command.c_str()); |
| 197 | if (result == -1) { |
| 198 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 199 | } |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 200 | _LOG_TRACE("EmailSending Script " + m_sendEmailScript + |
| 201 | " was executed successfully with return value" + std::to_string(result) + "."); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | } // namespace ndncert |
| 206 | } // namespace ndn |