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 | /* |
Zhiyi Zhang | 4c68b6e | 2020-04-20 16:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame^] | 25 | #include "ndn-cxx/util/io.hpp" |
| 26 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 27 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 28 | #if BOOST_VERSION < 106700 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 30 | #endif // BOOST_VERSION < 106700 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 31 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace ndnsec { |
| 34 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | int |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | ndnsec_cert_dump(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | namespace po = boost::program_options; |
| 39 | |
| 40 | std::string name; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | bool isIdentityName = false; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 42 | bool isKeyName = false; |
| 43 | bool isFileName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | bool isPretty = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | bool isRepoOut = false; |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 46 | std::string repoHost; |
| 47 | std::string repoPort; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 48 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 49 | po::options_description description( |
| 50 | "Usage: ndnsec cert-dump [-h] [-p] [-r [-H HOST] [-P PORT]] [-i|-k|-f] [-n] NAME\n" |
| 51 | "\n" |
| 52 | "Options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | description.add_options() |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 54 | ("help,h", "produce help message") |
| 55 | ("pretty,p", po::bool_switch(&isPretty), "display certificate in human readable format") |
| 56 | ("identity,i", po::bool_switch(&isIdentityName), |
| 57 | "treat the NAME argument as an identity name (e.g., /ndn/edu/ucla/alice)") |
| 58 | ("key,k", po::bool_switch(&isKeyName), |
| 59 | "treat the NAME argument as a key name (e.g., /ndn/edu/ucla/alice/ksk-123456789)") |
| 60 | ("file,f", po::bool_switch(&isFileName), |
| 61 | "treat the NAME argument as the name of a file containing a base64-encoded " |
| 62 | "certificate, '-' for stdin") |
| 63 | ("name,n", po::value<std::string>(&name), |
| 64 | "unless overridden by -i/-k/-f, the name of the certificate to be exported " |
| 65 | "(e.g., /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF)") |
| 66 | ("repo-output,r", po::bool_switch(&isRepoOut), |
Zhiyi Zhang | 4c68b6e | 2020-04-20 16:13:21 -0700 | [diff] [blame] | 67 | "publish the certificate into an NDN repo instance") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 68 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), |
| 69 | "repo hostname if --repo-output is specified") |
| 70 | ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), |
| 71 | "repo port number if --repo-output is specified") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | ; |
| 73 | |
| 74 | po::positional_options_description p; |
| 75 | p.add("name", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 77 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 78 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | 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] | 80 | po::notify(vm); |
| 81 | } |
| 82 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 83 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 84 | << description << std::endl; |
| 85 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 86 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 88 | if (vm.count("help") > 0) { |
| 89 | std::cout << description << std::endl; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 93 | if (vm.count("name") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 94 | std::cerr << "ERROR: you must specify a name" << std::endl; |
| 95 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 96 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 97 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 98 | if (isIdentityName + isKeyName + isFileName > 1) { |
| 99 | std::cerr << "ERROR: at most one of '--identity', '--key', " |
| 100 | "or '--file' may be specified" << std::endl; |
| 101 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 102 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 103 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 104 | if (isPretty && isRepoOut) { |
| 105 | std::cerr << "ERROR: '--pretty' is incompatible with '--repo-output'" << std::endl; |
| 106 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 107 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 109 | security::v2::KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 110 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 111 | security::v2::Certificate certificate; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 112 | try { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 113 | if (isIdentityName) { |
| 114 | certificate = keyChain.getPib() |
| 115 | .getIdentity(name) |
| 116 | .getDefaultKey() |
| 117 | .getDefaultCertificate(); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 118 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 119 | else if (isKeyName) { |
| 120 | certificate = keyChain.getPib() |
| 121 | .getIdentity(security::v2::extractIdentityFromKeyName(name)) |
| 122 | .getKey(name) |
| 123 | .getDefaultCertificate(); |
| 124 | } |
| 125 | else if (isFileName) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 126 | certificate = loadCertificate(name); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 127 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 128 | else { |
| 129 | certificate = keyChain.getPib() |
| 130 | .getIdentity(security::v2::extractIdentityFromCertName(name)) |
| 131 | .getKey(security::v2::extractKeyNameFromCertName(name)) |
| 132 | .getCertificate(name); |
| 133 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 134 | } |
| 135 | catch (const CannotLoadCertificate&) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 136 | std::cerr << "ERROR: Cannot load the certificate from `" << name << "`" << std::endl; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 137 | return 1; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 138 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 140 | if (isPretty) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 141 | std::cout << certificate << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 142 | return 0; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 143 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 144 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 145 | if (isRepoOut) { |
| 146 | boost::asio::ip::tcp::iostream requestStream; |
| 147 | #if BOOST_VERSION >= 106700 |
| 148 | requestStream.expires_after(std::chrono::seconds(10)); |
| 149 | #else |
| 150 | requestStream.expires_from_now(boost::posix_time::seconds(10)); |
| 151 | #endif // BOOST_VERSION >= 106700 |
| 152 | requestStream.connect(repoHost, repoPort); |
| 153 | if (!requestStream) { |
| 154 | std::cerr << "ERROR: Failed to connect to repo instance" << std::endl; |
| 155 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 156 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 157 | requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()), |
| 158 | certificate.wireEncode().size()); |
| 159 | return 0; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 160 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 161 | |
| 162 | io::save(certificate, std::cout); |
| 163 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 167 | } // namespace ndnsec |
| 168 | } // namespace ndn |