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 | 4604983 | 2020-09-28 17:08:12 -0700 | [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 | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 25 | #include "logging.hpp" |
| 26 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace ndncert { |
| 29 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 30 | _LOG_INIT(ndncert.challenge.pin); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 31 | NDNCERT_REGISTER_CHALLENGE(ChallengePin, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 32 | |
| 33 | const std::string ChallengePin::NEED_CODE = "need-code"; |
| 34 | const std::string ChallengePin::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 35 | const std::string ChallengePin::PARAMETER_KEY_CODE = "pin-code"; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | |
| 37 | ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime) |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 38 | : ChallengeModule("pin") |
| 39 | , m_secretLifetime(secretLifetime) |
| 40 | , m_maxAttemptTimes(maxAttemptTimes) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | // For CA |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 45 | std::tuple<Error, std::string> |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 46 | ChallengePin::handleChallengeRequest(const Block& params, CertificateRequest& request) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 47 | { |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 48 | params.parse(); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 49 | auto currentTime = time::system_clock::now(); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 50 | if (request.m_status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 51 | _LOG_TRACE("Challenge Interest arrives. Init the challenge"); |
| 52 | // for the first time, init the challenge |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 53 | std::string secretCode = generateSecretCode(); |
| 54 | JsonSection secretJson; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 55 | secretJson.add(PARAMETER_KEY_CODE, secretCode); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | _LOG_TRACE("Secret for request " << request.m_requestId << " : " << secretCode); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 57 | return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime.count()); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 58 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 59 | |
| 60 | if (request.m_challengeStatus == NEED_CODE || request.m_challengeStatus == WRONG_CODE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeStatus); |
| 62 | // the incoming interest should bring the pin code |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 63 | std::string givenCode = readString(params.get(tlv_parameter_value)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 64 | auto secret = request.m_challengeSecrets; |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 65 | if (currentTime - time::fromIsoString(request.m_challengeTp) >= m_secretLifetime) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 66 | return returnWithError(request, Error::OUT_OF_TIME, "Secret expired."); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 67 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 68 | if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) { |
| 69 | _LOG_TRACE("Correct PIN code. Challenge succeeded."); |
| 70 | return returnWithSuccess(request); |
| 71 | } |
| 72 | // check rest attempt times |
| 73 | if (request.m_remainingTries > 1) { |
| 74 | auto remainTime = m_secretLifetime - (currentTime - time::fromIsoString(request.m_challengeTp)); |
| 75 | _LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1."); |
| 76 | return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret), request.m_remainingTries - 1, remainTime.count()); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 77 | } |
| 78 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 79 | // run out times |
| 80 | _LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed."); |
| 81 | return returnWithError(request, Error::OUT_OF_TRIES, "Ran out tires."); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 84 | return returnWithError(request, Error::INVALID_PARAMETER, "Unexpected status or challenge status"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // For Client |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 88 | std::vector<std::tuple<std::string, std::string>> |
| 89 | ChallengePin::getRequestedParameterList(Status status, const std::string& challengeStatus) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 90 | { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 91 | std::vector<std::tuple<std::string, std::string>> result; |
| 92 | if (status == Status::BEFORE_CHALLENGE) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 93 | // do nothing |
| 94 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 95 | else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 96 | 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] | 97 | } |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 98 | else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 99 | 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] | 100 | } |
| 101 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 102 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 103 | } |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 104 | return result; |
| 105 | } |
| 106 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 107 | Block |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 108 | 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] | 109 | { |
| 110 | Block request = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 111 | if (status == Status::BEFORE_CHALLENGE) { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 112 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
| 113 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 114 | else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) { |
| 115 | if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) { |
| 116 | BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided.")); |
| 117 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 118 | request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 119 | request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE)); |
| 120 | request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0]))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 121 | } |
| 122 | else { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 123 | BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 124 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 125 | request.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 126 | return request; |
| 127 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 128 | } // namespace ndncert |
| 129 | } // namespace ndn |