blob: 0baf8dffadf45bcf2b6738b69e6a4701b7c48d62 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi478206e2017-10-15 00:22:35 +00002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Davide Pesaventob310efb2019-04-11 22:10:24 -040025#include <boost/asio/ip/tcp.hpp>
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050026
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080028
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029int
Yingdi Yub61f5402014-02-26 17:46:11 -080030ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 namespace po = boost::program_options;
33
34 std::string name;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 bool isIdentityName = false;
Davide Pesaventob310efb2019-04-11 22:10:24 -040036 bool isKeyName = false;
37 bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 bool isPretty = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 bool isRepoOut = false;
Yingdi Yuba8604d2014-10-13 19:03:12 -070040 std::string repoHost;
41 std::string repoPort;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 po::options_description description(
44 "Usage: ndnsec cert-dump [-h] [-p] [-r [-H HOST] [-P PORT]] [-i|-k|-f] [-n] NAME\n"
45 "\n"
46 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080047 description.add_options()
Davide Pesaventob310efb2019-04-11 22:10:24 -040048 ("help,h", "produce help message")
49 ("pretty,p", po::bool_switch(&isPretty), "display certificate in human readable format")
50 ("identity,i", po::bool_switch(&isIdentityName),
51 "treat the NAME argument as an identity name (e.g., /ndn/edu/ucla/alice)")
52 ("key,k", po::bool_switch(&isKeyName),
53 "treat the NAME argument as a key name (e.g., /ndn/edu/ucla/alice/ksk-123456789)")
54 ("file,f", po::bool_switch(&isFileName),
55 "treat the NAME argument as the name of a file containing a base64-encoded "
56 "certificate, '-' for stdin")
57 ("name,n", po::value<std::string>(&name),
58 "unless overridden by -i/-k/-f, the name of the certificate to be exported "
59 "(e.g., /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF)")
60 ("repo-output,r", po::bool_switch(&isRepoOut),
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070061 "publish the certificate into an NDN repo instance")
Davide Pesaventob310efb2019-04-11 22:10:24 -040062 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
63 "repo hostname if --repo-output is specified")
64 ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
65 "repo port number if --repo-output is specified")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 ;
67
68 po::positional_options_description p;
69 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070
Yingdi Yub61f5402014-02-26 17:46:11 -080071 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070072 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080073 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070074 po::notify(vm);
75 }
76 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040077 std::cerr << "ERROR: " << e.what() << "\n\n"
78 << description << std::endl;
79 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070080 }
Yingdi Yub61f5402014-02-26 17:46:11 -080081
Davide Pesaventob310efb2019-04-11 22:10:24 -040082 if (vm.count("help") > 0) {
83 std::cout << description << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070084 return 0;
85 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070087 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 std::cerr << "ERROR: you must specify a name" << std::endl;
89 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070090 }
Yingdi Yub61f5402014-02-26 17:46:11 -080091
Junxiao Shibc2e78e2020-05-20 15:01:08 -060092 int nIsNameOptions = isIdentityName + isKeyName + isFileName;
93 if (nIsNameOptions > 1) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 std::cerr << "ERROR: at most one of '--identity', '--key', "
95 "or '--file' may be specified" << std::endl;
96 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070097 }
Yingdi Yub61f5402014-02-26 17:46:11 -080098
Davide Pesaventob310efb2019-04-11 22:10:24 -040099 if (isPretty && isRepoOut) {
100 std::cerr << "ERROR: '--pretty' is incompatible with '--repo-output'" << std::endl;
101 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700102 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103
Davide Pesaventof2cae612021-03-24 18:47:05 -0400104 security::Certificate certificate;
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600105 if (isFileName) {
Davide Pesavento949075a2021-10-17 22:07:07 -0400106 certificate = loadFromFile<security::Certificate>(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800107 }
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600108 else {
Davide Pesaventof2cae612021-03-24 18:47:05 -0400109 KeyChain keyChain;
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600110 certificate = getCertificateFromPib(keyChain.getPib(), name,
111 isIdentityName, isKeyName, nIsNameOptions == 0);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700112 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800113
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700114 if (isPretty) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800115 std::cout << certificate << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400116 return 0;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700117 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800118
Davide Pesaventob310efb2019-04-11 22:10:24 -0400119 if (isRepoOut) {
120 boost::asio::ip::tcp::iostream requestStream;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400121 requestStream.expires_after(std::chrono::seconds(10));
Davide Pesaventob310efb2019-04-11 22:10:24 -0400122 requestStream.connect(repoHost, repoPort);
123 if (!requestStream) {
124 std::cerr << "ERROR: Failed to connect to repo instance" << std::endl;
125 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800126 }
Davide Pesavento258d51a2022-02-27 21:26:28 -0500127 requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().data()),
Davide Pesaventob310efb2019-04-11 22:10:24 -0400128 certificate.wireEncode().size());
129 return 0;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700130 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400131
132 io::save(certificate, std::cout);
133
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800134 return 0;
135}
136
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400137} // namespace ndn::ndnsec