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 | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, 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 | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 22 | #include <ndn-cxx/util/random.hpp> |
| 23 | |
| 24 | namespace ndn { |
| 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) |
Zhiyi Zhang | ead9f00 | 2020-10-03 15:42:52 -0700 | [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> |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 41 | ChallengePin::handleChallengeRequest(const Block& params, CaState& 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(); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 45 | if (request.m_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); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame^] | 51 | NDN_LOG_TRACE("Secret for request " << request.m_requestId << " : " << secretCode); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 52 | return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 53 | } |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 54 | if (request.m_challengeState) { |
| 55 | if (request.m_challengeState->m_challengeStatus == NEED_CODE || |
| 56 | request.m_challengeState->m_challengeStatus == WRONG_CODE) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame^] | 57 | NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeState->m_challengeStatus); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 58 | // the incoming interest should bring the pin code |
| 59 | std::string givenCode = readString(params.get(tlv_parameter_value)); |
| 60 | auto secret = request.m_challengeState->m_secrets; |
| 61 | if (currentTime - request.m_challengeState->m_timestamp >= m_secretLifetime) { |
| 62 | return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired."); |
| 63 | } |
| 64 | if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) { |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame^] | 65 | NDN_LOG_TRACE("Correct PIN code. Challenge succeeded."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 66 | return returnWithSuccess(request); |
| 67 | } |
| 68 | // check rest attempt times |
| 69 | if (request.m_challengeState->m_remainingTries > 1) { |
| 70 | auto remainTime = m_secretLifetime - (currentTime - request.m_challengeState->m_timestamp); |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame^] | 71 | NDN_LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 72 | return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), |
| 73 | request.m_challengeState->m_remainingTries - 1, |
| 74 | time::duration_cast<time::seconds>(remainTime)); |
| 75 | } |
| 76 | else { |
| 77 | // run out times |
Zhiyi Zhang | d61b4a8 | 2020-10-10 15:18:43 -0700 | [diff] [blame^] | 78 | NDN_LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed."); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 79 | return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires."); |
| 80 | } |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
Zhiyi Zhang | aafc55e | 2020-09-28 17:54:48 -0700 | [diff] [blame] | 83 | return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 87 | std::vector<std::tuple<std::string, std::string>> |
| 88 | ChallengePin::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 90 | std::vector<std::tuple<std::string, std::string>> result; |
| 91 | if (status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 92 | // do nothing |
| 93 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 94 | else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 95 | result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your PIN code")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 97 | else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 98 | result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 99 | } |
| 100 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 101 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 102 | } |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 103 | return result; |
| 104 | } |
| 105 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 106 | Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 107 | ChallengePin::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 108 | { |
| 109 | Block request = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 110 | if (status == Status::BEFORE_CHALLENGE) { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 111 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
| 112 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 113 | else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) { |
| 114 | if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) { |
| 115 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 116 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 117 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 118 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE)); |
| 119 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0]))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 120 | } |
| 121 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 122 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 123 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 124 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 125 | return request; |
| 126 | } |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 127 | } // namespace ndncert |
| 128 | } // namespace ndn |