blob: 96064b601c181faa6e1d8de860eb75d7e8489a5a [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 Zhangc87d52b2020-09-28 22:07:18 -070021#include "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 Zhangad9e04f2020-03-27 12:04:31 -070024#include <boost/asio.hpp>
Davide Pesaventob48bbda2020-07-27 19:41:37 -040025#include <boost/program_options/options_description.hpp>
26#include <boost/program_options/parsers.hpp>
27#include <boost/program_options/variables_map.hpp>
Zhiyi Zhang48f23782020-09-28 12:11:24 -070028#include <iostream>
Zhiyi Zhang837406d2020-10-05 22:01:31 -070029#include <ndn-cxx/security/verification-helpers.hpp>
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
278 auto redirectedCaName = redirects[index - names.size()];
279 std::cerr << "You selected redirects with certificate: " << redirectedCaName.getPrefix(-1).toUri() << std::endl;
280 face.expressInterest(*Requester::genCaProfileInterest(redirects[index - names.size()]),
281 bind(&InfoCb, _2, redirectedCaName), bind(&onNackCb), bind(&timeoutCb));
tylerliufeabfdc2020-10-03 15:09:58 -0700282 }
283}
284
285static void
286selectCaProfile(std::string configFilePath)
287{
288 RequesterCaCache caCache;
289 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700290 caCache.load(configFilePath);
tylerliufeabfdc2020-10-03 15:09:58 -0700291 }
292 catch (const std::exception& e) {
293 std::cerr << "Cannot load the configuration file: " << e.what() << std::endl;
294 exit(1);
295 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700296 size_t count = 0;
297 std::cerr << "***************************************\n"
298 << "Step " << nStep++ << ": CA SELECTION" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700299 for (auto item : caCache.m_caItems) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700300 std::cerr << "> Index: " << count++ << std::endl
301 << ">> CA prefix:" << item.m_caPrefix << std::endl
302 << ">> Introduction: " << item.m_caInfo << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700303 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700304 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 -0700305
Zhiyi Zhang36706832019-07-04 21:33:03 -0700306 std::string caIndexS, caIndexSLower;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700307 getline(std::cin, caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700308 caIndexSLower = caIndexS;
309 boost::algorithm::to_lower(caIndexSLower);
310 if (caIndexSLower == "none") {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700311 std::cerr << "\n***************************************\n"
312 << "Step " << nStep << ": ADD NEW CA\nPlease type in the CA's Name:" << std::endl;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700313 std::string expectedCAName;
314 getline(std::cin, expectedCAName);
tylerliufeabfdc2020-10-03 15:09:58 -0700315 face.expressInterest(*Requester::genCaProfileInterest(Name(expectedCAName)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700316 bind(&InfoCb, _2, Name()), bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700317 }
318 else {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700319 size_t caIndex;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700320 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700321 caIndex = std::stoul(caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700322 }
323 catch (const std::exception& e) {
324 std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl;
325 return;
326 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700327 if (caIndex >= count) {
Zhiyi Zhang36706832019-07-04 21:33:03 -0700328 std::cerr << "Your input is not an existing index. Exit" << std::endl;
329 return;
330 }
tylerliufeabfdc2020-10-03 15:09:58 -0700331 auto itemIterator = caCache.m_caItems.cbegin();
332 std::advance(itemIterator, caIndex);
333 auto targetCaItem = *itemIterator;
334 runProbe(targetCaItem);
335 }
336}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700337
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700338static void
339runProbe(CaProfile profile)
tylerliufeabfdc2020-10-03 15:09:58 -0700340{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700341 std::cerr << "\n***************************************\n"
342 << "Step " << nStep++ << ": Do you have the identity name already? Type in YES or NO" << std::endl;
343 bool validAnswer = false;
344 while (!validAnswer) {
345 std::string answer;
346 getline(std::cin, answer);
347 boost::algorithm::to_lower(answer);
348 if (answer == "yes") {
349 validAnswer = true;
350 std::cerr << "You answered YES" << std::endl;
351 std::cerr << "\n***************************************\n"
352 << "Step " << nStep++
353 << ": Please type in the full identity name you want to get (with CA prefix):" << std::endl;
354 std::string identityNameStr;
355 getline(std::cin, identityNameStr);
356 runNew(profile, Name(identityNameStr));
tylerliufeabfdc2020-10-03 15:09:58 -0700357 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700358 else if (answer == "no") {
359 validAnswer = true;
360 std::cerr << "You answered NO" << std::endl;
361 std::cerr << "\n***************************************\n"
362 << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl;
363 auto capturedParams = captureParams(profile.m_probeParameterKeys);
364 face.expressInterest(*Requester::genProbeInterest(profile, std::move(capturedParams)),
365 bind(&probeCb, _2, profile), bind(&onNackCb), bind(&timeoutCb));
366 }
367 else {
368 std::cerr << "Invalid answer. Type in YES or NO" << std::endl;
369 }
370 }
tylerliufeabfdc2020-10-03 15:09:58 -0700371}
372
373static void
374runNew(CaProfile profile, Name identityName)
375{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700376 int validityPeriod = captureValidityPeriod();
377 auto now = time::system_clock::now();
378 std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl;
379 requesterState = make_shared<RequesterState>(keyChain, profile, RequestType::NEW);
380 auto interest = Requester::genNewInterest(*requesterState, identityName, now, now + time::hours(validityPeriod));
381 if (interest != nullptr) {
382 face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb));
383 }
384 else {
385 std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl;
386 }
tylerliufeabfdc2020-10-03 15:09:58 -0700387}
388
389static void
390runChallenge(const std::string& challengeType)
391{
392 auto requirement = Requester::selectOrContinueChallenge(*requesterState, challengeType);
393 if (requirement.size() > 0) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700394 std::cerr << "\n***************************************\n"
395 << "Step " << nStep
396 << ": Please provide parameters used for Identity Verification Challenge" << std::endl;
397 captureParams(requirement);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700398 }
tylerliufeabfdc2020-10-03 15:09:58 -0700399 face.expressInterest(*Requester::genChallengeInterest(*requesterState, std::move(requirement)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700400 bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700401}
402
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700403static void
404handleSignal(const boost::system::error_code& error, int signalNum)
405{
406 if (error) {
407 return;
408 }
409 const char* signalName = ::strsignal(signalNum);
410 std::cerr << "Exiting on signal ";
411 if (signalName == nullptr) {
412 std::cerr << signalNum;
413 }
414 else {
415 std::cerr << signalName;
416 }
417 std::cerr << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700418 if (requesterState) {
419 Requester::endSession(*requesterState);
420 }
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700421 face.getIoService().stop();
tylerliufeabfdc2020-10-03 15:09:58 -0700422 exit(1);
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700423}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800424
425int
426main(int argc, char* argv[])
427{
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700428 boost::asio::signal_set terminateSignals(face.getIoService());
429 terminateSignals.add(SIGINT);
430 terminateSignals.add(SIGTERM);
431 terminateSignals.async_wait(handleSignal);
432
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800433 namespace po = boost::program_options;
434 std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
Zhiyi Zhang36706832019-07-04 21:33:03 -0700435 po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n");
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700436 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 -0800437 po::positional_options_description p;
438
439 po::variables_map vm;
440 try {
441 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
442 po::notify(vm);
443 }
444 catch (const std::exception& e) {
445 std::cerr << "ERROR: " << e.what() << std::endl;
446 return 1;
447 }
448 if (vm.count("help") != 0) {
449 std::cerr << description << std::endl;
450 return 0;
451 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700452 selectCaProfile(configFilePath);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800453 face.processEvents();
454 return 0;
455}
456
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700457} // namespace ndncert
458} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800459
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700460int
461main(int argc, char* argv[])
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800462{
463 return ndn::ndncert::main(argc, argv);
464}