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