blob: 2dd4acf849c8a4e6bd27ddf608451f536ca53259 [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"
22#include "json-helper.hpp"
23#include <ndn-cxx/util/random.hpp>
24
25namespace ndn {
26namespace ndncert {
27
28NDNCERT_REGISTER_CHALLENGE(ChallengePin, "PIN");
29
30const std::string ChallengePin::NEED_CODE = "need-code";
31const std::string ChallengePin::WRONG_CODE = "wrong-code";
32const std::string ChallengePin::FAILURE_TIMEOUT = "failure-timeout";
33const std::string ChallengePin::FAILURE_MAXRETRY = "failure-max-retry";
34
35const std::string ChallengePin::JSON_CODE_TP = "code-timepoint";
36const std::string ChallengePin::JSON_PIN_CODE = "code";
37const std::string ChallengePin::JSON_ATTEMPT_TIMES = "attempt-times";
38
39ChallengePin::ChallengePin(const size_t& maxAttemptTimes, const time::seconds& secretLifetime)
40 : ChallengeModule("PIN")
41 , m_secretLifetime(secretLifetime)
42 , m_maxAttemptTimes(maxAttemptTimes)
43{
44}
45
46JsonSection
47ChallengePin::processSelectInterest(const Interest& interest, CertificateRequest& request)
48{
49 // interest format: /CA/_SELECT/{"request-id":"id"}/PIN/<signature>
50 request.setStatus(NEED_CODE);
51 request.setChallengeType(CHALLENGE_TYPE);
52 request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(),
Zhiyi Zhangfb74ae22017-02-22 08:02:27 -080053 generateSecretCode(),
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054 m_maxAttemptTimes));
55 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE);
56}
57
58JsonSection
59ChallengePin::processValidateInterest(const Interest& interest, CertificateRequest& request)
60{
61 // interest format: /CA/_VALIDATION/{"request-id":"id"}/PIN/{"code":"code"}/<signature>
62 JsonSection infoJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 3);
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;
82 request.setChallengeSecrets(generateStoredSecrets(std::get<0>(parsedSecret),
83 std::get<1>(parsedSecret),
84 restAttemptTimes));
85 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, WRONG_CODE);
86 }
87 else {
88 // run out times
89 request.setStatus(FAILURE_MAXRETRY);
90 request.setChallengeSecrets(JsonSection());
91 return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_MAXRETRY);
92 }
93 }
94}
95
96std::list<std::string>
97ChallengePin::getSelectRequirements()
98{
99 std::list<std::string> result;
100 return result;
101}
102
103std::list<std::string>
104ChallengePin::getValidateRequirements(const std::string& status)
105{
106 std::list<std::string> result;
107 if (status == NEED_CODE) {
108 result.push_back("Please input your verification code:");
109 }
110 else if (status == WRONG_CODE) {
111 result.push_back("Incorrect PIN code, please input your verification code:");
112 }
113 return result;
114}
115
116JsonSection
117ChallengePin::genChallengeInfo(const std::string& interestType, const std::string& status,
118 const std::list<std::string>& paramList)
119{
120 JsonSection result;
121 if (interestType == "_SELECT") {
122 BOOST_ASSERT(paramList.size() == 0);
123 }
124 else if (interestType == "_VALIDATE") {
125 BOOST_ASSERT(paramList.size() == 1);
126 result.put(JSON_PIN_CODE, paramList.front());
127 }
128 return result;
129}
130
131std::tuple<time::system_clock::TimePoint, std::string, int>
132ChallengePin::parseStoredSecrets(const JsonSection& storedSecrets)
133{
134 auto tp = time::fromIsoString(storedSecrets.get<std::string>(JSON_CODE_TP));
135 std::string rightCode= storedSecrets.get<std::string>(JSON_PIN_CODE);
136 int attemptTimes = std::stoi(storedSecrets.get<std::string>(JSON_ATTEMPT_TIMES));
137
138 return std::make_tuple(tp, rightCode, attemptTimes);
139}
140
141JsonSection
142ChallengePin::generateStoredSecrets(const time::system_clock::TimePoint& tp,
143 const std::string& secretCode, int attempTimes)
144{
145 JsonSection json;
146 json.put(JSON_CODE_TP, time::toIsoString(tp));
147 json.put(JSON_PIN_CODE, secretCode);
148 json.put(JSON_ATTEMPT_TIMES, std::to_string(attempTimes));
149 return json;
150}
151
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800152} // namespace ndncert
153} // namespace ndn