blob: f6adbfe7a30994601ce5f59a129c4d5ba3026a4e [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"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080028void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029printCertificate(security::v1::KeyChain& keyChain,
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080030 const ndn::Name& certName,
31 bool isDefault,
32 int verboseLevel)
33{
34 if (isDefault)
35 std::cout << " +->* ";
36 else
37 std::cout << " +-> ";
38
39 std::cout << certName << std::endl;
40
41 if (verboseLevel >= 3) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080042 shared_ptr<security::v1::IdentityCertificate> certificate = keyChain.getCertificate(certName);
43 if (certificate != nullptr)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080044 certificate->printCertificate(std::cout, " ");
45 }
46}
47
48void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049printKey(security::v1::KeyChain& keyChain, const ndn::Name& keyName, bool isDefault, int verboseLevel)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080050{
51 if (isDefault)
52 std::cout << " +->* ";
53 else
54 std::cout << " +-> ";
55
56 std::cout << keyName << std::endl;
57
58 if (verboseLevel >= 2) {
59 std::vector<ndn::Name> defaultCertificates;
60 keyChain.getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
61
62 for (const auto& certName : defaultCertificates)
63 printCertificate(keyChain, certName, true, verboseLevel);
64
65 std::vector<ndn::Name> otherCertificates;
66 keyChain.getAllCertificateNamesOfKey(keyName, otherCertificates, false);
67 for (const auto& certName : otherCertificates)
68 printCertificate(keyChain, certName, false, verboseLevel);
69 }
70}
71
72void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080073printIdentity(security::v1::KeyChain& keyChain,
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080074 const ndn::Name& identity,
75 bool isDefault,
76 int verboseLevel)
77{
78 if (isDefault)
79 std::cout << "* ";
80 else
81 std::cout << " ";
82
83 std::cout << identity << std::endl;
84
85 if (verboseLevel >= 1) {
86 std::vector<ndn::Name> defaultKeys;
87 keyChain.getAllKeyNamesOfIdentity(identity, defaultKeys, true);
88 for (const auto& keyName : defaultKeys)
89 printKey(keyChain, keyName, true, verboseLevel);
90
91 std::vector<ndn::Name> otherKeys;
92 keyChain.getAllKeyNamesOfIdentity(identity, otherKeys, false);
93 for (const auto& keyName : otherKeys) {
94 printKey(keyChain, keyName, false, verboseLevel);
95 }
96
97 std::cout << std::endl;
98 }
99}
100
Yingdi Yub61f5402014-02-26 17:46:11 -0800101int
102ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103{
104 using namespace ndn;
105 namespace po = boost::program_options;
106
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800107 int verboseLevel = 0; // 0 print identity only
108 // 1 print key name
109 // 2 print cert name
110 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800111
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800112 po::options_description options("General Usage\n ndnsec list [-h] [-k|c]\nGeneral options");
113 options.add_options()
114 ("help,h", "produce help message")
115 ("key,k", "granularity: key")
116 ("cert,c", "granularity: certificate")
117 ("verbose,v", accumulator<int>(&verboseLevel),
118 "verbose mode: -v is equivalent to -k, -vv is equivalent to -c")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800119 ;
120
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800121 po::options_description oldOptions;
122 oldOptions.add_options()
123 ("key2,K", "granularity: key")
124 ("cert2,C", "granularity: certificate");
125
126 po::options_description allOptions;
127 allOptions.add(options).add(oldOptions);
128
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800129 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800130 try {
131 po::store(po::parse_command_line(argc, argv, allOptions), vm);
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 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;
Yingdi Yub61f5402014-02-26 17:46:11 -0800147 if (vm.count("cert") != 0 || vm.count("cert2") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800148 tmpVerboseLevel = 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800149 else if (vm.count("key") != 0 || vm.count("key2") != 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 Afanasyev82c359c2017-01-04 14:48:07 -0800154 security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -0800155
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800156 std::vector<Name> defaultIdentities;
157 keyChain.getAllIdentities(defaultIdentities, true);
158 for (const auto& identity : defaultIdentities) {
159 printIdentity(keyChain, identity, true, verboseLevel);
160 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800161
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800162 std::vector<Name> otherIdentities;
163 keyChain.getAllIdentities(otherIdentities, false);
164 for (const auto& identity : otherIdentities) {
165 printIdentity(keyChain, identity, false, verboseLevel);
166 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800167
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800168 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800169}
170
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800171} // namespace ndnsec
172} // namespace ndn