blob: 0e2b4991c988dc4c222a24f76dc625c41525210d [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- 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
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
33 bool isGetDefaultId = true;
34 bool isGetDefaultKey = false;
35 bool isGetDefaultCert = false;
36 bool isQuiet = false;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080037 Name identityName;
38 Name keyName;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039
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 Afanasyev35109a12017-01-04 15:39:06 -080047 ("identity,i", po::value<Name>(&identityName), "target identity")
48 ("key,K", po::value<Name>(&keyName), "target key")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049 ("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 Afanasyev35109a12017-01-04 15:39:06 -080082 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080083
84 if (vm.count("key") != 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080085 if (isGetDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080086 std::cout << keyChain.getPib()
87 .getIdentity(security::v2::extractIdentityFromKeyName(keyName))
88 .getKey(keyName)
89 .getDefaultCertificate().getName();
90
91 if (!isQuiet) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092 std::cout << std::endl;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080093 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094 return 0;
95 }
96 return 1;
97 }
98 else if (vm.count("identity") != 0) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080099 security::Key key = keyChain.getPib()
100 .getIdentity(identityName)
101 .getDefaultKey();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800102
103 if (isGetDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104 std::cout << key.getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800105 if (!isQuiet)
106 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800107 return 0;
108 }
109 if (isGetDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800110 std::cout << key.getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800111 if (!isQuiet)
112 std::cout << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800113 return 0;
114 }
115 return 1;
116 }
117 else {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800118 security::Identity identity = keyChain.getPib().getDefaultIdentity();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800119 if (isGetDefaultId) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800120 std::cout << identity.getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800121 if (!isQuiet)
122 std::cout << std::endl;
123 return 0;
124 }
125 if (isGetDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800126 std::cout << identity.getDefaultKey().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800127 if (!isQuiet)
128 std::cout << std::endl;
129 return 0;
130 }
131 if (isGetDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800132 std::cout << identity.getDefaultKey().getDefaultCertificate().getName();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800133 if (!isQuiet)
134 std::cout << std::endl;
135 return 0;
136 }
137 return 1;
138 }
139}
140
141} // namespace ndnsec
142} // namespace ndn