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 | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2021 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 { |
| 30 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 31 | class Printer |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 33 | public: |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 34 | explicit |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 35 | Printer(int verboseLevel) |
| 36 | : m_verboseLevel(verboseLevel) |
| 37 | { |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 38 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 40 | void |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 41 | printIdentity(const security::Identity& identity, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 42 | { |
| 43 | if (isDefault) |
| 44 | std::cout << "* "; |
| 45 | else |
| 46 | std::cout << " "; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 48 | std::cout << identity.getName() << std::endl; |
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 | if (m_verboseLevel >= 1) { |
| 51 | security::Key defaultKey; |
| 52 | try { |
| 53 | defaultKey = identity.getDefaultKey(); |
| 54 | } |
| 55 | catch (const security::Pib::Error&) { |
| 56 | // no default key |
| 57 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 59 | for (const auto& key : identity.getKeys()) { |
| 60 | printKey(key, key == defaultKey); |
| 61 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 63 | std::cout << std::endl; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 64 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 65 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 66 | |
| 67 | void |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 68 | printKey(const security::Key& key, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 69 | { |
| 70 | if (isDefault) |
| 71 | std::cout << " +->* "; |
| 72 | else |
| 73 | std::cout << " +-> "; |
| 74 | |
| 75 | std::cout << key.getName() << std::endl; |
| 76 | |
| 77 | if (m_verboseLevel >= 2) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame^] | 78 | security::Certificate defaultCert; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 79 | try { |
| 80 | defaultCert = key.getDefaultCertificate(); |
| 81 | } |
| 82 | catch (const security::Pib::Error&) { |
| 83 | // no default certificate |
| 84 | } |
| 85 | |
| 86 | for (const auto& cert : key.getCertificates()) { |
| 87 | printCertificate(cert, cert == defaultCert); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame^] | 93 | printCertificate(const security::Certificate& cert, bool isDefault) const |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 94 | { |
| 95 | if (isDefault) |
| 96 | std::cout << " +->* "; |
| 97 | else |
| 98 | std::cout << " +-> "; |
| 99 | |
| 100 | std::cout << cert.getName() << std::endl; |
| 101 | |
| 102 | if (m_verboseLevel >= 3) { |
| 103 | util::IndentedStream os(std::cout, " "); |
| 104 | os << cert; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | int m_verboseLevel; |
| 110 | }; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 111 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 112 | int |
| 113 | ndnsec_list(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 114 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 115 | namespace po = boost::program_options; |
| 116 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 117 | bool wantKey = false; |
| 118 | bool wantCert = false; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 119 | int verboseLevel = 0; // 0 print identity only |
| 120 | // 1 print key name |
| 121 | // 2 print cert name |
| 122 | // 3 print cert content |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 123 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 124 | po::options_description description( |
| 125 | "Usage: ndnsec list [-h] [-k] [-c] [-v]\n" |
| 126 | "\n" |
| 127 | "Options"); |
| 128 | description.add_options() |
| 129 | ("help,h", "produce help message") |
| 130 | ("key,k", po::bool_switch(&wantKey), "list all keys associated with each identity") |
| 131 | ("cert,c", po::bool_switch(&wantCert), "list all certificates associated with each key") |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 132 | ("verbose,v", accumulator<int>(&verboseLevel), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 133 | "verbose mode, can be repeated for increased verbosity: -v is equivalent to -k, " |
| 134 | "-vv is equivalent to -c, -vvv shows detailed information for each certificate") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 135 | ; |
| 136 | |
| 137 | po::variables_map vm; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 138 | try { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 139 | po::store(po::parse_command_line(argc, argv, description), vm); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 140 | po::notify(vm); |
| 141 | } |
| 142 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 143 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 144 | << description << std::endl; |
| 145 | return 2; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 146 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 147 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 148 | if (vm.count("help") > 0) { |
| 149 | std::cout << description << std::endl; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 152 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 153 | verboseLevel = std::max(verboseLevel, wantCert ? 2 : wantKey ? 1 : 0); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 154 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame^] | 155 | KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 156 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 157 | // TODO: add API to check for default identity (may be from the identity itself) |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 158 | security::Identity defaultIdentity; |
| 159 | try { |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 160 | defaultIdentity = keyChain.getPib().getDefaultIdentity(); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 161 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 162 | catch (const security::Pib::Error&) { |
| 163 | // no default identity |
| 164 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 165 | |
| 166 | Printer printer(verboseLevel); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 167 | for (const auto& identity : keyChain.getPib().getIdentities()) { |
| 168 | printer.printIdentity(identity, identity == defaultIdentity); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 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 | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 174 | } // namespace ndnsec |
| 175 | } // namespace ndn |