Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_CERT_DUMP_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_CERT_DUMP_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | |
| 29 | int |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | ndnsec_cert_dump(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | { |
| 32 | using namespace ndn; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 33 | using namespace ndn::security; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | namespace po = boost::program_options; |
| 35 | |
| 36 | std::string name; |
| 37 | bool isKeyName = false; |
| 38 | bool isIdentityName = false; |
| 39 | bool isCertName = true; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 40 | // bool isFileName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | bool isPretty = false; |
| 42 | bool isStdOut = true; |
| 43 | bool isRepoOut = false; |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 44 | std::string repoHost; |
| 45 | std::string repoPort; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 46 | // bool isDnsOut = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 48 | po::options_description description("General Usage\n" |
| 49 | " ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] " |
| 50 | "[-P repo-port] ] [-i|k|f] name\n" |
| 51 | "General options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 52 | description.add_options() |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 53 | ("help,h", "produce help message") |
| 54 | ("pretty,p", "display certificate in human readable format") |
| 55 | ("identity,i", "treat the name parameter as identity name (e.g., /ndn/edu/ucla/alice") |
| 56 | ("key,k", "treat the name parameter as key name " |
| 57 | "(e.g., /ndn/edu/ucla/alice/ksk-123456789)") |
| 58 | ("file,f", "treat the name parameter as file name with base64 encoded certificate, " |
| 59 | "- for stdin") |
| 60 | ("repo-output,r", "publish the certificate to the repo-ng") |
| 61 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), |
| 62 | "the repo host if repo-output is specified") |
| 63 | ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), |
| 64 | "the repo port if repo-output is specified") |
| 65 | // ("dns-output,d", "published the certificate to NDNS") |
| 66 | ("name,n", po::value<std::string>(&name), |
| 67 | "unless overridden with --identity or --key parameter, the certificate name, " |
| 68 | "for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890" |
| 69 | "/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | ; |
| 71 | |
| 72 | po::positional_options_description p; |
| 73 | p.add("name", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 74 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 75 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 76 | try { |
| 77 | 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 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 83 | std::cerr << description << std::endl; |
| 84 | return 1; |
| 85 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 87 | if (vm.count("help") != 0) { |
| 88 | std::cerr << description << std::endl; |
| 89 | return 0; |
| 90 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 92 | if (vm.count("name") == 0) { |
| 93 | std::cerr << "identity_name must be specified" << std::endl; |
| 94 | std::cerr << description << std::endl; |
| 95 | return 1; |
| 96 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 98 | if (vm.count("key") != 0) { |
| 99 | isCertName = false; |
| 100 | isKeyName = true; |
| 101 | } |
| 102 | else if (vm.count("identity") != 0) { |
| 103 | isCertName = false; |
| 104 | isIdentityName = true; |
| 105 | } |
| 106 | else if (vm.count("file") != 0) { |
| 107 | isCertName = false; |
| 108 | // isFileName = true; |
| 109 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 110 | |
| 111 | if (vm.count("pretty") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 112 | isPretty = true; |
| 113 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 114 | if (vm.count("repo-output") != 0) { |
| 115 | isRepoOut = true; |
| 116 | isStdOut = false; |
| 117 | } |
| 118 | else if (vm.count("dns-output") != 0) { |
| 119 | // isDnsOut = true; |
| 120 | isStdOut = false; |
| 121 | std::cerr << "Error: DNS output is not supported yet!" << std::endl; |
| 122 | return 1; |
| 123 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 125 | if (isPretty && !isStdOut) { |
| 126 | std::cerr << "Error: pretty option can only be specified when other " |
| 127 | << "output option is specified" << std::endl; |
| 128 | return 1; |
| 129 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 131 | shared_ptr<v1::IdentityCertificate> certificate; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 133 | ndn::security::v1::KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 135 | if (isIdentityName || isKeyName || isCertName) { |
| 136 | if (isIdentityName) { |
| 137 | Name certName = keyChain.getDefaultCertificateNameForIdentity(name); |
| 138 | certificate = keyChain.getCertificate(certName); |
| 139 | } |
| 140 | else if (isKeyName) { |
| 141 | Name certName = keyChain.getDefaultCertificateNameForKey(name); |
| 142 | certificate = keyChain.getCertificate(certName); |
| 143 | } |
| 144 | else |
| 145 | certificate = keyChain.getCertificate(name); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 147 | if (!static_cast<bool>(certificate)) { |
| 148 | std::cerr << "No certificate found!" << std::endl; |
| 149 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 150 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 151 | } |
| 152 | else { |
| 153 | certificate = getIdentityCertificate(name); |
| 154 | if (!static_cast<bool>(certificate)) |
| 155 | { |
| 156 | std::cerr << "No certificate read!" << std::endl; |
| 157 | return 1; |
| 158 | } |
| 159 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 161 | if (isPretty) { |
| 162 | std::cout << *certificate << std::endl; |
| 163 | } |
| 164 | else { |
| 165 | if (isStdOut) { |
| 166 | io::save(*certificate, std::cout); |
| 167 | return 0; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 168 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 169 | if (isRepoOut) { |
| 170 | using namespace boost::asio::ip; |
| 171 | tcp::iostream request_stream; |
| 172 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 173 | request_stream.connect(repoHost, repoPort); |
| 174 | if (!request_stream) { |
| 175 | std::cerr << "fail to open the stream!" << std::endl; |
| 176 | return 1; |
| 177 | } |
| 178 | request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()), |
| 179 | certificate->wireEncode().size()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 181 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 182 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 183 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 187 | #endif // NDN_TOOLS_NDNSEC_CERT_DUMP_HPP |