Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 476200b | 2017-10-05 12:16:27 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/impl/openssl.hpp" |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 26 | #include "ndn-cxx/util/io.hpp" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 27 | |
| 28 | #include <boost/scope_exit.hpp> |
| 29 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndnsec { |
| 32 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 33 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | ndnsec_export(int argc, char** argv) |
| 35 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 36 | namespace po = boost::program_options; |
| 37 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 38 | Name name; |
| 39 | bool isIdentityName = false; |
| 40 | bool isKeyName = false; |
| 41 | bool isCertName = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | std::string output; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 43 | std::string password; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 45 | // ASan reports a stack-use-after-scope on armhf when |
| 46 | // BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 47 | BOOST_SCOPE_EXIT(&password) { |
| 48 | OPENSSL_cleanse(&password.front(), password.size()); |
| 49 | } BOOST_SCOPE_EXIT_END |
| 50 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 51 | po::options_description visibleOptDesc( |
| 52 | "Usage: ndnsec export [-h] [-o FILE] [-P PASSPHRASE] [-i|-k|-c] NAME\n" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 53 | "\n" |
| 54 | "Options"); |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 55 | visibleOptDesc.add_options() |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 56 | ("help,h", "produce help message") |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 57 | ("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 Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 64 | ("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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | ; |
| 69 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 70 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 79 | |
| 80 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 81 | try { |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 82 | po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), vm); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 83 | po::notify(vm); |
| 84 | } |
| 85 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 86 | std::cerr << "ERROR: " << e.what() << "\n\n" |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 87 | << visibleOptDesc << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 88 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 89 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 90 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 91 | if (vm.count("help") > 0) { |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 92 | std::cout << visibleOptDesc << std::endl; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 95 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 96 | if (vm.count("name") == 0) { |
| 97 | std::cerr << "ERROR: you must specify a name" << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 98 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 99 | } |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 100 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 101 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 110 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 111 | security::v2::KeyChain keyChain; |
| 112 | auto cert = getCertificateFromPib(keyChain.getPib(), name, |
| 113 | isIdentityName, isKeyName, isCertName); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 114 | |
| 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 Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 124 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 125 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 126 | auto safeBag = keyChain.exportSafeBag(cert, password.data(), password.size()); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 127 | |
| 128 | if (output == "-") |
| 129 | io::save(*safeBag, std::cout); |
| 130 | else |
| 131 | io::save(*safeBag, output); |
| 132 | |
| 133 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 136 | } // namespace ndnsec |
| 137 | } // namespace ndn |