blob: 10d0fe53abb65b28771a9ac3229b750ce18bc86f [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * BSD license, See the LICENSE file for more information
5 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
8#ifndef NDNSEC_CERT_DUMP_HPP
9#define NDNSEC_CERT_DUMP_HPP
10
11#include "ndnsec-util.hpp"
12
13int
Yingdi Yub61f5402014-02-26 17:46:11 -080014ndnsec_cert_dump(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080015{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
19 std::string name;
20 bool isKeyName = false;
21 bool isIdentityName = false;
22 bool isCertName = true;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070023 // bool isFileName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024 bool isPretty = false;
25 bool isStdOut = true;
26 bool isRepoOut = false;
27 std::string repoHost = "127.0.0.1";
28 std::string repoPort = "7376";
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070029 // bool isDnsOut = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030
Yingdi Yub61f5402014-02-26 17:46:11 -080031 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");
32 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 ("help,h", "produce help message")
34 ("pretty,p", "optional, if specified, display certificate in human readable format")
35 ("identity,i", "optional, if specified, name is identity name (e.g. /ndn/edu/ucla/alice), otherwise certificate name")
36 ("key,k", "optional, if specified, name is key name (e.g. /ndn/edu/ucla/alice/KSK-123456789), otherwise certificate name")
37 ("file,f", "optional, if specified, name is file name, - for stdin")
38 ("repo-output,r", "optional, if specified, certificate is dumped (published) to repo")
39 ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), "optional, the repo host if repo-output is specified")
40 ("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 -070041 // ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("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")
43 ;
44
45 po::positional_options_description p;
46 p.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047
Yingdi Yub61f5402014-02-26 17:46:11 -080048 po::variables_map vm;
49 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 {
Yingdi Yub61f5402014-02-26 17:46:11 -080051 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
52 vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e)
56 {
57 std::cerr << "ERROR: " << e.what() << std::endl;
58 std::cerr << description << std::endl;
59 return 1;
60 }
61
62 if (vm.count("help") != 0)
63 {
64 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 return 0;
66 }
67
Yingdi Yub61f5402014-02-26 17:46:11 -080068 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 {
70 std::cerr << "identity_name must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080071 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 return 1;
73 }
Yingdi Yub61f5402014-02-26 17:46:11 -080074
75 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 {
77 isCertName = false;
78 isKeyName = true;
79 }
Yingdi Yub61f5402014-02-26 17:46:11 -080080 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 {
82 isCertName = false;
83 isIdentityName = true;
84 }
Yingdi Yub61f5402014-02-26 17:46:11 -080085 else if (vm.count("file") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 {
87 isCertName = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070088 // isFileName = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080089 }
90
91 if (vm.count("pretty") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092 isPretty = true;
93
Yingdi Yub61f5402014-02-26 17:46:11 -080094 if (vm.count("repo-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080095 {
96 isRepoOut = true;
97 isStdOut = false;
98 }
Yingdi Yub61f5402014-02-26 17:46:11 -080099 else if (vm.count("dns-output") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800100 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700101 // isDnsOut = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102 isStdOut = false;
103 std::cerr << "Error: DNS output is not supported yet!" << std::endl;
104 return 1;
105 }
106
107 if (isPretty && !isStdOut)
108 {
109 std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl;
110 return 1;
111 }
112
113 shared_ptr<IdentityCertificate> certificate;
114
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800116
Yingdi Yub61f5402014-02-26 17:46:11 -0800117 if (isIdentityName || isKeyName || isCertName)
118 {
119 if (isIdentityName)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800120 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800121 Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
122 certificate = keyChain.getCertificate(certName);
123 }
124 else if (isKeyName)
125 {
126 Name certName = keyChain.getDefaultCertificateNameForKey(name);
127 certificate = keyChain.getCertificate(certName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128 }
129 else
Yingdi Yub61f5402014-02-26 17:46:11 -0800130 certificate = keyChain.getCertificate(name);
131
132 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800133 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800134 std::cerr << "No certificate found!" << std::endl;
135 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800136 }
137 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800138 else
139 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800140 certificate = getIdentityCertificate(name);
141 if (!static_cast<bool>(certificate))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800142 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800143 std::cerr << "No certificate read!" << std::endl;
144 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800145 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800146 }
147
148 if (isPretty)
149 {
150 std::cout << *certificate << std::endl;
151 }
152 else
153 {
154 if (isStdOut)
155 {
156 io::save(*certificate, std::cout);
157 return 0;
158 }
159 if (isRepoOut)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800160 {
161 using namespace boost::asio::ip;
162 tcp::iostream request_stream;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800163 request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
Yingdi Yub61f5402014-02-26 17:46:11 -0800164 request_stream.connect(repoHost, repoPort);
165 if (!request_stream)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800166 {
167 std::cerr << "fail to open the stream!" << std::endl;
168 return 1;
169 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800170 request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()),
171 certificate->wireEncode().size());
172
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800173 return 0;
174 }
175 }
176 return 0;
177}
178
179#endif //NDNSEC_CERT_DUMP_HPP