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_get_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 | bool isGetDefaultId = true; |
| 34 | bool isGetDefaultKey = false; |
| 35 | bool isGetDefaultCert = false; |
| 36 | bool isQuiet = false; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 37 | Name identityName; |
| 38 | Name keyName; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 39 | |
| 40 | po::options_description description("General Usage\n" |
| 41 | " ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\n" |
| 42 | "General options"); |
| 43 | description.add_options() |
| 44 | ("help,h", "produce help message") |
| 45 | ("default_key,k", "get default key") |
| 46 | ("default_cert,c", "get default certificate") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 47 | ("identity,i", po::value<Name>(&identityName), "target identity") |
| 48 | ("key,K", po::value<Name>(&keyName), "target key") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 49 | ("quiet,q", "don't output trailing newline") |
| 50 | ; |
| 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) { |
| 58 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 59 | std::cerr << description << std::endl; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | if (vm.count("help") != 0) { |
| 64 | std::cerr << description << std::endl; |
| 65 | ; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | if (vm.count("default_cert") != 0) { |
| 70 | isGetDefaultCert = true; |
| 71 | isGetDefaultId = false; |
| 72 | } |
| 73 | else if (vm.count("default_key") != 0) { |
| 74 | isGetDefaultKey = true; |
| 75 | isGetDefaultId = false; |
| 76 | } |
| 77 | |
| 78 | if (vm.count("quiet") != 0) { |
| 79 | isQuiet = true; |
| 80 | } |
| 81 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 82 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 83 | |
| 84 | if (vm.count("key") != 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 85 | if (isGetDefaultCert) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 86 | std::cout << keyChain.getPib() |
| 87 | .getIdentity(security::v2::extractIdentityFromKeyName(keyName)) |
| 88 | .getKey(keyName) |
| 89 | .getDefaultCertificate().getName(); |
| 90 | |
| 91 | if (!isQuiet) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 92 | std::cout << std::endl; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | return 1; |
| 97 | } |
| 98 | else if (vm.count("identity") != 0) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 99 | security::Key key = keyChain.getPib() |
| 100 | .getIdentity(identityName) |
| 101 | .getDefaultKey(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 102 | |
| 103 | if (isGetDefaultKey) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 104 | std::cout << key.getName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 105 | if (!isQuiet) |
| 106 | std::cout << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | if (isGetDefaultCert) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 110 | std::cout << key.getDefaultCertificate().getName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 111 | if (!isQuiet) |
| 112 | std::cout << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | return 1; |
| 116 | } |
| 117 | else { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 118 | security::Identity identity = keyChain.getPib().getDefaultIdentity(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 119 | if (isGetDefaultId) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 120 | std::cout << identity.getName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 121 | if (!isQuiet) |
| 122 | std::cout << std::endl; |
| 123 | return 0; |
| 124 | } |
| 125 | if (isGetDefaultKey) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 126 | std::cout << identity.getDefaultKey().getName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 127 | if (!isQuiet) |
| 128 | std::cout << std::endl; |
| 129 | return 0; |
| 130 | } |
| 131 | if (isGetDefaultCert) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 132 | std::cout << identity.getDefaultKey().getDefaultCertificate().getName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 133 | if (!isQuiet) |
| 134 | std::cout << std::endl; |
| 135 | return 0; |
| 136 | } |
| 137 | return 1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } // namespace ndnsec |
| 142 | } // namespace ndn |