blob: 95b13205f570e8e94793e2e655e9bcc8bd0a0787 [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;
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;
75 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 {
Yingdi Yub61f5402014-02-26 17:46:11 -080077 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
78 vm);
79 po::notify(vm);
80 }
81 catch (const std::exception& e)
82 {
83 std::cerr << "ERROR: " << e.what() << std::endl;
84 std::cerr << description << std::endl;
85 return 1;
86 }
87
88 if (vm.count("help") != 0)
89 {
90 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091 return 0;
92 }
93
Yingdi Yub61f5402014-02-26 17:46:11 -080094 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080095 {
96 std::cerr << "identity_name must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080097 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 return 1;
99 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800100
101 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102 {
103 isCertName = false;
104 isKeyName = true;
105 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800106 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800107 {
108 isCertName = false;
109 isIdentityName = true;
110 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800111 else if (vm.count("file") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112 {
113 isCertName = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700114 // isFileName = true;
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 }
116
117 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800118 isPretty = true;
119
Yingdi Yub61f5402014-02-26 17:46:11 -0800120 if (vm.count("repo-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800121 {
122 isRepoOut = true;
123 isStdOut = false;
124 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800125 else if (vm.count("dns-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800126 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700127 // isDnsOut = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128 isStdOut = false;
129 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
130 return 1;
131 }
132
133 if (isPretty && !isStdOut)
134 {
Yingdi Yuba8604d2014-10-13 19:03:12 -0700135 std::cerr << "Error: pretty option can only be specified when other "
136 << "output option is specified" << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800137 return 1;
138 }
139
140 shared_ptr<IdentityCertificate> certificate;
141
Yingdi Yub61f5402014-02-26 17:46:11 -0800142 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800143
Yingdi Yub61f5402014-02-26 17:46:11 -0800144 if (isIdentityName || isKeyName || isCertName)
145 {
146 if (isIdentityName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800147 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800148 Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
149 certificate = keyChain.getCertificate(certName);
150 }
151 else if (isKeyName)
152 {
153 Name certName = keyChain.getDefaultCertificateNameForKey(name);
154 certificate = keyChain.getCertificate(certName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800155 }
156 else
Yingdi Yub61f5402014-02-26 17:46:11 -0800157 certificate = keyChain.getCertificate(name);
158
159 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800160 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800161 std::cerr << "No certificate found!" << std::endl;
162 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800163 }
164 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800165 else
166 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800167 certificate = getIdentityCertificate(name);
168 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800169 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800170 std::cerr << "No certificate read!" << std::endl;
171 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800172 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800173 }
174
175 if (isPretty)
176 {
177 std::cout << *certificate << std::endl;
178 }
179 else
180 {
181 if (isStdOut)
182 {
183 io::save(*certificate, std::cout);
184 return 0;
185 }
186 if (isRepoOut)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800187 {
188 using namespace boost::asio::ip;
189 tcp::iostream request_stream;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800190 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
Yingdi Yub61f5402014-02-26 17:46:11 -0800191 request_stream.connect(repoHost, repoPort);
192 if (!request_stream)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800193 {
194 std::cerr << "fail to open the stream!" << std::endl;
195 return 1;
196 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800197 request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()),
198 certificate->wireEncode().size());
199
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800200 return 0;
201 }
202 }
203 return 0;
204}
205
206#endif //NDNSEC_CERT_DUMP_HPP