blob: 36026d74850c3dad9f2b176154286e5d8a4c98a8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Alexander Afanasyev35109a12017-01-04 15:39:06 -080024#include "util/indented-stream.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080026namespace ndn {
27namespace ndnsec {
28
Alexander Afanasyev35109a12017-01-04 15:39:06 -080029class Printer
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080030{
Alexander Afanasyev35109a12017-01-04 15:39:06 -080031public:
32 Printer(int verboseLevel)
33 : m_verboseLevel(verboseLevel)
34 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080035 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080036
Alexander Afanasyev35109a12017-01-04 15:39:06 -080037 void
38 printIdentity(const security::Identity& identity, bool isDefault)
39 {
40 if (isDefault)
41 std::cout << "* ";
42 else
43 std::cout << " ";
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080044
Alexander Afanasyev35109a12017-01-04 15:39:06 -080045 std::cout << identity.getName() << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080046
Alexander Afanasyev35109a12017-01-04 15:39:06 -080047 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 Yu3e8b52e2014-11-26 22:05:00 -080055
Alexander Afanasyev35109a12017-01-04 15:39:06 -080056 for (const auto& key : identity.getKeys()) {
57 printKey(key, key == defaultKey);
58 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080059
Alexander Afanasyev35109a12017-01-04 15:39:06 -080060 std::cout << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080061 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080062 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080063
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
105private:
106 int m_verboseLevel;
107};
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800108
Yingdi Yub61f5402014-02-26 17:46:11 -0800109int
110ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800111{
112 using namespace ndn;
113 namespace po = boost::program_options;
114
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800115 int verboseLevel = 0; // 0 print identity only
116 // 1 print key name
117 // 2 print cert name
118 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800119
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800120 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 Yu8d7468f2014-02-21 14:49:45 -0800127 ;
128
129 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800130 try {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800131 po::store(po::parse_command_line(argc, argv, options), vm);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800132 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 Yub61f5402014-02-26 17:46:11 -0800139
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800140 if (vm.count("help") != 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800141 std::cerr << options << std::endl;
142 ;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800143 return 0;
144 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800145
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800146 int tmpVerboseLevel = 0;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800147 if (vm.count("cert") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800148 tmpVerboseLevel = 2;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800149 else if (vm.count("key") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800150 tmpVerboseLevel = 1;
151
152 verboseLevel = std::max(verboseLevel, tmpVerboseLevel);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800153
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800154 security::v2::KeyChain keyChain;
155 Printer printer(verboseLevel);
Yingdi Yub61f5402014-02-26 17:46:11 -0800156
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800157 // TODO add API to check for default identity (may be from the identity itself)
158 security::Identity defaultIdentity;
159 try {
160 keyChain.getPib().getDefaultIdentity();
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800161 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800162 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 Yu3e8b52e2014-11-26 22:05:00 -0800167 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800168
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800169 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800170}
171
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800172} // namespace ndnsec
173} // namespace ndn