blob: c58a6edc7baa7aaf6479c4182bd280f5efc6cf46 [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/*
Davide Pesavento78338c52020-04-20 23:00:02 -04003 * Copyright (c) 2013-2020 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
Davide Pesaventob310efb2019-04-11 22:10:24 -040025#include "ndn-cxx/security/impl/openssl.hpp"
26
27#include <boost/scope_exit.hpp>
28
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029namespace ndn {
30namespace ndnsec {
31
32int
33ndnsec_import(int argc, char** argv)
34{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035 namespace po = boost::program_options;
36
Davide Pesaventob310efb2019-04-11 22:10:24 -040037 std::string input;
38 std::string password;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039
Davide Pesavento78338c52020-04-20 23:00:02 -040040 // ASan reports a stack-use-after-scope on armhf when
41 // BOOST_SCOPE_EXIT_ALL is used in place of BOOST_SCOPE_EXIT
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 BOOST_SCOPE_EXIT(&password) {
43 OPENSSL_cleanse(&password.front(), password.size());
44 } BOOST_SCOPE_EXIT_END
45
46 po::options_description description(
47 "Usage: ndnsec import [-h] [-P PASSPHRASE] [-i] FILE\n"
48 "\n"
49 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 description.add_options()
51 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040052 ("input,i", po::value<std::string>(&input)->default_value("-"),
53 "input file, '-' for stdin (the default)")
54 ("password,P", po::value<std::string>(&password),
55 "passphrase, will prompt if empty or not specified")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080056 ;
57
58 po::positional_options_description p;
59 p.add("input", 1);
60
61 po::variables_map vm;
62 try {
63 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
64 po::notify(vm);
65 }
66 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 std::cerr << "ERROR: " << e.what() << "\n\n"
68 << description << std::endl;
69 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080070 }
71
Davide Pesaventob310efb2019-04-11 22:10:24 -040072 if (vm.count("help") > 0) {
73 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 return 0;
75 }
76
Davide Pesaventob310efb2019-04-11 22:10:24 -040077 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080078
Davide Pesaventob310efb2019-04-11 22:10:24 -040079 shared_ptr<security::SafeBag> safeBag;
80 if (input == "-")
81 safeBag = io::load<security::SafeBag>(std::cin);
82 else
83 safeBag = io::load<security::SafeBag>(input);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084
Davide Pesaventob310efb2019-04-11 22:10:24 -040085 if (password.empty()) {
86 int count = 3;
87 while (!getPassword(password, "Passphrase for the private key: ", false)) {
88 count--;
89 if (count <= 0) {
90 std::cerr << "ERROR: invalid password" << std::endl;
91 return 1;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080093 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 }
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040095
Davide Pesaventob310efb2019-04-11 22:10:24 -040096 keyChain.importSafeBag(*safeBag, password.data(), password.size());
97
98 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080099}
100
101} // namespace ndnsec
102} // namespace ndn