blob: b0735e3a4a20d5adbf20df04e0f354b7b8f03a63 [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev22ee0892017-09-02 12:29:16 -04002/*
Alexander Afanasyev634a62b2018-06-15 16:55:26 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08004 *
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
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040036 po::options_description description("General Usage\n"
37 " ndnsec import [-h] [-P passphrase] input \n"
38 "General options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039 description.add_options()
40 ("help,h", "produce help message")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080041 ("input,i", po::value<std::string>(&input), "input source, stdin if -")
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040042 ("password,P", po::value<std::string>(&importPassword), "Passphrase (will prompt if empty or not specified)")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080043 ;
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 Afanasyev35109a12017-01-04 15:39:06 -080064 try {
65 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080066
Alexander Afanasyev35109a12017-01-04 15:39:06 -080067 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 Afanasyev82c359c2017-01-04 14:48:07 -080072
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040073 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 Afanasyev82c359c2017-01-04 14:48:07 -080082 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080083 }
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040084
Alexander Afanasyev35109a12017-01-04 15:39:06 -080085 keyChain.importSafeBag(*safeBag, importPassword.c_str(), importPassword.size());
86 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080087 return 0;
88 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080089 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 Afanasyev82c359c2017-01-04 14:48:07 -080094}
95
96} // namespace ndnsec
97} // namespace ndn