blob: 1aa2d4ece6172073f33b3cd700ddbd87c743f62d [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 Pesaventob310efb2019-04-11 22:10:24 -04003 * Copyright (c) 2013-2019 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
Davide Pesavento7e780642018-11-24 15:51:34 -050025#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:
Davide Pesaventob310efb2019-04-11 22:10:24 -040033 explicit
Alexander Afanasyev35109a12017-01-04 15:39:06 -080034 Printer(int verboseLevel)
35 : m_verboseLevel(verboseLevel)
36 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080037 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080038
Alexander Afanasyev35109a12017-01-04 15:39:06 -080039 void
Davide Pesaventob310efb2019-04-11 22:10:24 -040040 printIdentity(const security::Identity& identity, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080041 {
42 if (isDefault)
43 std::cout << "* ";
44 else
45 std::cout << " ";
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080046
Alexander Afanasyev35109a12017-01-04 15:39:06 -080047 std::cout << identity.getName() << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080048
Alexander Afanasyev35109a12017-01-04 15:39:06 -080049 if (m_verboseLevel >= 1) {
50 security::Key defaultKey;
51 try {
52 defaultKey = identity.getDefaultKey();
53 }
54 catch (const security::Pib::Error&) {
55 // no default key
56 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080057
Alexander Afanasyev35109a12017-01-04 15:39:06 -080058 for (const auto& key : identity.getKeys()) {
59 printKey(key, key == defaultKey);
60 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080061
Alexander Afanasyev35109a12017-01-04 15:39:06 -080062 std::cout << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080063 }
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080064 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080065
66 void
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 printKey(const security::Key& key, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080068 {
69 if (isDefault)
70 std::cout << " +->* ";
71 else
72 std::cout << " +-> ";
73
74 std::cout << key.getName() << std::endl;
75
76 if (m_verboseLevel >= 2) {
77 security::v2::Certificate defaultCert;
78 try {
79 defaultCert = key.getDefaultCertificate();
80 }
81 catch (const security::Pib::Error&) {
82 // no default certificate
83 }
84
85 for (const auto& cert : key.getCertificates()) {
86 printCertificate(cert, cert == defaultCert);
87 }
88 }
89 }
90
91 void
Davide Pesaventob310efb2019-04-11 22:10:24 -040092 printCertificate(const security::v2::Certificate& cert, bool isDefault) const
Alexander Afanasyev35109a12017-01-04 15:39:06 -080093 {
94 if (isDefault)
95 std::cout << " +->* ";
96 else
97 std::cout << " +-> ";
98
99 std::cout << cert.getName() << std::endl;
100
101 if (m_verboseLevel >= 3) {
102 util::IndentedStream os(std::cout, " ");
103 os << cert;
104 }
105 }
106
107private:
108 int m_verboseLevel;
109};
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800110
Yingdi Yub61f5402014-02-26 17:46:11 -0800111int
112ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800113{
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800114 namespace po = boost::program_options;
115
Davide Pesaventob310efb2019-04-11 22:10:24 -0400116 bool wantKey = false;
117 bool wantCert = false;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800118 int verboseLevel = 0; // 0 print identity only
119 // 1 print key name
120 // 2 print cert name
121 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800122
Davide Pesaventob310efb2019-04-11 22:10:24 -0400123 po::options_description description(
124 "Usage: ndnsec list [-h] [-k] [-c] [-v]\n"
125 "\n"
126 "Options");
127 description.add_options()
128 ("help,h", "produce help message")
129 ("key,k", po::bool_switch(&wantKey), "list all keys associated with each identity")
130 ("cert,c", po::bool_switch(&wantCert), "list all certificates associated with each key")
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800131 ("verbose,v", accumulator<int>(&verboseLevel),
Davide Pesaventob310efb2019-04-11 22:10:24 -0400132 "verbose mode, can be repeated for increased verbosity: -v is equivalent to -k, "
133 "-vv is equivalent to -c, -vvv shows detailed information for each certificate")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800134 ;
135
136 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800137 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400138 po::store(po::parse_command_line(argc, argv, description), vm);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800139 po::notify(vm);
140 }
141 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400142 std::cerr << "ERROR: " << e.what() << "\n\n"
143 << description << std::endl;
144 return 2;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800145 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800146
Davide Pesaventob310efb2019-04-11 22:10:24 -0400147 if (vm.count("help") > 0) {
148 std::cout << description << std::endl;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800149 return 0;
150 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800151
Davide Pesaventob310efb2019-04-11 22:10:24 -0400152 verboseLevel = std::max(verboseLevel, wantCert ? 2 : wantKey ? 1 : 0);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800153
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800154 security::v2::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -0800155
Davide Pesaventob310efb2019-04-11 22:10:24 -0400156 // TODO: add API to check for default identity (may be from the identity itself)
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800157 security::Identity defaultIdentity;
158 try {
Junxiao Shi5759be32017-10-15 00:00:52 +0000159 defaultIdentity = keyChain.getPib().getDefaultIdentity();
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800160 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800161 catch (const security::Pib::Error&) {
162 // no default identity
163 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400164
165 Printer printer(verboseLevel);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800166 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