blob: 457fc2ef81a8b67bd89d1be441a4c813d5f1819e [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 Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 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
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040022#include "accumulator.hpp"
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080023#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#include "util.hpp"
Davide Pesavento19442812018-11-23 14:00:04 -050025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "ndn-cxx/util/indented-stream.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080028namespace ndn {
29namespace ndnsec {
30
Alexander Afanasyev35109a12017-01-04 15:39:06 -080031class Printer
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080032{
Alexander Afanasyev35109a12017-01-04 15:39:06 -080033public:
Davide Pesaventob310efb2019-04-11 22:10:24 -040034 explicit
Alexander Afanasyev35109a12017-01-04 15:39:06 -080035 Printer(int verboseLevel)
36 : m_verboseLevel(verboseLevel)
37 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080038 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080039
Alexander Afanasyev35109a12017-01-04 15:39:06 -080040 void
Davide Pesaventob310efb2019-04-11 22:10:24 -040041 printIdentity(const security::Identity& identity, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080042 {
43 if (isDefault)
44 std::cout << "* ";
45 else
46 std::cout << " ";
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080047
Alexander Afanasyev35109a12017-01-04 15:39:06 -080048 std::cout << identity.getName() << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080049
Alexander Afanasyev35109a12017-01-04 15:39:06 -080050 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 Yu3e8b52e2014-11-26 22:05:00 -080058
Alexander Afanasyev35109a12017-01-04 15:39:06 -080059 for (const auto& key : identity.getKeys()) {
60 printKey(key, key == defaultKey);
61 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080062
Alexander Afanasyev35109a12017-01-04 15:39:06 -080063 std::cout << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080064 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080065 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080066
67 void
Davide Pesaventob310efb2019-04-11 22:10:24 -040068 printKey(const security::Key& key, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080069 {
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 Pesaventof2cae612021-03-24 18:47:05 -040078 security::Certificate defaultCert;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080079 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 Pesaventof2cae612021-03-24 18:47:05 -040093 printCertificate(const security::Certificate& cert, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080094 {
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
108private:
109 int m_verboseLevel;
110};
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800111
Yingdi Yub61f5402014-02-26 17:46:11 -0800112int
113ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800114{
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800115 namespace po = boost::program_options;
116
Davide Pesaventob310efb2019-04-11 22:10:24 -0400117 bool wantKey = false;
118 bool wantCert = false;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800119 int verboseLevel = 0; // 0 print identity only
120 // 1 print key name
121 // 2 print cert name
122 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800123
Davide Pesaventob310efb2019-04-11 22:10:24 -0400124 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 Yu3e8b52e2014-11-26 22:05:00 -0800132 ("verbose,v", accumulator<int>(&verboseLevel),
Davide Pesaventob310efb2019-04-11 22:10:24 -0400133 "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 Yu8d7468f2014-02-21 14:49:45 -0800135 ;
136
137 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800138 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400139 po::store(po::parse_command_line(argc, argv, description), vm);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800140 po::notify(vm);
141 }
142 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400143 std::cerr << "ERROR: " << e.what() << "\n\n"
144 << description << std::endl;
145 return 2;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800146 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800147
Davide Pesaventob310efb2019-04-11 22:10:24 -0400148 if (vm.count("help") > 0) {
149 std::cout << description << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800150 return 0;
151 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800152
Davide Pesaventob310efb2019-04-11 22:10:24 -0400153 verboseLevel = std::max(verboseLevel, wantCert ? 2 : wantKey ? 1 : 0);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800154
Davide Pesaventof2cae612021-03-24 18:47:05 -0400155 KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -0800156
Davide Pesaventob310efb2019-04-11 22:10:24 -0400157 // TODO: add API to check for default identity (may be from the identity itself)
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800158 security::Identity defaultIdentity;
159 try {
Junxiao Shi5759be32017-10-15 00:00:52 +0000160 defaultIdentity = 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 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400165
166 Printer printer(verboseLevel);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800167 for (const auto& identity : keyChain.getPib().getIdentities()) {
168 printer.printIdentity(identity, identity == defaultIdentity);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800169 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800170
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800171 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800172}
173
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800174} // namespace ndnsec
175} // namespace ndn