blob: 4853eede9254aa1faf96a9f8059167fccc970c52 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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 Zhanga749f442020-09-29 17:19:51 -070022
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080023#include <ndn-cxx/util/random.hpp>
24
25namespace ndn {
26namespace ndncert {
27
Zhiyi Zhang46049832020-09-28 17:08:12 -070028_LOG_INIT(ndncert.challenge.pin);
Zhiyi Zhang36706832019-07-04 21:33:03 -070029NDNCERT_REGISTER_CHALLENGE(ChallengePin, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080030
31const std::string ChallengePin::NEED_CODE = "need-code";
32const std::string ChallengePin::WRONG_CODE = "wrong-code";
Zhiyi Zhang46049832020-09-28 17:08:12 -070033const std::string ChallengePin::PARAMETER_KEY_CODE = "pin-code";
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034
35ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime)
Zhiyi Zhang46049832020-09-28 17:08:12 -070036 : ChallengeModule("pin")
37 , m_secretLifetime(secretLifetime)
38 , m_maxAttemptTimes(maxAttemptTimes)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080039{
40}
41
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042// For CA
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070043std::tuple<ErrorCode, std::string>
Zhiyi Zhange232a742020-09-29 17:34:17 -070044ChallengePin::handleChallengeRequest(const Block& params, RequestState& request)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080045{
Suyong Won44d0cce2020-05-10 04:07:43 -070046 params.parse();
Zhiyi Zhang8da54d62019-11-21 00:03:05 -080047 auto currentTime = time::system_clock::now();
Zhiyi Zhang46049832020-09-28 17:08:12 -070048 if (request.m_status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049 _LOG_TRACE("Challenge Interest arrives. Init the challenge");
50 // for the first time, init the challenge
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 std::string secretCode = generateSecretCode();
52 JsonSection secretJson;
Zhiyi Zhang46049832020-09-28 17:08:12 -070053 secretJson.add(PARAMETER_KEY_CODE, secretCode);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054 _LOG_TRACE("Secret for request " << request.m_requestId << " : " << secretCode);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070055 return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080056 }
Zhiyi Zhanga749f442020-09-29 17:19:51 -070057 if (request.m_challengeState) {
58 if (request.m_challengeState->m_challengeStatus == NEED_CODE ||
59 request.m_challengeState->m_challengeStatus == WRONG_CODE) {
60 _LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.m_challengeState->m_challengeStatus);
61 // the incoming interest should bring the pin code
62 std::string givenCode = readString(params.get(tlv_parameter_value));
63 auto secret = request.m_challengeState->m_secrets;
64 if (currentTime - request.m_challengeState->m_timestamp >= m_secretLifetime) {
65 return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
66 }
67 if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
68 _LOG_TRACE("Correct PIN code. Challenge succeeded.");
69 return returnWithSuccess(request);
70 }
71 // check rest attempt times
72 if (request.m_challengeState->m_remainingTries > 1) {
73 auto remainTime = m_secretLifetime - (currentTime - request.m_challengeState->m_timestamp);
74 _LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
75 return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
76 request.m_challengeState->m_remainingTries - 1,
77 time::duration_cast<time::seconds>(remainTime));
78 }
79 else {
80 // run out times
81 _LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
82 return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
83 }
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080084 }
85 }
Zhiyi Zhangaafc55e2020-09-28 17:54:48 -070086 return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087}
88
89// For Client
Zhiyi Zhang46049832020-09-28 17:08:12 -070090std::vector<std::tuple<std::string, std::string>>
91ChallengePin::getRequestedParameterList(Status status, const std::string& challengeStatus)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092{
Zhiyi Zhang46049832020-09-28 17:08:12 -070093 std::vector<std::tuple<std::string, std::string>> result;
94 if (status == Status::BEFORE_CHALLENGE) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070095 // do nothing
96 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -070097 else if (status == Status::CHALLENGE && challengeStatus == NEED_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -070098 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Please input your PIN code"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099 }
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700100 else if (status == Status::CHALLENGE && challengeStatus == WRONG_CODE) {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101 result.push_back(std::make_tuple(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again"));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700102 }
103 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700104 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700105 }
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700106 return result;
107}
108
Suyong Won19fba4d2020-05-09 13:39:46 -0700109Block
Zhiyi Zhang46049832020-09-28 17:08:12 -0700110ChallengePin::genChallengeRequestTLV(Status status, const std::string& challengeStatus, std::vector<std::tuple<std::string, std::string>>&& params)
Suyong Won19fba4d2020-05-09 13:39:46 -0700111{
112 Block request = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700113 if (status == Status::BEFORE_CHALLENGE) {
Suyong Won19fba4d2020-05-09 13:39:46 -0700114 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
115 }
Zhiyi Zhang46049832020-09-28 17:08:12 -0700116 else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
117 if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_CODE) {
118 BOOST_THROW_EXCEPTION(std::runtime_error("Wrong parameter provided."));
119 }
Suyong Won19fba4d2020-05-09 13:39:46 -0700120 request.push_back(makeStringBlock(tlv_selected_challenge, CHALLENGE_TYPE));
Zhiyi Zhang46049832020-09-28 17:08:12 -0700121 request.push_back(makeStringBlock(tlv_parameter_key, PARAMETER_KEY_CODE));
122 request.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(params[0])));
Suyong Won19fba4d2020-05-09 13:39:46 -0700123 }
124 else {
Zhiyi Zhang46049832020-09-28 17:08:12 -0700125 BOOST_THROW_EXCEPTION(std::runtime_error("Unexpected status or challenge status."));
Suyong Won19fba4d2020-05-09 13:39:46 -0700126 }
Suyong Won44d0cce2020-05-10 04:07:43 -0700127 request.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -0700128 return request;
129}
Zhiyi Zhang46049832020-09-28 17:08:12 -0700130} // namespace ndncert
131} // namespace ndn