Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/util/logger.hpp" |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/util/logging.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/version.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 28 | #include <boost/exception/diagnostic_information.hpp> |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 29 | #include <filesystem> |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 30 | #include <iostream> |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 31 | |
| 32 | NDN_LOG_INIT(ndnsec); |
| 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | constexpr std::string_view NDNSEC_HELP_TEXT = R"STR(Usage: ndnsec COMMAND [OPTION]... |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 35 | |
| 36 | Available commands: |
| 37 | help Print this help text |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 38 | list List all known identities, keys, and certificates |
| 39 | get-default Show the default identity, key, or certificate |
| 40 | set-default Change the default identity, key, or certificate |
| 41 | delete Delete an identity, key, or certificate |
| 42 | key-gen Generate a key pair for an identity |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 43 | sign-req Generate a certificate signing request |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 44 | cert-gen Issue a certificate for an identity |
| 45 | cert-dump Display a certificate |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 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 Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 49 | version Print version information |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 50 | |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 51 | Run 'ndnsec COMMAND --help' for more information on a command. |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 52 | )STR"; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 54 | int |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 55 | main(int argc, char* argv[]) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 57 | std::string basename = std::filesystem::path(argv[0]).filename(); |
Davide Pesavento | f3089c3 | 2021-05-25 21:28:21 -0400 | [diff] [blame] | 58 | std::string command; |
| 59 | if (basename.rfind("ndnsec-", 0) == 0) { |
| 60 | command = basename.substr(std::strlen("ndnsec-")); |
| 61 | } |
| 62 | else if (argc >= 2) { |
| 63 | command = argv[1]; |
| 64 | argc--; |
| 65 | argv++; |
| 66 | } |
| 67 | else { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 68 | std::cerr << NDNSEC_HELP_TEXT; |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 69 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 70 | } |
Davide Pesavento | 97157b4 | 2021-05-27 21:00:43 -0400 | [diff] [blame] | 71 | |
Davide Pesavento | f3089c3 | 2021-05-25 21:28:21 -0400 | [diff] [blame] | 72 | NDN_LOG_TRACE("Command: " << command); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 74 | try { |
Davide Pesavento | f3089c3 | 2021-05-25 21:28:21 -0400 | [diff] [blame] | 75 | using namespace ndn::ndnsec; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 76 | if (command == "help") { std::cout << NDNSEC_HELP_TEXT; } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 77 | else if (command == "version") { std::cout << NDN_CXX_VERSION_BUILD_STRING << std::endl; } |
Davide Pesavento | f3089c3 | 2021-05-25 21:28:21 -0400 | [diff] [blame] | 78 | else if (command == "list") { return ndnsec_list(argc, argv); } |
| 79 | else if (command == "get-default") { return ndnsec_get_default(argc, argv); } |
| 80 | else if (command == "set-default") { return ndnsec_set_default(argc, argv); } |
| 81 | else if (command == "delete") { return ndnsec_delete(argc, argv); } |
| 82 | else if (command == "key-gen") { return ndnsec_key_gen(argc, argv); } |
| 83 | else if (command == "sign-req") { return ndnsec_sign_req(argc, argv); } |
| 84 | else if (command == "cert-gen") { return ndnsec_cert_gen(argc, argv); } |
| 85 | else if (command == "cert-dump") { return ndnsec_cert_dump(argc, argv); } |
| 86 | else if (command == "cert-install") { return ndnsec_cert_install(argc, argv); } |
| 87 | else if (command == "export") { return ndnsec_export(argc, argv); } |
| 88 | else if (command == "import") { return ndnsec_import(argc, argv); } |
| 89 | else if (command == "unlock-tpm") { return ndnsec_unlock_tpm(argc, argv); } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 90 | else { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 91 | std::cerr << "ERROR: Unknown command '" << command << "'\n\n" << NDNSEC_HELP_TEXT; |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 92 | return 2; |
Yingdi Yu | f8fc8de | 2014-02-25 15:45:39 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 95 | catch (const std::exception& e) { |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 96 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 97 | NDN_LOG_ERROR(boost::diagnostic_information(e)); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 98 | ndn::util::Logging::flush(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 99 | return 1; |
| 100 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |