blob: c9f98bacaa7d15e6cb93b0618b89b52b442f88fd [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08004 *
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 Zhang65ba9322017-01-19 14:15:03 -080022#include <ndn-cxx/util/random.hpp>
23
24namespace ndn {
25namespace ndncert {
26
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070027NDN_LOG_INIT(ndncert.challenge.pin);
Zhiyi Zhang36706832019-07-04 21:33:03 -070028NDNCERT_REGISTER_CHALLENGE(ChallengePin, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080029
30const std::string ChallengePin::NEED_CODE = "need-code";
31const std::string ChallengePin::WRONG_CODE = "wrong-code";
Zhiyi Zhangead9f002020-10-03 15:42:52 -070032const std::string ChallengePin::PARAMETER_KEY_CODE = "code";
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080033
34ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime)
Zhiyi Zhangead9f002020-10-03 15:42:52 -070035 : ChallengeModule("pin", maxAttemptTimes, secretLifetime)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036{
37}
38
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070039// For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070040std::tuple<ErrorCode, std::string>
tylerliu8704d032020-06-23 10:18:15 -070041ChallengePin::handleChallengeRequest(const Block& params, CaState& request)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080042{
Suyong Won44d0cce2020-05-10 04:07:43 -070043 params.parse();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080044 auto currentTime = time::system_clock::now();
Zhiyi Zhang46049832020-09-28 17:08:12 -070045 if (request.m_status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070046 NDN_LOG_TRACE("Challenge Interest arrives. Init the challenge");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070047 // for the first time, init the challenge
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 std::string secretCode = generateSecretCode();
49 JsonSection secretJson;
Zhiyi Zhang46049832020-09-28 17:08:12 -070050 secretJson.add(PARAMETER_KEY_CODE, secretCode);
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070051 NDN_LOG_TRACE("Secret for request " << toHex(request.m_requestId.data(), request.m_requestId.size()) << " : " << secretCode);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070052 return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080053 }
Zhiyi Zhanga749f442020-09-29 17:19:51 -070054 if (request.m_challengeState) {
55 if (request.m_challengeState->m_challengeStatus == NEED_CODE ||
56 request.m_challengeState->m_challengeStatus == WRONG_CODE) {
Zhiyi Zhangd61b4a82020-10-10 15:18:43 -070057 NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeState->m_challengeStatus);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070058 // the incoming interest should bring the pin code
tylerliu50d679e2020-10-14 14:08:39 -070059 std::string givenCode = readString(params.get(tlv::ParameterValue));
Zhiyi Zhanga749f442020-09-29 17:19:51 -070060 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 Zhangd61b4a82020-10-10 15:18:43 -070065 NDN_LOG_TRACE("Correct PIN code. Challenge succeeded.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070066 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 Zhangd61b4a82020-10-10 15:18:43 -070071 NDN_LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070072 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 Zhangd61b4a82020-10-10 15:18:43 -070078 NDN_LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
Zhiyi Zhanga749f442020-09-29 17:19:51 -070079 return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
80 }
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080081 }
82 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070083 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084}
85
86// For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070087std::vector<std::tuple<std::string, std::string>>
88ChallengePin::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089{
Zhiyi Zhang46049832020-09-28 17:08:12 -070090 std::vector<std::tuple<std::string, std::string>> result;
91 if (status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092 // do nothing
93 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -070094 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -070095 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your PIN code"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -070097 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -070098 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099 }
100 else {
tylerliu41c11532020-10-10 16:14:45 -0700101 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102 }
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700103 return result;
104}
105
Suyong Won19fba4d2020-05-09 13:39:46 -0700106Block
Zhiyi Zhang46049832020-09-28 17:08:12 -0700107ChallengePin::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700108{
tylerliu50d679e2020-10-14 14:08:39 -0700109 Block request = makeEmptyBlock(tlv::EncryptedPayload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700110 if (status == Status::BEFORE_CHALLENGE) {
tylerliu50d679e2020-10-14 14:08:39 -0700111 request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
Suyong Won19fba4d2020-05-09 13:39:46 -0700112 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700113 else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
114 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) {
tylerliu41c11532020-10-10 16:14:45 -0700115 NDN_THROW(std::runtime_error("Wrong parameter provided."));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700116 }
tylerliu50d679e2020-10-14 14:08:39 -0700117 request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
118 request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
119 request.push_back(makeStringBlock(tlv::ParameterValue, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700120 }
121 else {
tylerliu41c11532020-10-10 16:14:45 -0700122 NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700123 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700124 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700125 return request;
126}
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700127} // namespace ndncert
128} // namespace ndn