blob: 5f0e3a3a7cc198798fcdef878be1deacebe0bba4 [file] [log] [blame]
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod4496f72019-01-19 20:23:50 -05002/*
Davide Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2017-2021, Regents of the University of California.
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08004 *
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 Pesavento0dc02012021-11-23 22:55:03 -050022
Zhiyi Zhang90c75782020-10-06 15:04:03 -070023#include <boost/asio.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050024#include <boost/asio/ip/tcp.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050025#include <boost/program_options/options_description.hpp>
26#include <boost/program_options/parsers.hpp>
27#include <boost/program_options/variables_map.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050028#include <iostream>
tylerliu3ad68122020-10-17 12:10:34 -070029#include <chrono>
Zhiyi Zhangaa60c962021-01-22 10:57:41 -080030#include <deque>
Davide Pesavento0dc02012021-11-23 22:55:03 -050031
Zhiyi Zhang74c61142020-10-07 21:00:49 -070032#include <ndn-cxx/face.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080034
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080035namespace ndncert {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070036namespace ca {
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080037
Davide Pesavento0dc02012021-11-23 22:55:03 -050038static ndn::Face face;
39static ndn::KeyChain keyChain;
40static std::string repoHost = "localhost";
41static std::string repoPort = "7376";
Zhiyi Zhangaa60c962021-01-22 10:57:41 -080042const size_t MAX_CACHED_CERT_NUM = 100;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070043
44static bool
45writeDataToRepo(const Data& data) {
46 boost::asio::ip::tcp::iostream requestStream;
tylerliu99e72ac2020-10-17 12:28:56 -070047#if BOOST_VERSION >= 106600
tylerliu9ea1f762020-10-17 11:53:36 -070048 requestStream.expires_after(std::chrono::seconds(3));
49#else
tylerliu01e59632020-10-17 12:15:02 -070050 requestStream.expires_from_now(boost::posix_time::seconds(3));
tylerliu99e72ac2020-10-17 12:28:56 -070051#endif //BOOST_VERSION >= 106600
Zhiyi Zhang74c61142020-10-07 21:00:49 -070052 requestStream.connect(repoHost, repoPort);
53 if (!requestStream) {
Zhiyi Zhangaa60c962021-01-22 10:57:41 -080054 std::cerr << "ERROR: Cannot publish the certificate to repo-ng"
Zhiyi Zhang74c61142020-10-07 21:00:49 -070055 << " (" << requestStream.error().message() << ")" << std::endl;
56 return false;
57 }
58 requestStream.write(reinterpret_cast<const char*>(data.wireEncode().wire()),
59 data.wireEncode().size());
60 return true;
61}
Zhiyi Zhang90c75782020-10-06 15:04:03 -070062
63static void
64handleSignal(const boost::system::error_code& error, int signalNum)
65{
66 if (error) {
67 return;
68 }
69 const char* signalName = ::strsignal(signalNum);
70 std::cerr << "Exiting on signal ";
71 if (signalName == nullptr) {
72 std::cerr << signalNum;
73 }
74 else {
75 std::cerr << signalName;
76 }
77 std::cerr << std::endl;
78 face.getIoService().stop();
79 exit(1);
80}
81
Davide Pesaventod4496f72019-01-19 20:23:50 -050082static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080083main(int argc, char* argv[])
84{
Zhiyi Zhang90c75782020-10-06 15:04:03 -070085 boost::asio::signal_set terminateSignals(face.getIoService());
86 terminateSignals.add(SIGINT);
87 terminateSignals.add(SIGTERM);
88 terminateSignals.async_wait(handleSignal);
89
Zhiyi Zhang840afd92020-10-21 13:24:08 -070090 std::string configFilePath(NDNCERT_SYSCONFDIR "/ndncert/ca.conf");
Davide Pesaventod4496f72019-01-19 20:23:50 -050091 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080092
93 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050094 po::options_description optsDesc("Options");
95 optsDesc.add_options()
Zhiyi Zhang74c61142020-10-07 21:00:49 -070096 ("help,h", "print this help message and exit")
97 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), "path to configuration file")
98 ("repo-output,r", po::bool_switch(&wantRepoOut), "when enabled, all issued certificates will be published to repo-ng")
99 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
100 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800101
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800102 po::variables_map vm;
103 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -0500104 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800105 po::notify(vm);
106 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500107 catch (const po::error& e) {
108 std::cerr << "ERROR: " << e.what() << std::endl;
109 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800110 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500111 catch (const boost::bad_any_cast& e) {
112 std::cerr << "ERROR: " << e.what() << std::endl;
113 return 2;
114 }
115
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800116 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -0500117 std::cout << "Usage: " << argv[0] << " [options]\n"
118 << "\n"
119 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800120 return 0;
121 }
122
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800123 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhangaa60c962021-01-22 10:57:41 -0800124 std::deque<Data> cachedCertificates;
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700125 auto profileData = ca.getCaProfileData();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800126
Davide Pesaventod4496f72019-01-19 20:23:50 -0500127 if (wantRepoOut) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700128 writeDataToRepo(profileData);
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700129 ca.setStatusUpdateCallback([&](const RequestState& request) {
tylerliu7b9185c2020-11-24 12:15:18 -0800130 if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) {
131 writeDataToRepo(request.cert);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700132 }
133 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800134 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700135 else {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700136 ca.setStatusUpdateCallback([&](const RequestState& request) {
tylerliu7b9185c2020-11-24 12:15:18 -0800137 if (request.status == Status::SUCCESS && request.requestType == RequestType::NEW) {
Zhiyi Zhangaa60c962021-01-22 10:57:41 -0800138 cachedCertificates.push_front(request.cert);
139 if (cachedCertificates.size() > MAX_CACHED_CERT_NUM) {
140 cachedCertificates.pop_back();
141 }
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700142 }
143 });
144 face.setInterestFilter(
Davide Pesavento0dc02012021-11-23 22:55:03 -0500145 ndn::InterestFilter(ca.getCaConf().caProfile.caPrefix),
146 [&](const auto&, const auto& interest) {
Zhiyi Zhangaa60c962021-01-22 10:57:41 -0800147 const auto& interestName = interest.getName();
148 if (interestName.isPrefixOf(profileData.getName())) {
149 face.put(profileData);
150 return;
151 }
152 for (const auto& item : cachedCertificates) {
Zhiyi Zhangd35bc5f2021-01-22 17:02:41 -0800153 if (interestName.isPrefixOf(item.getFullName())) {
Zhiyi Zhangaa60c962021-01-22 10:57:41 -0800154 face.put(item);
155 return;
156 }
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700157 }
158 },
159 [](const Name&, const std::string& errorInfo) {
160 std::cerr << "ERROR: " << errorInfo << std::endl;
161 });
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700162 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800163
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800164 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800165 return 0;
166}
167
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700168} // namespace ca
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700169} // namespace ndncert
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800170
171int
172main(int argc, char* argv[])
173{
Davide Pesavento0dc02012021-11-23 22:55:03 -0500174 return ndncert::ca::main(argc, argv);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800175}