Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 2 | /** |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 21 | #include "challenge-module.hpp" |
| 22 | #include "client-module.hpp" |
| 23 | #include "protocol-detail/info.hpp" |
| 24 | #include <ndn-cxx/security/verification-helpers.hpp> |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 25 | #include <boost/asio.hpp> |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 26 | #include <boost/program_options/options_description.hpp> |
| 27 | #include <boost/program_options/parsers.hpp> |
| 28 | #include <boost/program_options/variables_map.hpp> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 29 | #include <iostream> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace ndncert { |
| 34 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 35 | static void |
| 36 | startApplication(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 37 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 38 | int nStep; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 39 | Face face; |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 40 | security::v2::KeyChain keyChain; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 41 | std::string challengeType; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 42 | int validityPeriod = -1; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 43 | ClientModule client(keyChain); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 44 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 45 | static void |
| 46 | captureParams(std::vector<std::tuple<std::string, std::string>>& requirement) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 47 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | std::list<std::string> results; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 49 | for (auto& item : requirement) { |
| 50 | std::cerr << std::get<1>(item) << std::endl; |
| 51 | std::string captured; |
| 52 | getline(std::cin, captured); |
| 53 | std::get<1>(item) = captured; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 54 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 56 | for (const auto& item : requirement) { |
| 57 | std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 58 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 60 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 61 | static std::vector<std::tuple<std::string, std::string>> |
| 62 | captureParams(const std::list<std::string>& requirement) |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 63 | { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 64 | std::vector<std::tuple<std::string, std::string>> results; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 65 | for (const auto& item : requirement) { |
| 66 | std::cerr << "Please provide the argument: " << item << " : " << std::endl; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 67 | std::string captured; |
| 68 | getline(std::cin, captured); |
| 69 | results.push_back(std::make_tuple(item, captured)); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 70 | } |
| 71 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 72 | for (const auto& item : results) { |
| 73 | std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 74 | } |
| 75 | return results; |
| 76 | } |
| 77 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 78 | static void |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 79 | captureValidityPeriod() |
| 80 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 81 | if (validityPeriod > 0) { |
| 82 | return; |
| 83 | } |
| 84 | std::cerr << "Step " << nStep++ |
| 85 | << ": Please type in your expected validity period of your certificate." |
| 86 | << " Type the number of hours (168 for week, 730 for month, 8760 for year)." |
| 87 | << " The CA may reject your application if your expected period is too long." << std::endl; |
| 88 | std::string periodStr = ""; |
| 89 | getline(std::cin, periodStr); |
| 90 | try { |
| 91 | validityPeriod = std::stoi(periodStr); |
| 92 | } |
| 93 | catch (const std::exception& e) { |
| 94 | validityPeriod = -1; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | static void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 99 | onNackCb() |
| 100 | { |
| 101 | std::cerr << "Got NACK\n"; |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | timeoutCb() |
| 106 | { |
| 107 | std::cerr << "Interest sent time out\n"; |
| 108 | } |
| 109 | |
| 110 | static void |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 111 | certFetchCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 112 | { |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 113 | client.onCertFetchResponse(reply); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | std::cerr << "Step " << nStep++ |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 115 | << ": DONE! Certificate has already been installed to local keychain\n" |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 116 | << "Certificate Name: " << reply.getName().toUri() << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void |
| 120 | challengeCb(const Data& reply) |
| 121 | { |
| 122 | client.onChallengeResponse(reply); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 123 | if (client.getApplicationStatus() == Status::SUCCESS) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 124 | std::cerr << "DONE! Certificate has already been issued \n"; |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 125 | face.expressInterest(*client.generateCertFetchInterest(), bind(&certFetchCb, _2), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 126 | bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | 4d89fe0 | 2017-04-28 18:51:51 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 130 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 131 | auto requirement = challenge->getRequestedParameterList(client.getApplicationStatus(), |
| 132 | client.getChallengeStatus()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 133 | if (requirement.size() > 0) { |
Zhiyi Zhang | 916ba2d | 2018-02-01 18:16:27 -0800 | [diff] [blame] | 134 | std::cerr << "Step " << nStep++ << ": Please satisfy following instruction(s)\n"; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 135 | captureParams(requirement); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 136 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 137 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestTLV(client.getApplicationStatus(), |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 138 | client.getChallengeStatus(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 139 | std::move(requirement))), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 140 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 142 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | static void |
| 144 | newCb(const Data& reply) |
| 145 | { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 146 | int challengeIndex = 0; |
tylerliu | 0e176c3 | 2020-09-29 11:39:46 -0700 | [diff] [blame] | 147 | auto challengeList = client.onNewRenewRevokeResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 148 | if (challengeList.size() < 1) { |
| 149 | std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl; |
| 150 | return; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 151 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 152 | else if (challengeList.size() > 1) { |
| 153 | int count = 0; |
| 154 | std::string choice = ""; |
| 155 | std::cerr << "Step " << nStep++ << ": Please type in the challenge index that you want to perform\n"; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 156 | count = 0; |
| 157 | for (auto item : challengeList) { |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 158 | std::cerr << "\t" << count++ << " : " << item << std::endl; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 159 | } |
| 160 | getline(std::cin, choice); |
| 161 | try { |
| 162 | challengeIndex = std::stoi(choice); |
| 163 | } |
| 164 | catch (const std::exception& e) { |
| 165 | challengeIndex = -1; |
| 166 | } |
| 167 | if (challengeIndex < 0 || challengeIndex >= count) { |
| 168 | std::cerr << "Your input index is out of range. Exit." << std::endl; |
| 169 | return; |
| 170 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 171 | } |
| 172 | auto it = challengeList.begin(); |
| 173 | std::advance(it, challengeIndex); |
| 174 | unique_ptr<ChallengeModule> challenge = ChallengeModule::createChallengeModule(*it); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 175 | if (challenge != nullptr) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 176 | challengeType = *it; |
| 177 | std::cerr << "The challenge has been selected: " << challengeType << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 178 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 179 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 180 | std::cerr << "Error. Cannot load selected Challenge Module. Exit." << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 183 | auto requirement = challenge->getRequestedParameterList(client.getApplicationStatus(), |
| 184 | client.getChallengeStatus()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 185 | if (requirement.size() > 0) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 186 | std::cerr << "Step " << nStep++ << ": Please provide parameters used for Identity Verification Challenge\n"; |
| 187 | captureParams(requirement); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 188 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 189 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestTLV(client.getApplicationStatus(), |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 190 | client.getChallengeStatus(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 191 | std::move(requirement))), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 192 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 193 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 194 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 195 | static void |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 196 | InfoCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 197 | { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 198 | const Block& contentBlock = reply.getContent(); |
| 199 | |
| 200 | if (!client.verifyInfoResponse(reply)) { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 201 | std::cerr << "The fetched CA information cannot be trusted because its integrity is broken" << std::endl; |
| 202 | return; |
| 203 | } |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 204 | auto caItem = INFO::decodeDataContentToCaProfile(contentBlock); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 205 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 206 | std::cerr << "Will use a new trust anchor, please double check the identity info: \n" |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 207 | << "This trust anchor information is signed by " << reply.getSignature().getKeyLocator() |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 208 | << std::endl |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 209 | << "The certificate is " << *caItem.m_cert << std::endl |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 210 | << "Do you trust the information? Type in YES or NO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 211 | |
| 212 | std::string answer; |
| 213 | getline(std::cin, answer); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 214 | boost::algorithm::to_lower(answer); |
| 215 | if (answer == "yes") { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 216 | std::cerr << "You answered YES: new CA will be used" << std::endl; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 217 | client.addCaFromInfoResponse(reply); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 218 | // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 219 | startApplication(); |
| 220 | } |
| 221 | else { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 222 | std::cerr << "You answered NO: new CA will not be used" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | probeCb(const Data& reply) |
| 229 | { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 230 | client.onProbeResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 231 | captureValidityPeriod(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 232 | if (validityPeriod <= 0) { |
| 233 | std::cerr << "Invalid period time. Exit." << std::endl; |
| 234 | return; |
| 235 | } |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 236 | auto now = time::system_clock::now(); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 237 | std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl; |
Zhiyi Zhang | b8bbc64 | 2020-09-29 14:08:26 -0700 | [diff] [blame] | 238 | auto interest = client.generateNewInterest(now, now + time::hours(validityPeriod), Name()); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 239 | if (interest != nullptr) { |
| 240 | face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
| 241 | } |
| 242 | else { |
| 243 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 244 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void |
| 248 | startApplication() |
| 249 | { |
| 250 | nStep = 0; |
| 251 | auto caList = client.getClientConf().m_caItems; |
| 252 | int count = 0; |
| 253 | for (auto item : caList) { |
| 254 | std::cerr << "***************************************\n" |
| 255 | << "Index: " << count++ << "\n" |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 256 | << "CA prefix:" << item.m_caPrefix << "\n" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 257 | << "Introduction: " << item.m_caInfo << "\n" |
| 258 | << "***************************************\n"; |
| 259 | } |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 260 | std::vector<CaConfigItem> caVector{std::begin(caList), std::end(caList)}; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 261 | std::cerr << "Step " |
| 262 | << nStep++ << ": Please type in the CA INDEX that you want to apply" |
| 263 | << " or type in NONE if your expected CA is not in the list\n"; |
| 264 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 265 | std::string caIndexS, caIndexSLower; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 266 | getline(std::cin, caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 267 | caIndexSLower = caIndexS; |
| 268 | boost::algorithm::to_lower(caIndexSLower); |
| 269 | if (caIndexSLower == "none") { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 270 | std::cerr << "Step " << nStep << ": Please type in the CA Name\n"; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 271 | std::string expectedCAName; |
| 272 | getline(std::cin, expectedCAName); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 273 | face.expressInterest(*client.generateInfoInterest(Name(expectedCAName)), |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 274 | bind(&InfoCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 275 | } |
| 276 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 277 | int caIndex; |
| 278 | try { |
| 279 | caIndex = std::stoi(caIndexS); |
| 280 | } |
| 281 | catch (const std::exception& e) { |
| 282 | std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl; |
| 283 | return; |
| 284 | } |
| 285 | if (caIndex < 0 || caIndex >= count) { |
| 286 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 287 | return; |
| 288 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 289 | auto targetCaItem = caVector[caIndex]; |
| 290 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 291 | if (!targetCaItem.m_probeParameterKeys.empty()) { |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 292 | std::cerr << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl; |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame^] | 293 | auto capturedParams = captureParams(targetCaItem.m_probeParameterKeys); |
| 294 | face.expressInterest(*client.generateProbeInterest(targetCaItem, std::move(capturedParams)), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 295 | bind(&probeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 296 | } |
| 297 | else { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 298 | std::cerr << "Step " << nStep++ << ": Please type in the full identity name you want to get (with CA prefix)\n"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 299 | std::string identityNameStr; |
| 300 | getline(std::cin, identityNameStr); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 301 | captureValidityPeriod(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 302 | if (validityPeriod <= 0) { |
| 303 | std::cerr << "Invalid period time. Exit." << std::endl; |
| 304 | return; |
| 305 | } |
| 306 | Name idName(identityNameStr); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 307 | std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl; |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 308 | auto now = time::system_clock::now(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 309 | auto interest = client.generateNewInterest(now, now + time::hours(validityPeriod), idName); |
| 310 | if (interest != nullptr) { |
| 311 | face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
| 312 | } |
| 313 | else { |
| 314 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 315 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 320 | static void |
| 321 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 322 | { |
| 323 | if (error) { |
| 324 | return; |
| 325 | } |
| 326 | const char* signalName = ::strsignal(signalNum); |
| 327 | std::cerr << "Exiting on signal "; |
| 328 | if (signalName == nullptr) { |
| 329 | std::cerr << signalNum; |
| 330 | } |
| 331 | else { |
| 332 | std::cerr << signalName; |
| 333 | } |
| 334 | std::cerr << std::endl; |
| 335 | client.endSession(); |
| 336 | face.getIoService().stop(); |
| 337 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 338 | |
| 339 | int |
| 340 | main(int argc, char* argv[]) |
| 341 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 342 | boost::asio::signal_set terminateSignals(face.getIoService()); |
| 343 | terminateSignals.add(SIGINT); |
| 344 | terminateSignals.add(SIGTERM); |
| 345 | terminateSignals.async_wait(handleSignal); |
| 346 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 347 | namespace po = boost::program_options; |
| 348 | std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf"; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 349 | po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n"); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 350 | description.add_options()("help,h", "produce help message")("config-file,c", po::value<std::string>(&configFilePath), "configuration file name")("validity-period,v", po::value<int>(&validityPeriod)->default_value(-1), |
| 351 | "desired validity period (hours) of the certificate being requested"); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 352 | po::positional_options_description p; |
| 353 | |
| 354 | po::variables_map vm; |
| 355 | try { |
| 356 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 357 | po::notify(vm); |
| 358 | } |
| 359 | catch (const std::exception& e) { |
| 360 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 361 | return 1; |
| 362 | } |
| 363 | if (vm.count("help") != 0) { |
| 364 | std::cerr << description << std::endl; |
| 365 | return 0; |
| 366 | } |
Zhiyi Zhang | d8993b9 | 2019-07-04 21:58:10 -0700 | [diff] [blame] | 367 | try { |
| 368 | client.getClientConf().load(configFilePath); |
| 369 | } |
| 370 | catch (const std::exception& e) { |
| 371 | std::cerr << "Cannot load the configuration file: " << e.what() << std::endl; |
| 372 | return 1; |
| 373 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 374 | startApplication(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 375 | face.processEvents(); |
| 376 | return 0; |
| 377 | } |
| 378 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 379 | } // namespace ndncert |
| 380 | } // namespace ndn |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 381 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 382 | int |
| 383 | main(int argc, char* argv[]) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 384 | { |
| 385 | return ndn::ndncert::main(argc, argv); |
| 386 | } |