blob: 0c96af4bf03d295b04f541e7cc2adad0b4ff8bb1 [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/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_DELETE_HPP
25#define NDN_TOOLS_NDNSEC_DELETE_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
30ndnsec_delete(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
Yingdi Yub61f5402014-02-26 17:46:11 -080035 bool isDeleteKey = false;
36 bool isDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 std::string name;
38
Yingdi Yu6147ef42014-12-08 17:48:32 -080039 po::options_description description("General Usage\n"
40 "ndnsec delete [-h] [-k|c] name\n"
41 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080042 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080044 ("delete-key,k", "(Optional) delete a key if specified.")
45 ("delete-key2,K", "(Optional) delete a key if specified.")
46 ("delete-cert,c", "(Optional) delete a certificate if specified.")
47 ("delete-cert2,C", "(Optional) delete a certificate if specified.")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048 ("name,n", po::value<std::string>(&name), "By default, it refers to an identity."
Yingdi Yub61f5402014-02-26 17:46:11 -080049 "If -k is specified, it refers to a key."
50 "If -c is specified, it refers to a certificate.");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051 ;
52
53 po::positional_options_description p;
54 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080055
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 po::variables_map vm;
Yingdi Yu6147ef42014-12-08 17:48:32 -080057 try {
58 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
59 vm);
60 po::notify(vm);
61 }
62 catch (const std::exception& e) {
63 std::cerr << "ERROR: " << e.what() << std::endl;
64 std::cerr << description << std::endl;
65 return 2;
66 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067
Yingdi Yu6147ef42014-12-08 17:48:32 -080068 if (vm.count("help") != 0) {
69 std::cerr << description << std::endl;;
70 return 0;
71 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072
Yingdi Yu6147ef42014-12-08 17:48:32 -080073 if (vm.count("name") == 0) {
74 std::cerr << "ERROR: name must be specified" << std::endl;
75 std::cerr << description << std::endl;
76 return 2;
77 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078
Yingdi Yub61f5402014-02-26 17:46:11 -080079 if (vm.count("delete-cert") != 0 || vm.count("delete-cert2") != 0)
Yingdi Yu6147ef42014-12-08 17:48:32 -080080 isDeleteCert = true;
81
Yingdi Yub61f5402014-02-26 17:46:11 -080082 else if (vm.count("delete-key") != 0 || vm.count("delete-key2") != 0)
Yingdi Yu6147ef42014-12-08 17:48:32 -080083 isDeleteKey = true;
Yingdi Yub61f5402014-02-26 17:46:11 -080084
85 KeyChain keyChain;
86
Yingdi Yu6147ef42014-12-08 17:48:32 -080087 try {
88 if (isDeleteCert) {
89 if (!keyChain.doesCertificateExist(name)) {
90 std::cerr << "ERROR: Certificate does not exist: " << name << std::endl;
91 return 1;
92 }
93
Yingdi Yub61f5402014-02-26 17:46:11 -080094 keyChain.deleteCertificate(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -080095 std::cerr << "OK: Delete certificate: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080096 }
Yingdi Yu6147ef42014-12-08 17:48:32 -080097 else if (isDeleteKey) {
98 if (!keyChain.doesPublicKeyExist(name) &&
Yingdi Yu99b2a002015-08-12 12:47:44 -070099 !keyChain.doesKeyExistInTpm(name, KeyClass::PRIVATE)) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800100 std::cerr << "ERROR: Key does not exist: " << name << std::endl;
101 return 1;
102 }
103
Yingdi Yub61f5402014-02-26 17:46:11 -0800104 keyChain.deleteKey(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -0800105 std::cerr << "OK: Delete key: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800106 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800107 else {
108 if (!keyChain.doesIdentityExist(name)) {
109 std::cerr << "ERROR: Identity does not exist: " << name << std::endl;
110 return 1;
111 }
112
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 keyChain.deleteIdentity(name);
Yingdi Yu6147ef42014-12-08 17:48:32 -0800114 std::cerr << "OK: Delete identity: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800116 }
117 catch (const SecPublicInfo::Error& e) {
118 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
119 return 2;
120 }
121 catch (const SecTpm::Error& e) {
122 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
123 return 2;
124 }
125 catch (const KeyChain::Error& e) {
126 std::cerr << "ERROR: " << e.what() << std::endl;
127 return 2;
128 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800129
130 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800131}
132
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800133#endif // NDN_TOOLS_NDNSEC_DELETE_HPP