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