Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | 97ee811 | 2020-08-11 21:38:34 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/util/scope.hpp" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | 80d671f | 2022-06-08 04:04:52 -0400 | [diff] [blame] | 27 | #include <openssl/crypto.h> |
| 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | |
| 31 | int |
| 32 | ndnsec_import(int argc, char** argv) |
| 33 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 34 | namespace po = boost::program_options; |
| 35 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 36 | std::string input; |
| 37 | std::string password; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | |
Davide Pesavento | 97ee811 | 2020-08-11 21:38:34 -0400 | [diff] [blame] | 39 | auto guard = make_scope_exit([&password] { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 40 | OPENSSL_cleanse(&password.front(), password.size()); |
Davide Pesavento | 97ee811 | 2020-08-11 21:38:34 -0400 | [diff] [blame] | 41 | }); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 42 | |
| 43 | po::options_description description( |
| 44 | "Usage: ndnsec import [-h] [-P PASSPHRASE] [-i] FILE\n" |
| 45 | "\n" |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame] | 46 | "Import a private key and its certificate from a SafeBag.\n" |
| 47 | "\n" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 48 | "Options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 49 | description.add_options() |
| 50 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 51 | ("input,i", po::value<std::string>(&input)->default_value("-"), |
| 52 | "input file, '-' for stdin (the default)") |
| 53 | ("password,P", po::value<std::string>(&password), |
| 54 | "passphrase, will prompt if empty or not specified") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 55 | ; |
| 56 | |
| 57 | po::positional_options_description p; |
| 58 | p.add("input", 1); |
| 59 | |
| 60 | po::variables_map vm; |
| 61 | try { |
| 62 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 63 | po::notify(vm); |
| 64 | } |
| 65 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 66 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 67 | << description << std::endl; |
| 68 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 71 | if (vm.count("help") > 0) { |
| 72 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 76 | KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 77 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 78 | auto safeBag = loadFromFile<security::SafeBag>(input); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 80 | if (password.empty()) { |
| 81 | int count = 3; |
| 82 | while (!getPassword(password, "Passphrase for the private key: ", false)) { |
| 83 | count--; |
| 84 | if (count <= 0) { |
| 85 | std::cerr << "ERROR: invalid password" << std::endl; |
| 86 | return 1; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 87 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 88 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 89 | } |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 90 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 91 | keyChain.importSafeBag(safeBag, password.data(), password.size()); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 92 | |
| 93 | return 0; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 96 | } // namespace ndn::ndnsec |