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 | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 61 | static std::list<std::string> |
| 62 | captureParams(const std::vector<std::string>& requirement) |
| 63 | { |
| 64 | std::list<std::string> results; |
| 65 | for (const auto& item : requirement) { |
| 66 | std::cerr << "Please provide the argument: " << item << " : " << std::endl; |
| 67 | std::string tempParam; |
| 68 | getline(std::cin, tempParam); |
| 69 | results.push_back(tempParam); |
| 70 | } |
| 71 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
| 72 | auto it1 = results.begin(); |
| 73 | auto it2 = requirement.begin(); |
| 74 | for (; it1 != results.end() && it2 != requirement.end(); it1++, it2++) { |
| 75 | std::cerr << *it2 << " : " << *it1 << std::endl; |
| 76 | } |
| 77 | return results; |
| 78 | } |
| 79 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 80 | static void |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 81 | captureValidityPeriod() |
| 82 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 83 | if (validityPeriod > 0) { |
| 84 | return; |
| 85 | } |
| 86 | std::cerr << "Step " << nStep++ |
| 87 | << ": Please type in your expected validity period of your certificate." |
| 88 | << " Type the number of hours (168 for week, 730 for month, 8760 for year)." |
| 89 | << " The CA may reject your application if your expected period is too long." << std::endl; |
| 90 | std::string periodStr = ""; |
| 91 | getline(std::cin, periodStr); |
| 92 | try { |
| 93 | validityPeriod = std::stoi(periodStr); |
| 94 | } |
| 95 | catch (const std::exception& e) { |
| 96 | validityPeriod = -1; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | static void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 101 | onNackCb() |
| 102 | { |
| 103 | std::cerr << "Got NACK\n"; |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | timeoutCb() |
| 108 | { |
| 109 | std::cerr << "Interest sent time out\n"; |
| 110 | } |
| 111 | |
| 112 | static void |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 113 | certFetchCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 114 | { |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 115 | client.onCertFetchResponse(reply); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 116 | std::cerr << "Step " << nStep++ |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 117 | << ": DONE! Certificate has already been installed to local keychain\n" |
Zhiyi Zhang | ef6b36a | 2020-09-22 21:20:59 -0700 | [diff] [blame] | 118 | << "Certificate Name: " << reply.getName().toUri() << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
| 122 | challengeCb(const Data& reply) |
| 123 | { |
| 124 | client.onChallengeResponse(reply); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 125 | if (client.getApplicationStatus() == Status::SUCCESS) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 126 | std::cerr << "DONE! Certificate has already been issued \n"; |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 127 | face.expressInterest(*client.generateCertFetchInterest(), bind(&certFetchCb, _2), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 128 | bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | 4d89fe0 | 2017-04-28 18:51:51 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 132 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 133 | auto requirement = challenge->getRequestedParameterList(client.getApplicationStatus(), |
| 134 | client.getChallengeStatus()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | if (requirement.size() > 0) { |
Zhiyi Zhang | 916ba2d | 2018-02-01 18:16:27 -0800 | [diff] [blame] | 136 | std::cerr << "Step " << nStep++ << ": Please satisfy following instruction(s)\n"; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 137 | captureParams(requirement); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 138 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 139 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestTLV(client.getApplicationStatus(), |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 140 | client.getChallengeStatus(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 141 | std::move(requirement))), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 142 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 144 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 145 | static void |
| 146 | newCb(const Data& reply) |
| 147 | { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 148 | int challengeIndex = 0; |
tylerliu | 0e176c3 | 2020-09-29 11:39:46 -0700 | [diff] [blame] | 149 | auto challengeList = client.onNewRenewRevokeResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 150 | if (challengeList.size() < 1) { |
| 151 | std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl; |
| 152 | return; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 153 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 154 | else if (challengeList.size() > 1) { |
| 155 | int count = 0; |
| 156 | std::string choice = ""; |
| 157 | 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] | 158 | count = 0; |
| 159 | for (auto item : challengeList) { |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 160 | std::cerr << "\t" << count++ << " : " << item << std::endl; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 161 | } |
| 162 | getline(std::cin, choice); |
| 163 | try { |
| 164 | challengeIndex = std::stoi(choice); |
| 165 | } |
| 166 | catch (const std::exception& e) { |
| 167 | challengeIndex = -1; |
| 168 | } |
| 169 | if (challengeIndex < 0 || challengeIndex >= count) { |
| 170 | std::cerr << "Your input index is out of range. Exit." << std::endl; |
| 171 | return; |
| 172 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 173 | } |
| 174 | auto it = challengeList.begin(); |
| 175 | std::advance(it, challengeIndex); |
| 176 | unique_ptr<ChallengeModule> challenge = ChallengeModule::createChallengeModule(*it); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 177 | if (challenge != nullptr) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 178 | challengeType = *it; |
| 179 | std::cerr << "The challenge has been selected: " << challengeType << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 180 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 181 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 182 | std::cerr << "Error. Cannot load selected Challenge Module. Exit." << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 185 | auto requirement = challenge->getRequestedParameterList(client.getApplicationStatus(), |
| 186 | client.getChallengeStatus()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 187 | if (requirement.size() > 0) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 188 | std::cerr << "Step " << nStep++ << ": Please provide parameters used for Identity Verification Challenge\n"; |
| 189 | captureParams(requirement); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 190 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 191 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestTLV(client.getApplicationStatus(), |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 192 | client.getChallengeStatus(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 193 | std::move(requirement))), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 194 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 195 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 196 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 197 | static void |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 198 | InfoCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 199 | { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 200 | const Block& contentBlock = reply.getContent(); |
| 201 | |
| 202 | if (!client.verifyInfoResponse(reply)) { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 203 | std::cerr << "The fetched CA information cannot be trusted because its integrity is broken" << std::endl; |
| 204 | return; |
| 205 | } |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 206 | auto caItem = INFO::decodeClientConfigFromContent(contentBlock); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 207 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 208 | 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] | 209 | << "This trust anchor information is signed by " << reply.getSignature().getKeyLocator() |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 210 | << std::endl |
| 211 | << "The certificate is " << caItem.m_anchor << std::endl |
| 212 | << "Do you trust the information? Type in YES or NO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 213 | |
| 214 | std::string answer; |
| 215 | getline(std::cin, answer); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 216 | boost::algorithm::to_lower(answer); |
| 217 | if (answer == "yes") { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 218 | std::cerr << "You answered YES: new CA will be used" << std::endl; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 219 | client.addCaFromInfoResponse(reply); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 220 | // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 221 | startApplication(); |
| 222 | } |
| 223 | else { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 224 | 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] | 225 | return; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | probeCb(const Data& reply) |
| 231 | { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 232 | client.onProbeResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 233 | captureValidityPeriod(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 234 | if (validityPeriod <= 0) { |
| 235 | std::cerr << "Invalid period time. Exit." << std::endl; |
| 236 | return; |
| 237 | } |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 238 | auto now = time::system_clock::now(); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 239 | 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] | 240 | auto interest = client.generateNewInterest(now, now + time::hours(validityPeriod), Name()); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 241 | if (interest != nullptr) { |
| 242 | face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
| 243 | } |
| 244 | else { |
| 245 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 246 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void |
| 250 | startApplication() |
| 251 | { |
| 252 | nStep = 0; |
| 253 | auto caList = client.getClientConf().m_caItems; |
| 254 | int count = 0; |
| 255 | for (auto item : caList) { |
| 256 | std::cerr << "***************************************\n" |
| 257 | << "Index: " << count++ << "\n" |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 258 | << "CA prefix:" << item.m_caPrefix << "\n" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 259 | << "Introduction: " << item.m_caInfo << "\n" |
| 260 | << "***************************************\n"; |
| 261 | } |
| 262 | std::vector<ClientCaItem> caVector{std::begin(caList), std::end(caList)}; |
| 263 | std::cerr << "Step " |
| 264 | << nStep++ << ": Please type in the CA INDEX that you want to apply" |
| 265 | << " or type in NONE if your expected CA is not in the list\n"; |
| 266 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 267 | std::string caIndexS, caIndexSLower; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 268 | getline(std::cin, caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 269 | caIndexSLower = caIndexS; |
| 270 | boost::algorithm::to_lower(caIndexSLower); |
| 271 | if (caIndexSLower == "none") { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 272 | std::cerr << "Step " << nStep << ": Please type in the CA Name\n"; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 273 | std::string expectedCAName; |
| 274 | getline(std::cin, expectedCAName); |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 275 | face.expressInterest(*client.generateInfoInterest(Name(expectedCAName)), |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 276 | bind(&InfoCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 277 | } |
| 278 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 279 | int caIndex; |
| 280 | try { |
| 281 | caIndex = std::stoi(caIndexS); |
| 282 | } |
| 283 | catch (const std::exception& e) { |
| 284 | std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl; |
| 285 | return; |
| 286 | } |
| 287 | if (caIndex < 0 || caIndex >= count) { |
| 288 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 289 | return; |
| 290 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 291 | auto targetCaItem = caVector[caIndex]; |
| 292 | |
| 293 | if (targetCaItem.m_probe != "") { |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 294 | std::cerr << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl; |
| 295 | std::vector<std::string> probeFields = ClientModule::parseProbeComponents(targetCaItem.m_probe); |
| 296 | std::string redo = ""; |
| 297 | std::list<std::string> capturedParams; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 298 | capturedParams = captureParams(probeFields); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 299 | std::string probeInfo; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 300 | for (const auto& item : capturedParams) { |
| 301 | probeInfo += item; |
| 302 | probeInfo += ":"; |
| 303 | } |
| 304 | probeInfo = probeInfo.substr(0, probeInfo.size() - 1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 305 | face.expressInterest(*client.generateProbeInterest(targetCaItem, probeInfo), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 306 | bind(&probeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 307 | } |
| 308 | else { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 309 | 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] | 310 | std::string identityNameStr; |
| 311 | getline(std::cin, identityNameStr); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 312 | captureValidityPeriod(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 313 | if (validityPeriod <= 0) { |
| 314 | std::cerr << "Invalid period time. Exit." << std::endl; |
| 315 | return; |
| 316 | } |
| 317 | Name idName(identityNameStr); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 318 | 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] | 319 | auto now = time::system_clock::now(); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 320 | auto interest = client.generateNewInterest(now, now + time::hours(validityPeriod), idName); |
| 321 | if (interest != nullptr) { |
| 322 | face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
| 323 | } |
| 324 | else { |
| 325 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 326 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 331 | static void |
| 332 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 333 | { |
| 334 | if (error) { |
| 335 | return; |
| 336 | } |
| 337 | const char* signalName = ::strsignal(signalNum); |
| 338 | std::cerr << "Exiting on signal "; |
| 339 | if (signalName == nullptr) { |
| 340 | std::cerr << signalNum; |
| 341 | } |
| 342 | else { |
| 343 | std::cerr << signalName; |
| 344 | } |
| 345 | std::cerr << std::endl; |
| 346 | client.endSession(); |
| 347 | face.getIoService().stop(); |
| 348 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 349 | |
| 350 | int |
| 351 | main(int argc, char* argv[]) |
| 352 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 353 | boost::asio::signal_set terminateSignals(face.getIoService()); |
| 354 | terminateSignals.add(SIGINT); |
| 355 | terminateSignals.add(SIGTERM); |
| 356 | terminateSignals.async_wait(handleSignal); |
| 357 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 358 | namespace po = boost::program_options; |
| 359 | std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf"; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 360 | 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] | 361 | 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), |
| 362 | "desired validity period (hours) of the certificate being requested"); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 363 | po::positional_options_description p; |
| 364 | |
| 365 | po::variables_map vm; |
| 366 | try { |
| 367 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 368 | po::notify(vm); |
| 369 | } |
| 370 | catch (const std::exception& e) { |
| 371 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 372 | return 1; |
| 373 | } |
| 374 | if (vm.count("help") != 0) { |
| 375 | std::cerr << description << std::endl; |
| 376 | return 0; |
| 377 | } |
Zhiyi Zhang | d8993b9 | 2019-07-04 21:58:10 -0700 | [diff] [blame] | 378 | try { |
| 379 | client.getClientConf().load(configFilePath); |
| 380 | } |
| 381 | catch (const std::exception& e) { |
| 382 | std::cerr << "Cannot load the configuration file: " << e.what() << std::endl; |
| 383 | return 1; |
| 384 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 385 | startApplication(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 386 | face.processEvents(); |
| 387 | return 0; |
| 388 | } |
| 389 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 390 | } // namespace ndncert |
| 391 | } // namespace ndn |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 392 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 393 | int |
| 394 | main(int argc, char* argv[]) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 395 | { |
| 396 | return ndn::ndncert::main(argc, argv); |
| 397 | } |