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