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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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_LIST_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_LIST_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 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 29 | void |
| 30 | printCertificate(ndn::KeyChain& keyChain, |
| 31 | const ndn::Name& certName, |
| 32 | bool isDefault, |
| 33 | int verboseLevel) |
| 34 | { |
| 35 | if (isDefault) |
| 36 | std::cout << " +->* "; |
| 37 | else |
| 38 | std::cout << " +-> "; |
| 39 | |
| 40 | std::cout << certName << std::endl; |
| 41 | |
| 42 | if (verboseLevel >= 3) { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 43 | ndn::shared_ptr<ndn::security::v1::IdentityCertificate> certificate = keyChain.getCertificate(certName); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 44 | if (static_cast<bool>(certificate)) |
| 45 | certificate->printCertificate(std::cout, " "); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | printKey(ndn::KeyChain& keyChain, |
| 51 | const ndn::Name& keyName, |
| 52 | bool isDefault, |
| 53 | int verboseLevel) |
| 54 | { |
| 55 | if (isDefault) |
| 56 | std::cout << " +->* "; |
| 57 | else |
| 58 | std::cout << " +-> "; |
| 59 | |
| 60 | std::cout << keyName << std::endl; |
| 61 | |
| 62 | if (verboseLevel >= 2) { |
| 63 | std::vector<ndn::Name> defaultCertificates; |
| 64 | keyChain.getAllCertificateNamesOfKey(keyName, defaultCertificates, true); |
| 65 | |
| 66 | for (const auto& certName : defaultCertificates) |
| 67 | printCertificate(keyChain, certName, true, verboseLevel); |
| 68 | |
| 69 | std::vector<ndn::Name> otherCertificates; |
| 70 | keyChain.getAllCertificateNamesOfKey(keyName, otherCertificates, false); |
| 71 | for (const auto& certName : otherCertificates) |
| 72 | printCertificate(keyChain, certName, false, verboseLevel); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | printIdentity(ndn::KeyChain& keyChain, |
| 78 | const ndn::Name& identity, |
| 79 | bool isDefault, |
| 80 | int verboseLevel) |
| 81 | { |
| 82 | if (isDefault) |
| 83 | std::cout << "* "; |
| 84 | else |
| 85 | std::cout << " "; |
| 86 | |
| 87 | std::cout << identity << std::endl; |
| 88 | |
| 89 | if (verboseLevel >= 1) { |
| 90 | std::vector<ndn::Name> defaultKeys; |
| 91 | keyChain.getAllKeyNamesOfIdentity(identity, defaultKeys, true); |
| 92 | for (const auto& keyName : defaultKeys) |
| 93 | printKey(keyChain, keyName, true, verboseLevel); |
| 94 | |
| 95 | std::vector<ndn::Name> otherKeys; |
| 96 | keyChain.getAllKeyNamesOfIdentity(identity, otherKeys, false); |
| 97 | for (const auto& keyName : otherKeys) { |
| 98 | printKey(keyChain, keyName, false, verboseLevel); |
| 99 | } |
| 100 | |
| 101 | std::cout << std::endl; |
| 102 | } |
| 103 | } |
| 104 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 105 | int |
| 106 | ndnsec_list(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 107 | { |
| 108 | using namespace ndn; |
| 109 | namespace po = boost::program_options; |
| 110 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 111 | int verboseLevel = 0; // 0 print identity only |
| 112 | // 1 print key name |
| 113 | // 2 print cert name |
| 114 | // 3 print cert content |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 115 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 116 | po::options_description options("General Usage\n ndnsec list [-h] [-k|c]\nGeneral options"); |
| 117 | options.add_options() |
| 118 | ("help,h", "produce help message") |
| 119 | ("key,k", "granularity: key") |
| 120 | ("cert,c", "granularity: certificate") |
| 121 | ("verbose,v", accumulator<int>(&verboseLevel), |
| 122 | "verbose mode: -v is equivalent to -k, -vv is equivalent to -c") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 123 | ; |
| 124 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 125 | po::options_description oldOptions; |
| 126 | oldOptions.add_options() |
| 127 | ("key2,K", "granularity: key") |
| 128 | ("cert2,C", "granularity: certificate"); |
| 129 | |
| 130 | po::options_description allOptions; |
| 131 | allOptions.add(options).add(oldOptions); |
| 132 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 133 | po::variables_map vm; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 134 | try { |
| 135 | po::store(po::parse_command_line(argc, argv, allOptions), vm); |
| 136 | po::notify(vm); |
| 137 | } |
| 138 | catch (const std::exception& e) { |
| 139 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 140 | std::cerr << options << std::endl; |
| 141 | return 1; |
| 142 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 143 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 144 | if (vm.count("help") != 0) { |
| 145 | std::cerr << options << std::endl;; |
| 146 | return 0; |
| 147 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 149 | int tmpVerboseLevel = 0; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 150 | if (vm.count("cert") != 0 || vm.count("cert2") != 0) |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 151 | tmpVerboseLevel = 2; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 152 | else if(vm.count("key") != 0 || vm.count("key2") != 0) |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 153 | tmpVerboseLevel = 1; |
| 154 | |
| 155 | verboseLevel = std::max(verboseLevel, tmpVerboseLevel); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 156 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 157 | KeyChain keyChain; |
| 158 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 159 | std::vector<Name> defaultIdentities; |
| 160 | keyChain.getAllIdentities(defaultIdentities, true); |
| 161 | for (const auto& identity : defaultIdentities) { |
| 162 | printIdentity(keyChain, identity, true, verboseLevel); |
| 163 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 164 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 165 | std::vector<Name> otherIdentities; |
| 166 | keyChain.getAllIdentities(otherIdentities, false); |
| 167 | for (const auto& identity : otherIdentities) { |
| 168 | printIdentity(keyChain, identity, false, verboseLevel); |
| 169 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 170 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 171 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 174 | #endif // NDN_TOOLS_NDNSEC_LIST_HPP |