Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 4 | * |
| 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 Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 22 | #include "challenge-module.hpp" |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
| 25 | #include <ndn-cxx/security/v2/key-chain.hpp> |
| 26 | |
| 27 | #include <boost/asio/ip/tcp.hpp> |
| 28 | #if BOOST_VERSION < 106700 |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 30 | #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 Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ndn { |
| 38 | namespace ndncert { |
| 39 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 40 | static int |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 41 | main(int argc, char* argv[]) |
| 42 | { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 43 | std::string configFilePath(SYSCONFDIR "/ndncert/ca.conf"); |
| 44 | std::string repoHost("localhost"); |
| 45 | std::string repoPort("7376"); |
| 46 | bool wantRepoOut = false; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 47 | |
| 48 | namespace po = boost::program_options; |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 49 | 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 Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 58 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 59 | po::variables_map vm; |
| 60 | try { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 61 | po::store(po::parse_command_line(argc, argv, optsDesc), vm); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 62 | po::notify(vm); |
| 63 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 64 | catch (const po::error& e) { |
| 65 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 66 | return 2; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 67 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 68 | catch (const boost::bad_any_cast& e) { |
| 69 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 70 | return 2; |
| 71 | } |
| 72 | |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 73 | if (vm.count("help") != 0) { |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 74 | std::cout << "Usage: " << argv[0] << " [options]\n" |
| 75 | << "\n" |
| 76 | << optsDesc; |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | Face face; |
| 81 | security::v2::KeyChain keyChain; |
| 82 | CaModule ca(face, keyChain, configFilePath); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 83 | |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 84 | if (wantRepoOut) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | ca.setStatusUpdateCallback([&] (const CertificateRequest& request) { |
| 86 | if (request.m_status == STATUS_SUCCESS) { |
| 87 | auto issuedCert = request.m_cert; |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 88 | boost::asio::ip::tcp::iostream requestStream; |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 89 | #if BOOST_VERSION >= 106700 |
| 90 | requestStream.expires_after(std::chrono::seconds(3)); |
| 91 | #else |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 92 | requestStream.expires_from_now(boost::posix_time::seconds(3)); |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 93 | #endif // BOOST_VERSION >= 106700 |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 94 | requestStream.connect(repoHost, repoPort); |
| 95 | if (!requestStream) { |
| 96 | std::cerr << "ERROR: Cannot publish certificate to repo-ng" |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 97 | << " (" << requestStream.error().message() << ")" << std::endl; |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | requestStream.write(reinterpret_cast<const char*>(issuedCert.wireEncode().wire()), |
| 101 | issuedCert.wireEncode().size()); |
| 102 | } |
Davide Pesavento | d4496f7 | 2019-01-19 20:23:50 -0500 | [diff] [blame] | 103 | }); |
Zhiyi Zhang | 343cdfb | 2018-01-17 12:04:28 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 106 | face.processEvents(); |
Zhiyi Zhang | 08e0e98 | 2017-03-01 10:10:42 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | } // namespace ndncert |
| 111 | } // namespace ndn |
| 112 | |
| 113 | int |
| 114 | main(int argc, char* argv[]) |
| 115 | { |
| 116 | return ndn::ndncert::main(argc, argv); |
| 117 | } |