blob: 2b6d528c365b9ea54cda4f067673ae128732161c [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 Pesavento97ee8112020-08-11 21:38:34 -040027#include "ndn-cxx/util/scope.hpp"
Davide Pesaventob310efb2019-04-11 22:10:24 -040028
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029namespace ndn {
30namespace ndnsec {
31
Yingdi Yub61f5402014-02-26 17:46:11 -080032int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033ndnsec_export(int argc, char** argv)
34{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 namespace po = boost::program_options;
36
Junxiao Shibc2e78e2020-05-20 15:01:08 -060037 Name name;
38 bool isIdentityName = false;
39 bool isKeyName = false;
40 bool isCertName = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 std::string output;
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 std::string password;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043
Davide Pesavento97ee8112020-08-11 21:38:34 -040044 auto guard = make_scope_exit([&password] {
Davide Pesaventob310efb2019-04-11 22:10:24 -040045 OPENSSL_cleanse(&password.front(), password.size());
Davide Pesavento97ee8112020-08-11 21:38:34 -040046 });
Davide Pesaventob310efb2019-04-11 22:10:24 -040047
Junxiao Shibc2e78e2020-05-20 15:01:08 -060048 po::options_description visibleOptDesc(
49 "Usage: ndnsec export [-h] [-o FILE] [-P PASSPHRASE] [-i|-k|-c] NAME\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040050 "\n"
51 "Options");
Junxiao Shibc2e78e2020-05-20 15:01:08 -060052 visibleOptDesc.add_options()
Davide Pesaventob310efb2019-04-11 22:10:24 -040053 ("help,h", "produce help message")
Junxiao Shibc2e78e2020-05-20 15:01:08 -060054 ("identity,i", po::bool_switch(&isIdentityName),
55 "treat the NAME argument as an identity name (e.g., /ndn/edu/ucla/alice)")
56 ("key,k", po::bool_switch(&isKeyName),
57 "treat the NAME argument as a key name (e.g., /ndn/edu/ucla/alice/KEY/1%5D%A7g%90%B2%CF%AA)")
58 ("cert,c", po::bool_switch(&isCertName),
59 "treat the NAME argument as a certificate name "
60 "(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 -040061 ("output,o", po::value<std::string>(&output)->default_value("-"),
62 "output file, '-' for stdout (the default)")
63 ("password,P", po::value<std::string>(&password),
64 "passphrase, will prompt if empty or not specified")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 ;
66
Junxiao Shibc2e78e2020-05-20 15:01:08 -060067 po::options_description hiddenOptDesc;
68 hiddenOptDesc.add_options()
69 ("name", po::value<Name>(&name));
70
71 po::options_description optDesc;
72 optDesc.add(visibleOptDesc).add(hiddenOptDesc);
73
74 po::positional_options_description optPos;
75 optPos.add("name", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076
77 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070078 try {
Junxiao Shibc2e78e2020-05-20 15:01:08 -060079 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070080 po::notify(vm);
81 }
82 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 std::cerr << "ERROR: " << e.what() << "\n\n"
Junxiao Shibc2e78e2020-05-20 15:01:08 -060084 << visibleOptDesc << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040085 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070086 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087
Davide Pesaventob310efb2019-04-11 22:10:24 -040088 if (vm.count("help") > 0) {
Junxiao Shibc2e78e2020-05-20 15:01:08 -060089 std::cout << visibleOptDesc << std::endl;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070090 return 0;
91 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092
Junxiao Shibc2e78e2020-05-20 15:01:08 -060093 if (vm.count("name") == 0) {
94 std::cerr << "ERROR: you must specify a name" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040095 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070096 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080097
Junxiao Shibc2e78e2020-05-20 15:01:08 -060098 int nIsNameOptions = isIdentityName + isKeyName + isCertName;
99 if (nIsNameOptions > 1) {
100 std::cerr << "ERROR: at most one of '--identity', '--key', "
101 "or '--cert' may be specified" << std::endl;
102 return 2;
103 }
104 else if (nIsNameOptions == 0) {
105 isIdentityName = true;
106 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800107
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600108 security::v2::KeyChain keyChain;
109 auto cert = getCertificateFromPib(keyChain.getPib(), name,
110 isIdentityName, isKeyName, isCertName);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400111
112 if (password.empty()) {
113 int count = 3;
114 while (!getPassword(password, "Passphrase for the private key: ")) {
115 count--;
116 if (count <= 0) {
117 std::cerr << "ERROR: invalid password" << std::endl;
118 return 1;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800119 }
120 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700121 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400122
Junxiao Shibc2e78e2020-05-20 15:01:08 -0600123 auto safeBag = keyChain.exportSafeBag(cert, password.data(), password.size());
Davide Pesaventob310efb2019-04-11 22:10:24 -0400124
125 if (output == "-")
126 io::save(*safeBag, std::cout);
127 else
128 io::save(*safeBag, output);
129
130 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800131}
132
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800133} // namespace ndnsec
134} // namespace ndn