Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, 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 | |
| 21 | #include "client-module.hpp" |
| 22 | #include "challenge-module.hpp" |
Zhiyi Zhang | b6fab0f | 2017-09-21 16:26:27 -0700 | [diff] [blame] | 23 | #include <iostream> |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 24 | #include <string> |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 25 | #include <boost/program_options/options_description.hpp> |
| 26 | #include <boost/program_options/variables_map.hpp> |
| 27 | #include <boost/program_options/parsers.hpp> |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 28 | #include <ndn-cxx/security/verification-helpers.hpp> |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndncert { |
| 32 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 33 | static void startApplication(); |
| 34 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 35 | int nStep; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | Face face; |
| 37 | security::v2::KeyChain keyChain; |
| 38 | std::string challengeType; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 39 | int validityPeriod = -1; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 40 | ClientModule client(keyChain); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 41 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | static std::list<std::string> |
| 43 | captureParams(const JsonSection& requirement) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 44 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | std::list<std::string> results; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 46 | for (const auto& item : requirement) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 47 | std::cerr << item.second.get<std::string>("") << std::endl; |
| 48 | std::cerr << "Please provide the argument: " << item.first << " : " << std::endl; |
| 49 | std::string tempParam; |
| 50 | getline(std::cin, tempParam); |
| 51 | results.push_back(tempParam); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 52 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 53 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
| 54 | auto it1 = results.begin(); |
| 55 | auto it2 = requirement.begin(); |
| 56 | for (; it1 != results.end() && it2 != requirement.end(); it1++, it2++) { |
| 57 | std::cerr << it2->first << " : " << *it1 << 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 | return results; |
| 60 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 61 | |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 62 | static std::list<std::string> |
| 63 | captureParams(const std::vector<std::string>& requirement) |
| 64 | { |
| 65 | std::list<std::string> results; |
| 66 | for (const auto& item : requirement) { |
| 67 | std::cerr << "Please provide the argument: " << item << " : " << std::endl; |
| 68 | std::string tempParam; |
| 69 | getline(std::cin, tempParam); |
| 70 | results.push_back(tempParam); |
| 71 | } |
| 72 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
| 73 | auto it1 = results.begin(); |
| 74 | auto it2 = requirement.begin(); |
| 75 | for (; it1 != results.end() && it2 != requirement.end(); it1++, it2++) { |
| 76 | std::cerr << *it2 << " : " << *it1 << std::endl; |
| 77 | } |
| 78 | return results; |
| 79 | } |
| 80 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 81 | static void |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 82 | captureValidityPeriod() |
| 83 | { |
| 84 | if (validityPeriod <= 0) { |
| 85 | std::cerr << "Step " << nStep++ |
| 86 | << ": Please type in your expected validity period of your certificate." |
| 87 | << " Type the number of hours (168 for week, 730 for month, 8760 for year). The CA may change the validity" |
| 88 | << " period if your expected period is too long." << std::endl; |
| 89 | std::string periodStr = ""; |
| 90 | do { |
| 91 | getline(std::cin, periodStr); |
| 92 | try { |
| 93 | validityPeriod = std::stoi(periodStr); |
| 94 | } |
| 95 | catch (const std::exception& e) { |
| 96 | validityPeriod = -1; |
| 97 | } |
| 98 | if (validityPeriod > 0) { |
| 99 | break; |
| 100 | } |
| 101 | std::cerr << "Invalid period time. Please input the period again." << std::endl; |
| 102 | } while (true); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 107 | onNackCb() |
| 108 | { |
| 109 | std::cerr << "Got NACK\n"; |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | timeoutCb() |
| 114 | { |
| 115 | std::cerr << "Interest sent time out\n"; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | downloadCb(const Data& reply) |
| 120 | { |
| 121 | client.onDownloadResponse(reply); |
| 122 | std::cerr << "Step " << nStep++ |
| 123 | << ": DONE! Certificate has already been installed to local keychain\n"; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | challengeCb(const Data& reply) |
| 129 | { |
| 130 | client.onChallengeResponse(reply); |
| 131 | if (client.getApplicationStatus() == STATUS_SUCCESS) { |
| 132 | std::cerr << "DONE! Certificate has already been issued \n"; |
| 133 | face.expressInterest(*client.generateDownloadInterest(), bind(&downloadCb, _2), |
| 134 | bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | 4d89fe0 | 2017-04-28 18:51:51 -0700 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 138 | auto challenge = ChallengeModule::createChallengeModule(challengeType); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 139 | auto requirement = challenge->getRequirementForChallenge(client.getApplicationStatus(), |
| 140 | client.getChallengeStatus()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | if (requirement.size() > 0) { |
Zhiyi Zhang | 916ba2d | 2018-02-01 18:16:27 -0800 | [diff] [blame] | 142 | std::cerr << "Step " << nStep++ << ": Please satisfy following instruction(s)\n"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 143 | std::string redo = ""; |
| 144 | std::list<std::string> capturedParams; |
| 145 | do { |
| 146 | capturedParams = captureParams(requirement); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 147 | std::cerr << "If everything is correct, please type in OK; otherwise, type in REDO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 148 | getline(std::cin, redo); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 149 | boost::algorithm::to_lower(redo); |
| 150 | } while (redo != "ok"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 151 | auto it1 = capturedParams.begin(); |
| 152 | auto it2 = requirement.begin(); |
| 153 | for (; it1 != capturedParams.end() && it2 != requirement.end(); it1++, it2++) { |
| 154 | it2->second.put("", *it1); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 155 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 156 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 157 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestJson(client.getApplicationStatus(), |
| 158 | client.getChallengeStatus(), |
| 159 | requirement)), |
| 160 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 161 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 162 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 163 | static void |
| 164 | newCb(const Data& reply) |
| 165 | { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 166 | int challengeIndex = 0; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 167 | auto challengeList = client.onNewResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 168 | if (challengeList.size() < 1) { |
| 169 | std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl; |
| 170 | return; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 171 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 172 | else if (challengeList.size() > 1) { |
| 173 | int count = 0; |
| 174 | std::string choice = ""; |
| 175 | std::cerr << "Step " << nStep++ << ": Please type in the challenge index that you want to perform\n"; |
| 176 | do { |
| 177 | count = 0; |
| 178 | for (auto item : challengeList) { |
| 179 | std::cerr << "\t" << count++ << " : "<< item << std::endl; |
| 180 | } |
| 181 | getline(std::cin, choice); |
| 182 | try { |
| 183 | challengeIndex = std::stoi(choice); |
| 184 | } |
| 185 | catch (const std::exception& e) { |
| 186 | challengeIndex = -1; |
| 187 | } |
| 188 | if (challengeIndex >= 0 && challengeIndex < count) { |
| 189 | break; |
| 190 | } |
| 191 | std::cerr << "Your input index is out of range. Please type in again." << std::endl; |
| 192 | } while (true); |
| 193 | } |
| 194 | auto it = challengeList.begin(); |
| 195 | std::advance(it, challengeIndex); |
| 196 | unique_ptr<ChallengeModule> challenge = ChallengeModule::createChallengeModule(*it); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 197 | if (challenge != nullptr) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 198 | challengeType = *it; |
| 199 | std::cerr << "The challenge has been selected: " << challengeType << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 200 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 201 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 202 | std::cerr << "Error. Cannot load selected Challenge Module. Exit." << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | auto requirement = challenge->getRequirementForChallenge(client.getApplicationStatus(), |
| 206 | client.getChallengeStatus()); |
| 207 | if (requirement.size() > 0) { |
| 208 | std::cerr << "Step " << nStep++ << ": Please satisfy following instruction(s)\n"; |
| 209 | std::string redo = ""; |
| 210 | std::list<std::string> capturedParams; |
| 211 | do { |
| 212 | capturedParams = captureParams(requirement); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 213 | std::cerr << "If everything is correct, please type in OK; otherwise, type in REDO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 214 | getline(std::cin, redo); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 215 | boost::algorithm::to_lower(redo); |
| 216 | } while (redo != "ok"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 217 | auto it1 = capturedParams.begin(); |
| 218 | auto it2 = requirement.begin(); |
| 219 | for (; it1 != capturedParams.end() && it2 != requirement.end(); it1++, it2++) { |
| 220 | it2->second.put("", *it1); |
| 221 | } |
| 222 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 223 | face.expressInterest(*client.generateChallengeInterest(challenge->genChallengeRequestJson(client.getApplicationStatus(), |
| 224 | client.getChallengeStatus(), |
| 225 | requirement)), |
| 226 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 227 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 228 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 229 | static void |
| 230 | probeInfoCb(const Data& reply) |
| 231 | { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame^] | 232 | if (!client.verifyProbeInfoResponse(reply)) { |
| 233 | std::cerr << "The fetched CA information cannot be trusted because its integrity is broken" << std::endl; |
| 234 | return; |
| 235 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 236 | auto contentJson = ClientModule::getJsonFromData(reply); |
| 237 | auto caItem = ClientConfig::extractCaItem(contentJson); |
| 238 | |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame^] | 239 | std::cerr << "Will use a new trust anchor, please double check the identity info: \n" |
| 240 | << "This trust anchor information is signed by " << reply.getSignature().getKeyLocator() << std::endl |
| 241 | << "The certificate is " << caItem.m_anchor << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 242 | std::cerr << "Do you trust the information? Type in YES or NO" << std::endl; |
| 243 | |
| 244 | std::string answer; |
| 245 | getline(std::cin, answer); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 246 | boost::algorithm::to_lower(answer); |
| 247 | if (answer == "yes") { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame^] | 248 | std::cerr << "You answered YES: new CA will be used" << std::endl; |
| 249 | client.addCaFromProbeInfoResponse(reply); |
| 250 | // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 251 | startApplication(); |
| 252 | } |
| 253 | else { |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame^] | 254 | 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] | 255 | return; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | probeCb(const Data& reply) |
| 261 | { |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 262 | client.onProbeResponse(reply); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 263 | captureValidityPeriod(); |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 264 | auto probeToken = make_shared<Data>(reply); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 265 | auto now = time::system_clock::now(); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 266 | std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl; |
| 267 | face.expressInterest(*client.generateNewInterest(now, now + time::hours(validityPeriod), |
Zhiyi Zhang | 781a560 | 2019-06-26 19:05:04 -0700 | [diff] [blame] | 268 | Name(), probeToken), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 269 | bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void |
| 273 | startApplication() |
| 274 | { |
| 275 | nStep = 0; |
| 276 | auto caList = client.getClientConf().m_caItems; |
| 277 | int count = 0; |
| 278 | for (auto item : caList) { |
| 279 | std::cerr << "***************************************\n" |
| 280 | << "Index: " << count++ << "\n" |
| 281 | << "CA prefix:" << item.m_caName << "\n" |
| 282 | << "Introduction: " << item.m_caInfo << "\n" |
| 283 | << "***************************************\n"; |
| 284 | } |
| 285 | std::vector<ClientCaItem> caVector{std::begin(caList), std::end(caList)}; |
| 286 | std::cerr << "Step " |
| 287 | << nStep++ << ": Please type in the CA INDEX that you want to apply" |
| 288 | << " or type in NONE if your expected CA is not in the list\n"; |
| 289 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 290 | std::string caIndexS, caIndexSLower; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 291 | getline(std::cin, caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 292 | caIndexSLower = caIndexS; |
| 293 | boost::algorithm::to_lower(caIndexSLower); |
| 294 | if (caIndexSLower == "none") { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 295 | std::cerr << "Step " << nStep << ": Please type in the CA Name\n"; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame^] | 296 | std::string expectedCAName; |
| 297 | getline(std::cin, expectedCAName); |
| 298 | face.expressInterest(*client.generateProbeInfoInterest(Name(expectedCAName)), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 299 | bind(&probeInfoCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 300 | } |
| 301 | else { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 302 | int caIndex; |
| 303 | try { |
| 304 | caIndex = std::stoi(caIndexS); |
| 305 | } |
| 306 | catch (const std::exception& e) { |
| 307 | std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl; |
| 308 | return; |
| 309 | } |
| 310 | if (caIndex < 0 || caIndex >= count) { |
| 311 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 312 | return; |
| 313 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 314 | auto targetCaItem = caVector[caIndex]; |
| 315 | |
| 316 | if (targetCaItem.m_probe != "") { |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 317 | std::cerr << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl; |
| 318 | std::vector<std::string> probeFields = ClientModule::parseProbeComponents(targetCaItem.m_probe); |
| 319 | std::string redo = ""; |
| 320 | std::list<std::string> capturedParams; |
| 321 | do { |
| 322 | capturedParams = captureParams(probeFields); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 323 | std::cerr << "If everything is correct, please type in OK; otherwise, type in REDO" << std::endl; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 324 | getline(std::cin, redo); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 325 | boost::algorithm::to_lower(redo); |
| 326 | } while (redo != "ok"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 327 | std::string probeInfo; |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 328 | for (const auto& item : capturedParams) { |
| 329 | probeInfo += item; |
| 330 | probeInfo += ":"; |
| 331 | } |
| 332 | probeInfo = probeInfo.substr(0, probeInfo.size() - 1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 333 | face.expressInterest(*client.generateProbeInterest(targetCaItem, probeInfo), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 334 | bind(&probeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 335 | } |
| 336 | else { |
| 337 | std::cerr << "Step " << nStep++ << ": Please type in the identity name you want to get (with CA prefix)\n"; |
| 338 | std::string identityNameStr; |
| 339 | getline(std::cin, identityNameStr); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 340 | captureValidityPeriod(); |
| 341 | 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] | 342 | auto now = time::system_clock::now(); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 343 | face.expressInterest(*client.generateNewInterest(now, now + time::hours(validityPeriod), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 344 | Name(identityNameStr)), |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 345 | bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 350 | |
| 351 | int |
| 352 | main(int argc, char* argv[]) |
| 353 | { |
| 354 | namespace po = boost::program_options; |
| 355 | std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf"; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 356 | po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n"); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 357 | description.add_options() |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 358 | ("help,h", "produce help message") |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 359 | ("config-file,c", po::value<std::string>(&configFilePath), "config file name") |
| 360 | ("validity-period,v", po::value<int>(&validityPeriod)->default_value(-1), "the validity period of your certificate being requested"); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 361 | po::positional_options_description p; |
| 362 | |
| 363 | po::variables_map vm; |
| 364 | try { |
| 365 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 366 | po::notify(vm); |
| 367 | } |
| 368 | catch (const std::exception& e) { |
| 369 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 370 | return 1; |
| 371 | } |
| 372 | if (vm.count("help") != 0) { |
| 373 | std::cerr << description << std::endl; |
| 374 | return 0; |
| 375 | } |
Zhiyi Zhang | d8993b9 | 2019-07-04 21:58:10 -0700 | [diff] [blame] | 376 | try { |
| 377 | client.getClientConf().load(configFilePath); |
| 378 | } |
| 379 | catch (const std::exception& e) { |
| 380 | std::cerr << "Cannot load the configuration file: " << e.what() << std::endl; |
| 381 | return 1; |
| 382 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 383 | startApplication(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 384 | face.processEvents(); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | } // namespace ndncert |
| 389 | } // namespace ndn |
| 390 | |
Zhiyi Zhang | 916ba2d | 2018-02-01 18:16:27 -0800 | [diff] [blame] | 391 | int main(int argc, char* argv[]) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 392 | { |
| 393 | return ndn::ndncert::main(argc, argv); |
| 394 | } |