blob: 37f74257d4800f4c975ec2fbc11c99df8a0d7ea5 [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 Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 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
25namespace ndn {
26namespace ndnsec {
27
28int
29ndnsec_get_default(int argc, char** argv)
30{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031 namespace po = boost::program_options;
32
Davide Pesaventob310efb2019-04-11 22:10:24 -040033 bool wantDefaultKey = false;
34 bool wantDefaultCert = false;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035 bool isQuiet = false;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080036 Name identityName;
37 Name keyName;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038
Davide Pesaventob310efb2019-04-11 22:10:24 -040039 po::options_description description(
40 "Usage: ndnsec get-default [-h] [-k|-c] [-i ID|-K KEY] [-q]\n"
41 "\n"
42 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080043 description.add_options()
44 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040045 ("default-key,k", po::bool_switch(&wantDefaultKey), "show default key, instead of identity")
46 ("default-cert,c", po::bool_switch(&wantDefaultCert), "show default certificate, instead of identity")
47 ("identity,i", po::value<Name>(&identityName), "target identity")
48 ("key,K", po::value<Name>(&keyName), "target key")
49 ("quiet,q", po::bool_switch(&isQuiet), "do not print trailing newline")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 ;
51
52 po::variables_map vm;
53 try {
54 po::store(po::parse_command_line(argc, argv, description), vm);
55 po::notify(vm);
56 }
57 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040058 std::cerr << "ERROR: " << e.what() << "\n\n"
59 << description << std::endl;
60 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080061 }
62
Davide Pesaventob310efb2019-04-11 22:10:24 -040063 if (vm.count("help") > 0) {
64 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080065 return 0;
66 }
67
Davide Pesaventob310efb2019-04-11 22:10:24 -040068 if (wantDefaultKey && wantDefaultCert) {
69 std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl;
70 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080071 }
72
Davide Pesaventob310efb2019-04-11 22:10:24 -040073 if (vm.count("identity") && vm.count("key")) {
74 std::cerr << "ERROR: cannot specify both '--identity' and '--key'" << std::endl;
75 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080076 }
77
Davide Pesaventof2cae612021-03-24 18:47:05 -040078 KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080079
Davide Pesaventob310efb2019-04-11 22:10:24 -040080 if (vm.count("key") > 0) {
81 if (wantDefaultCert) {
82 auto cert = keyChain.getPib()
Davide Pesaventof2cae612021-03-24 18:47:05 -040083 .getIdentity(security::extractIdentityFromKeyName(keyName))
Davide Pesaventob310efb2019-04-11 22:10:24 -040084 .getKey(keyName)
85 .getDefaultCertificate();
86 std::cout << cert.getName();
Alexander Afanasyev35109a12017-01-04 15:39:06 -080087 if (!isQuiet) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080088 std::cout << std::endl;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080089 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080090 return 0;
91 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040092 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080093 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 else if (vm.count("identity") > 0) {
95 auto key = keyChain.getPib()
96 .getIdentity(identityName)
97 .getDefaultKey();
98 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080099 std::cout << key.getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800100 if (!isQuiet)
101 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800102 return 0;
103 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400104 if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800105 std::cout << key.getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800106 if (!isQuiet)
107 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800108 return 0;
109 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400110 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800111 }
112 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400113 auto identity = keyChain.getPib()
114 .getDefaultIdentity();
115 if (wantDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800116 std::cout << identity.getDefaultKey().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800117 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400118 else if (wantDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800119 std::cout << identity.getDefaultKey().getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800120 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400121 else {
122 std::cout << identity.getName();
123 }
124 if (!isQuiet)
125 std::cout << std::endl;
126 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800127 }
128}
129
130} // namespace ndnsec
131} // namespace ndn