blob: 25de41cb1bb682fd0cece69d4b14696cb9089e0d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob310efb2019-04-11 22:10:24 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040025namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080026
Yingdi Yub61f5402014-02-26 17:46:11 -080027int
28ndnsec_delete(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030 namespace po = boost::program_options;
31
Davide Pesaventob310efb2019-04-11 22:10:24 -040032 bool wantDeleteKey = false;
33 bool wantDeleteCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 std::string name;
35
Davide Pesaventob310efb2019-04-11 22:10:24 -040036 po::options_description description(
37 "Usage: ndnsec delete [-h] [-k|-c] [-n] NAME\n"
38 "\n"
39 "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")
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 ("delete-key,k", po::bool_switch(&wantDeleteKey), "delete a key")
43 ("delete-cert,c", po::bool_switch(&wantDeleteCert), "delete a certificate")
44 ("name,n", po::value<std::string>(&name),
45 "name of the item to delete. By default, it refers to an identity. "
46 "If -k is specified, it refers to a key. "
Davide Pesaventof2cae612021-03-24 18:47:05 -040047 "If -c is specified, it refers to a certificate.")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048 ;
49
50 po::positional_options_description p;
51 p.add("name", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080052
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 po::variables_map vm;
Yingdi Yu6147ef42014-12-08 17:48:32 -080054 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080055 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Yingdi Yu6147ef42014-12-08 17:48:32 -080056 po::notify(vm);
57 }
58 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040059 std::cerr << "ERROR: " << e.what() << "\n\n"
60 << description << std::endl;
Yingdi Yu6147ef42014-12-08 17:48:32 -080061 return 2;
62 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063
Davide Pesaventob310efb2019-04-11 22:10:24 -040064 if (vm.count("help") > 0) {
65 std::cout << description << std::endl;
Yingdi Yu6147ef42014-12-08 17:48:32 -080066 return 0;
67 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068
Yingdi Yu6147ef42014-12-08 17:48:32 -080069 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040070 std::cerr << "ERROR: you must specify a name" << std::endl;
Yingdi Yu6147ef42014-12-08 17:48:32 -080071 return 2;
72 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073
Davide Pesaventob310efb2019-04-11 22:10:24 -040074 if (wantDeleteKey && wantDeleteCert) {
75 std::cerr << "ERROR: cannot specify both '--delete-key' and '--delete-cert'" << std::endl;
76 return 2;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080077 }
Yingdi Yub61f5402014-02-26 17:46:11 -080078
Davide Pesaventof2cae612021-03-24 18:47:05 -040079 KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080080
Yingdi Yu6147ef42014-12-08 17:48:32 -080081 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -040082 if (wantDeleteCert) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080083 security::Key key = keyChain.getPib()
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 .getIdentity(security::extractIdentityFromCertName(name))
85 .getKey(security::extractKeyNameFromCertName(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -080086
Alexander Afanasyev35109a12017-01-04 15:39:06 -080087 keyChain.deleteCertificate(key, key.getCertificate(name).getName());
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 std::cerr << "OK: certificate deleted: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080089 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040090 else if (wantDeleteKey) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080091 security::Identity identity = keyChain.getPib()
Davide Pesaventof2cae612021-03-24 18:47:05 -040092 .getIdentity(security::extractIdentityFromKeyName(name));
Yingdi Yu6147ef42014-12-08 17:48:32 -080093
Alexander Afanasyev35109a12017-01-04 15:39:06 -080094 keyChain.deleteKey(identity, identity.getKey(name));
Davide Pesaventob310efb2019-04-11 22:10:24 -040095 std::cerr << "OK: key deleted: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080096 }
Yingdi Yu6147ef42014-12-08 17:48:32 -080097 else {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080098 keyChain.deleteIdentity(keyChain.getPib().getIdentity(name));
Davide Pesaventob310efb2019-04-11 22:10:24 -040099 std::cerr << "OK: identity deleted: " << name << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800100 }
Yingdi Yu6147ef42014-12-08 17:48:32 -0800101 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800102 catch (const security::Pib::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800103 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400104 return 1;
Yingdi Yu6147ef42014-12-08 17:48:32 -0800105 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800106 catch (const security::Tpm::Error& e) {
Yingdi Yu6147ef42014-12-08 17:48:32 -0800107 std::cerr << "ERROR: Cannot delete the item: " << e.what() << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400108 return 1;
Yingdi Yu6147ef42014-12-08 17:48:32 -0800109 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800110
111 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112}
113
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400114} // namespace ndn::ndnsec