blob: a3e1c1f850c96562ffa162a696d02a611de3a442 [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 Zhang08e0e982017-03-01 10:10:42 -080080
Davide Pesaventod4496f72019-01-19 20:23:50 -050081 if (wantRepoOut) {
tylerliu8704d032020-06-23 10:18:15 -070082 ca.setStatusUpdateCallback([&] (const CaState& request) {
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070083 if (request.m_status == Status::SUCCESS && request.m_requestType == RequestType::NEW) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084 auto issuedCert = request.m_cert;
Davide Pesaventod4496f72019-01-19 20:23:50 -050085 boost::asio::ip::tcp::iostream requestStream;
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070086 requestStream.expires_after(std::chrono::seconds(3));
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080087 requestStream.connect(repoHost, repoPort);
88 if (!requestStream) {
89 std::cerr << "ERROR: Cannot publish certificate to repo-ng"
Davide Pesaventod4496f72019-01-19 20:23:50 -050090 << " (" << requestStream.error().message() << ")" << std::endl;
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080091 return;
92 }
93 requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()),
94 issuedCert.wireEncode().size());
95 }
Davide Pesaventod4496f72019-01-19 20:23:50 -050096 });
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -080097 }
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070098 else {
99
100 }
Zhiyi Zhang343cdfb2018-01-17 12:04:28 -0800101
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}