blob: a8ade74bf98fee68393ca4f319ec668d2884b0b2 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
24#ifndef NDNSEC_CERT_DUMP_HPP
25#define NDNSEC_CERT_DUMP_HPP
26
27#include "ndnsec-util.hpp"
28
29int
Yingdi Yub61f5402014-02-26 17:46:11 -080030ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
32 using namespace ndn;
33 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;
43 std::string repoHost = "127.0.0.1";
44 std::string repoPort = "7376";
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070045 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046
Yingdi Yub61f5402014-02-26 17:46:11 -080047 po::options_description description("General Usage\n ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] [-P repor-port] ] [-i|k|f] name\nGeneral options");
48 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 ("help,h", "produce help message")
50 ("pretty,p", "optional, if specified, display certificate in human readable format")
51 ("identity,i", "optional, if specified, name is identity name (e.g. /ndn/edu/ucla/alice), otherwise certificate name")
52 ("key,k", "optional, if specified, name is key name (e.g. /ndn/edu/ucla/alice/KSK-123456789), otherwise certificate name")
53 ("file,f", "optional, if specified, name is file name, - for stdin")
54 ("repo-output,r", "optional, if specified, certificate is dumped (published) to repo")
55 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), "optional, the repo host if repo-output is specified")
56 ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), "optional, the repo port if repo-output is specified")
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070057 // ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080058 ("name,n", po::value<std::string>(&name), "certificate name, for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF")
59 ;
60
61 po::positional_options_description p;
62 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063
Yingdi Yub61f5402014-02-26 17:46:11 -080064 po::variables_map vm;
65 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 {
Yingdi Yub61f5402014-02-26 17:46:11 -080067 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
68 vm);
69 po::notify(vm);
70 }
71 catch (const std::exception& e)
72 {
73 std::cerr << "ERROR: " << e.what() << std::endl;
74 std::cerr << description << std::endl;
75 return 1;
76 }
77
78 if (vm.count("help") != 0)
79 {
80 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 return 0;
82 }
83
Yingdi Yub61f5402014-02-26 17:46:11 -080084 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080085 {
86 std::cerr << "identity_name must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080087 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080088 return 1;
89 }
Yingdi Yub61f5402014-02-26 17:46:11 -080090
91 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092 {
93 isCertName = false;
94 isKeyName = true;
95 }
Yingdi Yub61f5402014-02-26 17:46:11 -080096 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080097 {
98 isCertName = false;
99 isIdentityName = true;
100 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800101 else if (vm.count("file") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102 {
103 isCertName = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700104 // isFileName = true;
Yingdi Yub61f5402014-02-26 17:46:11 -0800105 }
106
107 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800108 isPretty = true;
109
Yingdi Yub61f5402014-02-26 17:46:11 -0800110 if (vm.count("repo-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800111 {
112 isRepoOut = true;
113 isStdOut = false;
114 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 else if (vm.count("dns-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800116 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700117 // isDnsOut = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800118 isStdOut = false;
119 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
120 return 1;
121 }
122
123 if (isPretty && !isStdOut)
124 {
125 std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl;
126 return 1;
127 }
128
129 shared_ptr<IdentityCertificate> certificate;
130
Yingdi Yub61f5402014-02-26 17:46:11 -0800131 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800132
Yingdi Yub61f5402014-02-26 17:46:11 -0800133 if (isIdentityName || isKeyName || isCertName)
134 {
135 if (isIdentityName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800136 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800137 Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
138 certificate = keyChain.getCertificate(certName);
139 }
140 else if (isKeyName)
141 {
142 Name certName = keyChain.getDefaultCertificateNameForKey(name);
143 certificate = keyChain.getCertificate(certName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800144 }
145 else
Yingdi Yub61f5402014-02-26 17:46:11 -0800146 certificate = keyChain.getCertificate(name);
147
148 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800149 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800150 std::cerr << "No certificate found!" << std::endl;
151 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800152 }
153 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800154 else
155 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800156 certificate = getIdentityCertificate(name);
157 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800158 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800159 std::cerr << "No certificate read!" << std::endl;
160 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800161 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800162 }
163
164 if (isPretty)
165 {
166 std::cout << *certificate << std::endl;
167 }
168 else
169 {
170 if (isStdOut)
171 {
172 io::save(*certificate, std::cout);
173 return 0;
174 }
175 if (isRepoOut)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800176 {
177 using namespace boost::asio::ip;
178 tcp::iostream request_stream;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800179 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
Yingdi Yub61f5402014-02-26 17:46:11 -0800180 request_stream.connect(repoHost, repoPort);
181 if (!request_stream)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800182 {
183 std::cerr << "fail to open the stream!" << std::endl;
184 return 1;
185 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800186 request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()),
187 certificate->wireEncode().size());
188
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800189 return 0;
190 }
191 }
192 return 0;
193}
194
195#endif //NDNSEC_CERT_DUMP_HPP