Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 4 | * |
| 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 Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 25 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 26 | |
| 27 | int |
| 28 | ndnsec_set_default(int argc, char** argv) |
| 29 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | namespace po = boost::program_options; |
| 31 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 32 | Name name; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 33 | bool wantSetDefaultKey = false; |
| 34 | bool wantSetDefaultCert = false; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 36 | po::options_description description( |
| 37 | "Usage: ndnsec set-default [-h] [-k|-c] [-n] NAME\n" |
| 38 | "\n" |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 39 | "Change the default identity, key, or certificate.\n" |
| 40 | "\n" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 41 | "Options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 42 | description.add_options() |
| 43 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 44 | ("name,n", po::value<Name>(&name), "the identity/key/certificate name to set") |
| 45 | ("default-key,k", po::bool_switch(&wantSetDefaultKey), "set default key of the identity") |
| 46 | ("default-cert,c", po::bool_switch(&wantSetDefaultCert), "set default certificate of the key") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 47 | ; |
| 48 | |
| 49 | po::positional_options_description p; |
| 50 | p.add("name", 1); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 51 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 52 | po::variables_map vm; |
| 53 | try { |
| 54 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 55 | po::notify(vm); |
| 56 | } |
| 57 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 58 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 59 | << description << std::endl; |
| 60 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 63 | if (vm.count("help") > 0) { |
| 64 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | if (vm.count("name") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 69 | std::cerr << "ERROR: you must specify a name" << std::endl; |
| 70 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 73 | if (wantSetDefaultKey && wantSetDefaultCert) { |
| 74 | std::cerr << "ERROR: cannot specify both '--default-key' and '--default-cert'" << std::endl; |
| 75 | return 2; |
| 76 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 77 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 78 | KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 80 | if (wantSetDefaultKey) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 81 | auto identity = keyChain.getPib().getIdentity(security::extractIdentityFromKeyName(name)); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 82 | auto key = identity.getKey(name); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 83 | keyChain.setDefaultKey(identity, key); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 84 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 85 | else if (wantSetDefaultCert) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 86 | auto identity = keyChain.getPib().getIdentity(security::extractIdentityFromCertName(name)); |
| 87 | auto key = identity.getKey(security::extractKeyNameFromCertName(name)); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 88 | auto cert = key.getCertificate(name); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 89 | keyChain.setDefaultCertificate(key, cert); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 90 | } |
| 91 | else { |
| 92 | auto identity = keyChain.getPib().getIdentity(name); |
| 93 | keyChain.setDefaultIdentity(identity); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 96 | return 0; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 99 | } // namespace ndn::ndnsec |