blob: 4bb9b6379a37f2df7cbd9694b574f9edc6cbec06 [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- 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
25namespace ndn {
26namespace ndnsec {
27
28int
29ndnsec_import(int argc, char** argv)
30{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031 namespace po = boost::program_options;
32
33 std::string input("-");
34 std::string importPassword;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035
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 Afanasyev82c359c2017-01-04 14:48:07 -080039 ("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 Afanasyev35109a12017-01-04 15:39:06 -080061 try {
62 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063
Alexander Afanasyev35109a12017-01-04 15:39:06 -080064 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 Afanasyev82c359c2017-01-04 14:48:07 -080069
Alexander Afanasyev35109a12017-01-04 15:39:06 -080070 int count = 3;
71 while (!getPassword(importPassword, "Passphrase for the private key: ")) {
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 Afanasyev82c359c2017-01-04 14:48:07 -080077 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080079 keyChain.importSafeBag(*safeBag, importPassword.c_str(), importPassword.size());
80 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080081 return 0;
82 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080083 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 Afanasyev82c359c2017-01-04 14:48:07 -080088}
89
90} // namespace ndnsec
91} // namespace ndn