Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [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 | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "ndnsec.hpp" |
| 23 | #include "util.hpp" |
| 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 | |
| 33 | int |
| 34 | ndnsec_import(int argc, char** argv) |
| 35 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 36 | namespace po = boost::program_options; |
| 37 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 38 | std::string input; |
| 39 | std::string password; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | |
Davide Pesavento | 78338c5 | 2020-04-20 23:00:02 -0400 | [diff] [blame] | 41 | // ASan reports a stack-use-after-scope on armhf when |
| 42 | // BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 43 | BOOST_SCOPE_EXIT(&password) { |
| 44 | OPENSSL_cleanse(&password.front(), password.size()); |
| 45 | } BOOST_SCOPE_EXIT_END |
| 46 | |
| 47 | po::options_description description( |
| 48 | "Usage: ndnsec import [-h] [-P PASSPHRASE] [-i] FILE\n" |
| 49 | "\n" |
| 50 | "Options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 51 | description.add_options() |
| 52 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 53 | ("input,i", po::value<std::string>(&input)->default_value("-"), |
| 54 | "input file, '-' for stdin (the default)") |
| 55 | ("password,P", po::value<std::string>(&password), |
| 56 | "passphrase, will prompt if empty or not specified") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 57 | ; |
| 58 | |
| 59 | po::positional_options_description p; |
| 60 | p.add("input", 1); |
| 61 | |
| 62 | po::variables_map vm; |
| 63 | try { |
| 64 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 65 | po::notify(vm); |
| 66 | } |
| 67 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 68 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 69 | << description << std::endl; |
| 70 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 73 | if (vm.count("help") > 0) { |
| 74 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 78 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 80 | shared_ptr<security::SafeBag> safeBag; |
| 81 | if (input == "-") |
| 82 | safeBag = io::load<security::SafeBag>(std::cin); |
| 83 | else |
| 84 | safeBag = io::load<security::SafeBag>(input); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 85 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 86 | if (password.empty()) { |
| 87 | int count = 3; |
| 88 | while (!getPassword(password, "Passphrase for the private key: ", false)) { |
| 89 | count--; |
| 90 | if (count <= 0) { |
| 91 | std::cerr << "ERROR: invalid password" << std::endl; |
| 92 | return 1; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 93 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 95 | } |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 96 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 97 | keyChain.importSafeBag(*safeBag, password.data(), password.size()); |
| 98 | |
| 99 | return 0; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace ndnsec |
| 103 | } // namespace ndn |