blob: d2bc2811bfe8b0ed9ecf7822273eaac540381015 [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/*
3 * Copyright (c) 2013-2018 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"
23#include "util.hpp"
Davide Pesavento19442812018-11-23 14:00:04 -050024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/util/logger.hpp"
26#include "ndn-cxx/version.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027
Alexander Afanasyev35109a12017-01-04 15:39:06 -080028#include <boost/exception/get_error_info.hpp>
29
30NDN_LOG_INIT(ndnsec);
31
32std::string ndnsec_helper = R"STR(\
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080033 help Show all commands
34 version Show version and exit
35 list Display information in PublicInfo
36 get-default Get default setting info
37 set-default Configure default setting
38 key-gen Generate a Key-Signing-Key for an identity
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039 sign-req Generate a certificate signing request
40 cert-gen Generate an identity certificate
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080041 cert-dump Dump a certificate from PublicInfo
42 cert-install Install a certificate into PublicInfo
43 delete Delete identity/key/certificate
44 export Export an identity package
45 import Import an identity package
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080046 unlock-tpm Unlock Tpm
47)STR";
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048
Yingdi Yu05842f22014-04-15 19:21:56 -070049int
Yingdi Yu5c1f8412014-03-25 11:49:47 -070050main(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080052 if (argc < 2) {
53 std::cerr << ndnsec_helper << std::endl;
54 return 1;
55 }
56
57 using namespace ndn::ndnsec;
58
59 std::string command(argv[1]);
60 try {
61 if (command == "help") { std::cout << ndnsec_helper << std::endl; }
62 else if (command == "version") { std::cout << NDN_CXX_VERSION_BUILD_STRING << std::endl; }
63 else if (command == "list") { return ndnsec_list(argc - 1, argv + 1); }
64 else if (command == "get-default") { return ndnsec_get_default(argc - 1, argv + 1); }
65 else if (command == "set-default") { return ndnsec_set_default(argc - 1, argv + 1); }
66 else if (command == "key-gen") { return ndnsec_key_gen(argc - 1, argv + 1); }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080067 else if (command == "sign-req") { return ndnsec_sign_req(argc - 1, argv + 1); }
68 else if (command == "cert-gen") { return ndnsec_cert_gen(argc - 1, argv + 1); }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080069 else if (command == "cert-dump") { return ndnsec_cert_dump(argc - 1, argv + 1); }
70 else if (command == "cert-install") { return ndnsec_cert_install(argc - 1, argv + 1); }
71 else if (command == "delete") { return ndnsec_delete(argc - 1, argv + 1); }
72 else if (command == "export") { return ndnsec_export(argc - 1, argv + 1); }
73 else if (command == "import") { return ndnsec_import(argc - 1, argv + 1); }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 else if (command == "unlock-tpm") { return ndnsec_unlock_tpm(argc - 1, argv + 1); }
75 else {
Yingdi Yuf8fc8de2014-02-25 15:45:39 -080076 std::cerr << ndnsec_helper << std::endl;
77 return 1;
78 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080079 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080080 catch (const std::exception& e) {
81
82 std::cerr << "ERROR: " << e.what();
83
84 std::ostringstream extendedError;
85 const char* const* file = boost::get_error_info<boost::throw_file>(e);
86 const int* line = boost::get_error_info<boost::throw_line>(e);
87 const char* const* func = boost::get_error_info<boost::throw_function>(e);
88 if (file && line) {
89 extendedError << " [from " << *file << ":" << *line;
90 if (func) {
91 extendedError << " in " << *func;
92 }
93 extendedError << "]";
94 }
95 NDN_LOG_ERROR(e.what() << extendedError.str());
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080096 return 1;
97 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098
99 return 0;
100}