blob: b0ebf09ffe5cfbd447d09ae9a25d4a8cfab2f5cd [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_set_default(int argc, char** argv)
29{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030 namespace po = boost::program_options;
31
Alexander Afanasyev35109a12017-01-04 15:39:06 -080032 Name name;
Davide Pesaventob310efb2019-04-11 22:10:24 -040033 bool wantSetDefaultKey = false;
34 bool wantSetDefaultCert = false;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035
Davide Pesaventob310efb2019-04-11 22:10:24 -040036 po::options_description description(
37 "Usage: ndnsec set-default [-h] [-k|-c] [-n] NAME\n"
38 "\n"
39 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080040 description.add_options()
41 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 ("name,n", po::value<Name>(&name), "the identity/key/certificate name to set")
43 ("default-key,k", po::bool_switch(&wantSetDefaultKey), "set default key of the identity")
44 ("default-cert,c", po::bool_switch(&wantSetDefaultCert), "set default certificate of the key")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080045 ;
46
47 po::positional_options_description p;
48 p.add("name", 1);
Davide Pesaventob310efb2019-04-11 22:10:24 -040049
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 po::variables_map vm;
51 try {
52 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040056 std::cerr << "ERROR: " << e.what() << "\n\n"
57 << description << std::endl;
58 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080059 }
60
Davide Pesaventob310efb2019-04-11 22:10:24 -040061 if (vm.count("help") > 0) {
62 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 return 0;
64 }
65
66 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 std::cerr << "ERROR: you must specify a name" << std::endl;
68 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080069 }
70
Davide Pesaventob310efb2019-04-11 22:10:24 -040071 if (wantSetDefaultKey && wantSetDefaultCert) {
72 std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl;
73 return 2;
74 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075
Davide Pesaventof2cae612021-03-24 18:47:05 -040076 KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080077
Davide Pesaventob310efb2019-04-11 22:10:24 -040078 if (wantSetDefaultKey) {
Davide Pesaventof2cae612021-03-24 18:47:05 -040079 auto identity = keyChain.getPib().getIdentity(security::extractIdentityFromKeyName(name));
Davide Pesaventob310efb2019-04-11 22:10:24 -040080 auto key = identity.getKey(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -080081 keyChain.setDefaultKey(identity, key);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080082 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 else if (wantSetDefaultCert) {
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 auto identity = keyChain.getPib().getIdentity(security::extractIdentityFromCertName(name));
85 auto key = identity.getKey(security::extractKeyNameFromCertName(name));
Davide Pesaventob310efb2019-04-11 22:10:24 -040086 auto cert = key.getCertificate(name);
Alexander Afanasyev35109a12017-01-04 15:39:06 -080087 keyChain.setDefaultCertificate(key, cert);
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 }
89 else {
90 auto identity = keyChain.getPib().getIdentity(name);
91 keyChain.setDefaultIdentity(identity);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092 }
93
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080095}
96
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040097} // namespace ndn::ndnsec