blob: c5825f937d0d92b6d937cf6771da9b4f56e5d761 [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;
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080082 request.setStatus(WRONG_CODE);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080083 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
97std::list<std::string>
98ChallengePin::getSelectRequirements()
99{
100 std::list<std::string> result;
101 return result;
102}
103
104std::list<std::string>
105ChallengePin::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
117JsonSection
118ChallengePin::genChallengeInfo(const std::string& interestType, const std::string& status,
119 const std::list<std::string>& paramList)
120{
121 JsonSection result;
122 if (interestType == "_SELECT") {
123 BOOST_ASSERT(paramList.size() == 0);
124 }
125 else if (interestType == "_VALIDATE") {
126 BOOST_ASSERT(paramList.size() == 1);
127 result.put(JSON_PIN_CODE, paramList.front());
128 }
129 return result;
130}
131
132std::tuple<time::system_clock::TimePoint, std::string, int>
133ChallengePin::parseStoredSecrets(const JsonSection& storedSecrets)
134{
135 auto tp = time::fromIsoString(storedSecrets.get<std::string>(JSON_CODE_TP));
136 std::string rightCode= storedSecrets.get<std::string>(JSON_PIN_CODE);
137 int attemptTimes = std::stoi(storedSecrets.get<std::string>(JSON_ATTEMPT_TIMES));
138
139 return std::make_tuple(tp, rightCode, attemptTimes);
140}
141
142JsonSection
143ChallengePin::generateStoredSecrets(const time::system_clock::TimePoint& tp,
144 const std::string& secretCode, int attempTimes)
145{
146 JsonSection json;
147 json.put(JSON_CODE_TP, time::toIsoString(tp));
148 json.put(JSON_PIN_CODE, secretCode);
149 json.put(JSON_ATTEMPT_TIMES, std::to_string(attempTimes));
150 return json;
151}
152
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800153} // namespace ndncert
154} // namespace ndn