blob: d94885420ade1c9dc2447d6719677e745d85ea31 [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;
tylerliua7bea662020-10-08 18:51:02 -070046security::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 Zhang6bb1d082020-10-08 14:25:21 -0700216 std::cerr << "\n***************************************\n"
217 << "Step " << nStep++
218 << ": Will use a new trust anchor, please double check the identity info:" << std::endl
219 << "> New CA name: " << profile->m_caPrefix.toUri() << std::endl
tylerliua7bea662020-10-08 18:51:02 -0700220 << "> This trust anchor information is signed by: " << reply.getSignatureInfo().getKeyLocator() << std::endl
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700221 << "> The certificate: " << profile->m_cert << std::endl
Davide Pesaventob48bbda2020-07-27 19:41:37 -0400222 << "Do you trust the information? Type in YES or NO" << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700223
224 std::string answer;
225 getline(std::cin, answer);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700226 boost::algorithm::to_lower(answer);
227 if (answer == "yes") {
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700228 std::cerr << "You answered YES: new CA " << profile->m_caPrefix.toUri() << " will be used" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700229 runProbe(*profile);
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700230 // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700231 }
232 else {
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700233 std::cerr << "You answered NO: new CA " << profile->m_caPrefix.toUri() << " will not be used" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700234 exit(0);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700235 }
236}
237
238static void
tylerliufeabfdc2020-10-03 15:09:58 -0700239probeCb(const Data& reply, CaProfile profile)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700240{
tylerliufeabfdc2020-10-03 15:09:58 -0700241 std::vector<Name> names;
242 std::vector<Name> redirects;
243 Requester::onProbeResponse(reply, profile, names, redirects);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700244 size_t count = 0;
245 std::cerr << "\n***************************************\n"
246 << "Step " << nStep++
247 << ": You can either select one of the following names suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700248 for (const auto& name : names) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700249 std::cerr << "> Index: " << count++ << std::endl
250 << ">> Suggested name: " << name.toUri() << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700251 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700252 std::cerr << "\nOr choose another trusted CA suggested by the CA: " << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700253 for (const auto& redirect : redirects) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700254 std::cerr << "> Index: " << count++ << std::endl
tylerliua7bea662020-10-08 18:51:02 -0700255 << ">> Suggested CA: " << security::extractIdentityFromCertName(redirect.getPrefix(-1)) << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700256 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700257 std::cerr << "Please type in the index of your choice:" << std::endl;
258 size_t index = 0;
tylerliufeabfdc2020-10-03 15:09:58 -0700259 try {
260 std::string input;
261 getline(std::cin, input);
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700262 index = std::stoul(input);
tylerliufeabfdc2020-10-03 15:09:58 -0700263 }
264 catch (const std::exception& e) {
265 std::cerr << "Your input is Invalid. Exit" << std::endl;
266 exit(0);
267 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700268 if (index >= names.size() + redirects.size()) {
tylerliufeabfdc2020-10-03 15:09:58 -0700269 std::cerr << "Your input is not an existing index. Exit" << std::endl;
270 return;
271 }
272 if (index < names.size()) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700273 //names
274 std::cerr << "You selected name: " << names[index].toUri() << std::endl;
275 runNew(profile, names[index]);
276 }
277 else {
278 //redirects
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700279 auto redirectedCaFullName = redirects[index - names.size()];
tylerliua7bea662020-10-08 18:51:02 -0700280 auto redirectedCaName = security::extractIdentityFromCertName(redirectedCaFullName.getPrefix(-1));
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700281 std::cerr << "You selected to be redirected to CA: " << redirectedCaName.toUri() << std::endl;
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700282 face.expressInterest(
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700283 *Requester::genCaProfileDiscoveryInterest(redirectedCaName),
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700284 [&](const Interest&, const Data& data) {
285 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
286 face.expressInterest(*fetchingInterest,
287 bind(&InfoCb, _2, redirectedCaFullName),
288 bind(&onNackCb),
289 bind(&timeoutCb));
290 },
291 bind(&onNackCb),
292 bind(&timeoutCb));
tylerliufeabfdc2020-10-03 15:09:58 -0700293 }
294}
295
296static void
297selectCaProfile(std::string configFilePath)
298{
299 RequesterCaCache caCache;
300 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700301 caCache.load(configFilePath);
tylerliufeabfdc2020-10-03 15:09:58 -0700302 }
303 catch (const std::exception& e) {
304 std::cerr << "Cannot load the configuration file: " << e.what() << std::endl;
305 exit(1);
306 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700307 size_t count = 0;
308 std::cerr << "***************************************\n"
309 << "Step " << nStep++ << ": CA SELECTION" << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700310 for (auto item : caCache.m_caItems) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700311 std::cerr << "> Index: " << count++ << std::endl
312 << ">> CA prefix:" << item.m_caPrefix << std::endl
313 << ">> Introduction: " << item.m_caInfo << std::endl;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700314 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700315 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 -0700316
Zhiyi Zhang36706832019-07-04 21:33:03 -0700317 std::string caIndexS, caIndexSLower;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700318 getline(std::cin, caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700319 caIndexSLower = caIndexS;
320 boost::algorithm::to_lower(caIndexSLower);
321 if (caIndexSLower == "none") {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700322 std::cerr << "\n***************************************\n"
323 << "Step " << nStep << ": ADD NEW CA\nPlease type in the CA's Name:" << std::endl;
Zhiyi Zhangcaab5462019-10-18 13:41:02 -0700324 std::string expectedCAName;
325 getline(std::cin, expectedCAName);
Zhiyi Zhangfbcab842020-10-07 15:17:13 -0700326 face.expressInterest(
327 *Requester::genCaProfileDiscoveryInterest(Name(expectedCAName)),
328 [&](const Interest&, const Data& data) {
329 auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data);
330 face.expressInterest(*fetchingInterest,
331 bind(&InfoCb, _2, Name()),
332 bind(&onNackCb),
333 bind(&timeoutCb));
334 },
335 bind(&onNackCb),
336 bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700337 }
338 else {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700339 size_t caIndex;
Zhiyi Zhang36706832019-07-04 21:33:03 -0700340 try {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700341 caIndex = std::stoul(caIndexS);
Zhiyi Zhang36706832019-07-04 21:33:03 -0700342 }
343 catch (const std::exception& e) {
344 std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl;
345 return;
346 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700347 if (caIndex >= count) {
Zhiyi Zhang36706832019-07-04 21:33:03 -0700348 std::cerr << "Your input is not an existing index. Exit" << std::endl;
349 return;
350 }
tylerliufeabfdc2020-10-03 15:09:58 -0700351 auto itemIterator = caCache.m_caItems.cbegin();
352 std::advance(itemIterator, caIndex);
353 auto targetCaItem = *itemIterator;
354 runProbe(targetCaItem);
355 }
356}
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700357
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700358static void
359runProbe(CaProfile profile)
tylerliufeabfdc2020-10-03 15:09:58 -0700360{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700361 std::cerr << "\n***************************************\n"
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700362 << "Step " << nStep++
363 << ": Do you know your identity name to be certified by CA "
364 << profile.m_caPrefix.toUri()
365 << " already? Type in YES or NO" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700366 bool validAnswer = false;
367 while (!validAnswer) {
368 std::string answer;
369 getline(std::cin, answer);
370 boost::algorithm::to_lower(answer);
371 if (answer == "yes") {
372 validAnswer = true;
373 std::cerr << "You answered YES" << std::endl;
374 std::cerr << "\n***************************************\n"
375 << "Step " << nStep++
Zhiyi Zhang6bb1d082020-10-08 14:25:21 -0700376 << ": Please type in the full identity name you want to get (with CA prefix "
377 << profile.m_caPrefix.toUri()
378 << "):" << std::endl;
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700379 std::string identityNameStr;
380 getline(std::cin, identityNameStr);
381 runNew(profile, Name(identityNameStr));
tylerliufeabfdc2020-10-03 15:09:58 -0700382 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700383 else if (answer == "no") {
384 validAnswer = true;
385 std::cerr << "You answered NO" << std::endl;
386 std::cerr << "\n***************************************\n"
387 << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl;
388 auto capturedParams = captureParams(profile.m_probeParameterKeys);
389 face.expressInterest(*Requester::genProbeInterest(profile, std::move(capturedParams)),
390 bind(&probeCb, _2, profile), bind(&onNackCb), bind(&timeoutCb));
391 }
392 else {
393 std::cerr << "Invalid answer. Type in YES or NO" << std::endl;
394 }
395 }
tylerliufeabfdc2020-10-03 15:09:58 -0700396}
397
398static void
399runNew(CaProfile profile, Name identityName)
400{
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700401 int validityPeriod = captureValidityPeriod();
402 auto now = time::system_clock::now();
403 std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl;
404 requesterState = make_shared<RequesterState>(keyChain, profile, RequestType::NEW);
405 auto interest = Requester::genNewInterest(*requesterState, identityName, now, now + time::hours(validityPeriod));
406 if (interest != nullptr) {
407 face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb));
408 }
409 else {
410 std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl;
411 }
tylerliufeabfdc2020-10-03 15:09:58 -0700412}
413
414static void
415runChallenge(const std::string& challengeType)
416{
417 auto requirement = Requester::selectOrContinueChallenge(*requesterState, challengeType);
418 if (requirement.size() > 0) {
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700419 std::cerr << "\n***************************************\n"
420 << "Step " << nStep
421 << ": Please provide parameters used for Identity Verification Challenge" << std::endl;
422 captureParams(requirement);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700423 }
tylerliufeabfdc2020-10-03 15:09:58 -0700424 face.expressInterest(*Requester::genChallengeInterest(*requesterState, std::move(requirement)),
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700425 bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700426}
427
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700428static void
429handleSignal(const boost::system::error_code& error, int signalNum)
430{
431 if (error) {
432 return;
433 }
434 const char* signalName = ::strsignal(signalNum);
435 std::cerr << "Exiting on signal ";
436 if (signalName == nullptr) {
437 std::cerr << signalNum;
438 }
439 else {
440 std::cerr << signalName;
441 }
442 std::cerr << std::endl;
tylerliufeabfdc2020-10-03 15:09:58 -0700443 if (requesterState) {
444 Requester::endSession(*requesterState);
445 }
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700446 face.getIoService().stop();
tylerliufeabfdc2020-10-03 15:09:58 -0700447 exit(1);
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700448}
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800449
450int
451main(int argc, char* argv[])
452{
Zhiyi Zhangad9e04f2020-03-27 12:04:31 -0700453 boost::asio::signal_set terminateSignals(face.getIoService());
454 terminateSignals.add(SIGINT);
455 terminateSignals.add(SIGTERM);
456 terminateSignals.async_wait(handleSignal);
457
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800458 namespace po = boost::program_options;
459 std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
Zhiyi Zhang36706832019-07-04 21:33:03 -0700460 po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n");
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700461 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 -0800462 po::positional_options_description p;
463
464 po::variables_map vm;
465 try {
466 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
467 po::notify(vm);
468 }
469 catch (const std::exception& e) {
470 std::cerr << "ERROR: " << e.what() << std::endl;
471 return 1;
472 }
473 if (vm.count("help") != 0) {
474 std::cerr << description << std::endl;
475 return 0;
476 }
Zhiyi Zhang837406d2020-10-05 22:01:31 -0700477 selectCaProfile(configFilePath);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800478 face.processEvents();
479 return 0;
480}
481
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700482} // namespace ndncert
483} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800484
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700485int
486main(int argc, char* argv[])
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800487{
488 return ndn::ndncert::main(argc, argv);
489}