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