Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 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 "json-helper.hpp" |
| 24 | #include <ndn-cxx/util/random.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 29 | _LOG_INIT(ndncert.challenge-pin); |
| 30 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 31 | NDNCERT_REGISTER_CHALLENGE(ChallengePin, "PIN"); |
| 32 | |
| 33 | const std::string ChallengePin::NEED_CODE = "need-code"; |
| 34 | const std::string ChallengePin::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 35 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | const std::string ChallengePin::FAILURE_TIMEOUT = "failure-timeout"; |
| 37 | const std::string ChallengePin::FAILURE_MAXRETRY = "failure-max-retry"; |
| 38 | |
| 39 | const std::string ChallengePin::JSON_CODE_TP = "code-timepoint"; |
| 40 | const std::string ChallengePin::JSON_PIN_CODE = "code"; |
| 41 | const std::string ChallengePin::JSON_ATTEMPT_TIMES = "attempt-times"; |
| 42 | |
| 43 | ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime) |
| 44 | : ChallengeModule("PIN") |
| 45 | , m_secretLifetime(secretLifetime) |
| 46 | , m_maxAttemptTimes(maxAttemptTimes) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | JsonSection |
| 51 | ChallengePin::processSelectInterest(const Interest& interest, CertificateRequest& request) |
| 52 | { |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 53 | // interest format: /caName/CA/_SELECT/{"request-id":"id"}/PIN/<signature> |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 54 | request.setStatus(NEED_CODE); |
| 55 | request.setChallengeType(CHALLENGE_TYPE); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 56 | std::string secretCode = generateSecretCode(); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 57 | request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(), |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 58 | secretCode, |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 59 | m_maxAttemptTimes)); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 60 | _LOG_TRACE("Secret for request " << request.getRequestId() << " : " << secretCode); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 61 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE); |
| 62 | } |
| 63 | |
| 64 | JsonSection |
| 65 | ChallengePin::processValidateInterest(const Interest& interest, CertificateRequest& request) |
| 66 | { |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 67 | // interest format: /caName/CA/_VALIDATION/{"request-id":"id"}/PIN/{"code":"code"}/<signature> |
| 68 | JsonSection infoJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 69 | std::string givenCode = infoJson.get<std::string>(JSON_PIN_CODE); |
| 70 | |
| 71 | const auto parsedSecret = parseStoredSecrets(request.getChallengeSecrets()); |
| 72 | if (time::system_clock::now() - std::get<0>(parsedSecret) >= m_secretLifetime) { |
| 73 | // secret expires |
| 74 | request.setStatus(FAILURE_TIMEOUT); |
| 75 | request.setChallengeSecrets(JsonSection()); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 76 | return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_TIMEOUT); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 77 | } |
| 78 | else if (givenCode == std::get<1>(parsedSecret)) { |
| 79 | request.setStatus(SUCCESS); |
| 80 | request.setChallengeSecrets(JsonSection()); |
| 81 | Name downloadName = genDownloadName(request.getCaName(), request.getRequestId()); |
| 82 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS, downloadName); |
| 83 | } |
| 84 | else { |
| 85 | // check rest attempt times |
| 86 | if (std::get<2>(parsedSecret) > 1) { |
| 87 | int restAttemptTimes = std::get<2>(parsedSecret) - 1; |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 88 | request.setStatus(WRONG_CODE); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 89 | request.setChallengeSecrets(generateStoredSecrets(std::get<0>(parsedSecret), |
| 90 | std::get<1>(parsedSecret), |
| 91 | restAttemptTimes)); |
| 92 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, WRONG_CODE); |
| 93 | } |
| 94 | else { |
| 95 | // run out times |
| 96 | request.setStatus(FAILURE_MAXRETRY); |
| 97 | request.setChallengeSecrets(JsonSection()); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 98 | return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_MAXRETRY); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | std::list<std::string> |
| 104 | ChallengePin::getSelectRequirements() |
| 105 | { |
| 106 | std::list<std::string> result; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | std::list<std::string> |
| 111 | ChallengePin::getValidateRequirements(const std::string& status) |
| 112 | { |
| 113 | std::list<std::string> result; |
| 114 | if (status == NEED_CODE) { |
| 115 | result.push_back("Please input your verification code:"); |
| 116 | } |
| 117 | else if (status == WRONG_CODE) { |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 118 | result.push_back("Incorrect PIN code, please try again and input your verification code:"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 119 | } |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | JsonSection |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 124 | ChallengePin::doGenSelectParamsJson(const std::string& status, |
| 125 | const std::list<std::string>& paramList) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 126 | { |
| 127 | JsonSection result; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 128 | BOOST_ASSERT(status == WAIT_SELECTION); |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 129 | BOOST_ASSERT(paramList.size() == 0); |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | JsonSection |
| 134 | ChallengePin::doGenValidateParamsJson(const std::string& status, |
| 135 | const std::list<std::string>& paramList) |
| 136 | { |
| 137 | JsonSection result; |
| 138 | BOOST_ASSERT(paramList.size() == 1); |
| 139 | result.put(JSON_PIN_CODE, paramList.front()); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 140 | return result; |
| 141 | } |
| 142 | |
| 143 | std::tuple<time::system_clock::TimePoint, std::string, int> |
| 144 | ChallengePin::parseStoredSecrets(const JsonSection& storedSecrets) |
| 145 | { |
| 146 | auto tp = time::fromIsoString(storedSecrets.get<std::string>(JSON_CODE_TP)); |
| 147 | std::string rightCode= storedSecrets.get<std::string>(JSON_PIN_CODE); |
| 148 | int attemptTimes = std::stoi(storedSecrets.get<std::string>(JSON_ATTEMPT_TIMES)); |
| 149 | |
| 150 | return std::make_tuple(tp, rightCode, attemptTimes); |
| 151 | } |
| 152 | |
| 153 | JsonSection |
| 154 | ChallengePin::generateStoredSecrets(const time::system_clock::TimePoint& tp, |
| 155 | const std::string& secretCode, int attempTimes) |
| 156 | { |
| 157 | JsonSection json; |
| 158 | json.put(JSON_CODE_TP, time::toIsoString(tp)); |
| 159 | json.put(JSON_PIN_CODE, secretCode); |
| 160 | json.put(JSON_ATTEMPT_TIMES, std::to_string(attempTimes)); |
| 161 | return json; |
| 162 | } |
| 163 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 164 | } // namespace ndncert |
| 165 | } // namespace ndn |