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 | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 22 | #include "accumulator.hpp" |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 23 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #include "util.hpp" |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/util/indented-stream.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace ndnsec { |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 30 | namespace { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 32 | // https://en.wikipedia.org/wiki/Box_Drawing |
| 33 | // https://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr |
| 34 | static const char GLYPH_HORIZONTAL[] = "\u2500 "; // "─ " |
| 35 | static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ " |
| 36 | static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─" |
| 37 | static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─" |
| 38 | static const char GLYPH_SPACE[] = " "; |
| 39 | static const char GLYPH_STAR[] = "* "; |
| 40 | |
| 41 | enum class Verbosity { |
| 42 | IDENTITY = 0, |
| 43 | KEY = 1, |
| 44 | CERT_NAME = 2, |
| 45 | CERT_FULL = 3, |
| 46 | }; |
| 47 | |
| 48 | class TreePrinter |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 50 | public: |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 51 | explicit |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 52 | TreePrinter(std::ostream& output) |
| 53 | : m_out(output) |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 54 | { |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 55 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 56 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 57 | template<typename C, typename F> |
| 58 | void |
| 59 | forEachChild(const C& container, F fn) const |
| 60 | { |
| 61 | m_branches.push_back(false); |
| 62 | |
| 63 | auto end = container.end(); |
| 64 | for (auto it = container.begin(); it != end; ++it) { |
| 65 | bool isLast = std::next(it) == end; |
| 66 | if (isLast) { |
| 67 | m_branches.back() = true; |
| 68 | } |
| 69 | printBranches(m_out); |
| 70 | fn(*it); |
| 71 | } |
| 72 | |
| 73 | m_branches.pop_back(); |
| 74 | } |
| 75 | |
| 76 | std::string |
| 77 | getIndent() const |
| 78 | { |
| 79 | std::ostringstream oss; |
| 80 | printBranches(oss, true); |
| 81 | return oss.str(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | void |
| 86 | printBranches(std::ostream& os, bool ignoreLast = false) const |
| 87 | { |
| 88 | for (size_t i = 0; i < m_branches.size(); ++i) { |
| 89 | if (i == m_branches.size() - 1 && !ignoreLast) { |
| 90 | os << (m_branches[i] ? GLYPH_UP_AND_RIGHT : GLYPH_VERTICAL_AND_RIGHT); |
| 91 | } |
| 92 | else { |
| 93 | os << (m_branches[i] ? GLYPH_SPACE : GLYPH_VERTICAL) << GLYPH_SPACE; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | std::ostream& m_out; |
| 100 | |
| 101 | private: |
| 102 | mutable std::vector<bool> m_branches; |
| 103 | }; |
| 104 | |
| 105 | class Printer : private TreePrinter |
| 106 | { |
| 107 | public: |
| 108 | explicit |
| 109 | Printer(std::ostream& output, Verbosity verbosity) |
| 110 | : TreePrinter(output) |
| 111 | , m_verbosity(verbosity) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | printPib(const security::Pib& pib) const |
| 117 | { |
| 118 | m_out << pib.getPibLocator() << std::endl; |
| 119 | |
| 120 | security::Identity defaultIdentity; |
| 121 | try { |
| 122 | defaultIdentity = pib.getDefaultIdentity(); |
| 123 | } |
| 124 | catch (const security::Pib::Error&) { |
| 125 | // no default identity |
| 126 | } |
| 127 | |
| 128 | forEachChild(pib.getIdentities(), [&] (const auto& identity) { |
| 129 | printIdentity(identity, identity == defaultIdentity); |
| 130 | }); |
| 131 | } |
| 132 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 133 | void |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 134 | printIdentity(const security::Identity& identity, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 135 | { |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 136 | printDefault(isDefault); |
| 137 | m_out << identity.getName() << std::endl; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 138 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 139 | if (m_verbosity >= Verbosity::KEY) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 140 | security::Key defaultKey; |
| 141 | try { |
| 142 | defaultKey = identity.getDefaultKey(); |
| 143 | } |
| 144 | catch (const security::Pib::Error&) { |
| 145 | // no default key |
| 146 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 147 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 148 | forEachChild(identity.getKeys(), [&] (const auto& key) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 149 | printKey(key, key == defaultKey); |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 150 | }); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 151 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 152 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 153 | |
| 154 | void |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 155 | printKey(const security::Key& key, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 156 | { |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 157 | printDefault(isDefault); |
| 158 | m_out << key.getName() << std::endl; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 159 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 160 | if (m_verbosity >= Verbosity::CERT_NAME) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 161 | security::Certificate defaultCert; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 162 | try { |
| 163 | defaultCert = key.getDefaultCertificate(); |
| 164 | } |
| 165 | catch (const security::Pib::Error&) { |
| 166 | // no default certificate |
| 167 | } |
| 168 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 169 | forEachChild(key.getCertificates(), [&] (const auto& cert) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 170 | printCertificate(cert, cert == defaultCert); |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 171 | }); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 176 | printCertificate(const security::Certificate& cert, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 177 | { |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 178 | printDefault(isDefault); |
| 179 | m_out << cert.getName() << std::endl; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 180 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 181 | if (m_verbosity >= Verbosity::CERT_FULL) { |
| 182 | util::IndentedStream os(m_out, getIndent() + GLYPH_SPACE); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 183 | os << cert; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private: |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 188 | void |
| 189 | printDefault(bool isDefault) const |
| 190 | { |
| 191 | m_out << (isDefault ? GLYPH_STAR : GLYPH_HORIZONTAL); |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | Verbosity m_verbosity; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 196 | }; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 197 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 198 | } // namespace |
| 199 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 200 | int |
| 201 | ndnsec_list(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 202 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 203 | namespace po = boost::program_options; |
| 204 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 205 | bool wantKey = false; |
| 206 | bool wantCert = false; |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 207 | auto verboseLevel = to_underlying(Verbosity::IDENTITY); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 208 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 209 | po::options_description description( |
| 210 | "Usage: ndnsec list [-h] [-k] [-c] [-v]\n" |
| 211 | "\n" |
| 212 | "Options"); |
| 213 | description.add_options() |
| 214 | ("help,h", "produce help message") |
| 215 | ("key,k", po::bool_switch(&wantKey), "list all keys associated with each identity") |
| 216 | ("cert,c", po::bool_switch(&wantCert), "list all certificates associated with each key") |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 217 | ("verbose,v", accumulator<std::underlying_type_t<Verbosity>>(&verboseLevel), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 218 | "verbose mode, can be repeated for increased verbosity: -v is equivalent to -k, " |
| 219 | "-vv is equivalent to -c, -vvv shows detailed information for each certificate") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 220 | ; |
| 221 | |
| 222 | po::variables_map vm; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 223 | try { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 224 | po::store(po::parse_command_line(argc, argv, description), vm); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 225 | po::notify(vm); |
| 226 | } |
| 227 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 228 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 229 | << description << std::endl; |
| 230 | return 2; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 231 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 232 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 233 | if (vm.count("help") > 0) { |
| 234 | std::cout << description << std::endl; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 235 | return 0; |
| 236 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 237 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 238 | KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 239 | |
Davide Pesavento | 9faf34b | 2022-06-12 22:44:07 -0400 | [diff] [blame] | 240 | auto verbosity = std::max(static_cast<Verbosity>(verboseLevel), |
| 241 | wantCert ? Verbosity::CERT_NAME : |
| 242 | wantKey ? Verbosity::KEY : |
| 243 | Verbosity::IDENTITY); |
| 244 | Printer printer(std::cout, verbosity); |
| 245 | printer.printPib(keyChain.getPib()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 246 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 247 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 250 | } // namespace ndnsec |
| 251 | } // namespace ndn |