blob: ca730cbaba93b73c1f1f8c563c3f732ff95288f6 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_CERT_DUMP_HPP
16#define NDNSEC_CERT_DUMP_HPP
17
18#include "ndnsec-util.hpp"
19
20int
Yingdi Yub61f5402014-02-26 17:46:11 -080021ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
26 std::string name;
27 bool isKeyName = false;
28 bool isIdentityName = false;
29 bool isCertName = true;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070030 // bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 bool isPretty = false;
32 bool isStdOut = true;
33 bool isRepoOut = false;
34 std::string repoHost = "127.0.0.1";
35 std::string repoPort = "7376";
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070036 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037
Yingdi Yub61f5402014-02-26 17:46:11 -080038 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");
39 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 ("help,h", "produce help message")
41 ("pretty,p", "optional, if specified, display certificate in human readable format")
42 ("identity,i", "optional, if specified, name is identity name (e.g. /ndn/edu/ucla/alice), otherwise certificate name")
43 ("key,k", "optional, if specified, name is key name (e.g. /ndn/edu/ucla/alice/KSK-123456789), otherwise certificate name")
44 ("file,f", "optional, if specified, name is file name, - for stdin")
45 ("repo-output,r", "optional, if specified, certificate is dumped (published) to repo")
46 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), "optional, the repo host if repo-output is specified")
47 ("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 -070048 // ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 ("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")
50 ;
51
52 po::positional_options_description p;
53 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054
Yingdi Yub61f5402014-02-26 17:46:11 -080055 po::variables_map vm;
56 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 {
Yingdi Yub61f5402014-02-26 17:46:11 -080058 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
59 vm);
60 po::notify(vm);
61 }
62 catch (const std::exception& e)
63 {
64 std::cerr << "ERROR: " << e.what() << std::endl;
65 std::cerr << description << std::endl;
66 return 1;
67 }
68
69 if (vm.count("help") != 0)
70 {
71 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 return 0;
73 }
74
Yingdi Yub61f5402014-02-26 17:46:11 -080075 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 {
77 std::cerr << "identity_name must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080078 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079 return 1;
80 }
Yingdi Yub61f5402014-02-26 17:46:11 -080081
82 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083 {
84 isCertName = false;
85 isKeyName = true;
86 }
Yingdi Yub61f5402014-02-26 17:46:11 -080087 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080088 {
89 isCertName = false;
90 isIdentityName = true;
91 }
Yingdi Yub61f5402014-02-26 17:46:11 -080092 else if (vm.count("file") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093 {
94 isCertName = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070095 // isFileName = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080096 }
97
98 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080099 isPretty = true;
100
Yingdi Yub61f5402014-02-26 17:46:11 -0800101 if (vm.count("repo-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102 {
103 isRepoOut = true;
104 isStdOut = false;
105 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800106 else if (vm.count("dns-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800107 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700108 // isDnsOut = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800109 isStdOut = false;
110 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
111 return 1;
112 }
113
114 if (isPretty && !isStdOut)
115 {
116 std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl;
117 return 1;
118 }
119
120 shared_ptr<IdentityCertificate> certificate;
121
Yingdi Yub61f5402014-02-26 17:46:11 -0800122 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800123
Yingdi Yub61f5402014-02-26 17:46:11 -0800124 if (isIdentityName || isKeyName || isCertName)
125 {
126 if (isIdentityName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800127 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800128 Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
129 certificate = keyChain.getCertificate(certName);
130 }
131 else if (isKeyName)
132 {
133 Name certName = keyChain.getDefaultCertificateNameForKey(name);
134 certificate = keyChain.getCertificate(certName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800135 }
136 else
Yingdi Yub61f5402014-02-26 17:46:11 -0800137 certificate = keyChain.getCertificate(name);
138
139 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800140 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800141 std::cerr << "No certificate found!" << std::endl;
142 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800143 }
144 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800145 else
146 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800147 certificate = getIdentityCertificate(name);
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 read!" << std::endl;
151 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800152 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800153 }
154
155 if (isPretty)
156 {
157 std::cout << *certificate << std::endl;
158 }
159 else
160 {
161 if (isStdOut)
162 {
163 io::save(*certificate, std::cout);
164 return 0;
165 }
166 if (isRepoOut)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800167 {
168 using namespace boost::asio::ip;
169 tcp::iostream request_stream;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800170 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
Yingdi Yub61f5402014-02-26 17:46:11 -0800171 request_stream.connect(repoHost, repoPort);
172 if (!request_stream)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800173 {
174 std::cerr << "fail to open the stream!" << std::endl;
175 return 1;
176 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800177 request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()),
178 certificate->wireEncode().size());
179
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800180 return 0;
181 }
182 }
183 return 0;
184}
185
186#endif //NDNSEC_CERT_DUMP_HPP