blob: 8dba1bfad39a5606e6566400d8e7cfa0d0a5a4ae [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob310efb2019-04-11 22:10:24 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "ndnsec.hpp"
23#include "util.hpp"
24
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040025namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080026
27int
28ndnsec_get_default(int argc, char** argv)
29{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030 namespace po = boost::program_options;
31
Davide Pesaventob310efb2019-04-11 22:10:24 -040032 bool wantDefaultKey = false;
33 bool wantDefaultCert = false;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080034 bool isQuiet = false;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080035 Name identityName;
36 Name keyName;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080037
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 po::options_description description(
39 "Usage: ndnsec get-default [-h] [-k|-c] [-i ID|-K KEY] [-q]\n"
40 "\n"
41 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080042 description.add_options()
43 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040044 ("default-key,k", po::bool_switch(&wantDefaultKey), "show default key, instead of identity")
45 ("default-cert,c", po::bool_switch(&wantDefaultCert), "show default certificate, instead of identity")
46 ("identity,i", po::value<Name>(&identityName), "target identity")
47 ("key,K", po::value<Name>(&keyName), "target key")
48 ("quiet,q", po::bool_switch(&isQuiet), "do not print trailing newline")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049 ;
50
51 po::variables_map vm;
52 try {
53 po::store(po::parse_command_line(argc, argv, description), vm);
54 po::notify(vm);
55 }
56 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040057 std::cerr << "ERROR: " << e.what() << "\n\n"
58 << description << std::endl;
59 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080060 }
61
Davide Pesaventob310efb2019-04-11 22:10:24 -040062 if (vm.count("help") > 0) {
63 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080064 return 0;
65 }
66
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 if (wantDefaultKey && wantDefaultCert) {
68 std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl;
69 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080070 }
71
Davide Pesaventob310efb2019-04-11 22:10:24 -040072 if (vm.count("identity") && vm.count("key")) {
73 std::cerr << "ERROR: cannot specify both '--identity' and '--key'" << std::endl;
74 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075 }
76
Davide Pesaventof2cae612021-03-24 18:47:05 -040077 KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078
Davide Pesaventob310efb2019-04-11 22:10:24 -040079 if (vm.count("key") > 0) {
80 if (wantDefaultCert) {
81 auto cert = keyChain.getPib()
Davide Pesaventof2cae612021-03-24 18:47:05 -040082 .getIdentity(security::extractIdentityFromKeyName(keyName))
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 .getKey(keyName)
84 .getDefaultCertificate();
85 std::cout << cert.getName();
Alexander Afanasyev35109a12017-01-04 15:39:06 -080086 if (!isQuiet) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080087 std::cout << std::endl;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080088 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080089 return 0;
90 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040091 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040093 else if (vm.count("identity") > 0) {
94 auto key = keyChain.getPib()
95 .getIdentity(identityName)
96 .getDefaultKey();
97 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080098 std::cout << key.getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080099 if (!isQuiet)
100 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800101 return 0;
102 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400103 if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104 std::cout << key.getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800105 if (!isQuiet)
106 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800107 return 0;
108 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400109 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800110 }
111 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400112 auto identity = keyChain.getPib()
113 .getDefaultIdentity();
114 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800115 std::cout << identity.getDefaultKey().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800116 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400117 else if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800118 std::cout << identity.getDefaultKey().getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800119 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400120 else {
121 std::cout << identity.getName();
122 }
123 if (!isQuiet)
124 std::cout << std::endl;
125 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800126 }
127}
128
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400129} // namespace ndn::ndnsec