blob: 336a574b7e8c8414ca131af8db1887d4be61b984 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5759be32017-10-15 00:00:52 +00002/*
Davide Pesavento19442812018-11-23 14:00:04 -05003 * Copyright (c) 2013-2018 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"
Davide Pesavento19442812018-11-23 14:00:04 -050024
25#include <ndn-cxx/util/indented-stream.hpp>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080027namespace ndn {
28namespace ndnsec {
29
Alexander Afanasyev35109a12017-01-04 15:39:06 -080030class Printer
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080031{
Alexander Afanasyev35109a12017-01-04 15:39:06 -080032public:
33 Printer(int verboseLevel)
34 : m_verboseLevel(verboseLevel)
35 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080036 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080037
Alexander Afanasyev35109a12017-01-04 15:39:06 -080038 void
39 printIdentity(const security::Identity& identity, bool isDefault)
40 {
41 if (isDefault)
42 std::cout << "* ";
43 else
44 std::cout << " ";
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080045
Alexander Afanasyev35109a12017-01-04 15:39:06 -080046 std::cout << identity.getName() << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080047
Alexander Afanasyev35109a12017-01-04 15:39:06 -080048 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 Yu3e8b52e2014-11-26 22:05:00 -080056
Alexander Afanasyev35109a12017-01-04 15:39:06 -080057 for (const auto& key : identity.getKeys()) {
58 printKey(key, key == defaultKey);
59 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080060
Alexander Afanasyev35109a12017-01-04 15:39:06 -080061 std::cout << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080062 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080063 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080064
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
106private:
107 int m_verboseLevel;
108};
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800109
Yingdi Yub61f5402014-02-26 17:46:11 -0800110int
111ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112{
113 using namespace ndn;
114 namespace po = boost::program_options;
115
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800116 int verboseLevel = 0; // 0 print identity only
117 // 1 print key name
118 // 2 print cert name
119 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800120
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800121 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 Yu8d7468f2014-02-21 14:49:45 -0800128 ;
129
130 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800131 try {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800132 po::store(po::parse_command_line(argc, argv, options), vm);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800133 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 Yub61f5402014-02-26 17:46:11 -0800140
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800141 if (vm.count("help") != 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800142 std::cerr << options << std::endl;
143 ;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800144 return 0;
145 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800146
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800147 int tmpVerboseLevel = 0;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800148 if (vm.count("cert") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800149 tmpVerboseLevel = 2;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800150 else if (vm.count("key") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800151 tmpVerboseLevel = 1;
152
153 verboseLevel = std::max(verboseLevel, tmpVerboseLevel);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800154
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800155 security::v2::KeyChain keyChain;
156 Printer printer(verboseLevel);
Yingdi Yub61f5402014-02-26 17:46:11 -0800157
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800158 // TODO add API to check for default identity (may be from the identity itself)
159 security::Identity defaultIdentity;
160 try {
Junxiao Shi5759be32017-10-15 00:00:52 +0000161 defaultIdentity = keyChain.getPib().getDefaultIdentity();
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800162 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800163 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 Yu3e8b52e2014-11-26 22:05:00 -0800168 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800169
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800170 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800171}
172
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800173} // namespace ndnsec
174} // namespace ndn