Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 478206e | 2017-10-15 00:22:35 +0000 | [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 | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 127 | security::v2::Certificate certificate; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 129 | security::v2::KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 131 | try { |
| 132 | if (isIdentityName || isKeyName || isCertName) { |
| 133 | if (isIdentityName) { |
| 134 | certificate = keyChain.getPib() |
| 135 | .getIdentity(name) |
| 136 | .getDefaultKey() |
| 137 | .getDefaultCertificate(); |
| 138 | } |
| 139 | else if (isKeyName) { |
| 140 | certificate = keyChain.getPib() |
| 141 | .getIdentity(security::v2::extractIdentityFromKeyName(name)) |
| 142 | .getKey(name) |
| 143 | .getDefaultCertificate(); |
| 144 | } |
| 145 | else { |
| 146 | certificate = keyChain.getPib() |
| 147 | .getIdentity(security::v2::extractIdentityFromCertName(name)) |
| 148 | .getKey(security::v2::extractKeyNameFromCertName(name)) |
Junxiao Shi | 478206e | 2017-10-15 00:22:35 +0000 | [diff] [blame] | 149 | .getCertificate(name); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 150 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 151 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 152 | else { |
| 153 | certificate = loadCertificate(name); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 154 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 155 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 156 | catch (const security::Pib::Error& e) { |
| 157 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 158 | return 1; |
| 159 | } |
| 160 | catch (const CannotLoadCertificate&) { |
| 161 | std::cerr << "Cannot load certificate from `" << name << "`" << std::endl; |
| 162 | return 1; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 163 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 165 | if (isPretty) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 166 | std::cout << certificate << std::endl; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 167 | } |
| 168 | else { |
| 169 | if (isStdOut) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 170 | io::save(certificate, std::cout); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 171 | return 0; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 173 | if (isRepoOut) { |
| 174 | using namespace boost::asio::ip; |
| 175 | tcp::iostream request_stream; |
| 176 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 177 | request_stream.connect(repoHost, repoPort); |
| 178 | if (!request_stream) { |
| 179 | std::cerr << "fail to open the stream!" << std::endl; |
| 180 | return 1; |
| 181 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 182 | request_stream.write(reinterpret_cast<const char*>(certificate.wireEncode().wire()), |
| 183 | certificate.wireEncode().size()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 185 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 186 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 187 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 191 | } // namespace ndnsec |
| 192 | } // namespace ndn |