Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 13 | int |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 14 | ndnsec_cert_dump(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 15 | { |
| 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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 23 | // bool isFileName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | 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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 29 | // bool isDnsOut = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 31 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | ("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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 41 | // ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | ("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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 48 | po::variables_map vm; |
| 49 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 69 | { |
| 70 | std::cerr << "identity_name must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | return 1; |
| 73 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 74 | |
| 75 | if (vm.count("key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | { |
| 77 | isCertName = false; |
| 78 | isKeyName = true; |
| 79 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | else if (vm.count("identity") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 81 | { |
| 82 | isCertName = false; |
| 83 | isIdentityName = true; |
| 84 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 85 | else if (vm.count("file") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 86 | { |
| 87 | isCertName = false; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 88 | // isFileName = true; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (vm.count("pretty") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 92 | isPretty = true; |
| 93 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 94 | if (vm.count("repo-output") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 95 | { |
| 96 | isRepoOut = true; |
| 97 | isStdOut = false; |
| 98 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 99 | else if (vm.count("dns-output") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 101 | // isDnsOut = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 102 | 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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 115 | KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 116 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 117 | if (isIdentityName || isKeyName || isCertName) |
| 118 | { |
| 119 | if (isIdentityName) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 120 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 121 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 128 | } |
| 129 | else |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 130 | certificate = keyChain.getCertificate(name); |
| 131 | |
| 132 | if (!static_cast<bool>(certificate)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 133 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 134 | std::cerr << "No certificate found!" << std::endl; |
| 135 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 138 | else |
| 139 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 140 | certificate = getIdentityCertificate(name); |
| 141 | if (!static_cast<bool>(certificate)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 142 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 143 | std::cerr << "No certificate read!" << std::endl; |
| 144 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 145 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 146 | } |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 160 | { |
| 161 | using namespace boost::asio::ip; |
| 162 | tcp::iostream request_stream; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 163 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 164 | request_stream.connect(repoHost, repoPort); |
| 165 | if (!request_stream) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 166 | { |
| 167 | std::cerr << "fail to open the stream!" << std::endl; |
| 168 | return 1; |
| 169 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 170 | request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()), |
| 171 | certificate->wireEncode().size()); |
| 172 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | #endif //NDNSEC_CERT_DUMP_HPP |