blob: 078665425c795b1079d0a23a6d878ecdadf027bd [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"
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080023
Davide Pesaventod4496f72019-01-19 20:23:50 -050024#include <ndn-cxx/face.hpp>
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -040025#include <ndn-cxx/security/key-chain.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050026
27#include <boost/asio/ip/tcp.hpp>
28#if BOOST_VERSION < 106700
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080029#include <boost/date_time/posix_time/posix_time_duration.hpp>
Davide Pesaventod4496f72019-01-19 20:23:50 -050030#endif
31#include <boost/program_options/options_description.hpp>
32#include <boost/program_options/parsers.hpp>
33#include <boost/program_options/variables_map.hpp>
34
35#include <iostream>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080036
37namespace ndn {
38namespace ndncert {
39
Davide Pesaventod4496f72019-01-19 20:23:50 -050040static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080041main(int argc, char* argv[])
42{
Davide Pesaventod4496f72019-01-19 20:23:50 -050043 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
44 std::string repoHost("localhost");
45 std::string repoPort("7376");
46 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080047
48 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050049 po::options_description optsDesc("Options");
50 optsDesc.add_options()
51 ("help,h", "print this help message and exit")
52 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath),
53 "path to configuration file")
54 ("repo-output,r", po::bool_switch(&wantRepoOut),
55 "when enabled, all issued certificates will be published to repo-ng")
56 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
57 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080058
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080059 po::variables_map vm;
60 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050061 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080062 po::notify(vm);
63 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050064 catch (const po::error& e) {
65 std::cerr << "ERROR: " << e.what() << std::endl;
66 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080067 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050068 catch (const boost::bad_any_cast& e) {
69 std::cerr << "ERROR: " << e.what() << std::endl;
70 return 2;
71 }
72
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080073 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -050074 std::cout << "Usage: " << argv[0] << " [options]\n"
75 << "\n"
76 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080077 return 0;
78 }
79
80 Face face;
81 security::v2::KeyChain keyChain;
82 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080083
Davide Pesaventod4496f72019-01-19 20:23:50 -050084 if (wantRepoOut) {
tylerliu8704d032020-06-23 10:18:15 -070085 ca.setStatusUpdateCallback([&] (const CaState& request) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070086 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087 auto issuedCert = request.m_cert;
Davide Pesaventod4496f72019-01-19 20:23:50 -050088 boost::asio::ip::tcp::iostream requestStream;
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070089 requestStream.expires_after(std::chrono::seconds(3));
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080090 requestStream.connect(repoHost, repoPort);
91 if (!requestStream) {
92 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
Davide Pesaventod4496f72019-01-19 20:23:50 -050093 << " (" << requestStream.error().message() << ")" << std::endl;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080094 return;
95 }
96 requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()),
97 issuedCert.wireEncode().size());
98 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050099 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800100 }
101
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800102 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800103 return 0;
104}
105
106} // namespace ndncert
107} // namespace ndn
108
109int
110main(int argc, char* argv[])
111{
112 return ndn::ndncert::main(argc, argv);
113}