Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 478206e | 2017-10-15 00:22:35 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library 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 Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 25 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | int |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | ndnsec_cert_dump(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | namespace po = boost::program_options; |
| 33 | |
| 34 | std::string name; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | bool isIdentityName = false; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 36 | bool isKeyName = false; |
| 37 | bool isFileName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | bool isPretty = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | bool isRepoOut = false; |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 40 | std::string repoHost; |
| 41 | std::string repoPort; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 43 | po::options_description description( |
| 44 | "Usage: ndnsec cert-dump [-h] [-p] [-r [-H HOST] [-P PORT]] [-i|-k|-f] [-n] NAME\n" |
| 45 | "\n" |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 46 | "Display a certificate from a file or the local PIB.\n" |
| 47 | "\n" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 48 | "Options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | description.add_options() |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 50 | ("help,h", "produce help message") |
| 51 | ("pretty,p", po::bool_switch(&isPretty), "display certificate in human readable format") |
| 52 | ("identity,i", po::bool_switch(&isIdentityName), |
| 53 | "treat the NAME argument as an identity name (e.g., /ndn/edu/ucla/alice)") |
| 54 | ("key,k", po::bool_switch(&isKeyName), |
| 55 | "treat the NAME argument as a key name (e.g., /ndn/edu/ucla/alice/ksk-123456789)") |
| 56 | ("file,f", po::bool_switch(&isFileName), |
| 57 | "treat the NAME argument as the name of a file containing a base64-encoded " |
| 58 | "certificate, '-' for stdin") |
| 59 | ("name,n", po::value<std::string>(&name), |
| 60 | "unless overridden by -i/-k/-f, the name of the certificate to be exported " |
| 61 | "(e.g., /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF)") |
| 62 | ("repo-output,r", po::bool_switch(&isRepoOut), |
Zhiyi Zhang | 4c68b6e | 2020-04-20 16:13:21 -0700 | [diff] [blame] | 63 | "publish the certificate into an NDN repo instance") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 64 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), |
| 65 | "repo hostname if --repo-output is specified") |
| 66 | ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), |
| 67 | "repo port number if --repo-output is specified") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | ; |
| 69 | |
| 70 | po::positional_options_description p; |
| 71 | p.add("name", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 73 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 74 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 75 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 76 | po::notify(vm); |
| 77 | } |
| 78 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 79 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 80 | << description << std::endl; |
| 81 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 82 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 83 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 84 | if (vm.count("help") > 0) { |
| 85 | std::cout << description << std::endl; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 89 | if (vm.count("name") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 90 | std::cerr << "ERROR: you must specify a name" << std::endl; |
| 91 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 92 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 93 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 94 | int nIsNameOptions = isIdentityName + isKeyName + isFileName; |
| 95 | if (nIsNameOptions > 1) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 96 | std::cerr << "ERROR: at most one of '--identity', '--key', " |
| 97 | "or '--file' may be specified" << std::endl; |
| 98 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 99 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 100 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 101 | if (isPretty && isRepoOut) { |
| 102 | std::cerr << "ERROR: '--pretty' is incompatible with '--repo-output'" << std::endl; |
| 103 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 104 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 105 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 106 | security::Certificate certificate; |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 107 | if (isFileName) { |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 108 | certificate = loadFromFile<security::Certificate>(name); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 109 | } |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 110 | else { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 111 | KeyChain keyChain; |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 112 | certificate = getCertificateFromPib(keyChain.getPib(), name, |
| 113 | isIdentityName, isKeyName, nIsNameOptions == 0); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 114 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 116 | if (isPretty) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 117 | std::cout << certificate << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 118 | return 0; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 119 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 120 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 121 | if (isRepoOut) { |
| 122 | boost::asio::ip::tcp::iostream requestStream; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 123 | requestStream.expires_after(std::chrono::seconds(10)); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 124 | requestStream.connect(repoHost, repoPort); |
| 125 | if (!requestStream) { |
| 126 | std::cerr << "ERROR: Failed to connect to repo instance" << std::endl; |
| 127 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 128 | } |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 129 | requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().data()), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 130 | certificate.wireEncode().size()); |
| 131 | return 0; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 132 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 133 | |
| 134 | io::save(certificate, std::cout); |
| 135 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 139 | } // namespace ndn::ndnsec |