blob: 79bea3d25c4b6ae61c62eef5322cec616c9ae49c [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
Junxiao Shibc2e78e2020-05-20 15:01:08 -060038 Name name;
39 bool isIdentityName = false;
40 bool isKeyName = false;
41 bool isCertName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 std::string output;
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 std::string password;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044
Davide Pesavento78338c52020-04-20 23:00:02 -040045 // ASan reports a stack-use-after-scope on armhf when
46 // BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
Davide Pesaventob310efb2019-04-11 22:10:24 -040047 BOOST_SCOPE_EXIT(&password) {
48 OPENSSL_cleanse(&password.front(), password.size());
49 } BOOST_SCOPE_EXIT_END
50
Junxiao Shibc2e78e2020-05-20 15:01:08 -060051 po::options_description visibleOptDesc(
52 "Usage: ndnsec export [-h] [-o FILE] [-P PASSPHRASE] [-i|-k|-c] NAME\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040053 "\n"
54 "Options");
Junxiao Shibc2e78e2020-05-20 15:01:08 -060055 visibleOptDesc.add_options()
Davide Pesaventob310efb2019-04-11 22:10:24 -040056 ("help,h", "produce help message")
Junxiao Shibc2e78e2020-05-20 15:01:08 -060057 ("identity,i", po::bool_switch(&isIdentityName),
58 "treat the NAME argument as an identity name (e.g., /ndn/edu/ucla/alice)")
59 ("key,k", po::bool_switch(&isKeyName),
60 "treat the NAME argument as a key name (e.g., /ndn/edu/ucla/alice/KEY/1%5D%A7g%90%B2%CF%AA)")
61 ("cert,c", po::bool_switch(&isCertName),
62 "treat the NAME argument as a certificate name "
63 "(e.g., /ndn/edu/ucla/alice/KEY/1%5D%A7g%90%B2%CF%AA/self/%FD%00%00%01r-%D3%DC%2A)")
Davide Pesaventob310efb2019-04-11 22:10:24 -040064 ("output,o", po::value<std::string>(&output)->default_value("-"),
65 "output file, '-' for stdout (the default)")
66 ("password,P", po::value<std::string>(&password),
67 "passphrase, will prompt if empty or not specified")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 ;
69
Junxiao Shibc2e78e2020-05-20 15:01:08 -060070 po::options_description hiddenOptDesc;
71 hiddenOptDesc.add_options()
72 ("name", po::value<Name>(&name));
73
74 po::options_description optDesc;
75 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
76
77 po::positional_options_description optPos;
78 optPos.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079
80 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070081 try {
Junxiao Shibc2e78e2020-05-20 15:01:08 -060082 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070083 po::notify(vm);
84 }
85 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040086 std::cerr << "ERROR: " << e.what() << "\n\n"
Junxiao Shibc2e78e2020-05-20 15:01:08 -060087 << visibleOptDesc << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070089 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080090
Davide Pesaventob310efb2019-04-11 22:10:24 -040091 if (vm.count("help") > 0) {
Junxiao Shibc2e78e2020-05-20 15:01:08 -060092 std::cout << visibleOptDesc << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070093 return 0;
94 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080095
Junxiao Shibc2e78e2020-05-20 15:01:08 -060096 if (vm.count("name") == 0) {
97 std::cerr << "ERROR: you must specify a name" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040098 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070099 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800100
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600101 int nIsNameOptions = isIdentityName + isKeyName + isCertName;
102 if (nIsNameOptions > 1) {
103 std::cerr << "ERROR: at most one of '--identity', '--key', "
104 "or '--cert' may be specified" << std::endl;
105 return 2;
106 }
107 else if (nIsNameOptions == 0) {
108 isIdentityName = true;
109 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800110
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600111 security::v2::KeyChain keyChain;
112 auto cert = getCertificateFromPib(keyChain.getPib(), name,
113 isIdentityName, isKeyName, isCertName);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400114
115 if (password.empty()) {
116 int count = 3;
117 while (!getPassword(password, "Passphrase for the private key: ")) {
118 count--;
119 if (count <= 0) {
120 std::cerr << "ERROR: invalid password" << std::endl;
121 return 1;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800122 }
123 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700124 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400125
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600126 auto safeBag = keyChain.exportSafeBag(cert, password.data(), password.size());
Davide Pesaventob310efb2019-04-11 22:10:24 -0400127
128 if (output == "-")
129 io::save(*safeBag, std::cout);
130 else
131 io::save(*safeBag, output);
132
133 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800134}
135
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800136} // namespace ndnsec
137} // namespace ndn