blob: 39d5baf7ecb5a03d77aa6f597395ad6704a087d7 [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 Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 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 Pesavento6d433932018-04-17 18:35:00 -040025#if BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050026#include <boost/date_time/posix_time/posix_time_duration.hpp>
Davide Pesavento6d433932018-04-17 18:35:00 -040027#endif // BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050028
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029namespace ndn {
30namespace ndnsec {
31
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032int
Yingdi Yub61f5402014-02-26 17:46:11 -080033ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 namespace po = boost::program_options;
36
37 std::string name;
38 bool isKeyName = false;
39 bool isIdentityName = false;
40 bool isCertName = true;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070041 // bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 bool isPretty = false;
43 bool isStdOut = true;
44 bool isRepoOut = false;
Yingdi Yuba8604d2014-10-13 19:03:12 -070045 std::string repoHost;
46 std::string repoPort;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070047 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048
Yingdi Yuba8604d2014-10-13 19:03:12 -070049 po::options_description description("General Usage\n"
50 " ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] "
51 "[-P repo-port] ] [-i|k|f] name\n"
52 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080053 description.add_options()
Yingdi Yuba8604d2014-10-13 19:03:12 -070054 ("help,h", "produce help message")
55 ("pretty,p", "display certificate in human readable format")
56 ("identity,i", "treat the name parameter as identity name (e.g., /ndn/edu/ucla/alice")
57 ("key,k", "treat the name parameter as key name "
58 "(e.g., /ndn/edu/ucla/alice/ksk-123456789)")
59 ("file,f", "treat the name parameter as file name with base64 encoded certificate, "
60 "- for stdin")
61 ("repo-output,r", "publish the certificate to the repo-ng")
62 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
63 "the repo host if repo-output is specified")
64 ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
65 "the repo port if repo-output is specified")
66 // ("dns-output,d", "published the certificate to NDNS")
67 ("name,n", po::value<std::string>(&name),
68 "unless overridden with --identity or --key parameter, the certificate name, "
69 "for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890"
70 "/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 ;
72
73 po::positional_options_description p;
74 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075
Yingdi Yub61f5402014-02-26 17:46:11 -080076 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070077 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070079 po::notify(vm);
80 }
81 catch (const std::exception& e) {
82 std::cerr << "ERROR: " << e.what() << std::endl;
83 std::cerr << description << std::endl;
84 return 1;
85 }
Yingdi Yub61f5402014-02-26 17:46:11 -080086
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070087 if (vm.count("help") != 0) {
88 std::cerr << description << std::endl;
89 return 0;
90 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070092 if (vm.count("name") == 0) {
93 std::cerr << "identity_name must be specified" << std::endl;
94 std::cerr << description << std::endl;
95 return 1;
96 }
Yingdi Yub61f5402014-02-26 17:46:11 -080097
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070098 if (vm.count("key") != 0) {
99 isCertName = false;
100 isKeyName = true;
101 }
102 else if (vm.count("identity") != 0) {
103 isCertName = false;
104 isIdentityName = true;
105 }
106 else if (vm.count("file") != 0) {
107 isCertName = false;
108 // isFileName = true;
109 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800110
111 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112 isPretty = true;
113
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700114 if (vm.count("repo-output") != 0) {
115 isRepoOut = true;
116 isStdOut = false;
117 }
118 else if (vm.count("dns-output") != 0) {
119 // isDnsOut = true;
120 isStdOut = false;
121 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
122 return 1;
123 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800124
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700125 if (isPretty && !isStdOut) {
126 std::cerr << "Error: pretty option can only be specified when other "
127 << "output option is specified" << std::endl;
128 return 1;
129 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800130
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800131 security::v2::Certificate certificate;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800132
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800133 security::v2::KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800134
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800135 try {
136 if (isIdentityName || isKeyName || isCertName) {
137 if (isIdentityName) {
138 certificate = keyChain.getPib()
139 .getIdentity(name)
140 .getDefaultKey()
141 .getDefaultCertificate();
142 }
143 else if (isKeyName) {
144 certificate = keyChain.getPib()
145 .getIdentity(security::v2::extractIdentityFromKeyName(name))
146 .getKey(name)
147 .getDefaultCertificate();
148 }
149 else {
150 certificate = keyChain.getPib()
151 .getIdentity(security::v2::extractIdentityFromCertName(name))
152 .getKey(security::v2::extractKeyNameFromCertName(name))
Junxiao Shi478206e2017-10-15 00:22:35 +0000153 .getCertificate(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800154 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700155 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800156 else {
157 certificate = loadCertificate(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800158 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700159 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800160 catch (const security::Pib::Error& e) {
161 std::cerr << "ERROR: " << e.what() << std::endl;
162 return 1;
163 }
164 catch (const CannotLoadCertificate&) {
165 std::cerr << "Cannot load certificate from `" << name << "`" << std::endl;
166 return 1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700167 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800168
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700169 if (isPretty) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800170 std::cout << certificate << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700171 }
172 else {
173 if (isStdOut) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800174 io::save(certificate, std::cout);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700175 return 0;
Yingdi Yub61f5402014-02-26 17:46:11 -0800176 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700177 if (isRepoOut) {
Davide Pesavento6d433932018-04-17 18:35:00 -0400178 boost::asio::ip::tcp::iostream requestStream;
179#if BOOST_VERSION >= 106700
180 requestStream.expires_after(std::chrono::seconds(3));
181#else
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500182 requestStream.expires_from_now(boost::posix_time::seconds(3));
Davide Pesavento6d433932018-04-17 18:35:00 -0400183#endif // BOOST_VERSION >= 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500184 requestStream.connect(repoHost, repoPort);
185 if (!requestStream) {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700186 std::cerr << "fail to open the stream!" << std::endl;
187 return 1;
188 }
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500189 requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()),
190 certificate.wireEncode().size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800191
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700192 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800193 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700194 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800195 return 0;
196}
197
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800198} // namespace ndnsec
199} // namespace ndn