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