blob: f40ab5e99b612ac846cccd086a642997807ba8a4 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_LIST_HPP
25#define NDN_TOOLS_NDNSEC_LIST_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080029void
30printCertificate(ndn::KeyChain& keyChain,
31 const ndn::Name& certName,
32 bool isDefault,
33 int verboseLevel)
34{
35 if (isDefault)
36 std::cout << " +->* ";
37 else
38 std::cout << " +-> ";
39
40 std::cout << certName << std::endl;
41
42 if (verboseLevel >= 3) {
43 ndn::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
44 if (static_cast<bool>(certificate))
45 certificate->printCertificate(std::cout, " ");
46 }
47}
48
49void
50printKey(ndn::KeyChain& keyChain,
51 const ndn::Name& keyName,
52 bool isDefault,
53 int verboseLevel)
54{
55 if (isDefault)
56 std::cout << " +->* ";
57 else
58 std::cout << " +-> ";
59
60 std::cout << keyName << std::endl;
61
62 if (verboseLevel >= 2) {
63 std::vector<ndn::Name> defaultCertificates;
64 keyChain.getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
65
66 for (const auto& certName : defaultCertificates)
67 printCertificate(keyChain, certName, true, verboseLevel);
68
69 std::vector<ndn::Name> otherCertificates;
70 keyChain.getAllCertificateNamesOfKey(keyName, otherCertificates, false);
71 for (const auto& certName : otherCertificates)
72 printCertificate(keyChain, certName, false, verboseLevel);
73 }
74}
75
76void
77printIdentity(ndn::KeyChain& keyChain,
78 const ndn::Name& identity,
79 bool isDefault,
80 int verboseLevel)
81{
82 if (isDefault)
83 std::cout << "* ";
84 else
85 std::cout << " ";
86
87 std::cout << identity << std::endl;
88
89 if (verboseLevel >= 1) {
90 std::vector<ndn::Name> defaultKeys;
91 keyChain.getAllKeyNamesOfIdentity(identity, defaultKeys, true);
92 for (const auto& keyName : defaultKeys)
93 printKey(keyChain, keyName, true, verboseLevel);
94
95 std::vector<ndn::Name> otherKeys;
96 keyChain.getAllKeyNamesOfIdentity(identity, otherKeys, false);
97 for (const auto& keyName : otherKeys) {
98 printKey(keyChain, keyName, false, verboseLevel);
99 }
100
101 std::cout << std::endl;
102 }
103}
104
Yingdi Yub61f5402014-02-26 17:46:11 -0800105int
106ndnsec_list(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800107{
108 using namespace ndn;
109 namespace po = boost::program_options;
110
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800111 int verboseLevel = 0; // 0 print identity only
112 // 1 print key name
113 // 2 print cert name
114 // 3 print cert content
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800115
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800116 po::options_description options("General Usage\n ndnsec list [-h] [-k|c]\nGeneral options");
117 options.add_options()
118 ("help,h", "produce help message")
119 ("key,k", "granularity: key")
120 ("cert,c", "granularity: certificate")
121 ("verbose,v", accumulator<int>(&verboseLevel),
122 "verbose mode: -v is equivalent to -k, -vv is equivalent to -c")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800123 ;
124
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800125 po::options_description oldOptions;
126 oldOptions.add_options()
127 ("key2,K", "granularity: key")
128 ("cert2,C", "granularity: certificate");
129
130 po::options_description allOptions;
131 allOptions.add(options).add(oldOptions);
132
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800133 po::variables_map vm;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800134 try {
135 po::store(po::parse_command_line(argc, argv, allOptions), vm);
136 po::notify(vm);
137 }
138 catch (const std::exception& e) {
139 std::cerr << "ERROR: " << e.what() << std::endl;
140 std::cerr << options << std::endl;
141 return 1;
142 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800143
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800144 if (vm.count("help") != 0) {
145 std::cerr << options << std::endl;;
146 return 0;
147 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800148
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800149 int tmpVerboseLevel = 0;
Yingdi Yub61f5402014-02-26 17:46:11 -0800150 if (vm.count("cert") != 0 || vm.count("cert2") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800151 tmpVerboseLevel = 2;
Yingdi Yub61f5402014-02-26 17:46:11 -0800152 else if(vm.count("key") != 0 || vm.count("key2") != 0)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800153 tmpVerboseLevel = 1;
154
155 verboseLevel = std::max(verboseLevel, tmpVerboseLevel);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800156
Yingdi Yub61f5402014-02-26 17:46:11 -0800157 KeyChain keyChain;
158
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800159 std::vector<Name> defaultIdentities;
160 keyChain.getAllIdentities(defaultIdentities, true);
161 for (const auto& identity : defaultIdentities) {
162 printIdentity(keyChain, identity, true, verboseLevel);
163 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800164
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800165 std::vector<Name> otherIdentities;
166 keyChain.getAllIdentities(otherIdentities, false);
167 for (const auto& identity : otherIdentities) {
168 printIdentity(keyChain, identity, false, verboseLevel);
169 }
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 Afanasyevd7db8bf2015-01-04 15:31:02 -0800174#endif // NDN_TOOLS_NDNSEC_LIST_HPP