blob: 3d3a7f231c5211f94e7357a4cffac36a6dcebb4b [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 Pesavento25d9c9f2024-06-23 15:10:05 -04003 * Copyright (c) 2013-2024 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"
Davide Pesavento25d9c9f2024-06-23 15:10:05 -040041 "Show the default identity, key, or certificate.\n"
42 "\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080044 description.add_options()
45 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040046 ("default-key,k", po::bool_switch(&wantDefaultKey), "show default key, instead of identity")
47 ("default-cert,c", po::bool_switch(&wantDefaultCert), "show default certificate, instead of identity")
48 ("identity,i", po::value<Name>(&identityName), "target identity")
49 ("key,K", po::value<Name>(&keyName), "target key")
50 ("quiet,q", po::bool_switch(&isQuiet), "do not print trailing newline")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080051 ;
52
53 po::variables_map vm;
54 try {
55 po::store(po::parse_command_line(argc, argv, description), vm);
56 po::notify(vm);
57 }
58 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040059 std::cerr << "ERROR: " << e.what() << "\n\n"
60 << description << std::endl;
61 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080062 }
63
Davide Pesaventob310efb2019-04-11 22:10:24 -040064 if (vm.count("help") > 0) {
65 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080066 return 0;
67 }
68
Davide Pesaventob310efb2019-04-11 22:10:24 -040069 if (wantDefaultKey && wantDefaultCert) {
70 std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl;
71 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080072 }
73
Davide Pesaventob310efb2019-04-11 22:10:24 -040074 if (vm.count("identity") && vm.count("key")) {
75 std::cerr << "ERROR: cannot specify both '--identity' and '--key'" << std::endl;
76 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080077 }
78
Davide Pesaventof2cae612021-03-24 18:47:05 -040079 KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080080
Davide Pesaventob310efb2019-04-11 22:10:24 -040081 if (vm.count("key") > 0) {
82 if (wantDefaultCert) {
83 auto cert = keyChain.getPib()
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 .getIdentity(security::extractIdentityFromKeyName(keyName))
Davide Pesaventob310efb2019-04-11 22:10:24 -040085 .getKey(keyName)
86 .getDefaultCertificate();
87 std::cout << cert.getName();
Alexander Afanasyev35109a12017-01-04 15:39:06 -080088 if (!isQuiet) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080089 std::cout << std::endl;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080090 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080091 return 0;
92 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040093 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040095 else if (vm.count("identity") > 0) {
96 auto key = keyChain.getPib()
97 .getIdentity(identityName)
98 .getDefaultKey();
99 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800100 std::cout << key.getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800101 if (!isQuiet)
102 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800103 return 0;
104 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400105 if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800106 std::cout << key.getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800107 if (!isQuiet)
108 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800109 return 0;
110 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400111 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800112 }
113 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400114 auto identity = keyChain.getPib()
115 .getDefaultIdentity();
116 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800117 std::cout << identity.getDefaultKey().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800118 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400119 else if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800120 std::cout << identity.getDefaultKey().getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800121 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400122 else {
123 std::cout << identity.getName();
124 }
125 if (!isQuiet)
126 std::cout << std::endl;
127 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800128 }
129}
130
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400131} // namespace ndn::ndnsec