blob: 2bb81ea4498244fc74b83c9d5f3fb6669bed832a [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- 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 Zhang08e0e982017-03-01 10:10:42 -080022#include "logging.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080023#include "json-helper.hpp"
24#include <ndn-cxx/util/random.hpp>
25
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080029_LOG_INIT(ndncert.challenge-pin);
30
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080031NDNCERT_REGISTER_CHALLENGE(ChallengePin, "PIN");
32
33const std::string ChallengePin::NEED_CODE = "need-code";
34const std::string ChallengePin::WRONG_CODE = "wrong-code";
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070035
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036const std::string ChallengePin::FAILURE_TIMEOUT = "failure-timeout";
37const std::string ChallengePin::FAILURE_MAXRETRY = "failure-max-retry";
38
39const std::string ChallengePin::JSON_CODE_TP = "code-timepoint";
40const std::string ChallengePin::JSON_PIN_CODE = "code";
41const std::string ChallengePin::JSON_ATTEMPT_TIMES = "attempt-times";
42
43ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime)
44 : ChallengeModule("PIN")
45 , m_secretLifetime(secretLifetime)
46 , m_maxAttemptTimes(maxAttemptTimes)
47{
48}
49
50JsonSection
51ChallengePin::processSelectInterest(const Interest& interest, CertificateRequest& request)
52{
Zhiyi Zhange30eb352017-04-13 15:26:14 -070053 // interest format: /caName/CA/_SELECT/{"request-id":"id"}/PIN/<signature>
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054 request.setStatus(NEED_CODE);
55 request.setChallengeType(CHALLENGE_TYPE);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080056 std::string secretCode = generateSecretCode();
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080057 request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(),
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080058 secretCode,
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080059 m_maxAttemptTimes));
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080060 _LOG_TRACE("Secret for request " << request.getRequestId() << " : " << secretCode);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080061 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE);
62}
63
64JsonSection
65ChallengePin::processValidateInterest(const Interest& interest, CertificateRequest& request)
66{
Zhiyi Zhange30eb352017-04-13 15:26:14 -070067 // interest format: /caName/CA/_VALIDATION/{"request-id":"id"}/PIN/{"code":"code"}/<signature>
68 JsonSection infoJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080069 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 Zhanga9bda732017-05-20 22:58:55 -070076 return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_TIMEOUT);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080077 }
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 Zhang0df767e2017-02-21 16:05:36 -080088 request.setStatus(WRONG_CODE);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080089 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 Zhanga9bda732017-05-20 22:58:55 -070098 return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_MAXRETRY);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080099 }
100 }
101}
102
103std::list<std::string>
104ChallengePin::getSelectRequirements()
105{
106 std::list<std::string> result;
107 return result;
108}
109
110std::list<std::string>
111ChallengePin::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 Zhanga9bda732017-05-20 22:58:55 -0700118 result.push_back("Incorrect PIN code, please try again and input your verification code:");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800119 }
120 return result;
121}
122
123JsonSection
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700124ChallengePin::doGenSelectParamsJson(const std::string& status,
125 const std::list<std::string>& paramList)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800126{
127 JsonSection result;
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700128 BOOST_ASSERT(status == WAIT_SELECTION);
Zhiyi Zhangf72c0542017-03-16 14:45:30 -0700129 BOOST_ASSERT(paramList.size() == 0);
130 return result;
131}
132
133JsonSection
134ChallengePin::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 Zhang65ba9322017-01-19 14:15:03 -0800140 return result;
141}
142
143std::tuple<time::system_clock::TimePoint, std::string, int>
144ChallengePin::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
153JsonSection
154ChallengePin::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 Zhang65ba9322017-01-19 14:15:03 -0800164} // namespace ndncert
165} // namespace ndn