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