blob: 8c08cf0034a14fb0b4f0cad1c7cc3dcc83a492fb [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{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 namespace po = boost::program_options;
32
Yingdi Yub61f5402014-02-26 17:46:11 -080033 bool isDeleteKey = false;
34 bool isDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 std::string name;
36
Yingdi Yu6147ef42014-12-08 17:48:32 -080037 po::options_description description("General Usage\n"
38 "ndnsec delete [-h] [-k|c] name\n"
39 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080040 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080042 ("delete-key,k", "(Optional) delete a key if specified.")
Yingdi Yub61f5402014-02-26 17:46:11 -080043 ("delete-cert,c", "(Optional) delete a certificate if specified.")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ("name,n", po::value<std::string>(&name), "By default, it refers to an identity."
Yingdi Yub61f5402014-02-26 17:46:11 -080045 "If -k is specified, it refers to a key."
46 "If -c is specified, it refers to a certificate.");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 ;
48
49 po::positional_options_description p;
50 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080051
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052 po::variables_map vm;
Yingdi Yu6147ef42014-12-08 17:48:32 -080053 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080054 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Yingdi Yu6147ef42014-12-08 17:48:32 -080055 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 2;
61 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062
Yingdi Yu6147ef42014-12-08 17:48:32 -080063 if (vm.count("help") != 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080064 std::cerr << description << std::endl;
Yingdi Yu6147ef42014-12-08 17:48:32 -080065 return 0;
66 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067
Yingdi Yu6147ef42014-12-08 17:48:32 -080068 if (vm.count("name") == 0) {
69 std::cerr << "ERROR: name must be specified" << std::endl;
70 std::cerr << description << std::endl;
71 return 2;
72 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073
Alexander Afanasyev35109a12017-01-04 15:39:06 -080074 if (vm.count("delete-cert") != 0) {
Yingdi Yu6147ef42014-12-08 17:48:32 -080075 isDeleteCert = true;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080076 }
77 else if (vm.count("delete-key") != 0) {
Yingdi Yu6147ef42014-12-08 17:48:32 -080078 isDeleteKey = true;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080079 }
Yingdi Yub61f5402014-02-26 17:46:11 -080080
Alexander Afanasyev35109a12017-01-04 15:39:06 -080081 security::v2::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080082
Yingdi Yu6147ef42014-12-08 17:48:32 -080083 try {
84 if (isDeleteCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080085 security::Key key = keyChain.getPib()
86 .getIdentity(security::v2::extractIdentityFromCertName(name))
87 .getKey(security::v2::extractKeyNameFromCertName(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -080088
Alexander Afanasyev35109a12017-01-04 15:39:06 -080089 keyChain.deleteCertificate(key, key.getCertificate(name).getName());
Yingdi Yu6147ef42014-12-08 17:48:32 -080090 std::cerr << "OK: Delete certificate: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080091 }
Yingdi Yu6147ef42014-12-08 17:48:32 -080092 else if (isDeleteKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080093 security::Identity identity = keyChain.getPib()
94 .getIdentity(security::v2::extractIdentityFromKeyName(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -080095
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096 keyChain.deleteKey(identity, identity.getKey(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -080097 std::cerr << "OK: Delete key: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080098 }
Yingdi Yu6147ef42014-12-08 17:48:32 -080099 else {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800100 keyChain.deleteIdentity(keyChain.getPib().getIdentity(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -0800101 std::cerr << "OK: Delete identity: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800102 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800103 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104 catch (const security::Pib::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800105 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
106 return 2;
107 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800108 catch (const security::Tpm::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800109 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
110 return 2;
111 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800112 catch (const security::v2::KeyChain::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800113 std::cerr << "ERROR: " << e.what() << std::endl;
114 return 2;
115 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800116
117 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800118}
119
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800120} // namespace ndnsec
121} // namespace ndn