blob: 4429dd4e4b7fc9136ebfb19e108c5c5f425a0855 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi476200b2017-10-05 12:16:27 +00002/*
Davide Pesavento78338c52020-04-20 23:00:02 -04003 * Copyright (c) 2013-2020 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 Pesaventob310efb2019-04-11 22:10:24 -040025#include "ndn-cxx/security/impl/openssl.hpp"
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040026#include "ndn-cxx/util/io.hpp"
Davide Pesaventob310efb2019-04-11 22:10:24 -040027
28#include <boost/scope_exit.hpp>
29
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030namespace ndn {
31namespace ndnsec {
32
Yingdi Yub61f5402014-02-26 17:46:11 -080033int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034ndnsec_export(int argc, char** argv)
35{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 namespace po = boost::program_options;
37
Alexander Afanasyev35109a12017-01-04 15:39:06 -080038 Name identityName;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 std::string output;
Davide Pesaventob310efb2019-04-11 22:10:24 -040040 std::string password;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041
Davide Pesavento78338c52020-04-20 23:00:02 -040042 // ASan reports a stack-use-after-scope on armhf when
43 // BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
Davide Pesaventob310efb2019-04-11 22:10:24 -040044 BOOST_SCOPE_EXIT(&password) {
45 OPENSSL_cleanse(&password.front(), password.size());
46 } BOOST_SCOPE_EXIT_END
47
48 po::options_description description(
49 "Usage: ndnsec export [-h] [-o FILE] [-P PASSPHRASE] [-i] IDENTITY\n"
50 "\n"
51 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080052 description.add_options()
Davide Pesaventob310efb2019-04-11 22:10:24 -040053 ("help,h", "produce help message")
54 ("identity,i", po::value<Name>(&identityName), "name of the identity to export")
55 ("output,o", po::value<std::string>(&output)->default_value("-"),
56 "output file, '-' for stdout (the default)")
57 ("password,P", po::value<std::string>(&password),
58 "passphrase, will prompt if empty or not specified")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 ;
60
61 po::positional_options_description p;
62 p.add("identity", 1);
63
64 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070065 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080066 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070067 po::notify(vm);
68 }
69 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040070 std::cerr << "ERROR: " << e.what() << "\n\n"
71 << description << std::endl;
72 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070073 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074
Davide Pesaventob310efb2019-04-11 22:10:24 -040075 if (vm.count("help") > 0) {
76 std::cout << description << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070077 return 0;
78 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070080 if (vm.count("identity") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040081 std::cerr << "ERROR: you must specify an identity" << std::endl;
82 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070083 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080084
Davide Pesaventob310efb2019-04-11 22:10:24 -040085 security::v2::KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086
Davide Pesaventob310efb2019-04-11 22:10:24 -040087 auto id = keyChain.getPib().getIdentity(identityName);
88
89 if (password.empty()) {
90 int count = 3;
91 while (!getPassword(password, "Passphrase for the private key: ")) {
92 count--;
93 if (count <= 0) {
94 std::cerr << "ERROR: invalid password" << std::endl;
95 return 1;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096 }
97 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070098 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040099
100 // TODO: export all certificates, selected key pair, selected certificate
101 auto safeBag = keyChain.exportSafeBag(id.getDefaultKey().getDefaultCertificate(),
102 password.data(), password.size());
103
104 if (output == "-")
105 io::save(*safeBag, std::cout);
106 else
107 io::save(*safeBag, output);
108
109 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800110}
111
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800112} // namespace ndnsec
113} // namespace ndn