Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -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 | 65ba932 | 2017-01-19 14:15:03 -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-pin.hpp" |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 22 | #include "logging.hpp" |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/random.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 28 | _LOG_INIT(ndncert.challenge-pin); |
| 29 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 30 | NDNCERT_REGISTER_CHALLENGE(ChallengePin, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 31 | |
| 32 | const std::string ChallengePin::NEED_CODE = "need-code"; |
| 33 | const std::string ChallengePin::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 34 | const std::string ChallengePin::JSON_PIN_CODE = "pin-code"; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 35 | |
| 36 | ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime) |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 37 | : ChallengeModule("pin") |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 38 | , m_secretLifetime(secretLifetime) |
| 39 | , m_maxAttemptTimes(maxAttemptTimes) |
| 40 | { |
| 41 | } |
| 42 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 43 | // For CA |
| 44 | void |
| 45 | ChallengePin::handleChallengeRequest(const JsonSection& params, CertificateRequest& request) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 46 | { |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 47 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | if (request.m_challengeStatus == "") { |
| 49 | _LOG_TRACE("Challenge Interest arrives. Init the challenge"); |
| 50 | // for the first time, init the challenge |
| 51 | request.m_status = STATUS_CHALLENGE; |
| 52 | request.m_challengeStatus = NEED_CODE; |
| 53 | request.m_challengeType = CHALLENGE_TYPE; |
| 54 | std::string secretCode = generateSecretCode(); |
| 55 | JsonSection secretJson; |
| 56 | secretJson.add(JSON_PIN_CODE, secretCode); |
| 57 | request.m_challengeSecrets = secretJson; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 58 | request.m_challengeTp = time::toIsoString(currentTime); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | request.m_remainingTime = m_secretLifetime.count(); |
| 60 | request.m_remainingTries = m_maxAttemptTimes; |
| 61 | _LOG_TRACE("Secret for request " << request.m_requestId << " : " << secretCode); |
| 62 | return; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 63 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 64 | else if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) { |
| 65 | _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus); |
| 66 | // the incoming interest should bring the pin code |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 67 | std::string givenCode = params.get(JSON_PIN_CODE, ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 68 | const auto realCode = request.m_challengeSecrets.get<std::string>(JSON_PIN_CODE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 69 | if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 70 | // secret expires |
| 71 | request.m_status = STATUS_FAILURE; |
| 72 | request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_TIMEOUT; |
| 73 | updateRequestOnChallengeEnd(request); |
| 74 | _LOG_TRACE("Secret expired. Challenge failed."); |
| 75 | return; |
| 76 | } |
| 77 | else if (givenCode == realCode) { |
| 78 | // the code is correct |
| 79 | request.m_status = STATUS_PENDING; |
| 80 | request.m_challengeStatus = CHALLENGE_STATUS_SUCCESS; |
| 81 | updateRequestOnChallengeEnd(request); |
| 82 | _LOG_TRACE("PIN code matched. Challenge succeeded."); |
| 83 | return; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 84 | } |
| 85 | else { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 86 | // check rest attempt times |
| 87 | if (request.m_remainingTries > 1) { |
| 88 | request.m_challengeStatus = WRONG_CODE; |
| 89 | request.m_remainingTries = request.m_remainingTries - 1; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 90 | auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 91 | request.m_remainingTime = remainTime.count(); |
| 92 | _LOG_TRACE("PIN code didn't match. Remaining Tries - 1."); |
| 93 | return; |
| 94 | } |
| 95 | else { |
| 96 | // run out times |
| 97 | request.m_status = STATUS_FAILURE; |
| 98 | request.m_challengeStatus = CHALLENGE_STATUS_FAILURE_MAXRETRY; |
| 99 | updateRequestOnChallengeEnd(request); |
| 100 | _LOG_TRACE("PIN code didn't match. Ran out tires. Challenge failed."); |
| 101 | return; |
| 102 | } |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 105 | else { |
| 106 | _LOG_ERROR("The challenge status is wrong"); |
| 107 | request.m_status = STATUS_FAILURE; |
| 108 | return; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 109 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // For Client |
| 113 | JsonSection |
| 114 | ChallengePin::getRequirementForChallenge(int status, const std::string& challengeStatus) |
| 115 | { |
| 116 | JsonSection result; |
| 117 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 118 | // do nothing |
| 119 | } |
| 120 | else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) { |
| 121 | result.put(JSON_PIN_CODE, "Please_input_your_verification_code"); |
| 122 | } |
| 123 | else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) { |
| 124 | result.put(JSON_PIN_CODE, "Incorrect_PIN_code_please_try_again"); |
| 125 | } |
| 126 | else { |
| 127 | _LOG_ERROR("Client's status and challenge status are wrong"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | JsonSection |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 133 | ChallengePin::genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 134 | { |
| 135 | JsonSection result; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 136 | if (status == STATUS_BEFORE_CHALLENGE && challengeStatus == "") { |
| 137 | // do nothing |
| 138 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
| 139 | } |
| 140 | else if (status == STATUS_CHALLENGE && challengeStatus == NEED_CODE) { |
| 141 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 142 | result.put(JSON_PIN_CODE, params.get(JSON_PIN_CODE, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | } |
| 144 | else if (status == STATUS_CHALLENGE && challengeStatus == WRONG_CODE) { |
| 145 | result.put(JSON_CLIENT_SELECTED_CHALLENGE, CHALLENGE_TYPE); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 146 | result.put(JSON_PIN_CODE, params.get(JSON_PIN_CODE, "")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 147 | } |
| 148 | else { |
| 149 | _LOG_ERROR("Client's status and challenge status are wrong"); |
| 150 | } |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 151 | return result; |
| 152 | } |
| 153 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 154 | } // namespace ndncert |
| 155 | } // namespace ndn |