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