Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
| 28 | int |
| 29 | ndnsec_set_default(int argc, char** argv) |
| 30 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | namespace po = boost::program_options; |
| 32 | |
| 33 | std::string certFileName; |
| 34 | bool isSetDefaultId = true; |
| 35 | bool isSetDefaultKey = false; |
| 36 | bool isSetDefaultCert = false; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 37 | Name name; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | |
| 39 | po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options"); |
| 40 | description.add_options() |
| 41 | ("help,h", "produce help message") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 42 | ("default_key,k", po::bool_switch(&isSetDefaultKey), "set default key of the identity") |
| 43 | ("default_cert,c", po::bool_switch(&isSetDefaultCert), "set default certificate of the key") |
| 44 | ("name,n", po::value<Name>(&name), "the identity/key/certificate name to set") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 45 | ; |
| 46 | |
| 47 | po::positional_options_description p; |
| 48 | p.add("name", 1); |
| 49 | po::variables_map vm; |
| 50 | try { |
| 51 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 52 | po::notify(vm); |
| 53 | } |
| 54 | catch (const std::exception& e) { |
| 55 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 56 | std::cerr << description << std::endl; |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | if (vm.count("help") != 0) { |
| 61 | std::cerr << description << std::endl; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | if (vm.count("name") == 0) { |
| 66 | std::cerr << "ERROR: name is required!" << std::endl; |
| 67 | std::cerr << description << std::endl; |
| 68 | return 1; |
| 69 | } |
| 70 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 71 | isSetDefaultId = !isSetDefaultKey && !isSetDefaultCert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 73 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 74 | |
| 75 | if (isSetDefaultId) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 76 | security::Identity identity = keyChain.getPib().getIdentity(name); |
| 77 | keyChain.setDefaultIdentity(identity); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 81 | if (isSetDefaultKey) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 82 | security::Identity identity = keyChain.getPib().getIdentity(security::v2::extractIdentityFromKeyName(name)); |
| 83 | security::Key key = identity.getKey(name); |
| 84 | keyChain.setDefaultKey(identity, key); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if (isSetDefaultCert) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 89 | security::Identity identity = keyChain.getPib().getIdentity(security::v2::extractIdentityFromCertName(name)); |
| 90 | security::Key key = identity.getKey(security::v2::extractKeyNameFromCertName(name)); |
| 91 | security::v2::Certificate cert = key.getCertificate(name); |
| 92 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | } // namespace ndnsec |
| 100 | } // namespace ndn |