Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 | { |
| 31 | using namespace ndn; |
| 32 | namespace po = boost::program_options; |
| 33 | |
| 34 | std::string input("-"); |
| 35 | std::string importPassword; |
| 36 | bool isPrivateImport = false; |
| 37 | |
| 38 | po::options_description description("General Usage\n ndnsec import [-h] [-p] input \nGeneral options"); |
| 39 | description.add_options() |
| 40 | ("help,h", "produce help message") |
| 41 | ("private,p", "import info contains private key") |
| 42 | ("input,i", po::value<std::string>(&input), "input source, stdin if -") |
| 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 | |
| 64 | if (vm.count("private") != 0) |
| 65 | isPrivateImport = true; |
| 66 | |
| 67 | if (!isPrivateImport) { |
| 68 | std::cerr << "You are trying to import certificate!\n" |
| 69 | << "Please use ndnsec cert-install!" << std::endl; |
| 70 | return 1; |
| 71 | } |
| 72 | else { |
| 73 | try { |
| 74 | security::v1::KeyChain keyChain; |
| 75 | |
| 76 | shared_ptr<security::v1::SecuredBag> securedBag; |
| 77 | if (input == "-") |
| 78 | securedBag = io::load<security::v1::SecuredBag>(std::cin); |
| 79 | else |
| 80 | securedBag = io::load<security::v1::SecuredBag>(input); |
| 81 | |
| 82 | int count = 3; |
| 83 | while (!getPassword(importPassword, "Passphrase for the private key: ")) { |
| 84 | count--; |
| 85 | if (count <= 0) { |
| 86 | std::cerr << "ERROR: Fail to get password" << std::endl; |
| 87 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 88 | return 1; |
| 89 | } |
| 90 | } |
| 91 | keyChain.importIdentity(*securedBag, importPassword); |
| 92 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 93 | } |
| 94 | catch (const std::runtime_error& e) { |
| 95 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 96 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } // namespace ndnsec |
| 105 | } // namespace ndn |