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 | /* |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
| 28 | int |
| 29 | ndnsec_import(int argc, char** argv) |
| 30 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | namespace po = boost::program_options; |
| 32 | |
| 33 | std::string input("-"); |
| 34 | std::string importPassword; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 36 | po::options_description description("General Usage\n" |
| 37 | " ndnsec import [-h] [-P passphrase] input \n" |
| 38 | "General options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 39 | description.add_options() |
| 40 | ("help,h", "produce help message") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 41 | ("input,i", po::value<std::string>(&input), "input source, stdin if -") |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 42 | ("password,P", po::value<std::string>(&importPassword), "Passphrase (will prompt if empty or not specified)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 43 | ; |
| 44 | |
| 45 | po::positional_options_description p; |
| 46 | p.add("input", 1); |
| 47 | |
| 48 | po::variables_map vm; |
| 49 | try { |
| 50 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 51 | po::notify(vm); |
| 52 | } |
| 53 | catch (const std::exception& e) { |
| 54 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 55 | std::cerr << description << std::endl; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | if (vm.count("help") != 0) { |
| 60 | std::cerr << description << std::endl; |
| 61 | return 0; |
| 62 | } |
| 63 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 64 | try { |
| 65 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 67 | shared_ptr<security::SafeBag> safeBag; |
| 68 | if (input == "-") |
| 69 | safeBag = io::load<security::SafeBag>(std::cin); |
| 70 | else |
| 71 | safeBag = io::load<security::SafeBag>(input); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 73 | if (importPassword.empty()) { |
| 74 | int count = 3; |
| 75 | while (!getPassword(importPassword, "Passphrase for the private key: ", false)) { |
| 76 | count--; |
| 77 | if (count <= 0) { |
| 78 | std::cerr << "ERROR: Fail to get password" << std::endl; |
| 79 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 80 | return 1; |
| 81 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 83 | } |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 84 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 85 | keyChain.importSafeBag(*safeBag, importPassword.c_str(), importPassword.size()); |
| 86 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 87 | return 0; |
| 88 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 89 | catch (const std::runtime_error& e) { |
| 90 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 91 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 92 | return 1; |
| 93 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace ndnsec |
| 97 | } // namespace ndn |