Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 476200b | 2017-10-05 12:16:27 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 28 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | ndnsec_export(int argc, char** argv) |
| 30 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | namespace po = boost::program_options; |
| 32 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 33 | Name identityName; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | std::string output; |
| 35 | std::string exportPassword; |
| 36 | |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 37 | po::options_description description("General Usage\n" |
| 38 | " ndnsec export [-h] [-o output] [-P passphrase] identity \n" |
| 39 | "General options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | ("help,h", "Produce help message") |
| 42 | ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 43 | ("identity,i", po::value<Name>(&identityName), "Identity to export") |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 44 | ("password,P", po::value<std::string>(&exportPassword), "Passphrase (will prompt if empty or not specified)") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | ; |
| 46 | |
| 47 | po::positional_options_description p; |
| 48 | p.add("identity", 1); |
| 49 | |
| 50 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 51 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 52 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 53 | po::notify(vm); |
| 54 | } |
| 55 | catch (const std::exception& e) { |
| 56 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 57 | std::cerr << description << std::endl; |
| 58 | return 1; |
| 59 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 61 | if (vm.count("help") != 0) { |
| 62 | std::cerr << description << std::endl; |
| 63 | return 0; |
| 64 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 66 | if (vm.count("identity") == 0) { |
| 67 | std::cerr << "ERROR: identity must be specified" << std::endl; |
| 68 | std::cerr << description << std::endl; |
| 69 | return 1; |
| 70 | } |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 71 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | if (vm.count("output") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 73 | output = "-"; |
| 74 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 75 | try { |
Alexander Afanasyev | 634a62b | 2018-06-15 16:55:26 -0400 | [diff] [blame] | 76 | if (exportPassword.empty()) { |
| 77 | int count = 3; |
| 78 | while (!getPassword(exportPassword, "Passphrase for the private key: ")) { |
| 79 | count--; |
| 80 | if (count <= 0) { |
| 81 | std::cerr << "ERROR: invalid password" << std::endl; |
| 82 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
| 83 | return 1; |
| 84 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | security::v2::KeyChain keyChain; |
| 89 | security::Identity id = keyChain.getPib().getIdentity(identityName); |
| 90 | |
| 91 | // @TODO export all certificates, selected key pair, selected certificate |
| 92 | shared_ptr<security::SafeBag> safeBag = keyChain.exportSafeBag(id.getDefaultKey().getDefaultCertificate(), |
| 93 | exportPassword.c_str(), exportPassword.size()); |
| 94 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 95 | |
| 96 | if (output == "-") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 97 | io::save(*safeBag, std::cout); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 98 | else |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 99 | io::save(*safeBag, output); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 103 | catch (const std::runtime_error& e) { |
| 104 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 105 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
| 106 | return 1; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 107 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 110 | } // namespace ndnsec |
| 111 | } // namespace ndn |