blob: a32ad10dbe503e1c46937d82caf140548b6bfce5 [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>
Davide Pesaventod4496f72019-01-19 20:23:50 -050028#include <boost/program_options/options_description.hpp>
29#include <boost/program_options/parsers.hpp>
30#include <boost/program_options/variables_map.hpp>
31
32#include <iostream>
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080033
34namespace ndn {
35namespace ndncert {
36
Davide Pesaventod4496f72019-01-19 20:23:50 -050037static int
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080038main(int argc, char* argv[])
39{
Davide Pesaventod4496f72019-01-19 20:23:50 -050040 std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf");
41 std::string repoHost("localhost");
42 std::string repoPort("7376");
43 bool wantRepoOut = false;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080044
45 namespace po = boost::program_options;
Davide Pesaventod4496f72019-01-19 20:23:50 -050046 po::options_description optsDesc("Options");
47 optsDesc.add_options()
48 ("help,h", "print this help message and exit")
49 ("config-file,c", po::value<std::string>(&configFilePath)->default_value(configFilePath),
50 "path to configuration file")
51 ("repo-output,r", po::bool_switch(&wantRepoOut),
52 "when enabled, all issued certificates will be published to repo-ng")
53 ("repo-host,H", po::value<std::string>(&repoHost)->default_value(repoHost), "repo-ng host")
54 ("repo-port,P", po::value<std::string>(&repoPort)->default_value(repoPort), "repo-ng port");
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080055
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080056 po::variables_map vm;
57 try {
Davide Pesaventod4496f72019-01-19 20:23:50 -050058 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080059 po::notify(vm);
60 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050061 catch (const po::error& e) {
62 std::cerr << "ERROR: " << e.what() << std::endl;
63 return 2;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080064 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050065 catch (const boost::bad_any_cast& e) {
66 std::cerr << "ERROR: " << e.what() << std::endl;
67 return 2;
68 }
69
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080070 if (vm.count("help") != 0) {
Davide Pesaventod4496f72019-01-19 20:23:50 -050071 std::cout << "Usage: " << argv[0] << " [options]\n"
72 << "\n"
73 << optsDesc;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080074 return 0;
75 }
76
77 Face face;
78 security::v2::KeyChain keyChain;
79 CaModule ca(face, keyChain, configFilePath);
Zhiyi Zhangb420e262020-10-05 20:34:54 -070080 std::map<Name, security::v2::Certificate> cachedCertificates;
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080081
Davide Pesaventod4496f72019-01-19 20:23:50 -050082 if (wantRepoOut) {
tylerliu8704d032020-06-23 10:18:15 -070083 ca.setStatusUpdateCallback([&] (const CaState& request) {
Zhiyi Zhangb420e262020-10-05 20:34:54 -070084 if (request.m_status == Status::SUCCESS) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 auto issuedCert = request.m_cert;
Davide Pesaventod4496f72019-01-19 20:23:50 -050086 boost::asio::ip::tcp::iostream requestStream;
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070087 requestStream.expires_after(std::chrono::seconds(3));
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080088 requestStream.connect(repoHost, repoPort);
89 if (!requestStream) {
90 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
Davide Pesaventod4496f72019-01-19 20:23:50 -050091 << " (" << requestStream.error().message() << ")" << std::endl;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080092 return;
93 }
94 requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()),
95 issuedCert.wireEncode().size());
96 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050097 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080098 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070099 else {
Zhiyi Zhangb420e262020-10-05 20:34:54 -0700100 ca.setStatusUpdateCallback([&](const CaState& request) {
101 if (request.m_status == Status::SUCCESS) {
102 cachedCertificates[request.m_cert.getName()] = request.m_cert;
103 }
104 });
105 face.setInterestFilter(
106 InterestFilter(ca.getCaConf().m_caItem.m_caPrefix),
107 [&](const InterestFilter&, const Interest& interest) {
108 auto search = cachedCertificates.find(interest.getName());
109 if (search != cachedCertificates.end()) {
110 face.put(search->second);
111 }
112 },
113 [](const Name&, const std::string& errorInfo) {
114 std::cerr << "ERROR: " << errorInfo << std::endl;
115 });
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -0700116 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800117
Zhiyi Zhang1c0bd372017-12-18 18:32:55 +0800118 face.processEvents();
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800119 return 0;
120}
121
122} // namespace ndncert
123} // namespace ndn
124
125int
126main(int argc, char* argv[])
127{
128 return ndn::ndncert::main(argc, argv);
129}