blob: 1324bba7604fdcb549bbfeab2cadb020c3725c08 [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 Zhangdbd9d432020-10-07 15:56:27 -070021#include "identity-challenge/challenge-module.hpp"
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070022#include "protocol-detail/info.hpp"
Zhiyi Zhang837406d2020-10-05 22:01:31 -070023#include "requester.hpp"
Zhiyi Zhang90c75782020-10-06 15:04:03 -070024#include <ndn-cxx/security/verification-helpers.hpp>
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -070025#include <boost/asio.hpp>
Davide Pesaventob48bbda2020-07-27 19:41:37 -040026#include <boost/program_options/options_description.hpp>
27#include <boost/program_options/parsers.hpp>
28#include <boost/program_options/variables_map.hpp>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070029#include <iostream>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070030#include <string>
31
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080032namespace ndn {
33namespace ndncert {
34
Zhiyi Zhang48f23782020-09-28 12:11:24 -070035static void
tylerliufeabfdc2020-10-03 15:09:58 -070036selectCaProfile(std::string configFilePath);
37static void
38runProbe(CaProfile profile);
39static void
40runNew(CaProfile profile, Name identityName);
41static void
42runChallenge(const std::string& challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043
Zhiyi Zhang837406d2020-10-05 22:01:31 -070044size_t nStep = 1;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045Face face;
tylerliu182bc532020-09-25 01:54:45 -070046security::v2::KeyChain keyChain;
tylerliufeabfdc2020-10-03 15:09:58 -070047shared_ptr<RequesterState> requesterState = nullptr;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080048
Zhiyi Zhang46049832020-09-28 17:08:12 -070049static void
50captureParams(std::vector<std::tuple<std::string, std::string>>& requirement)
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080051{
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052 std::list<std::string> results;
Zhiyi Zhang46049832020-09-28 17:08:12 -070053 for (auto& item : requirement) {
54 std::cerr << std::get<1>(item) << std::endl;
55 std::string captured;
56 getline(std::cin, captured);
57 std::get<1>(item) = captured;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080058 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 std::cerr << "Got it. This is what you've provided:" << std::endl;
Zhiyi Zhang46049832020-09-28 17:08:12 -070060 for (const auto& item : requirement) {
61 std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080062 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080064
Zhiyi Zhang9829da92020-09-30 16:19:34 -070065static std::vector<std::tuple<std::string, std::string>>
66captureParams(const std::list<std::string>& requirement)
Zhiyi Zhang547c8512019-06-18 23:46:14 -070067{
Zhiyi Zhang9829da92020-09-30 16:19:34 -070068 std::vector<std::tuple<std::string, std::string>> results;
tylerliufeabfdc2020-10-03 15:09:58 -070069 for (const auto& r : requirement) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -070070 results.emplace_back(r, "Please input: " + r);
Zhiyi Zhang547c8512019-06-18 23:46:14 -070071 }
tylerliufeabfdc2020-10-03 15:09:58 -070072 captureParams(results);
Zhiyi Zhang547c8512019-06-18 23:46:14 -070073 return results;
74}
75
tylerliufeabfdc2020-10-03 15:09:58 -070076static int
Zhiyi Zhang36706832019-07-04 21:33:03 -070077captureValidityPeriod()
78{
Zhiyi Zhang837406d2020-10-05 22:01:31 -070079 std::cerr << "\n***************************************\n"
80 << "Step " << nStep++
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -070081 << ": Please type in your expected validity period of your certificate."
82 << " Type the number of hours (168 for week, 730 for month, 8760 for year)."
83 << " The CA may reject your application if your expected period is too long." << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -070084 while (true) {
85 std::string periodStr = "";
86 getline(std::cin, periodStr);
87 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -070088 return std::stoul(periodStr);
tylerliufeabfdc2020-10-03 15:09:58 -070089 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -070090 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -070091 std::cerr << "Your input is invalid. Try again: " << std::endl;
92 }
Zhiyi Zhang36706832019-07-04 21:33:03 -070093 }
94}
95
96static void
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097onNackCb()
98{
99 std::cerr << "Got NACK\n";
100}
101
102static void
103timeoutCb()
104{
105 std::cerr << "Interest sent time out\n";
106}
107
108static void
swa770cf1d8f72020-04-21 23:12:39 -0700109certFetchCb(const Data& reply)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110{
tylerliufeabfdc2020-10-03 15:09:58 -0700111 auto item = Requester::onCertFetchResponse(reply);
112 if (item) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700113 keyChain.addCertificate(keyChain.getPib().getIdentity(item->getIdentity()).getKey(item->getKeyName()), *item);
tylerliufeabfdc2020-10-03 15:09:58 -0700114 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700115 std::cerr << "\n***************************************\n"
116 << "Step " << nStep++
117 << ": DONE\nCertificate with Name: " << reply.getName().toUri()
118 << "has already been installed to your local keychain" << std::endl
119 << "Exit now";
120 face.getIoService().stop();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121}
122
123static void
124challengeCb(const Data& reply)
125{
tylerliufeabfdc2020-10-03 15:09:58 -0700126 try {
127 Requester::onChallengeResponse(*requesterState, reply);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700128 }
129 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -0700130 std::cerr << "Error when decoding challenge step: " << e.what() << std::endl;
131 exit(1);
132 }
133 if (requesterState->m_status == Status::SUCCESS) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700134 std::cerr << "Certificate has already been issued, downloading certificate..." << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700135 face.expressInterest(*Requester::genCertFetchInterest(*requesterState), bind(&certFetchCb, _2),
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700136 bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700137 return;
138 }
139
tylerliufeabfdc2020-10-03 15:09:58 -0700140 runChallenge(requesterState->m_challengeType);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800142
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700143static void
144newCb(const Data& reply)
145{
tylerliufeabfdc2020-10-03 15:09:58 -0700146 std::list<std::string> challengeList;
147 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700148 challengeList = Requester::onNewRenewRevokeResponse(*requesterState, reply);
149 }
150 catch (const std::exception& e) {
tylerliufeabfdc2020-10-03 15:09:58 -0700151 std::cerr << "Error on decoding NEW step reply because: " << e.what() << std::endl;
152 exit(1);
153 }
154
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700155 size_t challengeIndex = 0;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700156 if (challengeList.size() < 1) {
157 std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700158 exit(1);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800159 }
Zhiyi Zhang36706832019-07-04 21:33:03 -0700160 else if (challengeList.size() > 1) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700161 std::cerr << "\n***************************************\n"
162 << "Step " << nStep++
163 << ": CHALLENGE SELECTION" << std::endl;
164 size_t count = 0;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700165 std::string choice = "";
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700166 for (auto item : challengeList) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700167 std::cerr << "> Index: " << count++ << std::endl
168 << ">> Challenge:" << item << std::endl;
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700169 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700170 std::cerr << "Please type in the challenge index that you want to perform:" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700171 while (true) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700172 getline(std::cin, choice);
173 try {
174 challengeIndex = std::stoul(choice);
175 }
176 catch (const std::exception& e) {
177 std::cerr << "Your input is not valid. Try again:" << std::endl;
178 continue;
179 }
180 if (challengeIndex >= count) {
181 std::cerr << "Your input index is out of range. Try again:" << std::endl;
182 continue;
183 }
184 break;
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700185 }
Zhiyi Zhang36706832019-07-04 21:33:03 -0700186 }
187 auto it = challengeList.begin();
188 std::advance(it, challengeIndex);
189 unique_ptr<ChallengeModule> challenge = ChallengeModule::createChallengeModule(*it);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700190 if (challenge != nullptr) {
tylerliufeabfdc2020-10-03 15:09:58 -0700191 std::cerr << "The challenge has been selected: " << *it << std::endl;
192 runChallenge(*it);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800193 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700194 else {
Zhiyi Zhang36706832019-07-04 21:33:03 -0700195 std::cerr << "Error. Cannot load selected Challenge Module. Exit." << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700196 exit(1);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700197 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700198}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800199
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700200static void
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700201InfoCb(const Data& reply, const Name& certFullName)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700202{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700203 boost::optional<CaProfile> profile;
tylerliufeabfdc2020-10-03 15:09:58 -0700204 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700205 if (certFullName.empty()) {
206 profile = Requester::onCaProfileResponse(reply);
tylerliufeabfdc2020-10-03 15:09:58 -0700207 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700208 else {
209 profile = Requester::onCaProfileResponseAfterRedirection(reply, certFullName);
210 }
211 }
212 catch (const std::exception& e) {
213 std::cerr << "The fetched CA information cannot be used because: " << e.what() << std::endl;
214 return;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700215 }
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700216
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700217 std::cerr << "Will use a new trust anchor, please double check the identity info:" << std::endl
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -0700218 << "This trust anchor information is signed by " << reply.getSignature().getKeyLocator()
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400219 << std::endl
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700220 << "The certificate is " << profile->m_cert << std::endl
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400221 << "Do you trust the information? Type in YES or NO" << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700222
223 std::string answer;
224 getline(std::cin, answer);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700225 boost::algorithm::to_lower(answer);
226 if (answer == "yes") {
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700227 std::cerr << "You answered YES: new CA will be used" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700228 runProbe(*profile);
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700229 // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700230 }
231 else {
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700232 std::cerr << "You answered NO: new CA will not be used" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700233 exit(0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700234 }
235}
236
237static void
tylerliufeabfdc2020-10-03 15:09:58 -0700238probeCb(const Data& reply, CaProfile profile)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700239{
tylerliufeabfdc2020-10-03 15:09:58 -0700240 std::vector<Name> names;
241 std::vector<Name> redirects;
242 Requester::onProbeResponse(reply, profile, names, redirects);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700243 size_t count = 0;
244 std::cerr << "\n***************************************\n"
245 << "Step " << nStep++
246 << ": You can either select one of the following names suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700247 for (const auto& name : names) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700248 std::cerr << "> Index: " << count++ << std::endl
249 << ">> Suggested name: " << name.toUri() << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700250 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700251 std::cerr << "\nOr choose another trusted CA suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700252 for (const auto& redirect : redirects) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700253 std::cerr << "> Index: " << count++ << std::endl
254 << ">> Suggested CA: " << security::v2::extractIdentityFromCertName(redirect.getPrefix(-1)) << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700255 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700256 std::cerr << "Please type in the index of your choice:" << std::endl;
257 size_t index = 0;
tylerliufeabfdc2020-10-03 15:09:58 -0700258 try {
259 std::string input;
260 getline(std::cin, input);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700261 index = std::stoul(input);
tylerliufeabfdc2020-10-03 15:09:58 -0700262 }
263 catch (const std::exception& e) {
264 std::cerr << "Your input is Invalid. Exit" << std::endl;
265 exit(0);
266 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700267 if (index >= names.size() + redirects.size()) {
tylerliufeabfdc2020-10-03 15:09:58 -0700268 std::cerr << "Your input is not an existing index. Exit" << std::endl;
269 return;
270 }
271 if (index < names.size()) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700272 //names
273 std::cerr << "You selected name: " << names[index].toUri() << std::endl;
274 runNew(profile, names[index]);
275 }
276 else {
277 //redirects
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700278 auto redirectedCaFullName = redirects[index - names.size()];
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700279 auto redirectedCaName = security::v2::extractIdentityFromCertName(redirectedCaFullName.getPrefix(-1));
280 std::cerr << "You selected to be redirected to CA: " << redirectedCaName.toUri() << std::endl;
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700281 face.expressInterest(
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700282 *Requester::genCaProfileDiscoveryInterest(redirectedCaName),
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700283 [&](const Interest&, const Data& data) {
284 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
285 face.expressInterest(*fetchingInterest,
286 bind(&InfoCb, _2, redirectedCaFullName),
287 bind(&onNackCb),
288 bind(&timeoutCb));
289 },
290 bind(&onNackCb),
291 bind(&timeoutCb));
tylerliufeabfdc2020-10-03 15:09:58 -0700292 }
293}
294
295static void
296selectCaProfile(std::string configFilePath)
297{
298 RequesterCaCache caCache;
299 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700300 caCache.load(configFilePath);
tylerliufeabfdc2020-10-03 15:09:58 -0700301 }
302 catch (const std::exception& e) {
303 std::cerr << "Cannot load the configuration file: " << e.what() << std::endl;
304 exit(1);
305 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700306 size_t count = 0;
307 std::cerr << "***************************************\n"
308 << "Step " << nStep++ << ": CA SELECTION" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700309 for (auto item : caCache.m_caItems) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700310 std::cerr << "> Index: " << count++ << std::endl
311 << ">> CA prefix:" << item.m_caPrefix << std::endl
312 << ">> Introduction: " << item.m_caInfo << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700313 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700314 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 -0700315
Zhiyi Zhang36706832019-07-04 21:33:03 -0700316 std::string caIndexS, caIndexSLower;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700317 getline(std::cin, caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700318 caIndexSLower = caIndexS;
319 boost::algorithm::to_lower(caIndexSLower);
320 if (caIndexSLower == "none") {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700321 std::cerr << "\n***************************************\n"
322 << "Step " << nStep << ": ADD NEW CA\nPlease type in the CA's Name:" << std::endl;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700323 std::string expectedCAName;
324 getline(std::cin, expectedCAName);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700325 face.expressInterest(
326 *Requester::genCaProfileDiscoveryInterest(Name(expectedCAName)),
327 [&](const Interest&, const Data& data) {
328 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
329 face.expressInterest(*fetchingInterest,
330 bind(&InfoCb, _2, Name()),
331 bind(&onNackCb),
332 bind(&timeoutCb));
333 },
334 bind(&onNackCb),
335 bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700336 }
337 else {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700338 size_t caIndex;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700339 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700340 caIndex = std::stoul(caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700341 }
342 catch (const std::exception& e) {
343 std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl;
344 return;
345 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700346 if (caIndex >= count) {
Zhiyi Zhang36706832019-07-04 21:33:03 -0700347 std::cerr << "Your input is not an existing index. Exit" << std::endl;
348 return;
349 }
tylerliufeabfdc2020-10-03 15:09:58 -0700350 auto itemIterator = caCache.m_caItems.cbegin();
351 std::advance(itemIterator, caIndex);
352 auto targetCaItem = *itemIterator;
353 runProbe(targetCaItem);
354 }
355}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700356
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700357static void
358runProbe(CaProfile profile)
tylerliufeabfdc2020-10-03 15:09:58 -0700359{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700360 std::cerr << "\n***************************************\n"
361 << "Step " << nStep++ << ": Do you have the identity name already? Type in YES or NO" << std::endl;
362 bool validAnswer = false;
363 while (!validAnswer) {
364 std::string answer;
365 getline(std::cin, answer);
366 boost::algorithm::to_lower(answer);
367 if (answer == "yes") {
368 validAnswer = true;
369 std::cerr << "You answered YES" << std::endl;
370 std::cerr << "\n***************************************\n"
371 << "Step " << nStep++
372 << ": Please type in the full identity name you want to get (with CA prefix):" << std::endl;
373 std::string identityNameStr;
374 getline(std::cin, identityNameStr);
375 runNew(profile, Name(identityNameStr));
tylerliufeabfdc2020-10-03 15:09:58 -0700376 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700377 else if (answer == "no") {
378 validAnswer = true;
379 std::cerr << "You answered NO" << std::endl;
380 std::cerr << "\n***************************************\n"
381 << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl;
382 auto capturedParams = captureParams(profile.m_probeParameterKeys);
383 face.expressInterest(*Requester::genProbeInterest(profile, std::move(capturedParams)),
384 bind(&probeCb, _2, profile), bind(&onNackCb), bind(&timeoutCb));
385 }
386 else {
387 std::cerr << "Invalid answer. Type in YES or NO" << std::endl;
388 }
389 }
tylerliufeabfdc2020-10-03 15:09:58 -0700390}
391
392static void
393runNew(CaProfile profile, Name identityName)
394{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700395 int validityPeriod = captureValidityPeriod();
396 auto now = time::system_clock::now();
397 std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl;
398 requesterState = make_shared<RequesterState>(keyChain, profile, RequestType::NEW);
399 auto interest = Requester::genNewInterest(*requesterState, identityName, now, now + time::hours(validityPeriod));
400 if (interest != nullptr) {
401 face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb));
402 }
403 else {
404 std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl;
405 }
tylerliufeabfdc2020-10-03 15:09:58 -0700406}
407
408static void
409runChallenge(const std::string& challengeType)
410{
411 auto requirement = Requester::selectOrContinueChallenge(*requesterState, challengeType);
412 if (requirement.size() > 0) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700413 std::cerr << "\n***************************************\n"
414 << "Step " << nStep
415 << ": Please provide parameters used for Identity Verification Challenge" << std::endl;
416 captureParams(requirement);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700417 }
tylerliufeabfdc2020-10-03 15:09:58 -0700418 face.expressInterest(*Requester::genChallengeInterest(*requesterState, std::move(requirement)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700419 bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700420}
421
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700422static void
423handleSignal(const boost::system::error_code& error, int signalNum)
424{
425 if (error) {
426 return;
427 }
428 const char* signalName = ::strsignal(signalNum);
429 std::cerr << "Exiting on signal ";
430 if (signalName == nullptr) {
431 std::cerr << signalNum;
432 }
433 else {
434 std::cerr << signalName;
435 }
436 std::cerr << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700437 if (requesterState) {
438 Requester::endSession(*requesterState);
439 }
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700440 face.getIoService().stop();
tylerliufeabfdc2020-10-03 15:09:58 -0700441 exit(1);
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700442}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800443
444int
445main(int argc, char* argv[])
446{
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700447 boost::asio::signal_set terminateSignals(face.getIoService());
448 terminateSignals.add(SIGINT);
449 terminateSignals.add(SIGTERM);
450 terminateSignals.async_wait(handleSignal);
451
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800452 namespace po = boost::program_options;
453 std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
Zhiyi Zhang36706832019-07-04 21:33:03 -0700454 po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n");
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700455 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 -0800456 po::positional_options_description p;
457
458 po::variables_map vm;
459 try {
460 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
461 po::notify(vm);
462 }
463 catch (const std::exception& e) {
464 std::cerr << "ERROR: " << e.what() << std::endl;
465 return 1;
466 }
467 if (vm.count("help") != 0) {
468 std::cerr << description << std::endl;
469 return 0;
470 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700471 selectCaProfile(configFilePath);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800472 face.processEvents();
473 return 0;
474}
475
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700476} // namespace ndncert
477} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800478
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700479int
480main(int argc, char* argv[])
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800481{
482 return ndn::ndncert::main(argc, argv);
483}