blob: 83c34a38ecf7299cf50715b240610d5ff65d2ba3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
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 Afanasyev82c359c2017-01-04 14:48:07 -0800127 shared_ptr<security::v1::IdentityCertificate> certificate;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800129 security::v1::KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800130
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700131 if (isIdentityName || isKeyName || isCertName) {
132 if (isIdentityName) {
133 Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
134 certificate = keyChain.getCertificate(certName);
135 }
136 else if (isKeyName) {
137 Name certName = keyChain.getDefaultCertificateNameForKey(name);
138 certificate = keyChain.getCertificate(certName);
139 }
140 else
141 certificate = keyChain.getCertificate(name);
Yingdi Yub61f5402014-02-26 17:46:11 -0800142
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800143 if (certificate == nullptr) {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700144 std::cerr << "No certificate found!" << std::endl;
145 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800146 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700147 }
148 else {
149 certificate = getIdentityCertificate(name);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800150 if (certificate == nullptr) {
151 std::cerr << "No certificate read!" << std::endl;
152 return 1;
153 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700154 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800155
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700156 if (isPretty) {
157 std::cout << *certificate << std::endl;
158 }
159 else {
160 if (isStdOut) {
161 io::save(*certificate, std::cout);
162 return 0;
Yingdi Yub61f5402014-02-26 17:46:11 -0800163 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700164 if (isRepoOut) {
165 using namespace boost::asio::ip;
166 tcp::iostream request_stream;
167 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
168 request_stream.connect(repoHost, repoPort);
169 if (!request_stream) {
170 std::cerr << "fail to open the stream!" << std::endl;
171 return 1;
172 }
173 request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()),
174 certificate->wireEncode().size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800175
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700176 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800177 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700178 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800179 return 0;
180}
181
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800182} // namespace ndnsec
183} // namespace ndn