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