blob: 232c0d998952b3924ab8411ab71d6f7a863b52da [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 Zhang343cdfb2018-01-17 12:04:28 -080022#include "challenge-module.hpp"
Davide Pesaventod4496f72019-01-19 20:23:50 -050023#include <ndn-cxx/face.hpp>
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -040024#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhang90c75782020-10-06 15:04:03 -070025#include <boost/asio.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050026#include <boost/asio/ip/tcp.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050027#include <boost/program_options/options_description.hpp>
28#include <boost/program_options/parsers.hpp>
29#include <boost/program_options/variables_map.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050030#include <iostream>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080031
32namespace ndn {
33namespace ndncert {
34
Zhiyi Zhang90c75782020-10-06 15:04:03 -070035Face face;
36security::v2::KeyChain keyChain;
37
38static void
39handleSignal(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 Pesaventod4496f72019-01-19 20:23:50 -050057static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080058main(int argc, char* argv[])
59{
Zhiyi Zhang90c75782020-10-06 15:04:03 -070060 boost::asio::signal_set terminateSignals(face.getIoService());
61 terminateSignals.add(SIGINT);
62 terminateSignals.add(SIGTERM);
63 terminateSignals.async_wait(handleSignal);
64
Davide Pesaventod4496f72019-01-19 20:23:50 -050065 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
66 std::string repoHost("localhost");
67 std::string repoPort("7376");
68 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080069
70 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050071 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 Zhang343cdfb2018-01-17 12:04:28 -080080
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080081 po::variables_map vm;
82 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050083 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080084 po::notify(vm);
85 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050086 catch (const po::error& e) {
87 std::cerr << "ERROR: " << e.what() << std::endl;
88 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080089 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050090 catch (const boost::bad_any_cast& e) {
91 std::cerr << "ERROR: " << e.what() << std::endl;
92 return 2;
93 }
94
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080095 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -050096 std::cout << "Usage: " << argv[0] << " [options]\n"
97 << "\n"
98 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080099 return 0;
100 }
101
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800102 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700103 std::map<Name, security::v2::Certificate> cachedCertificates;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800104
Davide Pesaventod4496f72019-01-19 20:23:50 -0500105 if (wantRepoOut) {
tylerliu8704d032020-06-23 10:18:15 -0700106 ca.setStatusUpdateCallback([&] (const CaState& request) {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700107 if (request.m_status == Status::SUCCESS) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 auto issuedCert = request.m_cert;
Davide Pesaventod4496f72019-01-19 20:23:50 -0500109 boost::asio::ip::tcp::iostream requestStream;
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700110 requestStream.expires_after(std::chrono::seconds(3));
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800111 requestStream.connect(repoHost, repoPort);
112 if (!requestStream) {
113 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
Davide Pesaventod4496f72019-01-19 20:23:50 -0500114 << " (" << requestStream.error().message() << ")" << std::endl;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800115 return;
116 }
117 requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()),
118 issuedCert.wireEncode().size());
119 }
Davide Pesaventod4496f72019-01-19 20:23:50 -0500120 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800121 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700122 else {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700123 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 Zhangd1d9f5a2020-10-05 18:04:23 -0700139 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800140
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800141 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800142 return 0;
143}
144
145} // namespace ndncert
146} // namespace ndn
147
148int
149main(int argc, char* argv[])
150{
151 return ndn::ndncert::main(argc, argv);
152}