blob: aa6ce370d986d6f87a2b6928ed4c970c910de7b3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento19442812018-11-23 14:00:04 -05002/*
Davide Pesavento9b911e92024-06-23 13:30:30 -04003 * Copyright (c) 2013-2024 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"
Davide Pesavento19442812018-11-23 14:00:04 -050023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/util/logger.hpp"
Davide Pesavento949075a2021-10-17 22:07:07 -040025#include "ndn-cxx/util/logging.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "ndn-cxx/version.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027
Davide Pesaventofa995ac2019-03-27 23:44:46 -040028#include <boost/exception/diagnostic_information.hpp>
Davide Pesaventof3089c32021-05-25 21:28:21 -040029#include <boost/filesystem/path.hpp>
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040030#include <iostream>
Alexander Afanasyev35109a12017-01-04 15:39:06 -080031
32NDN_LOG_INIT(ndnsec);
33
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034constexpr std::string_view NDNSEC_HELP_TEXT = R"STR(Usage: ndnsec COMMAND [OPTION]...
Davide Pesaventofa995ac2019-03-27 23:44:46 -040035
36Available commands:
37 help Print this help text
Davide Pesaventofa995ac2019-03-27 23:44:46 -040038 list List all known identities/keys/certificates
39 get-default Show the default identity/key/certificate
40 set-default Change the default identity/key/certificate
41 delete Delete an identity/key/certificate
42 key-gen Generate a key for an identity
43 sign-req Generate a certificate signing request
44 cert-gen Create a certificate for an identity
45 cert-dump Export a certificate
46 cert-install Import a certificate from a file
47 export Export an identity as a SafeBag
48 import Import an identity from a SafeBag
Davide Pesavento9b911e92024-06-23 13:30:30 -040049 version Print version information
Davide Pesaventofa995ac2019-03-27 23:44:46 -040050
Davide Pesavento9b911e92024-06-23 13:30:30 -040051Run 'ndnsec COMMAND --help' for more information on a command.
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040052)STR";
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053
Yingdi Yu05842f22014-04-15 19:21:56 -070054int
Davide Pesaventofa995ac2019-03-27 23:44:46 -040055main(int argc, char* argv[])
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056{
Davide Pesaventof3089c32021-05-25 21:28:21 -040057 boost::filesystem::path p(argv[0]);
58 std::string basename(p.filename().string());
59 std::string command;
60 if (basename.rfind("ndnsec-", 0) == 0) {
61 command = basename.substr(std::strlen("ndnsec-"));
62 }
63 else if (argc >= 2) {
64 command = argv[1];
65 argc--;
66 argv++;
67 }
68 else {
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040069 std::cerr << NDNSEC_HELP_TEXT;
Davide Pesaventofa995ac2019-03-27 23:44:46 -040070 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080071 }
Davide Pesavento97157b42021-05-27 21:00:43 -040072
Davide Pesaventof3089c32021-05-25 21:28:21 -040073 NDN_LOG_TRACE("Command: " << command);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075 try {
Davide Pesaventof3089c32021-05-25 21:28:21 -040076 using namespace ndn::ndnsec;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040077 if (command == "help") { std::cout << NDNSEC_HELP_TEXT; }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078 else if (command == "version") { std::cout << NDN_CXX_VERSION_BUILD_STRING << std::endl; }
Davide Pesaventof3089c32021-05-25 21:28:21 -040079 else if (command == "list") { return ndnsec_list(argc, argv); }
80 else if (command == "get-default") { return ndnsec_get_default(argc, argv); }
81 else if (command == "set-default") { return ndnsec_set_default(argc, argv); }
82 else if (command == "delete") { return ndnsec_delete(argc, argv); }
83 else if (command == "key-gen") { return ndnsec_key_gen(argc, argv); }
84 else if (command == "sign-req") { return ndnsec_sign_req(argc, argv); }
85 else if (command == "cert-gen") { return ndnsec_cert_gen(argc, argv); }
86 else if (command == "cert-dump") { return ndnsec_cert_dump(argc, argv); }
87 else if (command == "cert-install") { return ndnsec_cert_install(argc, argv); }
88 else if (command == "export") { return ndnsec_export(argc, argv); }
89 else if (command == "import") { return ndnsec_import(argc, argv); }
90 else if (command == "unlock-tpm") { return ndnsec_unlock_tpm(argc, argv); }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080091 else {
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040092 std::cerr << "ERROR: Unknown command '" << command << "'\n\n" << NDNSEC_HELP_TEXT;
Davide Pesaventofa995ac2019-03-27 23:44:46 -040093 return 2;
Yingdi Yuf8fc8de2014-02-25 15:45:39 -080094 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080095 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096 catch (const std::exception& e) {
Davide Pesaventofa995ac2019-03-27 23:44:46 -040097 std::cerr << "ERROR: " << e.what() << std::endl;
98 NDN_LOG_ERROR(boost::diagnostic_information(e));
Davide Pesavento949075a2021-10-17 22:07:07 -040099 ndn::util::Logging::flush();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800100 return 1;
101 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102
103 return 0;
104}