blob: d39abdbea4fb1552a88aa9f430addd3ae760a930 [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_set_default(int argc, char** argv)
30{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031 namespace po = boost::program_options;
32
33 std::string certFileName;
34 bool isSetDefaultId = true;
35 bool isSetDefaultKey = false;
36 bool isSetDefaultCert = false;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080037 Name name;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038
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 Afanasyev35109a12017-01-04 15:39:06 -080042 ("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 Afanasyev82c359c2017-01-04 14:48:07 -080045 ;
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 Afanasyev35109a12017-01-04 15:39:06 -080071 isSetDefaultId = !isSetDefaultKey && !isSetDefaultCert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080072
Alexander Afanasyev35109a12017-01-04 15:39:06 -080073 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074
75 if (isSetDefaultId) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080076 security::Identity identity = keyChain.getPib().getIdentity(name);
77 keyChain.setDefaultIdentity(identity);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078 return 0;
79 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080080
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080081 if (isSetDefaultKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080082 security::Identity identity = keyChain.getPib().getIdentity(security::v2::extractIdentityFromKeyName(name));
83 security::Key key = identity.getKey(name);
84 keyChain.setDefaultKey(identity, key);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080085 return 0;
86 }
87
88 if (isSetDefaultCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080089 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 Afanasyev82c359c2017-01-04 14:48:07 -080093 return 0;
94 }
95
96 return 1;
97}
98
99} // namespace ndnsec
100} // namespace ndn