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 | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 21 | #include "requester.hpp" |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/security/verification-helpers.hpp> |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 23 | #include <boost/asio.hpp> |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 24 | #include <boost/program_options/options_description.hpp> |
| 25 | #include <boost/program_options/parsers.hpp> |
| 26 | #include <boost/program_options/variables_map.hpp> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 27 | #include <iostream> |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndncert { |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 32 | namespace requester { |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 33 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 34 | static void |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 35 | selectCaProfile(std::string configFilePath); |
| 36 | static void |
| 37 | runProbe(CaProfile profile); |
| 38 | static void |
| 39 | runNew(CaProfile profile, Name identityName); |
| 40 | static void |
| 41 | runChallenge(const std::string& challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 43 | size_t nStep = 1; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | Face face; |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 45 | security::KeyChain keyChain; |
Zhiyi Zhang | 59a366d | 2020-10-29 19:01:53 -0700 | [diff] [blame] | 46 | shared_ptr<RequestContext> requesterState = nullptr; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 47 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 48 | static void |
| 49 | captureParams(std::vector<std::tuple<std::string, std::string>>& requirement) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 50 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 51 | std::list<std::string> results; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 52 | for (auto& item : requirement) { |
| 53 | std::cerr << std::get<1>(item) << std::endl; |
| 54 | std::string captured; |
| 55 | getline(std::cin, captured); |
| 56 | std::get<1>(item) = captured; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 57 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | std::cerr << "Got it. This is what you've provided:" << std::endl; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 59 | for (const auto& item : requirement) { |
| 60 | std::cerr << std::get<0>(item) << " : " << std::get<1>(item) << std::endl; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 61 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 63 | |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 64 | static std::vector<std::tuple<std::string, std::string>> |
| 65 | captureParams(const std::list<std::string>& requirement) |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 66 | { |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 67 | std::vector<std::tuple<std::string, std::string>> results; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 68 | for (const auto& r : requirement) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 69 | results.emplace_back(r, "Please input: " + r); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 70 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 71 | captureParams(results); |
Zhiyi Zhang | 547c851 | 2019-06-18 23:46:14 -0700 | [diff] [blame] | 72 | return results; |
| 73 | } |
| 74 | |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 75 | static int |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 76 | captureValidityPeriod() |
| 77 | { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 78 | std::cerr << "\n***************************************\n" |
| 79 | << "Step " << nStep++ |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 80 | << ": Please type in your expected validity period of your certificate." |
| 81 | << " Type the number of hours (168 for week, 730 for month, 8760 for year)." |
| 82 | << " The CA may reject your application if your expected period is too long." << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 83 | while (true) { |
| 84 | std::string periodStr = ""; |
| 85 | getline(std::cin, periodStr); |
| 86 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 87 | return std::stoul(periodStr); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 88 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 89 | catch (const std::exception& e) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 90 | std::cerr << "Your input is invalid. Try again: " << std::endl; |
| 91 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | static void |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | onNackCb() |
| 97 | { |
| 98 | std::cerr << "Got NACK\n"; |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | timeoutCb() |
| 103 | { |
| 104 | std::cerr << "Interest sent time out\n"; |
| 105 | } |
| 106 | |
| 107 | static void |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 108 | certFetchCb(const Data& reply) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 110 | auto item = Requester::onCertFetchResponse(reply); |
| 111 | if (item) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 112 | keyChain.addCertificate(keyChain.getPib().getIdentity(item->getIdentity()).getKey(item->getKeyName()), *item); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 113 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 114 | std::cerr << "\n***************************************\n" |
| 115 | << "Step " << nStep++ |
| 116 | << ": DONE\nCertificate with Name: " << reply.getName().toUri() |
| 117 | << "has already been installed to your local keychain" << std::endl |
| 118 | << "Exit now"; |
| 119 | face.getIoService().stop(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void |
| 123 | challengeCb(const Data& reply) |
| 124 | { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 125 | try { |
| 126 | Requester::onChallengeResponse(*requesterState, reply); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 127 | } |
| 128 | catch (const std::exception& e) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 129 | std::cerr << "Error when decoding challenge step: " << e.what() << std::endl; |
| 130 | exit(1); |
| 131 | } |
| 132 | if (requesterState->m_status == Status::SUCCESS) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 133 | std::cerr << "Certificate has already been issued, downloading certificate..." << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 134 | face.expressInterest(*Requester::genCertFetchInterest(*requesterState), bind(&certFetchCb, _2), |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 135 | bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | 4d89fe0 | 2017-04-28 18:51:51 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 138 | runChallenge(requesterState->m_challengeType); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 139 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 140 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | static void |
| 142 | newCb(const Data& reply) |
| 143 | { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 144 | std::list<std::string> challengeList; |
| 145 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 146 | challengeList = Requester::onNewRenewRevokeResponse(*requesterState, reply); |
| 147 | } |
| 148 | catch (const std::exception& e) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 149 | std::cerr << "Error on decoding NEW step reply because: " << e.what() << std::endl; |
| 150 | exit(1); |
| 151 | } |
| 152 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 153 | size_t challengeIndex = 0; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 154 | if (challengeList.size() < 1) { |
| 155 | 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] | 156 | exit(1); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 157 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 158 | else if (challengeList.size() > 1) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 159 | std::cerr << "\n***************************************\n" |
| 160 | << "Step " << nStep++ |
| 161 | << ": CHALLENGE SELECTION" << std::endl; |
| 162 | size_t count = 0; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 163 | std::string choice = ""; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 164 | for (auto item : challengeList) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 165 | std::cerr << "> Index: " << count++ << std::endl |
| 166 | << ">> Challenge:" << item << std::endl; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 167 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 168 | std::cerr << "Please type in the challenge index that you want to perform:" << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 169 | while (true) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 170 | getline(std::cin, choice); |
| 171 | try { |
| 172 | challengeIndex = std::stoul(choice); |
| 173 | } |
| 174 | catch (const std::exception& e) { |
| 175 | std::cerr << "Your input is not valid. Try again:" << std::endl; |
| 176 | continue; |
| 177 | } |
| 178 | if (challengeIndex >= count) { |
| 179 | std::cerr << "Your input index is out of range. Try again:" << std::endl; |
| 180 | continue; |
| 181 | } |
| 182 | break; |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 183 | } |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 184 | } |
| 185 | auto it = challengeList.begin(); |
| 186 | std::advance(it, challengeIndex); |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 187 | std::cerr << "The challenge has been selected: " << *it << std::endl; |
| 188 | runChallenge(*it); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 189 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 190 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 191 | static void |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 192 | InfoCb(const Data& reply, const Name& certFullName) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 193 | { |
Zhiyi Zhang | 997669a | 2020-10-28 21:15:40 -0700 | [diff] [blame] | 194 | optional<CaProfile> profile; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 195 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 196 | if (certFullName.empty()) { |
| 197 | profile = Requester::onCaProfileResponse(reply); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 198 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 199 | else { |
| 200 | profile = Requester::onCaProfileResponseAfterRedirection(reply, certFullName); |
| 201 | } |
| 202 | } |
| 203 | catch (const std::exception& e) { |
| 204 | std::cerr << "The fetched CA information cannot be used because: " << e.what() << std::endl; |
| 205 | return; |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 206 | } |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 207 | std::cerr << "\n***************************************\n" |
| 208 | << "Step " << nStep++ |
| 209 | << ": Will use a new trust anchor, please double check the identity info:" << std::endl |
| 210 | << "> New CA name: " << profile->m_caPrefix.toUri() << std::endl |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 211 | << "> This trust anchor information is signed by: " << reply.getSignatureInfo().getKeyLocator() << std::endl |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 212 | << "> The certificate: " << profile->m_cert << std::endl |
Davide Pesavento | b48bbda | 2020-07-27 19:41:37 -0400 | [diff] [blame] | 213 | << "Do you trust the information? Type in YES or NO" << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 214 | |
| 215 | std::string answer; |
| 216 | getline(std::cin, answer); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 217 | boost::algorithm::to_lower(answer); |
| 218 | if (answer == "yes") { |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 219 | std::cerr << "You answered YES: new CA " << profile->m_caPrefix.toUri() << " will be used" << std::endl; |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 220 | runProbe(*profile); |
Zhiyi Zhang | caab546 | 2019-10-18 13:41:02 -0700 | [diff] [blame] | 221 | // client.getClientConf().save(std::string(SYSCONFDIR) + "/ndncert/client.conf"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 222 | } |
| 223 | else { |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 224 | std::cerr << "You answered NO: new CA " << profile->m_caPrefix.toUri() << " will not be used" << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 225 | exit(0); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | static void |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 230 | probeCb(const Data& reply, CaProfile profile) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 231 | { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 232 | std::vector<std::pair<Name, int>> names; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 233 | std::vector<Name> redirects; |
| 234 | Requester::onProbeResponse(reply, profile, names, redirects); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 235 | size_t count = 0; |
| 236 | std::cerr << "\n***************************************\n" |
| 237 | << "Step " << nStep++ |
| 238 | << ": You can either select one of the following names suggested by the CA: " << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 239 | for (const auto& name : names) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 240 | std::cerr << "> Index: " << count++ << std::endl |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 241 | << ">> Suggested name: " << name.first.toUri() << std::endl |
| 242 | << ">> Corresponding Max sufiix length: " << name.second << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 243 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 244 | std::cerr << "\nOr choose another trusted CA suggested by the CA: " << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 245 | for (const auto& redirect : redirects) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 246 | std::cerr << "> Index: " << count++ << std::endl |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 247 | << ">> Suggested CA: " << security::extractIdentityFromCertName(redirect.getPrefix(-1)) << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 248 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 249 | std::cerr << "Please type in the index of your choice:" << std::endl; |
| 250 | size_t index = 0; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 251 | try { |
| 252 | std::string input; |
| 253 | getline(std::cin, input); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 254 | index = std::stoul(input); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 255 | } |
| 256 | catch (const std::exception& e) { |
| 257 | std::cerr << "Your input is Invalid. Exit" << std::endl; |
| 258 | exit(0); |
| 259 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 260 | if (index >= names.size() + redirects.size()) { |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 261 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 262 | return; |
| 263 | } |
| 264 | if (index < names.size()) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 265 | //names |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 266 | std::cerr << "You selected name: " << names[index].first.toUri() << std::endl; |
| 267 | //TODO add prompt to "add suffix" |
| 268 | runNew(profile, names[index].first); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 269 | } |
| 270 | else { |
| 271 | //redirects |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 272 | auto redirectedCaFullName = redirects[index - names.size()]; |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 273 | auto redirectedCaName = security::extractIdentityFromCertName(redirectedCaFullName.getPrefix(-1)); |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 274 | std::cerr << "You selected to be redirected to CA: " << redirectedCaName.toUri() << std::endl; |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 275 | face.expressInterest( |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 276 | *Requester::genCaProfileDiscoveryInterest(redirectedCaName), |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 277 | [&](const Interest&, const Data& data) { |
| 278 | auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data); |
| 279 | face.expressInterest(*fetchingInterest, |
| 280 | bind(&InfoCb, _2, redirectedCaFullName), |
| 281 | bind(&onNackCb), |
| 282 | bind(&timeoutCb)); |
| 283 | }, |
| 284 | bind(&onNackCb), |
| 285 | bind(&timeoutCb)); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | static void |
| 290 | selectCaProfile(std::string configFilePath) |
| 291 | { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 292 | ProfileStorage profileStorage; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 293 | try { |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 294 | profileStorage.load(configFilePath); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 295 | } |
| 296 | catch (const std::exception& e) { |
| 297 | std::cerr << "Cannot load the configuration file: " << e.what() << std::endl; |
| 298 | exit(1); |
| 299 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 300 | size_t count = 0; |
| 301 | std::cerr << "***************************************\n" |
| 302 | << "Step " << nStep++ << ": CA SELECTION" << std::endl; |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 303 | for (auto item : profileStorage.m_caItems) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 304 | std::cerr << "> Index: " << count++ << std::endl |
| 305 | << ">> CA prefix:" << item.m_caPrefix << std::endl |
| 306 | << ">> Introduction: " << item.m_caInfo << std::endl; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 307 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 308 | 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] | 309 | |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 310 | std::string caIndexS, caIndexSLower; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 311 | getline(std::cin, caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 312 | caIndexSLower = caIndexS; |
| 313 | boost::algorithm::to_lower(caIndexSLower); |
| 314 | if (caIndexSLower == "none") { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 315 | std::cerr << "\n***************************************\n" |
| 316 | << "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] | 317 | std::string expectedCAName; |
| 318 | getline(std::cin, expectedCAName); |
Zhiyi Zhang | fbcab84 | 2020-10-07 15:17:13 -0700 | [diff] [blame] | 319 | face.expressInterest( |
| 320 | *Requester::genCaProfileDiscoveryInterest(Name(expectedCAName)), |
| 321 | [&](const Interest&, const Data& data) { |
| 322 | auto fetchingInterest = Requester::genCaProfileInterestFromDiscoveryResponse(data); |
| 323 | face.expressInterest(*fetchingInterest, |
| 324 | bind(&InfoCb, _2, Name()), |
| 325 | bind(&onNackCb), |
| 326 | bind(&timeoutCb)); |
| 327 | }, |
| 328 | bind(&onNackCb), |
| 329 | bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 330 | } |
| 331 | else { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 332 | size_t caIndex; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 333 | try { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 334 | caIndex = std::stoul(caIndexS); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 335 | } |
| 336 | catch (const std::exception& e) { |
| 337 | std::cerr << "Your input is neither NONE nor a valid index. Exit" << std::endl; |
| 338 | return; |
| 339 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 340 | if (caIndex >= count) { |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 341 | std::cerr << "Your input is not an existing index. Exit" << std::endl; |
| 342 | return; |
| 343 | } |
Zhiyi Zhang | a16b758 | 2020-10-29 18:59:46 -0700 | [diff] [blame] | 344 | auto itemIterator = profileStorage.m_caItems.cbegin(); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 345 | std::advance(itemIterator, caIndex); |
| 346 | auto targetCaItem = *itemIterator; |
| 347 | runProbe(targetCaItem); |
| 348 | } |
| 349 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 350 | |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 351 | static void |
| 352 | runProbe(CaProfile profile) |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 353 | { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 354 | std::cerr << "\n***************************************\n" |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 355 | << "Step " << nStep++ |
| 356 | << ": Do you know your identity name to be certified by CA " |
| 357 | << profile.m_caPrefix.toUri() |
| 358 | << " already? Type in YES or NO" << std::endl; |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 359 | bool validAnswer = false; |
| 360 | while (!validAnswer) { |
| 361 | std::string answer; |
| 362 | getline(std::cin, answer); |
| 363 | boost::algorithm::to_lower(answer); |
| 364 | if (answer == "yes") { |
| 365 | validAnswer = true; |
| 366 | std::cerr << "You answered YES" << std::endl; |
| 367 | std::cerr << "\n***************************************\n" |
| 368 | << "Step " << nStep++ |
Zhiyi Zhang | 6bb1d08 | 2020-10-08 14:25:21 -0700 | [diff] [blame] | 369 | << ": Please type in the full identity name you want to get (with CA prefix " |
| 370 | << profile.m_caPrefix.toUri() |
| 371 | << "):" << std::endl; |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 372 | std::string identityNameStr; |
| 373 | getline(std::cin, identityNameStr); |
| 374 | runNew(profile, Name(identityNameStr)); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 375 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 376 | else if (answer == "no") { |
| 377 | validAnswer = true; |
| 378 | std::cerr << "You answered NO" << std::endl; |
| 379 | std::cerr << "\n***************************************\n" |
| 380 | << "Step " << nStep++ << ": Please provide information for name assignment" << std::endl; |
| 381 | auto capturedParams = captureParams(profile.m_probeParameterKeys); |
Zhiyi Zhang | db1ec76 | 2020-10-30 08:58:39 -0700 | [diff] [blame] | 382 | face.expressInterest(*Requester::genProbeInterest(profile, std::move(capturedParams)), |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 383 | bind(&probeCb, _2, profile), bind(&onNackCb), bind(&timeoutCb)); |
| 384 | } |
| 385 | else { |
| 386 | std::cerr << "Invalid answer. Type in YES or NO" << std::endl; |
| 387 | } |
| 388 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static void |
| 392 | runNew(CaProfile profile, Name identityName) |
| 393 | { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 394 | int validityPeriod = captureValidityPeriod(); |
| 395 | auto now = time::system_clock::now(); |
| 396 | std::cerr << "The validity period of your certificate will be: " << validityPeriod << " hours" << std::endl; |
Zhiyi Zhang | 59a366d | 2020-10-29 19:01:53 -0700 | [diff] [blame] | 397 | requesterState =std::make_shared<RequestContext>(keyChain, profile, RequestType::NEW); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 398 | auto interest = Requester::genNewInterest(*requesterState, identityName, now, now + time::hours(validityPeriod)); |
| 399 | if (interest != nullptr) { |
| 400 | face.expressInterest(*interest, bind(&newCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
| 401 | } |
| 402 | else { |
| 403 | std::cerr << "Cannot generate the Interest for NEW step. Exit" << std::endl; |
| 404 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static void |
| 408 | runChallenge(const std::string& challengeType) |
| 409 | { |
Zhiyi Zhang | c5d93a9 | 2020-10-14 17:07:35 -0700 | [diff] [blame] | 410 | std::vector<std::tuple<std::string, std::string>> requirement; |
| 411 | try { |
| 412 | requirement = Requester::selectOrContinueChallenge(*requesterState, challengeType); |
| 413 | } |
| 414 | catch (const std::exception& e) { |
| 415 | std::cerr << "Error. Cannot successfully load the Challenge Module with error: " << std::string(e.what()) |
| 416 | << "Exit." << std::endl; |
| 417 | exit(1); |
| 418 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 419 | if (requirement.size() > 0) { |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 420 | std::cerr << "\n***************************************\n" |
| 421 | << "Step " << nStep |
| 422 | << ": Please provide parameters used for Identity Verification Challenge" << std::endl; |
| 423 | captureParams(requirement); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 424 | } |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 425 | face.expressInterest(*Requester::genChallengeInterest(*requesterState, std::move(requirement)), |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 426 | bind(&challengeCb, _2), bind(&onNackCb), bind(&timeoutCb)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 429 | static void |
| 430 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 431 | { |
| 432 | if (error) { |
| 433 | return; |
| 434 | } |
| 435 | const char* signalName = ::strsignal(signalNum); |
| 436 | std::cerr << "Exiting on signal "; |
| 437 | if (signalName == nullptr) { |
| 438 | std::cerr << signalNum; |
| 439 | } |
| 440 | else { |
| 441 | std::cerr << signalName; |
| 442 | } |
| 443 | std::cerr << std::endl; |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 444 | if (requesterState) { |
| 445 | Requester::endSession(*requesterState); |
| 446 | } |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 447 | face.getIoService().stop(); |
tylerliu | feabfdc | 2020-10-03 15:09:58 -0700 | [diff] [blame] | 448 | exit(1); |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 449 | } |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 450 | |
| 451 | int |
| 452 | main(int argc, char* argv[]) |
| 453 | { |
Zhiyi Zhang | ad9e04f | 2020-03-27 12:04:31 -0700 | [diff] [blame] | 454 | boost::asio::signal_set terminateSignals(face.getIoService()); |
| 455 | terminateSignals.add(SIGINT); |
| 456 | terminateSignals.add(SIGTERM); |
| 457 | terminateSignals.async_wait(handleSignal); |
| 458 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 459 | namespace po = boost::program_options; |
Zhiyi Zhang | 840afd9 | 2020-10-21 13:24:08 -0700 | [diff] [blame] | 460 | std::string configFilePath = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/client.conf"; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 461 | po::options_description description("General Usage\n ndncert-client [-h] [-c] [-v]\n"); |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 462 | description.add_options()("help,h", "produce help message")("config-file,c", po::value<std::string>(&configFilePath), "configuration file name"); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 463 | po::positional_options_description p; |
| 464 | |
| 465 | po::variables_map vm; |
| 466 | try { |
| 467 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 468 | po::notify(vm); |
| 469 | } |
| 470 | catch (const std::exception& e) { |
| 471 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 472 | return 1; |
| 473 | } |
| 474 | if (vm.count("help") != 0) { |
| 475 | std::cerr << description << std::endl; |
| 476 | return 0; |
| 477 | } |
Zhiyi Zhang | 837406d | 2020-10-05 22:01:31 -0700 | [diff] [blame] | 478 | selectCaProfile(configFilePath); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 479 | face.processEvents(); |
| 480 | return 0; |
| 481 | } |
| 482 | |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 483 | } // namespace requester |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 484 | } // namespace ndncert |
| 485 | } // namespace ndn |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 486 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 487 | int |
| 488 | main(int argc, char* argv[]) |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 489 | { |
Zhiyi Zhang | 3002e6b | 2020-10-29 18:54:07 -0700 | [diff] [blame] | 490 | return ndn::ndncert::requester::main(argc, argv); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 491 | } |