Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2017-2022, 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" |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 22 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/util/random.hpp> |
| 24 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 25 | namespace ndncert { |
| 26 | |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 27 | NDN_LOG_INIT(ndncert.challenge.pin); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 28 | NDNCERT_REGISTER_CHALLENGE(ChallengePin, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 29 | |
| 30 | const std::string ChallengePin::NEED_CODE = "need-code"; |
| 31 | const std::string ChallengePin::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [diff] [blame] | 32 | const std::string ChallengePin::PARAMETER_KEY_CODE = "code"; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 33 | |
| 34 | ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime) |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 35 | : ChallengeModule("pin", maxAttemptTimes, secretLifetime) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 39 | // For CA |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 40 | std::tuple<ErrorCode, std::string> |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 41 | ChallengePin::handleChallengeRequest(const Block& params, ca::RequestState& request) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 42 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 43 | params.parse(); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 44 | auto currentTime = time::system_clock::now(); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 45 | if (request.status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 46 | NDN_LOG_TRACE("Challenge Interest arrives. Init the challenge"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 47 | // for the first time, init the challenge |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | std::string secretCode = generateSecretCode(); |
| 49 | JsonSection secretJson; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 50 | secretJson.add(PARAMETER_KEY_CODE, secretCode); |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame^] | 51 | NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 52 | << " : " << secretCode); |
| 53 | return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, |
| 54 | m_secretLifetime); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 55 | } |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 56 | if (request.challengeState) { |
| 57 | if (request.challengeState->challengeStatus == NEED_CODE || |
| 58 | request.challengeState->challengeStatus == WRONG_CODE) { |
| 59 | NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 60 | // the incoming interest should bring the pin code |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 61 | std::string givenCode = readString(params.get(tlv::ParameterValue)); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 62 | auto secret = request.challengeState->secrets; |
| 63 | if (currentTime - request.challengeState->timestamp >= m_secretLifetime) { |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 64 | return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired."); |
| 65 | } |
| 66 | if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 67 | NDN_LOG_TRACE("Correct PIN code. Challenge succeeded."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 68 | return returnWithSuccess(request); |
| 69 | } |
| 70 | // check rest attempt times |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 71 | if (request.challengeState->remainingTries > 1) { |
| 72 | auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 73 | NDN_LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 74 | return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 75 | request.challengeState->remainingTries - 1, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 76 | time::duration_cast<time::seconds>(remainTime)); |
| 77 | } |
| 78 | else { |
| 79 | // run out times |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame] | 80 | NDN_LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 81 | return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires."); |
| 82 | } |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 85 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // For Client |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 89 | std::multimap<std::string, std::string> |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 90 | ChallengePin::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 91 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 92 | std::multimap<std::string, std::string> result; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 93 | if (status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 94 | // do nothing |
| 95 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 96 | else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 97 | result.emplace(PARAMETER_KEY_CODE, "Please input your PIN code"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 98 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 99 | else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 100 | result.emplace(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | } |
| 102 | else { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 103 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 104 | } |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 105 | return result; |
| 106 | } |
| 107 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 108 | Block |
tylerliu | 6563f93 | 2020-10-30 11:13:38 -0700 | [diff] [blame] | 109 | ChallengePin::genChallengeRequestTLV(Status status, const std::string& challengeStatus, |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 110 | const std::multimap<std::string, std::string>& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 111 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 112 | Block request(tlv::EncryptedPayload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 113 | if (status == Status::BEFORE_CHALLENGE) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 114 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 115 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 116 | else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 117 | if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 118 | NDN_THROW(std::runtime_error("Wrong parameter provided.")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 119 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 120 | request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE)); |
| 121 | request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE)); |
| 122 | request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 123 | } |
| 124 | else { |
tylerliu | 41c1153 | 2020-10-10 16:14:45 -0700 | [diff] [blame] | 125 | NDN_THROW(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 126 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 127 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 128 | return request; |
| 129 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 130 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 131 | } // namespace ndncert |