blob: 9e0c830f068ed158a0647168b9af89398b136fab [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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 Pesavento97ee8112020-08-11 21:38:34 -040025#include "ndn-cxx/util/scope.hpp"
Davide Pesaventob310efb2019-04-11 22:10:24 -040026
Davide Pesavento80d671f2022-06-08 04:04:52 -040027#include <openssl/crypto.h>
28
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030
31int
32ndnsec_import(int argc, char** argv)
33{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080034 namespace po = boost::program_options;
35
Davide Pesaventob310efb2019-04-11 22:10:24 -040036 std::string input;
37 std::string password;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038
Davide Pesavento97ee8112020-08-11 21:38:34 -040039 auto guard = make_scope_exit([&password] {
Davide Pesaventob310efb2019-04-11 22:10:24 -040040 OPENSSL_cleanse(&password.front(), password.size());
Davide Pesavento97ee8112020-08-11 21:38:34 -040041 });
Davide Pesaventob310efb2019-04-11 22:10:24 -040042
43 po::options_description description(
44 "Usage: ndnsec import [-h] [-P PASSPHRASE] [-i] FILE\n"
45 "\n"
46 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080047 description.add_options()
48 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040049 ("input,i", po::value<std::string>(&input)->default_value("-"),
50 "input file, '-' for stdin (the default)")
51 ("password,P", po::value<std::string>(&password),
52 "passphrase, will prompt if empty or not specified")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080053 ;
54
55 po::positional_options_description p;
56 p.add("input", 1);
57
58 po::variables_map vm;
59 try {
60 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
61 po::notify(vm);
62 }
63 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040064 std::cerr << "ERROR: " << e.what() << "\n\n"
65 << description << std::endl;
66 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080067 }
68
Davide Pesaventob310efb2019-04-11 22:10:24 -040069 if (vm.count("help") > 0) {
70 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080071 return 0;
72 }
73
Davide Pesaventof2cae612021-03-24 18:47:05 -040074 KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075
Davide Pesavento949075a2021-10-17 22:07:07 -040076 auto safeBag = loadFromFile<security::SafeBag>(input);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080077
Davide Pesaventob310efb2019-04-11 22:10:24 -040078 if (password.empty()) {
79 int count = 3;
80 while (!getPassword(password, "Passphrase for the private key: ", false)) {
81 count--;
82 if (count <= 0) {
83 std::cerr << "ERROR: invalid password" << std::endl;
84 return 1;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080085 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080086 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040087 }
Alexander Afanasyev634a62b2018-06-15 16:55:26 -040088
Davide Pesavento949075a2021-10-17 22:07:07 -040089 keyChain.importSafeBag(safeBag, password.data(), password.size());
Davide Pesaventob310efb2019-04-11 22:10:24 -040090
91 return 0;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092}
93
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040094} // namespace ndn::ndnsec