blob: 470aa39e402f38aa9d47d87bd36e13dc9aa61b93 [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/*
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -04003 * Copyright (c) 2017-2020, 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"
Zhiyi Zhangdbd9d432020-10-07 15:56:27 -070022#include "identity-challenge/challenge-module.hpp"
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>
Zhiyi Zhang74c61142020-10-07 21:00:49 -070029#include <ndn-cxx/face.hpp>
30#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080031
32namespace ndn {
33namespace ndncert {
34
Zhiyi Zhang90c75782020-10-06 15:04:03 -070035Face face;
tylerliua7bea662020-10-08 18:51:02 -070036security::KeyChain keyChain;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070037std::string repoHost = "localhost";
38std::string repoPort = "7376";
39
40static bool
41writeDataToRepo(const Data& data) {
42 boost::asio::ip::tcp::iostream requestStream;
43 requestStream.expires_after(std::chrono::seconds(3));
44 requestStream.connect(repoHost, repoPort);
45 if (!requestStream) {
46 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
47 << " (" << requestStream.error().message() << ")" << std::endl;
48 return false;
49 }
50 requestStream.write(reinterpret_cast<const char*>(data.wireEncode().wire()),
51 data.wireEncode().size());
52 return true;
53}
Zhiyi Zhang90c75782020-10-06 15:04:03 -070054
55static void
56handleSignal(const boost::system::error_code& error, int signalNum)
57{
58 if (error) {
59 return;
60 }
61 const char* signalName = ::strsignal(signalNum);
62 std::cerr << "Exiting on signal ";
63 if (signalName == nullptr) {
64 std::cerr << signalNum;
65 }
66 else {
67 std::cerr << signalName;
68 }
69 std::cerr << std::endl;
70 face.getIoService().stop();
71 exit(1);
72}
73
Davide Pesaventod4496f72019-01-19 20:23:50 -050074static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080075main(int argc, char* argv[])
76{
Zhiyi Zhang90c75782020-10-06 15:04:03 -070077 boost::asio::signal_set terminateSignals(face.getIoService());
78 terminateSignals.add(SIGINT);
79 terminateSignals.add(SIGTERM);
80 terminateSignals.async_wait(handleSignal);
81
Davide Pesaventod4496f72019-01-19 20:23:50 -050082 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
Davide Pesaventod4496f72019-01-19 20:23:50 -050083 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080084
85 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050086 po::options_description optsDesc("Options");
87 optsDesc.add_options()
Zhiyi Zhang74c61142020-10-07 21:00:49 -070088 ("help,h", "print this help message and exit")
89 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath), "path to configuration file")
90 ("repo-output,r", po::bool_switch(&wantRepoOut), "when enabled, all issued certificates will be published to repo-ng")
91 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
92 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080093
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080094 po::variables_map vm;
95 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050096 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080097 po::notify(vm);
98 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050099 catch (const po::error& e) {
100 std::cerr << "ERROR: " << e.what() << std::endl;
101 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800102 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500103 catch (const boost::bad_any_cast& e) {
104 std::cerr << "ERROR: " << e.what() << std::endl;
105 return 2;
106 }
107
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800108 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -0500109 std::cout << "Usage: " << argv[0] << " [options]\n"
110 << "\n"
111 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800112 return 0;
113 }
114
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800115 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700116 std::map<Name, Data> cachedCertificates;
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700117 auto profileData = ca.getCaProfileData();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800118
Davide Pesaventod4496f72019-01-19 20:23:50 -0500119 if (wantRepoOut) {
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700120 writeDataToRepo(profileData);
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700121 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700122 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700123 writeDataToRepo(request.m_cert);
124 }
125 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800126 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700127 else {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700128 ca.setStatusUpdateCallback([&](const CaState& request) {
tylerliu335e5222020-10-09 11:32:40 -0700129 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700130 cachedCertificates[request.m_cert.getName()] = request.m_cert;
131 }
132 });
Zhiyi Zhang696cd042020-10-07 21:27:36 -0700133 cachedCertificates[profileData.getName()] = profileData;
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700134 face.setInterestFilter(
135 InterestFilter(ca.getCaConf().m_caItem.m_caPrefix),
136 [&](const InterestFilter&, const Interest& interest) {
137 auto search = cachedCertificates.find(interest.getName());
138 if (search != cachedCertificates.end()) {
139 face.put(search->second);
140 }
141 },
142 [](const Name&, const std::string& errorInfo) {
143 std::cerr << "ERROR: " << errorInfo << std::endl;
144 });
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700145 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800146
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800147 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800148 return 0;
149}
150
Zhiyi Zhang74c61142020-10-07 21:00:49 -0700151} // namespace ndncert
152} // namespace ndn
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800153
154int
155main(int argc, char* argv[])
156{
157 return ndn::ndncert::main(argc, argv);
158}