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