blob: d553822e0f68a63c0130c87038408793ad06f058 [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/*
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -07003 * Copyright (c) 2013-2020 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 Pesavento25d4f1c2020-04-29 23:31:04 -040025#include "ndn-cxx/util/io.hpp"
26
Davide Pesaventob310efb2019-04-11 22:10:24 -040027#include <boost/asio/ip/tcp.hpp>
Davide Pesavento6d433932018-04-17 18:35:00 -040028#if BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050029#include <boost/date_time/posix_time/posix_time_duration.hpp>
Davide Pesavento6d433932018-04-17 18:35:00 -040030#endif // BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050031
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080032namespace ndn {
33namespace ndnsec {
34
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035int
Yingdi Yub61f5402014-02-26 17:46:11 -080036ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 namespace po = boost::program_options;
39
40 std::string name;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 bool isIdentityName = false;
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 bool isKeyName = false;
43 bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 bool isPretty = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 bool isRepoOut = false;
Yingdi Yuba8604d2014-10-13 19:03:12 -070046 std::string repoHost;
47 std::string repoPort;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048
Davide Pesaventob310efb2019-04-11 22:10:24 -040049 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 Yub61f5402014-02-26 17:46:11 -080053 description.add_options()
Davide Pesaventob310efb2019-04-11 22:10:24 -040054 ("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 Zhang4c68b6e2020-04-20 16:13:21 -070067 "publish the certificate into an NDN repo instance")
Davide Pesaventob310efb2019-04-11 22:10:24 -040068 ("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 Yu8d7468f2014-02-21 14:49:45 -080072 ;
73
74 po::positional_options_description p;
75 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076
Yingdi Yub61f5402014-02-26 17:46:11 -080077 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070078 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080079 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070080 po::notify(vm);
81 }
82 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 std::cerr << "ERROR: " << e.what() << "\n\n"
84 << description << std::endl;
85 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070086 }
Yingdi Yub61f5402014-02-26 17:46:11 -080087
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 if (vm.count("help") > 0) {
89 std::cout << description << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070090 return 0;
91 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070093 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 std::cerr << "ERROR: you must specify a name" << std::endl;
95 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070096 }
Yingdi Yub61f5402014-02-26 17:46:11 -080097
Junxiao Shibc2e78e2020-05-20 15:01:08 -060098 int nIsNameOptions = isIdentityName + isKeyName + isFileName;
99 if (nIsNameOptions > 1) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400100 std::cerr << "ERROR: at most one of '--identity', '--key', "
101 "or '--file' may be specified" << std::endl;
102 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700103 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800104
Davide Pesaventob310efb2019-04-11 22:10:24 -0400105 if (isPretty && isRepoOut) {
106 std::cerr << "ERROR: '--pretty' is incompatible with '--repo-output'" << std::endl;
107 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700108 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800109
Davide Pesaventob310efb2019-04-11 22:10:24 -0400110 security::v2::Certificate certificate;
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600111 if (isFileName) {
112 try {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800113 certificate = loadCertificate(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800114 }
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600115 catch (const CannotLoadCertificate&) {
116 std::cerr << "ERROR: Cannot load the certificate from `" << name << "`" << std::endl;
117 return 1;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400118 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800119 }
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600120 else {
121 security::v2::KeyChain keyChain;
122 certificate = getCertificateFromPib(keyChain.getPib(), name,
123 isIdentityName, isKeyName, nIsNameOptions == 0);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700124 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800125
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700126 if (isPretty) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800127 std::cout << certificate << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400128 return 0;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700129 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800130
Davide Pesaventob310efb2019-04-11 22:10:24 -0400131 if (isRepoOut) {
132 boost::asio::ip::tcp::iostream requestStream;
133#if BOOST_VERSION >= 106700
134 requestStream.expires_after(std::chrono::seconds(10));
135#else
136 requestStream.expires_from_now(boost::posix_time::seconds(10));
137#endif // BOOST_VERSION >= 106700
138 requestStream.connect(repoHost, repoPort);
139 if (!requestStream) {
140 std::cerr << "ERROR: Failed to connect to repo instance" << std::endl;
141 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800142 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400143 requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()),
144 certificate.wireEncode().size());
145 return 0;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700146 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400147
148 io::save(certificate, std::cout);
149
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800150 return 0;
151}
152
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800153} // namespace ndnsec
154} // namespace ndn