Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 2 | /* |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, 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 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 21 | #include "requester-request.hpp" |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/face.hpp> |
| 24 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/verification-helpers.hpp> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [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> |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 31 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 32 | #include <iostream> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 34 | namespace ndncert::requester { |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 35 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 36 | static void |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 37 | selectCaProfile(const std::string& configFilePath); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 38 | |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 39 | static void |
| 40 | runProbe(CaProfile profile); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 41 | |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 42 | static void |
| 43 | runNew(CaProfile profile, Name identityName); |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 44 | |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 45 | static void |
| 46 | runChallenge(const std::string& challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 47 | |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 48 | static size_t nStep = 1; |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 49 | static const std::string defaultChallenge = "email"; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 50 | static ndn::Face face; |
| 51 | static ndn::KeyChain keyChain; |
| 52 | static std::shared_ptr<Request> requesterState; |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 53 | static std::shared_ptr<std::multimap<std::string, std::string>> capturedProbeParams; |
| 54 | static Name newlyCreatedIdentityName; |
| 55 | static Name newlyCreatedKeyName; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 56 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 57 | static void |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 58 | captureParams(std::multimap<std::string, std::string>& requirement) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 59 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | std::list<std::string> results; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 61 | for (auto& item : requirement) { |
| 62 | std::cerr << std::get<1>(item) << std::endl; |
| 63 | std::string captured; |
| 64 | getline(std::cin, captured); |
| 65 | std::get<1>(item) = captured; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 66 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 67 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 68 | for (const auto& item : requirement) { |
| 69 | std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 70 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 71 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 72 | |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 73 | static std::multimap<std::string, std::string> |
Zhiyi Zhang | 2de4ea3 | 2020-12-13 17:45:25 -0800 | [diff] [blame] | 74 | captureParams(const std::vector<std::string>& requirement) |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 75 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 76 | std::multimap<std::string, std::string> results; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 77 | for (const auto& r : requirement) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 78 | results.emplace(r, "Please input: " + r); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 79 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 80 | captureParams(results); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 81 | return results; |
| 82 | } |
| 83 | |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 84 | static int |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 85 | captureValidityPeriod(time::hours maxValidityPeriod) |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 86 | { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 87 | std::cerr << "\n***************************************\n" |
| 88 | << "Step " << nStep++ |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 89 | << ": Please type in your expected validity period of your certificate." |
| 90 | << " Type the number of hours (168 for week, 730 for month, 8760 for year)." |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 91 | << " The CA may reject your application if your expected period is too long." |
| 92 | << " The maximum validity period allowed by this CA is " << maxValidityPeriod << "."<< std::endl; |
Zhiyi Zhang | 87ded73 | 2021-01-08 14:05:24 -0800 | [diff] [blame] | 93 | size_t count = 0; |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 94 | while (count < 3) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 95 | std::string periodStr = ""; |
| 96 | getline(std::cin, periodStr); |
| 97 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 98 | return std::stoul(periodStr); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 99 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 100 | catch (const std::exception&) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 101 | std::cerr << "Your input is invalid. Try again: " << std::endl; |
Zhiyi Zhang | 87ded73 | 2021-01-08 14:05:24 -0800 | [diff] [blame] | 102 | count++; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 103 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 104 | } |
Zhiyi Zhang | 87ded73 | 2021-01-08 14:05:24 -0800 | [diff] [blame] | 105 | std::cerr << "Invalid input for too many times, exit. " << std::endl; |
| 106 | exit(1); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 109 | static Name |
| 110 | captureKeyName(ndn::security::pib::Identity& identity, ndn::security::pib::Key& defaultKey) |
| 111 | { |
| 112 | size_t count = 0; |
| 113 | std::cerr << "***************************************\n" |
| 114 | << "Step " << nStep++ << ": KEY SELECTION" << std::endl; |
| 115 | for (const auto& key : identity.getKeys()) { |
| 116 | std::cerr << "> Index: " << count++ << std::endl |
| 117 | << ">> Key Name:"; |
| 118 | if (key == defaultKey) { |
| 119 | std::cerr << " +->* "; |
| 120 | } |
| 121 | else { |
| 122 | std::cerr << " +-> "; |
| 123 | } |
| 124 | std::cerr << key.getName() << std::endl; |
| 125 | } |
| 126 | |
| 127 | std::cerr << "Please type in the key's index that you want to certify or type in NEW if you want to certify a new key:\n"; |
| 128 | std::string indexStr = ""; |
| 129 | std::string indexStrLower = ""; |
| 130 | size_t keyIndex; |
| 131 | getline(std::cin, indexStr); |
| 132 | |
| 133 | indexStrLower = indexStr; |
| 134 | boost::algorithm::to_lower(indexStrLower); |
| 135 | if (indexStrLower == "new") { |
| 136 | auto newlyCreatedKey = keyChain.createKey(identity); |
| 137 | newlyCreatedKeyName = newlyCreatedKey.getName(); |
| 138 | std::cerr << "New key generated: " << newlyCreatedKeyName << std::endl; |
| 139 | return newlyCreatedKeyName; |
| 140 | } |
| 141 | else { |
| 142 | try { |
| 143 | keyIndex = std::stoul(indexStr); |
| 144 | } |
| 145 | catch (const std::exception&) { |
| 146 | std::cerr << "Your input is neither NEW nor a valid index. Exit" << std::endl; |
| 147 | exit(1); |
| 148 | } |
| 149 | |
| 150 | if (keyIndex >= count) { |
| 151 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 152 | exit(1); |
| 153 | } |
| 154 | else { |
| 155 | auto itemIterator = identity.getKeys().begin(); |
| 156 | std::advance(itemIterator, keyIndex); |
| 157 | auto targetKeyItem = *itemIterator; |
| 158 | return targetKeyItem.getName(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 163 | [[noreturn]] static void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | onNackCb() |
| 165 | { |
| 166 | std::cerr << "Got NACK\n"; |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 167 | exit(1); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void |
| 171 | timeoutCb() |
| 172 | { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 173 | std::cerr << "Interest timeout\n"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 177 | certFetchCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 178 | { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 179 | auto item = Request::onCertFetchResponse(reply); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 180 | if (item) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 181 | keyChain.addCertificate(keyChain.getPib().getIdentity(item->getIdentity()).getKey(item->getKeyName()), *item); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 182 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 183 | std::cerr << "\n***************************************\n" |
| 184 | << "Step " << nStep++ |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 185 | << ": DONE\nCertificate with Name: " << reply.getName() |
| 186 | << " has been installed to your local keychain\n" |
| 187 | << "Exit now" << std::endl; |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 188 | face.getIoService().stop(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void |
| 192 | challengeCb(const Data& reply) |
| 193 | { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 194 | try { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 195 | requesterState->onChallengeResponse(reply); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 196 | } |
| 197 | catch (const std::exception& e) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 198 | std::cerr << "Error when decoding challenge step: " << e.what() << std::endl; |
| 199 | exit(1); |
| 200 | } |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 201 | if (requesterState->m_status == Status::SUCCESS) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 202 | std::cerr << "Certificate has already been issued, downloading certificate..." << std::endl; |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 203 | face.expressInterest(*requesterState->genCertFetchInterest(), |
| 204 | [] (const auto&, const auto& data) { certFetchCb(data); }, |
| 205 | [] (auto&&...) { onNackCb(); }, |
| 206 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | 4d89fe0 | 2017-04-28 18:51:51 -0700 | [diff] [blame] | 207 | return; |
| 208 | } |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 209 | runChallenge(requesterState->m_challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 210 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 211 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 212 | static void |
| 213 | newCb(const Data& reply) |
| 214 | { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 215 | std::list<std::string> challengeList; |
| 216 | try { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 217 | challengeList = requesterState->onNewRenewRevokeResponse(reply); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 218 | } |
| 219 | catch (const std::exception& e) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 220 | std::cerr << "Error on decoding NEW step reply because: " << e.what() << std::endl; |
| 221 | exit(1); |
| 222 | } |
| 223 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 224 | size_t challengeIndex = 0; |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 225 | if (challengeList.empty()) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 226 | std::cerr << "There is no available challenge provided by the CA. Exit" << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 227 | exit(1); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 228 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 229 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 230 | auto item = std::find(challengeList.begin(), challengeList.end(), defaultChallenge); |
| 231 | if (item != challengeList.end()) { |
| 232 | runChallenge(defaultChallenge); |
| 233 | } |
| 234 | else { |
| 235 | // default challenge not available |
| 236 | std::cerr << "\n***************************************\n" |
| 237 | << "Step " << nStep++ |
| 238 | << ": CHALLENGE SELECTION" << std::endl; |
| 239 | size_t count = 0; |
| 240 | std::string choice = ""; |
| 241 | for (const auto& item : challengeList) { |
| 242 | std::cerr << "> Index: " << count++ << std::endl |
| 243 | << ">> Challenge: " << item << std::endl; |
Zhiyi Zhang | 87ded73 | 2021-01-08 14:05:24 -0800 | [diff] [blame] | 244 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 245 | std::cerr << "Please type in the index of the challenge that you want to perform:" << std::endl; |
| 246 | size_t inputCount = 0; |
| 247 | while (inputCount < 3) { |
| 248 | getline(std::cin, choice); |
| 249 | try { |
| 250 | challengeIndex = std::stoul(choice); |
| 251 | } |
| 252 | catch (const std::exception&) { |
| 253 | std::cerr << "Your input is not valid. Try again:" << std::endl; |
| 254 | inputCount++; |
| 255 | continue; |
| 256 | } |
| 257 | if (challengeIndex >= count) { |
| 258 | std::cerr << "Your input index is out of range. Try again:" << std::endl; |
| 259 | inputCount++; |
| 260 | continue; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | if (inputCount == 3) { |
| 265 | std::cerr << "Invalid input for too many times, exit. " << std::endl; |
| 266 | exit(1); |
| 267 | } |
| 268 | |
| 269 | auto it = challengeList.begin(); |
| 270 | std::advance(it, challengeIndex); |
| 271 | std::cerr << "The challenge has been selected: " << *it << std::endl; |
| 272 | runChallenge(*it); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 273 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 274 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 275 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 276 | static void |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 277 | infoCb(const Data& reply, const Name& certFullName) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 278 | { |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 279 | std::optional<CaProfile> profile; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 280 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 281 | if (certFullName.empty()) { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 282 | profile = Request::onCaProfileResponse(reply); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 283 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 284 | else { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 285 | profile = Request::onCaProfileResponseAfterRedirection(reply, certFullName); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | catch (const std::exception& e) { |
| 289 | std::cerr << "The fetched CA information cannot be used because: " << e.what() << std::endl; |
| 290 | return; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 291 | } |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 292 | std::cerr << "\n***************************************\n" |
| 293 | << "Step " << nStep++ |
| 294 | << ": Will use a new trust anchor, please double check the identity info:" << std::endl |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 295 | << "> New CA name: " << profile->caPrefix << std::endl |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 296 | << "> This trust anchor information is signed by: " << reply.getSignatureInfo().getKeyLocator() << std::endl |
Zhiyi Zhang | ec4aa3a | 2021-01-08 14:26:37 -0800 | [diff] [blame] | 297 | << "> The certificate: " << *profile->cert << std::endl |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 298 | << "Do you trust the information? Type in YES or NO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 299 | |
| 300 | std::string answer; |
| 301 | getline(std::cin, answer); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 302 | boost::algorithm::to_lower(answer); |
| 303 | if (answer == "yes") { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 304 | std::cerr << "You answered YES: new CA " << profile->caPrefix << " will be used" << std::endl; |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 305 | runProbe(*profile); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 306 | } |
| 307 | else { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 308 | std::cerr << "You answered NO: new CA " << profile->caPrefix << " will not be used" << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 309 | exit(0); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | static void |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 314 | probeCb(const Data& reply, CaProfile profile) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 315 | { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 316 | std::vector<std::pair<Name, int>> names; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 317 | std::vector<Name> redirects; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 318 | try { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 319 | Request::onProbeResponse(reply, profile, names, redirects); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 320 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 321 | catch (const std::exception& e) { |
| 322 | std::cerr << "The probed CA response cannot be used because: " << e.what() << std::endl; |
Zhiyi Zhang | 87ded73 | 2021-01-08 14:05:24 -0800 | [diff] [blame] | 323 | exit(1); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 324 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 325 | |
| 326 | size_t count = 0; |
| 327 | Name selectedName; |
| 328 | Name redirectedCaFullName; |
| 329 | // always prefer redirection over direct assignment |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 330 | if (!redirects.empty()) { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 331 | if (redirects.size() < 2) { |
| 332 | redirectedCaFullName = redirects.front(); |
| 333 | } |
| 334 | else { |
| 335 | std::cerr << "\n***************************************\n" |
| 336 | << "Step " << nStep++ |
| 337 | << " Choose another trusted CA suggested by the CA: " << std::endl; |
| 338 | for (const auto& redirect : redirects) { |
| 339 | std::cerr << "> Index: " << count++ << std::endl |
| 340 | << ">> Suggested CA: " << ndn::security::extractIdentityFromCertName(redirect.getPrefix(-1)) |
| 341 | << std::endl; |
tylerliu | d82cc5c | 2020-12-21 23:58:39 -0800 | [diff] [blame] | 342 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 343 | std::cerr << "Please type in the index of your choice:" << std::endl; |
| 344 | size_t index = 0; |
| 345 | try { |
| 346 | std::string input; |
| 347 | getline(std::cin, input); |
| 348 | index = std::stoul(input); |
| 349 | } |
| 350 | catch (const std::exception&) { |
| 351 | std::cerr << "Your input is Invalid. Exit" << std::endl; |
| 352 | exit(1); |
| 353 | } |
| 354 | if (index >= redirects.size()) { |
| 355 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 356 | exit(1); |
| 357 | } |
| 358 | redirectedCaFullName = redirects[index]; |
tylerliu | d82cc5c | 2020-12-21 23:58:39 -0800 | [diff] [blame] | 359 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 360 | auto redirectedCaName = ndn::security::extractIdentityFromCertName(redirectedCaFullName.getPrefix(-1)); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 361 | std::cerr << "You will be redirected to CA: " << redirectedCaName << std::endl; |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 362 | face.expressInterest( |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 363 | *Request::genCaProfileDiscoveryInterest(redirectedCaName), |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 364 | [&, redirectedCaFullName] (const auto&, const auto& data) { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 365 | auto fetchingInterest = Request::genCaProfileInterestFromDiscoveryResponse(data); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 366 | face.expressInterest(*fetchingInterest, |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 367 | [=] (const auto&, const auto& data2) { infoCb(data2, redirectedCaFullName); }, |
| 368 | [] (auto&&...) { onNackCb(); }, |
| 369 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 370 | }, |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 371 | [] (auto&&...) { onNackCb(); }, |
| 372 | [] (auto&&...) { timeoutCb(); }); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 373 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 374 | else if (!names.empty()) { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 375 | if (names.size() < 2) { |
| 376 | selectedName = names.front().first; |
| 377 | } |
| 378 | else { |
| 379 | std::cerr << "\n***************************************\n" |
| 380 | << "Step " << nStep++ |
| 381 | << ": You can either select one of the following names suggested by the CA: " << std::endl; |
| 382 | for (const auto& name : names) { |
| 383 | std::cerr << "> Index: " << count++ << std::endl |
| 384 | << ">> Suggested name: " << name.first << std::endl |
| 385 | << ">> Corresponding max suffix length: " << name.second << std::endl; |
| 386 | } |
| 387 | std::cerr << "Please type in the index of your choice:" << std::endl; |
| 388 | size_t index = 0; |
| 389 | try { |
| 390 | std::string input; |
| 391 | getline(std::cin, input); |
| 392 | index = std::stoul(input); |
| 393 | } |
| 394 | catch (const std::exception&) { |
| 395 | std::cerr << "Your input is invalid. Exit" << std::endl; |
| 396 | exit(1); |
| 397 | } |
| 398 | if (index >= names.size()) { |
| 399 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 400 | exit(1); |
| 401 | } |
| 402 | selectedName = names[index].first; |
| 403 | } |
| 404 | std::cerr << "You are applying for name: " << selectedName << std::endl; |
| 405 | runNew(profile, selectedName); |
| 406 | } |
| 407 | else { |
| 408 | std::cerr << "Neither name assignment nor redirection is available, exit." << std::endl; |
| 409 | exit(1); |
| 410 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 414 | selectCaProfile(const std::string& configFilePath) |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 415 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 416 | ProfileStorage profileStorage; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 417 | try { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 418 | profileStorage.load(configFilePath); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 419 | } |
| 420 | catch (const std::exception& e) { |
| 421 | std::cerr << "Cannot load the configuration file: " << e.what() << std::endl; |
| 422 | exit(1); |
| 423 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 424 | size_t count = 0; |
| 425 | std::cerr << "***************************************\n" |
| 426 | << "Step " << nStep++ << ": CA SELECTION" << std::endl; |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 427 | for (const auto& item : profileStorage.getKnownProfiles()) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 428 | std::cerr << "> Index: " << count++ << std::endl |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 429 | << ">> CA prefix:" << item.caPrefix << std::endl |
| 430 | << ">> Introduction: " << item.caInfo << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 431 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 432 | std::cerr << "Please type in the CA's index that you want to apply or type in NONE if your expected CA is not in the list:\n"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 433 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 434 | std::string caIndexS, caIndexSLower; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 435 | getline(std::cin, caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 436 | caIndexSLower = caIndexS; |
| 437 | boost::algorithm::to_lower(caIndexSLower); |
| 438 | if (caIndexSLower == "none") { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 439 | std::cerr << "\n***************************************\n" |
| 440 | << "Step " << nStep << ": ADD NEW CA\nPlease type in the CA's Name:" << std::endl; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 441 | std::string expectedCAName; |
| 442 | getline(std::cin, expectedCAName); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 443 | face.expressInterest( |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 444 | *Request::genCaProfileDiscoveryInterest(Name(expectedCAName)), |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 445 | [&] (const auto&, const auto& data) { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 446 | auto fetchingInterest = Request::genCaProfileInterestFromDiscoveryResponse(data); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 447 | face.expressInterest(*fetchingInterest, |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 448 | [] (const auto&, const auto& data2) { infoCb(data2, {}); }, |
| 449 | [] (auto&&...) { onNackCb(); }, |
| 450 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 451 | }, |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 452 | [] (auto&&...) { onNackCb(); }, |
| 453 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 454 | } |
| 455 | else { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 456 | size_t caIndex; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 457 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 458 | caIndex = std::stoul(caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 459 | } |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 460 | catch (const std::exception&) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 461 | std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl; |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 462 | exit(1); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 463 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 464 | if (caIndex >= count) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 465 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 466 | exit(1); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 467 | } |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 468 | auto itemIterator = profileStorage.getKnownProfiles().cbegin(); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 469 | std::advance(itemIterator, caIndex); |
| 470 | auto targetCaItem = *itemIterator; |
| 471 | runProbe(targetCaItem); |
| 472 | } |
| 473 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 474 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 475 | static void |
| 476 | runProbe(CaProfile profile) |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 477 | { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 478 | if (!capturedProbeParams) { |
| 479 | std::cerr << "\n***************************************\n" |
| 480 | << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl; |
| 481 | auto captured = captureParams(profile.probeParameterKeys); |
Tianyuan Yu | b5d3a4f | 2022-03-11 17:03:24 -0800 | [diff] [blame^] | 482 | auto it = captured.find(defaultChallenge); |
| 483 | if (it != captured.end()) { |
| 484 | it->second = boost::algorithm::to_lower_copy(it->second); |
| 485 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 486 | capturedProbeParams = std::make_shared<std::multimap<std::string, std::string>>(captured); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 487 | } |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 488 | face.expressInterest(*Request::genProbeInterest(profile, std::move(*capturedProbeParams)), |
| 489 | [profile] (const auto&, const auto& data) { probeCb(data, profile); }, |
| 490 | [] (auto&&...) { onNackCb(); }, |
| 491 | [] (auto&&...) { timeoutCb(); }); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static void |
| 495 | runNew(CaProfile profile, Name identityName) |
| 496 | { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 497 | int validityPeriod = captureValidityPeriod(time::duration_cast<time::hours>(profile.maxValidityPeriod)); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 498 | auto now = time::system_clock::now(); |
| 499 | std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl; |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 500 | requesterState = std::make_shared<Request>(keyChain, profile, RequestType::NEW); |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 501 | |
| 502 | // generate a newly key pair or choose an existing key |
| 503 | const auto& pib = keyChain.getPib(); |
| 504 | ndn::security::pib::Identity identity; |
| 505 | ndn::security::pib::Key defaultKey; |
| 506 | try { |
| 507 | identity = pib.getIdentity(identityName); |
| 508 | } |
| 509 | catch (const ndn::security::Pib::Error&) { |
| 510 | identity = keyChain.createIdentity(identityName); |
| 511 | newlyCreatedIdentityName = identity.getName(); |
| 512 | } |
| 513 | try { |
| 514 | defaultKey = identity.getDefaultKey(); |
| 515 | } |
| 516 | catch (const ndn::security::Pib::Error&) { |
| 517 | defaultKey = keyChain.createKey(identity); |
| 518 | newlyCreatedKeyName = defaultKey.getName(); |
| 519 | } |
| 520 | |
| 521 | Name keyName; |
| 522 | if (newlyCreatedIdentityName.empty()) { |
| 523 | keyName = captureKeyName(identity, defaultKey); |
| 524 | } |
| 525 | else { |
| 526 | keyName = defaultKey.getName(); |
| 527 | } |
| 528 | auto interest = requesterState->genNewInterest(keyName, now, now + time::hours(validityPeriod)); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 529 | if (interest != nullptr) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 530 | face.expressInterest(*interest, |
| 531 | [] (const auto&, const auto& data) { newCb(data); }, |
| 532 | [] (auto&&...) { onNackCb(); }, |
| 533 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 534 | } |
| 535 | else { |
| 536 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 537 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static void |
| 541 | runChallenge(const std::string& challengeType) |
| 542 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 543 | std::multimap<std::string, std::string> requirement; |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 544 | try { |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 545 | requirement = requesterState->selectOrContinueChallenge(challengeType); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 546 | } |
| 547 | catch (const std::exception& e) { |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 548 | std::cerr << "Error. Cannot successfully load the Challenge Module with error: " << e.what() |
| 549 | << "\nExit." << std::endl; |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 550 | exit(1); |
| 551 | } |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 552 | if (!requirement.empty()) { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 553 | if (requesterState->m_status == Status::BEFORE_CHALLENGE && challengeType == defaultChallenge) { |
| 554 | requirement.find(challengeType)->second = capturedProbeParams->find(defaultChallenge)->second; |
| 555 | } |
| 556 | else { |
| 557 | std::cerr << "\n***************************************\n" |
| 558 | << "Step " << nStep |
| 559 | << ": Please provide parameters used for Identity Verification Challenge" << std::endl; |
| 560 | captureParams(requirement); |
| 561 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 562 | } |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 563 | face.expressInterest(*requesterState->genChallengeInterest(std::move(requirement)), |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 564 | [] (const auto&, const auto& data) { challengeCb(data); }, |
| 565 | [] (auto&&...) { onNackCb(); }, |
| 566 | [] (auto&&...) { timeoutCb(); }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 569 | static void |
| 570 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 571 | { |
| 572 | if (error) { |
| 573 | return; |
| 574 | } |
| 575 | const char* signalName = ::strsignal(signalNum); |
| 576 | std::cerr << "Exiting on signal "; |
| 577 | if (signalName == nullptr) { |
| 578 | std::cerr << signalNum; |
| 579 | } |
| 580 | else { |
| 581 | std::cerr << signalName; |
| 582 | } |
| 583 | std::cerr << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 584 | if (requesterState) { |
Tianyuan Yu | ca23bb0 | 2022-03-09 14:09:14 -0800 | [diff] [blame] | 585 | if (!newlyCreatedIdentityName.empty()) { |
| 586 | auto identity = keyChain.getPib().getIdentity(newlyCreatedIdentityName); |
| 587 | keyChain.deleteIdentity(identity); |
| 588 | } |
| 589 | else if (!newlyCreatedKeyName.empty()) { |
| 590 | auto identity = keyChain.getPib().getIdentity(ndn::security::extractIdentityFromKeyName(newlyCreatedKeyName)); |
| 591 | keyChain.deleteKey(identity, identity.getKey(newlyCreatedKeyName)); |
| 592 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 593 | } |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 594 | face.getIoService().stop(); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 595 | exit(1); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 596 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 597 | |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 598 | static int |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 599 | main(int argc, char* argv[]) |
| 600 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 601 | boost::asio::signal_set terminateSignals(face.getIoService()); |
| 602 | terminateSignals.add(SIGINT); |
| 603 | terminateSignals.add(SIGTERM); |
| 604 | terminateSignals.async_wait(handleSignal); |
| 605 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 606 | namespace po = boost::program_options; |
Zhiyi Zhang | 840afd9 | 2020-10-21 13:24:08 -0700 | [diff] [blame] | 607 | std::string configFilePath = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/client.conf"; |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 608 | po::options_description description("Usage: ndncert-client [-h] [-c FILE]\n"); |
| 609 | description.add_options() |
| 610 | ("help,h", "produce help message") |
| 611 | ("config-file,c", po::value<std::string>(&configFilePath), "configuration file name") |
| 612 | ; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 613 | po::positional_options_description p; |
| 614 | |
| 615 | po::variables_map vm; |
| 616 | try { |
| 617 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 618 | po::notify(vm); |
| 619 | } |
| 620 | catch (const std::exception& e) { |
| 621 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 622 | return 1; |
| 623 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 624 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 625 | if (vm.count("help") != 0) { |
| 626 | std::cerr << description << std::endl; |
| 627 | return 0; |
| 628 | } |
Davide Pesavento | e5b4369 | 2021-11-15 22:05:03 -0500 | [diff] [blame] | 629 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 630 | selectCaProfile(configFilePath); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 631 | face.processEvents(); |
| 632 | return 0; |
| 633 | } |
| 634 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 635 | } // namespace ndncert::requester |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 636 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 637 | int |
| 638 | main(int argc, char* argv[]) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 639 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 640 | return ndncert::requester::main(argc, argv); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 641 | } |