blob: cfda520aedaaaa6cb33e76d8863363c68e689718 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yub61f5402014-02-26 17:46:11 -080028int
29ndnsec_delete(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030{
31 using namespace ndn;
32 namespace po = boost::program_options;
33
Yingdi Yub61f5402014-02-26 17:46:11 -080034 bool isDeleteKey = false;
35 bool isDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 std::string name;
37
Yingdi Yu6147ef42014-12-08 17:48:32 -080038 po::options_description description("General Usage\n"
39 "ndnsec delete [-h] [-k|c] name\n"
40 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080041 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080043 ("delete-key,k", "(Optional) delete a key if specified.")
44 ("delete-key2,K", "(Optional) delete a key if specified.")
45 ("delete-cert,c", "(Optional) delete a certificate if specified.")
46 ("delete-cert2,C", "(Optional) delete a certificate if specified.")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 ("name,n", po::value<std::string>(&name), "By default, it refers to an identity."
Yingdi Yub61f5402014-02-26 17:46:11 -080048 "If -k is specified, it refers to a key."
49 "If -c is specified, it refers to a certificate.");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 ;
51
52 po::positional_options_description p;
53 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080054
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055 po::variables_map vm;
Yingdi Yu6147ef42014-12-08 17:48:32 -080056 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080057 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Yingdi Yu6147ef42014-12-08 17:48:32 -080058 po::notify(vm);
59 }
60 catch (const std::exception& e) {
61 std::cerr << "ERROR: " << e.what() << std::endl;
62 std::cerr << description << std::endl;
63 return 2;
64 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065
Yingdi Yu6147ef42014-12-08 17:48:32 -080066 if (vm.count("help") != 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080067 std::cerr << description << std::endl;
Yingdi Yu6147ef42014-12-08 17:48:32 -080068 return 0;
69 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070
Yingdi Yu6147ef42014-12-08 17:48:32 -080071 if (vm.count("name") == 0) {
72 std::cerr << "ERROR: name must be specified" << std::endl;
73 std::cerr << description << std::endl;
74 return 2;
75 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076
Yingdi Yub61f5402014-02-26 17:46:11 -080077 if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 0)
Yingdi Yu6147ef42014-12-08 17:48:32 -080078 isDeleteCert = true;
79
Yingdi Yub61f5402014-02-26 17:46:11 -080080 else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0)
Yingdi Yu6147ef42014-12-08 17:48:32 -080081 isDeleteKey = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080082
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080083 security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080084
Yingdi Yu6147ef42014-12-08 17:48:32 -080085 try {
86 if (isDeleteCert) {
87 if (!keyChain.doesCertificateExist(name)) {
88 std::cerr << "ERROR: Certificate does not exist: " << name << std::endl;
89 return 1;
90 }
91
Yingdi Yub61f5402014-02-26 17:46:11 -080092 keyChain.deleteCertificate(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -080093 std::cerr << "OK: Delete certificate: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080094 }
Yingdi Yu6147ef42014-12-08 17:48:32 -080095 else if (isDeleteKey) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080096 if (!keyChain.doesPublicKeyExist(name) && !keyChain.doesKeyExistInTpm(name, KeyClass::PRIVATE)) {
Yingdi Yu6147ef42014-12-08 17:48:32 -080097 std::cerr << "ERROR: Key does not exist: " << name << std::endl;
98 return 1;
99 }
100
Yingdi Yub61f5402014-02-26 17:46:11 -0800101 keyChain.deleteKey(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -0800102 std::cerr << "OK: Delete key: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800103 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800104 else {
105 if (!keyChain.doesIdentityExist(name)) {
106 std::cerr << "ERROR: Identity does not exist: " << name << std::endl;
107 return 1;
108 }
109
Yingdi Yub61f5402014-02-26 17:46:11 -0800110 keyChain.deleteIdentity(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -0800111 std::cerr << "OK: Delete identity: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800112 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800113 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800114 catch (const security::v1::SecPublicInfo::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800115 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
116 return 2;
117 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800118 catch (const security::v1::SecTpm::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800119 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
120 return 2;
121 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800122 catch (const security::v1::KeyChain::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800123 std::cerr << "ERROR: " << e.what() << std::endl;
124 return 2;
125 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800126
127 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128}
129
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800130} // namespace ndnsec
131} // namespace ndn