Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDNSEC_CERT_DUMP_HPP |
| 16 | #define NDNSEC_CERT_DUMP_HPP |
| 17 | |
| 18 | #include "ndnsec-util.hpp" |
| 19 | |
| 20 | int |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 21 | ndnsec_cert_dump(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | { |
| 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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 30 | // bool isFileName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | 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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 36 | // bool isDnsOut = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 40 | ("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 Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 48 | // ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 49 | ("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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 55 | po::variables_map vm; |
| 56 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 75 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | { |
| 77 | std::cerr << "identity_name must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 79 | return 1; |
| 80 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 81 | |
| 82 | if (vm.count("key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 83 | { |
| 84 | isCertName = false; |
| 85 | isKeyName = true; |
| 86 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | else if (vm.count("identity") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 88 | { |
| 89 | isCertName = false; |
| 90 | isIdentityName = true; |
| 91 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 92 | else if (vm.count("file") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 93 | { |
| 94 | isCertName = false; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 95 | // isFileName = true; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (vm.count("pretty") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 99 | isPretty = true; |
| 100 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 101 | if (vm.count("repo-output") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 102 | { |
| 103 | isRepoOut = true; |
| 104 | isStdOut = false; |
| 105 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 106 | else if (vm.count("dns-output") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 107 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 108 | // isDnsOut = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 109 | 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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 122 | KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 123 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 124 | if (isIdentityName || isKeyName || isCertName) |
| 125 | { |
| 126 | if (isIdentityName) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 127 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 128 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 135 | } |
| 136 | else |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 137 | certificate = keyChain.getCertificate(name); |
| 138 | |
| 139 | if (!static_cast<bool>(certificate)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 140 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 141 | std::cerr << "No certificate found!" << std::endl; |
| 142 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 145 | else |
| 146 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 147 | certificate = getIdentityCertificate(name); |
| 148 | if (!static_cast<bool>(certificate)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 149 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 150 | std::cerr << "No certificate read!" << std::endl; |
| 151 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 152 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 153 | } |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 167 | { |
| 168 | using namespace boost::asio::ip; |
| 169 | tcp::iostream request_stream; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 170 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 171 | request_stream.connect(repoHost, repoPort); |
| 172 | if (!request_stream) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 173 | { |
| 174 | std::cerr << "fail to open the stream!" << std::endl; |
| 175 | return 1; |
| 176 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 177 | request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()), |
| 178 | certificate->wireEncode().size()); |
| 179 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | #endif //NDNSEC_CERT_DUMP_HPP |