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 | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 3 | * Copyright (c) 2017-2023, Regents of the University of California. |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "ca-module.hpp" |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 23 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | a3dbb94 | 2023-09-16 22:30:35 -0400 | [diff] [blame] | 24 | #include <boost/asio/signal_set.hpp> |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 25 | #include <boost/program_options/options_description.hpp> |
| 26 | #include <boost/program_options/parsers.hpp> |
| 27 | #include <boost/program_options/variables_map.hpp> |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 28 | |
tylerliu | 3ad6812 | 2020-10-17 12:10:34 -0700 | [diff] [blame] | 29 | #include <chrono> |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 30 | #include <deque> |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 31 | #include <iostream> |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 32 | |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/face.hpp> |
| 34 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 36 | namespace ndncert::ca { |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 37 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 38 | static ndn::Face face; |
| 39 | static ndn::KeyChain keyChain; |
| 40 | static std::string repoHost = "localhost"; |
| 41 | static std::string repoPort = "7376"; |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 42 | constexpr size_t MAX_CACHED_CERT_NUM = 100; |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 43 | |
| 44 | static bool |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 45 | writeDataToRepo(const Data& data) |
| 46 | { |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 47 | boost::asio::ip::tcp::iostream requestStream; |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 48 | requestStream.expires_after(std::chrono::seconds(5)); |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 49 | requestStream.connect(repoHost, repoPort); |
| 50 | if (!requestStream) { |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 51 | std::cerr << "ERROR: Cannot publish the certificate to repo-ng" |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 52 | << " (" << requestStream.error().message() << ")" << std::endl; |
| 53 | return false; |
| 54 | } |
Davide Pesavento | f48e163 | 2023-04-24 22:00:26 -0400 | [diff] [blame] | 55 | requestStream.write(reinterpret_cast<const char*>(data.wireEncode().data()), |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 56 | data.wireEncode().size()); |
| 57 | return true; |
| 58 | } |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame] | 59 | |
| 60 | static void |
| 61 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 62 | { |
| 63 | if (error) { |
| 64 | return; |
| 65 | } |
| 66 | const char* signalName = ::strsignal(signalNum); |
| 67 | std::cerr << "Exiting on signal "; |
| 68 | if (signalName == nullptr) { |
| 69 | std::cerr << signalNum; |
| 70 | } |
| 71 | else { |
| 72 | std::cerr << signalName; |
| 73 | } |
| 74 | std::cerr << std::endl; |
Davide Pesavento | 3e48184 | 2023-11-11 18:01:32 -0500 | [diff] [blame^] | 75 | face.getIoContext().stop(); |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame] | 76 | exit(1); |
| 77 | } |
| 78 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 79 | static int |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 80 | main(int argc, char* argv[]) |
| 81 | { |
Davide Pesavento | 3e48184 | 2023-11-11 18:01:32 -0500 | [diff] [blame^] | 82 | boost::asio::signal_set terminateSignals(face.getIoContext()); |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame] | 83 | terminateSignals.add(SIGINT); |
| 84 | terminateSignals.add(SIGTERM); |
| 85 | terminateSignals.async_wait(handleSignal); |
| 86 | |
Zhiyi Zhang | 840afd9 | 2020-10-21 13:24:08 -0700 | [diff] [blame] | 87 | std::string configFilePath(NDNCERT_SYSCONFDIR "/ndncert/ca.conf"); |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 88 | bool wantRepoOut = false; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 89 | |
| 90 | namespace po = boost::program_options; |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 91 | po::options_description optsDesc("Options"); |
| 92 | optsDesc.add_options() |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 93 | ("help,h", "print this help message and exit") |
| 94 | ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), "path to configuration file") |
| 95 | ("repo-output,r", po::bool_switch(&wantRepoOut), "when enabled, all issued certificates will be published to repo-ng") |
| 96 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host") |
| 97 | ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port"); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 98 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 99 | po::variables_map vm; |
| 100 | try { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 101 | po::store(po::parse_command_line(argc, argv, optsDesc), vm); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 102 | po::notify(vm); |
| 103 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 104 | catch (const po::error& e) { |
| 105 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 106 | return 2; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 107 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 108 | catch (const boost::bad_any_cast& e) { |
| 109 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 110 | return 2; |
| 111 | } |
| 112 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 113 | if (vm.count("help") != 0) { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 114 | std::cout << "Usage: " << argv[0] << " [options]\n" |
| 115 | << "\n" |
| 116 | << optsDesc; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 120 | CaModule ca(face, keyChain, configFilePath); |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 121 | std::deque<Data> cachedCertificates; |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 122 | auto profileData = ca.getCaProfileData(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 123 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 124 | if (wantRepoOut) { |
Zhiyi Zhang | 696cd04 | 2020-10-07 21:27:36 -0700 | [diff] [blame] | 125 | writeDataToRepo(profileData); |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 126 | ca.setStatusUpdateCallback([&](const RequestState& request) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 127 | if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) { |
| 128 | writeDataToRepo(request.cert); |
Zhiyi Zhang | 74c6114 | 2020-10-07 21:00:49 -0700 | [diff] [blame] | 129 | } |
| 130 | }); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 131 | } |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 132 | else { |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 133 | ca.setStatusUpdateCallback([&](const RequestState& request) { |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 134 | if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) { |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 135 | cachedCertificates.push_front(request.cert); |
| 136 | if (cachedCertificates.size() > MAX_CACHED_CERT_NUM) { |
| 137 | cachedCertificates.pop_back(); |
| 138 | } |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 139 | } |
| 140 | }); |
| 141 | face.setInterestFilter( |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 142 | ndn::InterestFilter(ca.getCaConf().caProfile.caPrefix), |
| 143 | [&](const auto&, const auto& interest) { |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 144 | const auto& interestName = interest.getName(); |
| 145 | if (interestName.isPrefixOf(profileData.getName())) { |
| 146 | face.put(profileData); |
| 147 | return; |
| 148 | } |
| 149 | for (const auto& item : cachedCertificates) { |
Zhiyi Zhang | d35bc5f | 2021-01-22 17:02:41 -0800 | [diff] [blame] | 150 | if (interestName.isPrefixOf(item.getFullName())) { |
Zhiyi Zhang | aa60c96 | 2021-01-22 10:57:41 -0800 | [diff] [blame] | 151 | face.put(item); |
| 152 | return; |
| 153 | } |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 154 | } |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 155 | }); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 156 | } |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 157 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 158 | face.processEvents(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 162 | } // namespace ndncert::ca |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 163 | |
| 164 | int |
| 165 | main(int argc, char* argv[]) |
| 166 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 167 | return ndncert::ca::main(argc, argv); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 168 | } |