blob: e0b99b49b6695e4cd6707f6e77baf9d76db54cf4 [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 Pesavento5afbb0b2018-01-01 17:24:18 -050025#include <boost/date_time/posix_time/posix_time_duration.hpp>
26
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080027namespace ndn {
28namespace ndnsec {
29
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030int
Yingdi Yub61f5402014-02-26 17:46:11 -080031ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 namespace po = boost::program_options;
34
35 std::string name;
36 bool isKeyName = false;
37 bool isIdentityName = false;
38 bool isCertName = true;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070039 // bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 bool isPretty = false;
41 bool isStdOut = true;
42 bool isRepoOut = false;
Yingdi Yuba8604d2014-10-13 19:03:12 -070043 std::string repoHost;
44 std::string repoPort;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070045 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046
Yingdi Yuba8604d2014-10-13 19:03:12 -070047 po::options_description description("General Usage\n"
48 " ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] "
49 "[-P repo-port] ] [-i|k|f] name\n"
50 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080051 description.add_options()
Yingdi Yuba8604d2014-10-13 19:03:12 -070052 ("help,h", "produce help message")
53 ("pretty,p", "display certificate in human readable format")
54 ("identity,i", "treat the name parameter as identity name (e.g., /ndn/edu/ucla/alice")
55 ("key,k", "treat the name parameter as key name "
56 "(e.g., /ndn/edu/ucla/alice/ksk-123456789)")
57 ("file,f", "treat the name parameter as file name with base64 encoded certificate, "
58 "- for stdin")
59 ("repo-output,r", "publish the certificate to the repo-ng")
60 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
61 "the repo host if repo-output is specified")
62 ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
63 "the repo port if repo-output is specified")
64 // ("dns-output,d", "published the certificate to NDNS")
65 ("name,n", po::value<std::string>(&name),
66 "unless overridden with --identity or --key parameter, the certificate name, "
67 "for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890"
68 "/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 ;
70
71 po::positional_options_description p;
72 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073
Yingdi Yub61f5402014-02-26 17:46:11 -080074 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070075 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080076 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070077 po::notify(vm);
78 }
79 catch (const std::exception& e) {
80 std::cerr << "ERROR: " << e.what() << std::endl;
81 std::cerr << description << std::endl;
82 return 1;
83 }
Yingdi Yub61f5402014-02-26 17:46:11 -080084
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070085 if (vm.count("help") != 0) {
86 std::cerr << description << std::endl;
87 return 0;
88 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080089
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070090 if (vm.count("name") == 0) {
91 std::cerr << "identity_name must be specified" << std::endl;
92 std::cerr << description << std::endl;
93 return 1;
94 }
Yingdi Yub61f5402014-02-26 17:46:11 -080095
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070096 if (vm.count("key") != 0) {
97 isCertName = false;
98 isKeyName = true;
99 }
100 else if (vm.count("identity") != 0) {
101 isCertName = false;
102 isIdentityName = true;
103 }
104 else if (vm.count("file") != 0) {
105 isCertName = false;
106 // isFileName = true;
107 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800108
109 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800110 isPretty = true;
111
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700112 if (vm.count("repo-output") != 0) {
113 isRepoOut = true;
114 isStdOut = false;
115 }
116 else if (vm.count("dns-output") != 0) {
117 // isDnsOut = true;
118 isStdOut = false;
119 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
120 return 1;
121 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800122
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700123 if (isPretty && !isStdOut) {
124 std::cerr << "Error: pretty option can only be specified when other "
125 << "output option is specified" << std::endl;
126 return 1;
127 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800129 security::v2::Certificate certificate;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800130
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800131 security::v2::KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800132
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800133 try {
134 if (isIdentityName || isKeyName || isCertName) {
135 if (isIdentityName) {
136 certificate = keyChain.getPib()
137 .getIdentity(name)
138 .getDefaultKey()
139 .getDefaultCertificate();
140 }
141 else if (isKeyName) {
142 certificate = keyChain.getPib()
143 .getIdentity(security::v2::extractIdentityFromKeyName(name))
144 .getKey(name)
145 .getDefaultCertificate();
146 }
147 else {
148 certificate = keyChain.getPib()
149 .getIdentity(security::v2::extractIdentityFromCertName(name))
150 .getKey(security::v2::extractKeyNameFromCertName(name))
Junxiao Shi478206e2017-10-15 00:22:35 +0000151 .getCertificate(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800152 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700153 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800154 else {
155 certificate = loadCertificate(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800156 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700157 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800158 catch (const security::Pib::Error& e) {
159 std::cerr << "ERROR: " << e.what() << std::endl;
160 return 1;
161 }
162 catch (const CannotLoadCertificate&) {
163 std::cerr << "Cannot load certificate from `" << name << "`" << std::endl;
164 return 1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700165 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800166
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700167 if (isPretty) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800168 std::cout << certificate << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700169 }
170 else {
171 if (isStdOut) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800172 io::save(certificate, std::cout);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700173 return 0;
Yingdi Yub61f5402014-02-26 17:46:11 -0800174 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700175 if (isRepoOut) {
176 using namespace boost::asio::ip;
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500177 tcp::iostream requestStream;
178 requestStream.expires_from_now(boost::posix_time::seconds(3));
179 requestStream.connect(repoHost, repoPort);
180 if (!requestStream) {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700181 std::cerr << "fail to open the stream!" << std::endl;
182 return 1;
183 }
Davide Pesavento5afbb0b2018-01-01 17:24:18 -0500184 requestStream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()),
185 certificate.wireEncode().size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800186
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700187 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800188 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700189 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800190 return 0;
191}
192
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800193} // namespace ndnsec
194} // namespace ndn