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