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