blob: 436b3994c51090900b1ddbd03663e26aafc07da1 [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/*
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028int
Yingdi Yub61f5402014-02-26 17:46:11 -080029ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 namespace po = boost::program_options;
32
33 std::string name;
34 bool isKeyName = false;
35 bool isIdentityName = false;
36 bool isCertName = true;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070037 // bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 bool isPretty = false;
39 bool isStdOut = true;
40 bool isRepoOut = false;
Yingdi Yuba8604d2014-10-13 19:03:12 -070041 std::string repoHost;
42 std::string repoPort;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070043 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044
Yingdi Yuba8604d2014-10-13 19:03:12 -070045 po::options_description description("General Usage\n"
46 " ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] "
47 "[-P repo-port] ] [-i|k|f] name\n"
48 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080049 description.add_options()
Yingdi Yuba8604d2014-10-13 19:03:12 -070050 ("help,h", "produce help message")
51 ("pretty,p", "display certificate in human readable format")
52 ("identity,i", "treat the name parameter as identity name (e.g., /ndn/edu/ucla/alice")
53 ("key,k", "treat the name parameter as key name "
54 "(e.g., /ndn/edu/ucla/alice/ksk-123456789)")
55 ("file,f", "treat the name parameter as file name with base64 encoded certificate, "
56 "- for stdin")
57 ("repo-output,r", "publish the certificate to the repo-ng")
58 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
59 "the repo host if repo-output is specified")
60 ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
61 "the repo port if repo-output is specified")
62 // ("dns-output,d", "published the certificate to NDNS")
63 ("name,n", po::value<std::string>(&name),
64 "unless overridden with --identity or --key parameter, the certificate name, "
65 "for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890"
66 "/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 ;
68
69 po::positional_options_description p;
70 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070073 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070075 po::notify(vm);
76 }
77 catch (const std::exception& e) {
78 std::cerr << "ERROR: " << e.what() << std::endl;
79 std::cerr << description << std::endl;
80 return 1;
81 }
Yingdi Yub61f5402014-02-26 17:46:11 -080082
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070083 if (vm.count("help") != 0) {
84 std::cerr << description << std::endl;
85 return 0;
86 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070088 if (vm.count("name") == 0) {
89 std::cerr << "identity_name must be specified" << std::endl;
90 std::cerr << description << std::endl;
91 return 1;
92 }
Yingdi Yub61f5402014-02-26 17:46:11 -080093
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070094 if (vm.count("key") != 0) {
95 isCertName = false;
96 isKeyName = true;
97 }
98 else if (vm.count("identity") != 0) {
99 isCertName = false;
100 isIdentityName = true;
101 }
102 else if (vm.count("file") != 0) {
103 isCertName = false;
104 // isFileName = true;
105 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800106
107 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800108 isPretty = true;
109
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700110 if (vm.count("repo-output") != 0) {
111 isRepoOut = true;
112 isStdOut = false;
113 }
114 else if (vm.count("dns-output") != 0) {
115 // isDnsOut = true;
116 isStdOut = false;
117 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
118 return 1;
119 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800120
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700121 if (isPretty && !isStdOut) {
122 std::cerr << "Error: pretty option can only be specified when other "
123 << "output option is specified" << std::endl;
124 return 1;
125 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800126
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800127 security::v2::Certificate certificate;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800129 security::v2::KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800130
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800131 try {
132 if (isIdentityName || isKeyName || isCertName) {
133 if (isIdentityName) {
134 certificate = keyChain.getPib()
135 .getIdentity(name)
136 .getDefaultKey()
137 .getDefaultCertificate();
138 }
139 else if (isKeyName) {
140 certificate = keyChain.getPib()
141 .getIdentity(security::v2::extractIdentityFromKeyName(name))
142 .getKey(name)
143 .getDefaultCertificate();
144 }
145 else {
146 certificate = keyChain.getPib()
147 .getIdentity(security::v2::extractIdentityFromCertName(name))
148 .getKey(security::v2::extractKeyNameFromCertName(name))
Junxiao Shi478206e2017-10-15 00:22:35 +0000149 .getCertificate(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800150 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700151 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800152 else {
153 certificate = loadCertificate(name);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800154 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700155 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800156 catch (const security::Pib::Error& e) {
157 std::cerr << "ERROR: " << e.what() << std::endl;
158 return 1;
159 }
160 catch (const CannotLoadCertificate&) {
161 std::cerr << "Cannot load certificate from `" << name << "`" << std::endl;
162 return 1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700163 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800164
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700165 if (isPretty) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800166 std::cout << certificate << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700167 }
168 else {
169 if (isStdOut) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800170 io::save(certificate, std::cout);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700171 return 0;
Yingdi Yub61f5402014-02-26 17:46:11 -0800172 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700173 if (isRepoOut) {
174 using namespace boost::asio::ip;
175 tcp::iostream request_stream;
176 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
177 request_stream.connect(repoHost, repoPort);
178 if (!request_stream) {
179 std::cerr << "fail to open the stream!" << std::endl;
180 return 1;
181 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800182 request_stream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()),
183 certificate.wireEncode().size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800184
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700185 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800186 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700187 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800188 return 0;
189}
190
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800191} // namespace ndnsec
192} // namespace ndn