blob: 3a9a2b6e016494cfb9c26580f545c4c540588526 [file] [log] [blame]
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
swa77020643ac2020-03-26 02:24:45 -07002/**
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang08e0e982017-03-01 10:10:42 -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
Zhiyi Zhang837406d2020-10-05 22:01:31 -070021#include "requester.hpp"
Zhiyi Zhang90c75782020-10-06 15:04:03 -070022#include <ndn-cxx/security/verification-helpers.hpp>
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -070023#include <boost/asio.hpp>
Davide Pesaventob48bbda2020-07-27 19:41:37 -040024#include <boost/program_options/options_description.hpp>
25#include <boost/program_options/parsers.hpp>
26#include <boost/program_options/variables_map.hpp>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070027#include <iostream>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070028#include <string>
29
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080030namespace ndn {
31namespace ndncert {
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -070032namespace requester {
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080033
Zhiyi Zhang48f23782020-09-28 12:11:24 -070034static void
tylerliufeabfdc2020-10-03 15:09:58 -070035selectCaProfile(std::string configFilePath);
36static void
37runProbe(CaProfile profile);
38static void
39runNew(CaProfile profile, Name identityName);
40static void
41runChallenge(const std::string& challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042
Zhiyi Zhang837406d2020-10-05 22:01:31 -070043size_t nStep = 1;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044Face face;
tylerliua7bea662020-10-08 18:51:02 -070045security::KeyChain keyChain;
tylerliubb630362020-11-10 11:31:35 -080046shared_ptr<RequestState> requesterState = nullptr;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080047
Zhiyi Zhang46049832020-09-28 17:08:12 -070048static void
49captureParams(std::vector<std::tuple<std::string, std::string>>& requirement)
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080050{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 std::list<std::string> results;
Zhiyi Zhang46049832020-09-28 17:08:12 -070052 for (auto& item : requirement) {
53 std::cerr << std::get<1>(item) << std::endl;
54 std::string captured;
55 getline(std::cin, captured);
56 std::get<1>(item) = captured;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080057 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 std::cerr << "Got it. This is what you've provided:" << std::endl;
Zhiyi Zhang46049832020-09-28 17:08:12 -070059 for (const auto& item : requirement) {
60 std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080061 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080063
Zhiyi Zhang9829da92020-09-30 16:19:34 -070064static std::vector<std::tuple<std::string, std::string>>
65captureParams(const std::list<std::string>& requirement)
Zhiyi Zhang547c8512019-06-18 23:46:14 -070066{
Zhiyi Zhang9829da92020-09-30 16:19:34 -070067 std::vector<std::tuple<std::string, std::string>> results;
tylerliufeabfdc2020-10-03 15:09:58 -070068 for (const auto& r : requirement) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -070069 results.emplace_back(r, "Please input: " + r);
Zhiyi Zhang547c8512019-06-18 23:46:14 -070070 }
tylerliufeabfdc2020-10-03 15:09:58 -070071 captureParams(results);
Zhiyi Zhang547c8512019-06-18 23:46:14 -070072 return results;
73}
74
tylerliufeabfdc2020-10-03 15:09:58 -070075static int
Zhiyi Zhang36706832019-07-04 21:33:03 -070076captureValidityPeriod()
77{
Zhiyi Zhang837406d2020-10-05 22:01:31 -070078 std::cerr << "\n***************************************\n"
79 << "Step " << nStep++
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -070080 << ": Please type in your expected validity period of your certificate."
81 << " Type the number of hours (168 for week, 730 for month, 8760 for year)."
82 << " The CA may reject your application if your expected period is too long." << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -070083 while (true) {
84 std::string periodStr = "";
85 getline(std::cin, periodStr);
86 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -070087 return std::stoul(periodStr);
tylerliufeabfdc2020-10-03 15:09:58 -070088 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -070089 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -070090 std::cerr << "Your input is invalid. Try again: " << std::endl;
91 }
Zhiyi Zhang36706832019-07-04 21:33:03 -070092 }
93}
94
95static void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096onNackCb()
97{
98 std::cerr << "Got NACK\n";
99}
100
101static void
102timeoutCb()
103{
104 std::cerr << "Interest sent time out\n";
105}
106
107static void
swa770cf1d8f72020-04-21 23:12:39 -0700108certFetchCb(const Data& reply)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109{
tylerliufeabfdc2020-10-03 15:09:58 -0700110 auto item = Requester::onCertFetchResponse(reply);
111 if (item) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700112 keyChain.addCertificate(keyChain.getPib().getIdentity(item->getIdentity()).getKey(item->getKeyName()), *item);
tylerliufeabfdc2020-10-03 15:09:58 -0700113 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700114 std::cerr << "\n***************************************\n"
115 << "Step " << nStep++
116 << ": DONE\nCertificate with Name: " << reply.getName().toUri()
117 << "has already been installed to your local keychain" << std::endl
118 << "Exit now";
119 face.getIoService().stop();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120}
121
122static void
123challengeCb(const Data& reply)
124{
tylerliufeabfdc2020-10-03 15:09:58 -0700125 try {
126 Requester::onChallengeResponse(*requesterState, reply);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700127 }
128 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -0700129 std::cerr << "Error when decoding challenge step: " << e.what() << std::endl;
130 exit(1);
131 }
132 if (requesterState->m_status == Status::SUCCESS) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700133 std::cerr << "Certificate has already been issued, downloading certificate..." << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700134 face.expressInterest(*Requester::genCertFetchInterest(*requesterState), bind(&certFetchCb, _2),
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700135 bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700136 return;
137 }
tylerliufeabfdc2020-10-03 15:09:58 -0700138 runChallenge(requesterState->m_challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700139}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800140
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141static void
142newCb(const Data& reply)
143{
tylerliufeabfdc2020-10-03 15:09:58 -0700144 std::list<std::string> challengeList;
145 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700146 challengeList = Requester::onNewRenewRevokeResponse(*requesterState, reply);
147 }
148 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -0700149 std::cerr << "Error on decoding NEW step reply because: " << e.what() << std::endl;
150 exit(1);
151 }
152
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700153 size_t challengeIndex = 0;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700154 if (challengeList.size() < 1) {
155 std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700156 exit(1);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800157 }
Zhiyi Zhang36706832019-07-04 21:33:03 -0700158 else if (challengeList.size() > 1) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700159 std::cerr << "\n***************************************\n"
160 << "Step " << nStep++
161 << ": CHALLENGE SELECTION" << std::endl;
162 size_t count = 0;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700163 std::string choice = "";
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700164 for (auto item : challengeList) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700165 std::cerr << "> Index: " << count++ << std::endl
166 << ">> Challenge:" << item << std::endl;
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700167 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700168 std::cerr << "Please type in the challenge index that you want to perform:" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700169 while (true) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700170 getline(std::cin, choice);
171 try {
172 challengeIndex = std::stoul(choice);
173 }
174 catch (const std::exception& e) {
175 std::cerr << "Your input is not valid. Try again:" << std::endl;
176 continue;
177 }
178 if (challengeIndex >= count) {
179 std::cerr << "Your input index is out of range. Try again:" << std::endl;
180 continue;
181 }
182 break;
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700183 }
Zhiyi Zhang36706832019-07-04 21:33:03 -0700184 }
185 auto it = challengeList.begin();
186 std::advance(it, challengeIndex);
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700187 std::cerr << "The challenge has been selected: " << *it << std::endl;
188 runChallenge(*it);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700189}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800190
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700191static void
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700192InfoCb(const Data& reply, const Name& certFullName)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700193{
Zhiyi Zhang997669a2020-10-28 21:15:40 -0700194 optional<CaProfile> profile;
tylerliufeabfdc2020-10-03 15:09:58 -0700195 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700196 if (certFullName.empty()) {
197 profile = Requester::onCaProfileResponse(reply);
tylerliufeabfdc2020-10-03 15:09:58 -0700198 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700199 else {
200 profile = Requester::onCaProfileResponseAfterRedirection(reply, certFullName);
201 }
202 }
203 catch (const std::exception& e) {
204 std::cerr << "The fetched CA information cannot be used because: " << e.what() << std::endl;
205 return;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700206 }
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700207 std::cerr << "\n***************************************\n"
208 << "Step " << nStep++
209 << ": Will use a new trust anchor, please double check the identity info:" << std::endl
210 << "> New CA name: " << profile->m_caPrefix.toUri() << std::endl
tylerliua7bea662020-10-08 18:51:02 -0700211 << "> This trust anchor information is signed by: " << reply.getSignatureInfo().getKeyLocator() << std::endl
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700212 << "> The certificate: " << profile->m_cert << std::endl
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400213 << "Do you trust the information? Type in YES or NO" << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700214
215 std::string answer;
216 getline(std::cin, answer);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700217 boost::algorithm::to_lower(answer);
218 if (answer == "yes") {
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700219 std::cerr << "You answered YES: new CA " << profile->m_caPrefix.toUri() << " will be used" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700220 runProbe(*profile);
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700221 // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700222 }
223 else {
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700224 std::cerr << "You answered NO: new CA " << profile->m_caPrefix.toUri() << " will not be used" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700225 exit(0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700226 }
227}
228
229static void
tylerliufeabfdc2020-10-03 15:09:58 -0700230probeCb(const Data& reply, CaProfile profile)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700231{
tylerliub47dad72020-10-08 21:36:55 -0700232 std::vector<std::pair<Name, int>> names;
tylerliufeabfdc2020-10-03 15:09:58 -0700233 std::vector<Name> redirects;
234 Requester::onProbeResponse(reply, profile, names, redirects);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700235 size_t count = 0;
236 std::cerr << "\n***************************************\n"
237 << "Step " << nStep++
238 << ": You can either select one of the following names suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700239 for (const auto& name : names) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700240 std::cerr << "> Index: " << count++ << std::endl
tylerliub47dad72020-10-08 21:36:55 -0700241 << ">> Suggested name: " << name.first.toUri() << std::endl
242 << ">> Corresponding Max sufiix length: " << name.second << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700243 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700244 std::cerr << "\nOr choose another trusted CA suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700245 for (const auto& redirect : redirects) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700246 std::cerr << "> Index: " << count++ << std::endl
tylerliua7bea662020-10-08 18:51:02 -0700247 << ">> Suggested CA: " << security::extractIdentityFromCertName(redirect.getPrefix(-1)) << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700248 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700249 std::cerr << "Please type in the index of your choice:" << std::endl;
250 size_t index = 0;
tylerliufeabfdc2020-10-03 15:09:58 -0700251 try {
252 std::string input;
253 getline(std::cin, input);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700254 index = std::stoul(input);
tylerliufeabfdc2020-10-03 15:09:58 -0700255 }
256 catch (const std::exception& e) {
257 std::cerr << "Your input is Invalid. Exit" << std::endl;
258 exit(0);
259 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700260 if (index >= names.size() + redirects.size()) {
tylerliufeabfdc2020-10-03 15:09:58 -0700261 std::cerr << "Your input is not an existing index. Exit" << std::endl;
262 return;
263 }
264 if (index < names.size()) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700265 //names
tylerliub47dad72020-10-08 21:36:55 -0700266 std::cerr << "You selected name: " << names[index].first.toUri() << std::endl;
267 //TODO add prompt to "add suffix"
268 runNew(profile, names[index].first);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700269 }
270 else {
271 //redirects
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700272 auto redirectedCaFullName = redirects[index - names.size()];
tylerliua7bea662020-10-08 18:51:02 -0700273 auto redirectedCaName = security::extractIdentityFromCertName(redirectedCaFullName.getPrefix(-1));
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700274 std::cerr << "You selected to be redirected to CA: " << redirectedCaName.toUri() << std::endl;
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700275 face.expressInterest(
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700276 *Requester::genCaProfileDiscoveryInterest(redirectedCaName),
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700277 [&](const Interest&, const Data& data) {
278 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
279 face.expressInterest(*fetchingInterest,
280 bind(&InfoCb, _2, redirectedCaFullName),
281 bind(&onNackCb),
282 bind(&timeoutCb));
283 },
284 bind(&onNackCb),
285 bind(&timeoutCb));
tylerliufeabfdc2020-10-03 15:09:58 -0700286 }
287}
288
289static void
290selectCaProfile(std::string configFilePath)
291{
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700292 ProfileStorage profileStorage;
tylerliufeabfdc2020-10-03 15:09:58 -0700293 try {
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700294 profileStorage.load(configFilePath);
tylerliufeabfdc2020-10-03 15:09:58 -0700295 }
296 catch (const std::exception& e) {
297 std::cerr << "Cannot load the configuration file: " << e.what() << std::endl;
298 exit(1);
299 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700300 size_t count = 0;
301 std::cerr << "***************************************\n"
302 << "Step " << nStep++ << ": CA SELECTION" << std::endl;
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700303 for (auto item : profileStorage.m_caItems) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700304 std::cerr << "> Index: " << count++ << std::endl
305 << ">> CA prefix:" << item.m_caPrefix << std::endl
306 << ">> Introduction: " << item.m_caInfo << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700307 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700308 std::cerr << "Please type in the CA's index that you want to apply or type in NONE if your expected CA is not in the list:\n";
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700309
Zhiyi Zhang36706832019-07-04 21:33:03 -0700310 std::string caIndexS, caIndexSLower;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700311 getline(std::cin, caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700312 caIndexSLower = caIndexS;
313 boost::algorithm::to_lower(caIndexSLower);
314 if (caIndexSLower == "none") {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700315 std::cerr << "\n***************************************\n"
316 << "Step " << nStep << ": ADD NEW CA\nPlease type in the CA's Name:" << std::endl;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700317 std::string expectedCAName;
318 getline(std::cin, expectedCAName);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700319 face.expressInterest(
320 *Requester::genCaProfileDiscoveryInterest(Name(expectedCAName)),
321 [&](const Interest&, const Data& data) {
322 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
323 face.expressInterest(*fetchingInterest,
324 bind(&InfoCb, _2, Name()),
325 bind(&onNackCb),
326 bind(&timeoutCb));
327 },
328 bind(&onNackCb),
329 bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700330 }
331 else {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700332 size_t caIndex;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700333 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700334 caIndex = std::stoul(caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700335 }
336 catch (const std::exception& e) {
337 std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl;
338 return;
339 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700340 if (caIndex >= count) {
Zhiyi Zhang36706832019-07-04 21:33:03 -0700341 std::cerr << "Your input is not an existing index. Exit" << std::endl;
342 return;
343 }
Zhiyi Zhanga16b7582020-10-29 18:59:46 -0700344 auto itemIterator = profileStorage.m_caItems.cbegin();
tylerliufeabfdc2020-10-03 15:09:58 -0700345 std::advance(itemIterator, caIndex);
346 auto targetCaItem = *itemIterator;
347 runProbe(targetCaItem);
348 }
349}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700350
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700351static void
352runProbe(CaProfile profile)
tylerliufeabfdc2020-10-03 15:09:58 -0700353{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700354 std::cerr << "\n***************************************\n"
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700355 << "Step " << nStep++
356 << ": Do you know your identity name to be certified by CA "
357 << profile.m_caPrefix.toUri()
358 << " already? Type in YES or NO" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700359 bool validAnswer = false;
360 while (!validAnswer) {
361 std::string answer;
362 getline(std::cin, answer);
363 boost::algorithm::to_lower(answer);
364 if (answer == "yes") {
365 validAnswer = true;
366 std::cerr << "You answered YES" << std::endl;
367 std::cerr << "\n***************************************\n"
368 << "Step " << nStep++
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700369 << ": Please type in the full identity name you want to get (with CA prefix "
370 << profile.m_caPrefix.toUri()
371 << "):" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700372 std::string identityNameStr;
373 getline(std::cin, identityNameStr);
374 runNew(profile, Name(identityNameStr));
tylerliufeabfdc2020-10-03 15:09:58 -0700375 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700376 else if (answer == "no") {
377 validAnswer = true;
378 std::cerr << "You answered NO" << std::endl;
379 std::cerr << "\n***************************************\n"
380 << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl;
381 auto capturedParams = captureParams(profile.m_probeParameterKeys);
Zhiyi Zhangdb1ec762020-10-30 08:58:39 -0700382 face.expressInterest(*Requester::genProbeInterest(profile, std::move(capturedParams)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700383 bind(&probeCb, _2, profile), bind(&onNackCb), bind(&timeoutCb));
384 }
385 else {
386 std::cerr << "Invalid answer. Type in YES or NO" << std::endl;
387 }
388 }
tylerliufeabfdc2020-10-03 15:09:58 -0700389}
390
391static void
392runNew(CaProfile profile, Name identityName)
393{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700394 int validityPeriod = captureValidityPeriod();
395 auto now = time::system_clock::now();
396 std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl;
tylerliubb630362020-11-10 11:31:35 -0800397 requesterState =std::make_shared<RequestState>(keyChain, profile, RequestType::NEW);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700398 auto interest = Requester::genNewInterest(*requesterState, identityName, now, now + time::hours(validityPeriod));
399 if (interest != nullptr) {
400 face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb));
401 }
402 else {
403 std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl;
404 }
tylerliufeabfdc2020-10-03 15:09:58 -0700405}
406
407static void
408runChallenge(const std::string& challengeType)
409{
Zhiyi Zhangc5d93a92020-10-14 17:07:35 -0700410 std::vector<std::tuple<std::string, std::string>> requirement;
411 try {
412 requirement = Requester::selectOrContinueChallenge(*requesterState, challengeType);
413 }
414 catch (const std::exception& e) {
415 std::cerr << "Error. Cannot successfully load the Challenge Module with error: " << std::string(e.what())
416 << "Exit." << std::endl;
417 exit(1);
418 }
tylerliufeabfdc2020-10-03 15:09:58 -0700419 if (requirement.size() > 0) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700420 std::cerr << "\n***************************************\n"
421 << "Step " << nStep
422 << ": Please provide parameters used for Identity Verification Challenge" << std::endl;
423 captureParams(requirement);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700424 }
tylerliufeabfdc2020-10-03 15:09:58 -0700425 face.expressInterest(*Requester::genChallengeInterest(*requesterState, std::move(requirement)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700426 bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700427}
428
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700429static void
430handleSignal(const boost::system::error_code& error, int signalNum)
431{
432 if (error) {
433 return;
434 }
435 const char* signalName = ::strsignal(signalNum);
436 std::cerr << "Exiting on signal ";
437 if (signalName == nullptr) {
438 std::cerr << signalNum;
439 }
440 else {
441 std::cerr << signalName;
442 }
443 std::cerr << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700444 if (requesterState) {
445 Requester::endSession(*requesterState);
446 }
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700447 face.getIoService().stop();
tylerliufeabfdc2020-10-03 15:09:58 -0700448 exit(1);
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700449}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800450
451int
452main(int argc, char* argv[])
453{
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700454 boost::asio::signal_set terminateSignals(face.getIoService());
455 terminateSignals.add(SIGINT);
456 terminateSignals.add(SIGTERM);
457 terminateSignals.async_wait(handleSignal);
458
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800459 namespace po = boost::program_options;
Zhiyi Zhang840afd92020-10-21 13:24:08 -0700460 std::string configFilePath = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/client.conf";
Zhiyi Zhang36706832019-07-04 21:33:03 -0700461 po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n");
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700462 description.add_options()("help,h", "produce help message")("config-file,c", po::value<std::string>(&configFilePath), "configuration file name");
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800463 po::positional_options_description p;
464
465 po::variables_map vm;
466 try {
467 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
468 po::notify(vm);
469 }
470 catch (const std::exception& e) {
471 std::cerr << "ERROR: " << e.what() << std::endl;
472 return 1;
473 }
474 if (vm.count("help") != 0) {
475 std::cerr << description << std::endl;
476 return 0;
477 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700478 selectCaProfile(configFilePath);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800479 face.processEvents();
480 return 0;
481}
482
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700483} // namespace requester
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700484} // namespace ndncert
485} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800486
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700487int
488main(int argc, char* argv[])
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800489{
Zhiyi Zhang3002e6b2020-10-29 18:54:07 -0700490 return ndn::ndncert::requester::main(argc, argv);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800491}