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 | /* |
Alexander Afanasyev | 7838cfd | 2020-06-03 14:16:43 -0400 | [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 | |
| 21 | #include "ca-module.hpp" |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 22 | #include "challenge-module.hpp" |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/face.hpp> |
Alexander Afanasyev | 7838cfd | 2020-06-03 14:16:43 -0400 | [diff] [blame] | 24 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame^] | 25 | #include <boost/asio.hpp> |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 26 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 27 | #include <boost/program_options/options_description.hpp> |
| 28 | #include <boost/program_options/parsers.hpp> |
| 29 | #include <boost/program_options/variables_map.hpp> |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 30 | #include <iostream> |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace ndncert { |
| 34 | |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame^] | 35 | Face face; |
| 36 | security::v2::KeyChain keyChain; |
| 37 | |
| 38 | static void |
| 39 | handleSignal(const boost::system::error_code& error, int signalNum) |
| 40 | { |
| 41 | if (error) { |
| 42 | return; |
| 43 | } |
| 44 | const char* signalName = ::strsignal(signalNum); |
| 45 | std::cerr << "Exiting on signal "; |
| 46 | if (signalName == nullptr) { |
| 47 | std::cerr << signalNum; |
| 48 | } |
| 49 | else { |
| 50 | std::cerr << signalName; |
| 51 | } |
| 52 | std::cerr << std::endl; |
| 53 | face.getIoService().stop(); |
| 54 | exit(1); |
| 55 | } |
| 56 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 57 | static int |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 58 | main(int argc, char* argv[]) |
| 59 | { |
Zhiyi Zhang | 90c7578 | 2020-10-06 15:04:03 -0700 | [diff] [blame^] | 60 | boost::asio::signal_set terminateSignals(face.getIoService()); |
| 61 | terminateSignals.add(SIGINT); |
| 62 | terminateSignals.add(SIGTERM); |
| 63 | terminateSignals.async_wait(handleSignal); |
| 64 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 65 | std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf"); |
| 66 | std::string repoHost("localhost"); |
| 67 | std::string repoPort("7376"); |
| 68 | bool wantRepoOut = false; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 69 | |
| 70 | namespace po = boost::program_options; |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 71 | po::options_description optsDesc("Options"); |
| 72 | optsDesc.add_options() |
| 73 | ("help,h", "print this help message and exit") |
| 74 | ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), |
| 75 | "path to configuration file") |
| 76 | ("repo-output,r", po::bool_switch(&wantRepoOut), |
| 77 | "when enabled, all issued certificates will be published to repo-ng") |
| 78 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host") |
| 79 | ("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] | 80 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 81 | po::variables_map vm; |
| 82 | try { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 83 | po::store(po::parse_command_line(argc, argv, optsDesc), vm); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 84 | po::notify(vm); |
| 85 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 86 | catch (const po::error& e) { |
| 87 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 88 | return 2; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 89 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 90 | catch (const boost::bad_any_cast& e) { |
| 91 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 92 | return 2; |
| 93 | } |
| 94 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 95 | if (vm.count("help") != 0) { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 96 | std::cout << "Usage: " << argv[0] << " [options]\n" |
| 97 | << "\n" |
| 98 | << optsDesc; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 102 | CaModule ca(face, keyChain, configFilePath); |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 103 | std::map<Name, security::v2::Certificate> cachedCertificates; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 104 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 105 | if (wantRepoOut) { |
tylerliu | 8704d03 | 2020-06-23 10:18:15 -0700 | [diff] [blame] | 106 | ca.setStatusUpdateCallback([&] (const CaState& request) { |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 107 | if (request.m_status == Status::SUCCESS) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 108 | auto issuedCert = request.m_cert; |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 109 | boost::asio::ip::tcp::iostream requestStream; |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 110 | requestStream.expires_after(std::chrono::seconds(3)); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 111 | requestStream.connect(repoHost, repoPort); |
| 112 | if (!requestStream) { |
| 113 | std::cerr << "ERROR: Cannot publish certificate to repo-ng" |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 114 | << " (" << requestStream.error().message() << ")" << std::endl; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()), |
| 118 | issuedCert.wireEncode().size()); |
| 119 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 120 | }); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 121 | } |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 122 | else { |
Zhiyi Zhang | b420e26 | 2020-10-05 20:34:54 -0700 | [diff] [blame] | 123 | ca.setStatusUpdateCallback([&](const CaState& request) { |
| 124 | if (request.m_status == Status::SUCCESS) { |
| 125 | cachedCertificates[request.m_cert.getName()] = request.m_cert; |
| 126 | } |
| 127 | }); |
| 128 | face.setInterestFilter( |
| 129 | InterestFilter(ca.getCaConf().m_caItem.m_caPrefix), |
| 130 | [&](const InterestFilter&, const Interest& interest) { |
| 131 | auto search = cachedCertificates.find(interest.getName()); |
| 132 | if (search != cachedCertificates.end()) { |
| 133 | face.put(search->second); |
| 134 | } |
| 135 | }, |
| 136 | [](const Name&, const std::string& errorInfo) { |
| 137 | std::cerr << "ERROR: " << errorInfo << std::endl; |
| 138 | }); |
Zhiyi Zhang | d1d9f5a | 2020-10-05 18:04:23 -0700 | [diff] [blame] | 139 | } |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 140 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 141 | face.processEvents(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | } // namespace ndncert |
| 146 | } // namespace ndn |
| 147 | |
| 148 | int |
| 149 | main(int argc, char* argv[]) |
| 150 | { |
| 151 | return ndn::ndncert::main(argc, argv); |
| 152 | } |